Skip to content

Commit 9f7b238

Browse files
authored
Unrolled build for rust-lang#134606
Rollup merge of rust-lang#134606 - RalfJung:ptr-copy-docs, r=Mark-Simulacrum ptr::copy: fix docs for the overlapping case Fixes rust-lang/unsafe-code-guidelines#549 As discussed in that issue, it doesn't make any sense for `copy` to read a byte via `src` after it was already written via `dst`. The entire point of this method is that is copies correctly even if they overlap, and that requires always reading any given location before writing it. Cc `@rust-lang/opsem`
2 parents 42591a4 + 526d298 commit 9f7b238

File tree

2 files changed

+5
-5
lines changed
  • compiler/rustc_const_eval/src/interpret
  • library/core/src/intrinsics

2 files changed

+5
-5
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
13591359
let src_alloc = self.get_alloc_raw(src_alloc_id)?;
13601360
let src_range = alloc_range(src_offset, size);
13611361
assert!(!self.memory.validation_in_progress, "we can't be copying during validation");
1362+
// For the overlapping case, it is crucial that we trigger the read hook
1363+
// before the write hook -- the aliasing model cares about the order.
13621364
M::before_memory_read(
13631365
tcx,
13641366
&self.machine,

library/core/src/intrinsics/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -4364,13 +4364,11 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
43644364
///
43654365
/// Behavior is undefined if any of the following conditions are violated:
43664366
///
4367-
/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes, and must remain valid even
4368-
/// when `dst` is written for `count * size_of::<T>()` bytes. (This means if the memory ranges
4369-
/// overlap, the two pointers must not be subject to aliasing restrictions relative to each
4370-
/// other.)
4367+
/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
43714368
///
43724369
/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes, and must remain valid even
4373-
/// when `src` is read for `count * size_of::<T>()` bytes.
4370+
/// when `src` is read for `count * size_of::<T>()` bytes. (This means if the memory ranges
4371+
/// overlap, the `dst` pointer must not be invalidated by `src` reads.)
43744372
///
43754373
/// * Both `src` and `dst` must be properly aligned.
43764374
///

0 commit comments

Comments
 (0)