From 7b24d2bb351efeea9c0109d8a662b1038fe8f539 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 2 Aug 2018 11:33:59 +0200 Subject: [PATCH 1/4] debug_assert to ensure that from_raw_parts is only used properly aligned --- src/libcore/slice/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a4dde38cb7bb6..eeb171ebb9b46 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -3878,6 +3878,7 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { + debug_assert!(data as usize % mem::align_of::() == 0, "attempt to create unaligned slice"); Repr { raw: FatPtr { data, len } }.rust } @@ -3891,6 +3892,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] { + debug_assert!(data as usize % mem::align_of::() == 0, "attempt to create unaligned slice"); Repr { raw: FatPtr { data, len} }.rust_mut } From 6fb97a6c86e792552d330c7ab9db6f39ba28efb1 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 2 Aug 2018 16:51:54 +0200 Subject: [PATCH 2/4] test that align_of handles alignment properly for the mid part --- src/libcore/slice/mod.rs | 2 ++ src/libcore/tests/slice.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index eeb171ebb9b46..f9de78c6cdfea 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1785,6 +1785,7 @@ impl [T] { return (self, &[], &[]); } else { let (left, rest) = self.split_at(offset); + // now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay let (us_len, ts_len) = rest.align_to_offsets::(); return (left, from_raw_parts(rest.as_ptr() as *const U, us_len), @@ -1837,6 +1838,7 @@ impl [T] { return (self, &mut [], &mut []); } else { let (left, rest) = self.split_at_mut(offset); + // now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay let (us_len, ts_len) = rest.align_to_offsets::(); let mut_ptr = rest.as_mut_ptr(); return (left, diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 7968521f7b461..b087ec81f59c6 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -986,3 +986,17 @@ fn test_align_to_non_trivial() { assert_eq!(aligned.len(), 4); assert_eq!(prefix.len() + suffix.len(), 2); } + +#[test] +fn test_align_to_empty_mid() { + use core::mem; + + // Make sure that we do not create empty unaligned slices for the mid part, even when the + // overall slice is too short to contain an aligned address. + let bytes = [1, 2, 3, 4, 5, 6, 7]; + type Chunk = u32; + for offset in 0..4 { + let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::() }; + assert_eq!(mid.as_ptr() as usize % mem::align_of::(), 0); + } +} From f47d56a55bdb940cfde1461e4371258728d8f5ad Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 6 Aug 2018 14:06:27 +0200 Subject: [PATCH 3/4] update ripgrep tested by cargotest --- src/tools/cargotest/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/cargotest/main.rs b/src/tools/cargotest/main.rs index d04231bbac089..352480543c7e3 100644 --- a/src/tools/cargotest/main.rs +++ b/src/tools/cargotest/main.rs @@ -33,7 +33,7 @@ const TEST_REPOS: &'static [Test] = &[ Test { name: "ripgrep", repo: "https://github.com/BurntSushi/ripgrep", - sha: "b65bb37b14655e1a89c7cd19c8b011ef3e312791", + sha: "ad9befbc1d3b5c695e7f6b6734ee1b8e683edd41", lock: None, packages: &[], }, From 1001b2beee595db76ae9d612134271e4a971fed8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 6 Aug 2018 18:45:15 +0200 Subject: [PATCH 4/4] inore some codegen tests when debug assertions are enabled --- .travis.yml | 2 ++ src/test/codegen/vec-clear.rs | 1 + src/test/codegen/vec-iter-collect-len.rs | 1 + src/test/codegen/vec-optimizes-away.rs | 1 + src/tools/compiletest/src/header.rs | 6 ++++-- 5 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0228fdc994dd7..16e55ca234868 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,6 +46,8 @@ matrix: # slow to run. # OSX builders running tests, these run the full test suite. + # NO_DEBUG_ASSERTIONS=1 to make them go faster, but also do have some + # runners that run `//ignore-debug` tests. # # Note that the compiler is compiled to target 10.8 here because the Xcode # version that we're using, 8.2, cannot compile LLVM for OSX 10.7. diff --git a/src/test/codegen/vec-clear.rs b/src/test/codegen/vec-clear.rs index a73dd077cea11..c44637376d7aa 100644 --- a/src/test/codegen/vec-clear.rs +++ b/src/test/codegen/vec-clear.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-debug: the debug assertions get in the way // compile-flags: -O #![crate_type = "lib"] diff --git a/src/test/codegen/vec-iter-collect-len.rs b/src/test/codegen/vec-iter-collect-len.rs index efb384d0afbce..05cbf05344411 100644 --- a/src/test/codegen/vec-iter-collect-len.rs +++ b/src/test/codegen/vec-iter-collect-len.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-debug: the debug assertions get in the way // no-system-llvm // compile-flags: -O #![crate_type="lib"] diff --git a/src/test/codegen/vec-optimizes-away.rs b/src/test/codegen/vec-optimizes-away.rs index 261564ed51aed..6bef01fd4ea2d 100644 --- a/src/test/codegen/vec-optimizes-away.rs +++ b/src/test/codegen/vec-optimizes-away.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. // +// ignore-debug: the debug assertions get in the way // no-system-llvm // compile-flags: -O #![crate_type="lib"] diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index 5f68d00eab1d4..3fd67366a8ca4 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -615,12 +615,14 @@ impl Config { common::DebugInfoLldb => name == "lldb", common::Pretty => name == "pretty", _ => false, - } || (self.target != self.host && name == "cross-compile") || + } || + (self.target != self.host && name == "cross-compile") || match self.compare_mode { Some(CompareMode::Nll) => name == "compare-mode-nll", Some(CompareMode::Polonius) => name == "compare-mode-polonius", None => false, - } + } || + (cfg!(debug_assertions) && name == "debug") } else { false }