This is an archive of the discontinued LLVM Phabricator instance.

[JITLink][RISCV] ADD/SUB relocs: read value from working memory
ClosedPublic

Authored by jobnoorman on Apr 6 2023, 2:58 AM.

Details

Summary

The various ADD/SUB relocations work by reading the current value the
relocation points to, transforming it, and then writing it back to
memory. While the current implementation writes the value back to
working memory, it reads the current value from the execution address of
the relocation. This causes at least wrong results, but often crashes,
when the addresses of working memory are not equal to execution
addresses. This patch fixes this by reading the current value from
working memory.

The bug was noticed by applying the BOLT RISC-V port patch (D145687) on
top of the patch that moves BOLT to JITLink (D147544). In the case of
BOLT, working memory and execution addresses are not equal.

Diff Detail

Event Timeline

jobnoorman created this revision.Apr 6 2023, 2:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 2:58 AM
Herald added subscribers: asb, luke, pmatos and 29 others. · View Herald Transcript
jobnoorman requested review of this revision.Apr 6 2023, 2:58 AM
lhames accepted this revision.Apr 6 2023, 10:27 AM

@jobnoorman Nice catch!

You can test this by adding -slab-allocate=1Mb -slab-address=0x1000 -slab-page-size=0x1000 to the RUN: llvm-jitlink lines in the existing llvm/test/ExecutionEngine/JITLink/RISCV/riscv_reloc_add.s test. This will force the JIT'd code to be laid out as-if allocated at 0x1000, which will trigger the bug.

This revision is now accepted and ready to land.Apr 6 2023, 10:27 AM
jobnoorman edited the summary of this revision. (Show Details)

You can test this by adding -slab-allocate=1Mb -slab-address=0x1000 -slab-page-size=0x1000 to the RUN: llvm-jitlink lines in the existing llvm/test/ExecutionEngine/JITLink/RISCV/riscv_reloc_add.s test. This will force the JIT'd code to be laid out as-if allocated at 0x1000, which will trigger the bug.

Nice, thanks for this @lhames!

Diff update:

  • Modify tests to use different working/execution addresses.
  • Fix forgotten SUB6 reloc.
This revision was landed with ongoing or failed builds.Apr 7 2023, 12:48 AM
This revision was automatically updated to reflect the committed changes.