unityrs-core 1.0.0

Read and write Unity asset bundles, serialized files and typetrees.
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::sync::Arc;

use super::{Counting, DecodeBuf, MemoryBudget, OWNER_COST, Permit, Reservation, Unbounded};
use crate::error::LimitKind;
use crate::limits::{ChargeError, Meter, heap_cost};

fn meter(limit: u64) -> Arc<Meter> {
    Meter::new(limit, Arc::new(Unbounded), Reservation::Input)
}

#[test]
fn a_permit_lives_as_long_as_its_last_slice() {
    let budget = Arc::new(Counting::new(1000));
    let m = Meter::new(u64::MAX, budget.clone(), Reservation::Input);
    let mut out = DecodeBuf::new(&m, 60, 1 << 20, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[7; 60]).unwrap();
    assert_eq!(
        budget.used(),
        heap_cost(60),
        "the meter booked it with the budget"
    );
    let whole = out.into_bytes().unwrap();
    let slice = whole.slice(10..20);
    drop(whole);
    let held = heap_cost(60) + OWNER_COST;
    assert_eq!(budget.used(), held, "the buffer and its owner record");
    assert_eq!(m.used(), held);
    drop(slice);
    assert_eq!((budget.used(), m.used()), (0, 0));
}

#[test]
fn a_decode_is_refused_before_it_outgrows_the_meter() {
    // A block declared at 10 bytes that keeps decoding: the meter holds
    // 200 bytes of buffer, and the owner record wrapping it at the end.
    let limit = 200 + OWNER_COST;
    let m = meter(limit);
    let mut out = DecodeBuf::new(&m, 10, 1 << 30, LimitKind::BlockOutput).unwrap();
    assert_eq!(m.used(), heap_cost(10));
    out.extend_from_slice(&[1; 10]).unwrap();
    let e = out.grow_zeroed(1000).unwrap_err();
    assert!(
        matches!(&e, ChargeError::Limit(l) if l.kind() == LimitKind::InputMemory),
        "{e:?}"
    );
    assert_eq!(out.capacity(), 10, "never allocated past the meter");
    assert_eq!(out.len(), 10);
    assert_eq!(m.used(), heap_cost(10), "a refused growth charges nothing");
    // Growth the meter can pay for goes through, charged as it grows.
    out.extend_from_slice(&[2; 80]).unwrap();
    assert!(m.used() <= limit);
    assert_eq!(m.used(), out.charged());
    let bytes = out.into_bytes().unwrap();
    assert_eq!(
        m.used(),
        heap_cost(90) + OWNER_COST,
        "the charge equals the buffer kept and its owner record"
    );
    drop(bytes);
    assert_eq!(m.used(), 0);
}

#[test]
fn new_charges_before_it_allocates() {
    let m = meter(100);
    let _held = m.charge(90).unwrap();
    let e = DecodeBuf::new(&m, 64 << 20, 1 << 30, LimitKind::BlockOutput).unwrap_err();
    assert!(matches!(e, ChargeError::Limit(_)), "{e:?}");
    assert_eq!(m.used(), 90);
}

#[test]
fn each_block_of_a_run_has_its_own_cap() {
    let m = meter(1 << 20);
    let mut out = DecodeBuf::new(&m, 0, 16, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[0; 16]).unwrap();
    let e = out.extend_from_slice(&[0]).unwrap_err();
    assert!(
        matches!(&e, ChargeError::Limit(l) if l.kind() == LimitKind::BlockOutput && l.max() == 16 && l.value() == 17),
        "{e:?}"
    );
    // The run's next block may write 16 more, and no further.
    out.begin_block(16, LimitKind::BlockOutput, 16);
    out.extend_from_slice(&[1; 16]).unwrap();
    let before = out.capacity();
    let e = out.grow_zeroed(1).unwrap_err();
    assert!(
        matches!(&e, ChargeError::Limit(l) if l.kind() == LimitKind::BlockOutput),
        "{e:?}"
    );
    assert_eq!(out.capacity(), before, "the cap is checked before growing");
    assert_eq!(out.len(), 32);
}

