Skip to content

Commit

Permalink
fix 32 bit error && format code
Browse files Browse the repository at this point in the history
  • Loading branch information
kmalloc committed Jul 4, 2019
1 parent f0cb654 commit a1223da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
36 changes: 26 additions & 10 deletions arch_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"errors"
"fmt"
"golang.org/x/arch/x86/x86asm"
"math"
"unsafe"

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

type CodeFix struct {
Expand Down Expand Up @@ -199,7 +201,7 @@ func adjustInstructionOffset(code []byte, off int64) ([]byte, error) {
return code, nil
}
if isIntOverflow(off) {
return nil, fmt.Errorf("int overflow in adjusting offset")
return nil, fmt.Errorf("int overflow in adjusting 6-bytes inst offset")
}
code[2] = byte(off)
code[3] = byte(off >> 8)
Expand All @@ -211,7 +213,7 @@ func adjustInstructionOffset(code []byte, off int64) ([]byte, error) {
return code, nil
}
if isIntOverflow(off) {
return nil, fmt.Errorf("int overflow in adjusting offset")
return nil, fmt.Errorf("int overflow in adjusting 5-bytes inst offset")
}
code[1] = byte(off)
code[2] = byte(off >> 8)
Expand Down Expand Up @@ -521,9 +523,13 @@ func adjustJmpOffset(mode int, start, delem uintptr, funcSize, moveSize int, ins
}

if absAddr != uintptr(0) {
off := int64(absAddr - curAddr - uintptr(len(code)))
delta := absAddr - curAddr - uintptr(len(code))
off := int64(delta)
if unsafe.Sizeof(uintptr(0)) == unsafe.Sizeof(int32(0)) {
off = int64(int32(off))
}

//fmt.Printf("adjust inst at:%x, sz:%d, delem:%x, target:%x, funcEnd:%x, off:%x\n", curAddr, len(code), delem, absAddr, funcEnd, uintptr(off))
// fmt.Printf("adjust inst at:%x, sz:%d, delem:%x, target:%x, funcEnd:%x, off:%x\n", curAddr, len(code), delem, absAddr, funcEnd, uintptr(off))

if (curAddr < delem || curAddr >= funcEnd) && absAddr > delem && absAddr < funcEnd {
off += int64(moveSize)
Expand All @@ -535,7 +541,7 @@ func adjustJmpOffset(mode int, start, delem uintptr, funcSize, moveSize int, ins

c, err := adjustInstructionOffset(code, off)
if err != nil {
return err
return fmt.Errorf("err occurs adjusting inst, addr:%x,off:%x,err:%s", curAddr, off, err.Error())
}

inst[i].Code = c
Expand Down Expand Up @@ -582,7 +588,7 @@ func translateShortJump(mode int, addr, to uintptr, inst []CodeFix, funcSz int,
}

inst[i].Code = nc
// fmt.Printf("extent overflow inst at:%x, toAddr:%x, sz:%d\n", curAddr, inst[i].Addr, len(nc))
// fmt.Printf("extent overflow inst at:%x, sz:%d, move sz:%d\n", curAddr, len(nc), move_sz)

err = adjustJmpOffset(mode, addr, curAddr, funcSz, delta, inst[i:])
if err != nil {
Expand Down Expand Up @@ -672,9 +678,8 @@ func fixFuncInstructionInplace(mode int, addr, to uintptr, funcSz int, move_sz i
return nil, err
}

// fmt.Printf("translate short jump done, addr:%x, to:%x, total:%d\n", addr, to, len(fix))

fix, err1 := doFixTargetFuncCode(true, mode, addr, funcSz, to, move_sz, fix)

if err1 != nil {
return fix, err1
}
Expand Down Expand Up @@ -714,7 +719,18 @@ func genJumpCode(mode int, to, from uintptr) []byte {
// 2. otherwise, push target, then ret

var code []byte
relative := (uint32(math.Abs(float64(from-to))) < 0x7fffffff)

delta := int64(from - to)
if unsafe.Sizeof(uintptr(0)) == unsafe.Sizeof(int32(0)) {
delta = int64(int32(from - to))
}

relative := (delta <= 0x7fffffff)

if delta < 0 {
delta = -delta
relative = (delta <= 0x80000000)
}

// relative = false

Expand Down
9 changes: 6 additions & 3 deletions hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package gohook
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"unsafe"

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

func myPrintf(f string, a ...interface{}) (n int, err error) {
Expand All @@ -32,6 +33,8 @@ func init() {
} else {
fmt.Printf("hook fmt.Printf() done\n")
}

fmt.Printf("debug info for init():%s\n", ShowDebugInfo())
}

//go:noinline
Expand Down Expand Up @@ -940,11 +943,11 @@ func TestInplaceFixAtMoveArea(t *testing.T) {

sz1 := 5
na1 := trampoline + uintptr(2)
ta1 := target + uintptr(2 + 5 + 4 - 3)
ta1 := target + uintptr(2+5+4-3)
off1 := ta1 - (na1 + uintptr(sz1))

sz2 := 6
na2 := target + uintptr(15 + 3 - 3)
na2 := target + uintptr(15+3-3)
ta2 := trampoline + uintptr(1)
off2 := ta2 - (na2 + uintptr(sz2))

Expand Down
2 changes: 0 additions & 2 deletions utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func hookFunction(mode int, target, replace, trampoline uintptr) (*CodeInfo, err
err1 := doFixFuncInplace(mode, target, trampoline, int(sz1), insLen, info, len(jumpcode))
if err1 != nil {
info.How = "copy"
// fmt.Printf("fix inplace failed, %s\n", err1.Error())
origin, err2 := doCopyFunction(mode, false, target, trampoline, sz1, sz2, info)
if err2 != nil {
return nil, fmt.Errorf("both fix/fix2/copy failed, fix:%s, fix2:%s, copy:%s", err.Error(), err1.Error(), err2.Error())
Expand Down Expand Up @@ -209,7 +208,6 @@ func hookFunction(mode int, target, replace, trampoline uintptr) (*CodeInfo, err
}

func printInstructionFix(v CodeFix, origin []byte) {
// fmt.Printf("addr:0x%x, code:", v.Addr)
for _, c := range v.Code {
fmt.Printf(" %x", c)
}
Expand Down

0 comments on commit a1223da

Please sign in to comment.