Avoid using the copy_nonoverlapping wrapper through mem::replace.
This commit is contained in:
parent
4c29cc8fd0
commit
a1d014bdbc
3 changed files with 47 additions and 30 deletions
25
src/test/codegen/mem-replace-direct-memcpy.rs
Normal file
25
src/test/codegen/mem-replace-direct-memcpy.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// This test ensures that `mem::replace::<T>` only ever calls `@llvm.memcpy`
|
||||
// with `size_of::<T>()` as the size, and never goes through any wrapper that
|
||||
// may e.g. multiply `size_of::<T>()` with a variable "count" (which is only
|
||||
// known to be `1` after inlining).
|
||||
|
||||
// compile-flags: -C no-prepopulate-passes
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
pub fn replace_byte(dst: &mut u8, src: u8) -> u8 {
|
||||
std::mem::replace(dst, src)
|
||||
}
|
||||
|
||||
// NOTE(eddyb) the `CHECK-NOT`s ensure that the only calls of `@llvm.memcpy` in
|
||||
// the entire output, are the two direct calls we want, from `ptr::{read,write}`.
|
||||
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
// CHECK: ; core::ptr::read
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i{{.*}}(i8* align 1 %{{.*}}, i8* align 1 %src, i{{.*}} 1, i1 false)
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
// CHECK: ; core::ptr::write
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
// CHECK: call void @llvm.memcpy.p0i8.p0i8.i{{.*}}(i8* align 1 %dst, i8* align 1 %src, i{{.*}} 1, i1 false)
|
||||
// CHECK-NOT: call void @llvm.memcpy
|
||||
|
|
@ -1,16 +1,11 @@
|
|||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
LL | unsafe { copy_nonoverlapping(src, dst, count) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
LL | copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
|
||||
| --------------------------------------------- inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
::: $DIR/out_of_bounds_read.rs:13:33
|
||||
|
|
||||
|
|
@ -18,18 +13,13 @@ LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) };
|
|||
| ----------------------- inside `_READ` at $DIR/out_of_bounds_read.rs:13:33
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
LL | unsafe { copy_nonoverlapping(src, dst, count) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
LL | copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
|
||||
| --------------------------------------------- inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
::: $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
|
|
@ -42,18 +32,13 @@ LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() };
|
|||
| ------------------- inside `_CONST_READ` at $DIR/out_of_bounds_read.rs:14:39
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
LL | unsafe { copy_nonoverlapping(src, dst, count) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
|
||||
|
|
||||
::: $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
LL | copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
|
||||
| --------------------------------------------- inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
| memory access failed: alloc7 has size 4, so pointer to 4 bytes starting at offset 4 is out-of-bounds
|
||||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
||||
|
|
||||
::: $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL
|
||||
|
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue