Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Implement SetFinalizer for latest tinygo dev (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Feb 17, 2023
1 parent 0823f16 commit e76a322
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ jobs:
- run: go run mage.go check
build-tinygodev:
runs-on: ubuntu-22.04
container: ghcr.io/tinygo-org/tinygo/tinygo-dev:sha-e0a5fc255522933f651a89c071ebd531ad634e7b
container: ghcr.io/tinygo-org/tinygo/tinygo-dev:sha-f6df2761187f1975e35eb43461d735d6e325df85
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '^1.20.0'
cache: true

- run: |
go install github.com/wasilibs/tools/cmd/wasmtime@c93d2e477ab3c1eb7f5303c66a35c84a21d06dbd
Expand Down
59 changes: 59 additions & 0 deletions finalizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build nottinygc_finalizer

package nottinygc

/*
void GC_register_finalizer(void* obj, void* fn, void* cd, void** ofn, void** ocn);
void onFinalizer(void* obj, void* fn);
void* malloc(unsigned int long);
void free(void* ptr);
*/
import "C"
import "unsafe"

var finalizers = map[uintptr]interface{}{}

//go:linkname SetFinalizer runtime.SetFinalizer
func SetFinalizer(obj interface{}, finalizer interface{}) {
finKey := uintptr((*_interface)(unsafe.Pointer(&finalizer)).value)
finalizers[finKey] = finalizer

in := (*_interface)(unsafe.Pointer(&obj))

rf := (*registeredFinalizer)(C.malloc(C.ulong(unsafe.Sizeof(registeredFinalizer{}))))
rf.typecode = in.typecode
rf.finKey = finKey

C.GC_register_finalizer(in.value, C.onFinalizer, unsafe.Pointer(rf), nil, nil)
}

//export onFinalizer
func onFinalizer(obj unsafe.Pointer, data unsafe.Pointer) {
defer C.free(data)

rf := (*registeredFinalizer)(data)
finalizer := finalizers[rf.finKey]
delete(finalizers, rf.finKey)

var in interface{}
inFields := (*_interface)(unsafe.Pointer(&in))
inFields.typecode = rf.typecode
inFields.value = obj

switch f := finalizer.(type) {
case func(interface{}):
f(in)
default:
panic("currently only finalizers of the form func(interface{}) are supported")
}
}

type _interface struct {
typecode uintptr
value unsafe.Pointer
}

type registeredFinalizer struct {
typecode uintptr
finKey uintptr
}
45 changes: 45 additions & 0 deletions finalizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build nottinygc_finalizer

package nottinygc

import (
"runtime"
"testing"
)

func TestFinalizer(t *testing.T) {
finalized := 0
allocFinalized(10, func() {
finalized++
})

runtime.GC()

if finalized == 0 {
t.Errorf("finalizer not called")
}
}

//go:noinline
func allocFinalized(num int, cb func()) {
f := &finalized{
a: 100,
b: "foo",
}

runtime.SetFinalizer(f, func(f interface{}) {
cb()
})

// Recurse to create some more stack frames or else the shadow stack
// may still not have been reset, causing GC to find the inaccessible
// pointers.
if num > 1 {
allocFinalized(num-1, cb)
}
}

type finalized struct {
a int
b string
}
13 changes: 12 additions & 1 deletion magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

// Test runs unit tests
func Test() error {
return sh.RunV("tinygo", "test", "-gc=custom", "-tags=custommalloc", "-target=wasi", "-v", "-scheduler=none", "./...")
v, err := sh.Output("tinygo", "version")
if err != nil {
return fmt.Errorf("invoking tinygo: %w", err)
}

tags := []string{"custommalloc"}
if strings.HasSuffix(v, "tinygo version 0.28.") {
tags = append(tags, "nottinygc_finalizer")
}

return sh.RunV("tinygo", "test", "-gc=custom", fmt.Sprintf("-tags='%s'", strings.Join(tags, " ")), "-target=wasi", "-v", "-scheduler=none", "./...")
}

func Format() error {
Expand Down

0 comments on commit e76a322

Please sign in to comment.