Merge pull request #4018 from YohDeadfall/ecx-name-standardization

Standardized variable names for InterpCx
This commit is contained in:
Ralf Jung 2024-11-08 15:03:24 +00:00 committed by GitHub
commit c8e089edc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -111,8 +111,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Returns the exposed `AllocId` that corresponds to the specified addr, // Returns the exposed `AllocId` that corresponds to the specified addr,
// or `None` if the addr is out of bounds // or `None` if the addr is out of bounds
fn alloc_id_from_addr(&self, addr: u64, size: i64) -> Option<AllocId> { fn alloc_id_from_addr(&self, addr: u64, size: i64) -> Option<AllocId> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let global_state = ecx.machine.alloc_addresses.borrow(); let global_state = this.machine.alloc_addresses.borrow();
assert!(global_state.provenance_mode != ProvenanceMode::Strict); assert!(global_state.provenance_mode != ProvenanceMode::Strict);
// We always search the allocation to the right of this address. So if the size is structly // We always search the allocation to the right of this address. So if the size is structly
@ -134,7 +134,7 @@ fn alloc_id_from_addr(&self, addr: u64, size: i64) -> Option<AllocId> {
// entered for addresses that are not the base address, so even zero-sized // entered for addresses that are not the base address, so even zero-sized
// allocations will get recognized at their base address -- but all other // allocations will get recognized at their base address -- but all other
// allocations will *not* be recognized at their "end" address. // allocations will *not* be recognized at their "end" address.
let size = ecx.get_alloc_info(alloc_id).0; let size = this.get_alloc_info(alloc_id).0;
if offset < size.bytes() { Some(alloc_id) } else { None } if offset < size.bytes() { Some(alloc_id) } else { None }
} }
}?; }?;
@ -142,7 +142,7 @@ fn alloc_id_from_addr(&self, addr: u64, size: i64) -> Option<AllocId> {
// We only use this provenance if it has been exposed. // We only use this provenance if it has been exposed.
if global_state.exposed.contains(&alloc_id) { if global_state.exposed.contains(&alloc_id) {
// This must still be live, since we remove allocations from `int_to_ptr_map` when they get freed. // This must still be live, since we remove allocations from `int_to_ptr_map` when they get freed.
debug_assert!(ecx.is_alloc_live(alloc_id)); debug_assert!(this.is_alloc_live(alloc_id));
Some(alloc_id) Some(alloc_id)
} else { } else {
None None
@ -155,9 +155,9 @@ fn addr_from_alloc_id_uncached(
alloc_id: AllocId, alloc_id: AllocId,
memory_kind: MemoryKind, memory_kind: MemoryKind,
) -> InterpResult<'tcx, u64> { ) -> InterpResult<'tcx, u64> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let mut rng = ecx.machine.rng.borrow_mut(); let mut rng = this.machine.rng.borrow_mut();
let (size, align, kind) = ecx.get_alloc_info(alloc_id); let (size, align, kind) = this.get_alloc_info(alloc_id);
// This is either called immediately after allocation (and then cached), or when // This is either called immediately after allocation (and then cached), or when
// adjusting `tcx` pointers (which never get freed). So assert that we are looking // adjusting `tcx` pointers (which never get freed). So assert that we are looking
// at a live allocation. This also ensures that we never re-assign an address to an // at a live allocation. This also ensures that we never re-assign an address to an
@ -166,12 +166,12 @@ fn addr_from_alloc_id_uncached(
assert!(!matches!(kind, AllocKind::Dead)); assert!(!matches!(kind, AllocKind::Dead));
// This allocation does not have a base address yet, pick or reuse one. // This allocation does not have a base address yet, pick or reuse one.
if ecx.machine.native_lib.is_some() { if this.machine.native_lib.is_some() {
// In native lib mode, we use the "real" address of the bytes for this allocation. // In native lib mode, we use the "real" address of the bytes for this allocation.
// This ensures the interpreted program and native code have the same view of memory. // This ensures the interpreted program and native code have the same view of memory.
let base_ptr = match kind { let base_ptr = match kind {
AllocKind::LiveData => { AllocKind::LiveData => {
if ecx.tcx.try_get_global_alloc(alloc_id).is_some() { if this.tcx.try_get_global_alloc(alloc_id).is_some() {
// For new global allocations, we always pre-allocate the memory to be able use the machine address directly. // For new global allocations, we always pre-allocate the memory to be able use the machine address directly.
let prepared_bytes = MiriAllocBytes::zeroed(size, align) let prepared_bytes = MiriAllocBytes::zeroed(size, align)
.unwrap_or_else(|| { .unwrap_or_else(|| {
@ -185,7 +185,7 @@ fn addr_from_alloc_id_uncached(
.unwrap(); .unwrap();
ptr ptr
} else { } else {
ecx.get_alloc_bytes_unchecked_raw(alloc_id)? this.get_alloc_bytes_unchecked_raw(alloc_id)?
} }
} }
AllocKind::Function | AllocKind::VTable => { AllocKind::Function | AllocKind::VTable => {
@ -204,10 +204,10 @@ fn addr_from_alloc_id_uncached(
} }
// We are not in native lib mode, so we control the addresses ourselves. // We are not in native lib mode, so we control the addresses ourselves.
if let Some((reuse_addr, clock)) = if let Some((reuse_addr, clock)) =
global_state.reuse.take_addr(&mut *rng, size, align, memory_kind, ecx.active_thread()) global_state.reuse.take_addr(&mut *rng, size, align, memory_kind, this.active_thread())
{ {
if let Some(clock) = clock { if let Some(clock) = clock {
ecx.acquire_clock(&clock); this.acquire_clock(&clock);
} }
interp_ok(reuse_addr) interp_ok(reuse_addr)
} else { } else {
@ -230,7 +230,7 @@ fn addr_from_alloc_id_uncached(
.checked_add(max(size.bytes(), 1)) .checked_add(max(size.bytes(), 1))
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?; .ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
// Even if `Size` didn't overflow, we might still have filled up the address space. // Even if `Size` didn't overflow, we might still have filled up the address space.
if global_state.next_base_addr > ecx.target_usize_max() { if global_state.next_base_addr > this.target_usize_max() {
throw_exhaust!(AddressSpaceFull); throw_exhaust!(AddressSpaceFull);
} }
@ -243,8 +243,8 @@ fn addr_from_alloc_id(
alloc_id: AllocId, alloc_id: AllocId,
memory_kind: MemoryKind, memory_kind: MemoryKind,
) -> InterpResult<'tcx, u64> { ) -> InterpResult<'tcx, u64> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let mut global_state = ecx.machine.alloc_addresses.borrow_mut(); let mut global_state = this.machine.alloc_addresses.borrow_mut();
let global_state = &mut *global_state; let global_state = &mut *global_state;
match global_state.base_addr.get(&alloc_id) { match global_state.base_addr.get(&alloc_id) {
@ -283,22 +283,22 @@ fn addr_from_alloc_id(
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {} impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
fn expose_ptr(&mut self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> { fn expose_ptr(&mut self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> {
let ecx = self.eval_context_mut(); let this = self.eval_context_mut();
let global_state = ecx.machine.alloc_addresses.get_mut(); let global_state = this.machine.alloc_addresses.get_mut();
// In strict mode, we don't need this, so we can save some cycles by not tracking it. // In strict mode, we don't need this, so we can save some cycles by not tracking it.
if global_state.provenance_mode == ProvenanceMode::Strict { if global_state.provenance_mode == ProvenanceMode::Strict {
return interp_ok(()); return interp_ok(());
} }
// Exposing a dead alloc is a no-op, because it's not possible to get a dead allocation // Exposing a dead alloc is a no-op, because it's not possible to get a dead allocation
// via int2ptr. // via int2ptr.
if !ecx.is_alloc_live(alloc_id) { if !this.is_alloc_live(alloc_id) {
return interp_ok(()); return interp_ok(());
} }
trace!("Exposing allocation id {alloc_id:?}"); trace!("Exposing allocation id {alloc_id:?}");
let global_state = ecx.machine.alloc_addresses.get_mut(); let global_state = this.machine.alloc_addresses.get_mut();
global_state.exposed.insert(alloc_id); global_state.exposed.insert(alloc_id);
if ecx.machine.borrow_tracker.is_some() { if this.machine.borrow_tracker.is_some() {
ecx.expose_tag(alloc_id, tag)?; this.expose_tag(alloc_id, tag)?;
} }
interp_ok(()) interp_ok(())
} }
@ -306,8 +306,8 @@ fn expose_ptr(&mut self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> {
fn ptr_from_addr_cast(&self, addr: u64) -> InterpResult<'tcx, Pointer> { fn ptr_from_addr_cast(&self, addr: u64) -> InterpResult<'tcx, Pointer> {
trace!("Casting {:#x} to a pointer", addr); trace!("Casting {:#x} to a pointer", addr);
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let global_state = ecx.machine.alloc_addresses.borrow(); let global_state = this.machine.alloc_addresses.borrow();
// Potentially emit a warning. // Potentially emit a warning.
match global_state.provenance_mode { match global_state.provenance_mode {
@ -319,9 +319,9 @@ fn ptr_from_addr_cast(&self, addr: u64) -> InterpResult<'tcx, Pointer> {
} }
PAST_WARNINGS.with_borrow_mut(|past_warnings| { PAST_WARNINGS.with_borrow_mut(|past_warnings| {
let first = past_warnings.is_empty(); let first = past_warnings.is_empty();
if past_warnings.insert(ecx.cur_span()) { if past_warnings.insert(this.cur_span()) {
// Newly inserted, so first time we see this span. // Newly inserted, so first time we see this span.
ecx.emit_diagnostic(NonHaltingDiagnostic::Int2Ptr { details: first }); this.emit_diagnostic(NonHaltingDiagnostic::Int2Ptr { details: first });
} }
}); });
} }
@ -347,19 +347,19 @@ fn adjust_alloc_root_pointer(
tag: BorTag, tag: BorTag,
kind: MemoryKind, kind: MemoryKind,
) -> InterpResult<'tcx, interpret::Pointer<Provenance>> { ) -> InterpResult<'tcx, interpret::Pointer<Provenance>> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance) let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
let alloc_id = prov.alloc_id(); let alloc_id = prov.alloc_id();
// Get a pointer to the beginning of this allocation. // Get a pointer to the beginning of this allocation.
let base_addr = ecx.addr_from_alloc_id(alloc_id, kind)?; let base_addr = this.addr_from_alloc_id(alloc_id, kind)?;
let base_ptr = interpret::Pointer::new( let base_ptr = interpret::Pointer::new(
Provenance::Concrete { alloc_id, tag }, Provenance::Concrete { alloc_id, tag },
Size::from_bytes(base_addr), Size::from_bytes(base_addr),
); );
// Add offset with the right kind of pointer-overflowing arithmetic. // Add offset with the right kind of pointer-overflowing arithmetic.
interp_ok(base_ptr.wrapping_offset(offset, ecx)) interp_ok(base_ptr.wrapping_offset(offset, this))
} }
// This returns some prepared `MiriAllocBytes`, either because `addr_from_alloc_id` reserved // This returns some prepared `MiriAllocBytes`, either because `addr_from_alloc_id` reserved
@ -371,16 +371,16 @@ fn get_global_alloc_bytes(
bytes: &[u8], bytes: &[u8],
align: Align, align: Align,
) -> InterpResult<'tcx, MiriAllocBytes> { ) -> InterpResult<'tcx, MiriAllocBytes> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
if ecx.machine.native_lib.is_some() { if this.machine.native_lib.is_some() {
// In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`. // In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`.
// This additional call ensures that some `MiriAllocBytes` are always prepared, just in case // This additional call ensures that some `MiriAllocBytes` are always prepared, just in case
// this function gets called before the first time `addr_from_alloc_id` gets called. // this function gets called before the first time `addr_from_alloc_id` gets called.
ecx.addr_from_alloc_id(id, kind)?; this.addr_from_alloc_id(id, kind)?;
// The memory we need here will have already been allocated during an earlier call to // The memory we need here will have already been allocated during an earlier call to
// `addr_from_alloc_id` for this allocation. So don't create a new `MiriAllocBytes` here, instead // `addr_from_alloc_id` for this allocation. So don't create a new `MiriAllocBytes` here, instead
// fetch the previously prepared bytes from `prepared_alloc_bytes`. // fetch the previously prepared bytes from `prepared_alloc_bytes`.
let mut global_state = ecx.machine.alloc_addresses.borrow_mut(); let mut global_state = this.machine.alloc_addresses.borrow_mut();
let mut prepared_alloc_bytes = global_state let mut prepared_alloc_bytes = global_state
.prepared_alloc_bytes .prepared_alloc_bytes
.remove(&id) .remove(&id)
@ -403,7 +403,7 @@ fn ptr_get_alloc(
ptr: interpret::Pointer<Provenance>, ptr: interpret::Pointer<Provenance>,
size: i64, size: i64,
) -> Option<(AllocId, Size)> { ) -> Option<(AllocId, Size)> {
let ecx = self.eval_context_ref(); let this = self.eval_context_ref();
let (tag, addr) = ptr.into_parts(); // addr is absolute (Tag provenance) let (tag, addr) = ptr.into_parts(); // addr is absolute (Tag provenance)
@ -411,15 +411,15 @@ fn ptr_get_alloc(
alloc_id alloc_id
} else { } else {
// A wildcard pointer. // A wildcard pointer.
ecx.alloc_id_from_addr(addr.bytes(), size)? this.alloc_id_from_addr(addr.bytes(), size)?
}; };
// This cannot fail: since we already have a pointer with that provenance, adjust_alloc_root_pointer // This cannot fail: since we already have a pointer with that provenance, adjust_alloc_root_pointer
// must have been called in the past, so we can just look up the address in the map. // must have been called in the past, so we can just look up the address in the map.
let base_addr = *ecx.machine.alloc_addresses.borrow().base_addr.get(&alloc_id).unwrap(); let base_addr = *this.machine.alloc_addresses.borrow().base_addr.get(&alloc_id).unwrap();
// Wrapping "addr - base_addr" // Wrapping "addr - base_addr"
let rel_offset = ecx.truncate_to_target_usize(addr.bytes().wrapping_sub(base_addr)); let rel_offset = this.truncate_to_target_usize(addr.bytes().wrapping_sub(base_addr));
Some((alloc_id, Size::from_bytes(rel_offset))) Some((alloc_id, Size::from_bytes(rel_offset)))
} }
} }