Skip to content

WASI gc=conservative frees data which is still malloc'ed by cgo code #2187

Description

@fritzbauer

The code to malloc, calloc and free in src/runtime/arch_tinygowasm.go does not ensure that malloc'ed memory is not freed during GC.
I experienced the gc=conservative freeing memory which was still used during the execution of a cgo function. The only option to avoid this was to use gc=leaking.

Not sure if this workaround has any undesired side-effects, but I could resolve the issue by keeping a reference to the alloced cgo pointers in Go.

Here is what I did:

  1. Add a map to the var (...) section of arch_tinygowasm.go: cgoAllocations = make(map[uintptr]uintptr)
  2. Fill the map in libc_malloc and libc_calloc: cgoAllocations[uintptr(allocResult)] = size
  3. Delete from the map in libc_free: delete(cgoAllocations, uintptr(ptr))

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasmWebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions