|
| 1 | +package mpatch // import "github.com/undefinedlabs/go-mpatch" |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "sync" |
| 8 | + "syscall" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +type ( |
| 13 | + Patch struct { |
| 14 | + targetBytes []byte |
| 15 | + target *reflect.Value |
| 16 | + redirection *reflect.Value |
| 17 | + } |
| 18 | + pointer struct { |
| 19 | + length uintptr |
| 20 | + ptr uintptr |
| 21 | + } |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + patchLock = sync.Mutex{} |
| 26 | + patches = make(map[uintptr]*Patch) |
| 27 | + pageSize = syscall.Getpagesize() |
| 28 | +) |
| 29 | + |
| 30 | +func PatchMethod(target, redirection interface{}) (*Patch, error) { |
| 31 | + tValue := getValueFrom(target) |
| 32 | + rValue := getValueFrom(redirection) |
| 33 | + err := isPatchable(&tValue, &rValue) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + patch := &Patch{target: &tValue, redirection: &rValue} |
| 38 | + err = applyPatch(patch) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + return patch, nil |
| 43 | +} |
| 44 | +func PatchInstanceMethodByName(target reflect.Type, methodName string, redirection interface{}) (*Patch, error) { |
| 45 | + if target.Kind() == reflect.Struct { |
| 46 | + target = reflect.PtrTo(target) |
| 47 | + } |
| 48 | + method, ok := target.MethodByName(methodName) |
| 49 | + if !ok { |
| 50 | + return nil, errors.New(fmt.Sprintf("Method '%v' not found", methodName)) |
| 51 | + } |
| 52 | + return PatchMethodByReflect(method, redirection) |
| 53 | +} |
| 54 | +func PatchMethodByReflect(target reflect.Method, redirection interface{}) (*Patch, error) { |
| 55 | + tValue := &target.Func |
| 56 | + rValue := getValueFrom(redirection) |
| 57 | + err := isPatchable(tValue, &rValue) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + patch := &Patch{target: tValue, redirection: &rValue} |
| 62 | + err = applyPatch(patch) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + return patch, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (p *Patch) Patch() error { |
| 70 | + if p == nil { |
| 71 | + return errors.New("patch is nil") |
| 72 | + } |
| 73 | + err := isPatchable(p.target, p.redirection) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + err = applyPatch(p) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | +func (p *Patch) Unpatch() error { |
| 84 | + if p == nil { |
| 85 | + return errors.New("patch is nil") |
| 86 | + } |
| 87 | + return applyUnpatch(p) |
| 88 | +} |
| 89 | + |
| 90 | +func isPatchable(target, redirection *reflect.Value) error { |
| 91 | + if target.Kind() != reflect.Func || redirection.Kind() != reflect.Func { |
| 92 | + return errors.New("the target and/or redirection is not a Func") |
| 93 | + } |
| 94 | + if target.Type() != redirection.Type() { |
| 95 | + return errors.New(fmt.Sprintf("the target and/or redirection doesn't have the same type: %s != %s", target.Type(), redirection.Type())) |
| 96 | + } |
| 97 | + if _, ok := patches[target.Pointer()]; ok { |
| 98 | + return errors.New("the target is already patched") |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func applyPatch(patch *Patch) error { |
| 104 | + patchLock.Lock() |
| 105 | + defer patchLock.Unlock() |
| 106 | + tPointer := patch.target.Pointer() |
| 107 | + rPointer := getInternalPtrFromValue(*patch.redirection) |
| 108 | + rPointerJumpBytes := getJumpFuncBytes(rPointer) |
| 109 | + tPointerBytes := getMemorySliceFromPointer(tPointer, len(rPointerJumpBytes)) |
| 110 | + targetBytes := make([]byte, len(tPointerBytes)) |
| 111 | + copy(targetBytes, tPointerBytes) |
| 112 | + err := copyDataToPtr(tPointer, rPointerJumpBytes) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + patch.targetBytes = targetBytes |
| 117 | + patches[tPointer] = patch |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func applyUnpatch(patch *Patch) error { |
| 122 | + patchLock.Lock() |
| 123 | + defer patchLock.Unlock() |
| 124 | + if patch.targetBytes == nil || len(patch.targetBytes) == 0 { |
| 125 | + return errors.New("the target is not patched") |
| 126 | + } |
| 127 | + tPointer := patch.target.Pointer() |
| 128 | + if _, ok := patches[tPointer]; !ok { |
| 129 | + return errors.New("the target is not patched") |
| 130 | + } |
| 131 | + delete(patches, tPointer) |
| 132 | + err := copyDataToPtr(tPointer, patch.targetBytes) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func getInternalPtrFromValue(value reflect.Value) uintptr { |
| 140 | + return (*pointer)(unsafe.Pointer(&value)).ptr |
| 141 | +} |
| 142 | + |
| 143 | +func getValueFrom(data interface{}) reflect.Value { |
| 144 | + if cValue, ok := data.(reflect.Value); ok { |
| 145 | + return cValue |
| 146 | + } else { |
| 147 | + return reflect.ValueOf(data) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func getMemorySliceFromPointer(p uintptr, length int) []byte { |
| 152 | + return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ |
| 153 | + Data: p, |
| 154 | + Len: length, |
| 155 | + Cap: length, |
| 156 | + })) |
| 157 | +} |
| 158 | + |
| 159 | +func getPageStartPtr(ptr uintptr) uintptr { |
| 160 | + return ptr & ^(uintptr(pageSize - 1)) |
| 161 | +} |
0 commit comments