1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Fuzz-only decode entry points (GAUNT-FUZZ-1).
//!
//! This module exists **solely** so the workspace-excluded `batpak-fuzz`
//! cargo-fuzz crate (a path dependency built with
//! `--features dangerous-test-hooks`) can reach the **real** on-disk / untrusted
//! DECODE entry points of the store — with no copies. Every wrapper here calls
//! production code directly so a libFuzzer crash is a crash in real parse logic.
//!
//! The whole module is gated behind `#[cfg(feature = "dangerous-test-hooks")]`
//! and `#[doc(hidden)]`, so:
//! * a default build never compiles it (no production API-surface change), and
//! * even with the feature on it never appears in published docs.
//!
//! ## Contract for the fuzz target authors
//!
//! Each `__fuzz_*` function takes arbitrary `&[u8]` (plus a small scalar where the
//! decoder needs one) and **must never panic by construction beyond what the
//! decoder itself does** — it simply forwards to the real decoder and returns its
//! `Result`/`Option`/discriminant. The fuzz target asserts no-panic; these
//! wrappers add no assertions of their own. Some return types of the underlying
//! decoders are crate-private, so those wrappers collapse the success value to a
//! `bool` / `&'static str` discriminant — the fuzz contract is "does decoding this
//! arbitrary buffer panic", not "what did it decode to".
//!
//! File-path decoders (those that take a directory / open a file rather than a
//! `&[u8]`) get a wrapper that writes the bytes to a freshly-created `tempfile`
//! tree under the real on-disk filename, calls the real loader, and drops the
//! tempfile on return (RAII cleanup). `tempfile` is a normal `[dependencies]`
//! entry of this crate, so it is available in `src` without any feature plumbing.
use crateStoreError;
// ---------------------------------------------------------------------------
// Direct `&[u8]` decoders.
// ---------------------------------------------------------------------------
/// `encoding::from_bytes::<SegmentHeader>(&[u8])`.
///
/// `encoding::from_bytes` and `store::segment::SegmentHeader` are both already
/// `pub`, so a fuzz target *could* call them directly; this wrapper pins the
/// concrete monomorphization (`SegmentHeader`) into one stable entry point and
/// collapses the decoded header to `()` so the target need not name the header
/// type. Returns the real `rmp_serde` decode error on failure.
/// `SidxEntry::decode_from(&[u8], segment_id)` (segment/sidx.rs).
///
/// `SidxEntry` and its decoder are `pub(crate)`; the decoded entry type cannot
/// appear in a `pub fn` signature, so this wrapper discards it and returns
/// `Result<(), StoreError>`. The required SIDX entry buffer length is a fixed
/// `ENTRY_SIZE` (162 bytes); a buffer of any other length is the canonical typed
/// `Err` path. `segment_id` is a free scalar fed straight through to the decoder
/// — the fuzz target can pass any `u64` (e.g. `0`).
/// `decode_checkpoint_data(path, version, &[u8])` (cold_start/checkpoint).
///
/// Wraps the `pub(super)` decoder via the crate-visible
/// `checkpoint::__fuzz_decode_checkpoint_data` shim, which supplies a throwaway
/// `Path` internally. `version` selects the checkpoint body version; `body` is
/// the msgpack body. Returns `true` when the decoder produced `Some`, `false`
/// when it returned `None` (the typed "ignore corrupt checkpoint" path).
/// `decode_checkpoint_snapshot_v6(path, &[u8])` (cold_start/checkpoint).
///
/// Wraps the `pub(super)` v6 snapshot decoder via the crate-visible shim. Returns
/// `true` for `Some`, `false` for `None`.
/// `MmapIndexEntry::decode_from(&[u8], version)` (cold_start/mmap).
///
/// Wraps the `pub(super)` fixed-width mmap entry decoder via the crate-visible
/// `mmap::__fuzz_decode_mmap_entry` shim. Returns `true` for a successful decode,
/// `false` for the typed `Err` path. `version` is fed straight through.
/// `CacheMeta::decode_from_bytes(&[u8])` (projection/mod.rs).
///
/// `CacheMeta` is `pub`, so the real `(remaining_bytes, CacheMeta)` tuple can be
/// returned directly. The decoder splits a small fixed-size meta header off the
/// front and returns the trailing state bytes alongside the parsed meta.
/// Representative concrete state for `decode_cached_state::<T>` fuzzing.
///
/// `decode_cached_state<T>` (projection/flow/mod.rs) is generic over
/// `T: serde::de::DeserializeOwned` and its body is exactly
/// `serde_json::from_slice::<T>(bytes)` — the decode path is identical for every
/// `T`. The crate's real projection-state types are all test-local (private), so
/// there is no public production state type to monomorphize on; this struct
/// stands in for a typical projection state (a couple of scalar counters plus a
/// map), exercising the same monomorphized `serde_json::from_slice` decode path
/// the production callers hit. Documented as the chosen `T` for the contract.
/// `decode_cached_state::<T>(entity, &[u8], warn)` (projection/flow/mod.rs),
/// monomorphized on [`FuzzProjectionState`].
///
/// `decode_cached_state` is a private free fn; this wrapper re-implements its
/// one-line body against the **same** representative `T` so the fuzz crate drives
/// the identical `serde_json::from_slice` decode path the production callers use.
/// Returns `true` when the JSON deserialized, `false` on the warn-and-`None`
/// path. (The underlying fn is private and cannot be re-exported; mirroring its
/// trivial body here keeps the fuzzed code path byte-for-byte identical.)
// ---------------------------------------------------------------------------
// File-path decoders. The wrapper owns a `tempfile::TempDir`/`NamedTempFile`,
// writes the untrusted bytes to the real on-disk filename, calls the real
// loader, and drops the tempfile on return.
// ---------------------------------------------------------------------------
/// `load_cancelled_ranges(dir)` (hidden_ranges.rs).
///
/// Writes `data` to `<tmp>/visibility_ranges.fbv` (the real
/// `VISIBILITY_RANGES_FILENAME`) inside a throwaway `TempDir`, then calls the real
/// loader. The loader returns `Ok(Some(ranges))` on a valid file, `Ok(None)` when
/// absent (never here), or a typed `Err` on corruption. The success value is
/// collapsed to `Result<bool, StoreError>` (`bool` = "ranges present"). Returns
/// the I/O error as a `StoreError::Io` if the temp write itself fails (it should
/// not, but the wrapper never panics).
/// `load_mmap_index(dir, &clock)` (cold_start/mmap/load.rs).
///
/// Writes `data` to `<tmp>/index.fbati` (the real `MMAP_INDEX_FILENAME`) inside a
/// throwaway `TempDir`, then calls the real loader via the crate-visible
/// `mmap::__fuzz_load_mmap_index` shim (which supplies a real `SystemClock`). The
/// loader's private `FileLoad` outcome is returned as a stable `&'static str`
/// discriminant: `"missing" | "loaded" | "invalid" | "future_version"`. Returns
/// `"io_error"` if the temp write itself fails. Never panics.
/// `footer::read_layout(&mut Read+Seek, seg_id)` then
/// `read_entries_unauthenticated` (segment/sidx/footer.rs).
///
/// Writes `data` to a throwaway `NamedTempFile`, opens it `Read + Seek`, and
/// drives BOTH footer-parse paths on the same untrusted file:
/// * `authenticated_string_table_offset` — which internally calls
/// `footer::read_layout` (the private footer fns are reachable only through
/// these `pub(crate)` sidx-module wrappers), and
/// * `read_entries_unauthenticated`.
///
/// Both are seeked from the file start before each call. Returns
/// `Result<(bool, usize), StoreError>` = `(layout authenticated an offset,
/// number of unauthenticated entries parsed)`. `segment_id` is fed through to
/// both; the fuzz target can pass any `u64`.