Skip to content

Commit

Permalink
Update GC code for latest fork (envoyproxy#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga committed Dec 20, 2022
1 parent 63723e9 commit ed57eba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions internal/gc/gc_conservative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ed57eba

Please sign in to comment.