wasm: add Boehm GC support

This adds support for `-gc=boehm` on `-target=wasip1` and `-target=wasm`
(in a browser or NodeJS). Notably it does *not* add Boehm GC support for
`-target=wasip2`, since that target doesn't have a real libc.
This commit is contained in:
Ayke van Laethem
2025-03-17 13:37:44 +01:00
committed by Ron Evans
parent 9c3b706533
commit c08b2dfe06
21 changed files with 121 additions and 60 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && !(custommalloc || wasm_unknown)
//go:build tinygo.wasm && !(custommalloc || wasm_unknown || gc.boehm)
package runtime
+37
View File
@@ -0,0 +1,37 @@
//go:build none
// This file is included in the build on systems that support the Boehm GC,
// despite the //go:build line above.
#include <stdint.h>
typedef void (* GC_push_other_roots_proc)(void);
void GC_set_push_other_roots(GC_push_other_roots_proc);
typedef void(* GC_warn_proc)(const char *msg, uintptr_t arg);
void GC_set_warn_proc(GC_warn_proc p);
void tinygo_runtime_bdwgc_callback(void);
static void callback(void) {
tinygo_runtime_bdwgc_callback();
}
static void warn_proc(const char *msg, uintptr_t arg) {
}
void tinygo_runtime_bdwgc_init(void) {
GC_set_push_other_roots(callback);
#if defined(__wasm__)
// There are a lot of warnings on WebAssembly in the form:
//
// GC Warning: Repeated allocation of very large block (appr. size 68 KiB):
// May lead to memory leak and poor performance
//
// The usual advice is to use something like GC_malloc_ignore_off_page but
// unfortunately for most allocations that's not allowed: Go allocations can
// legitimately hold pointers further than one page in the allocation. So
// instead we just disable the warning.
GC_set_warn_proc(warn_proc);
#endif
}
+16 -9
View File
@@ -20,7 +20,6 @@ package runtime
import (
"internal/gclayout"
"internal/reflectlite"
"internal/task"
"unsafe"
)
@@ -39,22 +38,30 @@ var needsResumeWorld bool
func initHeap() {
libgc_init()
libgc_set_push_other_roots(gcCallbackPtr)
// Call GC_set_push_other_roots(gcCallback) in C because of function
// signature differences that do matter in WebAssembly.
gcInit()
}
var gcCallbackPtr = reflectlite.ValueOf(gcCallback).UnsafePointer()
//export tinygo_runtime_bdwgc_init
func gcInit()
//export tinygo_runtime_bdwgc_callback
func gcCallback() {
// Mark globals and all stacks, and stop the world if we're using threading.
gcMarkReachable()
if needsResumeWorld {
// Should never happen, check for it anyway.
runtimePanic("gc: world already stopped")
}
// If we use a scheduler with parallelism (the threads scheduler for
// example), we need to call gcResumeWorld() after scanning has finished.
if hasParallelism {
if needsResumeWorld {
// Should never happen, check for it anyway.
runtimePanic("gc: world already stopped")
}
// Note that we need to resume the world after finishing the GC call.
needsResumeWorld = true
// Note that we need to resume the world after finishing the GC call.
needsResumeWorld = true
}
}
func markRoots(start, end uintptr) {
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build (gc.conservative || gc.custom || gc.precise) && tinygo.wasm
//go:build (gc.conservative || gc.custom || gc.precise || gc.boehm) && tinygo.wasm
package runtime