src/anchor_evidence.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file anchor_evidence.cpp | ||
| 3 | * @brief This TU owns anchor evidence identity: the drift fingerprints and the quorum independence atoms. | ||
| 4 | * | ||
| 5 | * The resolution engine stays in anchor.cpp and reaches the independence gate through internal/anchor_evidence.hpp. | ||
| 6 | * Fingerprint results stay stable across runs and builds. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "DetourModKit/anchor.hpp" | ||
| 10 | |||
| 11 | #include "internal/anchor_evidence.hpp" | ||
| 12 | |||
| 13 | #include <cstddef> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <span> | ||
| 16 | #include <string_view> | ||
| 17 | #include <type_traits> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace DetourModKit | ||
| 21 | { | ||
| 22 | namespace anchor | ||
| 23 | { | ||
| 24 | namespace | ||
| 25 | { | ||
| 26 | // FNV-1a 64 hashes evidence for anchor_fingerprint. Integers use least-significant-byte order. Every | ||
| 27 | // variable-length field has a length prefix. | ||
| 28 | inline constexpr std::uint64_t FNV1A64_OFFSET = 14695981039346656037ULL; | ||
| 29 | inline constexpr std::uint64_t FNV1A64_PRIME = 1099511628211ULL; | ||
| 30 | |||
| 31 | 20028 | [[nodiscard]] std::uint64_t fnv1a_byte(std::uint64_t hash, std::uint8_t value) noexcept | |
| 32 | { | ||
| 33 | 20028 | return (hash ^ value) * FNV1A64_PRIME; | |
| 34 | } | ||
| 35 | |||
| 36 | // Widen to u64 before each shift so a 1-byte type never hits a shift-width edge case. | ||
| 37 | 1560 | template <typename T> [[nodiscard]] std::uint64_t fnv1a_int(std::uint64_t hash, T value) noexcept | |
| 38 | { | ||
| 39 | 1560 | auto bits = static_cast<std::uint64_t>(static_cast<std::make_unsigned_t<T>>(value)); | |
| 40 | 14040 | for (std::size_t i = 0; i < sizeof(T); ++i) | |
| 41 | { | ||
| 42 | 12480 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(bits & 0xFFu)); | |
| 43 | 12480 | bits >>= 8; | |
| 44 | } | ||
| 45 | 1560 | return hash; | |
| 46 | } | ||
| 47 | |||
| 48 | // Prefixes each string field with its length so adjacent fields never alias. | ||
| 49 | 251 | [[nodiscard]] std::uint64_t fnv1a_field(std::uint64_t hash, std::string_view field) noexcept | |
| 50 | { | ||
| 51 | 251 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(field.size())); | |
| 52 | 2587 | for (const char c : field) | |
| 53 | { | ||
| 54 | 2336 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(c)); | |
| 55 | } | ||
| 56 | 251 | return hash; | |
| 57 | } | ||
| 58 | |||
| 59 | // Uses ASCII case-insensitive module basenames. Case variants of one DLL must not cast independent export | ||
| 60 | // votes. | ||
| 61 | 12 | [[nodiscard]] std::uint64_t fnv1a_module_field(std::uint64_t hash, std::string_view module_name) noexcept | |
| 62 | { | ||
| 63 | 12 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(module_name.size())); | |
| 64 | 198 | for (const char c : module_name) | |
| 65 | { | ||
| 66 | 186 | const auto byte = static_cast<std::uint8_t>(c); | |
| 67 | 186 | const std::uint8_t folded = | |
| 68 | 176 | (byte >= static_cast<std::uint8_t>('A') && byte <= static_cast<std::uint8_t>('Z')) | |
| 69 | 186 | ? static_cast<std::uint8_t>(byte + ('a' - 'A')) | |
| 70 | : byte; | ||
| 71 | 186 | hash = fnv1a_byte(hash, folded); | |
| 72 | } | ||
| 73 | 12 | return hash; | |
| 74 | } | ||
| 75 | |||
| 76 | // A length prefix delimits the raw-byte field for a compiled Pattern's byte or mask span. | ||
| 77 | 480 | [[nodiscard]] std::uint64_t fnv1a_bytes(std::uint64_t hash, std::span<const std::byte> data) noexcept | |
| 78 | { | ||
| 79 | 480 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(data.size())); | |
| 80 | 5288 | for (const std::byte b : data) | |
| 81 | { | ||
| 82 | 4328 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(b)); | |
| 83 | } | ||
| 84 | 480 | return hash; | |
| 85 | } | ||
| 86 | |||
| 87 | // Folds the bounded-jump gap structure. bytes()/mask() carry only fixed segments. Without gap data, | ||
| 88 | // patterns that differ only by variable gaps share a fingerprint. Jump-free patterns retain their prior | ||
| 89 | // fingerprint. | ||
| 90 | 240 | [[nodiscard]] std::uint64_t fnv1a_pattern_jumps(std::uint64_t hash, const scan::Pattern &pattern) noexcept | |
| 91 | { | ||
| 92 | 240 | const detail::PatternBuffer &buffer = detail::pattern_buffer(pattern); | |
| 93 | 240 | if (buffer.jump_count == 0) | |
| 94 | { | ||
| 95 | 217 | return hash; | |
| 96 | } | ||
| 97 | 23 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(buffer.jump_count)); | |
| 98 | 46 | for (std::size_t index = 0; index < buffer.jump_count; ++index) | |
| 99 | { | ||
| 100 | 23 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(buffer.jumps[index].position)); | |
| 101 | 23 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(buffer.jumps[index].min_skip)); | |
| 102 | 23 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(buffer.jumps[index].max_skip)); | |
| 103 | } | ||
| 104 | 23 | return hash; | |
| 105 | } | ||
| 106 | |||
| 107 | // Hashes one candidate's address-independent content. It includes compiled bytes, mask, offset, and decode | ||
| 108 | // parameters for byte tiers. It includes owned name, literal, and shape flags for text tiers. | ||
| 109 | 70 | [[nodiscard]] std::uint64_t fnv1a_candidate(std::uint64_t hash, const scan::Candidate &candidate) noexcept | |
| 110 | { | ||
| 111 | 70 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(candidate.mode())); | |
| 112 | 70 | switch (candidate.mode()) | |
| 113 | { | ||
| 114 | 58 | case scan::Mode::Direct: | |
| 115 | { | ||
| 116 | 58 | const scan::DirectPattern &direct = *candidate.as_direct(); | |
| 117 | 58 | hash = fnv1a_bytes(hash, direct.pattern.bytes()); | |
| 118 | 58 | hash = fnv1a_bytes(hash, direct.pattern.mask()); | |
| 119 | 58 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(direct.pattern.offset())); | |
| 120 | 58 | hash = fnv1a_int(hash, static_cast<std::int64_t>(direct.walk_back)); | |
| 121 | 58 | hash = fnv1a_pattern_jumps(hash, direct.pattern); | |
| 122 | 58 | break; | |
| 123 | } | ||
| 124 | 4 | case scan::Mode::RipRelative: | |
| 125 | { | ||
| 126 | 4 | const scan::RipRelativePattern &rip = *candidate.as_rip_relative(); | |
| 127 | 4 | hash = fnv1a_bytes(hash, rip.pattern.bytes()); | |
| 128 | 4 | hash = fnv1a_bytes(hash, rip.pattern.mask()); | |
| 129 | 4 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(rip.pattern.offset())); | |
| 130 | 4 | hash = fnv1a_int(hash, static_cast<std::int64_t>(rip.displacement_at)); | |
| 131 | 4 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(rip.instruction_length)); | |
| 132 | 4 | hash = fnv1a_pattern_jumps(hash, rip.pattern); | |
| 133 | 4 | break; | |
| 134 | } | ||
| 135 | 4 | case scan::Mode::RttiVtable: | |
| 136 | 4 | hash = fnv1a_field(hash, candidate.as_rtti_vtable()->mangled); | |
| 137 | 4 | break; | |
| 138 | 4 | case scan::Mode::StringXref: | |
| 139 | { | ||
| 140 | 4 | const scan::StringXref &xref = *candidate.as_string_xref(); | |
| 141 | 4 | hash = fnv1a_field(hash, xref.text); | |
| 142 | 4 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(xref.encoding)); | |
| 143 | 4 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(xref.return_mode)); | |
| 144 | 4 | hash = fnv1a_byte(hash, xref.require_terminator ? 1U : 0U); | |
| 145 | 4 | hash = fnv1a_byte(hash, xref.broad_match ? 1U : 0U); | |
| 146 | 4 | break; | |
| 147 | } | ||
| 148 | } | ||
| 149 | 70 | return hash; | |
| 150 | } | ||
| 151 | |||
| 152 | [[nodiscard]] std::uint64_t | ||
| 153 | 55 | fnv1a_cascade(std::uint64_t hash, std::span<const scan::Candidate> site) noexcept | |
| 154 | { | ||
| 155 | 55 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(site.size())); | |
| 156 | 180 | for (const scan::Candidate &candidate : site) | |
| 157 | { | ||
| 158 | 70 | hash = fnv1a_candidate(hash, candidate); | |
| 159 | } | ||
| 160 | 55 | return hash; | |
| 161 | } | ||
| 162 | |||
| 163 | // Hashes one anchor's evidence without quorum recursion. If a malformed sub-anchor sends Quorum here, | ||
| 164 | // only its kind contributes. This limits recursion to one level. | ||
| 165 | 315 | [[nodiscard]] std::uint64_t fingerprint_evidence(const Anchor &anchor) noexcept | |
| 166 | { | ||
| 167 | 315 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(anchor.kind)); | |
| 168 | 315 | switch (anchor.kind) | |
| 169 | { | ||
| 170 | 184 | case AnchorKind::VtableIdentity: | |
| 171 | 184 | hash = fnv1a_field(hash, anchor.mangled); | |
| 172 | 184 | break; | |
| 173 | 51 | case AnchorKind::RipGlobal: | |
| 174 | 51 | hash = fnv1a_cascade(hash, anchor.site); | |
| 175 | // Preserve legacy fingerprints for the default Readable policy. Treat any other page policy as a | ||
| 176 | // declarative signature change that a persisted baseline can detect. | ||
| 177 | 51 | if (anchor.pages != scan::Pages::Readable) | |
| 178 | { | ||
| 179 | 2 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(anchor.pages)); | |
| 180 | } | ||
| 181 | 51 | break; | |
| 182 | 4 | case AnchorKind::CodeOperand: | |
| 183 | 4 | hash = fnv1a_cascade(hash, anchor.site); | |
| 184 | 4 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(anchor.operand_kind)); | |
| 185 | 4 | hash = fnv1a_byte(hash, anchor.operand_index); | |
| 186 | 4 | hash = fnv1a_byte(hash, anchor.byte_width); | |
| 187 | 4 | break; | |
| 188 | 2 | case AnchorKind::StringXref: | |
| 189 | 2 | hash = fnv1a_field(hash, anchor.xref_text); | |
| 190 | 2 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(anchor.xref_encoding)); | |
| 191 | 2 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(anchor.xref_return)); | |
| 192 | 2 | hash = fnv1a_byte(hash, anchor.xref_require_terminator ? 1U : 0U); | |
| 193 | 2 | hash = fnv1a_byte(hash, anchor.xref_broad_match ? 1U : 0U); | |
| 194 | 2 | break; | |
| 195 | 12 | case AnchorKind::ExportName: | |
| 196 | // The module and export name are the whole declarative signature. | ||
| 197 | 12 | hash = fnv1a_field(hash, anchor.export_module); | |
| 198 | 12 | hash = fnv1a_field(hash, anchor.export_name); | |
| 199 | 12 | break; | |
| 200 | 59 | case AnchorKind::Manual: | |
| 201 | 59 | hash = fnv1a_int(hash, anchor.manual_value); | |
| 202 | 59 | break; | |
| 203 | 3 | case AnchorKind::CallArgHome: | |
| 204 | case AnchorKind::Quorum: | ||
| 205 | case AnchorKind::Unset: | ||
| 206 | // No address-independent evidence beyond the kind byte already folded above. | ||
| 207 | 3 | break; | |
| 208 | } | ||
| 209 | 315 | return hash; | |
| 210 | } | ||
| 211 | |||
| 212 | // Independence evidence asks whether two anchors can decode one site, while drift evidence asks whether a | ||
| 213 | // declaration changed. Each anchor becomes a set of site evidence atoms. Scan policy and AnchorKind | ||
| 214 | // wrappers do not alter the site. Thus policy variants and a flat StringXref versus a one-rung RipGlobal | ||
| 215 | // over the same literal count as one signal. EvidenceClass, rather than AnchorKind or scan::Mode, tags each | ||
| 216 | // atom. | ||
| 217 | enum class EvidenceClass : std::uint8_t | ||
| 218 | { | ||
| 219 | ByteDirect = 1, | ||
| 220 | ByteRip = 2, | ||
| 221 | Vtable = 3, | ||
| 222 | String = 4, | ||
| 223 | Manual = 5, | ||
| 224 | Empty = 6, | ||
| 225 | Export = 7, | ||
| 226 | }; | ||
| 227 | |||
| 228 | // A located literal uses its bytes and storage format as identity. Utf8 "foo" and Utf16le "foo" are | ||
| 229 | // different image literals. Scan facets are not evidence. | ||
| 230 | [[nodiscard]] std::uint64_t | ||
| 231 | 11 | string_evidence_atom(std::string_view text, scan::StringEncoding encoding) noexcept | |
| 232 | { | ||
| 233 | 11 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::String)); | |
| 234 | 11 | hash = fnv1a_field(hash, text); | |
| 235 | 11 | return fnv1a_byte(hash, static_cast<std::uint8_t>(encoding)); | |
| 236 | } | ||
| 237 | |||
| 238 | // The mangled type name identifies vtable evidence. | ||
| 239 | 4 | [[nodiscard]] std::uint64_t vtable_evidence_atom(std::string_view mangled) noexcept | |
| 240 | { | ||
| 241 | 4 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::Vtable)); | |
| 242 | 4 | return fnv1a_field(hash, mangled); | |
| 243 | } | ||
| 244 | |||
| 245 | // A named export uses its declared module and export name as identity. The live table decides which names | ||
| 246 | // alias, so backend provenance resolves aliases later. Use the declared module name, not a resolved base. | ||
| 247 | // A base key collapses distinct unloaded modules onto one empty base. quorum_sub_anchors_independent | ||
| 248 | // catches the empty and explicit module overlap. | ||
| 249 | [[nodiscard]] std::uint64_t | ||
| 250 | 12 | export_evidence_atom(std::string_view module_name, std::string_view export_name) noexcept | |
| 251 | { | ||
| 252 | 12 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::Export)); | |
| 253 | 12 | hash = fnv1a_module_field(hash, module_name); | |
| 254 | 12 | return fnv1a_field(hash, export_name); | |
| 255 | } | ||
| 256 | |||
| 257 | // Builds one candidate rung's site identity atom without kind or policy data. A byte tier keeps every field | ||
| 258 | // that moves its address. A text tier reduces to its flat AnchorKind atom. | ||
| 259 | 180 | [[nodiscard]] std::uint64_t candidate_evidence_atom(const scan::Candidate &candidate) noexcept | |
| 260 | { | ||
| 261 | 180 | switch (candidate.mode()) | |
| 262 | { | ||
| 263 | 171 | case scan::Mode::Direct: | |
| 264 | { | ||
| 265 | 171 | const scan::DirectPattern &direct = *candidate.as_direct(); | |
| 266 | std::uint64_t hash = | ||
| 267 | 171 | fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::ByteDirect)); | |
| 268 | 171 | hash = fnv1a_bytes(hash, direct.pattern.bytes()); | |
| 269 | 171 | hash = fnv1a_bytes(hash, direct.pattern.mask()); | |
| 270 | 171 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(direct.pattern.offset())); | |
| 271 | 171 | hash = fnv1a_int(hash, static_cast<std::int64_t>(direct.walk_back)); | |
| 272 | 171 | return fnv1a_pattern_jumps(hash, direct.pattern); | |
| 273 | } | ||
| 274 | 7 | case scan::Mode::RipRelative: | |
| 275 | { | ||
| 276 | 7 | const scan::RipRelativePattern &rip = *candidate.as_rip_relative(); | |
| 277 | 7 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::ByteRip)); | |
| 278 | 7 | hash = fnv1a_bytes(hash, rip.pattern.bytes()); | |
| 279 | 7 | hash = fnv1a_bytes(hash, rip.pattern.mask()); | |
| 280 | 7 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(rip.pattern.offset())); | |
| 281 | 7 | hash = fnv1a_int(hash, static_cast<std::int64_t>(rip.displacement_at)); | |
| 282 | 7 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(rip.instruction_length)); | |
| 283 | 7 | return fnv1a_pattern_jumps(hash, rip.pattern); | |
| 284 | } | ||
| 285 | ✗ | case scan::Mode::RttiVtable: | |
| 286 | ✗ | return vtable_evidence_atom(candidate.as_rtti_vtable()->mangled); | |
| 287 | 2 | case scan::Mode::StringXref: | |
| 288 | { | ||
| 289 | 2 | const scan::StringXref &xref = *candidate.as_string_xref(); | |
| 290 | 2 | return string_evidence_atom(xref.text, xref.encoding); | |
| 291 | } | ||
| 292 | } | ||
| 293 | ✗ | return fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::Empty)); | |
| 294 | } | ||
| 295 | |||
| 296 | // Collects one site evidence atom per resolvable rung, or one for a flat kind. Do not reuse the drift | ||
| 297 | // fingerprint here. It keeps policy and order because a facet edit or reorder is signature drift. | ||
| 298 | 222 | void collect_independence_atoms(const Anchor &anchor, std::vector<std::uint64_t> &out) | |
| 299 | { | ||
| 300 | 222 | const std::size_t start = out.size(); | |
| 301 | 222 | switch (anchor.kind) | |
| 302 | { | ||
| 303 | 4 | case AnchorKind::VtableIdentity: | |
| 304 | 4 | out.push_back(vtable_evidence_atom(anchor.mangled)); | |
| 305 | 4 | break; | |
| 306 | 9 | case AnchorKind::StringXref: | |
| 307 | 9 | out.push_back(string_evidence_atom(anchor.xref_text, anchor.xref_encoding)); | |
| 308 | 9 | break; | |
| 309 | 12 | case AnchorKind::ExportName: | |
| 310 | 12 | out.push_back(export_evidence_atom(anchor.export_module, anchor.export_name)); | |
| 311 | 12 | break; | |
| 312 | 21 | case AnchorKind::Manual: | |
| 313 | { | ||
| 314 | 21 | std::uint64_t atom = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::Manual)); | |
| 315 | 21 | out.push_back(fnv1a_int(atom, anchor.manual_value)); | |
| 316 | 21 | break; | |
| 317 | } | ||
| 318 | 176 | case AnchorKind::RipGlobal: | |
| 319 | case AnchorKind::CodeOperand: | ||
| 320 | // Both kinds resolve through a rung site, which defines the failure domain. Each rung contributes | ||
| 321 | // only its site atom. The atom deliberately omits a CodeOperand selector. Two selectors over one | ||
| 322 | // site form one witness. | ||
| 323 | 532 | for (const scan::Candidate &candidate : anchor.site) | |
| 324 | { | ||
| 325 | 180 | out.push_back(candidate_evidence_atom(candidate)); | |
| 326 | } | ||
| 327 | 176 | break; | |
| 328 | ✗ | case AnchorKind::CallArgHome: | |
| 329 | case AnchorKind::Quorum: | ||
| 330 | case AnchorKind::Unset: | ||
| 331 | // No resolvable evidence exists. The post-switch guard contributes a kind-tagged Empty atom. | ||
| 332 | ✗ | break; | |
| 333 | } | ||
| 334 | 222 | if (out.size() == start) | |
| 335 | { | ||
| 336 | // Contribute one kind-tagged Empty atom so the set is never empty. Two degenerate anchors of the | ||
| 337 | // same kind remain dependent. | ||
| 338 | ✗ | std::uint64_t atom = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(EvidenceClass::Empty)); | |
| 339 | ✗ | out.push_back(fnv1a_byte(atom, static_cast<std::uint8_t>(anchor.kind))); | |
| 340 | } | ||
| 341 | 222 | } | |
| 342 | |||
| 343 | // Returns true when two resolvable sub-anchors can decode one site. Such anchors share at least one | ||
| 344 | // evidence atom, so one physical signal can satisfy both. Set intersection catches partial overlap. | ||
| 345 | // Two ladders with one shared rung can land on one site and cast two votes. collect_independence_atoms | ||
| 346 | // defines the canonical axes. An atom collision rejects a valid pair and therefore fails closed. | ||
| 347 | 111 | [[nodiscard]] bool same_backend_config(const Anchor &a, const Anchor &b) | |
| 348 | { | ||
| 349 | 111 | std::vector<std::uint64_t> atoms_a; | |
| 350 | 111 | collect_independence_atoms(a, atoms_a); | |
| 351 | 111 | std::vector<std::uint64_t> atoms_b; | |
| 352 | 111 | collect_independence_atoms(b, atoms_b); | |
| 353 | 322 | for (const std::uint64_t atom_a : atoms_a) | |
| 354 | { | ||
| 355 | 327 | for (const std::uint64_t atom_b : atoms_b) | |
| 356 | { | ||
| 357 | 115 | if (atom_a == atom_b) | |
| 358 | { | ||
| 359 | 12 | return true; | |
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | 99 | return false; | |
| 364 | 111 | } | |
| 365 | |||
| 366 | // This independence gate fails closed before agreement. The same Anchor object, two Manual literals, or | ||
| 367 | // one shared backend config are not independent. Two Manual values prove no live-image corroboration. | ||
| 368 | 114 | [[nodiscard]] bool quorum_sub_anchors_independent(const Anchor &a, const Anchor &b) | |
| 369 | { | ||
| 370 | 114 | if (&a == &b) | |
| 371 | { | ||
| 372 | 1 | return false; | |
| 373 | } | ||
| 374 | 113 | if (a.kind == AnchorKind::Manual && b.kind == AnchorKind::Manual) | |
| 375 | { | ||
| 376 | 1 | return false; | |
| 377 | } | ||
| 378 | // Two same-name ExportName members are one witness if the modules match or either module is empty. | ||
| 379 | // The empty module can resolve inside the named module. The static atom cannot detect this overlap. | ||
| 380 | 14 | if (a.kind == AnchorKind::ExportName && b.kind == AnchorKind::ExportName && | |
| 381 | 127 | a.export_name == b.export_name && | |
| 382 | 3 | (a.export_module.empty() || b.export_module.empty() || a.export_module == b.export_module)) | |
| 383 | { | ||
| 384 | 1 | return false; | |
| 385 | } | ||
| 386 | 111 | return !same_backend_config(a, b); | |
| 387 | } | ||
| 388 | |||
| 389 | constexpr std::uint64_t NULL_SUB_ANCHOR = 0; | ||
| 390 | |||
| 391 | 184 | [[nodiscard]] std::uint64_t quorum_member_evidence(const Anchor *member) noexcept | |
| 392 | { | ||
| 393 | 184 | return member != nullptr ? fingerprint_evidence(*member) : NULL_SUB_ANCHOR; | |
| 394 | } | ||
| 395 | } // anonymous namespace | ||
| 396 | |||
| 397 | namespace internal | ||
| 398 | { | ||
| 399 | // Contract in internal/anchor_evidence.hpp. | ||
| 400 | 71 | bool quorum_members_pairwise_independent(std::span<const Anchor *const> members) | |
| 401 | { | ||
| 402 | 203 | for (std::size_t i = 0; i < members.size(); ++i) | |
| 403 | { | ||
| 404 | 246 | for (std::size_t j = i + 1; j < members.size(); ++j) | |
| 405 | { | ||
| 406 | 114 | if (!quorum_sub_anchors_independent(*members[i], *members[j])) | |
| 407 | { | ||
| 408 | 15 | return false; | |
| 409 | } | ||
| 410 | } | ||
| 411 | } | ||
| 412 | 56 | return true; | |
| 413 | } | ||
| 414 | } // namespace internal | ||
| 415 | |||
| 416 | 155 | std::uint64_t anchor_fingerprint(const Anchor &anchor) noexcept | |
| 417 | { | ||
| 418 | 155 | if (anchor.kind != AnchorKind::Quorum) | |
| 419 | { | ||
| 420 | 139 | return fingerprint_evidence(anchor); | |
| 421 | } | ||
| 422 | |||
| 423 | // A quorum folds member evidence without order because the vote is symmetric. It also folds the effective | ||
| 424 | // threshold and agreement policy. Member hashes use sorted order without allocation. A null member adds a | ||
| 425 | // fixed sentinel. | ||
| 426 | 16 | const std::span<const Anchor *const> members = anchor.quorum_members; | |
| 427 | |||
| 428 | 16 | std::uint64_t hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(AnchorKind::Quorum)); | |
| 429 | 16 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(members.size())); | |
| 430 | |||
| 431 | 16 | std::uint64_t previous = 0; | |
| 432 | 16 | bool have_previous = false; | |
| 433 | 16 | std::size_t emitted = 0; | |
| 434 | 52 | while (emitted < members.size()) | |
| 435 | { | ||
| 436 | 36 | std::uint64_t next = 0; | |
| 437 | 36 | bool found_next = false; | |
| 438 | 164 | for (const Anchor *member : members) | |
| 439 | { | ||
| 440 | 92 | const std::uint64_t evidence = quorum_member_evidence(member); | |
| 441 | 92 | if (have_previous && evidence <= previous) | |
| 442 | { | ||
| 443 | 27 | continue; | |
| 444 | } | ||
| 445 | 65 | if (!found_next || evidence < next) | |
| 446 | { | ||
| 447 | 39 | next = evidence; | |
| 448 | 39 | found_next = true; | |
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | 36 | if (!found_next) | |
| 453 | { | ||
| 454 | ✗ | break; | |
| 455 | } | ||
| 456 | |||
| 457 | 36 | std::size_t duplicate_count = 0; | |
| 458 | 164 | for (const Anchor *member : members) | |
| 459 | { | ||
| 460 | 92 | if (quorum_member_evidence(member) == next) | |
| 461 | { | ||
| 462 | 40 | ++duplicate_count; | |
| 463 | } | ||
| 464 | } | ||
| 465 | 76 | for (std::size_t i = 0; i < duplicate_count; ++i) | |
| 466 | { | ||
| 467 | 40 | hash = fnv1a_int(hash, next); | |
| 468 | } | ||
| 469 | |||
| 470 | 36 | previous = next; | |
| 471 | 36 | have_previous = true; | |
| 472 | 36 | emitted += duplicate_count; | |
| 473 | } | ||
| 474 | const std::size_t effective_threshold = | ||
| 475 | 16 | (anchor.quorum_threshold == 0) ? members.size() : anchor.quorum_threshold; | |
| 476 | 16 | hash = fnv1a_int(hash, static_cast<std::uint64_t>(effective_threshold)); | |
| 477 | 16 | hash = fnv1a_byte(hash, static_cast<std::uint8_t>(anchor.quorum_match)); | |
| 478 | 16 | hash = fnv1a_int(hash, anchor.quorum_tolerance); | |
| 479 | 16 | return hash; | |
| 480 | } | ||
| 481 | |||
| 482 | 11 | std::uint64_t anchor_trust_fingerprint(const Anchor &anchor, scan::ImageIdentity scope_identity) noexcept | |
| 483 | { | ||
| 484 | 11 | std::uint64_t hash = 0; | |
| 485 | 11 | if (anchor.kind == AnchorKind::ExportName) | |
| 486 | { | ||
| 487 | // Bind the effective module by identity, not by the declared export_module text. An empty module from | ||
| 488 | // scope and an explicit name for that module therefore fold to one key. | ||
| 489 | 6 | hash = fnv1a_byte(FNV1A64_OFFSET, static_cast<std::uint8_t>(AnchorKind::ExportName)); | |
| 490 | 6 | hash = fnv1a_field(hash, anchor.export_name); | |
| 491 | } | ||
| 492 | else | ||
| 493 | { | ||
| 494 | 5 | hash = anchor_fingerprint(anchor); | |
| 495 | } | ||
| 496 | // Fold the effective image identity last: a same-base remap changes it, while ASLR alone does not. | ||
| 497 | 11 | return fnv1a_int(hash, scope_identity.token()); | |
| 498 | } | ||
| 499 | } // namespace anchor | ||
| 500 | } // namespace DetourModKit | ||
| 501 |