Skip to content

Commit

Permalink
feat: check inlining and optimization when init mockey
Browse files Browse the repository at this point in the history
Change-Id: I3a362d2160ce4db55f9b4574f8fcf9e0cfb170f8
  • Loading branch information
Sychorius committed Oct 7, 2023
1 parent 7eec163 commit 2a5f529
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/monkey/mem/write_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"github.com/bytedance/mockey/internal/tool"
)

var writeFnPage = common.PageOf(reflect.ValueOf(write).Pointer())

func Write(target uintptr, data []byte) error {
targetPage := common.PageOf(target)
fnPage := common.PageOf(reflect.ValueOf(write).Pointer())
tool.DebugPrintf("Write: target page(0x%x), fn page(0x%x)\n", targetPage, fnPage)
tool.DebugPrintf("Write: target page(0x%x), fn page(0x%x), writeFn page(%x)\n", targetPage, fnPage, writeFnPage)
res := write(target, common.PtrOf(data), len(data), targetPage, common.PageSize(), syscall.PROT_READ|syscall.PROT_EXEC)
if res != 0 {
return fmt.Errorf("write failed, code %v", res)
Expand Down
20 changes: 20 additions & 0 deletions internal/tool/check_gcflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tool

import (
"os"
)

func init() {
// set MOCKEY_CHECK_GCFLAGS=false to disable this check
checkGCflags()
}

func checkGCflags() int {
if flag := os.Getenv("MOCKEY_CHECK_GCFLAGS"); flag != "false" && !IsGCFlagsSet() {
panic(`
Mockey init failed, did you forget to add -gcflags="all=-N -l" ?
(Set env MOCKEY_CHECK_GCFLAGS=false to disable gcflags check)
`)
}
return 0
}
36 changes: 36 additions & 0 deletions internal/tool/check_gcflags_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tool

import (
"reflect"
"unsafe"

"golang.org/x/arch/x86/x86asm"
)

func fn() {
}

Check failure on line 11 in internal/tool/check_gcflags_amd64.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
func fn2() {
fn()
}

Check failure on line 14 in internal/tool/check_gcflags_amd64.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
func IsGCFlagsSet() bool {
var asm []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&asm))
header.Data = reflect.ValueOf(fn2).Pointer()
header.Len = 1000
header.Cap = 1000

flag := false
pos := 0
for pos < len(asm) {
inst, _ := x86asm.Decode(asm[pos:], 64)
if inst.Op == x86asm.RET {
break
}
if inst.Op == x86asm.CALL {
flag = true
break
}
pos += int(inst.Len)
}
return flag
}
36 changes: 36 additions & 0 deletions internal/tool/check_gcflags_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tool

import (
"reflect"
"unsafe"

"golang.org/x/arch/arm64/arm64asm"
)

func fn() {
}
func fn2() {
fn()
}
func IsGCFlagsSet() bool {
var asm []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&asm))
header.Data = reflect.ValueOf(fn2).Pointer()
header.Len = 1000
header.Cap = 1000

flag := false
pos := 0
for pos < len(asm) {
inst, _ := arm64asm.Decode(asm[pos:])
if inst.Op == arm64asm.RET {
break
}
if inst.Op == arm64asm.BL {
flag = true
break
}
pos += int(unsafe.Sizeof(inst.Enc))
}
return flag
}

0 comments on commit 2a5f529

Please sign in to comment.