-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Entry point function cannot do a far jump #1243
Comments
For more context, the pedantic definition for auipc ra, TOP 20 bits
jalr ra, ra, LOWER 12 bits There is no need to feel guilty about using pseudo instructions, as well. We are already using those. 002024c8 17 b1 10 00 auipc sp,0x10b
002024cc 13 01 c1 d5 addi sp,sp,-0x2a4 |
Thanks for tracking down this issue! I've opened #1257 to replace
In general, we don't guarentee image ID stability unless the user pins to an exact version (git commit) of the zkVM and builds in Docker using |
As described in #1243, our use of the `jal` instruction in our entrypoint breaks when the jump distance becomes too far. This PR changes the instruction to the `call` psuedo-instruction to allow the assembler flexibility to solve this issue and implement a far jump. Co-authored-by: @weikengchen Resolves: #1243 --------- Co-authored-by: Frank Laub <frank@risczero.com> Co-authored-by: Frank Laub <flaub@risc0.com>
As described in #1243, our use of the `jal` instruction in our entrypoint breaks when the jump distance becomes too far. This PR changes the instruction to the `call` psuedo-instruction to allow the assembler flexibility to solve this issue and implement a far jump. Co-authored-by: @weikengchen Resolves: #1243 --------- Co-authored-by: Frank Laub <frank@risczero.com> Co-authored-by: Frank Laub <flaub@risc0.com>
As described in #1243, our use of the `jal` instruction in our entrypoint breaks when the jump distance becomes too far. This PR changes the instruction to the `call` psuedo-instruction to allow the assembler flexibility to solve this issue and implement a far jump. Co-authored-by: @weikengchen Resolves: #1243 --------- Co-authored-by: Frank Laub <frank@risczero.com> Co-authored-by: Frank Laub <flaub@risc0.com>
Bug Report
Based on the conversation in the Discord server (see: https://discord.com/channels/953703904086994974/1184915191411003463), if the guest program is sophisticated enough, the entry point function, showed below, cannot call the
__start
function, because it is too far.@DefiCake reported this problem, from their codebase https://github.com/DefiCake/fuel-risc0-prover/.
where the definition of the
__start
function is as follows. This is the function that starts the guest main.jal
is an RISC-V raw instruction that jumps to__start
, where__start
is represented by a signed 20-bit offset from the pc. This means that it can jump either backward up to 1MB or forward up to 1MB. But, if it takes more than 1MB to go to__start
in the memory from the currentpc
, this instruction does not work.This would result in a compilation time error, shown as follows.
Solution
Tested locally. If we make a single-line change to the https://github.com/risc0/risc0/blob/main/risc0/zkvm/src/guest/mod.rs#L166.
The problem can be fixed.
call
is a "pseudo" instruction, in that LLVM would expand it according to its need.A remaining question is whether this change would affect the binary that is generated by previous code. I.e., whether the image file would be different. This is not really a big issue, but it would be a headache to, if necessary, update the IMAGE id that has been hardcoded.
I tested https://github.com/l2iterative/vfhe0 with Ghidra. The answer is positive---even if the jump can be done with "jal" using an offset, Rust (indeed, LLVM) would emit two instructions: auipc and jalr, as expected.
For this reason, I am reluctant to submit a PR since there is going to be a lot of work to update the IMAGE ids as it would affect everything. That being said, it is a good argument that this fix should be included in the incoming LTS release.
The text was updated successfully, but these errors were encountered: