/
link_name.go
158 lines (138 loc) · 4.87 KB
/
link_name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package patch
import (
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/typecheck"
xgo_ctxt "cmd/compile/internal/xgo_rewrite_internal/patch/ctxt"
xgo_record "cmd/compile/internal/xgo_rewrite_internal/patch/record"
xgo_syntax "cmd/compile/internal/xgo_rewrite_internal/patch/syntax"
"cmd/internal/src"
"strings"
)
const xgoRuntimePkgPrefix = xgo_ctxt.XgoRuntimePkg + "/"
const xgoTestPkgPrefix = xgo_ctxt.XgoModule + "/test/"
const xgoRuntimeTrapPkg = xgoRuntimePkgPrefix + "trap"
// accepts interface{} as argument
const xgoOnTestStart = "__xgo_on_test_start"
const XgoLinkSetTrap = "__xgo_link_set_trap"
const XgoTrapForGenerated = "__xgo_trap_for_generated"
const setTrap = "__xgo_set_trap"
// only allowed from reflect
const reflectSetImpl = "__xgo_set_all_method_by_name_impl"
var linkMap = map[string]string{
"__xgo_link_getcurg": "__xgo_getcurg",
"__xgo_link_set_trap": setTrap,
xgo_syntax.XgoLinkTrapForGenerated: XgoTrapForGenerated,
"__xgo_link_init_finished": "__xgo_init_finished",
"__xgo_link_on_init_finished": "__xgo_on_init_finished",
"__xgo_link_on_gonewproc": "__xgo_on_gonewproc",
"__xgo_link_on_goexit": "__xgo_on_goexit",
"__xgo_link_on_test_start": xgoOnTestStart,
"__xgo_link_get_test_starts": "__xgo_get_test_starts",
"__xgo_link_retrieve_all_funcs_and_clear": "__xgo_retrieve_all_funcs_and_clear",
"__xgo_link_peek_panic": "__xgo_peek_panic",
"__xgo_link_mem_equal": "__xgo_mem_equal",
"__xgo_link_get_pc_name": "__xgo_get_pc_name",
xgo_syntax.XgoLinkGeneratedRegisterFunc: "__xgo_register_func",
// reflect (not enabled)
// "__xgo_link_set_all_method_by_name_impl": reflectSetImpl,
// "__xgo_link_get_all_method_by_name": "__xgo_get_all_method_by_name",
}
// a switch to control link
const disableXgoLink bool = false
func isLinkValid(fnName string, targetName string, pkgPath string) bool {
if disableXgoLink {
return false
}
safeGenerated := (fnName == xgo_syntax.XgoLinkGeneratedRegisterFunc || fnName == xgo_syntax.XgoLinkTrapForGenerated)
if safeGenerated {
// generated by xgo on the fly for every instrumented package
return true
}
isReflect := targetName == reflectSetImpl
if isReflect {
// the special reflect
return pkgPath == "reflect"
}
isLinkTrap := fnName == XgoLinkSetTrap
if isLinkTrap {
// the special trap
return pkgPath == xgoRuntimeTrapPkg || strings.HasPrefix(pkgPath, xgoTestPkgPrefix)
}
// no special link, must be inside xgoRuntime, or test
if pkgPath == "testing" {
return true
}
if strings.HasPrefix(pkgPath, xgoRuntimePkgPrefix) {
return true
}
if strings.HasPrefix(pkgPath, xgoTestPkgPrefix) {
return true
}
return false
}
// for go1.20 and above, needs to convert
const needConvertArg = goMajor > 1 || (goMajor == 1 && goMinor >= 20)
func replaceWithRuntimeCall(fn *ir.Func, name string) {
if false {
debugReplaceBody(fn)
// newBody = []ir.Node{debugPrint("replaced body")}
return
}
isRuntime := true
var runtimeFunc *ir.Name
if isRuntime {
runtimeFunc = typecheck.LookupRuntime(name)
} else {
// NOTE: cannot reference testing package
// only runtime is available
// lookup testing
testingPkg := findTestingPkg()
sym := testingPkg.Lookup(name)
if sym.Def != nil {
runtimeFunc = sym.Def.(*ir.Name)
} else {
runtimeFunc = NewNameAt(fn.Pos(), sym, fn.Type())
runtimeFunc.Class = ir.PEXTERN
}
}
params := fn.Type().Params()
results := fn.Type().Results()
paramNames := getTypeNames(params)
if name == XgoTrapForGenerated {
// fill PC by
getCallerPC := typecheck.LookupRuntime("getcallerpc")
paramNames[1] = ir.NewCallExpr(fn.Pos(), ir.OCALL, getCallerPC, nil)
}
resNames := getTypeNames(results)
fnPos := fn.Pos()
if needConvertArg && name == xgoOnTestStart {
for i, p := range paramNames {
paramNames[i] = convToEFace(fnPos, p, p.(*ir.Name).Type(), false)
}
}
var callNode ir.Node
callNode = ir.NewCallExpr(fnPos, ir.OCALL, runtimeFunc, paramNames)
if len(resNames) > 0 {
// if len(resNames) == 1 {
// callNode = ir.NewAssignListStmt(fnPos, ir.OAS, resNames, []ir.Node{callNode})
// } else {
callNode = ir.NewReturnStmt(fnPos, []ir.Node{callNode})
// callNode = ir.NewAssignListStmt(fnPos, ir.OAS2, resNames, []ir.Node{callNode})
// callNode = ir.NewAssignListStmt(fnPos, ir.OAS2, resNames, []ir.Node{callNode})
// }
}
replaceFuncBodyWithPos(base.AutogeneratedPos, fn, []ir.Node{
// debugPrint("debug getg"),
callNode,
})
}
func replaceFuncBody(fn *ir.Func, nodes []ir.Node) {
replaceFuncBodyWithPos(fn.Pos(), fn, nodes)
}
func replaceFuncBodyWithPos(pos src.XPos, fn *ir.Func, nodes []ir.Node) {
node := ifConstant(pos, true, nodes, fn.Body)
fn.Body = []ir.Node{node}
xgo_record.SetRewrittenBody(fn, fn.Body)
typeCheckBody(fn)
}