Skip to content

Commit

Permalink
Assert OBJECT_REF_OFFSET_LOWER_BOUND invariant
Browse files Browse the repository at this point in the history
Do a `debug_assert` on the invariant of
`ObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND` when calling
`address.to_object_start::<VM>()`.  VM bindings may violate this
invariant when changing their object models.
  • Loading branch information
wks committed Sep 2, 2024
1 parent fa53275 commit 8c7c3fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/util/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,15 @@ impl ObjectReference {
use crate::vm::ObjectModel;
let object_start = VM::VMObjectModel::ref_to_object_start(self);
debug_assert!(!VM::VMObjectModel::UNIFIED_OBJECT_REFERENCE_ADDRESS || object_start == self.to_raw_address(), "The binding claims unified object reference address, but for object reference {}, ref_to_object_start() returns {}", self, object_start);
debug_assert!(
self.to_raw_address()
>= object_start + VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
"The invariant `object_ref >= object_start + OBJECT_REF_OFFSET_LOWER_BOUND` is violated. \
object_ref: {}, object_start: {}, OBJECT_REF_OFFSET_LOWER_BOUND: {}",
self.to_raw_address(),
object_start,
VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
);
object_start
}

Expand Down
3 changes: 3 additions & 0 deletions src/vm/object_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ pub trait ObjectModel<VM: VMBinding> {
/// Return the lowest address of the storage associated with an object. This should be
/// the address that a binding gets by an allocation call ([`crate::memory_manager::alloc`]).
///
/// Note that the return value needs to satisfy the invariant mentioned in the doc comment of
/// [`Self::OBJECT_REF_OFFSET_LOWER_BOUND`].
///
/// Arguments:
/// * `object`: The object to be queried.
fn ref_to_object_start(object: ObjectReference) -> Address;
Expand Down

0 comments on commit 8c7c3fa

Please sign in to comment.