Skip to content

Investigate and address RISC-V-specific JumpStub issues #135607

Description

@jkotas

Not from this PR, but on this line: the inner PCODE jumpStubAddr shadows the one at 12549. So the stub is dropped, the method recompiles, and pc-relative calls are turned off process-wide. Checked with a reduced repro only, not on RISC-V.

                jumpStubAddr = ExecutionManager::jumpStub(m_pMethodBeingCompiled, (PCODE)target, loAddr, hiAddr, nullptr);

Originally posted by @pcshrosbree in #135411 (comment)

Activity

  1. added
    untriaged
    on Oct 11, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 11, 2026
  3. added
    arch-riscv
    on Oct 11, 2026
  4. dotnet-policy-service commented on Oct 11, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. jkotas commented on Oct 11, 2026

    @jkotas
    MemberAuthor

    The JumpStubs are implemented for RISC-V, but not actually used due to the bug listed above. RISC-V codegen seems to be working around this bug by falling back to indirect calls with long addressing modes.

    We should either remove the RISC-V JumpStub implementation entirely, or fix it and remove the corresponding codegen workaround.

  6. pcshrosbree commented on Oct 12, 2026

    @pcshrosbree
    Contributor

    TL;DR: From reading the source only, the shadowing means the RISC-V jump-stub path has never used a stub it allocated. Fixing the shadowing alone would switch on a stub that is itself broken, because it leaves ra holding the target; removing the encoder would need per-architecture guards in the shared code that uses it. I would fix the encoder and the assignment first, then drop the codegen workaround. Nothing here was run on RISC-V.

    Two data points for the fix-or-remove choice, from reading main at 0d73c1f. I have not built or run anything on RISC-V; the disassembly below is llvm-mc on the constant words in the header.

    1. The shadowing came in with [RISC-V] Synthesizing PC-relative pointer constants #119203, which is also where RISC-V joined JumpStubOverflowCheck and getRelocTypeHint. Here the inner declaration discards the address jumpStub returned, the outer null sets the overflow flag, and the method is recompiled with the absolute form. recordRelocation is the only RISC-V caller of jumpStub, so this path has never used a stub it allocated.

    2. Fixing the shadowing alone switches on a stub that is itself broken. emitBackToBackJump in vm/riscv64/cgencpu.h encodes

      auipc ra, 0
      ld    ra, 16(ra)
      ret            ; jalr x0, ra, 0
      

      It reaches the target but leaves ra = target. The JIT gets there by auipc ra; jalr ra, ra (ra = return address) or, for a tail jump, auipc t2; jalr x0, t2 (ra = the caller's return address), so a callee returning through that incoming ra re-enters the target. arm64 uses x16 and loongarch64 $r21 here. Of the RISC-V temporaries, t0/t1 carry the profiler-enter arguments, t2 the secret stub parameter and the dispatch token, t3/t4 the write barrier arguments and t5 the indirection cell. t6 (REG_OPT_RSVD) is the one none of those contracts pass across a call; the VSD lookup stub already uses it the same way (auipc t6; ld t2, 24(t6); ld t6, 16(t6); jalr x0, t6), and JIT_WriteBarrier declares it trashed.

    On "remove entirely": removing the stub request in recordRelocation is small, though the overflow handling around it also serves the data relocations. The encoder is another matter: it also writes the CLRPersonalityRoutine thunk of every 64-bit code heap that has one, and jumpStub, JumpStubCache and RangeSectionStubManager consume BACK_TO_BACK_JUMP_ALLOCATE_SIZE and decodeBackToBackJump with no per-arch guard, so deleting it means guarding shared code. (loongarch64 is in the same position: its relocation case never asks for a stub.) I did not find anything on Linux that executes that thunk.

    On "fix it and remove the codegen workaround": #117196 says it always records the relocation because "it leaves the door open for further code size reductions like arm64, by emitting a single jal instruction and inserting a jump stub". amd64 already works that way for direct calls: genCodeAddrNeedsReloc returns true unconditionally there, and the hint is consulted for data and indirect-call addresses only. RISC-V's emitIns_Call is the one that consults the hint for direct calls and uses li + jalr when IsAddressInRange is false. With a working stub that branch can go, and the recovery when a stub cannot be allocated becomes amd64's: recompile with room reserved for stubs next to the code. The hint stays for the PCREL_I/PCREL_S data relocations. That is the direction I would take, with the encoder and the assignment first; the retry on a failed stub allocation is the part that needs a hardware run.

    I found no test that deliberately exercises the stub path; it is reached when the range runs out (±2 GiB here, ±128 MiB on arm64). In Checked builds DOTNET_ForceRelocs sets m_fJumpStubOverflow before the JIT runs, so recordRelocation requests no stub on that pass, and the retry on amd64 and riscv64 turns rel32 off. Once the shadowing is fixed, a Checked-only knob that forces every RISCV64_CALL_PLT through a stub turns the ra defect into a crash the first time a redirected call returns; with the shadowing in place nothing executes the stub at all.

    I can put up a PR with the fix, the emitter change and that knob, if that's the direction. It would need someone with hardware to run it: the only linux_riscv64 job I find here builds, and the Helix queue files have no riscv64 entry.

    cc @tomeksowi

  7. removed
    untriaged
    on Nov 25, 2026
  8. added a commit that references this issue on Nov 25, 2026
    d372a1b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-riscvarea-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions