Skip to content

Commit

Permalink
Not support refer to internal symbols in the standard library anymore…
Browse files Browse the repository at this point in the history
…, as the linker have changed since go1.23
  • Loading branch information
timandy committed Aug 14, 2024
1 parent aa049b4 commit 196812b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
12 changes: 0 additions & 12 deletions g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,11 @@ import (
"unsafe"
)

// g0 the value of runtime.g0.
//
//go:linkname g0 runtime.g0
var g0 struct{}

// getgp returns the pointer to the current runtime.g.
//
//go:nosplit
func getgp() unsafe.Pointer

// getg0 returns the value of runtime.g0.
//
//go:nosplit
func getg0() interface{} {
return packEface(getgt(), unsafe.Pointer(&g0))
}

// getgt returns the type of runtime.g.
//
//go:nosplit
Expand Down
18 changes: 9 additions & 9 deletions g/g_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ func TestGetgp(t *testing.T) {
})
}

func TestGetg0(t *testing.T) {
runTest(t, func() {
g := getg0()
runtime.GC()
stackguard0 := reflect.ValueOf(g).FieldByName("stackguard0")
assert.Greater(t, stackguard0.Uint(), uint64(0))
})
}

func TestGetgt(t *testing.T) {
runTest(t, func() {
gt := getgt()
Expand All @@ -44,6 +35,15 @@ func TestGetgt(t *testing.T) {
})
}

func TestGetg(t *testing.T) {
runTest(t, func() {
g := packEface(getgt(), getgp())
runtime.GC()
stackguard0 := reflect.ValueOf(g).FieldByName("stackguard0")
assert.Greater(t, stackguard0.Uint(), uint64(0))
})
}

func runTest(t *testing.T, fun func()) {
run := false
wg := &sync.WaitGroup{}
Expand Down
24 changes: 21 additions & 3 deletions gohack_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package gohack

import (
"bytes"
"fmt"
"reflect"
"runtime"
"strconv"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

var goroutineSpace = []byte("goroutine ")

func TestG_Goid(t *testing.T) {
runTest(t, func() {
gp := getg()
Expand Down Expand Up @@ -97,9 +102,22 @@ func TestOffset(t *testing.T) {
}

// curGoroutineID parse the current g's goid from caller stack.
//
//go:linkname curGoroutineID net/http.http2curGoroutineID
func curGoroutineID() int64
func curGoroutineID() int64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
// Parse the 4707 out of "goroutine 4707 ["
b = bytes.TrimPrefix(b, goroutineSpace)
i := bytes.IndexByte(b, ' ')
if i < 0 {
panic(fmt.Sprintf("No space found in %q", b))
}
b = b[:i]
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
}
return n
}

// setPanicOnFault controls the runtime's behavior when a program faults at an unexpected (non-nil) address.
//
Expand Down
5 changes: 0 additions & 5 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import (
//go:linkname getgp github.com/timandy/gohack/g.getgp
func getgp() unsafe.Pointer

// getg0 returns the value of runtime.g0.
//
//go:linkname getg0 github.com/timandy/gohack/g.getg0
func getg0() interface{}

// getgt returns the type of runtime.g.
//
//go:linkname getgt github.com/timandy/gohack/g.getgt
Expand Down
22 changes: 13 additions & 9 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"sync"
"sync/atomic"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

//go:linkname packEface github.com/timandy/gohack/g.packEface
func packEface(typ reflect.Type, p unsafe.Pointer) (i interface{})

func TestGetgp(t *testing.T) {
gp0 := getgp()
runtime.GC()
Expand All @@ -25,15 +29,6 @@ func TestGetgp(t *testing.T) {
})
}

func TestGetg0(t *testing.T) {
runTest(t, func() {
g0 := getg0()
runtime.GC()
stackguard0 := reflect.ValueOf(g0).FieldByName("stackguard0")
assert.Greater(t, stackguard0.Uint(), uint64(0))
})
}

func TestGetgt(t *testing.T) {
fmt.Println("*** GOOS:", runtime.GOOS, "***")
fmt.Println("*** GOARCH:", runtime.GOARCH, "***")
Expand Down Expand Up @@ -73,6 +68,15 @@ func TestGetgt(t *testing.T) {
})
}

func TestGetg(t *testing.T) {
runTest(t, func() {
g0 := packEface(getgt(), getgp())
runtime.GC()
stackguard0 := reflect.ValueOf(g0).FieldByName("stackguard0")
assert.Greater(t, stackguard0.Uint(), uint64(0))
})
}

func runTest(t *testing.T, fun func()) {
var count int32
wg := &sync.WaitGroup{}
Expand Down

0 comments on commit 196812b

Please sign in to comment.