• Stars
    star
    250
  • Rank 162,397 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 4 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Patch all Go functions for testing

SuperMonkey

gopher

This lib is inspired by https://github.com/bouk/monkey, and uses some of the code

Introduction

Patch all functions without limits, including which are unexported

Warning : please add -l to your gcflags or add //go:noinline to func which you want to patch.

when running in tests

you should run this lib under a go mod project and provide the full project path

Warning : use go test -ldflags="-s=false" -gcflags="-l" to enable symbol table and disable inline.

2023/04/23 14:01:05 reading /root/tmp/go-build541175936/b001/exe/a: no symbol section
2023/04/23 14:01:05 reading /root/tmp/go-build541175936/b001/exe/a: no symbols

if you has this problem, just set -ldflags="-s=false"

when running not in tests

patch private function

normal

package main

import (
	"fmt"

	sm "github.com/cch123/supermonkey"
)

func main() {
	fmt.Println("original function output:")
	heyHey()

	patchGuard := sm.Patch(heyHey, func() {
		fmt.Println("please be polite")
	})
	fmt.Println("after patch, function output:")
	heyHey()

	patchGuard.Unpatch()
	fmt.Println("unpatch, then output:")
	heyHey()
}

//go:noinline
func heyHey() {
	fmt.Println("fake")
}

go run -gcflags="-l" yourfile.go

full symbol name

package main

import (
	"fmt"

	sm "github.com/cch123/supermonkey"
)

func main() {
	fmt.Println("original function output:")
	heyHeyHey()

	patchGuard := sm.PatchByFullSymbolName("main.heyHeyHey", func() {
		fmt.Println("please be polite")
	})
	fmt.Println("after patch, function output:")
	heyHeyHey()

	patchGuard.Unpatch()
	fmt.Println("unpatch, then output:")
	heyHeyHey()
}

//go:noinline
func heyHeyHey() {
	fmt.Println("fake")
}

go run -gcflags="-l" yourfile.go

patch private instance method

normal

package main

import (
	"fmt"

	sm "github.com/cch123/supermonkey"
)

type person struct{ name string }

//go:noinline
func (p *person) speak() {
	fmt.Println("my name is ", p.name)
}

func main() {
	var p = person{"Lance"}
	fmt.Println("original function output:")
	p.speak()

	patchGuard := sm.Patch((*person).speak, func(*person) {
        fmt.Println("we are all the same")
    })
    fmt.Println("after patch, function output:")
    p.speak()

	patchGuard.Unpatch()
	fmt.Println("unpatch, then output:")
	p.speak()
}

go run -gcflags="-l" yourfile.go

full symbol name

package main

import (
	"fmt"
	"unsafe"

	sm "github.com/cch123/supermonkey"
)

type person struct{ name string }

//go:noinline
func (p *person) speak() {
	fmt.Println("my name is ", p.name)
}

func main() {
	var p = person{"Linda"}
	fmt.Println("original function output:")
	p.speak()

	patchGuard := sm.PatchByFullSymbolName("main.(*person).speak", func(ptr uintptr) {
		p = (*person)(unsafe.Pointer(ptr))
		fmt.Println(p.name, ", we are all the same")
	})
	fmt.Println("after patch, function output:")
	p.speak()

	patchGuard.Unpatch()
	fmt.Println("unpatch, then output:")
	p.speak()
}
package main

import (
	"context"
	"fmt"

	sm "github.com/cch123/supermonkey"
)

type Bar struct {
	Name string
}

type Foo struct{}

func (*Foo) MyFunc(ctx context.Context) (*Bar, error) {
	return &Bar{"Bar"}, nil
}

