Related to dynamic allocation of memory within WebAssembly programs implemented using the Zig programming language.
Exploring WebAssembly's dynamic memory allocation capabilities using Zig language showcases intriguing behaviors. This examination reveals how re-entrancy in a WebAssembly (WASM) module, particularly with Bun or Node.js, affects variable state and memory references across multiple function invocations.
We will use Bun for this example, but it reproduces also with Nodejs.
bun run build-zig
# With only one run the vars data and result are unreferenced.
# The memory changes of data and result are preserved at memory.buffer
bun run test-oneRun
# If we run the function a second time it works a spected.
bun run test
Utilizing Bun for demonstration purposes, we uncovered notable patterns concerning dynamic memory allocation within a Zig-implemented WASM module:
- Initial State (One Run): Upon the first invocation of our test function (
test-oneRun
), variabledata_out
does not retain a reference to allocated memory inmemory.buffer
. This absence is confirmed through console logging, displaying an empty Uint8Array.
# console.log('data_out');
data_out
Uint8Array(0) [ ]
- Re-invocation and Memory Reinitialization (Second Run): After executing the test function (
test
) reveals thatdata_out
once again loses its reference tomemory.buffer
. However, we subsequently reassign values todata_out
, effectively resetting it to its initial state as intended. This restoration allows our example function to execute correctly on subsequent calls.
# console.log('data_out');
data_out
Uint8Array(4) [ 1, 2, 3, 4 ]
Assistance required to pinpoint the source of the problem!