You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Require ObjectReference to point inside object (#1195)
Require the raw address of `ObjectReference` to be within the address
range of the object it refers to. The raw address is now used directly
for side metadata access and SFT dispatching. This makes "in-object
address" unnecessary, and we removed the concept of "in-object address"
and related constants and methods.
Methods which use the "in-object address" for SFT dispatching or
side-metadata access used to have a `<VM: VMBinding>` type parameter.
This PR removes that type parameter.
Because `ObjectReference` is now both within an object an word-aligned,
the algorithm for searching for VO bits from internal pointers is
slightly simplified. The method `is_mmtk_object` now has undefined
behavior for arguments that are zero or misaligned because they are
obviously illegal addresses for `ObjectReference`, and the user should
have filtered them out in the first place.
Fixes: #1170
Copy file name to clipboardExpand all lines: docs/userguide/src/portingguide/howto/nogc.md
+44-10
Original file line number
Diff line number
Diff line change
@@ -95,13 +95,39 @@ We recommend going through the [list of metadata specifications](https://docs.mm
95
95
96
96
#### `ObjectReference` vs `Address`
97
97
98
-
A key principle in MMTk is the distinction between [`ObjectReference`](https://docs.mmtk.io/api/mmtk/util/address/struct.ObjectReference.html) and [`Address`](https://docs.mmtk.io/api/mmtk/util/address/struct.Address.html). The idea is that very few operations are allowed on an `ObjectReference`. For example, MMTk does not allow address arithmetic on `ObjectReference`s. This allows us to preserve memory-safety, only performing unsafe operations when required, and gives us a cleaner and more flexible abstraction to work with as it can allow object handles or offsets etc. `Address`, on the other hand, represents an arbitrary machine address. You might be interested in reading the *Demystifying Magic: High-level Low-level Programming* paper[^3] which describes the above in more detail.
99
-
100
-
In MMTk, `ObjectReference` is a special address that represents an object. A binding may use tagged references, compressed pointers, etc.
101
-
They need to deal with the encoding and the decoding in their [`Slot`](https://docs.mmtk.io/api/mmtk/vm/slot/trait.Slot.html) implementation,
102
-
and always present plain `ObjectReference`s to MMTk. See [this test](https://github.com/mmtk/mmtk-core/blob/master/src/vm/tests/mock_tests/mock_test_slots.rs) for some `Slot` implementation examples.
A key principle in MMTk is the distinction between [`ObjectReference`](https://docs.mmtk.io/api/mmtk/util/address/struct.ObjectReference.html) and [`Address`](https://docs.mmtk.io/api/mmtk/util/address/struct.Address.html). The idea is that very few operations are allowed on an `ObjectReference`. For example, MMTk does not allow address arithmetic on `ObjectReference`s. This allows us to preserve memory-safety, only performing unsafe operations when required, and gives us a cleaner and more flexible abstraction to work with as it can allow object handles or offsets etc. `Address`, on the other hand, represents an arbitrary machine address. You might be interested in reading the [*Demystifying Magic: High-level Low-level Programming*][FBC09] paper which describes the above in more detail.
99
+
100
+
In MMTk, `ObjectReference` is a special address that represents an object. It is required to be
101
+
within the address range of the object it refers to, and must be word-aligned. This address is used
102
+
by MMTk to access side metadata, and find the space or regions (chunk, block, line, etc.) that
103
+
contains the object. It must also be efficient to locate the object header (where in-header MMTk
104
+
metadata are held) and the object's VM-specific metadata, such as type information, from a given
105
+
`ObjectReference`. MMTk will need to access those information, either directly or indirectly via
106
+
traits implemented by the binding, during tracing, which is performance-critical.
107
+
108
+
The address used as `ObjectReference` is nominated by the VM binding when an object is allocated (or
109
+
moved by a moving GC, which we can ignore for now when supporting NoGC). VMs usually have their own
110
+
concepts of "object reference" which refer to objects. Some of them, including OpenJDK and CRuby,
111
+
uses addresses to the object (the starting address or at an offset within the object) to refer to an
112
+
object. Such VMs can directly use their "object reference" for the address of MMTk's
113
+
`ObjectReference`.
114
+
115
+
Some VMs, such as JikesRVM, refers to an object by an address at a constant offset after the header,
116
+
and can be outside the object. This does not satisfy the requirement of MMTk's `ObjectReference`,
117
+
and the VM binding needs to make a clear distinction between the VM-level object reference and
118
+
MMTk's `ObjectReference` type. A detailed example for supporting such a VM can be found
119
+
[here][jikesrvm-objref].
120
+
121
+
Other VMs may use tagged references, compressed pointers, etc. They need to convert them to plain
122
+
addresses to be used as MMTk's `ObjectReference`. Specifically, if the VM use such representations
123
+
in object fields, the VM binding can deal with the encoding and the decoding in its
124
+
[`Slot`][slot-trait] implementation, and always present plain `ObjectReference`s to MMTk. See [this
* @param mutator the mutator instance that is requesting the allocation
264
-
* @param object the returned address of the allocated object
290
+
* @param object the ObjectReference address chosen by the VM binding
265
291
* @param size the size of the allocated object
266
292
* @param allocator the allocation semantics to use for the allocation
267
293
*/
@@ -274,13 +300,21 @@ In order to perform allocations, you will need to know what object alignment the
274
300
275
301
Now that MMTk is aware of each mutator thread, you have to change the runtime's allocation functions to call into MMTk to allocate using `mmtk_alloc` and set object metadata using `mmtk_post_alloc`. Note that there may be multiple allocation functions in the runtime so make sure that you edit them all!
276
302
277
-
You should use the saved `Mutator` pointer as the first parameter, the requested object size as the next parameter, and any alignment requirements the runtimes has as the third parameter.
303
+
When calling `mmtk_alloc`, you should use the saved `Mutator` pointer as the first parameter, the requested object size as the next parameter, and any alignment requirements the runtimes has as the third parameter.
278
304
279
305
If your runtime requires a non-zero allocation offset (i.e. the alignment requirements are for the offset address, not the returned address) then you have to provide the required value as the fourth parameter. Note that you ***must*** also update the [`USE_ALLOCATION_OFFSET`](https://docs.mmtk.io/api/mmtk/vm/trait.VMBinding.html#associatedconstant.USE_ALLOCATION_OFFSET) constant in the `VMBinding` implementation if your runtime requires a non-zero allocation offset.
280
306
281
307
For the time-being, you can ignore the `allocator` parameter in both these functions and always pass a value of `0` which means MMTk will pick the default allocator for your collector (a bump pointer allocator in the case of NoGC).
282
308
283
-
Finally, you need to call `mmtk_post_alloc` with the object address returned from the previous `mmtk_alloc` call in order to initialize object metadata.
309
+
The return value of `mmtk_alloc` is the starting address of the allocated object.
310
+
311
+
Then you should nominate a word-aligned address within the allocated bytes to be the
312
+
`ObjectReference` used to refer to that object from now on. It doesn't have to be the starting
313
+
address.
314
+
315
+
Finally, you need to call `mmtk_post_alloc` with your chosen `ObjectReference` in order to
316
+
initialize MMTk-level object metadata, such as logging bits, valid-object (VO) bits, etc. As a VM
317
+
binding developer, you can ignore the details for now.
284
318
285
319
**Note:** Currently MMTk assumes object sizes are multiples of the `MIN_ALIGNMENT`. If you encounter errors with alignment, a simple workaround would be to align the requested object size up to the `MIN_ALIGNMENT`. See [here](https://github.com/mmtk/mmtk-core/issues/730) for the tracking issue to fix this bug.
0 commit comments