| 1 |
//go:build !no_antithesis_sdk |
| 2 |
|
| 3 |
package internal |
| 4 |
|
| 5 |
import ( |
| 6 |
crand "crypto/rand" |
| 7 |
"encoding/binary" |
| 8 |
"encoding/json" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"runtime" |
| 12 |
) |
| 13 |
|
| 14 |
func Json_data(v any) error { |
| 15 |
if data, err := json.Marshal(v); err != nil { |
| 16 |
return err |
| 17 |
} else { |
| 18 |
handler.output(string(data)) |
| 19 |
return nil |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func Get_random() uint64 { |
| 24 |
return handler.random() |
| 25 |
} |
| 26 |
|
| 27 |
func Notify(edge uint64) bool { |
| 28 |
return handler.notify(edge) |
| 29 |
} |
| 30 |
|
| 31 |
// NotifyV2 reports a coverage hit through the lease ABI (linkage v2); ok is |
| 32 |
// false when the loaded library does not support leases. |
| 33 |
func NotifyV2(edge uint64, hits uint64) (uint64, bool) { |
| 34 |
return handler.notify_v2(edge, hits) |
| 35 |
} |
| 36 |
|
| 37 |
// LeaseGeneration reads the lease revocation word; ok is false when the |
| 38 |
// loaded library does not support leases. |
| 39 |
func LeaseGeneration() (uint64, bool) { |
| 40 |
return handler.lease_generation() |
| 41 |
} |
| 42 |
|
| 43 |
func InitCoverage(num_edges uint64, symbols string) uint64 { |
| 44 |
return handler.init_coverage(num_edges, symbols) |
| 45 |
} |
| 46 |
|
| 47 |
type libHandler interface { |
| 48 |
output(message string) |
| 49 |
random() uint64 |
| 50 |
notify(edge uint64) bool |
| 51 |
notify_v2(edge uint64, hits uint64) (uint64, bool) |
| 52 |
lease_generation() (uint64, bool) |
| 53 |
init_coverage(num_edges uint64, symbols string) uint64 |
| 54 |
} |
| 55 |
|
| 56 |
const ( |
| 57 |
errorLogLinePrefix = "[* antithesis-sdk-go *]" |
| 58 |
) |
| 59 |
|
| 60 |
var handler libHandler |
| 61 |
|
| 62 |
// Mirrors the no_antithesis_sdk implementation of random.GetRandom. |
| 63 |
func osRandom() uint64 { |
| 64 |
var tmp [8]byte |
| 65 |
crand.Read(tmp[:]) |
| 66 |
return binary.LittleEndian.Uint64(tmp[:]) |
| 67 |
} |
| 68 |
|
| 69 |
type localHandler struct { |
| 70 |
outputFile *os.File // can be nil |
| 71 |
} |
| 72 |
|
| 73 |
func (h *localHandler) output(message string) { |
| 74 |
msg_len := len(message) |
| 75 |
if msg_len == 0 { |
| 76 |
return |
| 77 |
} |
| 78 |
if h.outputFile != nil { |
| 79 |
h.outputFile.WriteString(message + "\n") |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func (h *localHandler) random() uint64 { |
| 84 |
return osRandom() |
| 85 |
} |
| 86 |
|
| 87 |
func (h *localHandler) notify(edge uint64) bool { |
| 88 |
return false |
| 89 |
} |
| 90 |
|
| 91 |
func (h *localHandler) notify_v2(edge uint64, hits uint64) (uint64, bool) { |
| 92 |
return 0, false |
| 93 |
} |
| 94 |
|
| 95 |
func (h *localHandler) lease_generation() (uint64, bool) { |
| 96 |
return 0, false |
| 97 |
} |
| 98 |
|
| 99 |
func (h *localHandler) init_coverage(num_edges uint64, symbols string) uint64 { |
| 100 |
return 0 |
| 101 |
} |
| 102 |
|
| 103 |
func init() { |
| 104 |
handler = init_in_antithesis() |
| 105 |
if handler == nil { |
| 106 |
// Otherwise fallback to the local handler. |
| 107 |
handler = openLocalHandler() |
| 108 |
} |
| 109 |
|
| 110 |
// The version message is the first output of every run, emitted when the |
| 111 |
// handler comes up. |
| 112 |
emitVersionMessage() |
| 113 |
|
| 114 |
if sdkRunningInDegradedMode { |
| 115 |
emitDegradedModeProperty() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func emitVersionMessage() { |
| 120 |
languageBlock := map[string]any{ |
| 121 |
"name": "Go", |
| 122 |
"version": runtime.Version(), |
| 123 |
} |
| 124 |
versionBlock := map[string]any{ |
| 125 |
"language": languageBlock, |
| 126 |
"sdk_version": SDK_Version, |
| 127 |
"protocol_version": Protocol_Version, |
| 128 |
} |
| 129 |
if data, err := json.Marshal(map[string]any{"antithesis_sdk": versionBlock}); err == nil { |
| 130 |
handler.output(string(data)) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// If `localOutputEnvVar` is set to a non-empty path, attempt to open that path for appending |
| 135 |
// to serve as the log file of the local handler. |
| 136 |
// Otherwise, we don't have a log file, and logging is a no-op in the local handler. |
| 137 |
func openLocalHandler() *localHandler { |
| 138 |
path, is_set := os.LookupEnv(localOutputEnvVar) |
| 139 |
if !is_set || len(path) == 0 { |
| 140 |
return &localHandler{nil} |
| 141 |
} |
| 142 |
|
| 143 |
// Open the file for writing (create if needed and possible, use O_APPEND to support concurrent writers) |
| 144 |
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) |
| 145 |
if err != nil { |
| 146 |
log.Printf("%s Failed to open path %s: %v", errorLogLinePrefix, path, err) |
| 147 |
file = nil |
| 148 |
} |
| 149 |
|
| 150 |
return &localHandler{file} |
| 151 |
} |
| 152 |
|
| 153 |
const degradedModeProperty = "Go application under test compiled with CGO enabled" |
| 154 |
|
| 155 |
func emitDegradedModeProperty() { |
| 156 |
Json_data(map[string]any{ |
| 157 |
"antithesis_assert": map[string]any{ |
| 158 |
"hit": true, |
| 159 |
"must_hit": true, |
| 160 |
"assert_type": "always", |
| 161 |
"display_type": "Always", |
| 162 |
"message": degradedModeProperty, |
| 163 |
"id": degradedModeProperty, |
| 164 |
"condition": false, |
| 165 |
"location": map[string]any{ |
| 166 |
"class": "", |
| 167 |
"function": "", |
| 168 |
"file": "", |
| 169 |
"begin_line": 0, |
| 170 |
"begin_column": 0, |
| 171 |
}, |
| 172 |
"details": map[string]any{ |
| 173 |
"info": "This binary was built without CGO enabled, which is required for " + |
| 174 |
"coverage-guided fuzzing and thread pausing. Rebuild with CGO_ENABLED=1 so the " + |
| 175 |
"SDK can link " + defaultNativeLibraryPath + " to enable them.", |
| 176 |
}, |
| 177 |
}, |
| 178 |
}) |
| 179 |
} |