| 1 |
//go:build !no_antithesis_sdk |
| 2 |
|
| 3 |
package instrumentation |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"sync/atomic" |
| 8 |
|
| 9 |
"github.com/antithesishq/antithesis-sdk-go/internal" |
| 10 |
) |
| 11 |
|
| 12 |
// Lease-word packing, from instrumentation.h ("Coverage leases"): these are |
| 13 |
// part of the frozen v2 linkage ABI, so a hardcoded mirror is safe. |
| 14 |
const ( |
| 15 |
leaseEpochShift = 40 |
| 16 |
leaseGrantedShift = 20 |
| 17 |
leaseFieldMask = 0xFFFFF |
| 18 |
leaseEpochMask = 0xFFFFFF |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
moduleInitialized = false |
| 23 |
moduleOffset uint64 |
| 24 |
// Per-edge cached lease words (coverage leases, linkage ABI v2). A |
| 25 |
// zeroed word is always safe: `remaining` is 0, so the next hit calls |
| 26 |
// in. Nil when the loaded library predates leases; then every hit calls |
| 27 |
// in — the v1 return value's "never call again" is not revocable, so |
| 28 |
// caching it would go deaf to coverage-seen resets. |
| 29 |
leases []uint64 |
| 30 |
) |
| 31 |
|
| 32 |
const instrumentor_tag = "github.com/antithesishq/antithesis-sdk-go/instrumentation" |
| 33 |
|
| 34 |
// InitializeModule should be called only once from a program. |
| 35 |
func InitializeModule(symbolTable string, edgeCount int) uint64 { |
| 36 |
if moduleInitialized { |
| 37 |
// We cannot support incorrect workflows. |
| 38 |
panic("InitializeModule() has already been called!") |
| 39 |
} |
| 40 |
|
| 41 |
// WARN Re: integer type conversion, see https://github.com/golang/go/issues/29878 |
| 42 |
offset := internal.InitCoverage(uint64(edgeCount), symbolTable) |
| 43 |
moduleOffset = uint64(offset) |
| 44 |
leases = newLeaseCache(edgeCount) |
| 45 |
moduleInitialized = true |
| 46 |
return moduleOffset |
| 47 |
} |
| 48 |
|
| 49 |
// Notify will be called from instrumented code. Repeat hits are counted locally |
| 50 |
// under a coverage lease and reported when the lease runs out or is revoked; see |
| 51 |
// notifyEdge, which this shares with the per-module handles in module.go. |
| 52 |
func Notify(edge int) { |
| 53 |
if !moduleInitialized { |
| 54 |
// We cannot support incorrect workflows. |
| 55 |
panic(fmt.Sprintf("%s.Notify() called before InitializeModule()", instrumentor_tag)) |
| 56 |
} |
| 57 |
notifyEdge(edge, moduleOffset, leases) |
| 58 |
} |
| 59 |
|
| 60 |
// newLeaseCache allocates per-edge lease storage for one coverage module, or |
| 61 |
// returns nil when the loaded library predates leases — then every hit calls |
| 62 |
// in, because the v1 return value's "never call again" is not revocable and |
| 63 |
// caching it would go deaf to coverage-seen resets. |
| 64 |
func newLeaseCache(edgeCount int) []uint64 { |
| 65 |
if _, ok := internal.LeaseGeneration(); !ok { |
| 66 |
return nil |
| 67 |
} |
| 68 |
// Some instrumentors use 1-based edge ids; index edgeCount is |
| 69 |
// reachable (the library sizes its guard table the same way). |
| 70 |
return make([]uint64, edgeCount+1) |
| 71 |
} |
| 72 |
|
| 73 |
// hasCachedLease reports whether edge has a lease word to count hits against. |
| 74 |
// Anything else — a library predating leases, or an edge id outside the range |
| 75 |
// the module registered — has to call in on every hit. |
| 76 |
func hasCachedLease(edge int, leases []uint64) bool { |
| 77 |
return leases != nil && edge >= 0 && edge < len(leases) |
| 78 |
} |
| 79 |
|
| 80 |
// notifyEdge reports one hit of edge in a module based at offset, whose cached |
| 81 |
// lease words are leases (nil if the library predates leases). |
| 82 |
// |
| 83 |
// Repeat hits are counted locally under a coverage lease (instrumentation.h, |
| 84 |
// "Coverage leases") and reported when the lease runs out or is revoked — a |
| 85 |
// revocation (e.g. a coverage-seen reset) reaches this cache through the epoch |
| 86 |
// check, unlike the retired v1 never-call-again cache, which was deaf to it. |
| 87 |
func notifyEdge(edge int, offset uint64, leases []uint64) { |
| 88 |
edgePlusOffset := uint64(edge) + offset |
| 89 |
if !hasCachedLease(edge, leases) { |
| 90 |
// v1 library (or an out-of-range edge): every hit calls in. |
| 91 |
internal.Notify(edgePlusOffset) |
| 92 |
return |
| 93 |
} |
| 94 |
lease, counted := leaseLocalHit(&leases[edge], internal.LeaseGeneration) |
| 95 |
if counted { |
| 96 |
return |
| 97 |
} |
| 98 |
// The store may clobber a concurrent local decrement of the fresh lease; |
| 99 |
// that only under-reports folded hits, which the ABI does tolerate (it |
| 100 |
// perturbs pause-interval statistics). |
| 101 |
if fresh, ok := internal.NotifyV2(edgePlusOffset, foldedHits(lease)); ok { |
| 102 |
atomic.StoreUint64(&leases[edge], fresh) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// granted and remaining come from one atomic snapshot of the word, so |
| 107 |
// granted >= remaining (the library's grant contract) and the |
| 108 |
// subtraction cannot wrap. |
| 109 |
func foldedHits(lease uint64) uint64 { |
| 110 |
granted := (lease >> leaseGrantedShift) & leaseFieldMask |
| 111 |
remaining := lease & leaseFieldMask |
| 112 |
return granted - remaining |
| 113 |
} |
| 114 |
|
| 115 |
// leaseLocalHit tries to count one hit locally against the cached lease |
| 116 |
// word: it decrements the word only if, at the instant the decrement |
| 117 |
// commits, the lease is still live — remaining nonzero and epoch current. |
| 118 |
// The whole-word decrement is safe only under that CAS revalidation: |
| 119 |
// remaining and granted are adjacent bitfields, so a decrement guarded by |
| 120 |
// a stale snapshot can borrow across the field boundary, corrupt the word, |
| 121 |
// and make a later call-in's granted-remaining wrap (which the library |
| 122 |
// treats as fatal). Per instrumentation.h the caller decrements the |
| 123 |
// *field*, not the word; this mirrors the library's own guard fast path |
| 124 |
// (instrumentation/coverage.cpp), where compare_exchange re-tests both |
| 125 |
// conditions against the current word on every iteration. |
| 126 |
// |
| 127 |
// Returns the last word observed and whether the hit was counted locally; |
| 128 |
// when false, the caller must call in, folding that word's |
| 129 |
// granted-remaining locally-counted hits. |
| 130 |
func leaseLocalHit(lease *uint64, currentEpoch func() (uint64, bool)) (uint64, bool) { |
| 131 |
word := atomic.LoadUint64(lease) |
| 132 |
for { |
| 133 |
if word&leaseFieldMask == 0 { |
| 134 |
return word, false |
| 135 |
} |
| 136 |
// Re-read the revocation epoch every iteration; the header requires |
| 137 |
// the read to be non-hoistable out of hot loops. |
| 138 |
generation, ok := currentEpoch() |
| 139 |
if !ok || (word>>leaseEpochShift)&leaseEpochMask != generation&leaseEpochMask { |
| 140 |
return word, false |
| 141 |
} |
| 142 |
if atomic.CompareAndSwapUint64(lease, word, word-1) { |
| 143 |
return word - 1, true |
| 144 |
} |
| 145 |
word = atomic.LoadUint64(lease) |
| 146 |
} |
| 147 |
} |