@@ -15,15 +15,19 @@ type (
15
15
target * reflect.Value
16
16
redirection * reflect.Value
17
17
}
18
- pointer struct {
19
- length uintptr
20
- ptr uintptr
18
+ sliceHeader struct {
19
+ Data unsafe.Pointer
20
+ Len int
21
+ Cap int
21
22
}
22
23
)
23
24
25
+ //go:linkname getInternalPtrFromValue reflect.(*Value).pointer
26
+ func getInternalPtrFromValue (v * reflect.Value ) unsafe.Pointer
27
+
24
28
var (
25
29
patchLock = sync.Mutex {}
26
- patches = make (map [uintptr ]* Patch )
30
+ patches = make (map [unsafe. Pointer ]* Patch )
27
31
pageSize = syscall .Getpagesize ()
28
32
)
29
33
@@ -99,7 +103,7 @@ func isPatchable(target, redirection *reflect.Value) error {
99
103
if target .Type () != redirection .Type () {
100
104
return errors .New (fmt .Sprintf ("the target and/or redirection doesn't have the same type: %s != %s" , target .Type (), redirection .Type ()))
101
105
}
102
- if _ , ok := patches [target . Pointer ( )]; ok {
106
+ if _ , ok := patches [getSafePointer ( target )]; ok {
103
107
return errors .New ("the target is already patched" )
104
108
}
105
109
return nil
@@ -108,8 +112,8 @@ func isPatchable(target, redirection *reflect.Value) error {
108
112
func applyPatch (patch * Patch ) error {
109
113
patchLock .Lock ()
110
114
defer patchLock .Unlock ()
111
- tPointer := patch .target . Pointer ( )
112
- rPointer := getInternalPtrFromValue (* patch .redirection )
115
+ tPointer := getSafePointer ( patch .target )
116
+ rPointer := getInternalPtrFromValue (patch .redirection )
113
117
rPointerJumpBytes , err := getJumpFuncBytes (rPointer )
114
118
if err != nil {
115
119
return err
@@ -132,7 +136,7 @@ func applyUnpatch(patch *Patch) error {
132
136
if patch .targetBytes == nil || len (patch .targetBytes ) == 0 {
133
137
return errors .New ("the target is not patched" )
134
138
}
135
- tPointer := patch .target . Pointer ( )
139
+ tPointer := getSafePointer ( patch .target )
136
140
if _ , ok := patches [tPointer ]; ! ok {
137
141
return errors .New ("the target is not patched" )
138
142
}
@@ -144,10 +148,6 @@ func applyUnpatch(patch *Patch) error {
144
148
return nil
145
149
}
146
150
147
- func getInternalPtrFromValue (value reflect.Value ) uintptr {
148
- return (* pointer )(unsafe .Pointer (& value )).ptr
149
- }
150
-
151
151
func getValueFrom (data interface {}) reflect.Value {
152
152
if cValue , ok := data .(reflect.Value ); ok {
153
153
return cValue
@@ -156,14 +156,18 @@ func getValueFrom(data interface{}) reflect.Value {
156
156
}
157
157
}
158
158
159
- func getMemorySliceFromPointer (p uintptr , length int ) []byte {
160
- return * (* []byte )(unsafe .Pointer (& reflect. SliceHeader {
159
+ func getMemorySliceFromPointer (p unsafe. Pointer , length int ) []byte {
160
+ return * (* []byte )(unsafe .Pointer (& sliceHeader {
161
161
Data : p ,
162
162
Len : length ,
163
163
Cap : length ,
164
164
}))
165
165
}
166
166
167
- func getPageStartPtr (ptr uintptr ) uintptr {
168
- return ptr & ^ (uintptr (pageSize - 1 ))
167
+ func getSafePointer (value * reflect.Value ) unsafe.Pointer {
168
+ p := getInternalPtrFromValue (value )
169
+ if p != nil {
170
+ p = * (* unsafe .Pointer )(p )
171
+ }
172
+ return p
169
173
}
0 commit comments