forked from pkujhd/goloader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert.go
439 lines (406 loc) · 16 KB
/
convert.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//go:build go1.18
// +build go1.18
package goloader
import (
"fmt"
"github.com/eh-steve/goloader/goversion"
"github.com/eh-steve/goloader/mprotect"
"log"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"strings"
"unsafe"
)
func CanAttemptConversion(oldValue interface{}, newType reflect.Type) bool {
oldT := efaceOf(&oldValue)._type
newT := fromRType(newType)
seen := map[_typePair]struct{}{}
return typesEqual(oldT, newT, seen)
}
func ConvertTypesAcrossModules(oldModule, newModule *CodeModule, oldValue interface{}, newType reflect.Type) (res interface{}, err error) {
defer func() {
if v := recover(); v != nil {
err = fmt.Errorf("unexpected panic (this is a bug): %v\n stack trace: %s", v, debug.Stack())
}
}()
// You can't just do a reflect.cvtDirect() across modules if composite types of oldValue/newValue contain an interface.
// The value stored in the interface could point to the itab from the old module, which might get unloaded
// So we need to recurse over the entire structure, and find any itabs and replace them with the equivalent from the new module
oldT := efaceOf(&oldValue)._type
newT := fromRType(newType)
seen := map[_typePair]struct{}{}
if !typesEqual(oldT, newT, seen) {
return nil, fmt.Errorf("old type %T and new type %s are not equal", oldValue, newType)
}
// Need to take data in old value and copy into new value one field at a time, but check that
// the type is either shared (first module) or translated from the old to the new modules
oldV := Indirect(ValueOf(&oldValue)).Elem()
cycleDetector := map[uintptr]*Value{}
typeHash := make(map[uint32][]*_type, len(newModule.module.typelinks))
buildModuleTypeHash(activeModules()[0], typeHash)
buildModuleTypeHash(newModule.module, typeHash)
cvt(oldModule, newModule, Value{oldV}, AsType(newT), nil, cycleDetector, typeHash)
return oldV.ConvertWithInterface(AsType(newT)).Interface(), err
}
func toType(t Type) *_type {
var x interface{} = t
return (*_type)(efaceOf(&x).data)
}
func fromRType(t reflect.Type) *_type {
var x interface{} = t
return (*_type)(efaceOf(&x).data)
}
type fakeValue struct {
typ *_type
ptr unsafe.Pointer
flag uintptr
}
func AsType(_typ *_type) Type {
var t interface{} = TypeOf("")
eface := efaceOf(&t)
eface.data = unsafe.Pointer(_typ)
return t.(Type)
}
func AsRType(_typ *_type) reflect.Type {
var t interface{} = reflect.TypeOf("")
eface := efaceOf(&t)
eface.data = unsafe.Pointer(_typ)
return t.(reflect.Type)
}
var closureFuncRegex = regexp.MustCompile(`^.*\.func[0-9]+$`)
func cvt(oldModule, newModule *CodeModule, oldValue Value, newType Type, oldValueBeforeElem *Value, cycleDetector map[uintptr]*Value, typeHash map[uint32][]*_type) {
// By this point we're sure that types are structurally equal, but their *_type addresses might not be
kind := oldValue.Kind()
if newType.Kind() != kind {
panic(fmt.Sprintf("old value's kind (%s) and new type (%s - %s) don't match", kind, newType.String(), newType.Kind()))
}
// Non-composite types of equal kind have same underlying type
if Bool <= kind && kind <= Complex128 || kind == String || kind == UnsafePointer {
return
}
switch kind {
case Array, Ptr, Slice:
elemKind := oldValue.Type().Elem().Kind()
if Bool <= elemKind && elemKind <= Complex128 || elemKind == String || elemKind == UnsafePointer {
// Shortcut for non-composite types
return
}
}
// Composite types.
switch kind {
case Interface:
innerVal := oldValue.Elem()
if innerVal.Kind() == Invalid {
return
}
oldTInner := toType(innerVal.Type())
oldTOuter := toType(oldValue.Type())
var newTypeInner *_type
var newTypeOuter *_type
types := typeHash[oldTInner.hash]
for _, _typeNew := range types {
seen := map[_typePair]struct{}{}
if oldTInner == _typeNew || typesEqual(oldTInner, _typeNew, seen) {
newTypeInner = _typeNew
break
}
}
types = typeHash[oldTOuter.hash]
for _, _typeNew := range types {
seen := map[_typePair]struct{}{}
if oldTOuter == _typeNew || typesEqual(oldTOuter, _typeNew, seen) {
newTypeOuter = _typeNew
break
}
}
if newTypeInner == nil {
oldTAddr := uintptr(unsafe.Pointer(oldTInner))
if innerVal.Type().PkgPath() == "" || (firstmoduledata.types >= oldTAddr && oldTAddr < firstmoduledata.etypes) {
newTypeInner = oldTInner
} else {
panic(fmt.Sprintf("new module does not contain equivalent type for %s (hash %d)", innerVal.Type(), toType(innerVal.Type()).hash))
}
}
if newTypeOuter == nil {
oldTAddr := uintptr(unsafe.Pointer(oldTOuter))
if oldValue.Type().PkgPath() == "" || (firstmoduledata.types >= oldTAddr && oldTAddr < firstmoduledata.etypes) {
newTypeOuter = oldTOuter
} else {
panic(fmt.Sprintf("new module does not contain equivalent type for %s (hash %d)", oldValue.Type(), toType(oldValue.Type()).hash))
}
}
newInnerType := AsType(newTypeInner)
newOuterType := AsType(newTypeOuter)
tt := (*interfacetype)(unsafe.Pointer(newTypeOuter))
if len(tt.mhdr) > 0 {
iface := (*nonEmptyInterface)(((*fakeValue)(unsafe.Pointer(&oldValue))).ptr)
if iface.itab == nil {
// nil value in interface, no further work required
return
} else {
// Need to check whether itab points at old module, and find the equivalent itab in the new module and point to that instead
var oldItab *itab
for _, o := range oldModule.module.itablinks {
if iface.itab == o {
oldItab = o
break
}
}
if oldItab != nil {
var newItab *itab
for _, n := range newModule.module.itablinks {
// Need to compare these types carefully
if oldItab.inter.typ.hash == n.inter.typ.hash && oldItab._type.hash == n._type.hash {
seen := map[_typePair]struct{}{}
if typesEqual(&oldItab.inter.typ, &n.inter.typ, seen) && typesEqual(oldItab._type, n._type, seen) {
newItab = n
break
}
}
}
if newItab == nil {
panic(fmt.Sprintf("could not find equivalent itab for interface %s type %s in new module.", oldValue.Type().String(), oldValue.Elem().Type().String()))
}
iface.itab = newItab
}
}
} else {
eface := (*emptyInterface)(((*fakeValue)(unsafe.Pointer(&oldValue))).ptr)
eface._type = newTypeInner
}
innerValKind := innerVal.Kind()
if !(Bool <= innerValKind && innerValKind <= Complex128 || innerValKind == String || innerValKind == UnsafePointer) {
cvt(oldModule, newModule, Value{innerVal}, newInnerType, &oldValue, cycleDetector, typeHash)
} else {
if innerVal.CanConvert(newInnerType) {
newVal := innerVal.Convert(newInnerType)
if !oldValue.CanSet() {
if !oldValue.CanAddr() {
if oldValueBeforeElem != nil && oldValueBeforeElem.Kind() == Interface {
oldValueBeforeElem.Set(newVal)
} else {
panic(fmt.Sprintf("can't set old value of type %s with new value %s (can't address or indirect)", oldValue.Type(), newVal.Type()))
}
} else {
NewAt(newOuterType, unsafe.Pointer(oldValue.UnsafeAddr())).Elem().Set(newVal)
}
} else {
oldValue.Set(newVal)
}
} else {
panic(fmt.Sprintf("can't convert old value of type %s with new value %s", innerVal.Type(), newInnerType))
}
}
case Func:
oldPtr := oldValue.Pointer()
if oldPtr != 0 {
if oldPtr < firstmoduledata.text || oldPtr >= firstmoduledata.etext {
if oldPtr >= oldModule.module.text && oldPtr < oldModule.module.etext {
// If the func points at code inside the old module, we need to either find the address of
// the equivalent func by name, or error if we can't find it
oldF := runtime.FuncForPC(oldPtr)
oldFName := oldF.Name()
if oldFName == "" {
panic(fmt.Sprintf("old value's function pointer 0x%x does not have a name - cannot convert anonymous functions", oldPtr))
}
found := false
for _, f := range newModule.module.ftab {
_func := (*_func)(unsafe.Pointer(&(newModule.module.pclntable[f.funcoff])))
name := getfuncname(_func, newModule.module)
if name == oldFName {
entry := getfuncentry(_func, newModule.module.text)
// This is actually unsafe, because there's no guarantee that the new version
// of the function has the same signature as the old, and there's no way of accessing
// the function *_type from just a PC addr, unless the compiler populated a ptab.
log.Printf("WARNING - converting functions %s by name - no guarantees that signatures will match \n", oldFName)
newValue := oldValue
manipulation := (*fakeValue)(unsafe.Pointer(&newValue))
var funcContainer unsafe.Pointer
if strings.HasSuffix(oldFName, "-fm") {
// This is a method, so the data pointer in the value is actually to a closure struct { F uintptr; R *receiver }
// and the function pointer is to a wrapper func which accepts this struct as its argument
closure := *(**struct {
F uintptr
R unsafe.Pointer
})(manipulation.ptr)
// We need to not only set the func entrypoint, but also convert the receiver and set that too
// TODO - how can we find out the receiver's type in order to convert across modules?
// This code might not be safe if the receivers then call other methods?
// Now check whether the old closure.F is an itab method or a concrete type
var oldItab *itab
// This deref of the receiver into an 8 byte word is 100% unsafe, but I can't figure out how to find out what the type of R is...
recvVal := (*itab)(closure.R)
for _, itab := range oldModule.module.itablinks {
if itab.inter == recvVal.inter && itab._type == recvVal._type {
oldItab = itab
}
}
if oldItab != nil {
var newItab *itab
for _, n := range newModule.module.itablinks {
// Need to compare these types carefully
if oldItab.inter.typ.hash == n.inter.typ.hash && oldItab._type.hash == n._type.hash {
seen := map[_typePair]struct{}{}
if typesEqual(&oldItab.inter.typ, &n.inter.typ, seen) && typesEqual(oldItab._type, n._type, seen) {
newItab = n
break
}
}
}
if newItab == nil {
panic(fmt.Sprintf("could not find equivalent itab for interface %s type %s in new module.", oldValue.Type().String(), oldValue.Elem().Type().String()))
}
closure.R = unsafe.Pointer(newItab)
}
funcContainer = unsafe.Pointer(closure)
closure.F = entry
} else if closureFuncRegex.MatchString(oldFName) {
containerSym, haveContainerSym := newModule.Syms[oldFName+"·f"]
if haveContainerSym && goversion.GoVersion() > 18 {
funcContainer = unsafe.Pointer(containerSym)
} else {
// This is a closure which is unlikely to be safe since the variables it closes over might be in the old module's memory
closure := *(**struct {
F uintptr
// ... <- variables which are captured by the closure would follow, but we can't know how many they are or what their types are - the best we can do is switch the function implementation and keep the variables the same
})(manipulation.ptr)
if runtime.GOARCH == "arm64" && runtime.GOOS == "darwin" {
err := mprotect.MprotectMakeWritable(mprotect.GetPage(uintptr(unsafe.Pointer(closure))))
if err != nil {
panic(fmt.Sprintf("failed to make page of closure writable: %s", err))
}
}
closure.F = entry
funcContainer = unsafe.Pointer(closure)
log.Printf("EVEN BIGGER WARNING - converting anonymous function %s by name - no guarantees that signatures, or the closed over variable sizes, or types will match. This is dangerous! \n", oldFName)
}
} else {
containerSym, haveContainerSym := newModule.Syms[oldFName+"·f"]
if haveContainerSym {
funcContainer = unsafe.Pointer(containerSym)
} else {
// PC addresses for functions are 2 levels of indirection from a reflect value's word addr,
// so we allocate addresses on the heap to hold the indirections
// Normally the RODATA has a pkgname.FuncName·f symbol which stores this - Ideally we would use that instead of the heap
// TODO - is this definitely safe from GC?
funcPtr := new(uintptr)
*funcPtr = entry
funcContainer = unsafe.Pointer(funcPtr)
}
}
funcPtrContainer := new(unsafe.Pointer)
*funcPtrContainer = funcContainer
manipulation.ptr = unsafe.Pointer(funcPtrContainer)
manipulation.typ = toType(newType)
doSet := true
if !oldValue.CanSet() {
if oldValue.CanAddr() {
oldValue = Value{NewAt(newType, unsafe.Pointer(oldValue.UnsafeAddr())).Elem()}
} else {
if oldValueBeforeElem != nil && oldValueBeforeElem.Kind() == Interface {
doSet = false
oldValueBeforeElem.Set(newValue.Value)
} else {
panic(fmt.Sprintf("can't set old func of type %s with new value 0x%x (can't address or indirect)", oldValue.Type(), entry))
}
}
}
if doSet {
oldValue.Set(newValue.Value)
}
found = true
break
}
}
if !found {
panic(fmt.Sprintf("old value's function pointer 0x%x with name %s has no equivalent name in new module - cannot convert", oldPtr, oldFName))
}
} else {
panic(fmt.Sprintf("old value's function pointer 0x%x not in first module (0x%x - 0x%x) nor old module (0x%x - 0x%x) - cannot convert", oldPtr, firstmoduledata.text, firstmoduledata.etext, oldModule.module.text, oldModule.module.etext))
}
}
}
case Array, Slice:
for i := 0; i < oldValue.Len(); i++ {
cvt(oldModule, newModule, Value{oldValue.Index(i)}, newType.Elem(), nil, cycleDetector, typeHash)
}
case Map:
if oldValue.Len() == 0 {
return
}
keyType := oldValue.Type().Key()
valType := oldValue.Type().Elem()
mvKind := valType.Kind()
mkKind := keyType.Kind()
if !(Bool <= mvKind && mvKind <= Complex128 || mvKind == String || mvKind == UnsafePointer) ||
!(Bool <= mkKind && mkKind <= Complex128 || mkKind == String || mkKind == UnsafePointer) {
// Need to recreate map entirely since they aren't mutable
newMap := MakeMapWithSize(oldValue.Type(), oldValue.Len())
mapKeys := oldValue.MapKeys()
for _, mapKey := range mapKeys {
mapValue := oldValue.MapIndex(mapKey)
var nk Value
if mkKind == Ptr {
nk = Value{New(keyType.Elem())}
} else {
nk = Value{Indirect(New(keyType))}
}
var nv Value
if mvKind == Ptr {
nv = Value{New(valType.Elem())}
} else {
nv = Value{Indirect(New(valType))}
}
if mkKind == Ptr {
nk.Elem().Set(mapKey.Elem())
} else {
nk.Set(mapKey)
}
if mvKind == Ptr {
nv.Elem().Set(mapValue.Elem())
} else {
nv.Set(mapValue)
}
cvt(oldModule, newModule, nv, newType.Elem(), &oldValue, cycleDetector, typeHash)
cvt(oldModule, newModule, nk, newType.Key(), &oldValue, cycleDetector, typeHash)
newMap.SetMapIndex(nk.Value, nv.Value)
}
doSet := true
if !oldValue.CanSet() {
if oldValue.CanAddr() {
oldValue = Value{NewAt(oldValue.Type(), unsafe.Pointer(oldValue.UnsafeAddr())).Elem()}
} else {
if oldValueBeforeElem != nil && oldValueBeforeElem.Kind() == Interface {
doSet = false
oldValueBeforeElem.Set(newMap)
} else {
panic(fmt.Sprintf("can't set old map of type %s with new value (can't address or indirect)", oldValue.Type()))
}
}
}
if doSet {
oldValue.Set(newMap)
}
}
case Ptr:
if !oldValue.IsNil() {
up := oldValue.Pointer()
if _, cyclic := cycleDetector[up]; cyclic {
return
} else {
cycleDetector[up] = &oldValue
cvt(oldModule, newModule, Value{oldValue.Elem()}, newType.Elem(), &oldValue, cycleDetector, typeHash)
}
}
case Struct:
for i := 0; i < oldValue.NumField(); i++ {
field := oldValue.Field(i)
fieldKind := field.Kind()
if !(Bool <= fieldKind && fieldKind <= Complex128 || fieldKind == String || fieldKind == UnsafePointer) {
cvt(oldModule, newModule, Value{field}, newType.Field(i).Type, nil, cycleDetector, typeHash)
}
}
}
}