| 1 |
//go:build !no_antithesis_sdk && linux && amd64 && cgo |
| 2 |
|
| 3 |
package internal |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"sync/atomic" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
// -------------------------------------------------------------------------------- |
| 13 |
// To build and run an executable with this package |
| 14 |
// |
| 15 |
// CC=clang CGO_ENABLED=1 go run ./main.go |
| 16 |
// -------------------------------------------------------------------------------- |
| 17 |
|
| 18 |
// \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ |
| 19 |
// |
| 20 |
// The commented lines below, and the `import "C"` line which must directly follow |
| 21 |
// the commented lines are used by CGO. They are load-bearing, and should not be |
| 22 |
// changed without first understanding how CGO uses them. |
| 23 |
// |
| 24 |
// \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ |
| 25 |
|
| 26 |
// #cgo LDFLAGS: -ldl |
| 27 |
// |
| 28 |
// #include <dlfcn.h> |
| 29 |
// #include <stdbool.h> |
| 30 |
// #include <stdint.h> |
| 31 |
// #include <stdlib.h> |
| 32 |
// |
| 33 |
// typedef void (*go_fuzz_json_data_fn)(const char *data, size_t size); |
| 34 |
// void |
| 35 |
// go_fuzz_json_data(void *f, const char *data, size_t size) { |
| 36 |
// ((go_fuzz_json_data_fn)f)(data, size); |
| 37 |
// } |
| 38 |
// |
| 39 |
// typedef void (*go_fuzz_flush_fn)(void); |
| 40 |
// void |
| 41 |
// go_fuzz_flush(void *f) { |
| 42 |
// ((go_fuzz_flush_fn)f)(); |
| 43 |
// } |
| 44 |
// |
| 45 |
// typedef uint64_t (*go_fuzz_get_random_fn)(void); |
| 46 |
// uint64_t |
| 47 |
// go_fuzz_get_random(void *f) { |
| 48 |
// return ((go_fuzz_get_random_fn)f)(); |
| 49 |
// } |
| 50 |
// |
| 51 |
// typedef bool (*go_notify_coverage_fn)(size_t); |
| 52 |
// int |
| 53 |
// go_notify_coverage(void *f, size_t edges) { |
| 54 |
// bool b = ((go_notify_coverage_fn)f)(edges); |
| 55 |
// return b ? 1 : 0; |
| 56 |
// } |
| 57 |
// |
| 58 |
// typedef uint64_t (*go_init_coverage_fn)(size_t num_edges, const char *symbols); |
| 59 |
// uint64_t |
| 60 |
// go_init_coverage(void *f, size_t num_edges, const char *symbols) { |
| 61 |
// return ((go_init_coverage_fn)f)(num_edges, symbols); |
| 62 |
// } |
| 63 |
// |
| 64 |
// typedef uint64_t (*go_notify_coverage_v2_fn)(size_t, uint64_t); |
| 65 |
// uint64_t |
| 66 |
// go_notify_coverage_v2(void *f, size_t edge, uint64_t hits) { |
| 67 |
// return ((go_notify_coverage_v2_fn)f)(edge, hits); |
| 68 |
// } |
| 69 |
// |
| 70 |
// typedef const uint64_t *(*go_lease_generation_addr_fn)(void); |
| 71 |
// const uint64_t * |
| 72 |
// go_lease_generation_addr(void *f) { |
| 73 |
// return ((go_lease_generation_addr_fn)f)(); |
| 74 |
// } |
| 75 |
// |
| 76 |
// typedef uint64_t (*go_request_abi_version_fn)(uint64_t); |
| 77 |
// uint64_t |
| 78 |
// go_request_abi_version(void *f, uint64_t v) { |
| 79 |
// return ((go_request_abi_version_fn)f)(v); |
| 80 |
// } |
| 81 |
// |
| 82 |
import "C" |
| 83 |
|
| 84 |
type voidstarHandler struct { |
| 85 |
fuzzJsonData unsafe.Pointer |
| 86 |
fuzzFlush unsafe.Pointer |
| 87 |
fuzzGetRandom unsafe.Pointer |
| 88 |
initCoverage unsafe.Pointer |
| 89 |
notifyCoverage unsafe.Pointer |
| 90 |
// Coverage-lease support (linkage ABI v2, instrumentation.h). Nil/zero |
| 91 |
// when the loaded library predates the mechanism or negotiation failed. |
| 92 |
notifyCoverageV2 unsafe.Pointer |
| 93 |
leaseGeneration *uint64 |
| 94 |
} |
| 95 |
|
| 96 |
func (h *voidstarHandler) output(message string) { |
| 97 |
msg_len := len(message) |
| 98 |
if msg_len == 0 { |
| 99 |
return |
| 100 |
} |
| 101 |
cstrMessage := C.CString(message) |
| 102 |
defer C.free(unsafe.Pointer(cstrMessage)) |
| 103 |
C.go_fuzz_json_data(h.fuzzJsonData, cstrMessage, C.ulong(msg_len)) |
| 104 |
C.go_fuzz_flush(h.fuzzFlush) |
| 105 |
} |
| 106 |
|
| 107 |
func (h *voidstarHandler) random() uint64 { |
| 108 |
return uint64(C.go_fuzz_get_random(h.fuzzGetRandom)) |
| 109 |
} |
| 110 |
|
| 111 |
func (h *voidstarHandler) init_coverage(num_edge uint64, symbols string) uint64 { |
| 112 |
cstrSymbols := C.CString(symbols) |
| 113 |
defer C.free(unsafe.Pointer(cstrSymbols)) |
| 114 |
return uint64(C.go_init_coverage(h.initCoverage, C.ulong(num_edge), cstrSymbols)) |
| 115 |
} |
| 116 |
|
| 117 |
func (h *voidstarHandler) notify(edge uint64) bool { |
| 118 |
ival := int(C.go_notify_coverage(h.notifyCoverage, C.ulong(edge))) |
| 119 |
return ival == 1 |
| 120 |
} |
| 121 |
|
| 122 |
func (h *voidstarHandler) notify_v2(edge uint64, hits uint64) (uint64, bool) { |
| 123 |
if h.notifyCoverageV2 == nil { |
| 124 |
return 0, false |
| 125 |
} |
| 126 |
return uint64(C.go_notify_coverage_v2(h.notifyCoverageV2, C.ulong(edge), C.uint64_t(hits))), true |
| 127 |
} |
| 128 |
|
| 129 |
func (h *voidstarHandler) lease_generation() (uint64, bool) { |
| 130 |
if h.leaseGeneration == nil { |
| 131 |
return 0, false |
| 132 |
} |
| 133 |
// The header requires this read to be non-hoistable out of hot loops; |
| 134 |
// an atomic load has the required opaque semantics. The word lives for |
| 135 |
// the life of the process and is only ever written by the fuzzer. |
| 136 |
return atomic.LoadUint64(h.leaseGeneration), true |
| 137 |
} |
| 138 |
|
| 139 |
// Attempt to load libvoidstar and some symbols from `path` |
| 140 |
func openSharedLib(path string) (*voidstarHandler, error) { |
| 141 |
cstrPath := C.CString(path) |
| 142 |
defer C.free(unsafe.Pointer(cstrPath)) |
| 143 |
|
| 144 |
dlError := func(message string) error { |
| 145 |
return fmt.Errorf("%s: (%s)", message, C.GoString(C.dlerror())) |
| 146 |
} |
| 147 |
|
| 148 |
sharedLib := C.dlopen(cstrPath, C.int(C.RTLD_NOW)) |
| 149 |
if sharedLib == nil { |
| 150 |
return nil, dlError("Can not load the Antithesis native library") |
| 151 |
} |
| 152 |
|
| 153 |
loadFunc := func(name string) (symbol unsafe.Pointer, err error) { |
| 154 |
cstrName := C.CString(name) |
| 155 |
defer C.free(unsafe.Pointer(cstrName)) |
| 156 |
if symbol = C.dlsym(sharedLib, cstrName); symbol == nil { |
| 157 |
err = dlError(fmt.Sprintf("Can not access symbol %s", name)) |
| 158 |
} |
| 159 |
return |
| 160 |
} |
| 161 |
|
| 162 |
// Symbols absent from older library vintages must resolve softly: no |
| 163 |
// library version may fail the SDK's load (instrumentation.h, "Linkage |
| 164 |
// ABI versioning"). |
| 165 |
loadOptional := func(name string) unsafe.Pointer { |
| 166 |
cstrName := C.CString(name) |
| 167 |
defer C.free(unsafe.Pointer(cstrName)) |
| 168 |
symbol := C.dlsym(sharedLib, cstrName) |
| 169 |
C.dlerror() // clear any lookup error |
| 170 |
return symbol |
| 171 |
} |
| 172 |
|
| 173 |
fuzzJsonData, err := loadFunc("fuzz_json_data") |
| 174 |
if err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
fuzzFlush, err := loadFunc("fuzz_flush") |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
fuzzGetRandom, err := loadFunc("fuzz_get_random") |
| 182 |
if err != nil { |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
notifyCoverage, err := loadFunc("notify_coverage") |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
initCoverage, err := loadFunc("init_coverage_module") |
| 190 |
if err != nil { |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
handler := &voidstarHandler{ |
| 194 |
fuzzJsonData: fuzzJsonData, |
| 195 |
fuzzFlush: fuzzFlush, |
| 196 |
fuzzGetRandom: fuzzGetRandom, |
| 197 |
initCoverage: initCoverage, |
| 198 |
notifyCoverage: notifyCoverage, |
| 199 |
} |
| 200 |
|
| 201 |
// Negotiate for coverage leases: request ABI v2 and gate on the granted |
| 202 |
// version (a grant above the request means "cannot negotiate"). |
| 203 |
requestAbi := loadOptional("instrumentation_request_abi_version") |
| 204 |
notifyV2 := loadOptional("notify_coverage_v2") |
| 205 |
leaseAddr := loadOptional("coverage_lease_generation_addr") |
| 206 |
if requestAbi != nil && notifyV2 != nil && leaseAddr != nil { |
| 207 |
if granted := uint64(C.go_request_abi_version(requestAbi, 2)); granted == 2 { |
| 208 |
if addr := C.go_lease_generation_addr(leaseAddr); addr != nil { |
| 209 |
handler.notifyCoverageV2 = notifyV2 |
| 210 |
handler.leaseGeneration = (*uint64)(unsafe.Pointer(addr)) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
return handler, nil |
| 215 |
} |
| 216 |
|
| 217 |
const sdkRunningInDegradedMode = false |
| 218 |
|
| 219 |
// If we have a file at `defaultNativeLibraryPath`, we load the shared library |
| 220 |
// (and panic on any error encountered during load). |
| 221 |
func init_in_antithesis() libHandler { |
| 222 |
if _, err := os.Stat(defaultNativeLibraryPath); err == nil { |
| 223 |
handler, err := openSharedLib(defaultNativeLibraryPath) |
| 224 |
if err != nil { |
| 225 |
panic(err) |
| 226 |
} |
| 227 |
return handler |
| 228 |
} |
| 229 |
return nil |
| 230 |
} |