runtime: temporarily store waspi2 memory allocations from cabi_realloc #4897
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an initial take at solving the interaction between the TinyGo GC and the Component Model allocation scheme (
cabi_realloc
).Specifically, when the host calls an exported function with argument(s) that require allocations, these are first allocated in the guest via calls to
cabi_realloc
, then these pointer(s) are passed to the function exported withgo:wasmexport
.Because these allocated pointers are not stored anywhere in the TinyGo stack or heap, they are subject to immediate collection. This can happen if the host wants to copy a large string into the guest memory, and performs a sequence of
cabi_realloc
calls to grow the target memory.The fix is simple: hold onto the pointers allocated by
cabi_realloc
until the nextwasmexport
function returns.This logic currently works for reactor modules, where allocations created with
cabi_realloc
are held in memory until ago:wasmexport
function exits. Caveat: when used with a long-running program like the defaultwasi-cli
world, any allocations created viacabi_realloc
will be held forever until funcmain
exits.This is intended to fix the problem in bytecodealliance/go-modules#348.