oxedyne/fe2o3/fe2o3_jdat/tests/bdat_stream_depth.rs
6.0 KiB, 9 runs
created by r3025735233:77593, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The stream walkers `Dat::load_bytes` and `Dat::count_bytes` are held to a nesting depth. |
| 2 | //! |
| 3 | //! A box, an option or a heterogeneous tuple holds its inner values inline, one code byte a |
| 4 | //! level, so a file of repeated box codes once drove both walkers down one stack frame a byte |
| 5 | //! until the stack overflowed and the process died. Each walker now counts its depth against |
| 6 | //! `DecodeLimits::max_depth` and refuses a value past it with an error. |
| 7 | |
| 8 | use oxedyne_fe2o3_core::{ |
| 9 | prelude::*, |
| 10 | byte::ToBytes, |
| 11 | }; |
| 12 | use oxedyne_fe2o3_jdat::{ |
| 13 | bdat::DecodeLimits, |
| 14 | prelude::*, |
| 15 | }; |
| 16 | |
| 17 | use std::io::Cursor; |
| 18 | |
| 19 | // Deep enough to overflow a test thread's stack many times over without a limit. |
| 20 | const HOSTILE_LEVELS: usize = 1 << 20; |
| 21 | |
| 22 | // `levels` copies of a one-byte wrapper code around an empty value. |
| 23 | fn wrapped(code: u8, levels: usize) -> Vec<u8> { |
| 24 | let mut buf = vec![code; levels]; |
| 25 | buf.push(Dat::EMPTY_CODE); |
| 26 | buf |
| 27 | } |
| 28 | |
| 29 | // A `Tup2` whose first item is the next `Tup2`, `levels` deep, each closed by an empty value. |
| 30 | fn tupled(levels: usize) -> Vec<u8> { |
| 31 | let mut buf = vec![Dat::TUP2_CODE; levels]; |
| 32 | buf.push(Dat::EMPTY_CODE); |
| 33 | buf.extend(std::iter::repeat(Dat::EMPTY_CODE).take(levels)); |
| 34 | buf |
| 35 | } |
| 36 | |
| 37 | fn boxed(levels: usize) -> Dat { |
| 38 | let mut dat = Dat::Empty; |
| 39 | for _ in 0..levels { |
| 40 | dat = Dat::Box(Box::new(dat)); |
| 41 | } |
| 42 | dat |
| 43 | } |
| 44 | |
| 45 | fn refused_as_too_deep(what: &str, outcome: Outcome<impl std::fmt::Debug>) { |
| 46 | match outcome { |
| 47 | Ok(v) => panic!("{}: expected a depth error, got {:?}.", what, v), |
| 48 | Err(e) => { |
| 49 | let msg = e.to_string(); |
| 50 | assert!(msg.contains("depth"), "{}: the error should name the depth: {}", what, msg); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn load_bytes_refuses_a_run_of_box_codes() { |
| 57 | let buf = wrapped(Dat::BOX_CODE, HOSTILE_LEVELS); |
| 58 | refused_as_too_deep("load box", Dat::load_bytes(&mut Cursor::new(buf))); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn load_bytes_refuses_a_run_of_some_codes() { |
| 63 | let buf = wrapped(Dat::OPT_SOME_CODE, HOSTILE_LEVELS); |
| 64 | refused_as_too_deep("load some", Dat::load_bytes(&mut Cursor::new(buf))); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn load_bytes_refuses_nested_tuples() { |
| 69 | let buf = tupled(HOSTILE_LEVELS); |
| 70 | refused_as_too_deep("load tup2", Dat::load_bytes(&mut Cursor::new(buf))); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn count_bytes_refuses_a_run_of_box_codes() { |
| 75 | let buf = wrapped(Dat::BOX_CODE, HOSTILE_LEVELS); |
| 76 | refused_as_too_deep("count box", Dat::count_bytes(&mut Cursor::new(buf))); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn count_bytes_refuses_a_run_of_some_codes() { |
| 81 | let buf = wrapped(Dat::OPT_SOME_CODE, HOSTILE_LEVELS); |
| 82 | refused_as_too_deep("count some", Dat::count_bytes(&mut Cursor::new(buf))); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn count_bytes_refuses_nested_tuples() { |
| 87 | let buf = tupled(HOSTILE_LEVELS); |
| 88 | refused_as_too_deep("count tup2", Dat::count_bytes(&mut Cursor::new(buf))); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_value_within_the_default_depth_still_loads_and_counts() -> Outcome<()> { |
| 93 | // The innermost empty value sits one level below the last box. |
| 94 | let dat = boxed(DecodeLimits::DEFAULT_MAX_DEPTH - 1); |
| 95 | let buf = res!(dat.to_bytes(Vec::new())); |
| 96 | let loaded = res!(Dat::load_bytes(&mut Cursor::new(buf.clone()))); |
| 97 | assert_eq!(loaded, buf); |
| 98 | let n = res!(Dat::count_bytes(&mut Cursor::new(buf.clone()))); |
| 99 | assert_eq!(n, buf.len()); |
| 100 | Ok(()) |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn a_caller_limit_is_held_exactly_by_both_walkers() -> Outcome<()> { |
| 105 | const LEVELS: usize = 16; |
| 106 | // Sixteen boxes and the empty value inside them make seventeen levels. |
| 107 | let buf = res!(boxed(LEVELS).to_bytes(Vec::new())); |
| 108 | let fits = DecodeLimits::default().with_max_depth(LEVELS + 1); |
| 109 | let short = DecodeLimits::default().with_max_depth(LEVELS); |
| 110 | assert_eq!(res!(Dat::load_bytes_limited(&mut Cursor::new(buf.clone()), &fits)), buf); |
| 111 | assert_eq!(res!(Dat::count_bytes_limited(&mut Cursor::new(buf.clone()), &fits)), buf.len()); |
| 112 | refused_as_too_deep("load limited", Dat::load_bytes_limited(&mut Cursor::new(buf.clone()), &short)); |
| 113 | refused_as_too_deep("count limited", Dat::count_bytes_limited(&mut Cursor::new(buf), &short)); |
| 114 | Ok(()) |
| 115 | } |
| 116 | |
| 117 | // A walker reads a list by its length and does not descend, so a deep value inside a list |
| 118 | // passes it at depth 1; the decode that follows must be the one that refuses. |
| 119 | #[test] |
| 120 | fn deep_value_inside_a_list_passes_the_walker_and_stored_decode_refuses() -> Outcome<()> { |
| 121 | let mut buf = vec![Dat::LIST_CODE]; |
| 122 | buf = res!(Dat::C64((HOSTILE_LEVELS + 1) as u64).to_bytes(buf)); |
| 123 | buf.extend(wrapped(Dat::BOX_CODE, HOSTILE_LEVELS)); |
| 124 | let walked = res!(Dat::load_bytes(&mut Cursor::new(buf))); |
| 125 | refused_as_too_deep("stored list", Dat::from_bytes_limited(&walked, &DecodeLimits::STORED)); |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
| 129 | // The stored depth is bounded by the stack: a value at the limit decodes on a spawned thread's |
| 130 | // default 2 MiB stack. |
| 131 | #[test] |
| 132 | fn stored_depth_decodes_on_a_spawned_thread() -> Outcome<()> { |
| 133 | let levels = DecodeLimits::DEFAULT_MAX_STORED_DEPTH - 1; |
| 134 | let buf = res!(boxed(levels).to_bytes(Vec::new())); |
| 135 | let run = std::thread::Builder::new() |
| 136 | .stack_size(2 * 1024 * 1024) // fixed: Rust's default for a spawned thread |
| 137 | .spawn(move || Dat::from_bytes_limited(&buf, &DecodeLimits::STORED).map(|(d, _)| d)); |
| 138 | let join = res!(run.map_err(|e| err!(e, "Spawning the decode thread."; Thread))); |
| 139 | let dat = res!(join.join().map_err(|_| err!("The decode thread panicked."; Thread))); |
| 140 | assert_eq!(res!(dat), boxed(levels)); |
| 141 | Ok(()) |
| 142 | } |
| 143 | |
| 144 | // `Dat::from_bytes` is the decode every caller reaches for, so it is held to the default depth |
| 145 | // rather than trusting its bytes: a value one past the limit is refused, one at it decodes. |
| 146 | #[test] |
| 147 | fn plain_from_bytes_is_held_to_the_stored_depth() -> Outcome<()> { |
| 148 | let lim = DecodeLimits::DEFAULT_MAX_STORED_DEPTH; |
| 149 | refused_as_too_deep("plain decode", Dat::from_bytes(&wrapped(Dat::BOX_CODE, lim))); |
| 150 | let buf = res!(boxed(lim - 1).to_bytes(Vec::new())); |
| 151 | let (dat, _) = res!(Dat::from_bytes(&buf)); |
| 152 | assert_eq!(dat, boxed(lim - 1)); |
| 153 | Ok(()) |
| 154 | } |
| 155 | |
| 156 | // An Ore frame may pass the 64 MiB a reader of untrusted input would set, so the plain decode |
| 157 | // bounds depth alone. |
| 158 | #[test] |
| 159 | fn plain_from_bytes_reads_a_value_past_64_mib() -> Outcome<()> { |
| 160 | let big = Dat::BU64(vec![7u8; 65 * 1024 * 1024]); |
| 161 | let buf = res!(big.to_bytes(Vec::new())); |
| 162 | let (dat, _) = res!(Dat::from_bytes(&buf)); |
| 163 | assert_eq!(dat, big); |
| 164 | Ok(()) |
| 165 | } |