-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a new compare_bytes
intrinsic instead of calling memcmp
directly
#114382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -261,6 +261,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | |||||||
sym::write_bytes => { | ||||||||
self.write_bytes_intrinsic(&args[0], &args[1], &args[2])?; | ||||||||
} | ||||||||
sym::compare_bytes => { | ||||||||
let result = self.compare_bytes_intrinsic(&args[0], &args[1], &args[2])?; | ||||||||
self.write_scalar(result, dest)?; | ||||||||
} | ||||||||
sym::arith_offset => { | ||||||||
let ptr = self.read_pointer(&args[0])?; | ||||||||
let offset_count = self.read_target_isize(&args[1])?; | ||||||||
|
@@ -643,6 +647,24 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | |||||||
self.write_bytes_ptr(dst, bytes) | ||||||||
} | ||||||||
|
||||||||
pub(crate) fn compare_bytes_intrinsic( | ||||||||
&mut self, | ||||||||
left: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>, | ||||||||
right: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>, | ||||||||
byte_count: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>, | ||||||||
) -> InterpResult<'tcx, Scalar<M::Provenance>> { | ||||||||
let left = self.read_pointer(left)?; | ||||||||
let right = self.read_pointer(right)?; | ||||||||
let n = Size::from_bytes(self.read_target_usize(byte_count)?); | ||||||||
|
||||||||
let left_bytes = self.read_bytes_ptr_strip_provenance(left, n)?; | ||||||||
let right_bytes = self.read_bytes_ptr_strip_provenance(right, n)?; | ||||||||
|
||||||||
// `Ordering`'s discriminants are -1/0/+1, so casting does the right thing. | ||||||||
let result = Ord::cmp(left_bytes, right_bytes) as i32; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
Ok(Scalar::from_i32(result)) | ||||||||
} | ||||||||
|
||||||||
pub(crate) fn raw_eq_intrinsic( | ||||||||
&mut self, | ||||||||
lhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>, | ||||||||
|
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 | ||
} |
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 | ||
}; | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test that involves provenance? As in, pass in a buffer that contains pointers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like |
||
|
||
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`. |
Uh oh!
There was an error while loading. Please reload this page.