From ed57ebaaecb8bff9d0eb8c1ff4c9e63d8563eeca Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Tue, 20 Dec 2022 10:34:29 +0900 Subject: [PATCH] Update GC code for latest fork (#115) --- internal/gc/gc_conservative.go | 41 ++++++++++++++++++++++------------ magefiles/magefile.go | 2 +- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/gc/gc_conservative.go b/internal/gc/gc_conservative.go index 657c7c11a00b8..2ef47df8e7f1d 100644 --- a/internal/gc/gc_conservative.go +++ b/internal/gc/gc_conservative.go @@ -13,13 +13,31 @@ import ( void* GC_malloc(unsigned int size); void GC_free(void* ptr); void GC_gcollect(); + +void onCollectionEvent(); */ import "C" -// Initialize the memory allocator. We currently do not have anything needing initialization. +//export GC_set_on_collection_event +func GC_set_on_collection_event(f unsafe.Pointer) + +const ( + gcEventStart = 0 +) + +//export onCollectionEvent +func onCollectionEvent(eventType uint32) { + switch eventType { + case gcEventStart: + markStack() + } +} + +// Initialize the memory allocator. // //go:linkname initHeap runtime.initHeap func initHeap() { + GC_set_on_collection_event(C.onCollectionEvent) } // alloc tries to find some free space on the heap, possibly doing a garbage @@ -40,22 +58,17 @@ func free(ptr unsafe.Pointer) { C.GC_free(ptr) } +//go:linkname markRoots runtime.markRoots +func markRoots(start, end uintptr) { + // Roots are already registered in bdwgc so we have nothing to do here. +} + +//go:linkname markStack runtime.markStack +func markStack() + // GC performs a garbage collection cycle. // //go:linkname GC runtime.GC func GC() { C.GC_gcollect() } - -//go:linkname KeepAlive runtime.KeepAlive -func KeepAlive(x interface{}) { - // no-op should be fine, pointers are tracked in a shadow stack which will keep references - // to pointers throughout a function call regardless of calling this. - // TODO(anuraaga): Verify this -} - -//go:linkname SetFinalizer runtime.SetFinalizer -func SetFinalizer(obj interface{}, finalizer interface{}) { - // Unimplemented for now. - // TODO(anuraaga): Try using GC_register_finalizer to implement -} diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 9d589963e13dd..852f2f0ce9c38 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -193,7 +193,7 @@ func Build() error { script := fmt.Sprintf(` cd /src && \ tinygo build -gc=custom -opt=2 -o %s -scheduler=none -target=wasi %s`, filepath.Join("build", "mainraw.wasm"), buildTagArg) - if err := sh.RunV("docker", "run", "--pull=always", "--rm", "-v", fmt.Sprintf("%s:/src", wd), "ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-tinygo:sha-447795c", + if err := sh.RunV("docker", "run", "--pull=always", "--rm", "-v", fmt.Sprintf("%s:/src", wd), "ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-tinygo:sha-63723e9", "bash", "-c", script); err != nil { return err }