SPB Git

Repository Public

Coverage.swift 5.5 KB 136 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Sources/FuzzTargets/Coverage.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243// Edge-coverage feedback through LLVM SanitizerCoverage inline 8-bit counters.44//45// The Xcode toolchain refuses `-sanitize=fuzzer` (no libFuzzer), but it accepts46// `-sanitize=address -sanitize-coverage=edge,inline-8bit-counters`. With these flags every47// instrumented module owns an array of one byte per control-flow edge, incremented inline, and48// registers it at load time by calling `__sanitizer_cov_8bit_counters_init`, which is defined49// here. The engine zeroes the counters before an execution and reads them afterwards: an input50// that reaches an edge (or an edge hit-count bucket) never seen before is kept in the corpus.51//52// Built without the flags, no module calls the hook, `FuzzCoverage.available` is false and the53// engine falls back to outcome classes (see Engine.swift).5455nonisolated(unsafe) var coverageRegions: [(start: UnsafeMutablePointer<UInt8>, count: Int)] = []5657@_cdecl("__sanitizer_cov_8bit_counters_init")58public func qyvoraCoverageCountersInit(_ start: UnsafeMutablePointer<UInt8>?, _ stop: UnsafeMutablePointer<UInt8>?) {59    guard let start, let stop, stop > start else { return }60    if coverageRegions.contains(where: { $0.start == start }) { return }61    coverageRegions.append((start, stop - start))62}6364public enum FuzzCoverage {65    public static var available: Bool { !coverageRegions.isEmpty }66    public static var edgeCount: Int { coverageRegions.reduce(0) { $0 + $1.count } }67    public static var moduleCount: Int { coverageRegions.count }6869    /// Zeroes every counter.70    public static func reset() {71        for r in coverageRegions { memset(r.start, 0, r.count) }72    }7374    /// Hit-count bucket of a counter value, as one bit (the libFuzzer / AFL classes 1, 2, 3, 4–7, 8–15, 16–31, 32–127, 128+).75    @inline(__always)76    static func bucket(_ v: UInt8) -> UInt8 {77        switch v {78        case 1: return 179        case 2: return 280        case 3: return 481        case 4...7: return 882        case 8...15: return 1683        case 16...31: return 3284        case 32...127: return 6485        default: return 12886        }87    }8889    /// Folds the counters of the last execution into `seen` (one byte of bucket bits per edge) and returns90    /// (new edges, new edge/bucket pairs). `seen` must hold `edgeCount` bytes.91    public static func collect(into seen: inout [UInt8]) -> (edges: Int, features: Int) {92        var newEdges = 0, newFeatures = 093        var base = 094        seen.withUnsafeMutableBufferPointer { map in95            for r in coverageRegions {96                var i = 097                let p = UnsafeRawPointer(r.start)98                while i + 8 <= r.count {99                    if p.loadUnaligned(fromByteOffset: i, as: UInt64.self) == 0 {100                        i += 8101                        continue102                    }103                    for j in i..<(i + 8) {104                        let v = r.start[j]105                        if v != 0 {106                            let b = bucket(v)107                            let old = map[base + j]108                            if old & b == 0 {109                                if old == 0 { newEdges += 1 }110                                newFeatures += 1111                                map[base + j] = old | b112                            }113                        }114                    }115                    i += 8116                }117                while i < r.count {118                    let v = r.start[i]119                    if v != 0 {120                        let b = bucket(v)121                        let old = map[base + i]122                        if old & b == 0 {123                            if old == 0 { newEdges += 1 }124                            newFeatures += 1125                            map[base + i] = old | b126                        }127                    }128                    i += 1129                }130                base += r.count131            }132        }133        return (newEdges, newFeatures)134    }135}136
Files main
Menu

Theme