Skip to content

Commit ac7dfca

Browse files
committed
Fix #313 by correctly copying relocations when doing overlapping copies
1 parent 7355a1e commit ac7dfca

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/librustc_mir/interpret/memory.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,17 @@ impl<'a, 'tcx, M: Machine<'tcx>> Memory<'a, 'tcx, M> {
10881088
let dest = dest.to_ptr()?;
10891089
self.check_relocation_edges(src, size)?;
10901090

1091+
// first copy the relocations to a temporary buffer, because
1092+
// `get_bytes_mut` will clear the relocations, which is correct,
1093+
// since we don't want to keep any relocations at the target.
1094+
1095+
let relocations: Vec<_> = self.relocations(src, size)?
1096+
.map(|(&offset, &alloc_id)| {
1097+
// Update relocation offsets for the new positions in the destination allocation.
1098+
(offset + dest.offset - src.offset, alloc_id)
1099+
})
1100+
.collect();
1101+
10911102
let src_bytes = self.get_bytes_unchecked(src, size, align)?.as_ptr();
10921103
let dest_bytes = self.get_bytes_mut(dest, size, align)?.as_mut_ptr();
10931104

@@ -1113,7 +1124,8 @@ impl<'a, 'tcx, M: Machine<'tcx>> Memory<'a, 'tcx, M> {
11131124
}
11141125

11151126
self.copy_undef_mask(src, dest, size)?;
1116-
self.copy_relocations(src, dest, size)?;
1127+
// copy back the relocations
1128+
self.get_mut(dest.alloc_id)?.relocations.extend(relocations);
11171129

11181130
Ok(())
11191131
}
@@ -1388,22 +1400,6 @@ impl<'a, 'tcx, M: Machine<'tcx>> Memory<'a, 'tcx, M> {
13881400
}
13891401
Ok(())
13901402
}
1391-
1392-
fn copy_relocations(
1393-
&mut self,
1394-
src: MemoryPointer,
1395-
dest: MemoryPointer,
1396-
size: u64,
1397-
) -> EvalResult<'tcx> {
1398-
let relocations: Vec<_> = self.relocations(src, size)?
1399-
.map(|(&offset, &alloc_id)| {
1400-
// Update relocation offsets for the new positions in the destination allocation.
1401-
(offset + dest.offset - src.offset, alloc_id)
1402-
})
1403-
.collect();
1404-
self.get_mut(dest.alloc_id)?.relocations.extend(relocations);
1405-
Ok(())
1406-
}
14071403
}
14081404

14091405
/// Undefined bytes

0 commit comments

Comments
 (0)