wasmfx / wasmfxtime

A fork of wasmtime (a fast and secure runtime for WebAssembly) supporting the WasmFX instruction set
https://wasmfx.dev/
Apache License 2.0
19 stars 1 forks source link

In resume instruction, update StackLimits from generated code #223

Closed frank-emrich closed 2 months ago

frank-emrich commented 2 months ago

This PR is part of a series that implements native stack switching.

This particular PR moves one particular aspect out of the tc_resume libcall: Updating the StackLimits object of the parent of the continuation being resumed.

Doing this inside the libcall had practical reasons: We needed access to the updated last_wasm_exit_pc/last_wasm_exit_fp value in the VMRuntimeLimits in order to copy them into the StackLimits. However, these values are only updated by the libcall mechanism itself, so they are only available inside the libcall implementation itself.

This PR obtains the corresponding values using code generated for resume, before the actual tc_resume libcall happening, and writes them into the StackLimits.

  1. We use the get_frame_pointer instruction to obtain a value for last_wasm_exit_fp.
  2. The value for last_wasm_exit_pc is obtained using a new CLIF instruction, get_instruction_pointer. All this does is giving us some instruction pointer that is guaranteed to be associated with the current Wasm instruction being translated (i.e., resume in our case). While this means that we will write slightly different values for last_wasm_exit_pc into the StackLimits than before, this difference does not matter at all for backtrace creation. last_wasm_exit_pc is never used for control flow (i.e., it is never branched to), all that matters is what Wasm instruction it is associated with.

I consider this to be a workaround, once native stack switching is fully rolled out it would be nice to overhaul the whole backtrace generation mechanism in the longer term. But this PR's goal is to make it possible to move to native stack switching with as little changes to backtrace creation as possible.