hno.Origin().(func(typ unsafe.Pointer) unsafe.Pointer) 断言失败
bmslaravel opened this issue · comments
func fno(typ unsafe.Pointer) unsafe.Pointer {
t := reflect.TypeOf(0)
(*(*[2]unsafe.Pointer)(unsafe.Pointer(&t)))[1] = typ // 相当于反射了闭包类型
println(t.String())
fn, ok := hno.Origin().(func(typ unsafe.Pointer) unsafe.Pointer)
if ok {
return fn(typ) // 调用原runtime.newobject
}
println("ok", ok) // ok false
}
不知道什么原因在 go1.17 版本和 go1.20.3 版本中 windows10 系统下一直在 hno.Origin().(func(typ unsafe.Pointer) unsafe.Pointer) 导致无法调用原runtime.newobject,因此这种的处理了下,以便正常看到闭包的结构:
package main
import (
"github.com/fengyoulin/hookingo"
"reflect"
"unsafe"
)
var hno hookingo.Hook
//go:linkname newobject runtime.newobject
func newobject(typ unsafe.Pointer) unsafe.Pointer
//go:linkname newobject2 runtime.mallocgc
func newobject2(size uintptr, typ unsafe.Pointer, b bool) unsafe.Pointer
func fno(typ unsafe.Pointer) unsafe.Pointer {
t := reflect.TypeOf(0)
(*(*[2]unsafe.Pointer)(unsafe.Pointer(&t)))[1] = typ // 相当于反射了闭包类型
println(t.String())
//fn, ok := hno.Origin().(func(typ unsafe.Pointer) unsafe.Pointer)
//if ok {
// return fn(typ) // 调用原runtime.newobject
//}
//println("ok", ok)
//os.Exit(1)
println(*(*uintptr)(unsafe.Pointer(typ)))
return newobject2(*(*uintptr)(unsafe.Pointer(typ)), typ, true)
}
// 创建一个闭包,make closure
func mc(start int, text string) func() string {
return func() string {
l := len(text)
s := start % l
r := text[s:]
start++
return r
}
}
func main() {
var err error
hno, err = hookingo.Apply(newobject, fno) // 应用钩子,替换函数
if err != nil {
panic(err)
}
f := mc(10, "hello, closure!")
println(f())
}
windows10,go1.20.3 版本下的
PS D:\workspace\helium> go run -gcflags -l .
int
8
struct { F uintptr; text string; start *int }
32
sure!