Skip to content

Commit 2826ff4

Browse files
committed
first code review fixes
1 parent e383a47 commit 2826ff4

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -950,36 +950,37 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
950950
initial_prov: M::Provenance,
951951
) -> InterpResult<'tcx> {
952952
// Expose provenance of the root allocation.
953-
M::expose_provenance(self, initial_prov)?; // TODO: Is this the right way to expose provenance?
953+
M::expose_provenance(self, initial_prov)?;
954954

955955
let mut done = rustc_data_structures::fx::FxHashSet::default();
956956
let mut todo = vec![id];
957957
while let Some(id) = todo.pop() {
958-
if done.insert(id) {
959-
// This is a new allocation, add the allocation it points to `todo`.
960-
let info = self.get_alloc_info(id);
958+
if !done.insert(id) {
959+
continue;
960+
}
961+
// This is a new allocation, add the allocations it points to to `todo`.
962+
let info = self.get_alloc_info(id);
961963

962-
// If there is no data behind this pointer, skip this.
963-
if !matches!(info.kind, AllocKind::LiveData) {
964-
continue;
965-
}
964+
// If there is no data behind this pointer, skip this.
965+
if !matches!(info.kind, AllocKind::LiveData) {
966+
continue;
967+
}
966968

967-
let alloc = self.get_alloc_raw(id)?;
968-
for prov in alloc.provenance().provenances() {
969-
//M::expose_provenance(self, prov)?; // TODO: Is this the right way to expose provenance? + mutable borrow here gives issues due to provenances iterator lifetime...
970-
if let Some(id) = prov.get_alloc_id() {
971-
todo.push(id);
972-
}
969+
let alloc = self.get_alloc_raw(id)?;
970+
for prov in alloc.provenance().provenances() {
971+
//M::expose_provenance(self, prov)?; // TODO: mutable borrow here gives issues due to provenances iterator lifetime...
972+
if let Some(id) = prov.get_alloc_id() {
973+
todo.push(id);
973974
}
975+
}
974976

975-
// Prepare for possible write from native code if mutable.
976-
if info.mutbl.is_mut() {
977-
let tcx = self.tcx;
978-
self.get_alloc_raw_mut(id)?
979-
.0
980-
.prepare_for_native_call(&tcx)
981-
.map_err(|e| e.to_interp_error(id))?;
982-
}
977+
// Prepare for possible write from native code if mutable.
978+
if info.mutbl.is_mut() {
979+
let tcx = self.tcx;
980+
self.get_alloc_raw_mut(id)?
981+
.0
982+
.prepare_for_native_call(&tcx)
983+
.map_err(|e| e.to_interp_error(id))?;
983984
}
984985
}
985986
interp_ok(())

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
644644
}
645645

646646
/// Initialize all previously uninitialized bytes in the entire allocation, and set
647-
/// provenance of everything to `Wildcard`
647+
/// provenance of everything to `Wildcard`. Before calling this, make sure all
648+
/// provenance in this allocation is exposed!
648649
pub fn prepare_for_native_call(&mut self, cx: &impl HasDataLayout) -> AllocResult {
649650
let full_range = AllocRange { start: Size::ZERO, size: Size::from_bytes(self.len()) };
650651
// Overwrite uninitialized bytes.

src/tools/miri/src/shims/native_lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
154154
}
155155
let imm = this.read_immediate(arg)?;
156156
libffi_args.push(imm_to_carg(&imm, this)?);
157+
// If we are passing a pointer, prepare the memory it points to.
157158
if matches!(arg.layout.ty.kind(), ty::RawPtr(..)) {
158159
let ptr = imm.to_scalar().to_pointer(this)?;
159160
let Some(prov) = ptr.provenance else {
160-
// Pointer without provenance may access any memory.
161+
// Pointer without provenance may not access any memory.
161162
continue;
162163
};
163164
// We use `get_alloc_id` for its best-effort behaviour with Wildcard provenance.
164165
let Some(alloc_id) = prov.get_alloc_id() else {
165-
// Pointer without provenance may access any memory.
166+
// Wildcard pointer, whatever it points to must be already exposed.
166167
continue;
167168
};
168169
this.prepare_for_native_call(alloc_id, prov)?;

src/tools/miri/tests/native-lib/pass/ptr_write_access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ fn test_dangling() {
8888
fn write_nullptr(pptr: *mut *const i32);
8989
}
9090

91-
let x = 71;
92-
let mut ptr = &raw const x;
91+
let x = Box::new(71);
92+
let mut ptr = x.into_raw();
9393
drop(x);
9494
unsafe { write_nullptr(&mut ptr) };
9595
assert_eq!(ptr, std::ptr::null());

0 commit comments

Comments
 (0)