-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathoversized_recovery.rs
More file actions
350 lines (320 loc) · 12.2 KB
/
oversized_recovery.rs
File metadata and controls
350 lines (320 loc) · 12.2 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#![no_main]
//! Fuzz test for oversized journal crash recovery.
//!
//! This test creates valid data, randomly corrupts storage, and verifies
//! that recovery doesn't panic and leaves the journal in a consistent state.
use arbitrary::{Arbitrary, Result, Unstructured};
use commonware_codec::{FixedSize, Read, ReadExt, Write};
use commonware_runtime::{
buffer::paged::CacheRef, deterministic, Blob as _, Buf, BufMut, BufferPooler,
Error as RuntimeError, Runner, Storage as _, Supervisor as _,
};
use commonware_storage::journal::{
segmented::oversized::{Config, Oversized, Record},
Error as JournalError,
};
use commonware_utils::{NZUsize, NZU16};
use libfuzzer_sys::fuzz_target;
use std::num::{NonZeroU16, NonZeroUsize};
/// Test index entry that stores a u64 id and references a value.
#[derive(Debug, Clone, PartialEq)]
struct TestEntry {
id: u64,
value_offset: u64,
value_size: u32,
}
impl TestEntry {
fn new(id: u64) -> Self {
Self {
id,
value_offset: 0,
value_size: 0,
}
}
}
impl Write for TestEntry {
fn write(&self, buf: &mut impl BufMut) {
self.id.write(buf);
self.value_offset.write(buf);
self.value_size.write(buf);
}
}
impl Read for TestEntry {
type Cfg = ();
fn read_cfg(
buf: &mut impl Buf,
_: &Self::Cfg,
) -> std::result::Result<Self, commonware_codec::Error> {
let id = u64::read(buf)?;
let value_offset = u64::read(buf)?;
let value_size = u32::read(buf)?;
Ok(Self {
id,
value_offset,
value_size,
})
}
}
impl FixedSize for TestEntry {
const SIZE: usize = u64::SIZE + u64::SIZE + u32::SIZE;
}
impl Record for TestEntry {
fn value_location(&self) -> (u64, u32) {
(self.value_offset, self.value_size)
}
fn with_location(mut self, offset: u64, size: u32) -> Self {
self.value_offset = offset;
self.value_size = size;
self
}
}
type TestValue = [u8; 16];
#[derive(Debug, Clone)]
enum CorruptionType {
/// Truncate index to a random size
TruncateIndex { section: u64, size_factor: u8 },
/// Truncate glob to a random size
TruncateGlob { section: u64, size_factor: u8 },
/// Write random bytes at a random offset in index
CorruptIndexBytes {
section: u64,
offset_factor: u8,
data: [u8; 4],
},
/// Write random bytes at a random offset in glob
CorruptGlobBytes {
section: u64,
offset_factor: u8,
data: [u8; 4],
},
/// Delete index section
DeleteIndex { section: u64 },
/// Delete glob section
DeleteGlob { section: u64 },
/// Extend index with garbage
ExtendIndex { section: u64, garbage: [u8; 32] },
/// Extend glob with garbage
ExtendGlob { section: u64, garbage: [u8; 64] },
}
impl<'a> Arbitrary<'a> for CorruptionType {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let variant = u.int_in_range(0..=7)?;
match variant {
0 => Ok(CorruptionType::TruncateIndex {
section: u.int_in_range(1..=3)?,
size_factor: u.arbitrary()?,
}),
1 => Ok(CorruptionType::TruncateGlob {
section: u.int_in_range(1..=3)?,
size_factor: u.arbitrary()?,
}),
2 => Ok(CorruptionType::CorruptIndexBytes {
section: u.int_in_range(1..=3)?,
offset_factor: u.arbitrary()?,
data: u.arbitrary()?,
}),
3 => Ok(CorruptionType::CorruptGlobBytes {
section: u.int_in_range(1..=3)?,
offset_factor: u.arbitrary()?,
data: u.arbitrary()?,
}),
4 => Ok(CorruptionType::DeleteIndex {
section: u.int_in_range(1..=3)?,
}),
5 => Ok(CorruptionType::DeleteGlob {
section: u.int_in_range(1..=3)?,
}),
6 => Ok(CorruptionType::ExtendIndex {
section: u.int_in_range(1..=3)?,
garbage: u.arbitrary()?,
}),
_ => Ok(CorruptionType::ExtendGlob {
section: u.int_in_range(1..=3)?,
garbage: u.arbitrary()?,
}),
}
}
}
#[derive(Arbitrary, Debug)]
struct FuzzInput {
/// Number of entries per section (1-10)
entries_per_section: [u8; 3],
/// Corruptions to apply before recovery
corruptions: Vec<CorruptionType>,
/// Whether to sync before corruption
sync_before_corrupt: bool,
}
const PAGE_SIZE: NonZeroU16 = NZU16!(128);
const PAGE_CACHE_SIZE: NonZeroUsize = NZUsize!(4);
const INDEX_PARTITION: &str = "fuzz-index";
const VALUE_PARTITION: &str = "fuzz-values";
fn overlaps_existing_blob(offset: u64, write_len: usize, blob_size: u64) -> bool {
let end = offset.saturating_add(write_len as u64);
offset < blob_size && end > offset
}
fn test_cfg(pooler: &impl BufferPooler) -> Config<()> {
Config {
index_partition: INDEX_PARTITION.into(),
value_partition: VALUE_PARTITION.into(),
index_page_cache: CacheRef::from_pooler(pooler, PAGE_SIZE, PAGE_CACHE_SIZE),
index_write_buffer: NZUsize!(512),
value_write_buffer: NZUsize!(512),
compression: None,
codec_config: (),
}
}
fn fuzz(input: FuzzInput) {
let runner = deterministic::Runner::default();
runner.start(|context| async move {
let cfg = test_cfg(&context);
// Phase 1: Create valid data
let mut oversized: Oversized<_, TestEntry, TestValue> =
Oversized::init(context.child("initial"), cfg.clone())
.await
.expect("Failed to init");
let mut entry_id = 0u64;
for (section_idx, &count) in input.entries_per_section.iter().enumerate() {
let section = (section_idx + 1) as u64;
let count = (count % 10) + 1; // 1-10 entries per section
for _ in 0..count {
let value: TestValue = [entry_id as u8; 16];
let entry = TestEntry::new(entry_id);
let _ = oversized.append(section, entry, &value).await;
entry_id += 1;
}
let _ = oversized.sync(section).await;
}
if input.sync_before_corrupt {
let _ = oversized.sync_all().await;
}
drop(oversized);
// Phase 2: Apply corruptions
let mut index_page_integrity_may_be_invalidated = false;
for corruption in &input.corruptions {
match corruption {
CorruptionType::TruncateIndex {
section,
size_factor,
} => {
if let Ok((blob, size)) =
context.open(INDEX_PARTITION, §ion.to_be_bytes()).await
{
let new_size = (size * (*size_factor as u64)) / 256;
let _ = blob.resize(new_size).await;
let _ = blob.sync().await;
}
}
CorruptionType::TruncateGlob {
section,
size_factor,
} => {
if let Ok((blob, size)) =
context.open(VALUE_PARTITION, §ion.to_be_bytes()).await
{
let new_size = (size * (*size_factor as u64)) / 256;
let _ = blob.resize(new_size).await;
let _ = blob.sync().await;
}
}
CorruptionType::CorruptIndexBytes {
section,
offset_factor,
data,
} => {
if let Ok((blob, size)) =
context.open(INDEX_PARTITION, §ion.to_be_bytes()).await
{
if size > 0 {
let offset = (size * (*offset_factor as u64)) / 256;
// Overwriting existing index bytes can invalidate the fixed-journal
// page-integrity checks. Pure extensions/truncations are handled by
// lower-level tail trimming and should not require this allowance.
if overlaps_existing_blob(offset, data.len(), size) {
index_page_integrity_may_be_invalidated = true;
}
let _ = blob.write_at_sync(offset, data.to_vec()).await;
}
}
}
CorruptionType::CorruptGlobBytes {
section,
offset_factor,
data,
} => {
if let Ok((blob, size)) =
context.open(VALUE_PARTITION, §ion.to_be_bytes()).await
{
if size > 0 {
let offset = (size * (*offset_factor as u64)) / 256;
let _ = blob.write_at_sync(offset, data.to_vec()).await;
}
}
}
CorruptionType::DeleteIndex { section } => {
let _ = context
.remove(INDEX_PARTITION, Some(§ion.to_be_bytes()))
.await;
}
CorruptionType::DeleteGlob { section } => {
let _ = context
.remove(VALUE_PARTITION, Some(§ion.to_be_bytes()))
.await;
}
CorruptionType::ExtendIndex { section, garbage } => {
if let Ok((blob, size)) =
context.open(INDEX_PARTITION, §ion.to_be_bytes()).await
{
let _ = blob.write_at_sync(size, garbage.to_vec()).await;
}
}
CorruptionType::ExtendGlob { section, garbage } => {
if let Ok((blob, size)) =
context.open(VALUE_PARTITION, §ion.to_be_bytes()).await
{
let _ = blob.write_at_sync(size, garbage.to_vec()).await;
}
}
}
}
// Phase 3: Recovery - this should not panic
let mut recovered: Oversized<_, TestEntry, TestValue> =
match Oversized::init(context.child("recovered"), cfg.clone()).await {
Ok(recovered) => recovered,
// Existing-byte overwrites in the paged index can invalidate fixed-journal
// integrity checks before oversized recovery has a chance to inspect entries.
Err(JournalError::Runtime(RuntimeError::InvalidChecksum))
if index_page_integrity_may_be_invalidated =>
{
return;
}
Err(err) => panic!("Unexpected recovery failure: {err:?}"),
};
// Phase 4: Verify get operations don't panic
// Note: Value checksums are verified lazily on read, not during recovery.
// So an entry may exist but get_value() may return ChecksumMismatch - this is expected.
for section in 1u64..=3 {
let mut pos = 0u64;
while let Ok(entry) = recovered.get(section, pos).await {
// Entry exists, verify get_value doesn't panic (may return error)
let (offset, size) = entry.value_location();
let _ = recovered.get_value(section, offset, size).await;
pos += 1;
}
}
// Phase 5: Verify we can append after recovery
for section in 1u64..=3 {
let value: TestValue = [0xFF; 16];
let entry = TestEntry::new(u64::MAX);
let append_result = recovered.append(section, entry, &value).await;
// Append should succeed (recovery should have left journal in appendable state)
assert!(
append_result.is_ok(),
"Should be able to append to section {section} after recovery"
);
}
let _ = recovered.destroy().await;
});
}
fuzz_target!(|input: FuzzInput| {
fuzz(input);
});