Skip to content

Commit 73afc00

Browse files
committedDec 3, 2023
use assume(idx < self.len()) in [T]::get_unchecked
1 parent 9fad685 commit 73afc00

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed
 

‎library/core/src/slice/index.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ unsafe impl<T> SliceIndex<[T]> for usize {
233233
// cannot be longer than `isize::MAX`. They also guarantee that
234234
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
235235
// so the call to `add` is safe.
236-
unsafe { slice.as_ptr().add(self) }
236+
unsafe {
237+
crate::intrinsics::assume(self < slice.len());
238+
slice.as_ptr().add(self)
239+
}
237240
}
238241

239242
#[inline]
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//@compile-flags: -Zmiri-strict-provenance
2-
//@error-in-other-file: /retag .* tag does not exist in the borrow stack/
32

43
fn main() {
54
unsafe {
65
let a = [1, 2, 3];
76
let s = &a[0..0];
87
assert_eq!(s.len(), 0);
9-
assert_eq!(*s.get_unchecked(1), 2);
8+
assert_eq!(*s.as_ptr().add(1), 2); //~ ERROR: /retag .* tag does not exist in the borrow stack/
109
}
1110
}

‎src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
error: Undefined Behavior: trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
2-
--> RUSTLIB/core/src/slice/mod.rs:LL:CC
2+
--> $DIR/zst_slice.rs:LL:CC
33
|
4-
LL | unsafe { &*index.get_unchecked(self) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
8-
| this error occurs as part of retag at ALLOC[0x4..0x8]
4+
LL | assert_eq!(*s.as_ptr().add(1), 2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
8+
| this error occurs as part of retag at ALLOC[0x4..0x8]
99
|
1010
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
1111
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
1212
help: <TAG> would have been created here, but this is a zero-size retag ([0x0..0x0]) so the tag in question does not exist anywhere
1313
--> $DIR/zst_slice.rs:LL:CC
1414
|
15-
LL | assert_eq!(*s.get_unchecked(1), 2);
16-
| ^^^^^^^^^^^^^^^^^^
15+
LL | assert_eq!(*s.as_ptr().add(1), 2);
16+
| ^^^^^^^^^^
1717
= note: BACKTRACE (of the first span):
18-
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at RUSTLIB/core/src/slice/mod.rs:LL:CC
19-
note: inside `main`
20-
--> $DIR/zst_slice.rs:LL:CC
21-
|
22-
LL | assert_eq!(*s.get_unchecked(1), 2);
23-
| ^^^^^^^^^^^^^^^^^^
18+
= note: inside `main` at RUSTLIB/core/src/macros/mod.rs:LL:CC
19+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2420

2521
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
2622

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zmiri-disable-stacked-borrows
22
fn main() {
33
let v: Vec<u8> = Vec::with_capacity(10);
4-
let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR: uninitialized
4+
let undef = unsafe { *v.as_ptr().add(5) }; //~ ERROR: uninitialized
55
let x = undef + 1;
66
panic!("this should never print: {}", x);
77
}

‎src/tools/miri/tests/fail/uninit/uninit_byte_read.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
22
--> $DIR/uninit_byte_read.rs:LL:CC
33
|
4-
LL | let undef = unsafe { *v.get_unchecked(5) };
5-
| ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let undef = unsafe { *v.as_ptr().add(5) };
5+
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

‎src/tools/miri/tests/pass/float_nan.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use NaNKind::*;
2020
#[track_caller]
2121
fn check_all_outcomes<T: Eq + Hash + fmt::Display>(expected: HashSet<T>, generate: impl Fn() -> T) {
2222
let mut seen = HashSet::new();
23-
// Let's give it 8x as many tries as we are expecting values.
24-
let tries = expected.len() * 8;
23+
// Let's give it sixteen times as many tries as we are expecting values.
24+
let tries = expected.len() * 16;
2525
for _ in 0..tries {
2626
let val = generate();
2727
assert!(expected.contains(&val), "got an unexpected value: {val}");

‎tests/codegen/issues/issue-116878.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// no-system-llvm
2+
// compile-flags: -O
3+
// ignore-debug: the debug assertions get in the way
4+
#![crate_type = "lib"]
5+
6+
/// Make sure no bounds checks are emitted after a `get_unchecked`.
7+
// CHECK-LABEL: @unchecked_slice_no_bounds_check
8+
#[no_mangle]
9+
pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 {
10+
let a = *s.get_unchecked(1);
11+
// CHECK-NOT: panic_bounds_check
12+
a + s[0]
13+
}

0 commit comments

Comments
 (0)
Please sign in to comment.