|
| 1 | +/* |
| 2 | + * Copyright 2026, Datadog, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// Compile-time fault-injection layer for the profiler's memory-access sites. |
| 18 | +// |
| 19 | +// The macros below wrap a pointer or value expression at a real dereference |
| 20 | +// site (VMStructs::at, walkVM, walkFP, walkDwarf). When __FAULT_INJECTION__ is |
| 21 | +// defined, each wrapped expression, with the tier's probability, is replaced by |
| 22 | +// a deliberately bad address (so the load faults and the profiler's recovery |
| 23 | +// path — SafeAccess safefetch or walkVM's setjmp/longjmp — is exercised) or a |
| 24 | +// random int/long value. When the flag is NOT defined, every macro is a strict |
| 25 | +// identity: it expands to exactly the parenthesized original expression, with |
| 26 | +// unchanged type and value category and zero runtime cost. |
| 27 | +// |
| 28 | +// pc = SafeAccess::load(INJECT_FAULT_ADDRESS_LIKELY((void**)fp)); |
| 29 | +// VMMethod* m = ((VMMethod**)INJECT_FAULT_ADDRESS_UNLIKELY(fp))[off]; |
| 30 | +// |
| 31 | +// The three tiers name their firing frequency: RARE 0.01%, UNLIKELY 0.1%, |
| 32 | +// LIKELY 1%. See faultInjection.cpp for the poison-address and PRNG details. |
| 33 | + |
| 34 | +#ifndef _FAULT_INJECTION_H |
| 35 | +#define _FAULT_INJECTION_H |
| 36 | + |
| 37 | +#ifdef __FAULT_INJECTION__ |
| 38 | + |
| 39 | +#include "arch.h" // u64 |
| 40 | +#include <cstdint> |
| 41 | + |
| 42 | +namespace faultinj { |
| 43 | + |
| 44 | +// Firing probability expressed as an xorshift64 threshold (round(p * 2^64)), so |
| 45 | +// the hot-path check is a single integer compare (rng < threshold) with no |
| 46 | +// floating point in the signal handler. |
| 47 | +constexpr u64 PROB_RARE = 1844674407370955ULL; // 1e-4 (0.01%) |
| 48 | +constexpr u64 PROB_UNLIKELY = 18446744073709552ULL; // 1e-3 (0.1%) |
| 49 | +constexpr u64 PROB_LIKELY = 184467440737095520ULL; // 1e-2 (1%) |
| 50 | + |
| 51 | +// Called once at profiler startup (off the signal path) to mmap the PROT_NONE |
| 52 | +// guard region used by poisonAddress(). Safe to call before any injection. |
| 53 | +void init(); |
| 54 | + |
| 55 | +// Draws one per-thread (or global-fallback) PRNG value. Async-signal-safe. |
| 56 | +u64 nextRandom(); |
| 57 | + |
| 58 | +// Returns true with probability threshold/2^64. The function name perturbs the |
| 59 | +// draw so distinct call sites get statistically independent decisions. |
| 60 | +bool shouldFire(u64 threshold, const char* fn); |
| 61 | + |
| 62 | +// A word-aligned address guaranteed to fault on access: half the time a pointer |
| 63 | +// into the mmap'd PROT_NONE guard region (deterministic SIGSEGV), half the time |
| 64 | +// a random non-canonical address (SIGSEGV or SIGBUS). Arithmetic-only, no |
| 65 | +// syscall — safe on the signal-handler hot path. |
| 66 | +uintptr_t poisonAddress(); |
| 67 | + |
| 68 | +// Returns ptr unchanged, or a poison address (cast to T) when the tier fires. |
| 69 | +// Templated so the wrapped expression's static type (void**, const char*, |
| 70 | +// uintptr_t, ...) is preserved exactly. |
| 71 | +template <typename T> |
| 72 | +inline T injectAddress(T ptr, u64 threshold, const char* fn) { |
| 73 | + if (__builtin_expect(shouldFire(threshold, fn), 0)) { |
| 74 | + // C-style cast intentionally: converts the numeric poison address to any |
| 75 | + // pointer type or to uintptr_t. This code only ever compiles under the flag. |
| 76 | + return (T)poisonAddress(); |
| 77 | + } |
| 78 | + return ptr; |
| 79 | +} |
| 80 | + |
| 81 | +// Returns v unchanged, or a pseudo-random value when the tier fires. |
| 82 | +int32_t injectInt(int32_t v, u64 threshold, const char* fn); |
| 83 | +int64_t injectLong(int64_t v, u64 threshold, const char* fn); |
| 84 | + |
| 85 | +} // namespace faultinj |
| 86 | + |
| 87 | +#define INJECT_FAULT_ADDRESS_RARE(ptr) \ |
| 88 | + ::faultinj::injectAddress((ptr), ::faultinj::PROB_RARE, __func__) |
| 89 | +#define INJECT_FAULT_ADDRESS_UNLIKELY(ptr) \ |
| 90 | + ::faultinj::injectAddress((ptr), ::faultinj::PROB_UNLIKELY, __func__) |
| 91 | +#define INJECT_FAULT_ADDRESS_LIKELY(ptr) \ |
| 92 | + ::faultinj::injectAddress((ptr), ::faultinj::PROB_LIKELY, __func__) |
| 93 | + |
| 94 | +#define INJECT_FAULT_INT_RARE(v) \ |
| 95 | + ::faultinj::injectInt((v), ::faultinj::PROB_RARE, __func__) |
| 96 | +#define INJECT_FAULT_INT_UNLIKELY(v) \ |
| 97 | + ::faultinj::injectInt((v), ::faultinj::PROB_UNLIKELY, __func__) |
| 98 | +#define INJECT_FAULT_INT_LIKELY(v) \ |
| 99 | + ::faultinj::injectInt((v), ::faultinj::PROB_LIKELY, __func__) |
| 100 | + |
| 101 | +#define INJECT_FAULT_LONG_RARE(v) \ |
| 102 | + ::faultinj::injectLong((v), ::faultinj::PROB_RARE, __func__) |
| 103 | +#define INJECT_FAULT_LONG_UNLIKELY(v) \ |
| 104 | + ::faultinj::injectLong((v), ::faultinj::PROB_UNLIKELY, __func__) |
| 105 | +#define INJECT_FAULT_LONG_LIKELY(v) \ |
| 106 | + ::faultinj::injectLong((v), ::faultinj::PROB_LIKELY, __func__) |
| 107 | + |
| 108 | +#else // __FAULT_INJECTION__ not defined — strict identity, zero cost. |
| 109 | + |
| 110 | +#define INJECT_FAULT_ADDRESS_RARE(ptr) (ptr) |
| 111 | +#define INJECT_FAULT_ADDRESS_UNLIKELY(ptr) (ptr) |
| 112 | +#define INJECT_FAULT_ADDRESS_LIKELY(ptr) (ptr) |
| 113 | + |
| 114 | +#define INJECT_FAULT_INT_RARE(v) (v) |
| 115 | +#define INJECT_FAULT_INT_UNLIKELY(v) (v) |
| 116 | +#define INJECT_FAULT_INT_LIKELY(v) (v) |
| 117 | + |
| 118 | +#define INJECT_FAULT_LONG_RARE(v) (v) |
| 119 | +#define INJECT_FAULT_LONG_UNLIKELY(v) (v) |
| 120 | +#define INJECT_FAULT_LONG_LIKELY(v) (v) |
| 121 | + |
| 122 | +#endif // __FAULT_INJECTION__ |
| 123 | + |
| 124 | +#endif // _FAULT_INJECTION_H |
0 commit comments