Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复代码中的一些警告问题 #15

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions monkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func checkStructMonkeyType(a, b reflect.Type) bool {
t1 := a.In(0).String()
t2 := b.In(0).String()

if strings.Index(t2, "__monkey__") == -1 {
if !strings.Contains(t2, "__monkey__") {
return false
}

Expand Down Expand Up @@ -158,12 +158,12 @@ func PatchEmpty(target interface{}) {

t := reflect.ValueOf(target).Pointer()

p, ok := patches[t]
_, ok := patches[t]
if ok {
return
}

p = &patch{from: t}
p := &patch{from: t}
patches[t] = p
p.Apply()
}
Expand Down Expand Up @@ -207,10 +207,6 @@ func unpatchValue(target reflect.Value) bool {
return patch.Del()
}

func unpatch(target uintptr, p *patch) {
copyToLocation(target, p.original)
}

type patch struct {
from uintptr
realFrom uintptr
Expand Down
22 changes: 11 additions & 11 deletions monkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestEmpty(t *testing.T) {
}
}(j)
monkey.Patch(foo, f)
assert(t, 1+1+j == foo(1, 1))
assert(t, foo(1, 1) == 1+1+j)
monkey.Unpatch(foo)
select {
case <-stop:
Expand All @@ -169,7 +169,7 @@ func TestEmpty(t *testing.T) {
}

for i := 0; i < 2000000; i++ {
assert(t, i+1 == foo(i, 1))
assert(t, foo(i, 1) == i+1)
}
close(stop)
wg.Wait()
Expand All @@ -178,19 +178,19 @@ func TestEmpty(t *testing.T) {
func TestG(t *testing.T) {
monkey.Patch(foo, bar)

assert(t, -1 == foo(1, 2))
assert(t, foo(1, 2) == -1)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
assert(t, 3 == foo(1, 2))
assert(t, foo(1, 2) == 3)
}()
go func() {
monkey.Patch(foo, func(a, b int) int {
return a * b
})
defer wg.Done()
assert(t, 2 == foo(1, 2))
assert(t, foo(1, 2) == 2)
}()
wg.Wait()
}
Expand All @@ -204,12 +204,12 @@ func TestGlobal(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
assert(t, 2 == math.Abs(1))
assert(t, math.Abs(1) == 2)
}()
wg.Wait()

g0.Unpatch()
assert(t, 1 == math.Abs(1))
assert(t, math.Abs(1) == 1)
}

func add2[T int | float64](i, j T) T {
Expand All @@ -222,15 +222,15 @@ func (s *S2__monkey__[T]) Foo() T { return s.I * 2 }

func TestGeneric(t *testing.T) {
g1 := monkey.Patch(demo.Add[int], add2[int], monkey.OptGeneric)
assert(t, -1 == demo.Add(1, 2))
assert(t, demo.Add(1, 2) == -1)
g1.Unpatch()
assert(t, 3 == demo.Add(1, 2))
assert(t, demo.Add(1, 2) == 3)

g2 := monkey.Patch((*demo.S2[int]).Foo, (*S2__monkey__[int]).Foo, monkey.OptGeneric)
s := demo.S2[int]{I: 2}
assert(t, 4 == s.Foo())
assert(t, s.Foo() == 4)
g2.Unpatch()
assert(t, 2 == s.Foo())
assert(t, s.Foo() == 2)
}

type TreeNode struct {
Expand Down
7 changes: 1 addition & 6 deletions replace.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package monkey

import (
"reflect"
"syscall"
"unsafe"
)

func rawMemoryAccess(p uintptr, length int) []byte {
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: p,
Len: length,
Cap: length,
}))
return unsafe.Slice((*byte)(unsafe.Pointer(p)), length)
}

func pageStart(ptr uintptr) uintptr {
Expand Down