1
16
17 package loader
18
19 import (
20 `os`
21 `runtime`
22 `runtime/debug`
23 `testing`
24 `time`
25
26 `github.com/stretchr/testify/require`
27 )
28
29 var (
30 debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
31 )
32
33 func TestMain(m *testing.M) {
34 go func () {
35 if !debugAsyncGC {
36 return
37 }
38 println("Begin GC looping...")
39 for {
40 runtime.GC()
41 debug.FreeOSMemory()
42 }
43 println("stop GC looping!")
44 }()
45 time.Sleep(time.Millisecond*100)
46 m.Run()
47 }
48
49 func TestWrapC(t *testing.T) {
50 var stub func(a int64, val *int64) (ret int64)
51 ct := []byte{
52 0x55,
53 0x48, 0x89, 0xe5,
54 0x48, 0x89, 0x7d, 0xf8,
55 0x48, 0x89, 0x75, 0xf0,
56 0x48, 0x8b, 0x75, 0xf8,
57 0x48, 0x8b, 0x7d, 0xf0,
58 0x48, 0x03, 0x37,
59 0x48, 0x89, 0xf0,
60 0x5d,
61 0xc3,
62 }
63
64 WrapGoC(ct, []CFunc{{
65 Name: "add",
66 EntryOff: 0,
67 TextSize: uint32(len(ct)),
68 MaxStack: uintptr(16),
69 Pcsp: [][2]uint32{
70 {1, 8},
71 },
72 }}, []GoC{{
73 CName: "add",
74 GoFunc: &stub,
75 } }, "dummy/native", "dummy/native.c")
76
77
78
79
80
81
82
83
84
85 f := stub
86 b := int64(2)
87 println("b : ", &b)
88 var c *int64 = &b
89 runtime.SetFinalizer(c, func(x *int64){
90 println("c got GC: ", x)
91 })
92 runtime.GC()
93 println("before")
94 act := f(1, c)
95 println("after")
96 runtime.GC()
97 require.Equal(t, int64(3), act)
98 }
99
View as plain text