Skip to content

Avoid pointer offsets for JIT relocation targets#13772

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
HueCodes:hue-issue-7588
Jun 30, 2026
Merged

Avoid pointer offsets for JIT relocation targets#13772
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
HueCodes:hue-issue-7588

Conversation

@HueCodes

Copy link
Copy Markdown
Contributor

Fixes #7588.

CompiledBlob::perform_relocations` computed relocation targets with unsafe pointer offsets, then cast those pointers back to integers for relocation math.

This computes checked numeric target addresses up front with ptr.addr().checked_add_signed(...). The relocation math stays the same and unlike #8041 this does not use wrapping_offset.

Tested with cargo test -p cranelift-jit, cargo clippy -p cranelift-jit --all-targets --all-features -- -D warnings, and Cranelift filetests.

Signed-off-by: Hugh Ryan <197298026+HueCodes@users.noreply.github.com>
@HueCodes HueCodes requested a review from a team as a code owner June 30, 2026 18:23
@HueCodes HueCodes requested review from alexcrichton and removed request for a team June 30, 2026 18:23
@github-actions github-actions Bot added the cranelift Issues related to the Cranelift code generator label Jun 30, 2026
@alexcrichton alexcrichton added this pull request to the merge queue Jun 30, 2026
Merged via the queue into bytecodealliance:main with commit 4838b44 Jun 30, 2026
52 checks passed
let mut next_veneer_idx = 0;
let relocation_target_addr = |name: &ModuleRelocTarget, addend: Addend| {
let addend = isize::try_from(addend).unwrap();
get_address(name).addr().checked_add_signed(addend).unwrap()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have used expose_provenance, not addr. addr() doesn't allow dereferencing the address after turning it back into a pointer, yet that is exactly what the JITed code will likely do.

@HueCodes

HueCodes commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. This is good learning for me.

That makes sense: these relocation values are not just opaque integers for comparison or range math. They can become addresses that JITed code later uses as pointers, so using addr() drops provenance information that should be exposed for that use case.

I prepared a follow-up that switches this helper to expose_provenance() while leaving the checked addend handling unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eagerly cast pointers in aarch64 relocation code to usize to avoid unsafe blocks in cranelift-jit

3 participants