#[non_exhaustive]pub struct Limits {
pub max_input_memory: u64,
pub max_input_bytes: u64,
pub max_block_output: u64,
pub max_decode_work: u64,
pub max_inflate: u64,
pub max_container_depth: u32,
pub max_tree_depth: u32,
pub max_value_depth: u32,
pub max_values_per_object: u64,
pub max_zero_size_elements: u64,
pub max_prealloc: usize,
}Expand description
Bounds on allocation, recursion and work per input.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.max_input_memory: u64What the core may hold for one input at once, in bytes, charged to
the input’s Meter before every allocation whose size the input
chooses (module docs). Default 8 GiB.
max_input_bytes: u64The longest input the Environment loads, from any
crate::env::Source: a path’s file is checked from its length
before anything is read, and again as it is read
(crate::env::read_path); a fetch from the size its fetcher states
(crate::env::Fetch::size_hint) before it is fetched, and from the
length of what it returns; given bytes from their length. Default
4 GiB.
max_block_output: u64One block’s actual decoded output (blocks-info’s included). LZMA
decodes to the end of its input whatever blocks-info says
(helpers/CompressionHelper.py:17-44), so a bomb is stopped here
before the meter. Default 4 GiB: a bundle states each block’s size,
and its blocks-info’s, in 32 bits (files/BundleFile.py, each
read_u_int), so no well-formed block’s output reaches it (review
round 9’s sweep; it was 1 GiB, which refused an LZMA bundle, one
block, that decodes to more).
max_decode_work: u64The work one open of an input may cost, in bytes: every byte a
block decode writes, kept or not (a first decode, a measuring decode
whose output is dropped, a re-decode of a shared block, a decode that
fails after it wrote), whether or not its buffer had to grow for it,
and every byte the meter granted to a lazy computation, on the
computation’s own thread, that the meter itself then refused
(LimitKind::InputMemory: a parse or compile that alone needs more
than the meter, which the next ask runs again:
crate::once::get_or_publish_result); and the bytes a parse
searches or indexes (a string’s search, a typetree blob’s one pass
over its string buffer, an old-format file’s type index:
typetree::parse and files::serialized), every byte of a block a
codec reads beyond what it writes, counted before it reads it
(files::bundle: an LZ4 scan that fails writes nothing), the bytes a
bundle entry’s gzip detection reads and inflates (detect), and every byte
the meter granted an entry parse that ended in an error its cell
keeps (files::bundle), all of which K directory rows over one span
repeat K times (round 4); and the work of every read and write of an
object’s value, whatever its outcome (crate::walk::VALUE_WORK a
value built, written or skipped, a primitive run’s bytes, a string’s
bytes a write copies, a ref type a lookup reads,
crate::walk::STEP_WORK a step that builds no value: walk’s
“Work”), which K objects over one span repeat K times, a read refused
by its own bound keeping nothing (fix pass 4’s sweep), as K objects
search one list of ref types; a read or a write counts as it goes and
is refused once its work takes the open past the allowance, and past
the allowance one is refused before it starts. The meter
bounds what is
held; this bounds what is done, so an input cannot make its readers
decode or recompute without end while the meter stays empty
(thousands of blocks that each decode to the block cap, measured and
dropped by a lazy read of the last entry; one type whose compile alone
needs more than the meter, asked by every object of the type). A
write the meter or the budget refuses decodes nothing and counts
nothing, and a lazy computation the budget, an I/O failure or a
provider refused counts nothing either (those depend on what else is
held or on caller code, and the engine bounds its retries), so
contention never uses the allowance up; a write is counted atomically
(between a growth’s charge and its allocation when it grows), so
decodes running at once never pass the bound
(Meter::count_decode). It is counted
per open: every file of one open shares its meter’s count, and the
Environment reopens an evicted dependency on Meter::reopen, so
eviction churn costs work in proportion to the reads that cause it
and never exhausts a valid dependency. UnityPy decodes every block
once; a lazy read of a bundle whose entries do not overlap decodes a
block at most four times: measured, decoded for the entries wholly
inside it, and decoded again for each of the two entries that
straddle its edges (plan section 3.6, “Runs”). Default 32 GiB, four
times Self::max_input_memory.
max_inflate: u64One gzip stream inflated by detection (rule 7, streamed into a fixed scratch buffer and never held). Default 4 GiB.
max_container_depth: u32Containers nested in one input (a bundle entry that is itself a bundle, and so on): parsing one recurses. Default 16.
max_tree_depth: u32Depth of a compiled typetree. Default 256.
max_value_depth: u32Runtime nesting while reading or writing a value, ReferencedObject
data included: the frames each reader and the writer recurse through
(plan section 3.10, “Bounds”). UnityPyBoost’s are its C recursion,
which has no bound of its own; the pure-Python reader’s and the
writer’s are the Python frames UnityPy’s own pushes (read_value,
read_value_array, each comprehension and lambda), so every value
UnityPy refuses with RecursionError (Python’s 1,000 frames) passes
this first. What it costs in stack: Self::stack_needed. Default 512.
max_values_per_object: u64The work of one object read or write that neither its bytes nor
its memory bound: each child it skips (a ReferencedObject’s data
that does not resolve to a tree, a ManagedReferencesRegistry once
one is in scope), since a class can skip many children for each
element of an array; each value built or written under a child whose
name a sibling repeats, since such a tree hands one value to each
child of the name, which writes it whole, and a read keeps only the
last; each child of a class a write writes from a value that is not
a dict whose write writes no byte (the child’s value is an attribute
of the class’s, which neither the value’s memory nor the output
bounds); and every value and every element of a primitive run the
pure-Python reader reads, since a negative length lets it read the
same bytes again. A value UnityPyBoost’s reader keeps, a primitive
run being one, is charged to the meter, and a write writes it from
there, so neither counts (review round 9: a vector<UInt8> of 2^26 +
1 bytes was refused at this default; tests/walk_runs.rs). A write
through the tree its value was read with counts no more than its
read did unless the tree repeats a name, or a node whose type name
holds a NUL (read by UnityPyBoost as a primitive, a string or bytes,
written as a class) has a child that writes no byte
(tests/walk_bounds.rs). The work of every read and write together
is the open’s (Self::max_decode_work). Default 2^26.
max_zero_size_elements: u64Array elements, per object, whose read ended no further on than it
began and did work it keeps nothing of: skipped a child (a registry
while one is in scope, a ReferencedObject’s data that resolves to
no tree), built a value a sibling of the same name replaces, or, in
the pure-Python reader, took the position back (a negative
TypelessData length), so every element reads the same bytes. An
element that reads a byte is never counted, whatever its tree, nor is
one that keeps all it builds: an array of a class with no fields is
read at any count, each element a value charged to the meter (plan
section 9, Q15 (c); tests/walk_elements.rs). Default 2^20.
max_prealloc: usizeElements a read reserves before the bytes prove they exist, in all
the lists it holds open at once (a list nested in another, or in a
ReferencedObject re-entering its ref type, has what its outer
lists left); each list grows past its first reservation as it is
filled. Default 2^16.
Implementations§
Source§impl Limits
impl Limits
Sourcepub fn stack_needed(&self) -> usize
pub fn stack_needed(&self) -> usize
The stack a thread needs to read or write one object under these
limits, from the frame that calls the reader or the writer: they
recurse once per level they count against
Self::max_value_depth, and nothing else an object’s bytes drive
recurses deeper (a typetree compiles without recursion; a value is
dropped one level per reader level). It grows with max_value_depth,
and a debug build’s frames are several times a release build’s.
At the defaults: 1.75 MiB in a release build, 9 MiB in a debug one.
tests/walk_stack.rs reads, writes and drops the deepest object of
every shape on a thread this size, and its stack_measured prints
the least each needs: at most 1,261,568 bytes (1.2 MiB) in release
and 7,979,008 (7.6 MiB) in debug, with toolchain 1.99.1 (cargo test -p unityrs-core --test walk_stack [--release] -- --ignored --nocapture; review round 11: this said 0.90 MiB, measured before
fix pass 10; docs/plans/core.md section 6 records each integrated
head’s). Rust’s spawned threads get 2 MiB unless told
otherwise, and a main thread usually 8 MiB, so a debug build reads
untrusted input on a thread built with
std::thread::Builder::stack_size(limits.stack_needed()).