/// Review round 13: the doubling passed the segment's end, so a block held
/// room no write of it could use, up to twice its cap (a TPK's LZMA payload
/// of 16 MiB held 32 MiB, which its retry's dictionary then had to fit
/// beside). The room stops at the segment's end, in each block of a run.
#[test]
fn room_never_passes_the_segments_end() {
    let m = meter(1 << 20);
    let mut out = DecodeBuf::new(&m, 0, 100, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[1; 60]).unwrap();
    out.extend_from_slice(&[2; 30]).unwrap();
    assert_eq!(out.capacity(), 100, "doubled from 60, cut at the cap");
    assert_eq!(out.charged(), heap_cost(100));
    out.extend_from_slice(&[3; 10]).unwrap();
    assert_eq!(out.capacity(), 100);
    // The run's next block ends 50 bytes past where it begins.
    out.begin_block(50, LimitKind::BlockOutput, 50);
    out.grow_zeroed(1).unwrap();
    assert_eq!(
        out.capacity(),
        150,
        "doubled from 100, cut at the block's end"
    );
    // Below the end it still doubles.
    let mut out = DecodeBuf::new(&m, 0, 1000, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[1; 60]).unwrap();
    out.extend_from_slice(&[2; 30]).unwrap();
    assert_eq!(out.capacity(), 120);
    // A block that declares more than its cap, or a run whose next blocks
    // declare more, still gets no room past its segment's end (perf stage
    // 2's review: the declared end was not cut at the segment's, so this
    // block held room of 120, past its cap of 100).
    for (declared, ahead) in [(10_000, 0), (50, 10_000)] {
        let mut out = DecodeBuf::new(&m, 0, 1 << 20, LimitKind::BlockOutput).unwrap();
        out.set_ahead(ahead);
        out.begin_block(100, LimitKind::BlockOutput, declared);
        out.extend_from_slice(&[1; 60]).unwrap();
        out.extend_from_slice(&[2; 30]).unwrap();
        assert_eq!(out.capacity(), 100, "{declared} {ahead}");
    }
}

/// The read-ahead (`set_ahead`): a run's room doubles past the next block's
/// declared end into what the blocks after it declare, and no further, and
/// a write past both grows geometrically to the segment's end.
#[test]
fn room_reaches_into_what_the_next_blocks_declare() {
    let m = meter(1 << 20);
    let mut out = DecodeBuf::new(&m, 0, 1 << 20, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[1; 100]).unwrap();
    // The next block declares 50, the blocks after it 100.
    out.set_ahead(100);
    out.begin_block(1000, LimitKind::BlockOutput, 50);
    out.grow_zeroed(1).unwrap();
    assert_eq!(out.capacity(), 200, "doubled, within 100 + 50 + 100");
    out.grow_zeroed(49).unwrap();
    // The last block declares 100: the run ends at 250.
    out.set_ahead(0);
    out.begin_block(1000, LimitKind::BlockOutput, 100);
    out.grow_zeroed(50).unwrap();
    assert_eq!(out.capacity(), 200);
    out.grow_zeroed(1).unwrap();
    assert_eq!(out.capacity(), 250, "the doubling cut at the run's end");
    out.grow_zeroed(60).unwrap();
    assert_eq!(out.capacity(), 500, "past it, doubled to the segment's end");
}

#[test]
fn a_dropped_decode_returns_its_charge() {
    let m = meter(1 << 20);
    let mut out = DecodeBuf::new(&m, 0, 1 << 20, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[0; 1000]).unwrap();
    assert!(m.used() >= 1000);
    // The codec fails here (a corrupt stream): the buffer is dropped.
    drop(out);
    assert_eq!(m.used(), 0);
}

#[test]
fn a_full_meter_still_fits_data_that_fits() {
    // 100 bytes in two writes (60 then 40) under a meter that holds 100
    // bytes' allocation and no doubling past it.
    let m = meter(heap_cost(60) + heap_cost(100));
    let mut out = DecodeBuf::new(&m, 60, 1 << 20, LimitKind::BlockOutput).unwrap();
    out.extend_from_slice(&[1; 60]).unwrap();
    out.extend_from_slice(&[2; 40]).unwrap();
    assert_eq!(out.len(), 100);
    // Wrapping it needs the owner record, which this meter has no room for.
    assert!(out.into_bytes().is_err());
    assert_eq!(m.used(), 0);
}

#[test]
fn a_decode_repeated_for_every_read_stops_at_the_work_bound() {
    // Round 3: a block that fails after decoding 1 MiB, measured again
    // by every entry's read. Each attempt returns its memory; the work
    // is counted and stops the fourth attempt before it allocates.
    let limits = crate::limits::Limits {
        max_decode_work: 3 << 20,
        ..crate::limits::Limits::default()
    };
    let m = Meter::for_input(&limits, Arc::new(Unbounded), Reservation::Input);
    for _ in 0..3 {
        let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
        out.grow_zeroed(1 << 20).unwrap();
        drop(out); // the codec failed
    }
    assert_eq!(m.used(), 0);
    let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
    let e = out.grow_zeroed(1 << 20).unwrap_err();
    assert!(
        matches!(&e, ChargeError::Limit(l) if l.kind() == LimitKind::DecodeWork),
        "{e:?}"
    );
    assert_eq!(out.capacity(), 0, "refused before it allocated");
    // A path read or a writer's output is not decode work.
    let mut path = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::InputBytes).unwrap();
    path.grow_zeroed(1 << 20).unwrap();
}

