1
2
3
4
19
20 package loader
21
22 import (
23 `runtime`
24 `runtime/debug`
25 `strconv`
26 `testing`
27 `unsafe`
28
29 `github.com/bytedance/sonic/internal/rt`
30 `github.com/stretchr/testify/require`
31 )
32
33 func TestLoad(t *testing.T) {
34
35
36
37
38
39
40
41
42
43
44
45 var hstr string
46
47 type TestFunc func(i *int, hook func(i *int)) int
48 var hook = func(i *int) {
49 runtime.GC()
50 debug.FreeOSMemory()
51 hstr = ("hook" + strconv.Itoa(*i))
52 runtime.GC()
53 debug.FreeOSMemory()
54 }
55
56
57
58
59
60 bc := []byte {
61 0x48, 0x83, 0xec, 0x18,
62 0x48, 0x89, 0x6c, 0x24, 0x10,
63 0x48, 0x8d, 0x6c, 0x24, 0x10,
64 0x48, 0x89, 0x44, 0x24, 0x20,
65 0x48, 0x8b, 0x08,
66 0x48, 0x89, 0x4c, 0x24, 0x08,
67 0x48, 0x8b, 0x33,
68 0x48, 0x89, 0xda,
69 0xff, 0xd6,
70 0x48, 0x8b, 0x44, 0x24, 0x08,
71 0x48, 0x8b, 0x4c, 0x24, 0x20,
72 0x48, 0x03, 0x01,
73 0x48, 0x8b, 0x6c, 0x24, 0x10,
74 0x48, 0x83, 0xc4, 0x18,
75 0xc3,
76 }
77 size := uint32(len(bc))
78 fn := Func{
79 ID: 0,
80 Flag: 0,
81 ArgsSize: 16,
82 EntryOff: 0,
83 TextSize: size,
84 DeferReturn: 0,
85 FileIndex: 0,
86 Name: "dummy",
87 }
88
89 fn.Pcsp = &Pcdata{
90 {PC: size, Val: 24},
91 }
92
93 fn.Pcline = &Pcdata{
94 {PC: 0x01, Val: 0},
95 {PC: 0x0e, Val: 1},
96 {PC: 0x1d, Val: 2},
97 {PC: size, Val: 3},
98 }
99
100 fn.Pcfile = &Pcdata{
101 {PC: size, Val: 0},
102 }
103
104 fn.PcUnsafePoint = &Pcdata{
105 {PC: size, Val: PCDATA_UnsafePointUnsafe},
106 }
107
108 fn.PcStackMapIndex = &Pcdata{
109 {PC: size, Val: 0},
110 }
111
112 args := rt.StackMapBuilder{}
113 args.AddField(true)
114 args.AddField(true)
115 fn.ArgsPointerMaps = args.Build()
116
117 locals := rt.StackMapBuilder{}
118 locals.AddField(false)
119 locals.AddField(false)
120 fn.LocalsPointerMaps = locals.Build()
121
122 rets := Load(bc, []Func{fn}, "dummy_module", []string{"github.com/bytedance/sonic/dummy.go"})
123 println("func address ", *(*unsafe.Pointer)(rets[0]))
124
125
126
127
128 f := *(*TestFunc)(unsafe.Pointer(&rets[0]))
129 i := 1
130 j := f(&i, hook)
131 require.Equal(t, 2, j)
132 require.Equal(t, "hook1", hstr)
133
134 fi := runtime.FuncForPC(*(*uintptr)(rets[0]))
135 require.Equal(t, "dummy", fi.Name())
136 file, line := fi.FileLine(0)
137 require.Equal(t, "github.com/bytedance/sonic/dummy.go", file)
138 require.Equal(t, 0, line)
139 }
140
View as plain text