Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

runtime: temporarily store waspi2 memory allocations from cabi_realloc#4897

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
ydnar wants to merge2 commits intotinygo-org:dev
base:dev
Choose a base branch
Loading
fromydnar:ydnar/wasmexport-realloc

Conversation

ydnar
Copy link
Contributor

@ydnarydnar commentedMay 17, 2025
edited
Loading

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 tocabi_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 ofcabi_realloc calls to grow the target memory.

The fix is simple: hold onto the pointers allocated bycabi_realloc until the nextwasmexport function returns.

This logic currently works for reactor modules, where allocations created withcabi_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 inbytecodealliance/go-modules#348.

@deadprogram
Copy link
Member

I think I have run into a similar problem usingwasm-unknown andwasip1 having to do with lifetimes and memory. This PR makes me think about some ways to possibly look into it further for those targets.

In any case, this seems fine to me if it does fix the wasip2 issue as described.

@dgryski any comment here?

@ydnar
Copy link
ContributorAuthor

I ran into a related issue with the wasmimport functions today and have a reproducer.

Unfortunately the fix in this PR will not fix that issue, and instead result in a memory leak.

Do not merge this.

@deadprogramdeadprogram marked this pull request as draftMay 18, 2025 17:49
@aykevl
Copy link
Member

aykevl commentedMay 21, 2025
edited
Loading

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 ofcabi_realloc calls to grow the target memory.

We have the same issue for libc malloc/realloc. A simple solution would be to calllibc_realloc instead (defined in arch_tinygwasm_malloc.go). That would also avoid duplicating this logic.

(I also see thatlibc_realloc has a bug in the precise GC: it allocates usingmake([]byte, ...) which tells the GC the buffer doesn't contain pointers but that is of course an incorrect assumption with C calloc).

EDIT: See:#4898

ydnarand others added2 commitsMay 25, 2025 14:10
This is an initial take at solving the interaction between the TinyGo GC and the Component Model allocation scheme (cabi_realloc).
  - Do not use make([]byte, ...) to allocate, instead allocate a slice    of pointers. This makes sure the precise GC will scan the contents    of the allocation, since C could very well put pointers in there.  - Simplify the map to use the pointer as the key and the size as the    value, instead of storing the slices directly in the map.
@ydnarydnarforce-pushed theydnar/wasmexport-realloc branch from1889b33 toc8bb418CompareMay 25, 2025 21:10
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dgryskidgryskiAwaiting requested review from dgryski

@deadprogramdeadprogramAwaiting requested review from deadprogram

Assignees

@ydnarydnar

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@ydnar@deadprogram@aykevl

[8]ページ先頭

©2009-2025 Movatter.jp