fix: compute field offsets per-class instead of globally#1683
Merged
Conversation
0utplay
approved these changes
Jul 5, 2025
0utplay
pushed a commit
that referenced
this pull request
Jul 5, 2025
### Motivation Currently our unsafe replacement implementation computes field offsets using a global long counter (which should be fine as the documentation in `sun.misc.Unsafe` is not specifying how field offsets are resolved). However, there are cases where libraries depend on the fact that the returned offset is rather small and computed based on the class tree. This is for example the case in the google protobuf implementation, which allow a max field offset value of `(1 << 20) - 1` (1,048.575) as they do some optimizations using bit packing. ### Modification Compute field offsets based on the class hierachy instead of globally and provide values that are much closer to the values that are actually returned by `sun.misc.Unsafe`. ### Result No more breaking library code because of bad assumptions about the return value of field offset getter methods. ##### Other context Code that packs the field offset and breaks: https://github.com/protocolbuffers/protobuf/blob/960e79087b332583c80537c949621108a85aa442/java/core/src/main/java/com/google/protobuf/MessageSchema.java#L568-L575 (cherry picked from commit cf4e1ec)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Currently our unsafe replacement implementation computes field offsets using a global long counter (which should be fine as the documentation in
sun.misc.Unsafeis not specifying how field offsets are resolved). However, there are cases where libraries depend on the fact that the returned offset is rather small and computed based on the class tree. This is for example the case in the google protobuf implementation, which allow a max field offset value of(1 << 20) - 1(1,048.575) as they do some optimizations using bit packing.Modification
Compute field offsets based on the class hierachy instead of globally and provide values that are much closer to the values that are actually returned by
sun.misc.Unsafe.Result
No more breaking library code because of bad assumptions about the return value of field offset getter methods.
Other context
Code that packs the field offset and breaks: https://github.com/protocolbuffers/protobuf/blob/960e79087b332583c80537c949621108a85aa442/java/core/src/main/java/com/google/protobuf/MessageSchema.java#L568-L575