max-dau opened PR #13883 from COLORADIO-Project:fix-rv32imac-stack-switching to bytecodealliance:main:
The stackswitching implementation for 32-bit RISC-V IMAC ISAs contained a bug. When saving registers to the stack, these registers were first stored below the current stack pointer, then the stack pointer was decreased so all of the saved values were above it. This left relevant data below the stack pointer while the registers were being saved.
If for example an interrupt occured when registers had already been saved, but the stackpointer had not been moved, the interrupt handler might overwrite some of the data previously saved for stack switching. This PR makes this impossible by first moving the stack pointer, only saving the registers values to the reserved stack space afterwards.
I was able to somewhat reliably have the previous bug lead to a device halting execution by having a Wasm program call
asyncbindings very often and using fuel instrumentation with afuel_async_yield_intervalof 1. I do not currently have the up-to-date version ofmainrunning on 32-bit RISC-V, so I was only able to test this fix with an older version of Wasmtime. The stackswitching implementation has only been changed by commitf3156fesince then, those changes do not appear relevant to this fix.I suspect this bug to be present in other stack switching implementations as well but do not have the hardware available to test a fix. For this reason, I have started a Zulip topic about this.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
max-dau requested wasmtime-core-reviewers for a review on PR #13883.
max-dau requested pchickey for a review on PR #13883.
max-dau edited PR #13883:
The stackswitching implementation for 32-bit RISC-V IMAC ISAs contained a bug. When saving registers to the stack, these registers were first stored below the current stack pointer, then the stack pointer was decreased so all of the saved values were above it. This left relevant data below the stack pointer while the registers were being saved.
If for example an interrupt occured when registers had already been saved, but the stackpointer had not been moved, the interrupt handler might overwrite some of the data previously saved for stack switching. This PR makes this impossible by first moving the stack pointer, only saving the registers values to the reserved stack space afterwards.
I was able to somewhat reliably have the previous bug lead to a device halting execution by having a Wasm program call
asyncbindings very often and using fuel instrumentation with afuel_async_yield_intervalof 1. I do not currently have the up-to-date version ofmainrunning on 32-bit RISC-V, so I was only able to test this fix with an older version of Wasmtime. The stackswitching implementation has only been changed by commitf3156fesince then, those changes do not appear relevant to this fix.I suspect this bug to be present in other stack switching implementations as well but do not have the hardware available to test a fix. For this reason, I have started a Zulip topic about this.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
:thumbs_up: alexcrichton submitted PR review:
Thanks for this! I'll file issues for the other implementations.
alexcrichton added PR #13883 Reserve stack space before writing in riscv32imac stackswitching implementation to the merge queue.
:check: alexcrichton merged PR #13883.
alexcrichton removed PR #13883 Reserve stack space before writing in riscv32imac stackswitching implementation from the merge queue.
Last updated: Jul 29 2026 at 05:03 UTC