#[test]
fn every_byte_written_counts_whatever_the_buffer_already_holds() {
    // Round 6 (Codex M, claude6 M1): work was counted only on a write
    // that grew the buffer, and only that write's own bytes, so a buffer
    // sized up front (W1), one scratch buffer reused across decodes (W2)
    // and output written in small pieces (W3) decoded without bound.
    let mib = 1usize << 20;
    let work = |max: u64| {
        let limits = crate::limits::Limits {
            max_decode_work: max,
            ..crate::limits::Limits::default()
        };
        Meter::for_input(&limits, Arc::new(Unbounded), Reservation::Input)
    };
    let is_work =
        |e: &ChargeError| matches!(e, ChargeError::Limit(l) if l.kind() == LimitKind::DecodeWork);
    // W1: pre-sized from blocks-info, ten blocks of 1 MiB.
    let m = work(3 << 20);
    let mut done = 0;
    for _ in 0..10 {
        let mut out = DecodeBuf::new(&m, 1 << 20, 1 << 30, LimitKind::BlockOutput).unwrap();
        out.begin_block(1 << 30, LimitKind::BlockOutput, 1 << 30);
        match out.grow_zeroed(mib) {
            Ok(_) => done += 1,
            Err(e) => assert!(is_work(&e), "{e:?}"),
        }
    }
    assert_eq!((done, m.decoded()), (3, 3 << 20), "W1");
    // W2: one scratch buffer, truncated and reused for every decode.
    let m = work(3 << 20);
    let mut scratch = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
    let mut done = 0;
    for _ in 0..100 {
        scratch.truncate(0);
        scratch.begin_block(1 << 30, LimitKind::BlockOutput, 1 << 30);
        match scratch.grow_zeroed(mib) {
            Ok(_) => done += 1,
            Err(e) => assert!(is_work(&e), "{e:?}"),
        }
    }
    assert_eq!((done, m.decoded()), (3, 3 << 20), "W2");
    // W3: 64 MiB written in 4 KiB pieces counts all 64 MiB.
    let m = work(u64::MAX);
    let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
    for _ in 0..(64 << 20) / 4096 {
        out.extend_from_slice(&[0; 4096]).unwrap();
    }
    assert_eq!(m.decoded(), 64 << 20, "W3");
    // Room made ahead is memory, not work: only what is written counts.
    let m = work(u64::MAX);
    let mut out = DecodeBuf::new(&m, 1 << 20, 1 << 30, LimitKind::BlockOutput).unwrap();
    out.reserve(1 << 20).unwrap();
    assert_eq!(m.decoded(), 0);
    out.extend_from_slice(&[1; 10]).unwrap();
    assert_eq!(m.decoded(), 10);
}

#[test]
fn a_refused_growth_counts_no_work_and_the_read_succeeds_once_there_is_room() {
    // Round 4: decode work was counted before the growth was granted, so
    // refusals that decoded nothing used up the allowance, and the
    // transient refusal became a permanent `DecodeWork`.
    let limits = crate::limits::Limits {
        max_decode_work: 4 << 20,
        ..crate::limits::Limits::default()
    };
    let pool = Arc::new(Counting::new(2 << 20));
    let m = Meter::for_input(&limits, pool.clone(), Reservation::Dependency);
    // Another job holds all of the pool but 64 KiB.
    let other = Meter::new(u64::MAX, pool.clone(), Reservation::Input);
    let held = other.charge((2 << 20) - (64 << 10)).unwrap();
    for _ in 0..10 {
        // Ten 1 MiB refusals: more than twice the work bound.
        let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
        let e = out.grow_zeroed(1 << 20).unwrap_err();
        assert!(
            matches!(&e, ChargeError::Budget(b) if b.what == Reservation::Dependency),
            "{e:?}"
        );
        assert!(e.is_transient());
    }
    assert_eq!(m.decoded(), 0, "a refused growth decodes nothing");
    drop(held);
    // The pool has room again: the same decode succeeds, and counts.
    let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput).unwrap();
    out.grow_zeroed(1 << 20).unwrap();
    assert_eq!(m.decoded(), 1 << 20);
    drop(out);
    assert_eq!((m.used(), pool.used()), (0, 0));
}

