-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #114382 - scottmcm:compare-bytes-intrinsic, r=cjgillot
Add a new `compare_bytes` intrinsic instead of calling `memcmp` directly As discussed in #113435, this lets the backends be the place that can have the "don't call the function if n == 0" logic, if it's needed for the target. (I didn't actually *add* those checks, though, since as I understood it we didn't actually need them on known targets?) Doing this also let me make it `const` (unstable), which I don't think `extern "C" fn memcmp` can be. cc `@RalfJung` `@Amanieu`
- Loading branch information
Showing
17 changed files
with
270 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/tools/miri/tests/fail/uninit_buffer_with_provenance.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// revisions: INT32 INT16 | ||
// compile-flags: -O | ||
// [INT32] ignore-16bit | ||
// [INT16] only-16bit | ||
|
||
#![crate_type = "lib"] | ||
#![feature(core_intrinsics)] | ||
|
||
use std::intrinsics::compare_bytes; | ||
|
||
#[no_mangle] | ||
// CHECK-LABEL: @bytes_cmp( | ||
pub unsafe fn bytes_cmp(a: *const u8, b: *const u8, n: usize) -> i32 { | ||
// INT32: %[[TEMP:.+]] = tail call i32 @memcmp(ptr %a, ptr %b, {{i32|i64}} %n) | ||
// INT32-NOT: sext | ||
// INT32: ret i32 %[[TEMP]] | ||
|
||
// INT16: %[[TEMP1:.+]] = tail call i16 @memcmp(ptr %a, ptr %b, i16 %n) | ||
// INT16: %[[TEMP2:.+]] = sext i16 %[[TEMP1]] to i32 | ||
// INT16: ret i32 %[[TEMP2]] | ||
compare_bytes(a, b, n) | ||
} | ||
|
||
// Ensure that, even though there's an `sext` emitted by the intrinsic, | ||
// that doesn't end up pessiming checks against zero. | ||
#[no_mangle] | ||
// CHECK-LABEL: @bytes_eq( | ||
pub unsafe fn bytes_eq(a: *const u8, b: *const u8, n: usize) -> bool { | ||
// CHECK: call {{.+}} @{{bcmp|memcmp}}(ptr %a, ptr %b, {{i16|i32|i64}} %n) | ||
// CHECK-NOT: sext | ||
// INT32: icmp eq i32 | ||
// INT16: icmp eq i16 | ||
compare_bytes(a, b, n) == 0_i32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// check-fail | ||
|
||
#![feature(core_intrinsics)] | ||
#![feature(const_intrinsic_compare_bytes)] | ||
use std::intrinsics::compare_bytes; | ||
use std::mem::MaybeUninit; | ||
|
||
fn main() { | ||
const LHS_NULL: i32 = unsafe { | ||
compare_bytes(0 as *const u8, 2 as *const u8, 0) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const RHS_NULL: i32 = unsafe { | ||
compare_bytes(1 as *const u8, 0 as *const u8, 0) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const DANGLING_PTR_NON_ZERO_LENGTH: i32 = unsafe { | ||
compare_bytes(1 as *const u8, 2 as *const u8, 1) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const LHS_OUT_OF_BOUNDS: i32 = unsafe { | ||
compare_bytes([1, 2, 3].as_ptr(), [1, 2, 3, 4].as_ptr(), 4) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const RHS_OUT_OF_BOUNDS: i32 = unsafe { | ||
compare_bytes([1, 2, 3, 4].as_ptr(), [1, 2, 3].as_ptr(), 4) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const LHS_UNINIT: i32 = unsafe { | ||
compare_bytes(MaybeUninit::uninit().as_ptr(), [1].as_ptr(), 1) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const RHS_UNINIT: i32 = unsafe { | ||
compare_bytes([1].as_ptr(), MaybeUninit::uninit().as_ptr(), 1) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
const WITH_PROVENANCE: i32 = unsafe { | ||
compare_bytes([&1].as_ptr().cast(), [&2].as_ptr().cast(), std::mem::size_of::<usize>()) | ||
//~^ ERROR evaluation of constant value failed | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:10:9 | ||
| | ||
LL | compare_bytes(0 as *const u8, 2 as *const u8, 0) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: null pointer is a dangling pointer (it has no provenance) | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:14:9 | ||
| | ||
LL | compare_bytes(1 as *const u8, 0 as *const u8, 0) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: null pointer is a dangling pointer (it has no provenance) | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:18:9 | ||
| | ||
LL | compare_bytes(1 as *const u8, 2 as *const u8, 1) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: 0x1[noalloc] is a dangling pointer (it has no provenance) | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:22:9 | ||
| | ||
LL | compare_bytes([1, 2, 3].as_ptr(), [1, 2, 3, 4].as_ptr(), 4) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc6 has size 3, so pointer to 4 bytes starting at offset 0 is out-of-bounds | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:26:9 | ||
| | ||
LL | compare_bytes([1, 2, 3, 4].as_ptr(), [1, 2, 3].as_ptr(), 4) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc13 has size 3, so pointer to 4 bytes starting at offset 0 is out-of-bounds | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:30:9 | ||
| | ||
LL | compare_bytes(MaybeUninit::uninit().as_ptr(), [1].as_ptr(), 1) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reading memory at alloc17[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:34:9 | ||
| | ||
LL | compare_bytes([1].as_ptr(), MaybeUninit::uninit().as_ptr(), 1) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reading memory at alloc25[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/const-compare-bytes-ub.rs:38:9 | ||
| | ||
LL | compare_bytes([&1].as_ptr().cast(), [&2].as_ptr().cast(), std::mem::size_of::<usize>()) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer | ||
| | ||
= help: this code performed an operation that depends on the underlying bytes representing a pointer | ||
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported | ||
|
||
error: aborting due to 8 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Oops, something went wrong.