extend Miri to correctly pass mutable pointers through FFI
Co-authored-by: Ralf Jung <post@ralfj.de>
This commit is contained in:
@@ -238,7 +238,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
let scalar = src.to_scalar();
|
||||
let ptr = scalar.to_pointer(self)?;
|
||||
match ptr.into_pointer_or_addr() {
|
||||
Ok(ptr) => M::expose_ptr(self, ptr)?,
|
||||
Ok(ptr) => M::expose_provenance(self, ptr.provenance)?,
|
||||
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
|
||||
};
|
||||
interp_ok(ImmTy::from_scalar(
|
||||
|
||||
@@ -327,11 +327,11 @@ pub trait Machine<'tcx>: Sized {
|
||||
addr: u64,
|
||||
) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
|
||||
|
||||
/// Marks a pointer as exposed, allowing it's provenance
|
||||
/// Marks a pointer as exposed, allowing its provenance
|
||||
/// to be recovered. "Pointer-to-int cast"
|
||||
fn expose_ptr(
|
||||
ecx: &mut InterpCx<'tcx, Self>,
|
||||
ptr: Pointer<Self::Provenance>,
|
||||
fn expose_provenance(
|
||||
ecx: &InterpCx<'tcx, Self>,
|
||||
provenance: Self::Provenance,
|
||||
) -> InterpResult<'tcx>;
|
||||
|
||||
/// Convert a pointer with provenance into an allocation-offset pair and extra provenance info.
|
||||
|
||||
@@ -944,6 +944,52 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
interp_ok(())
|
||||
}
|
||||
|
||||
/// Handle the effect an FFI call might have on the state of allocations.
|
||||
/// This overapproximates the modifications which external code might make to memory:
|
||||
/// We set all reachable allocations as initialized, mark all provenances as exposed
|
||||
/// and overwrite them with `Provenance::WILDCARD`.
|
||||
pub fn prepare_for_native_call(
|
||||
&mut self,
|
||||
id: AllocId,
|
||||
initial_prov: M::Provenance,
|
||||
) -> InterpResult<'tcx> {
|
||||
// Expose provenance of the root allocation.
|
||||
M::expose_provenance(self, initial_prov)?;
|
||||
|
||||
let mut done = FxHashSet::default();
|
||||
let mut todo = vec![id];
|
||||
while let Some(id) = todo.pop() {
|
||||
if !done.insert(id) {
|
||||
// We already saw this allocation before, don't process it again.
|
||||
continue;
|
||||
}
|
||||
let info = self.get_alloc_info(id);
|
||||
|
||||
// If there is no data behind this pointer, skip this.
|
||||
if !matches!(info.kind, AllocKind::LiveData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Expose all provenances in this allocation, and add them to `todo`.
|
||||
let alloc = self.get_alloc_raw(id)?;
|
||||
for prov in alloc.provenance().provenances() {
|
||||
M::expose_provenance(self, prov)?;
|
||||
if let Some(id) = prov.get_alloc_id() {
|
||||
todo.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare for possible write from native code if mutable.
|
||||
if info.mutbl.is_mut() {
|
||||
self.get_alloc_raw_mut(id)?
|
||||
.0
|
||||
.prepare_for_native_write()
|
||||
.map_err(|e| e.to_interp_error(id))?;
|
||||
}
|
||||
}
|
||||
interp_ok(())
|
||||
}
|
||||
|
||||
/// Create a lazy debug printer that prints the given allocation and all allocations it points
|
||||
/// to, recursively.
|
||||
#[must_use]
|
||||
|
||||
Reference in New Issue
Block a user