Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
can no longer build a `ColorSpace::RestrictedIcc`: match on it and read the
profile (#769).
- `Jp2Metadata::xml`, `uuid`, `ipr`, `uuid_info` and `color_specifications`,
and `ReaderRequirements::standard_features` and `vendor_features`, are a
`MetadataList`, not a `Vec`. A clone shares its items, so
`ReaderRequirements::standard_features` and `vendor_features`,
`FileType::compatibility` and `UuidInfo::ids` are a `MetadataList`, not a
`Vec`, and `Jp2Metadata::channel_definitions` is an
`Option<MetadataList<ChannelDefinitionEntry>>`. A clone shares its items, so
`IncrementalDecoder::header` reads only the boxes fed since the last call.
It is not a slice, so `&list[..]` and slice methods do not work: use
`.iter()`, `for item in &list`, or `for item in list` (a
Expand All @@ -94,7 +96,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
to a slice, `Vec` or array of the same items, either way round. It has no
public constructor but `Default`, an empty list. It cannot be changed in
place: assign `Default::default()` to empty it, or edit a `.to_vec()` (#769,
#780, #783).
#780, #783, #786).
- `IncrementalDecoder::header` leaves out of the metadata a last box of length
0 after the codestream of a type the metadata reports (`xml `, `uuid`,
`jp2i`, `uinf`, `rreq`), as more bytes can still change it. `finish`,
Expand Down Expand Up @@ -250,6 +252,30 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
which copied it in 4.9 ms, takes about 2 µs. Its entries hold at most
about 4.6 MiB, so the metadata holds up to about 3.2 bytes for each byte of
metadata boxes and about 4.6 MiB more for the first `rreq` (#783).
- The `ftyp`, `pclr`, `cmap` and `cdef` boxes and the `uinf` UUIDs are read
once too, and a later header shares them. Each was read twice by a read of
the file, once for the checks and once for the metadata, and copied by each
`header` call. A 4 MiB `ftyp` peaked at 2.0 bytes of heap for each byte of
it and now peaks at 1.16, what it holds; a 65535-entry `cdef` 3.4 and now
1.8; a 1024 x 255 `pclr` 8.2 and now 4.3. A `header` call with nothing new
fed copied each again, 4.9 ms and 8.4 MB of heap for the `ftyp`, and now
allocates about 1 KiB. It still checks the boxes before the codestream,
which takes 0.5 ms for the `ftyp` or the `cdef` and 24 µs for the `pclr`,
where it took 4.9, 1.0 and 1.6 ms. A palette holds 4 bytes
for each entry of a column, at most about 1 MiB, so the metadata holds up
to about 3.2 bytes for each byte of metadata boxes, about 4.6 MiB more for
the first `rreq` and about 1 MiB more for a palette (#786).
- While the codestream is fed, each `IncrementalDecoder::header` call shares
what the boxes before it built, where it built them again: on a file of
4.8 MB of header boxes with an 8 MiB tile-part fed 4 KiB at a time, a call
took 2.0 ms and allocated 6.5 MB, and 200 headers held 690 MB; now 0.34 ms
and 1.8 KB, and 2.5 MB. A call keeps no record of the boxes before the
codestream the metadata reports, and builds no name for a box it walks:
100000 empty `xml ` boxes there took 6.4 ms and 7.1 MB a call, and now take
0.89 ms and allocate nothing for them. Under `ColorOutput::Srgb`, the
decoder keeps what it read of an ICC profile and the transform it built,
so a later call builds neither again: it took 0.8 to 11 ms and 52 to 281
KB on the conformance files, and now 2 to 3 µs and about 2 KB (#786).
- `cargo test` on the unpacked package now runs the part of each mixed fixture
test that uses the files the package ships, and skips only the entries it
leaves out. A test that reads only left-out files still skips whole.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ has the value R, which is how eccodes and NCEP's g2clib read it.
Every failure comes back as an `Error`. With the default limits, decoding never
panics or hangs, whatever the input, and needs at most about 6.4 GiB of memory
for an input of up to 64 MiB, or about 102 bytes per byte of input past that,
plus up to about 3.2 bytes for each byte of JP2 metadata boxes and about 4.6
MiB more for the first `rreq`. If the machine
plus up to about 3.2 bytes for each byte of JP2 metadata boxes, about 4.6
MiB more for the first `rreq` and about 1 MiB more for a palette. If the machine
can't supply that, the allocation fails and the process aborts, as in any Rust
program. Those figures cover the one-shot decode functions;
`IncrementalDecoder` keeps per-tile state and can hold one decode's peak plus
Expand Down
27 changes: 18 additions & 9 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ the setters) return either a result or a typed `Error` for any input. They
never panic, read out of bounds or hang, and they allocate at most what the
limits allow. For the one-shot functions that is about 6.4 GiB at peak for an
input of up to 64 MiB, and about 102 bytes per byte of input past that, plus
up to about 3.2 bytes for each byte of JP2 metadata boxes and about 4.6 MiB
more for the first `rreq` (step 1 of [Running a decode
up to about 3.2 bytes for each byte of JP2 metadata boxes, about 4.6 MiB
more for the first `rreq` and about 1 MiB more for a palette (step 1 of [Running a decode
service](#running-a-decode-service)). An `IncrementalDecoder` holds
one decode's peak plus the packet walks it keeps, at most twice the image's
samples plus twice the walk of the largest tile walked so far, plus up to
Expand All @@ -68,14 +68,14 @@ docs](https://docs.rs/rust-j2k/latest/rust_j2k/fn.decode.html)). A file
that spends its bytes on code-block segments peaks past 43 with square
tiles too: a 96 MB file of one such tile after 39 tiles of samples peaked
at about 77 bytes per byte. About 102 is the bound for any file, plus up to
about 3.2 for each byte of JP2 metadata boxes and about 4.6 MiB more for
the first `rreq`. Padding counts as input, even in
about 3.2 for each byte of JP2 metadata boxes, about 4.6 MiB more for
the first `rreq` and about 1 MiB more for a palette. Padding counts as input, even in
a conformant file (`COM` segments, a JP2 `xml ` box, bytes after `EOC`), and it
compresses well: 67 MB of zeros gzips to 65 KB. Bound the input's size (step 1
of [Running a decode service](#running-a-decode-service)).
Past 64 MiB the worst-case peak is about 102 times that size, plus up to about
3.2 bytes for each byte of JP2 metadata boxes, about 4.6 MiB more for the first
`rreq`, and, for an `IncrementalDecoder`, one decode's peak plus the packet
`rreq`, about 1 MiB more for a palette, and, for an `IncrementalDecoder`, one decode's peak plus the packet
walks it keeps and the bytes fed (see below). A one-tile 1 x 2^30 image
followed by 128 MiB of zeros after `EOC` (134,283,241 bytes) decodes at the
defaults (`strict` on) in 23.5 s with a peak RSS of 8.30 GiB, measured. The
Expand Down Expand Up @@ -105,10 +105,10 @@ its calls, `header` also holds:
- once a call has walked the tile-parts for the decode's error, that walk's
heap: up to 14 bytes a tile of the grid, and the coding of each tile that
later tile-parts restate with headers that differ;
- in a JP2 file, the walks past the boxes before the codestream, 1424 bytes
- in a JP2 file, the walks past the boxes before the codestream, 2448 bytes
and up to four error messages. Of a box after the codestream it holds
nothing but, for a last box of length 0, a fixed record of where its read
stopped, 152 of those 1424 bytes, and, for a box the metadata reports, a
stopped, 152 of those 2448 bytes, and, for a box the metadata reports, a
24-byte record until the box is in the lists below;
- in a JP2 file, the boxes the metadata reports (`xml `, `uuid`, `jp2i`,
`uinf`, the first `rreq`), each read once and shared by every later
Expand All @@ -120,10 +120,18 @@ its calls, `header` also holds:
ones, and 2.4 for 24-byte ones fed one box a call. The first `rreq` holds
an entry of 32 or 40 bytes (24 or 36 on a 32-bit target) for each
feature, at most about 4.6 MiB more, read once and shared the same way;
- in a JP2 file, the `ftyp` codes (4 bytes each), the `cdef` entries (10
bytes each) and the palette (4 bytes for each entry of a column, at most
about 1 MiB), read once and shared the same way;
- in a JP2 file, the `colr` list (`jp2::Lists::colr`, a `MetadataList`): an
entry of 32 bytes (24 on a 32-bit target) for each `colr` box before the
codestream, so bounded by the header boxes there, with its payload held as
above.
above;
- in a JP2 file decoded under `ColorOutput::Srgb` with an ICC profile, what
the plan read of the profile and built from it, kept so that a later call
builds none of it again: its curves and a transform for each output
format, at most three, of 50 to 400 KB each, until a call plans another
profile.

The `IncrementalDecoder::header` rustdoc gives each of these in full (see also
"Work and memory" in
Expand Down Expand Up @@ -168,7 +176,8 @@ service](docs/resource-bounds.md#figures-for-a-decode-service).
through the decode and after it, measured (a `jp2c` box longer than its
codestream makes the boxes after it walked twice, which adds time but no
records). The first `rreq` keeps an entry for each of its features, up
to about 4.6 MiB more, which it also peaks at. A box before the
to about 4.6 MiB more, which it also peaks at, and a palette 4 bytes for
each entry of a column, up to about 1 MiB more. A box before the
codestream that is neither read nor reported holds nothing (see "JP2 box
records" in [`docs/resource-bounds.md`](docs/resource-bounds.md)).
2. **Set a fixed sample ceiling.** Set `DecodeOptions::sample_limit` to
Expand Down
24 changes: 19 additions & 5 deletions docs/incremental-decoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,18 @@ file (4.4 to 5.0 s before, when each call copied it), and a million 8-byte
(about 7 s before, when each call built the lists of every box so far). So a
call costs the main header (`PPM` data included), the boxes before the
codestream, and the child of a last `jp2h` of length 0 that the input ends
inside, plus the tile-parts, boxes and children fed since. While the bytes fed
inside, plus the tile-parts, boxes and children fed since. The lists as the
boxes before the codestream left them, with the `ftyp`, the palette and the
`cdef`, are kept apart (`jp2::Walks::before`) for as long as the `jp2c` box
starts at the same place, so the walk after the codestream, which starts
again on each call while the codestream's end is not yet fed, starts from
them and reads only the boxes after it. The walk of the boxes before the
codestream runs on each call to check them, about 9 ns a box at the top
level with nothing allocated for them, and a 24-byte record for each child
of a `jp2h` for the length of the call. Under `ColorOutput::Srgb` the
decoder also keeps what the plan read of an ICC profile and the transform it
built (`jp2::IccMemo`), at most three transforms of 50 to 400 KB, until a
call plans another profile. `docs/resource-bounds.md` gives the figures. While the bytes fed
end before the main header is whole, a call goes on from where the last one
stopped instead, in `pipeline::header::KeptCut`: after the last whole box
before a JP2 file's `jp2c`, and after the main header's last whole marker
Expand Down Expand Up @@ -448,7 +459,7 @@ finds where it ends is kept with it (`HeaderCache::end`), a few dozen bytes,
over a change of options. What it keeps of the tiles is for one selected area
and reduction, and a change of options drops it. The JP2 walks are kept from
the first call on a JP2 file, a change of options included, for as long as the
decoder lives: 1424 bytes (`size_of::<jp2::Walks>()`) and up to four error
decoder lives: 2448 bytes (`size_of::<jp2::Walks>()`) and up to four error
messages, plus the reported boxes in `jp2::Lists`. The walk lets go of the
record of a box once the lists hold it. Each box is an entry of 24 bytes (40
for a `uuid`, 56 for a `uinf`; on a 32-bit target 20, and 36 for both), a
Expand All @@ -457,14 +468,17 @@ allocation shared with the payloads read in the same call, behind a 16-byte
header (8 on a 32-bit target). The first `rreq` is an entry for each of its
features, 32 bytes for a standard one and 40 for a vendor one (24 and 36 on a
32-bit target), with its masks held as payloads are, so it keeps at most about
4.6 MiB more. So the lists keep at most about 3 bytes for each byte of the
4.6 MiB more. The lists also keep the `ftyp` codes (4 bytes each), the
`cdef` entries (10 bytes each) and the palette (4 bytes for each entry of a
column, at most about 1 MiB more), read once by the first call that finds the
`jp2c` box, as the boxes before it are then fixed. So the lists keep at most about 3 bytes for each byte of the
other boxes fed, whatever the pieces: a million 8-byte `xml ` boxes after the
codestream keep 25.8 MB, 3.1 bytes for each byte of them, and a million of 9
bytes 2.7, fed 4 KiB or one box at a time; a million 24-byte `xml ` boxes keep
1.7 fed 4 KiB at a time and 2.4 fed one box at a time (counted by a test
allocator over the bytes fed, release build, at a 1-minute load of about 9).
Each header returned shares the lists, and holds 48 bytes of its own for them.
A call with nothing new fed peaks at 692 bytes allocated, with 2^17 + 319
Each header returned shares the lists, and holds 44 bytes of its own for them.
A call with nothing new fed peaks at 688 bytes allocated, with 2^17 + 319
boxes of each type kept, so that every list has blocks and a tail
(`tests/incremental_header_list_alloc.rs`).

Expand Down
Loading
Loading