Skip to content

Commit b3385b8

Browse files
authored
Assert OBJECT_REF_OFFSET_LOWER_BOUND invariant (#1194)
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.
1 parent 61987ea commit b3385b8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/util/address.rs

+9
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,15 @@ impl ObjectReference {
568568
use crate::vm::ObjectModel;
569569
let object_start = VM::VMObjectModel::ref_to_object_start(self);
570570
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);
571+
debug_assert!(
572+
self.to_raw_address()
573+
>= object_start + VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
574+
"The invariant `object_ref >= object_start + OBJECT_REF_OFFSET_LOWER_BOUND` is violated. \
575+
object_ref: {}, object_start: {}, OBJECT_REF_OFFSET_LOWER_BOUND: {}",
576+
self.to_raw_address(),
577+
object_start,
578+
VM::VMObjectModel::OBJECT_REF_OFFSET_LOWER_BOUND,
579+
);
571580
object_start
572581
}
573582

src/vm/object_model.rs

+3
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ pub trait ObjectModel<VM: VMBinding> {
457457
/// Return the lowest address of the storage associated with an object. This should be
458458
/// the address that a binding gets by an allocation call ([`crate::memory_manager::alloc`]).
459459
///
460+
/// Note that the return value needs to satisfy the invariant mentioned in the doc comment of
461+
/// [`Self::OBJECT_REF_OFFSET_LOWER_BOUND`].
462+
///
460463
/// Arguments:
461464
/// * `object`: The object to be queried.
462465
fn ref_to_object_start(object: ObjectReference) -> Address;

0 commit comments

Comments
 (0)