Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/runtime/arch_tinygowasm_malloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import "unsafe"
// code linked from other languages can allocate memory without colliding with
// our GC allocations.

var allocs = make(map[uintptr][]byte)
// Map of allocations, where the key is the allocated pointer and the value is
// the size of the allocation.
var allocs = make(map[unsafe.Pointer]uintptr)

//export malloc
func libc_malloc(size uintptr) unsafe.Pointer {
if size == 0 {
return nil
}
buf := make([]byte, size)
const wordSize = unsafe.Sizeof(unsafe.Pointer(nil))
buf := make([]unsafe.Pointer, (size+wordSize-1)/wordSize)
ptr := unsafe.Pointer(&buf[0])
allocs[uintptr(ptr)] = buf
allocs[ptr] = size
return ptr
}

Expand All @@ -26,8 +29,8 @@ func libc_free(ptr unsafe.Pointer) {
if ptr == nil {
return
}
if _, ok := allocs[uintptr(ptr)]; ok {
delete(allocs, uintptr(ptr))
if _, ok := allocs[ptr]; ok {
delete(allocs, ptr)
} else {
panic("free: invalid pointer")
}
Expand All @@ -48,18 +51,22 @@ func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {

// It's hard to optimize this to expand the current buffer with our GC, but
// it is theoretically possible. For now, just always allocate fresh.
buf := make([]byte, size)
// TODO: we could skip this if the new allocation is smaller than the old.
const wordSize = unsafe.Sizeof(unsafe.Pointer(nil))
buf := make([]unsafe.Pointer, (size+wordSize-1)/wordSize)
ptr := unsafe.Pointer(&buf[0])

if oldPtr != nil {
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok {
copy(buf, oldBuf)
delete(allocs, uintptr(oldPtr))
if oldSize, ok := allocs[oldPtr]; ok {
oldBuf := unsafe.Slice((*byte)(oldPtr), oldSize)
newBuf := unsafe.Slice((*byte)(ptr), size)
copy(newBuf, oldBuf)
delete(allocs, oldPtr)
} else {
panic("realloc: invalid pointer")
}
}

ptr := unsafe.Pointer(&buf[0])
allocs[uintptr(ptr)] = buf
allocs[ptr] = size
return ptr
}
17 changes: 15 additions & 2 deletions src/runtime/runtime_wasip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,21 @@ func os_runtime_args() []string {
}

//export cabi_realloc
func cabi_realloc(ptr, oldsize, align, newsize unsafe.Pointer) unsafe.Pointer {
return realloc(ptr, uintptr(newsize))
func cabi_realloc(ptr unsafe.Pointer, oldSize, align, newSize uintptr) unsafe.Pointer {
if newSize == 0 {
return nil
}
newPtr := realloc(ptr, newSize)
if ptr != nil {
for i := range wasmAllocs {
if wasmAllocs[i] == ptr {
wasmAllocs[i] = newPtr
return newPtr
}
}
}
wasmAllocs = append(wasmAllocs, newPtr)
return newPtr
}

func ticksToNanoseconds(ticks timeUnit) int64 {
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/runtime_wasmentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ func wasmExportExit() {
// //go:wasmexport function has exited.
schedulerExit = true

// Clear wasm allocations.
wasmAllocs = nil

task.Pause()

// TODO: we could cache the allocated stack so we don't have to keep
// allocating a new stack on every //go:wasmexport call.
}

// wasmAllocs holds memory allocated by the host in guest memory.
// See func cabi_realloc for more information.
var wasmAllocs []unsafe.Pointer
Loading