Code Monkey home page Code Monkey logo

Comments (3)

bmslaravel avatar bmslaravel commented on August 20, 2024
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
}

from hookingo.

bmslaravel avatar bmslaravel commented on August 20, 2024

不知道什么原因在 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())
}

from hookingo.

bmslaravel avatar bmslaravel commented on August 20, 2024

windows10,go1.20.3 版本下的

PS D:\workspace\helium> go run -gcflags -l .
int
8
struct { F uintptr; text string; start *int }
32
sure!

from hookingo.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.