func main() {
	f := &Foo{}
	fmt.Println("original function output:")
	fmt.Println(f.MyFunc(nil))

	//patchGuard := sm.PatchByFullSymbolName("main.(*Foo).MyFunc", func(_ *Foo, ctx context.Context) (*Bar, error) {
	patchGuard := sm.PatchByFullSymbolName("main.(*Foo).MyFunc", func(_ uintptr, ctx context.Context) (*Bar, error) {
		return &Bar{"Not bar"}, nil
	})

	fmt.Println("after patch, function output:")
	fmt.Println(f.MyFunc(nil))

	patchGuard.Unpatch()
	fmt.Println("unpatch, then output:")
	fmt.Println(f.MyFunc(nil))
}

go run -gcflags="-l" yourfile.go

Authors

cch123

Mutated1994

More Repositories

1

golang-notes

Go source code analysis(zh-cn)
HTML
3,761
star
2

elasticsql

convert sql to elasticsearch DSL in golang(go)
Go
1,091
star
3

asmshare

some ideas about asm && plan9 asm video: https://www.bilibili.com/video/BV1Xb411J7Yk
349
star
4

asm-cli-rust

interative assembly shell written in rust
Rust
255
star
5

asm-cli

Interactive shell of assembly language(X86/X64) based on unicorn and keystone
Go
250
star
6

gogctuner

auto adjust your GOGC value
Go
173
star
7

leetcode-go

leetcode in golang
Go
81
star
8

awesome-gc

A curated list of awesome garbage collection resources
67
star
9

llp-trans

Low Level Progarmming Chinese translation(WIP)
65
star
10

goroutineid

extract goroutine id to the out world although it's dangerous and useless.
Go
56
star
11

leetcode-rust

leetcode in rust
Rust
51
star
12

gomod-conflict-detect

detect dependencies which may conflict
Go
49
star
13

elastic-rs

comvert bool expression to elasticsearch DSL in rust
Rust
43
star
14

eco2020

for English learning, 看不到图片(github usercontent 部分被 block)请梯子,有需要可以 fork,哪天被 DMCA 了也不奇怪
35
star
15

go-ddd

Go
22
star
16

how-ai-will-change-our-work

18
star
17

iapp

《程序员的算法趣题》Go 解法(WIP)
Go
18
star
18

whisper-tts

Go
17
star
19

parser_example

various parser demos
Rust
15
star
20

test

learn and test examples
HTML
15
star
21

perf_workshop_2021

Go
13
star
22

bottlenecks-go

Cases for high concurrency go programs
12
star
23

asciidoc-rs

asciidoc parser in pest(WIP)
Rust
10
star
24

knowledge-index

knowledge index for my career
10
star
25

cch123.github.io

personal share
HTML
8
star
26

blog_comment

comments of xargin.com
8
star
27

rust-book-code-collect

collect code of various rust books
Rust
8
star
28

cch123

6
star
29

collins-wordlist

collins dict word list
5
star
30

threadkill

Go
2
star
31

stackdiff

2
star
32

nginx-annotated

C
2
star
33

cmake-learn

CMake
2
star
34

structs

Some useful structs implemented by go including skiplist...
Go
1
star
35

monkey

hand write parser test
Go
1
star
36

gomodexp

go mod experiments
Go
1
star
37

mosn_easy_configure

Vue
1
star
38

woboq-lua-5.0

lua code online preview
HTML
1
star
39

wrkrs

modern wrk
Rust
1
star
40

schema-parser

convert between pb, thrift, avro or yaml schema
1
star
41

voice-clone-demo

Python
1
star
42

crawl_log

Go
1
star
43

woboq-script

CMake
1
star
44

hazama

personal web framework
1
star
45

algos-svg

record the process of writing algorithms
1
star
46

paper-translation-project

1
star
47

pdf-url-remover

remove link/url in pdf files
Go
1
star
48

woboq-nginx-1.13

nginx 1.13 code online preview
HTML
1
star
49

woboq-redis-3.0

redis 3.0 code online preview
HTML
1
star
50

pest_gen

generate basic layout of pest parsers(WIP)
Rust
1
star