// The source scan behind "the core starts no thread and never waits"
// (`unityrs_testkit::waits`) takes only an item under `#[cfg(test)]` in its
// own file for test code: this file's `#[cfg(test)] mod tests;` is in
// another.
#[cfg(test)]
#[test]
fn decodes_on_several_threads_never_pass_the_work_bound_together() {
    // Round 5 (Codex M1): the bound was checked before a growth and
    // counted after it, so threads that checked at once all passed and
    // all counted. This budget holds each thread at its first
    // reservation until all eight have arrived (a test budget may block;
    // a real one must not), so every thread has checked the bound before
    // any of them counts: eight 1 MiB growths against 4 MiB of work.
    use std::sync::atomic::{AtomicUsize, Ordering};
    #[derive(Debug)]
    struct Gate {
        barrier: std::sync::Barrier,
        arrived: AtomicUsize,
        threads: usize,
    }
    impl MemoryBudget for Gate {
        fn try_reserve(
            &self,
            bytes: u64,
            _what: Reservation,
        ) -> Result<Permit, super::BudgetExceeded> {
            if self.arrived.fetch_add(1, Ordering::AcqRel) < self.threads {
                self.barrier.wait();
            }
            Ok(Permit::detached(bytes))
        }
    }
    const THREADS: usize = 8;
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let limits = crate::limits::Limits {
            max_decode_work: 4 << 20,
            ..crate::limits::Limits::default()
        };
        let gate = Arc::new(Gate {
            barrier: std::sync::Barrier::new(THREADS),
            arrived: AtomicUsize::new(0),
            threads: THREADS,
        });
        let m = Meter::for_input(&limits, gate, Reservation::Input);
        let outcomes: Vec<_> = (0..THREADS)
            .map(|_| {
                let m = m.clone();
                std::thread::spawn(move || {
                    let mut out = DecodeBuf::new(&m, 0, 1 << 30, LimitKind::BlockOutput)?;
                    out.grow_zeroed(1 << 20)?;
                    Ok::<_, ChargeError>(out)
                })
            })
            .collect::<Vec<_>>()
            .into_iter()
            .map(|t| t.join().unwrap())
            .collect();
        let granted = outcomes.iter().filter(|o| o.is_ok()).count();
        let refused = outcomes
            .iter()
            .filter(
                |o| matches!(o, Err(ChargeError::Limit(l)) if l.kind() == LimitKind::DecodeWork),
            )
            .count();
        let held = m.used();
        let decoded = m.decoded();
        drop(outcomes);
        let _ = tx.send((granted, refused, decoded, held, m.used()));
    });
    let (granted, refused, decoded, held, after) =
        unityrs_testkit::guard::recv(&rx, "the growths to finish");
    assert_eq!(decoded, 4 << 20, "the work bound held");
    assert_eq!((granted, refused), (4, 4));
    assert_eq!(
        held,
        4 * heap_cost(1 << 20),
        "a refused growth kept no charge"
    );
    assert_eq!(after, 0);
}

#[test]
fn permits_merge_by_home() {
    let a = Counting::new(1000);
    let b = Counting::new(1000);
    // A charge's first permit is detached and empty: it takes the home.
    let mut p = Permit::detached(0);
    for _ in 0..100 {
        p.absorb(a.try_reserve(1, Reservation::Input).unwrap());
        p.absorb(Permit::detached(0));
        p.absorb(b.try_reserve(2, Reservation::Input).unwrap());
    }
    assert_eq!(p.records(), 1, "one record for the second home");
    assert_eq!((p.bytes(), a.used(), b.used()), (300, 100, 200));
    drop(p);
    assert_eq!((a.used(), b.used()), (0, 0));
}

#[test]
fn absorbed_permits_from_another_home_still_return() {
    let a = Counting::new(100);
    let b = Counting::new(100);
    let mut p = a.try_reserve(10, Reservation::Input).unwrap();
    p.absorb(a.try_reserve(5, Reservation::Input).unwrap());
    p.absorb(b.try_reserve(7, Reservation::Input).unwrap());
    p.absorb(Permit::detached(3));
    assert_eq!(p.bytes(), 25);
    assert_eq!((a.used(), b.used()), (15, 7));
    p.shrink_to(12);
    assert_eq!(p.bytes(), 12);
    assert_eq!((a.used(), b.used()), (12, 0));
    drop(p);
    assert_eq!((a.used(), b.used()), (0, 0));
}