mmtk/vm/slot.rs
1//! This module provides the trait [`Slot`] and related traits and types which allow VMs to
2//! customize the layout of slots and the behavior of loading and updating object references in
3//! slots.
4
5use std::hash::Hash;
6use std::marker::PhantomData;
7use std::{fmt::Debug, ops::Range};
8
9use atomic::Atomic;
10
11use crate::util::constants::{BYTES_IN_ADDRESS, LOG_BYTES_IN_ADDRESS};
12use crate::util::{Address, ObjectReference};
13
14/// `Slot` is an abstraction for MMTk to load and update object references in memory.
15///
16/// # Slots and the `Slot` trait
17///
18/// In a VM, a slot can contain an object reference or a non-reference value. It can be in an
19/// object (a.k.a. a field), on the stack (i.e. a local variable) or in any other places (such as
20/// global variables). It may have different representations in different VMs. Some VMs put a
21/// direct pointer to an object into a slot, while others may use compressed pointers, tagged
22/// pointers, offsetted pointers, etc. Some VMs (such as JVM) have null references, and others
23/// (such as CRuby and JavaScript engines) can also use tagged bits to represent non-reference
24/// values such as small integers, `true`, `false`, `null` (a.k.a. "none", "nil", etc.),
25/// `undefined`, etc.
26///
27/// In MMTk, the `Slot` trait is intended to abstract out such different representations of
28/// reference fields (compressed, tagged, offsetted, etc.) among different VMs. From MMTk's point
29/// of view, **MMTk only cares about the object reference held inside the slot, but not
30/// non-reference values**, such as `null`, `true`, etc. When the slot is holding an object
31/// reference, we can load the object reference from it, and we can update the object reference in
32/// it after the GC moves the object.
33///
34/// # The `Slot` trait has pointer semantics
35///
36/// A `Slot` value *points to* a slot, and is not the slot itself. In fact, the simplest
37/// implementation of the `Slot` trait ([`SimpleSlot`], see below) can simply contain the address of
38/// the slot.
39///
40/// A `Slot` can be [copied](std::marker::Copy), and the copied `Slot` instance points to the same
41/// slot.
42///
43/// # How to implement `Slot`?
44///
45/// If a reference field of a VM is word-sized and holds the raw pointer to an object, and uses the
46/// 0 word as the null pointer, it can use the default [`SimpleSlot`] we provide. It simply
47/// contains a pointer to a memory location that holds an address.
48///
49/// ```rust
50/// pub struct SimpleSlot {
51/// slot_addr: *mut Atomic<Address>,
52/// }
53/// ```
54///
55/// In other cases, the VM need to implement its own `Slot` instances.
56///
57/// For example:
58/// - The VM uses **compressed pointers** (Compressed OOPs in OpenJDK's terminology), where the
59/// heap size is limited, and a 64-bit pointer is stored in a 32-bit slot.
60/// - The VM uses **tagged pointers**, where some bits of a word are used as metadata while the
61/// rest are used as pointer.
62/// - The VM uses **offsetted pointers**, i.e. the value of the field is an address at an offset
63/// from the [`ObjectReference`] of the target object. Such offsetted pointers are usually used
64/// to represent **interior pointers**, i.e. pointers to an object field, an array element, etc.
65///
66/// If needed, the implementation of `Slot` can contain not only the pointer, but also additional
67/// information. The `OffsetSlot` example below also contains an offset which can be used when
68/// decoding the pointer. See `src/vm/tests/mock_tests/mock_test_slots.rs` for more concrete
69/// examples, such as `CompressedOopSlot` and `TaggedSlot`.
70///
71/// ```rust
72/// pub struct OffsetSlot {
73/// slot_addr: *mut Atomic<Address>,
74/// offset: usize,
75/// }
76/// ```
77///
78/// When loading, `Slot::load` shall load the value from the slot and decode the value into a
79/// regular `ObjectReference` (note that MMTk has specific requirements for `ObjectReference`, such
80/// as being aligned, pointing inside an object, and cannot be null. Please read the doc comments
81/// of [`ObjectReference`] for details). The decoding is VM-specific, but usually involves removing
82/// tag bits and/or adding an offset to the word, and (in the case of compressed pointers) extending
83/// the word size. By doing this conversion, MMTk can implement GC algorithms in a VM-neutral way,
84/// knowing only `ObjectReference`.
85///
86/// When GC moves object, `Slot::store` shall convert the updated `ObjectReference` back to the
87/// slot-specific representation. Compressed pointers remain compressed; tagged pointers preserve
88/// their tag bits; and offsetted pointers keep their offsets.
89///
90/// # Performance notes
91///
92/// The methods of this trait are called on hot paths. Please ensure they have high performance.
93///
94/// The size of the data structure of the `Slot` implementation may affect the performance as well.
95/// During GC, MMTk enqueues `Slot` instances, and its size affects the overhead of copying. If
96/// your `Slot` implementation has multiple fields or uses `enum` for multiple kinds of slots, it
97/// may have extra cost when copying or decoding. You should measure it. If the cost is too much,
98/// you can implement `Slot` with a tagged word. For example, the [mmtk-openjdk] binding uses the
99/// low order bit to encode whether the slot is compressed or not.
100///
101/// [mmtk-openjdk]: https://github.com/mmtk/mmtk-openjdk/blob/master/mmtk/src/slots.rs
102///
103/// # About weak references
104///
105/// This trait only concerns the representation (i.e. the shape) of the slot, not its semantics,
106/// such as whether it holds strong or weak references. Therefore, one `Slot` implementation can be
107/// used for both slots that hold strong references and slots that hold weak references.
108pub trait Slot: Copy + Send + Debug + PartialEq + Eq + Hash {
109 /// Load object reference from the slot.
110 ///
111 /// If the slot is not holding an object reference (For example, if it is holding NULL or a
112 /// tagged non-reference value. See trait-level doc comment.), this method should return
113 /// `None`.
114 ///
115 /// If the slot holds an object reference with tag bits, the returned value shall be the object
116 /// reference with the tag bits removed.
117 fn load(&self) -> Option<ObjectReference>;
118
119 /// Store the object reference `object` into the slot.
120 ///
121 /// If the slot holds an object reference with tag bits, this method must preserve the tag
122 /// bits while updating the object reference so that it points to the forwarded object given by
123 /// the parameter `object`.
124 ///
125 /// FIXME: This design is inefficient for handling object references with tag bits. Consider
126 /// introducing a new updating function to do the load, trace and store in one function.
127 /// See: <https://github.com/mmtk/mmtk-core/issues/1033>
128 ///
129 /// FIXME: This method is currently used by both moving GC algorithms and the subsuming write
130 /// barrier ([`crate::memory_manager::object_reference_write`]). The two reference writing
131 /// operations have different semantics, and need to be implemented differently if the VM
132 /// supports offsetted or tagged references.
133 /// See: <https://github.com/mmtk/mmtk-core/issues/1038>
134 fn store(&self, object: ObjectReference);
135
136 /// Prefetch the slot so that a subsequent `load` will be faster.
137 fn prefetch_load(&self) {
138 // no-op by default
139 }
140
141 /// Prefetch the slot so that a subsequent `store` will be faster.
142 fn prefetch_store(&self) {
143 // no-op by default
144 }
145}
146
147/// A simple slot implementation that represents a word-sized slot which holds the raw address of
148/// an `ObjectReference`, or 0 if it is holding a null reference.
149///
150/// It is the default slot type, and should be suitable for most VMs.
151#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
152#[repr(transparent)]
153pub struct SimpleSlot {
154 slot_addr: *mut Atomic<Address>,
155}
156
157impl SimpleSlot {
158 /// Create a simple slot from an address.
159 ///
160 /// Arguments:
161 /// * `address`: The address in memory where an `ObjectReference` is stored.
162 pub fn from_address(address: Address) -> Self {
163 Self {
164 slot_addr: address.to_mut_ptr(),
165 }
166 }
167
168 /// Get the address of the slot.
169 ///
170 /// Return the address at which the `ObjectReference` is stored.
171 pub fn as_address(&self) -> Address {
172 Address::from_mut_ptr(self.slot_addr)
173 }
174}
175
176unsafe impl Send for SimpleSlot {}
177
178impl Slot for SimpleSlot {
179 fn load(&self) -> Option<ObjectReference> {
180 let addr = unsafe { (*self.slot_addr).load(atomic::Ordering::Relaxed) };
181 ObjectReference::from_raw_address(addr)
182 }
183
184 fn store(&self, object: ObjectReference) {
185 unsafe { (*self.slot_addr).store(object.to_raw_address(), atomic::Ordering::Relaxed) }
186 }
187}
188
189/// For backword compatibility, we let `Address` implement `Slot` with the same semantics as
190/// [`SimpleSlot`] so that existing bindings that use `Address` as `Slot` can continue to work.
191///
192/// However, we should use `SimpleSlot` directly instead of using `Address`. The purpose of the
193/// `Address` type is to represent an address in memory. It is not directly related to fields
194/// that hold references to other objects. Calling `load()` and `store()` on an `Address` does
195/// not indicate how many bytes to load or store, or how to interpret those bytes. On the other
196/// hand, `SimpleSlot` is all about how to access a field that holds a reference represented
197/// simply as an `ObjectReference`. The intention and the semantics are clearer with
198/// `SimpleSlot`.
199impl Slot for Address {
200 fn load(&self) -> Option<ObjectReference> {
201 let addr = unsafe { Address::load(*self) };
202 ObjectReference::from_raw_address(addr)
203 }
204
205 fn store(&self, object: ObjectReference) {
206 unsafe { Address::store(*self, object) }
207 }
208}
209
210#[test]
211fn a_simple_slot_should_have_the_same_size_as_a_pointer() {
212 assert_eq!(
213 std::mem::size_of::<SimpleSlot>(),
214 std::mem::size_of::<*mut libc::c_void>()
215 );
216}
217
218/// A abstract memory slice represents a piece of **heap** memory which may contains many slots.
219pub trait MemorySlice: Send + Debug + PartialEq + Eq + Clone + Hash {
220 /// The associate type to define how to access slots from a memory slice.
221 type SlotType: Slot;
222 /// The associate type to define how to iterate slots in a memory slice.
223 type SlotIterator: Iterator<Item = Self::SlotType>;
224 /// Iterate object slots within the slice. If there are non-reference values in the slice, the iterator should skip them.
225 fn iter_slots(&self) -> Self::SlotIterator;
226 /// The object which this slice belongs to. If we know the object for the slice, we will check the object state (e.g. mature or not), rather than the slice address.
227 /// Normally checking the object and checking the slice does not make a difference, as the slice is part of the object (in terms of memory range). However,
228 /// if a slice is in a different location from the object, the object state and the slice can be hugely different, and providing a proper implementation
229 /// of this method for the owner object is important.
230 fn object(&self) -> Option<ObjectReference>;
231 /// Start address of the memory slice
232 fn start(&self) -> Address;
233 /// Size of the memory slice
234 fn bytes(&self) -> usize;
235 /// Memory copy support
236 fn copy(src: &Self, tgt: &Self);
237}
238
239/// Iterate slots within `Range<Address>`.
240pub struct AddressRangeIterator {
241 cursor: Address,
242 limit: Address,
243}
244
245impl Iterator for AddressRangeIterator {
246 type Item = Address;
247
248 fn next(&mut self) -> Option<Self::Item> {
249 if self.cursor >= self.limit {
250 None
251 } else {
252 let slot = self.cursor;
253 self.cursor += BYTES_IN_ADDRESS;
254 Some(slot)
255 }
256 }
257}
258
259impl MemorySlice for Range<Address> {
260 type SlotType = Address;
261 type SlotIterator = AddressRangeIterator;
262
263 fn iter_slots(&self) -> Self::SlotIterator {
264 AddressRangeIterator {
265 cursor: self.start,
266 limit: self.end,
267 }
268 }
269
270 fn object(&self) -> Option<ObjectReference> {
271 None
272 }
273
274 fn start(&self) -> Address {
275 self.start
276 }
277
278 fn bytes(&self) -> usize {
279 self.end - self.start
280 }
281
282 fn copy(src: &Self, tgt: &Self) {
283 debug_assert_eq!(src.bytes(), tgt.bytes());
284 debug_assert_eq!(
285 src.bytes() & ((1 << LOG_BYTES_IN_ADDRESS) - 1),
286 0,
287 "bytes are not a multiple of words"
288 );
289 // Raw memory copy
290 unsafe {
291 let words = tgt.bytes() >> LOG_BYTES_IN_ADDRESS;
292 let src = src.start().to_ptr::<usize>();
293 let tgt = tgt.start().to_mut_ptr::<usize>();
294 std::ptr::copy(src, tgt, words)
295 }
296 }
297}
298
299/// Memory slice type with empty implementations.
300/// For VMs that do not use the memory slice type.
301#[derive(Debug, PartialEq, Eq, Clone, Hash)]
302pub struct UnimplementedMemorySlice<SL: Slot = SimpleSlot>(PhantomData<SL>);
303
304/// Slot iterator for `UnimplementedMemorySlice`.
305pub struct UnimplementedMemorySliceSlotIterator<SL: Slot>(PhantomData<SL>);
306
307impl<SL: Slot> Iterator for UnimplementedMemorySliceSlotIterator<SL> {
308 type Item = SL;
309
310 fn next(&mut self) -> Option<Self::Item> {
311 unimplemented!()
312 }
313}
314
315impl<SL: Slot> MemorySlice for UnimplementedMemorySlice<SL> {
316 type SlotType = SL;
317 type SlotIterator = UnimplementedMemorySliceSlotIterator<SL>;
318
319 fn iter_slots(&self) -> Self::SlotIterator {
320 unimplemented!()
321 }
322
323 fn object(&self) -> Option<ObjectReference> {
324 unimplemented!()
325 }
326
327 fn start(&self) -> Address {
328 unimplemented!()
329 }
330
331 fn bytes(&self) -> usize {
332 unimplemented!()
333 }
334
335 fn copy(_src: &Self, _tgt: &Self) {
336 unimplemented!()
337 }
338}
339
340#[cfg(test)]
341mod tests {
342 use super::*;
343
344 #[test]
345 fn address_range_iteration() {
346 let src: Vec<usize> = (0..32).collect();
347 let src_slice = Address::from_ptr(&src[0])..Address::from_ptr(&src[0]) + src.len();
348 for (i, v) in src_slice.iter_slots().enumerate() {
349 assert_eq!(i, unsafe { v.load::<usize>() })
350 }
351 }
352
353 #[test]
354 fn memory_copy_on_address_ranges() {
355 let src = [1u8; 32];
356 let mut dst = [0u8; 32];
357 let src_slice = Address::from_ptr(&src[0])..Address::from_ptr(&src[0]) + src.len();
358 let dst_slice =
359 Address::from_mut_ptr(&mut dst[0])..Address::from_mut_ptr(&mut dst[0]) + src.len();
360 MemorySlice::copy(&src_slice, &dst_slice);
361 assert_eq!(dst.iter().sum::<u8>(), src.len() as u8);
362 }
363}