Skip to content

wasm heap corruption when pointers are in wasm locals #3277

@anuraaga

Description

@anuraaga

While understanding how wasm locals get marked during GC, I found this case where they don't seem to get marked causing heap corruption of valid objects.

Using latest dev tinygo

https://github.com/anuraaga/tinygo-gctest

git clone https://github.com/anuraaga/tinygo-gctest
cd tinygo-gctest
tinygo build -scheduler=none -target=wasi -o gctest.wasm .
go run github.com/tetratelabs/wazero/cmd/wazero@latest run gctest.wasm

Should print yogi and garfield, but prints garfield twice because yogi got overwritten.

Definitely not an expert of the code, but looking at gc.go my reading is it creates stack slots for functions with a call to runtime.alloc inside them, but doesn't seem to ever go up the call tree from these. In this example, the pointer is returned to the calling function, which itself has no direct call to runtime.alloc, but keeps the pointer in one of its wasm slots and would need a GC stack slot allocated for it. Not sure how to update the code to do this if it's missing, but hopefully it's not too hard 😂

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwasmWebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions