strawberry/fuzz/fuzz_model_file.cpp
trvon fdd66e8bab
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Stage 4 groundwork: window features, inspection MLP, model files
Nothing runs in the daemon yet; these are the pieces the next increments
train and run in shadow.

- learn/features: a WindowSummary accumulated from a window's events
  (exec locations, memfd/deleted/uid0 execs, depth, connection classes
  and ports, persistence and drop writes, ptrace, module loads, scan
  outcomes, capped surprisal per tuple kind, bounded tokens) and
  featurize(): 48 explicit features scaled to about [0, 1] plus signed
  hashing of tokens into 208 dims, with the governor level and budget
  left as context. A window's own detections are never features: they
  are the teacher labels.
- learn/mlp: 256 -> 128 -> 64 ReLU with a policy head over the
  inspection actions (none, memory_scan, deep_rescan, watch_subtree,
  rescan_on_next_rules) and a value head. Scalar reference, AVX2+FMA
  chosen at runtime, NEON on aarch64; safe default (no inspection) on a
  wrong input size or an inconsistent model. Deterministic init per seed.
- learn/model_file: "SBMD" format with bounded dimensions, exact length,
  a sha256 trailer and finite weights checked on load, each failure
  named; atomic save into a 0700 directory.

Tests: test_model (kernels against scalar for arbitrary sizes, forward
pass invariants, deterministic init, round-trip, every flipped byte and
truncation rejected, non-finite weights rejected, features bounded and
deterministic). fuzz_model_file. NEON is built and tested only on aarch64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 09:53:25 -06:00

23 lines
714 B
C++

// Model file decoder over untrusted bytes: never crashes; whatever it
// accepts re-encodes to the same bytes and runs a forward pass safely.
#include "strawberry/learn/model_file.hpp"
#include <cstdint>
#include <string>
using namespace strawberry::learn;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
const std::string_view in(reinterpret_cast<const char*>(data), size);
Model m;
std::string error;
if (decode_model(in, m, error)) {
if (encode_model(m) != in || !m.consistent()) __builtin_trap();
std::vector<float> x(m.shape.in, 0.5f);
(void) forward(m, x);
} else if (error.empty()) {
__builtin_trap();
}
return 0;
}