SPB Git

Repository Public

FuzzReplayTests.swift 5.9 KB 122 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : security/tests/Tests/FuzzReplayTests/FuzzReplayTests.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 AuditSupport42import Foundation43import FuzzTargets44import QyvoraMath45import Testing4647// Regression layer of the fuzzing work: every committed corpus entry and every recorded crash48// input goes through its harness again, in this process. A harness that traps ends the test49// process, which the orchestrator reports as a failure: a fixed crash stays fixed.50//51// The harnesses share one outcome register, so the suite is serialised.5253@Suite(.serialized)54struct FuzzReplayTests {55    static let corpus = AuditPaths.fuzzing.appendingPathComponent("corpus")56    static let crashes = AuditPaths.fuzzing.appendingPathComponent("crashes")5758    @Test func registryIsComplete() {59        let names = FuzzRegistry.names60        #expect(names.count == Set(names).count, "duplicate target names")61        #expect(names.count >= 40)62        // every target except the one with private runtime seeds has committed seeds63        var missing = [String]()64        for t in FuzzRegistry.all where t.name != "identity-private" {65            if FuzzFiles.load(Self.corpus.appendingPathComponent(t.name)).isEmpty { missing.append(t.name) }66        }67        #expect(missing.isEmpty, "targets without committed seeds: \(missing) — run `qyfuzz seed`")68        AuditRecorder.record(area: "fuzz", name: "registry", cases: names.count, failed: missing.count, note: "targets with a committed seed corpus")69    }7071    @Test(arguments: FuzzRegistry.names)72    func corpusReplay(_ name: String) throws {73        let target = try #require(FuzzRegistry.target(name))74        let files = FuzzFiles.load(Self.corpus.appendingPathComponent(name))75        for f in files { _ = FuzzEngine.replay(target, f.bytes) }76        // runtime seeds too (generated in this process)77        let runtime = target.runtimeSeeds?() ?? []78        for s in runtime { _ = FuzzEngine.replay(target, s) }79        AuditRecorder.record(area: "fuzz", name: "corpus-replay/\(name)", algorithm: name, cases: files.count + runtime.count, failed: 0, note: "committed and runtime seeds replayed without a trap")80    }8182    @Test(arguments: FuzzRegistry.names)83    func crashReplay(_ name: String) throws {84        let target = try #require(FuzzRegistry.target(name))85        let dir = Self.crashes.appendingPathComponent(name)86        let files = FuzzFiles.load(dir).filter { $0.name.hasSuffix(".bin") }87        guard !files.isEmpty else { return }88        let trusted = Set(FuzzFiles.load(Self.corpus.appendingPathComponent(name)).map(\.bytes))89        var failed = 090        for f in files {91            let r = FuzzEngine.replay(target, f.bytes)92            if target.acceptOnlySeeds, r.accepted, !trusted.contains(f.bytes) {93                failed += 194                Issue.record("\(name): recorded input \(f.name) is still accepted although it is not a valid seed")95            }96        }97        AuditRecorder.record(area: "fuzz", name: "crash-replay/\(name)", algorithm: name, cases: files.count, failed: failed, note: "recorded crash inputs replayed without a trap")98    }99100    /// A short deterministic mutation pass in this process (the long campaigns run in `qyfuzz`): it puts every101    /// harness and the mutator under whatever sanitizer the test run was built with.102    @Test(arguments: FuzzRegistry.names)103    func mutationSmoke(_ name: String) throws {104        let target = try #require(FuzzRegistry.target(name))105        var seeds = FuzzFiles.load(Self.corpus.appendingPathComponent(name)).map(\.bytes) + (target.runtimeSeeds?() ?? [])106        if seeds.isEmpty { seeds = [[0]] }107        let trusted = Set(seeds)108        let seed: UInt64 = 0x5159_5646_555a_5a01  // fixed: this pass is a regression check, the random campaigns are elsewhere109        var mutator = FuzzMutator(seed: seed, target: target)110        var rng = AuditRNG(seed: seed ^ 0xa5a5)111        let rounds = AuditMode.cases(quick: 300, full: 3000)112        var failed = 0113        for _ in 0..<rounds {114            let input = mutator.mutate(rng.pick(seeds), other: rng.pick(seeds))115            let r = FuzzEngine.replay(target, input)116            if target.acceptOnlySeeds, r.accepted, !trusted.contains(input) { failed += 1 }117        }118        #expect(failed == 0, "\(name): mutated inputs were accepted")119        AuditRecorder.record(area: "fuzz", name: "mutation-smoke/\(name)", algorithm: name, cases: rounds, failed: failed, seed: seed, note: "in-process mutations, no trap")120    }121}122
Files main
Menu

Theme