Skip to content

fix: compute field offsets per-class instead of globally#1683

Merged
0utplay merged 2 commits into
nightlyfrom
unsafe-class-offset-compute
Jul 5, 2025
Merged

fix: compute field offsets per-class instead of globally#1683
0utplay merged 2 commits into
nightlyfrom
unsafe-class-offset-compute

Conversation

@derklaro

@derklaro derklaro commented Jul 4, 2025

Copy link
Copy Markdown
Member

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

@derklaro derklaro added this to the 4.0.0-RC14 milestone Jul 4, 2025
@derklaro derklaro requested a review from 0utplay July 4, 2025 20:43
@derklaro derklaro self-assigned this Jul 4, 2025
@derklaro derklaro added v: 4.X This pull should be included in the 4.0 release t: fix A pull request introducing a fix for a bug. in: wrapper An issue/pull request releated to the wrapper module code labels Jul 4, 2025
@github-actions

github-actions Bot commented Jul 4, 2025

Copy link
Copy Markdown

Test Results

 56 files  ±0   56 suites  ±0   2m 42s ⏱️ -14s
570 tests +4  570 ✅ +4  0 💤 ±0  0 ❌ ±0 
935 runs  +4  935 ✅ +4  0 💤 ±0  0 ❌ ±0 

Results for commit d920f64. ± Comparison against base commit ce44537.

@0utplay 0utplay merged commit cf4e1ec into nightly Jul 5, 2025
6 checks passed
@0utplay 0utplay deleted the unsafe-class-offset-compute branch July 5, 2025 13:53
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: wrapper An issue/pull request releated to the wrapper module code t: fix A pull request introducing a fix for a bug. v: 4.X This pull should be included in the 4.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants