From a2133957594536b9f6bab870708d52473f7b93ec Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 02:35:39 +0000 Subject: [PATCH 01/13] Engine: fuzzing, and the faults it found in reading damaged CHDs libFuzzer under ASan and UBSan (tests/fuzz, scripts/fuzz.sh, weekly in .github/workflows/fuzz.yml), with the browser-only code built in. It found, and the engine fixes (all but one also in chdman 0.289): a Huffman tree's lookup table written past its end and left partly unset, a map size computed in 32 bits, maps longer than the file allocated first, map bit widths over 32, CD codec sector data and LaserDisc frames longer than their hunk, CD metadata read with %s into 16-byte buffers or missing a track, old-style CD metadata read past its end, a 0-byte read that read a whole hunk, hunks naming a codec or a parent the CHD doesn't have, and undefined behavior with unchanged results. Also: a header with a hunk or unit size of 0, or more hunks than a map counts, is an error. Default CHDs are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QUSh6AFtA1CAR66DNvjtty --- .github/workflows/fuzz.yml | 69 ++ engine/README.md | 16 +- engine/mame-0.289.diff | 673 +++++++++++++++--- .../flac/src/libFLAC/stream_encoder.c | 3 +- engine/mame/src/lib/util/avhuff.cpp | 10 + engine/mame/src/lib/util/bitstream.h | 5 + engine/mame/src/lib/util/cdrom.cpp | 31 +- engine/mame/src/lib/util/chd.cpp | 126 +++- engine/mame/src/lib/util/chd.h | 2 + engine/mame/src/lib/util/chdcodec.cpp | 13 + engine/mame/src/lib/util/huffman.cpp | 11 + scripts/fuzz.sh | 157 ++++ tests/fuzz/README.md | 97 +++ tests/fuzz/fuzz_chd.cpp | 137 ++++ tests/fuzz/fuzz_codec.cpp | 39 + tests/fuzz/fuzz_desc.cpp | 99 +++ tests/fuzz/fuzz_ecm.cpp | 35 + tests/fuzz/host.cpp | 188 +++++ tests/fuzz/host.h | 29 + tests/fuzz/regressions/chd/codec-missing | Bin 0 -> 13088 bytes .../regressions/chd/huffman-uncovered-codes | Bin 0 -> 20945 bytes tests/fuzz/regressions/chd/map-bits-over-32 | Bin 0 -> 1594 bytes tests/fuzz/regressions/chd/map-length-0 | Bin 0 -> 48115 bytes tests/fuzz/regressions/chd/map-past-end | Bin 0 -> 5594 bytes .../fuzz/regressions/chd/metadata-long-string | Bin 0 -> 5705 bytes tests/fuzz/regressions/chd/parent-missing | Bin 0 -> 2742 bytes tests/fuzz/regressions/chd/read-0-bytes | Bin 0 -> 5504 bytes .../codec/avhuff-frame-larger-than-hunk | Bin 0 -> 116 bytes .../codec/cd-base-longer-than-hunk | Bin 0 -> 31 bytes .../codec/huffman-one-bit-overfull | 1 + tests/fuzz/seeds.cpp | 372 ++++++++++ tests/fuzz/shim/emscripten.h | 5 + tests/support/chdcraft.js | 16 +- tests/ui/engine.spec.js | 123 ++++ 34 files changed, 2130 insertions(+), 127 deletions(-) create mode 100644 .github/workflows/fuzz.yml create mode 100755 scripts/fuzz.sh create mode 100644 tests/fuzz/README.md create mode 100644 tests/fuzz/fuzz_chd.cpp create mode 100644 tests/fuzz/fuzz_codec.cpp create mode 100644 tests/fuzz/fuzz_desc.cpp create mode 100644 tests/fuzz/fuzz_ecm.cpp create mode 100644 tests/fuzz/host.cpp create mode 100644 tests/fuzz/host.h create mode 100644 tests/fuzz/regressions/chd/codec-missing create mode 100644 tests/fuzz/regressions/chd/huffman-uncovered-codes create mode 100644 tests/fuzz/regressions/chd/map-bits-over-32 create mode 100644 tests/fuzz/regressions/chd/map-length-0 create mode 100644 tests/fuzz/regressions/chd/map-past-end create mode 100644 tests/fuzz/regressions/chd/metadata-long-string create mode 100644 tests/fuzz/regressions/chd/parent-missing create mode 100644 tests/fuzz/regressions/chd/read-0-bytes create mode 100644 tests/fuzz/regressions/codec/avhuff-frame-larger-than-hunk create mode 100644 tests/fuzz/regressions/codec/cd-base-longer-than-hunk create mode 100644 tests/fuzz/regressions/codec/huffman-one-bit-overfull create mode 100644 tests/fuzz/seeds.cpp create mode 100644 tests/fuzz/shim/emscripten.h diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml new file mode 100644 index 0000000..03aa805 --- /dev/null +++ b/.github/workflows/fuzz.yml @@ -0,0 +1,69 @@ +# Fuzzes the engine (scripts/fuzz.sh, tests/fuzz/README.md): every seed, past finding and saved corpus +# input once, then each fuzzer for a while: 30 minutes weekly, as long as asked on demand, 5 minutes when +# engine/, wasm/ or the fuzzers change. The corpus grows from run to run in the Actions cache (pruned to +# the inputs that add coverage); a finding fails the run and is uploaded with it. +name: Fuzz + +on: + push: + branches: [main] + paths: ['engine/**', 'wasm/**', 'tests/fuzz/**', 'tests/support/lsan.supp', 'scripts/fuzz.sh', '.github/workflows/fuzz.yml'] + pull_request: + paths: ['engine/**', 'wasm/**', 'tests/fuzz/**', 'tests/support/lsan.supp', 'scripts/fuzz.sh', '.github/workflows/fuzz.yml'] + schedule: + - cron: '23 4 * * 3' + workflow_dispatch: + inputs: + seconds: + description: Seconds each fuzzer runs (the four run at once) + default: '1800' + +permissions: + contents: read + +concurrency: + group: fuzz-${{ github.ref }} + cancel-in-progress: true + +jobs: + fuzz: + runs-on: ubuntu-24.04 + timeout-minutes: 120 + env: + CC: clang-18 + CXX: clang++-18 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - name: clang and its libFuzzer runtime + run: sudo apt-get update && sudo apt-get install -y --no-install-recommends clang-18 libclang-rt-18-dev + - name: The fuzzers and their seeds + run: ./scripts/fuzz.sh build + - name: Restore the corpus + uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: build/fuzz/corpus + key: fuzz-corpus-${{ github.run_id }} + restore-keys: fuzz-corpus- + - name: Every seed, regression and corpus input once + run: ./scripts/fuzz.sh check all + - name: Fuzz + run: ./scripts/fuzz.sh run all ${{ github.event_name == 'schedule' && '1800' || (github.event_name == 'workflow_dispatch' && inputs.seconds) || '300' }} + - name: Prune the corpus + if: success() + run: ./scripts/fuzz.sh prune all + # (a pull request's cache is its own: only main's runs keep the corpus) + - name: Save the corpus + if: always() && github.event_name != 'pull_request' + uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: build/fuzz/corpus + key: fuzz-corpus-${{ github.run_id }} + - name: Findings + if: failure() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: fuzz-findings + path: | + build/fuzz/findings + build/fuzz/*.log + if-no-files-found: ignore diff --git a/engine/README.md b/engine/README.md index 92167bb..1e6941a 100644 --- a/engine/README.md +++ b/engine/README.md @@ -27,7 +27,7 @@ The paths mirror MAME's, so every file can be compared with its original. **[`ma - **Keeping the cue sheet (`createcd --keepcue`, `-kc`; `chdman.cpp`), opt-in:** the input `.cue` is stored as it is, as metadata (`CUES`) after the CD metadata and without the checksum flag, so neither SHA-1 changes and most readers ignore it; Kronos and Yabause (Saturn) and jgenesis (Sega CD) reject a CHD with any tag they don't know, so the page leaves `--keepcue` out for those consoles, and chdman 0.289 reads, verifies and extracts such CHDs as usual. `extractcd --redump` writes that sheet back, with the names of the files it writes, when those are laid out as the sheet's files (one `BINARY` file per track, or one for a single track); otherwise it writes its own. So what a CHD's tracks don't hold comes back: CATALOG, FLAGS, ISRC, INDEX 02 and later, `CDI/2352`. The CHD's bytes then differ from 0.289's (the plan's tier C), which is why it is off by default, in the page too. - **The codec plan (`createcd`, `createdvd`, `createhd`, `createraw` with `--codecplan`, `-cp`; `chd.cpp`, `chd.h`, `chdcodec.cpp`, `chdman.cpp`), opt-in:** chdman tries every codec in the list on every hunk and keeps the smallest result, though most of them hardly ever win. With the plan, each hunk tries only the codecs meant for what it holds: a CD hunk in audio tracks `cdfl`, one in data tracks `cdlz` (`cdzl` wins about one data hunk in ten, by a few bytes), and a hunk of any other CHD every codec but `flac`. A CD hunk that holds both, or a codec list without those codecs, tries them all. The codec list in the header is unchanged, so every reader decodes the result, and the data and both SHA-1s are the same; the bytes differ where a codec left out would have won (the plan's tier C), so it is off by default. The choice depends only on where a hunk lies, so the CHD is the same at any thread count, and in the page (the codec slots travel with each hunk to the helper workers). Natively, `-np 4`, against the default: CD data 1.8 times less CPU and 0.22% larger, CD audio 1.9 times less and the same size, a mixed CD 2.0 times less and 0.11% larger, a DVD 1.4 times less and 0.06% larger (`docs/chd/measurements.md`). - **libdeflate for the deflate codec (`--libdeflate`, `-ld` on the same four commands; `chd.h`, `chdcodec.cpp`, `chdman.cpp`), opt-in:** `chd_zlib_compressor`, which does `zlib`, `cdzl` and the subcode of `cdlz`, encodes with [libdeflate](libdeflate/README.md) at its level 9 instead of zlib's level 9: standard raw deflate that every reader decodes with zlib, so the checksums are the same, but not zlib's bytes (tier C). Natively, `-np 4`, CPU: with the default codecs 1.1–1.5 times less and a little smaller; with `-c cdzl,cdfl` (the page's "Faster to create") 1.5–1.8 times less and 0.3–0.7% smaller; with the codec plan on a DVD 1.17 times less than the plan alone, and smaller than chdman's default. Helper workers are told with the codecs (`wasm_par_enabled`'s flags). -- **Fixes to 0.289's failure handling and to the descriptors it reads wrong (`chdman.cpp`, `cdrom.cpp`, `cdrom.h`, `chd.cpp`, `chdcodec.cpp`, libFLAC's `lpc.c` and `fixed.c`, LZMA SDK `LzmaEnc.c`); output for input that 0.289 reads right is unchanged, except the `.toc` that `extractcd` writes for a CD with a pregap it holds, and the GD-ROM outputs below that 0.289 can't read back:** +- **Fixes to 0.289's failure handling and to the descriptors it reads wrong (`chdman.cpp`, `cdrom.cpp`, `cdrom.h`, `chd.cpp`, `chdcodec.cpp`, `huffman.cpp`, `avhuff.cpp`, `bitstream.h`, libFLAC's `lpc.c`, `fixed.c` and `stream_encoder.c`, LZMA SDK `LzmaEnc.c`); output for input that 0.289 reads right is unchanged, except the `.toc` that `extractcd` writes for a CD with a pregap it holds, and the GD-ROM outputs below that 0.289 can't read back:** - `verify` exits 1 when the data doesn't match the header's SHA-1, unless `--fix` corrects it. Upstream exits 0, so scripts and the page reported success. - An output is never written over an input: `copy -i x -o x -f` used to truncate `x` and then delete it. - `extractcd` of a CHD that isn't a CD, and a unit or sector size of 0, are errors (exit 1). Upstream aborts on `throw nullptr` or divides by zero. @@ -41,7 +41,7 @@ The paths mirror MAME's, so every file can be compared with its original. **[`ma - Cue sheets (`parse_cue`): a track's `INDEX` points must be in order and not go back in the same file, a track in the same file as the next must end before the next starts, and every track must have frames from its `INDEX 01` on. Upstream made negative pregaps or lengths of them (exit 0, or a read error), and tracks of their pregap alone, whose `.toc` doesn't read back. - `cdrom_file`'s logical reads (`read_data` and `read_subcode` without `phys`, `get_track`), which no chdman command uses but the page's identification does (`wasm_probe_read`): a track's LBAs start at its `INDEX 00`, so a pregap the CHD holds is read from the track's own data, and a pregap or postgap it doesn't hold (or a gap between sessions of a mounted image) reads as zeros. Upstream counted a track's pregap as the track before's, so it read that track's padding and then data shifted by it, and for a gap the CHD doesn't hold the next track's data. LBAs from `INDEX 01` on, which is all the page reads, are read as before. `tests/support/cdprobe.cpp` (`make -C wasm T=native cdprobe`) reads every LBA as the page does. - libFLAC's LPC and fixed-predictor restores (`lpc.c`, `FLAC__lpc_restore_signal`; `fixed.c`, `FLAC__fixed_restore_signal`) compute in unsigned 32-bit arithmetic: on damaged `cdfl` data its signed sums and products overflowed, which C leaves undefined (UBSan flags it). The build wrapped them as two's complement, and unsigned arithmetic gives the same bits, so decoding is unchanged (each checked against the old function, built with `-fwrapv`, on 200,000 random blocks with gcc and clang); the encoder never calls them. - - Undefined behavior UBSan reports, with the same results: the LZMA encoder stored its properties' dictionary size (`LzmaEnc_WriteProperties`, `LzmaEnc.c`) through a `UInt32` pointer at an odd address, now with `memcpy`; the zlib and LZMA allocators (`chdcodec.cpp`) kept each block's size in 4 bytes in front of it, so every block, the codecs' state structures among them, was only 4-byte aligned, and now keep it in 16 (`alignof(std::max_align_t)`); `msf_to_frames` (`cdrom.cpp`) overflowed an `int` on huge minutes, and now clamps (the same results for any time that fits). + - Undefined behavior UBSan reports, with the same results: the LZMA encoder stored its properties' dictionary size (`LzmaEnc_WriteProperties`, `LzmaEnc.c`) through a `UInt32` pointer at an odd address, now with `memcpy`; the zlib and LZMA allocators (`chdcodec.cpp`) kept each block's size in 4 bytes in front of it, so every block, the codecs' state structures among them, was only 4-byte aligned, and now keep it in 16 (`alignof(std::max_align_t)`); `msf_to_frames` (`cdrom.cpp`) overflowed an `int` on huge minutes, and now clamps (the same results for any time that fits); the bit writer (`bitstream.h`) shifted a 32-bit value by 32 when writing no bits (as for a map without copies of hunks), and now returns, as it did after the shift; libFLAC's encoder (`stream_encoder.c`) added an offset to a null pointer when it doesn't use escape codes, and now passes the null pointer; a map or metadata of 0 bytes took element 0 of an empty vector (`chd.cpp`), now `data()`. - Memory that LeakSanitizer reports: the command line's option values (`chdman_run`), `extractcd`'s `cdrom_file`, and after an error, the compression work items still out (`~chd_file_compressor`). Other allocations on the error paths are freed as `report_error` unwinds, and descriptor parsers that return early without closing their file (upstream's) are left as they are: each runs once per command. - `cdrom_file::get_track_index` (not used by chdman or the page) gives 0 for a frame in a track's pregap: since `logical_to_chd_lba` gives the pregap to its own track (above), counting back from `INDEX 01` there wrapped. - An `.iso` of 2352-byte sectors (`parse_iso`) is typed by its first two sectors' sync pattern and mode byte as well as its size: upstream typed it by size alone, so an image of 128·n sectors (also a multiple of 2048 bytes) came out as 2048-byte `MODE1` sectors, and one of 146·n (a multiple of 2336) as 2336-byte `MODE2`. Such an image now gives `MODE2_RAW`, as upstream types every other image of 2352-byte sectors; images of 2048- and 2336-byte sectors are upstream's. @@ -52,11 +52,21 @@ The paths mirror MAME's, so every file can be compared with its original. **[`ma - `extractcd` of a CHD with a damaged hunk is an error, as with the other extract commands. Upstream writes whatever it last read and exits 0. - A FLAC stream that ends before its hunk is complete (damaged `flac` or `cdfl` data) is a decompression error: upstream's decoder asks for more forever (`flac.cpp`). - A hunk stored as a copy of another hunk of the same file is followed in a loop, to the hunk that holds the data, and a chain that leads back to itself is an error (`self_target`, `chd.cpp`). Upstream calls `read_hunk` again for each step, so a map that makes a hunk a copy of itself, as a damaged or crafted file can, overflows the stack. Copies of copies that end are read as before. - - A hunk whose stored length is larger than a hunk is an error (`read_hunk`, `codec_process_hunk`): upstream reads that many bytes into its hunk-sized buffer. + - A hunk whose stored length is larger than a hunk is an error (`read_hunk`, `codec_process_hunk`): upstream reads that many bytes into its hunk-sized buffer. So is a hunk compressed with a codec the CHD has none for (a v3/v4 CHD without compression whose map says a hunk is compressed, a v5 map naming an empty codec slot), and a hunk in a parent in a CHD that has none: upstream calls through a null pointer. - A CHD of a hunk or three can be read: upstream sizes the map's buffer without the Huffman tree that opens it, so the map overflows and is cut short, and the CHD it made (exit 0) fails with "Decompression error" (`compress_v5_map`, `chd.cpp`). Where upstream's map fits, which is every CHD of more than a few hunks, the bytes are the same. - The CD codecs compress the subcode into what is left of the hunk-sized buffer; upstream lets it run past the end when the sectors hardly compress (a heap overflow). Such a result is too large to win either way, so the CHD is the same (`chdcodec.cpp`). - `dumpmeta` without `-o` prints its banner on stderr, so stdout carries only the metadata. + - A header with a hunk or unit size of 0, or a logical size of more hunks than a map can count (2^32), is an error ("Invalid file"), and so is a map whose size in memory doesn't fit in 32 bits (`check_sizes`, `create_open_common`, `chd.cpp`). Upstream divides by zero, or computes the size in 32 bits and makes a map too small for its hunks, then reads and writes past its end (a segfault at 358 million hunks). + - A map that would run past the end of the file is the error reading it gives ("Input/output error") before its buffer is allocated: upstream allocated what the header said, up to 4 GB, first (`past_end`, `chd.cpp`). + - The Huffman lookup table's entries that no code covers (only a damaged tree leaves any) decode as code 0 of no bits (`build_lookup_table`, `huffman.cpp`): upstream never set them, so decoding took bit counts from whatever that memory held. + - A Huffman tree with more codes than one bit has room for (a damaged `huff` hunk, LaserDisc audio, or a v5 map's tree) is a decompression error (`assign_canonical_codes`, `huffman.cpp`): upstream gave codes more bits than their length and wrote the lookup table past its end, on the stack for a map or a `huff` hunk ("stack smashing detected"). + - A CD codec's hunk (`cdlz`, `cdzl`, `cdzs`) whose header is longer than the hunk's data, or says its sector data is, is a decompression error (`chdcodec.cpp`): upstream decoded past the end of the data, and the subcode's length wrapped around. So is a v5 map that reads more than 32 bits at a time (`decompress_v5_map`): upstream's bit reader shifted by a negative count. + - CD track metadata is read with a width for each string (`%15s`, the longest a CD has being 15 characters): upstream's `%s` wrote a longer one past the end of its 16-byte buffer, on the stack (`parse_metadata`, `cdrom.cpp`). + - CD metadata that leaves out a track (a track number repeated or skipped), and old-style binary CD metadata (`CHCD`) shorter than its table, with more than 99 tracks or with sectors no CD has, are invalid data (`parse_metadata`, `cdrom.cpp`): upstream made a track of 0-byte sectors, which `extractcd` divided by, or read past the end of the metadata and of a sector's buffer. + - Reading or writing 0 bytes (`read_bytes`, `write_bytes`) does nothing: upstream's last hunk wrapped around at offset 0, and it read a whole hunk into the caller's buffer. The page's identification reads a CD track that has no metadata (a damaged CHD's) that way. + - LaserDisc (`avhu`) hunks: a frame larger than the hunk, more than 16 audio channels, or raw audio shorter than its samples is a decompression error (`chdcodec.cpp`, `avhuff.cpp`). Upstream decoded past the end of the hunk, of its tables of channels, and of the data. - Any other unexpected exception is an error instead of a crash. + - `tests/fuzz/` fuzzes the engine (libFuzzer, ASan and UBSan; `scripts/fuzz.sh`, `.github/workflows/fuzz.yml`), and found the faults above in maps, Huffman trees and the CD and LaserDisc codecs (the map size in 32 bits while looking at one), and the undefined behavior below. - `tests/ui/engine.spec.js` compares each of these with upstream. ## Contents diff --git a/engine/mame-0.289.diff b/engine/mame-0.289.diff index 05e76b8..d910f70 100644 --- a/engine/mame-0.289.diff +++ b/engine/mame-0.289.diff @@ -467,6 +467,18 @@ if(bps >= 0.0) return bps; else +--- a/3rdparty/flac/src/libFLAC/stream_encoder.c ++++ b/3rdparty/flac/src/libFLAC/stream_encoder.c +@@ -4126,7 +4126,8 @@ + residual, + #endif + abs_residual_partition_sums+sum, +- raw_bits_per_partition+sum, ++ /* Discpress: null without escape coding, which C doesn't let an offset be added to */ ++ raw_bits_per_partition != NULL ? raw_bits_per_partition+sum : NULL, + residual_samples, + predictor_order, + rice_parameter_limit, --- a/3rdparty/flac/src/libFLAC/window.c +++ b/3rdparty/flac/src/libFLAC/window.c @@ -39,6 +39,7 @@ @@ -657,6 +669,53 @@ *destLen -= (SizeT)outStream.rem; if (outStream.overflow) +--- a/src/lib/util/avhuff.cpp ++++ b/src/lib/util/avhuff.cpp +@@ -67,6 +67,7 @@ + #include + #include + #include ++#include + #include + + +@@ -723,6 +724,11 @@ + uint32_t width = get_u16be(&source[4]); + uint32_t height = get_u16be(&source[6]); + ++ // Discpress: no more channels than there are places for (the encoder takes up to 16): 0.289 went past ++ // the end of audiostart and of m_config.audio ++ if (channels > std::size(m_config.audio)) ++ return AVHERR_INVALID_DATA; ++ + // validate that the sizes make sense + if (complength < 10 + 2 * channels) + return AVHERR_INVALID_DATA; +@@ -932,6 +938,10 @@ + // if no huffman length, just copy the data + if (treesize == 0) + { ++ // Discpress: as much as the samples take (as stored): 0.289 read past the channel's ++ // data, and past the end of the hunk's ++ if (size < 2 * samples) ++ return AVHERR_INVALID_DATA; + const uint8_t *cursource = source; + for (int sampnum = 0; sampnum < samples; sampnum++) + { +--- a/src/lib/util/bitstream.h ++++ b/src/lib/util/bitstream.h +@@ -226,6 +226,11 @@ + + inline void bitstream_out::write(uint32_t newbits, int numbits) + { ++ // Discpress: nothing to write, as for a CHD map with no copies of hunks (0.289 went on to shift ++ // the value by 32 bits, which C++ leaves undefined, and then returned without using it) ++ if (numbits <= 0) ++ return; ++ + newbits <<= 32 - numbits; + + // flush the buffer if we're going to overflow it --- a/src/lib/util/cdrom.cpp +++ b/src/lib/util/cdrom.cpp @@ -28,6 +28,8 @@ @@ -722,7 +781,85 @@ const uint32_t index_offset = frame - track_start; int index = 0; -@@ -1153,204 +1177,106 @@ +@@ -897,6 +921,13 @@ + * @return A std::error_condition. + */ + ++// Discpress: chd.cpp's formats for the track metadata, with a width for each string that fills its 16-byte ++// buffer at most (the longest a CD has is 15 characters, VMODE2_FORM_MIX); a longer one, in damaged or ++// crafted metadata, doesn't match. 0.289 read them with %s, which wrote past the end of the buffer. ++static const char CDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d"; ++static const char CDROM_TRACK_METADATA2_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; ++static const char GDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; ++ + std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) + { + std::string metadata; +@@ -924,12 +955,12 @@ + // fetch the metadata for this track + if (!chd->read_metadata(CDROM_TRACK_METADATA_TAG, toc.numtrks, metadata)) + { +- if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames) != 4) ++ if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames) != 4) + return chd_file::error::INVALID_DATA; + } + else if (!chd->read_metadata(CDROM_TRACK_METADATA2_TAG, toc.numtrks, metadata)) + { +- if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) ++ if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT_READ, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) + return chd_file::error::INVALID_DATA; + } + else +@@ -944,7 +975,7 @@ + if (err) + break; + +- if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) ++ if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) + return chd_file::error::INVALID_DATA; + + toc.flags |= CD_FLAG_GDROM; +@@ -995,7 +1026,14 @@ + + /* if we got any tracks this way, we're done */ + if (toc.numtrks > 0) ++ { ++ // Discpress: with each track once: a track number that repeats or skips one (damaged or crafted ++ // metadata) left a track of nothing, of 0-byte sectors, which extractcd divided by ++ for (uint32_t i = 0; i < toc.numtrks; i++) ++ if (toc.tracks[i].datasize == 0) ++ return chd_file::error::INVALID_DATA; + return std::error_condition(); ++ } + + osd_printf_info("toc.numtrks = %u?!\n", toc.numtrks); + +@@ -1004,6 +1042,9 @@ + err = chd->read_metadata(CDROM_OLD_METADATA_TAG, 0, oldmetadata); + if (err) + return err; ++ // Discpress: as long as the table it holds (0.289 read past the end of shorter metadata) ++ if (oldmetadata.size() < 4 + MAX_TRACKS * 6 * 4) ++ return chd_file::error::INVALID_DATA; + + /* reconstruct the TOC from it */ + auto *mrp = reinterpret_cast(&oldmetadata[0]); +@@ -1044,6 +1085,14 @@ + } + } + ++ // Discpress: no more tracks than the table holds, with sectors a CD has: anything else (damaged or ++ // crafted metadata) made 0.289 read past the end of the table, and of a sector's buffer ++ if (toc.numtrks > MAX_TRACKS) ++ return chd_file::error::INVALID_DATA; ++ for (uint32_t i = 0; i < toc.numtrks; i++) ++ if (toc.tracks[i].datasize == 0 || toc.tracks[i].datasize > MAX_SECTOR_DATA || toc.tracks[i].subsize > MAX_SUBCODE_DATA) ++ return chd_file::error::INVALID_DATA; ++ + return std::error_condition(); + } + +@@ -1153,204 +1202,106 @@ 0x50, 0xa4, 0xa5, 0x51, 0xa7, 0x53, 0x52, 0xa6, 0xa3, 0x57, 0x56, 0xa2, 0x54, 0xa0, 0xa1, 0x55 }; @@ -962,10 +1099,7 @@ + uint64_t a[11] = { }, b[11] = { }; + int const count = q ? ECC_Q_NUM_BYTES : ECC_P_NUM_BYTES; + if (!q) - { -- val1 ^= ecc_source_byte(sector, row[component]); -- val2 ^= ecc_source_byte(sector, row[component]); -- val1 = ecclow[val1]; ++ { + // the columns of the first 24 rows (a row's last word also reads 2 bytes of the next row, unused) + for (int row = 0; row < ECC_P_COMP; row++) + for (int l = 0; l < 11; l++) @@ -1000,7 +1134,10 @@ + memcpy(av, a, sizeof(a)); + memcpy(bv, b, sizeof(b)); + for (int i = 0; i < count; i++) -+ { + { +- val1 ^= ecc_source_byte(sector, row[component]); +- val2 ^= ecc_source_byte(sector, row[component]); +- val1 = ecclow[val1]; + uint8_t const p0 = ecchigh[ecclow[av[i]] ^ bv[i]]; + parity[i] = p0; + parity[count + i] = p0 ^ bv[i]; @@ -1010,7 +1147,7 @@ } /** -@@ -1367,24 +1293,17 @@ +@@ -1367,24 +1318,17 @@ bool cdrom_file::ecc_verify(const uint8_t *sector) { @@ -1043,7 +1180,7 @@ } /** -@@ -1400,13 +1319,13 @@ +@@ -1400,13 +1344,13 @@ void cdrom_file::ecc_generate(uint8_t *sector) { @@ -1063,7 +1200,7 @@ } /** -@@ -1578,22 +1497,25 @@ +@@ -1578,22 +1522,25 @@ int cdrom_file::msf_to_frames(const char *token) { @@ -1103,7 +1240,7 @@ } /*------------------------------------------------- -@@ -1604,17 +1526,18 @@ +@@ -1604,17 +1551,18 @@ -------------------------------------------------*/ /** @@ -1124,7 +1261,7 @@ { unsigned long offset = 0; uint32_t length, rate, filesize; -@@ -1633,7 +1556,7 @@ +@@ -1633,7 +1581,7 @@ } /* read the core header and make sure it's a WAVE file */ @@ -1133,7 +1270,7 @@ offset += actual; if (offset < 4) { -@@ -1647,7 +1570,7 @@ +@@ -1647,7 +1595,7 @@ } /* get the total size */ @@ -1142,7 +1279,7 @@ offset += actual; if (offset < 8) { -@@ -1657,7 +1580,7 @@ +@@ -1657,7 +1605,7 @@ filesize = little_endianize_int32(filesize); /* read the RIFF file type and make sure it's a WAVE file */ @@ -1151,7 +1288,7 @@ offset += actual; if (offset < 12) { -@@ -1673,9 +1596,9 @@ +@@ -1673,9 +1621,9 @@ /* seek until we find a format tag */ while (true) { @@ -1163,7 +1300,7 @@ offset += actual; length = little_endianize_int32(length); if (memcmp(&buf[0], "fmt ", 4) == 0) -@@ -1691,7 +1614,7 @@ +@@ -1691,7 +1639,7 @@ } /* read the format -- make sure it is PCM */ @@ -1172,7 +1309,7 @@ offset += actual; temp16 = little_endianize_int16(temp16); if (temp16 != 1) -@@ -1701,7 +1624,7 @@ +@@ -1701,7 +1649,7 @@ } /* number of channels -- only stereo is supported */ @@ -1181,7 +1318,7 @@ offset += actual; temp16 = little_endianize_int16(temp16); if (temp16 != 2) -@@ -1711,7 +1634,7 @@ +@@ -1711,7 +1659,7 @@ } /* sample rate */ @@ -1190,7 +1327,7 @@ offset += actual; rate = little_endianize_int32(rate); if (rate != 44100) -@@ -1721,11 +1644,11 @@ +@@ -1721,11 +1669,11 @@ } /* bytes/second and block alignment are ignored */ @@ -1204,7 +1341,7 @@ offset += actual; bits = little_endianize_int16(bits); if (bits != 16) -@@ -1740,9 +1663,9 @@ +@@ -1740,9 +1688,9 @@ /* seek until we find a data tag */ while (true) { @@ -1216,7 +1353,7 @@ offset += actual; length = little_endianize_int32(length); if (memcmp(&buf[0], "data", 4) == 0) -@@ -1764,7 +1687,12 @@ +@@ -1764,7 +1712,12 @@ return 0; } @@ -1230,7 +1367,7 @@ return length; } -@@ -1864,11 +1792,11 @@ +@@ -1864,11 +1817,11 @@ outtoc.numsessions = 1; @@ -1246,7 +1383,7 @@ { osd_printf_error("ERROR: Not a Nero 5.5 or later image!\n"); fclose(infile); -@@ -1888,8 +1816,13 @@ +@@ -1888,8 +1841,13 @@ while (!done) { @@ -1262,7 +1399,7 @@ chunk_size = get_u32be(&buffer[4]); -@@ -1901,15 +1834,20 @@ +@@ -1901,15 +1859,20 @@ // skip second chunk size and UPC code fseek(infile, 20, SEEK_CUR); @@ -1285,7 +1422,7 @@ for (int track = start; track <= end; track++) { uint32_t size, mode; -@@ -1925,7 +1863,15 @@ +@@ -1925,7 +1888,15 @@ // printf("Track %d: sector size %d mode %x index0 %llx index1 %llx track_end %llx (pregap %d sectors, length %d sectors)\n", track, size, mode, index0, index1, track_end, (uint32_t)(index1-index0)/size, (uint32_t)(track_end-index1)/size); outinfo.track[track-1].fname.assign(tocfname); @@ -1302,7 +1439,7 @@ outinfo.track[track-1].idx[0] = outinfo.track[track-1].idx[1] = 0; switch (mode) -@@ -1976,21 +1922,29 @@ +@@ -1976,21 +1947,29 @@ return chd_file::error::UNSUPPORTED_FORMAT; } @@ -1337,7 +1474,7 @@ } } -@@ -2000,7 +1954,15 @@ +@@ -2000,7 +1979,15 @@ } else { @@ -1354,7 +1491,7 @@ } } -@@ -2042,6 +2004,20 @@ +@@ -2042,6 +2029,20 @@ outinfo.reset(); uint64_t size = get_file_size(tocfname); @@ -1375,7 +1512,7 @@ fclose(infile); -@@ -2052,7 +2028,15 @@ +@@ -2052,7 +2053,15 @@ outinfo.track[0].offset = 0; outinfo.track[0].idx[0] = outinfo.track[0].idx[1] = 0; @@ -1392,7 +1529,7 @@ { outtoc.tracks[0].trktype = CD_TRACK_MODE1; outtoc.tracks[0].frames = size / 2048; -@@ -2149,7 +2133,7 @@ +@@ -2149,7 +2158,7 @@ const int numtracks = atoi(token); @@ -1401,7 +1538,7 @@ { osd_printf_error("GDI expects too many tracks. Expected %d tracks but only up to %zu tracks allowed\n", numtracks, std::size(outinfo.track) + 1); return chd_file::error::INVALID_DATA; -@@ -2178,7 +2162,7 @@ +@@ -2178,7 +2187,7 @@ paramcnt++; const int trknum = atoi(token) - 1; @@ -1410,7 +1547,7 @@ { osd_printf_error("Track %d is out of expected range of 1 to %d\n", trknum + 1, numtracks); return chd_file::error::INVALID_DATA; -@@ -2222,7 +2206,7 @@ +@@ -2222,7 +2231,7 @@ outtoc.tracks[trknum].trktype = CD_TRACK_MODE1; outtoc.tracks[trknum].datasize = 2048; } @@ -1419,7 +1556,7 @@ { outtoc.tracks[trknum].trktype = CD_TRACK_AUDIO; outtoc.tracks[trknum].datasize = 2352; -@@ -2252,6 +2236,12 @@ +@@ -2252,6 +2261,12 @@ outinfo.track[trknum].fname.assign(path).append(token); const uint64_t sz = get_file_size(outinfo.track[trknum].fname); @@ -1432,7 +1569,7 @@ outtoc.tracks[trknum].frames = sz / trksize; outtoc.tracks[trknum].padframes = 0; -@@ -2345,6 +2335,19 @@ +@@ -2345,6 +2360,19 @@ enum gdi_area current_area = SINGLE_DENSITY; bool is_multibin = false; int leadin = -1; @@ -1452,7 +1589,7 @@ FILE *infile = fopen(path.c_str(), "rt"); if (!infile) -@@ -2371,6 +2374,14 @@ +@@ -2371,6 +2399,14 @@ char linebuffer[512]; memset(linebuffer, 0, sizeof(linebuffer)); @@ -1467,7 +1604,7 @@ while (!feof(infile)) { /* get the next line */ -@@ -2427,6 +2438,8 @@ +@@ -2427,6 +2463,8 @@ /* get lead-out time */ TOKENIZE int leadout_offset = msf_to_frames(token); @@ -1476,7 +1613,7 @@ outinfo.track[trknum].leadout = leadout_offset; } else if (!strncmp(linebuffer+i, "LEAD-IN", 7)) -@@ -2472,13 +2485,18 @@ +@@ -2472,13 +2510,18 @@ /* get the file type */ TOKENIZE @@ -1497,7 +1634,7 @@ } else if (!strcmp(token, "WAVE")) { -@@ -2489,6 +2507,8 @@ +@@ -2489,6 +2532,8 @@ osd_printf_error("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname); return chd_file::error::INVALID_DATA; } @@ -1506,7 +1643,7 @@ } else { -@@ -2502,6 +2522,12 @@ +@@ -2502,6 +2547,12 @@ /* get the track number */ TOKENIZE trknum = strtoul(token, nullptr, 10) - 1; @@ -1519,7 +1656,7 @@ /* next token on the line is the track type */ TOKENIZE -@@ -2541,6 +2567,10 @@ +@@ -2541,6 +2592,10 @@ outinfo.track[trknum].offset = wavoffs; wavoffs = wavlen = 0; } @@ -1530,7 +1667,7 @@ outinfo.track[trknum].fname.assign(lastfname); /* default filename to the last one */ -@@ -2586,6 +2616,54 @@ +@@ -2586,6 +2641,54 @@ osd_printf_error("ERROR: encountered invalid index %d\n", idx); return chd_file::error::INVALID_DATA; } @@ -1585,7 +1722,7 @@ outinfo.track[trknum].idx[idx] = frames; -@@ -2610,6 +2688,8 @@ +@@ -2610,6 +2713,8 @@ /* get index */ TOKENIZE frames = msf_to_frames(token); @@ -1594,7 +1731,7 @@ outtoc.tracks[trknum].pregap = frames; } -@@ -2620,11 +2700,15 @@ +@@ -2620,11 +2725,15 @@ /* get index */ TOKENIZE frames = msf_to_frames(token); @@ -1610,7 +1747,7 @@ outtoc.tracks[trknum].control_flags = 0; /* keep looping over remaining tokens in FLAGS line until there's no more to read */ -@@ -2667,18 +2751,51 @@ +@@ -2667,18 +2776,51 @@ } /* this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian */ @@ -1664,7 +1801,7 @@ tlen = get_file_size(outinfo.track[trknum].fname); if (tlen == 0) { -@@ -2700,11 +2817,17 @@ +@@ -2700,11 +2842,17 @@ return chd_file::error::INVALID_DATA; } @@ -1683,7 +1820,7 @@ } else if (outtoc.tracks[trknum].frames == 0) { -@@ -2777,6 +2900,77 @@ +@@ -2777,6 +2925,77 @@ } } @@ -1761,7 +1898,7 @@ if (is_gdrom) { /* -@@ -2787,6 +2981,18 @@ +@@ -2787,6 +3006,18 @@ uint32_t this_pregap = outtoc.tracks[trknum].pregap; uint32_t this_offset = this_pregap * (outtoc.tracks[trknum].datasize + outtoc.tracks[trknum].subsize); @@ -1780,7 +1917,7 @@ outtoc.tracks[trknum-1].frames += this_pregap; outtoc.tracks[trknum-1].splitframes += this_pregap; -@@ -2808,7 +3014,7 @@ +@@ -2808,7 +3039,7 @@ outtoc.tracks[trknum].physframeofs = 45000; int dif=outtoc.tracks[trknum].physframeofs-(outtoc.tracks[trknum-1].frames+outtoc.tracks[trknum-1].physframeofs); outtoc.tracks[trknum-1].frames += dif; @@ -1789,7 +1926,7 @@ } else { -@@ -2965,6 +3171,67 @@ +@@ -2965,6 +3196,67 @@ char linebuffer[512]; memset(linebuffer, 0, sizeof(linebuffer)); @@ -1857,7 +1994,7 @@ while (!feof(infile)) { /* get the next line */ -@@ -2989,6 +3256,8 @@ +@@ -2989,6 +3281,8 @@ */ if (!strcmp(token, "NO")) { @@ -1866,7 +2003,7 @@ TOKENIZE if (!strcmp(token, "COPY")) outtoc.tracks[trknum].control_flags &= ~CD_FLAG_CONTROL_DIGITAL_COPY_PERMITTED; -@@ -2997,100 +3266,210 @@ +@@ -2997,100 +3291,210 @@ } else if (!strcmp(token, "COPY")) { @@ -2120,7 +2257,7 @@ /* next token on the line is the track type */ TOKENIZE -@@ -3117,22 +3496,128 @@ +@@ -3117,22 +3521,128 @@ } else if (!strcmp(token, "START")) { @@ -2324,7 +2461,11 @@ static uint64_t read_uint64(FILE *infile); --- a/src/lib/util/chd.cpp +++ b/src/lib/util/chd.cpp -@@ -28,6 +28,20 @@ +@@ -25,9 +25,24 @@ + #include + #include + #include ++#include #include #include @@ -2345,7 +2486,7 @@ //************************************************************************** // CONSTANTS -@@ -853,6 +867,17 @@ +@@ -853,6 +868,17 @@ void chd_file::close() { @@ -2363,7 +2504,7 @@ // reset file characteristics m_file.reset(); m_allow_reads = false; -@@ -892,6 +917,48 @@ +@@ -892,6 +918,48 @@ m_cachehunk = ~0; } @@ -2412,17 +2553,20 @@ std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) { // punt if no file -@@ -919,6 +986,9 @@ +@@ -919,6 +987,12 @@ case V34_MAP_ENTRY_TYPE_COMPRESSED: { uint32_t const blocklen = get_u16be(&rawmap[12]) | (uint32_t(rawmap[14]) << 16); + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -931,7 +1001,14 @@ +@@ -931,11 +1005,21 @@ return std::error_condition(error::UNSUPPORTED_FORMAT); case V34_MAP_ENTRY_TYPE_SELF_HUNK: @@ -2438,17 +2582,27 @@ case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) -@@ -958,6 +1035,9 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->codec_process_hunk(blockoffs); + } + } +@@ -958,6 +1042,12 @@ case COMPRESSION_TYPE_2: case COMPRESSION_TYPE_3: { + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -970,7 +1050,14 @@ +@@ -970,11 +1060,24 @@ return std::error_condition(error::UNSUPPORTED_FORMAT); case COMPRESSION_SELF: @@ -2464,17 +2618,30 @@ case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) -@@ -1045,6 +1132,9 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: a parent whose units are larger than its hunks has no hunk for a unit (0.289 divided by zero) ++ if (UNEXPECTED(m_parent->unit_bytes() > m_parent->hunk_bytes())) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->codec_process_hunk(blockoffs / (m_parent->hunk_bytes() / m_parent->unit_bytes())); + } + break; +@@ -1045,6 +1148,12 @@ case V34_MAP_ENTRY_TYPE_COMPRESSED: { uint32_t const blocklen = get_u16be(&rawmap[12]) | (uint32_t(rawmap[14]) << 16); + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -1073,7 +1163,14 @@ +@@ -1073,11 +1182,21 @@ return std::error_condition(); case V34_MAP_ENTRY_TYPE_SELF_HUNK: @@ -2490,7 +2657,14 @@ case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) -@@ -1115,6 +1212,14 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->read_hunk(blockoffs, dest); + } + } +@@ -1115,6 +1234,17 @@ case COMPRESSION_TYPE_2: case COMPRESSION_TYPE_3: { @@ -2501,11 +2675,14 @@ +#endif + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -1130,16 +1235,29 @@ +@@ -1130,20 +1260,36 @@ case COMPRESSION_NONE: { @@ -2536,7 +2713,169 @@ case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) -@@ -2173,6 +2291,10 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->read_bytes(blockoffs * m_parent->unit_bytes(), dest, m_hunkbytes); + } + } +@@ -1307,6 +1453,11 @@ + + std::error_condition chd_file::read_bytes(uint64_t offset, void *buffer, uint32_t bytes) + { ++ // Discpress: nothing to read, as for a CD track without data (a damaged or crafted CHD's): 0.289's ++ // last hunk wrapped around at offset 0, and it read a whole hunk into the buffer ++ if (bytes == 0) ++ return std::error_condition(); ++ + // iterate over hunks + uint32_t const first_hunk = offset / m_hunkbytes; + uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; +@@ -1360,6 +1511,10 @@ + + std::error_condition chd_file::write_bytes(uint64_t offset, const void *buffer, uint32_t bytes) + { ++ // Discpress: nothing to write (as read_bytes) ++ if (bytes == 0) ++ return std::error_condition(); ++ + // iterate over hunks + uint32_t const first_hunk = offset / m_hunkbytes; + uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; +@@ -1453,7 +1608,7 @@ + // read the metadata + try { output.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- return file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); ++ return file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty + } + + /** +@@ -1515,7 +1670,7 @@ + // read the metadata + try { output.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); ++ err = file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + return err; + resulttag = metaentry.metatag; +@@ -1678,12 +1833,12 @@ + // read the metadata item + try { filedata.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); ++ err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + return err; + + // write it to the destination +- err = write_metadata(metaentry.metatag, (uint32_t)-1, &filedata[0], metaentry.length, metaentry.flags); ++ err = write_metadata(metaentry.metatag, (uint32_t)-1, filedata.data(), metaentry.length, metaentry.flags); + if (UNEXPECTED(err)) + return err; + } +@@ -1725,14 +1880,14 @@ + + // allocate memory and read the data + filedata.resize(metaentry.length); +- err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); ++ err = file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + throw err; + + // create an entry for this metadata and add it + metadata_hash hashentry; + put_u32be(hashentry.tag, metaentry.metatag); +- hashentry.sha1 = util::sha1_creator::simple(&filedata[0], metaentry.length); ++ hashentry.sha1 = util::sha1_creator::simple(filedata.data(), metaentry.length); + hasharray.push_back(hashentry); + } + if (err != error::METADATA_NOT_FOUND) +@@ -1880,6 +2035,41 @@ + } + + /** ++ * @fn bool chd_file::past_end(uint64_t offset, uint64_t length) const ++ * @brief past_end - Discpress: whether length bytes from offset would run past the end of the ++ * file (false when its length is unknown, so that the read reports it) ++ */ ++bool chd_file::past_end(uint64_t offset, uint64_t length) const ++{ ++ uint64_t filelength; ++ if (!m_file || m_file->length(filelength)) ++ return false; ++ return (offset > filelength) || (length > filelength - offset); ++} ++ ++/** ++ * @fn void chd_file::check_sizes(uint64_t hunkcount) const ++ * ++ * @brief ------------------------------------------------- ++ * check_sizes - Discpress: a header whose sizes no data can have (a damaged or ++ * crafted file) is an error. 0.289 divided by a hunk or unit size of 0, and with a ++ * logical size of more hunks than a map can count, it made a map too small for them ++ * -------------------------------------------------. ++ * ++ * @exception CHDERR_INVALID_FILE Thrown when the sizes can't be right. ++ * ++ * @param hunkcount The number of hunks the header gives or implies. ++ */ ++ ++void chd_file::check_sizes(uint64_t hunkcount) const ++{ ++ if (UNEXPECTED(m_hunkbytes == 0 || m_unitbytes == 0)) ++ throw std::error_condition(error::INVALID_FILE); ++ if (UNEXPECTED(hunkcount > std::numeric_limits::max())) ++ throw std::error_condition(error::INVALID_FILE); ++} ++ ++/** + * @fn void chd_file::parse_v3_header(uint8_t *rawheader, sha1_t &parentsha1) + * + * @brief ------------------------------------------------- +@@ -1938,7 +2128,8 @@ + parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); + + // guess at the units based on snooping the metadata +- m_unitbytes = guess_unitbytes(); ++ m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; ++ check_sizes(m_hunkcount); + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + } + +@@ -2001,7 +2192,8 @@ + parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); + + // guess at the units based on snooping the metadata +- m_unitbytes = guess_unitbytes(); ++ m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; ++ check_sizes(m_hunkcount); + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + } + +@@ -2029,8 +2221,9 @@ + m_mapoffset = get_u64be(&rawheader[40]); + m_metaoffset = get_u64be(&rawheader[48]); + m_hunkbytes = get_u32be(&rawheader[56]); +- m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; + m_unitbytes = get_u32be(&rawheader[60]); ++ check_sizes((m_hunkbytes != 0) ? ((m_logicalbytes / m_hunkbytes) + ((m_logicalbytes % m_hunkbytes) != 0)) : 0); ++ m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + + // determine compression +@@ -2073,7 +2266,7 @@ + try + { + // first get a CRC-16 of the original rawmap +- util::crc16_t mapcrc = util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12); ++ util::crc16_t mapcrc = util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12); + + // create a buffer to hold the RLE data + std::vector compression_rle(m_hunkcount); +@@ -2173,6 +2366,10 @@ // for COMPRESSION_PARENT: parentbits // the overall size is clamped later with bitbuf.flush() int nbits_needed = (8*16) + (12 + std::max({lengthbits+16, selfbits, parentbits}))*m_hunkcount; @@ -2547,7 +2886,7 @@ std::vector compressed(nbits_needed / 8 + 1); bitstream_out bitbuf(&compressed[16], compressed.size() - 16); -@@ -2255,7 +2377,8 @@ +@@ -2255,7 +2452,8 @@ // write the map header uint32_t complen = bitbuf.flush(); @@ -2557,7 +2896,75 @@ put_u32be(&compressed[0], complen); put_u48be(&compressed[4], firstoffs); put_u16be(&compressed[10], mapcrc); -@@ -2966,6 +3089,13 @@ +@@ -2298,7 +2496,7 @@ + // if no offset, we haven't written it yet + if (m_mapoffset == 0) + { +- memset(&m_rawmap[0], 0xff, m_rawmap.size()); ++ memset(m_rawmap.data(), 0xff, m_rawmap.size()); + return; + } + +@@ -2314,13 +2512,21 @@ + uint8_t const lengthbits = rawbuf[12]; + uint8_t const selfbits = rawbuf[13]; + uint8_t const parentbits = rawbuf[14]; ++ // Discpress: the bit reader reads at most 32 bits at a time, as many as compress_v5_map writes: ++ // more (a damaged or crafted map) shifted by a negative count, which C++ leaves undefined ++ if (UNEXPECTED((lengthbits > 32) || (selfbits > 32) || (parentbits > 32))) ++ throw std::error_condition(error::DECOMPRESSION_ERROR); + + // now read the map ++ // Discpress: one that would run past the end of the file is the error reading it gives, without ++ // allocating it first (up to 4 GB, for a damaged or crafted header) ++ if (UNEXPECTED(past_end(m_mapoffset + 16, mapbytes))) ++ throw std::error_condition(std::errc::io_error); + std::vector compressed(mapbytes); +- ioerr = file_read(m_mapoffset + 16, &compressed[0], mapbytes); ++ ioerr = file_read(m_mapoffset + 16, compressed.data(), mapbytes); + if (UNEXPECTED(ioerr)) + throw ioerr; +- bitstream_in bitbuf(&compressed[0], compressed.size()); ++ bitstream_in bitbuf(compressed.data(), compressed.size()); + + // first decode the compression types + huffman_decoder<16, 8> decoder; +@@ -2409,7 +2615,7 @@ + } + + // verify the final CRC +- if (UNEXPECTED(util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12) != mapcrc)) ++ if (UNEXPECTED(util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12) != mapcrc)) + throw std::error_condition(error::DECOMPRESSION_ERROR); + } + +@@ -2636,14 +2842,23 @@ + } + + // read the map; v5+ compressed drives need to read and decompress their map +- m_rawmap.resize(m_hunkcount * m_mapentrybytes); ++ // Discpress: a map larger than 32 bits can count (a damaged or crafted header) is an error: 0.289 ++ // computed its size in 32 bits, and with the size wrapped, read and wrote past the end of the map ++ uint64_t const mapsize = uint64_t(m_hunkcount) * m_mapentrybytes; ++ if (UNEXPECTED(mapsize > std::numeric_limits::max())) ++ throw std::error_condition(error::INVALID_FILE); ++ // Discpress: and a stored map that would run past the end of the file is the error reading it ++ // gives, without allocating it first ++ if (!(m_version >= 5 && compressed()) && UNEXPECTED(past_end(m_mapoffset, mapsize))) ++ throw std::error_condition(std::errc::io_error); ++ m_rawmap.resize(mapsize); + if (m_version >= 5 && compressed()) + { + decompress_v5_map(); + } + else + { +- std::error_condition err = file_read(m_mapoffset, &m_rawmap[0], m_rawmap.size()); ++ std::error_condition err = file_read(m_mapoffset, m_rawmap.data(), m_rawmap.size()); + if (UNEXPECTED(err)) + throw err; + } +@@ -2966,6 +3181,13 @@ chd_file_compressor::~chd_file_compressor() { @@ -2571,7 +2978,7 @@ // free the work queues osd_work_queue_free(m_read_queue); osd_work_queue_free(m_work_queue); -@@ -3022,6 +3152,10 @@ +@@ -3022,6 +3244,10 @@ // reset write state m_write_hunk = 0; @@ -2582,7 +2989,7 @@ } /** -@@ -3123,6 +3257,11 @@ +@@ -3123,6 +3349,11 @@ } else { @@ -2594,7 +3001,7 @@ // otherwise, append it compressed and add to the self map hunk_write_compressed(item.m_hunknum, item.m_compression, item.m_compressed, item.m_complen, item.m_hash[0].m_crc16); m_total_out += item.m_complen; -@@ -3263,12 +3402,44 @@ +@@ -3263,12 +3494,44 @@ // TODO: data race if ((m_current_map.find(item.m_hash[0].m_crc16, item.m_hash[0].m_sha1) == hashmap::NOT_FOUND) && (m_parent_map.find(item.m_hash[0].m_crc16, item.m_hash[0].m_sha1) == hashmap::NOT_FOUND)) @@ -2640,7 +3047,7 @@ /** * @fn void *chd_file_compressor::async_read_static(void *param, int threadid) * -@@ -3345,6 +3516,16 @@ +@@ -3345,6 +3608,16 @@ assert(item.m_status == WS_READING); item.m_status = WS_QUEUED; item.m_hunknum = hunknum; @@ -2657,7 +3064,7 @@ item.m_osd = osd_work_item_queue(m_work_queue, m_walking_parent ? async_walk_parent_static : async_compress_hunk_static, &item, 0); } -@@ -3512,3 +3693,184 @@ +@@ -3512,3 +3785,184 @@ metadata_entry metaentry; return metadata_find(AV_METADATA_TAG, 0, metaentry); } @@ -2825,7 +3232,7 @@ + memcpy(dest, &ahead.data[0], m_hunkbytes); + return true; + case 3: // compressed bytes -+ m_decompressor[type]->decompress(&ahead.data[0], ahead.data.size(), reinterpret_cast(dest), m_hunkbytes); ++ m_decompressor[type]->decompress(ahead.data.data(), ahead.data.size(), reinterpret_cast(dest), m_hunkbytes); + return true; + default: + return false; @@ -2885,7 +3292,16 @@ private: struct metadata_entry; struct metadata_hash; -@@ -391,6 +411,7 @@ +@@ -380,6 +400,8 @@ + + // internal helpers + uint32_t guess_unitbytes(); ++ void check_sizes(uint64_t hunkcount) const; // Discpress ++ bool past_end(uint64_t offset, uint64_t length) const; // Discpress + void parse_v3_header(uint8_t *rawheader, util::sha1_t &parentsha1); + void parse_v4_header(uint8_t *rawheader, util::sha1_t &parentsha1); + void parse_v5_header(uint8_t *rawheader, util::sha1_t &parentsha1); +@@ -391,6 +413,7 @@ std::error_condition verify_proper_compression_append(uint32_t hunknum) const noexcept; void hunk_write_compressed(uint32_t hunknum, int8_t compression, const uint8_t *compressed, uint32_t complength, util::crc16_t crc16); void hunk_copy_from_self(uint32_t hunknum, uint32_t otherhunk); @@ -2893,7 +3309,7 @@ void hunk_copy_from_parent(uint32_t hunknum, uint64_t parentunit); std::error_condition metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume = false) const noexcept; std::error_condition metadata_set_previous_next(uint64_t prevoffset, uint64_t nextoffset) noexcept; -@@ -433,6 +454,21 @@ +@@ -433,6 +456,21 @@ // caching std::vector m_cache; // single-hunk cache for partial reads/writes uint32_t m_cachehunk; // which hunk is in the cache? @@ -2915,7 +3331,7 @@ }; -@@ -450,11 +486,28 @@ +@@ -450,11 +488,28 @@ void compress_begin(); std::error_condition compress_continue(double &progress, double &ratio); @@ -2944,7 +3360,7 @@ // hash map for looking up values class hashmap { -@@ -524,6 +577,7 @@ +@@ -524,6 +579,7 @@ , m_complen(0) , m_compression(0) , m_codecs(nullptr) @@ -2952,7 +3368,7 @@ { } osd_work_item * m_osd; // OSD work item running on this block -@@ -537,6 +591,7 @@ +@@ -537,6 +593,7 @@ uint32_t m_complen; // compressed data length int8_t m_compression; // type of compression used chd_compressor_group *m_codecs; // codec instance @@ -2960,7 +3376,7 @@ std::vector m_hash; // array of hashes }; -@@ -549,6 +604,7 @@ +@@ -549,6 +606,7 @@ void async_read(); // current compression status @@ -3035,7 +3451,24 @@ return header_bytes + complen + m_subcode_compressor.compress(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA], frames * cdrom_file::MAX_SUBCODE_DATA, &dest[header_bytes + complen]); } -@@ -735,17 +751,33 @@ +@@ -426,8 +442,16 @@ + uint32_t ecc_bytes = (frames + 7) / 8; + uint32_t header_bytes = ecc_bytes + complen_bytes; + ++ // Discpress: a header longer than the data, or a base longer than what follows the header (a ++ // damaged or crafted hunk), is an error: 0.289 decoded past the end of the data, and the ++ // subcode's length wrapped ++ if (UNEXPECTED(complen < header_bytes)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); ++ + // extract compressed length of base + uint32_t complen_base = (complen_bytes > 2) ? get_u24be(&src[ecc_bytes]) : get_u16be(&src[ecc_bytes]); ++ if (UNEXPECTED(complen_base > complen - header_bytes)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); + + // reset and decode + m_base_decompressor.decompress(&src[header_bytes], complen_base, &m_buffer[0], frames * cdrom_file::MAX_SECTOR_DATA); +@@ -735,17 +759,33 @@ // compression for this hunk //------------------------------------------------- @@ -3071,7 +3504,7 @@ // if this is the best one, copy the data into the permanent buffer uint32_t compbytes = m_compressor[codecnum]->compress(src, m_hunkbytes, &m_compress_test[0]); #if CHDCODEC_VERIFY_COMPRESSION -@@ -772,7 +804,7 @@ +@@ -772,7 +812,7 @@ } printf(" codec%d=%d bytes \n", codecnum, compbytes); #endif @@ -3080,7 +3513,7 @@ { compression = codecnum; complen = compbytes; -@@ -787,6 +819,7 @@ +@@ -787,6 +827,7 @@ // if the best is none, copy it over if (compression == -1) memcpy(compressed, src, m_hunkbytes); @@ -3088,7 +3521,7 @@ return compression; } -@@ -852,12 +885,12 @@ +@@ -852,12 +893,12 @@ { // set the low bit of the size so we don't match next time *ptr |= 1; @@ -3103,7 +3536,7 @@ for (int scan = 0; scan < MAX_ZLIB_ALLOCS; scan++) if (codec->m_allocptr[scan] == nullptr) { -@@ -867,7 +900,7 @@ +@@ -867,7 +908,7 @@ // set the low bit of the size so we don't match next time *ptr = size | 1; @@ -3112,7 +3545,7 @@ } -@@ -881,7 +914,7 @@ +@@ -881,7 +922,7 @@ auto *codec = reinterpret_cast(opaque); // find the hunk @@ -3121,7 +3554,7 @@ for (int scan = 0; scan < MAX_ZLIB_ALLOCS; scan++) if (ptr == codec->m_allocptr[scan]) { -@@ -915,6 +948,13 @@ +@@ -915,6 +956,13 @@ throw std::bad_alloc(); else if (zerr != Z_OK) throw std::error_condition(chd_file::error::CODEC_ERROR); @@ -3135,7 +3568,7 @@ } -@@ -925,6 +965,8 @@ +@@ -925,6 +973,8 @@ chd_zlib_compressor::~chd_zlib_compressor() { deflateEnd(&m_deflater); @@ -3144,7 +3577,7 @@ } -@@ -934,12 +976,24 @@ +@@ -934,12 +984,24 @@ uint32_t chd_zlib_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { @@ -3170,7 +3603,7 @@ m_deflater.total_out = 0; int zerr = deflateReset(&m_deflater); if (zerr != Z_OK) -@@ -949,7 +1003,7 @@ +@@ -949,7 +1011,7 @@ zerr = deflate(&m_deflater, Z_FINISH); // if we ended up with more data than we started with, return an error @@ -3179,7 +3612,7 @@ throw std::error_condition(chd_file::error::COMPRESSION_ERROR); // otherwise, return the length -@@ -1066,7 +1120,7 @@ +@@ -1066,7 +1128,7 @@ // do it ZSTD_inBuffer input{ src, srclen, 0 }; @@ -3188,7 +3621,7 @@ while (output.pos < output.size) { result = ZSTD_compressStream2(m_stream, &output, &input, ZSTD_e_end); -@@ -1197,12 +1251,12 @@ +@@ -1197,12 +1259,12 @@ { // set the low bit of the size so we don't match next time *ptr |= 1; @@ -3203,7 +3636,7 @@ for (int scan = 0; scan < MAX_LZMA_ALLOCS; scan++) if (codec->m_allocptr[scan] == nullptr) { -@@ -1212,7 +1266,7 @@ +@@ -1212,7 +1274,7 @@ // set the low bit of the size so we don't match next time *ptr = size | 1; @@ -3212,7 +3645,7 @@ } -@@ -1229,7 +1283,7 @@ +@@ -1229,7 +1291,7 @@ auto *const codec = static_cast(const_cast(p)); // find the hunk @@ -3221,7 +3654,7 @@ for (int scan = 0; scan < MAX_LZMA_ALLOCS; scan++) if (ptr == codec->m_allocptr[scan]) { -@@ -1284,8 +1338,8 @@ +@@ -1284,8 +1346,8 @@ if (res != SZ_OK) throw std::error_condition(chd_file::error::COMPRESSION_ERROR); @@ -3232,7 +3665,7 @@ res = LzmaEnc_MemEncode(encoder, dest, &complen, src, srclen, 0, nullptr, &m_allocator, &m_allocator); if (res != SZ_OK) throw std::error_condition(chd_file::error::COMPRESSION_ERROR); -@@ -1419,7 +1473,7 @@ +@@ -1419,7 +1481,7 @@ uint32_t chd_huffman_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { uint32_t complen; @@ -3241,7 +3674,7 @@ throw std::error_condition(chd_file::error::COMPRESSION_ERROR); return complen; } -@@ -1474,6 +1528,7 @@ +@@ -1474,6 +1536,7 @@ m_encoder.set_num_channels(2); m_encoder.set_block_size(blocksize(hunkbytes)); m_encoder.set_strip_metadata(true); @@ -3249,7 +3682,7 @@ } -@@ -1483,32 +1538,47 @@ +@@ -1483,32 +1546,47 @@ uint32_t chd_flac_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { @@ -3310,7 +3743,7 @@ } return complen + 1; } -@@ -1642,14 +1712,16 @@ +@@ -1642,14 +1720,16 @@ memcpy(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA + framenum * cdrom_file::MAX_SUBCODE_DATA], &src[framenum * cdrom_file::FRAME_SIZE + cdrom_file::MAX_SECTOR_DATA], cdrom_file::MAX_SUBCODE_DATA); } @@ -3330,6 +3763,18 @@ // deflate the subcode data m_deflater.next_in = const_cast(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA]); +@@ -1950,6 +2030,11 @@ + + void chd_avhuff_decompressor::decompress(const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) + { ++ // Discpress: a frame larger than the hunk (damaged or crafted data) is an error: 0.289 decoded it ++ // past the end of the hunk (its size, as raw_data_size has it: header, metadata, audio, video) ++ if ((complen >= 8) && (12 + uint64_t(src[0]) + 2 * uint64_t(src[1]) * get_u16be(&src[2]) + 2 * uint64_t(get_u16be(&src[4])) * get_u16be(&src[6]) > destlen)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); ++ + // decode the audio and video + avhuff_error averr = m_decoder.decode_data(src, complen, dest); + if (averr != AVHERR_NONE) --- a/src/lib/util/chdcodec.h +++ b/src/lib/util/chdcodec.h @@ -71,6 +71,13 @@ @@ -3708,6 +4153,42 @@ m_accum.m_raw = crc; } +--- a/src/lib/util/huffman.cpp ++++ b/src/lib/util/huffman.cpp +@@ -631,6 +631,10 @@ + uint32_t nextstart = (curstart + bithisto[codelen]) >> 1; + if (codelen != 1 && nextstart * 2 != (curstart + bithisto[codelen])) + return HUFFERR_INTERNAL_INCONSISTENCY; ++ // Discpress: more codes than one bit has room for (a damaged or crafted tree): 0.289 gave codes ++ // more bits than their length, and build_lookup_table wrote past the end of its table ++ if (codelen == 1 && (curstart + bithisto[codelen]) > 2) ++ return HUFFERR_INTERNAL_INCONSISTENCY; + bithisto[codelen] = curstart; + curstart = nextstart; + } +@@ -654,6 +658,7 @@ + void huffman_context_base::build_lookup_table() + { + // iterate over all codes ++ uint32_t covered = 0; + for (int curcode = 0; curcode < m_numcodes; curcode++) + { + // process all nodes which have non-zero bits +@@ -669,8 +674,14 @@ + lookup_value *destend = &m_lookup[((node.m_bits + 1) << shift) - 1]; + while (dest <= destend) + *dest++ = value; ++ covered = std::max(covered, (node.m_bits + 1) << shift); + } + } ++ ++ // Discpress: the entries no code covers decode as code 0 of no bits. Canonical codes cover the table ++ // from 0 up, all of it for a complete tree (what an encoder writes); 0.289 left the rest as it was, ++ // whatever was in that memory, and a damaged tree's data read bit counts from it ++ std::fill(m_lookup + covered, m_lookup + (uint32_t(1) << m_maxbits), lookup_value(0)); + } + + --- a/src/lib/util/unicode.cpp +++ b/src/lib/util/unicode.cpp @@ -16,71 +16,12 @@ diff --git a/engine/mame/3rdparty/flac/src/libFLAC/stream_encoder.c b/engine/mame/3rdparty/flac/src/libFLAC/stream_encoder.c index c1c03e4..c518289 100644 --- a/engine/mame/3rdparty/flac/src/libFLAC/stream_encoder.c +++ b/engine/mame/3rdparty/flac/src/libFLAC/stream_encoder.c @@ -4126,7 +4126,8 @@ uint32_t find_best_partition_order_( residual, #endif abs_residual_partition_sums+sum, - raw_bits_per_partition+sum, + /* Discpress: null without escape coding, which C doesn't let an offset be added to */ + raw_bits_per_partition != NULL ? raw_bits_per_partition+sum : NULL, residual_samples, predictor_order, rice_parameter_limit, diff --git a/engine/mame/src/lib/util/avhuff.cpp b/engine/mame/src/lib/util/avhuff.cpp index 410f8ae..bf27e58 100644 --- a/engine/mame/src/lib/util/avhuff.cpp +++ b/engine/mame/src/lib/util/avhuff.cpp @@ -67,6 +67,7 @@ #include #include #include +#include #include @@ -723,6 +724,11 @@ avhuff_error avhuff_decoder::decode_data(const uint8_t *source, uint32_t complen uint32_t width = get_u16be(&source[4]); uint32_t height = get_u16be(&source[6]); + // Discpress: no more channels than there are places for (the encoder takes up to 16): 0.289 went past + // the end of audiostart and of m_config.audio + if (channels > std::size(m_config.audio)) + return AVHERR_INVALID_DATA; + // validate that the sizes make sense if (complength < 10 + 2 * channels) return AVHERR_INVALID_DATA; @@ -932,6 +938,10 @@ avhuff_error avhuff_decoder::decode_audio(int channels, int samples, const uint8 // if no huffman length, just copy the data if (treesize == 0) { + // Discpress: as much as the samples take (as stored): 0.289 read past the channel's + // data, and past the end of the hunk's + if (size < 2 * samples) + return AVHERR_INVALID_DATA; const uint8_t *cursource = source; for (int sampnum = 0; sampnum < samples; sampnum++) { diff --git a/engine/mame/src/lib/util/bitstream.h b/engine/mame/src/lib/util/bitstream.h index d96c2e5..5ecb534 100644 --- a/engine/mame/src/lib/util/bitstream.h +++ b/engine/mame/src/lib/util/bitstream.h @@ -226,6 +226,11 @@ inline bitstream_out::bitstream_out(void *dest, uint32_t destlength) inline void bitstream_out::write(uint32_t newbits, int numbits) { + // Discpress: nothing to write, as for a CHD map with no copies of hunks (0.289 went on to shift + // the value by 32 bits, which C++ leaves undefined, and then returned without using it) + if (numbits <= 0) + return; + newbits <<= 32 - numbits; // flush the buffer if we're going to overflow it diff --git a/engine/mame/src/lib/util/cdrom.cpp b/engine/mame/src/lib/util/cdrom.cpp index 96eff6c..13d0bbf 100644 --- a/engine/mame/src/lib/util/cdrom.cpp +++ b/engine/mame/src/lib/util/cdrom.cpp @@ -921,6 +921,13 @@ const char *cdrom_file::get_subtype_string(uint32_t subtype) * @return A std::error_condition. */ +// Discpress: chd.cpp's formats for the track metadata, with a width for each string that fills its 16-byte +// buffer at most (the longest a CD has is 15 characters, VMODE2_FORM_MIX); a longer one, in damaged or +// crafted metadata, doesn't match. 0.289 read them with %s, which wrote past the end of the buffer. +static const char CDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d"; +static const char CDROM_TRACK_METADATA2_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; +static const char GDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; + std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) { std::string metadata; @@ -948,12 +955,12 @@ std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) // fetch the metadata for this track if (!chd->read_metadata(CDROM_TRACK_METADATA_TAG, toc.numtrks, metadata)) { - if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames) != 4) + if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames) != 4) return chd_file::error::INVALID_DATA; } else if (!chd->read_metadata(CDROM_TRACK_METADATA2_TAG, toc.numtrks, metadata)) { - if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) + if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT_READ, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) return chd_file::error::INVALID_DATA; } else @@ -968,7 +975,7 @@ std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) if (err) break; - if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) + if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) return chd_file::error::INVALID_DATA; toc.flags |= CD_FLAG_GDROM; @@ -1019,7 +1026,14 @@ std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) /* if we got any tracks this way, we're done */ if (toc.numtrks > 0) + { + // Discpress: with each track once: a track number that repeats or skips one (damaged or crafted + // metadata) left a track of nothing, of 0-byte sectors, which extractcd divided by + for (uint32_t i = 0; i < toc.numtrks; i++) + if (toc.tracks[i].datasize == 0) + return chd_file::error::INVALID_DATA; return std::error_condition(); + } osd_printf_info("toc.numtrks = %u?!\n", toc.numtrks); @@ -1028,6 +1042,9 @@ std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) err = chd->read_metadata(CDROM_OLD_METADATA_TAG, 0, oldmetadata); if (err) return err; + // Discpress: as long as the table it holds (0.289 read past the end of shorter metadata) + if (oldmetadata.size() < 4 + MAX_TRACKS * 6 * 4) + return chd_file::error::INVALID_DATA; /* reconstruct the TOC from it */ auto *mrp = reinterpret_cast(&oldmetadata[0]); @@ -1068,6 +1085,14 @@ std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) } } + // Discpress: no more tracks than the table holds, with sectors a CD has: anything else (damaged or + // crafted metadata) made 0.289 read past the end of the table, and of a sector's buffer + if (toc.numtrks > MAX_TRACKS) + return chd_file::error::INVALID_DATA; + for (uint32_t i = 0; i < toc.numtrks; i++) + if (toc.tracks[i].datasize == 0 || toc.tracks[i].datasize > MAX_SECTOR_DATA || toc.tracks[i].subsize > MAX_SUBCODE_DATA) + return chd_file::error::INVALID_DATA; + return std::error_condition(); } diff --git a/engine/mame/src/lib/util/chd.cpp b/engine/mame/src/lib/util/chd.cpp index f9c1c5f..db3abee 100644 --- a/engine/mame/src/lib/util/chd.cpp +++ b/engine/mame/src/lib/util/chd.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -989,6 +990,9 @@ std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end if (UNEXPECTED(blocklen > m_compressed.size())) return std::error_condition(error::INVALID_DATA); + // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have + if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; @@ -1013,6 +1017,9 @@ std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) return std::error_condition(error::REQUIRES_PARENT); + // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer + if (UNEXPECTED(!m_parent)) + return std::error_condition(error::INVALID_DATA); return m_parent->codec_process_hunk(blockoffs); } } @@ -1038,6 +1045,9 @@ std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end if (UNEXPECTED(blocklen > m_compressed.size())) return std::error_condition(error::INVALID_DATA); + // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have + if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; @@ -1062,6 +1072,12 @@ std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) return std::error_condition(error::REQUIRES_PARENT); + // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer + if (UNEXPECTED(!m_parent)) + return std::error_condition(error::INVALID_DATA); + // Discpress: a parent whose units are larger than its hunks has no hunk for a unit (0.289 divided by zero) + if (UNEXPECTED(m_parent->unit_bytes() > m_parent->hunk_bytes())) + return std::error_condition(error::INVALID_DATA); return m_parent->codec_process_hunk(blockoffs / (m_parent->hunk_bytes() / m_parent->unit_bytes())); } break; @@ -1135,6 +1151,9 @@ std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer) // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end if (UNEXPECTED(blocklen > m_compressed.size())) return std::error_condition(error::INVALID_DATA); + // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have + if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; @@ -1175,6 +1194,9 @@ std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer) case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) return std::error_condition(error::REQUIRES_PARENT); + // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer + if (UNEXPECTED(!m_parent)) + return std::error_condition(error::INVALID_DATA); return m_parent->read_hunk(blockoffs, dest); } } @@ -1220,6 +1242,9 @@ std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer) // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end if (UNEXPECTED(blocklen > m_compressed.size())) return std::error_condition(error::INVALID_DATA); + // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have + if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; @@ -1262,6 +1287,9 @@ std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer) case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) return std::error_condition(error::REQUIRES_PARENT); + // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer + if (UNEXPECTED(!m_parent)) + return std::error_condition(error::INVALID_DATA); return m_parent->read_bytes(blockoffs * m_parent->unit_bytes(), dest, m_hunkbytes); } } @@ -1425,6 +1453,11 @@ std::error_condition chd_file::write_units(uint64_t unitnum, const void *buffer, std::error_condition chd_file::read_bytes(uint64_t offset, void *buffer, uint32_t bytes) { + // Discpress: nothing to read, as for a CD track without data (a damaged or crafted CHD's): 0.289's + // last hunk wrapped around at offset 0, and it read a whole hunk into the buffer + if (bytes == 0) + return std::error_condition(); + // iterate over hunks uint32_t const first_hunk = offset / m_hunkbytes; uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; @@ -1478,6 +1511,10 @@ std::error_condition chd_file::read_bytes(uint64_t offset, void *buffer, uint32_ std::error_condition chd_file::write_bytes(uint64_t offset, const void *buffer, uint32_t bytes) { + // Discpress: nothing to write (as read_bytes) + if (bytes == 0) + return std::error_condition(); + // iterate over hunks uint32_t const first_hunk = offset / m_hunkbytes; uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; @@ -1571,7 +1608,7 @@ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_ // read the metadata try { output.resize(metaentry.length); } catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } - return file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); + return file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty } /** @@ -1633,7 +1670,7 @@ std::error_condition chd_file::read_metadata(chd_metadata_tag searchtag, uint32_ // read the metadata try { output.resize(metaentry.length); } catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } - err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); + err = file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty if (UNEXPECTED(err)) return err; resulttag = metaentry.metatag; @@ -1796,12 +1833,12 @@ std::error_condition chd_file::clone_all_metadata(chd_file &source) // read the metadata item try { filedata.resize(metaentry.length); } catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } - err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); + err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty if (UNEXPECTED(err)) return err; // write it to the destination - err = write_metadata(metaentry.metatag, (uint32_t)-1, &filedata[0], metaentry.length, metaentry.flags); + err = write_metadata(metaentry.metatag, (uint32_t)-1, filedata.data(), metaentry.length, metaentry.flags); if (UNEXPECTED(err)) return err; } @@ -1843,14 +1880,14 @@ util::sha1_t chd_file::compute_overall_sha1(util::sha1_t rawsha1) // allocate memory and read the data filedata.resize(metaentry.length); - err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); + err = file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty if (UNEXPECTED(err)) throw err; // create an entry for this metadata and add it metadata_hash hashentry; put_u32be(hashentry.tag, metaentry.metatag); - hashentry.sha1 = util::sha1_creator::simple(&filedata[0], metaentry.length); + hashentry.sha1 = util::sha1_creator::simple(filedata.data(), metaentry.length); hasharray.push_back(hashentry); } if (err != error::METADATA_NOT_FOUND) @@ -1997,6 +2034,41 @@ uint32_t chd_file::guess_unitbytes() return m_hunkbytes; } +/** + * @fn bool chd_file::past_end(uint64_t offset, uint64_t length) const + * @brief past_end - Discpress: whether length bytes from offset would run past the end of the + * file (false when its length is unknown, so that the read reports it) + */ +bool chd_file::past_end(uint64_t offset, uint64_t length) const +{ + uint64_t filelength; + if (!m_file || m_file->length(filelength)) + return false; + return (offset > filelength) || (length > filelength - offset); +} + +/** + * @fn void chd_file::check_sizes(uint64_t hunkcount) const + * + * @brief ------------------------------------------------- + * check_sizes - Discpress: a header whose sizes no data can have (a damaged or + * crafted file) is an error. 0.289 divided by a hunk or unit size of 0, and with a + * logical size of more hunks than a map can count, it made a map too small for them + * -------------------------------------------------. + * + * @exception CHDERR_INVALID_FILE Thrown when the sizes can't be right. + * + * @param hunkcount The number of hunks the header gives or implies. + */ + +void chd_file::check_sizes(uint64_t hunkcount) const +{ + if (UNEXPECTED(m_hunkbytes == 0 || m_unitbytes == 0)) + throw std::error_condition(error::INVALID_FILE); + if (UNEXPECTED(hunkcount > std::numeric_limits::max())) + throw std::error_condition(error::INVALID_FILE); +} + /** * @fn void chd_file::parse_v3_header(uint8_t *rawheader, sha1_t &parentsha1) * @@ -2056,7 +2128,8 @@ void chd_file::parse_v3_header(uint8_t *rawheader, util::sha1_t &parentsha1) parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); // guess at the units based on snooping the metadata - m_unitbytes = guess_unitbytes(); + m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; + check_sizes(m_hunkcount); m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; } @@ -2119,7 +2192,8 @@ void chd_file::parse_v4_header(uint8_t *rawheader, util::sha1_t &parentsha1) parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); // guess at the units based on snooping the metadata - m_unitbytes = guess_unitbytes(); + m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; + check_sizes(m_hunkcount); m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; } @@ -2147,8 +2221,9 @@ void chd_file::parse_v5_header(uint8_t *rawheader, util::sha1_t &parentsha1) m_mapoffset = get_u64be(&rawheader[40]); m_metaoffset = get_u64be(&rawheader[48]); m_hunkbytes = get_u32be(&rawheader[56]); - m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; m_unitbytes = get_u32be(&rawheader[60]); + check_sizes((m_hunkbytes != 0) ? ((m_logicalbytes / m_hunkbytes) + ((m_logicalbytes % m_hunkbytes) != 0)) : 0); + m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; // determine compression @@ -2191,7 +2266,7 @@ std::error_condition chd_file::compress_v5_map() try { // first get a CRC-16 of the original rawmap - util::crc16_t mapcrc = util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12); + util::crc16_t mapcrc = util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12); // create a buffer to hold the RLE data std::vector compression_rle(m_hunkcount); @@ -2421,7 +2496,7 @@ void chd_file::decompress_v5_map() // if no offset, we haven't written it yet if (m_mapoffset == 0) { - memset(&m_rawmap[0], 0xff, m_rawmap.size()); + memset(m_rawmap.data(), 0xff, m_rawmap.size()); return; } @@ -2437,13 +2512,21 @@ void chd_file::decompress_v5_map() uint8_t const lengthbits = rawbuf[12]; uint8_t const selfbits = rawbuf[13]; uint8_t const parentbits = rawbuf[14]; + // Discpress: the bit reader reads at most 32 bits at a time, as many as compress_v5_map writes: + // more (a damaged or crafted map) shifted by a negative count, which C++ leaves undefined + if (UNEXPECTED((lengthbits > 32) || (selfbits > 32) || (parentbits > 32))) + throw std::error_condition(error::DECOMPRESSION_ERROR); // now read the map + // Discpress: one that would run past the end of the file is the error reading it gives, without + // allocating it first (up to 4 GB, for a damaged or crafted header) + if (UNEXPECTED(past_end(m_mapoffset + 16, mapbytes))) + throw std::error_condition(std::errc::io_error); std::vector compressed(mapbytes); - ioerr = file_read(m_mapoffset + 16, &compressed[0], mapbytes); + ioerr = file_read(m_mapoffset + 16, compressed.data(), mapbytes); if (UNEXPECTED(ioerr)) throw ioerr; - bitstream_in bitbuf(&compressed[0], compressed.size()); + bitstream_in bitbuf(compressed.data(), compressed.size()); // first decode the compression types huffman_decoder<16, 8> decoder; @@ -2532,7 +2615,7 @@ void chd_file::decompress_v5_map() } // verify the final CRC - if (UNEXPECTED(util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12) != mapcrc)) + if (UNEXPECTED(util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12) != mapcrc)) throw std::error_condition(error::DECOMPRESSION_ERROR); } @@ -2759,14 +2842,23 @@ void chd_file::create_open_common() } // read the map; v5+ compressed drives need to read and decompress their map - m_rawmap.resize(m_hunkcount * m_mapentrybytes); + // Discpress: a map larger than 32 bits can count (a damaged or crafted header) is an error: 0.289 + // computed its size in 32 bits, and with the size wrapped, read and wrote past the end of the map + uint64_t const mapsize = uint64_t(m_hunkcount) * m_mapentrybytes; + if (UNEXPECTED(mapsize > std::numeric_limits::max())) + throw std::error_condition(error::INVALID_FILE); + // Discpress: and a stored map that would run past the end of the file is the error reading it + // gives, without allocating it first + if (!(m_version >= 5 && compressed()) && UNEXPECTED(past_end(m_mapoffset, mapsize))) + throw std::error_condition(std::errc::io_error); + m_rawmap.resize(mapsize); if (m_version >= 5 && compressed()) { decompress_v5_map(); } else { - std::error_condition err = file_read(m_mapoffset, &m_rawmap[0], m_rawmap.size()); + std::error_condition err = file_read(m_mapoffset, m_rawmap.data(), m_rawmap.size()); if (UNEXPECTED(err)) throw err; } @@ -3857,7 +3949,7 @@ bool chd_file::wasm_read_cached(uint32_t hunknum, uint8_t type, void *dest) memcpy(dest, &ahead.data[0], m_hunkbytes); return true; case 3: // compressed bytes - m_decompressor[type]->decompress(&ahead.data[0], ahead.data.size(), reinterpret_cast(dest), m_hunkbytes); + m_decompressor[type]->decompress(ahead.data.data(), ahead.data.size(), reinterpret_cast(dest), m_hunkbytes); return true; default: return false; diff --git a/engine/mame/src/lib/util/chd.h b/engine/mame/src/lib/util/chd.h index 8b6e993..c10ee8d 100644 --- a/engine/mame/src/lib/util/chd.h +++ b/engine/mame/src/lib/util/chd.h @@ -400,6 +400,8 @@ class chd_file // internal helpers uint32_t guess_unitbytes(); + void check_sizes(uint64_t hunkcount) const; // Discpress + bool past_end(uint64_t offset, uint64_t length) const; // Discpress void parse_v3_header(uint8_t *rawheader, util::sha1_t &parentsha1); void parse_v4_header(uint8_t *rawheader, util::sha1_t &parentsha1); void parse_v5_header(uint8_t *rawheader, util::sha1_t &parentsha1); diff --git a/engine/mame/src/lib/util/chdcodec.cpp b/engine/mame/src/lib/util/chdcodec.cpp index 6d58e99..0048b48 100644 --- a/engine/mame/src/lib/util/chdcodec.cpp +++ b/engine/mame/src/lib/util/chdcodec.cpp @@ -442,8 +442,16 @@ class chd_cd_decompressor : public chd_decompressor uint32_t ecc_bytes = (frames + 7) / 8; uint32_t header_bytes = ecc_bytes + complen_bytes; + // Discpress: a header longer than the data, or a base longer than what follows the header (a + // damaged or crafted hunk), is an error: 0.289 decoded past the end of the data, and the + // subcode's length wrapped + if (UNEXPECTED(complen < header_bytes)) + throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); + // extract compressed length of base uint32_t complen_base = (complen_bytes > 2) ? get_u24be(&src[ecc_bytes]) : get_u16be(&src[ecc_bytes]); + if (UNEXPECTED(complen_base > complen - header_bytes)) + throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); // reset and decode m_base_decompressor.decompress(&src[header_bytes], complen_base, &m_buffer[0], frames * cdrom_file::MAX_SECTOR_DATA); @@ -2022,6 +2030,11 @@ void chd_avhuff_decompressor::process(const uint8_t *src, uint32_t complen) void chd_avhuff_decompressor::decompress(const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) { + // Discpress: a frame larger than the hunk (damaged or crafted data) is an error: 0.289 decoded it + // past the end of the hunk (its size, as raw_data_size has it: header, metadata, audio, video) + if ((complen >= 8) && (12 + uint64_t(src[0]) + 2 * uint64_t(src[1]) * get_u16be(&src[2]) + 2 * uint64_t(get_u16be(&src[4])) * get_u16be(&src[6]) > destlen)) + throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); + // decode the audio and video avhuff_error averr = m_decoder.decode_data(src, complen, dest); if (averr != AVHERR_NONE) diff --git a/engine/mame/src/lib/util/huffman.cpp b/engine/mame/src/lib/util/huffman.cpp index a1f37b6..0638810 100644 --- a/engine/mame/src/lib/util/huffman.cpp +++ b/engine/mame/src/lib/util/huffman.cpp @@ -631,6 +631,10 @@ huffman_error huffman_context_base::assign_canonical_codes() uint32_t nextstart = (curstart + bithisto[codelen]) >> 1; if (codelen != 1 && nextstart * 2 != (curstart + bithisto[codelen])) return HUFFERR_INTERNAL_INCONSISTENCY; + // Discpress: more codes than one bit has room for (a damaged or crafted tree): 0.289 gave codes + // more bits than their length, and build_lookup_table wrote past the end of its table + if (codelen == 1 && (curstart + bithisto[codelen]) > 2) + return HUFFERR_INTERNAL_INCONSISTENCY; bithisto[codelen] = curstart; curstart = nextstart; } @@ -654,6 +658,7 @@ huffman_error huffman_context_base::assign_canonical_codes() void huffman_context_base::build_lookup_table() { // iterate over all codes + uint32_t covered = 0; for (int curcode = 0; curcode < m_numcodes; curcode++) { // process all nodes which have non-zero bits @@ -669,8 +674,14 @@ void huffman_context_base::build_lookup_table() lookup_value *destend = &m_lookup[((node.m_bits + 1) << shift) - 1]; while (dest <= destend) *dest++ = value; + covered = std::max(covered, (node.m_bits + 1) << shift); } } + + // Discpress: the entries no code covers decode as code 0 of no bits. Canonical codes cover the table + // from 0 up, all of it for a complete tree (what an encoder writes); 0.289 left the rest as it was, + // whatever was in that memory, and a damaged tree's data read bit counts from it + std::fill(m_lookup + covered, m_lookup + (uint32_t(1) << m_maxbits), lookup_value(0)); } diff --git a/scripts/fuzz.sh b/scripts/fuzz.sh new file mode 100755 index 0000000..ab6bece --- /dev/null +++ b/scripts/fuzz.sh @@ -0,0 +1,157 @@ +#!/usr/bin/env bash +# Fuzzes the engine with libFuzzer, under AddressSanitizer and UndefinedBehaviorSanitizer +# (tests/fuzz/README.md: what each fuzzer feeds the engine, and how to read a finding). +# +# scripts/fuzz.sh build the fuzzers (build/fuzz/) and their seeds (build/fuzz/seeds/) +# scripts/fuzz.sh run [seconds] fuzz (default 300 s each); new inputs go to build/fuzz/corpus/, +# findings to build/fuzz/findings/; with "all", the four run at once +# scripts/fuzz.sh check every seed, past finding (tests/fuzz/regressions) and corpus input +# once: fails on any finding (CI) +# scripts/fuzz.sh repro one input, with the engine's output +# scripts/fuzz.sh prune the corpus without the inputs that add no coverage +# +# The fuzzers are chd, desc, codec and ecm. The engine is built with __EMSCRIPTEN__ defined, so that its +# browser-only code is in, with tests/fuzz/host.cpp doing what the page's JavaScript does for it. +# Needs clang with its libFuzzer runtime (Debian and Ubuntu: clang and libclang-rt--dev) and make. +set -euo pipefail +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +JOBS="${JOBS:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)}" +CC="${CC:-clang}" +CXX="${CXX:-clang++}" +OUT="${FUZZ_OUT:-$ROOT/build/fuzz}" +FUZZERS=(chd desc codec ecm) + +# (without clang's check of calls through a pointer to another function type, which gcc's UBSan, as in +# scripts/build-sanitized.sh, doesn't have: the LZMA SDK calls its match finder's functions through +# pointers that take void *, which every ABI makes the same call, WebAssembly's call_indirect included) +SAN="-fsanitize=address,undefined -fno-sanitize=function -fno-sanitize-recover=undefined -fno-omit-frame-pointer" +# -Dmain=...: chdman's main gives way to libFuzzer's (host.cpp runs commands with chdman_begin) +OPT="-O1 -g $SAN -fsanitize=fuzzer-no-link -D__EMSCRIPTEN__ -Dmain=chdman_native_main -I$ROOT/tests/fuzz/shim" +# the native build of every engine source, wasm_helper.cpp and ecm.cpp too +MAKEVARS=(T=native "OPT=$OPT" "CC=$CC" "CXX=$CXX" "O=$OUT/obj" 'CPP_SRC=$(UTIL_SRC) $(OSD_SRC) $(TOOL_SRC)') + +# failing to allocate more than 1 GB at once (as a phone's browser does) must be an error, not a crash; +# leaks that unmodified chdman 0.289 has too are suppressed, as for the sanitized build +export ASAN_OPTIONS="${ASAN_OPTIONS:-allocator_may_return_null=1:max_allocation_size_mb=1024:detect_stack_use_after_return=1:strict_string_checks=1}" +export LSAN_OPTIONS="${LSAN_OPTIONS:-suppressions=$ROOT/tests/support/lsan.supp}" +export UBSAN_OPTIONS="${UBSAN_OPTIONS:-print_stacktrace=1}" +FUZZ_OPTS=(-timeout=25 -rss_limit_mb=2560 -malloc_limit_mb=2048) + +build() { + mkdir -p "$OUT" + # objects built with other flags are built again (the Makefile only tracks sources and headers) + if [ "$(cat "$OUT/obj/flags" 2>/dev/null)" != "$OPT $CC $CXX" ]; then rm -rf "$OUT/obj"; mkdir -p "$OUT/obj"; echo "$OPT $CC $CXX" > "$OUT/obj/flags"; fi + make -s -C "$ROOT/wasm" -j"$JOBS" "${MAKEVARS[@]}" objs + local cxxflags + # (the engine's flags, but for the main rename: seeds.cpp has its own) + cxxflags="$(make -s -C "$ROOT/wasm" "${MAKEVARS[@]}" --eval 'print-cxxflags: ; @echo $(CXXFLAGS)' print-cxxflags)" + cxxflags="${cxxflags//-Dmain=chdman_native_main/}" + local objs + mapfile -t objs < "$OUT/obj/objs.rsp" + local f + for f in host "${FUZZERS[@]/#/fuzz_}" seeds; do + # shellcheck disable=SC2086 + $CXX $cxxflags -c "$ROOT/tests/fuzz/$f.cpp" -o "$OUT/obj/$f.o" & + done + wait + for f in "${FUZZERS[@]}"; do + # shellcheck disable=SC2086 + $CXX $SAN -fsanitize=fuzzer "$OUT/obj/fuzz_$f.o" "$OUT/obj/host.o" "${objs[@]}" -o "$OUT/$f" -lpthread & + done + # shellcheck disable=SC2086 + $CXX $SAN "$OUT/obj/seeds.o" "$OUT/obj/host.o" "${objs[@]}" -o "$OUT/seeds-tool" -lpthread & + wait + rm -rf "$OUT/seeds" + ASAN_OPTIONS="$ASAN_OPTIONS:detect_leaks=0" "$OUT/seeds-tool" "$OUT/seeds" > /dev/null 2> "$OUT/seeds.log" || { cat "$OUT/seeds.log" >&2; exit 1; } + echo "fuzzers: ${FUZZERS[*]/#/$OUT/}; seeds: $(find "$OUT/seeds" -type f | wc -l) in $OUT/seeds" +} + +names() { + if [ "${1:-}" = all ]; then echo "${FUZZERS[@]}"; return; fi + local f + for f in "${FUZZERS[@]}"; do [ "$f" = "${1:-}" ] && { echo "$f"; return; }; done + echo "unknown fuzzer '${1:-}' (${FUZZERS[*]} or all)" >&2 + exit 2 +} + +need_build() { + local f + for f in "$@"; do [ -x "$OUT/$f" ] || { echo "no $OUT/$f: scripts/fuzz.sh build first" >&2; exit 1; }; done +} + +# libFuzzer writes new inputs into the first folder; the seeds are only read +run() { + local secs="${2:-300}" pids=() status=0 f + local list + read -ra list <<< "$(names "$1")" + need_build "${list[@]}" + mkdir -p "$OUT/findings" + for f in "${list[@]}"; do + mkdir -p "$OUT/corpus/$f" + "$OUT/$f" "${FUZZ_OPTS[@]}" -max_total_time="$secs" -close_fd_mask=3 -print_final_stats=1 \ + -artifact_prefix="$OUT/findings/$f-" "$OUT/corpus/$f" "$OUT/seeds/$f" $([ -d "$ROOT/tests/fuzz/regressions/$f" ] && echo "$ROOT/tests/fuzz/regressions/$f") > "$OUT/$f.log" 2>&1 & + pids+=($!) + done + for i in "${!pids[@]}"; do + f="${list[$i]}" + if wait "${pids[$i]}"; then + echo "$f: $(grep -m1 -o 'stat::number_of_executed_units: *[0-9]*' "$OUT/$f.log" | grep -o '[0-9]*$') runs, corpus $(find "$OUT/corpus/$f" -type f | wc -l), no findings" + else + status=1 + echo "$f: FINDING (log: $OUT/$f.log)" + grep -m3 -E 'ERROR:|runtime error:|SUMMARY:|keeps pausing|Test unit written' "$OUT/$f.log" || tail -20 "$OUT/$f.log" + fi + done + return $status +} + +check() { + local status=0 f dirs + local list + read -ra list <<< "$(names "$1")" + need_build "${list[@]}" + mkdir -p "$OUT/findings" + for f in "${list[@]}"; do + dirs=("$OUT/seeds/$f") + [ -d "$ROOT/tests/fuzz/regressions/$f" ] && dirs+=("$ROOT/tests/fuzz/regressions/$f") + [ -d "$OUT/corpus/$f" ] && dirs+=("$OUT/corpus/$f") + if "$OUT/$f" "${FUZZ_OPTS[@]}" -runs=0 -close_fd_mask=3 -artifact_prefix="$OUT/findings/$f-" "${dirs[@]}" > "$OUT/$f-check.log" 2>&1; then + echo "$f: $(find "${dirs[@]}" -type f | wc -l) inputs, no findings" + else + status=1 + echo "$f: FINDING (log: $OUT/$f-check.log)" + grep -m3 -E 'ERROR:|runtime error:|SUMMARY:|keeps pausing' "$OUT/$f-check.log" || tail -20 "$OUT/$f-check.log" + fi + done + return $status +} + +# keeps only the corpus inputs that add coverage (libFuzzer's merge), so the saved corpus doesn't grow forever +prune() { + local f + local list + read -ra list <<< "$(names "$1")" + need_build "${list[@]}" + for f in "${list[@]}"; do + [ -d "$OUT/corpus/$f" ] || continue + rm -rf "$OUT/corpus/$f.new" + mkdir -p "$OUT/corpus/$f.new" + "$OUT/$f" "${FUZZ_OPTS[@]}" -merge=1 -close_fd_mask=3 "$OUT/corpus/$f.new" "$OUT/corpus/$f" > "$OUT/$f-prune.log" 2>&1 \ + || { echo "$f: merge failed (log: $OUT/$f-prune.log)"; return 1; } + echo "$f: corpus $(find "$OUT/corpus/$f" -type f | wc -l) -> $(find "$OUT/corpus/$f.new" -type f | wc -l)" + rm -rf "$OUT/corpus/$f" + mv "$OUT/corpus/$f.new" "$OUT/corpus/$f" + done +} + +case "${1:-}" in + build) build ;; + run) run "${2:-all}" "${3:-300}" ;; + check) check "${2:-all}" ;; + prune) prune "${2:-all}" ;; + repro) + [ $# -eq 3 ] || { echo "usage: scripts/fuzz.sh repro " >&2; exit 2; } + need_build "$(names "$2")" + exec "$OUT/$2" "${FUZZ_OPTS[@]}" "$3" ;; + *) sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; exit 2 ;; +esac diff --git a/tests/fuzz/README.md b/tests/fuzz/README.md new file mode 100644 index 0000000..78af072 --- /dev/null +++ b/tests/fuzz/README.md @@ -0,0 +1,97 @@ +# Fuzzing the engine + +libFuzzer feeds the engine damaged and made-up files under AddressSanitizer and UndefinedBehaviorSanitizer, +to find what a file can make it do wrong: read or write outside its memory, do what C++ leaves undefined, +hang, or leak. Each finding becomes an input in `regressions/`, and the fix goes into `engine/` like any +other change (`scripts/engine-diff.sh`, `./build.sh`). + +```sh +./scripts/fuzz.sh build # build/fuzz/{chd,desc,codec,ecm}, and their seeds in build/fuzz/seeds/ +./scripts/fuzz.sh run all 600 # the four at once, 10 minutes each; new inputs go to build/fuzz/corpus/ +./scripts/fuzz.sh check all # every seed, regression and corpus input once (CI) +./scripts/fuzz.sh repro chd build/fuzz/findings/chd-crash-… # one input, with the engine's messages +./scripts/fuzz.sh prune all # the corpus without inputs that add nothing (CI, after a run) +``` + +It needs clang and its libFuzzer runtime (Debian and Ubuntu: `clang` and `libclang-rt--dev`). +`.github/workflows/fuzz.yml` runs it weekly for 30 minutes per fuzzer, on demand, and for 5 minutes when +`engine/`, `wasm/` or the fuzzers change; it keeps the corpus between runs in the Actions cache and uploads +a finding with the run. + +## What each fuzzer feeds the engine + +- **`chd`** (`fuzz_chd.cpp`): a CHD file, read the ways the page reads one. Identification first, with + `wasm/wasm_helper.cpp`'s probe functions as `app/ident.js` calls them (the tracks, the first sectors, the + volume descriptor and the directory it names). Then, for a CHD whose data is at most 8 MB, chdman's `info`, + `verify` and every extract command (`extractraw`, `extracthd`, `extractdvd`, `extractcd` to a cue sheet, a + TOC, a GDI and with `--redump`, `extractld`). +- **`desc`** (`fuzz_desc.cpp`): a cue sheet, cdrdao TOC, GDI, Nero image or ISO (the first byte picks the + kind and the options), parsed with `cdrom_file::parse_toc`; one whose tracks are short is made into a CD + CHD with `createcd` (stored, or with the default codecs), then verified and extracted again. Its folder + holds the track files the seeds name: `t.bin`, `a.bin`, `a.wav`, `odd.bin` and `empty.bin`. +- **`codec`** (`fuzz_codec.cpp`): one compressed hunk for each codec a CHD can use, decompressed as a + helper worker does (`wasm_helper_dinit`, `wasm_helper_decompress`). +- **`ecm`** (`fuzz_ecm.cpp`): an ECM image's stored bytes, rebuilt into sectors a window at a time as the + job worker asks for them (`wasm/ecm.cpp`'s `wasm_ecm_decode`), in buffers of exactly their size. + +The engine is built with `__EMSCRIPTEN__` defined, so the browser-only code is fuzzed too: the read-ahead of +extract and verify (`chd_file::wasm_read_ahead`), the probe and helper functions, the ECM decoder. +`host.cpp` stands in for the page's JavaScript (`wasm/par_lib.js`, `app/worker.js`): it runs chdman +commands as the job worker does (`chdman_begin`, then `chdman_resume` while chdman pauses), and its +"helpers" decompress the hunks chdman reads ahead, in one of four ways chosen by the input's size: none, +all of them, all but one in three (as a helper that traps), or in reverse order. A command that pauses +again and again with nothing left to wait for aborts: in the browser it would spin forever. Compression +stays in the engine, as on a page without helper workers. + +Allocating more than 1 GB at once fails (`max_allocation_size_mb`, as a phone's browser would) and must be +an error, not a crash. Leaks unmodified chdman 0.289 has too are suppressed (`tests/support/lsan.supp`). +clang's check of calls through a pointer to another function type is off (`-fno-sanitize=function`): the +LZMA SDK calls its match finder through pointers that take `void *`, which every ABI, WebAssembly's +included, makes the same call. gcc's sanitizers (`scripts/build-sanitized.sh`) don't have that check. + +`seeds.cpp` writes the seeds with the engine itself: CHDs of every kind and codec (v3 and v4 by hand, +since chdman no longer writes them), descriptors of every kind, compressed hunks and ECM chunks. They are +synthetic, like the test fixtures. + +## Findings + +Fixed, each with its input in `regressions/`: + +- **A Huffman tree with more codes than one bit has room for** (`codec/huffman-one-bit-overfull`): codes + longer than their length, and a lookup table written past its end, on the stack for the small tree a + `huff` hunk starts with. chdman 0.289 has it, in the hunks' `huff` codec and in a v5 CHD's map + (`huffman.cpp`'s `assign_canonical_codes`; `engine.spec.js` tests the map). +- **A CD codec's sector data longer than its hunk** (`codec/cd-base-longer-than-hunk`): the base codec + decoded past the end of the hunk's data, and the subcode's length wrapped (`chdcodec.cpp`; chdman 0.289 + has it). +- **A LaserDisc frame larger than its hunk** (`codec/avhuff-frame-larger-than-hunk`): decoded past the end + of the hunk; also more than 16 audio channels, and raw audio shorter than its samples (`avhuff.cpp`, + `chdcodec.cpp`; chdman 0.289 has them). +- **A map larger than the file** (`chd/map-past-end`): up to 4 GB allocated before the read that fails. + Looking at it turned up a map size computed in 32 bits, which wraps at 358 million hunks, so that the + map is written past its end (`chd.cpp`; chdman 0.289 segfaults, `engine.spec.js`). +- **A map's bit widths over 32** (`chd/map-bits-over-32`): the bit reader shifted by a negative count. +- **A hunk compressed with a codec the CHD has none for** (`chd/codec-missing`): a v3 CHD without + compression whose map says a hunk is compressed, or a v5 map that names an empty codec slot; a call + through a null pointer (`chd.cpp`; chdman 0.289 segfaults, `engine.spec.js`). +- **A hunk in a parent the CHD doesn't have** (`chd/parent-missing`): a map entry for a parent's hunk + in a CHD without a parent, read through a null pointer (`chd.cpp`; chdman 0.289 segfaults, + `engine.spec.js`). +- **CD metadata with a string longer than 15 characters** (`chd/metadata-long-string`): read with `%s` + into 16-byte buffers on the stack (`cdrom.cpp`'s `parse_metadata`; chdman 0.289 has it, as does every + reader that took MAME's parser). +- **CD metadata without one of its tracks** (a corpus input; `engine.spec.js` names a track twice): a + track of 0-byte sectors, which `extractcd` divided by. The old binary CD metadata (`CHCD`) was also read + without checking its length, its number of tracks or their sector sizes (`cdrom.cpp`'s `parse_metadata`; + chdman 0.289 has both). +- **A read of 0 bytes** (`chd/read-0-bytes`), as identification makes of a CD track that has no metadata + (the next track's is there): the last hunk wrapped around, and a whole hunk was read into a sector's + buffer, on the stack (`chd_file::read_bytes`; chdman 0.289 has it). +- **A Huffman tree that leaves codes uncovered** (`chd/huffman-uncovered-codes`): the lookup table's + entries no code covers were never set, and decoding took bit counts from whatever that memory held, + then shifted by more than 32 (`huffman.cpp`'s `build_lookup_table`; chdman 0.289 has it). +- **A compressed map of 0 bytes** (`chd/map-length-0`), and metadata of 0 bytes: element 0 of an empty + vector, which C++ leaves undefined (`chd.cpp`). +- Found while writing the seeds: the bit writer shifted a 32-bit value by 32 when a map has no copies of + hunks (`bitstream.h`, unused result), and libFLAC added an offset to a null pointer when it doesn't use + escape codes (`stream_encoder.c`). Neither changes a byte of output. diff --git a/tests/fuzz/fuzz_chd.cpp b/tests/fuzz/fuzz_chd.cpp new file mode 100644 index 0000000..0be011d --- /dev/null +++ b/tests/fuzz/fuzz_chd.cpp @@ -0,0 +1,137 @@ +// Fuzzer: a CHD file, read the ways the page reads one it is given (tests/fuzz/README.md). +// Identification first (wasm/wasm_helper.cpp's probe functions, as app/ident.js uses them: the +// tracks, the first sectors, the volume descriptor and the directory it points to), then, for a +// CHD small enough to read whole quickly, the commands that read a CHD: info, verify and every +// extract command, with the hunks they read ahead going to helper workers in one of four ways +// (tests/fuzz/host.cpp). + +#include "host.h" + +#include "chd.h" + +#include +#include +#include +#include + +extern "C" { +int wasm_probe_open(const char *path, const char *parent); +int wasm_probe_tracks(); +int wasm_probe_track_info(int track, uint32_t *out); +int wasm_probe_read(int track, uint32_t lba, uint8_t *out); +double wasm_probe_logical(); +} + +namespace { + +std::string s_dir; + +uint32_t get_le32(uint8_t const *p) { return p[0] | (p[1] << 8) | (p[2] << 16) | (uint32_t(p[3]) << 24); } +uint32_t get_be32(uint8_t const *p) { return (uint32_t(p[0]) << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; } +uint64_t get_be64(uint8_t const *p) { return (uint64_t(get_be32(p)) << 32) | get_be32(p + 4); } + +// The engine allocates a map entry for every hunk the header claims and buffers of a hunk's size. A +// claim larger than a phone's browser can allocate fails there with std::bad_alloc, an error; but +// AddressSanitizer reports any failed new as a crash, so such inputs are left out. +bool too_large(uint8_t const *d, size_t n) +{ + if ((n < 124) || std::memcmp(d, "MComprHD", 8)) + return false; + uint64_t hunkbytes, hunks, entrybytes; + switch (get_be32(&d[12])) + { + case 3: hunkbytes = get_be32(&d[76]); hunks = get_be32(&d[24]); entrybytes = 16; break; + case 4: hunkbytes = get_be32(&d[44]); hunks = get_be32(&d[24]); entrybytes = 16; break; + case 5: + hunkbytes = get_be32(&d[56]); + hunks = hunkbytes ? (get_be64(&d[32]) + hunkbytes - 1) / hunkbytes : 0; + entrybytes = get_be32(&d[16]) ? 12 : 4; + break; + default: return false; + } + return (hunkbytes > (64 << 20)) || (hunks * entrybytes > (512 << 20)); +} + +// the ISO 9660 primary volume descriptor's root directory, as identification follows it +void probe_volume(int track) +{ + uint8_t sector[2048]; + for (uint32_t lba = 0; lba < 18; lba++) + wasm_probe_read(track, lba, sector); + if (!wasm_probe_read(track, 16, sector)) + { + uint32_t const root = get_le32(§or[158]); + if (!wasm_probe_read(track, root, sector)) + wasm_probe_read(track, get_le32(§or[2]), sector); + } +} + +void identify(std::string const &path) +{ + int const kind = wasm_probe_open(path.c_str(), nullptr); + if (kind == 1) + { + int const tracks = std::min(wasm_probe_tracks(), 99); + for (int t = 0; t < tracks; t++) + { + uint32_t info[5]; + if (wasm_probe_track_info(t, info)) + continue; + probe_volume(t); + uint8_t sector[2048]; + if (info[1]) + wasm_probe_read(t, info[1] - 1, sector); + wasm_probe_read(t, info[1], sector); + } + } + else if (kind == 2) + { + probe_volume(0); + double const sectors = wasm_probe_logical() / 2048; + uint8_t sector[2048]; + if (sectors >= 1) + wasm_probe_read(0, uint32_t(std::min(sectors - 1, 4e9)), sector); + } + wasm_probe_open("", nullptr); // closes it +} + +} // anonymous namespace + +extern "C" int LLVMFuzzerInitialize(int *, char ***) +{ + s_dir = fuzzhost::make_dir("chd"); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) +{ + if (too_large(data, size)) + return 0; + std::string const in = s_dir + "/in.chd"; + fuzzhost::write_file(in, data, size); + identify(in); + + // the commands read all of it: only for a small CHD + { + chd_file chd; + if (chd.open(in)) + return 0; + if ((chd.logical_bytes() > (8 << 20)) || (chd.hunk_bytes() > (1 << 20)) || (chd.hunk_count() > 32768)) + return 0; + } + static fuzzhost::helpers const modes[] = { fuzzhost::helpers::none, fuzzhost::helpers::all, fuzzhost::helpers::fail_some, fuzzhost::helpers::reversed }; + fuzzhost::set_helpers(modes[size % 4]); + std::string const out = s_dir + "/out"; + fuzzhost::run({ "info", "-i", in, "-v" }); + fuzzhost::run({ "verify", "-i", in }); + fuzzhost::run({ "extractraw", "-i", in, "-o", out + ".raw", "-f" }); + fuzzhost::run({ "extracthd", "-i", in, "-o", out + ".img", "-f" }); + fuzzhost::run({ "extractdvd", "-i", in, "-o", out + ".iso", "-f" }); + fuzzhost::run({ "extractcd", "-i", in, "-o", out + ".cue", "-ob", out + ".bin", "-f" }); + // the other descriptors, split tracks, and the cue sheet and checksums Redump has (app/ui.js's redumpCheck) + fuzzhost::run({ "extractcd", "-i", in, "-o", out + ".toc", "-f" }); + fuzzhost::run({ "extractcd", "-i", in, "-o", out + ".gdi", "-f" }); + fuzzhost::run({ "extractcd", "-i", in, "-o", out + "-r.cue", "-sb", "--redump", "-f" }); + fuzzhost::run({ "extractld", "-i", in, "-o", out + ".avi", "-f" }); + return 0; +} diff --git a/tests/fuzz/fuzz_codec.cpp b/tests/fuzz/fuzz_codec.cpp new file mode 100644 index 0000000..153522a --- /dev/null +++ b/tests/fuzz/fuzz_codec.cpp @@ -0,0 +1,39 @@ +// Fuzzer: one compressed hunk, decompressed by a codec as a helper worker does for extract and verify +// (wasm/wasm_helper.cpp's wasm_helper_dinit and wasm_helper_decompress; tests/fuzz/README.md). Byte 0 +// picks the codec, byte 1 the hunk size; the rest is the hunk's stored bytes. + +#include "chd.h" +#include "chdcodec.h" + +#include +#include + +extern "C" { +int wasm_helper_dinit(uint32_t hunkbytes, uint32_t unitbytes, uint32_t c0, uint32_t c1, uint32_t c2, uint32_t c3); +uint8_t *wasm_helper_dinbuf(); +uint8_t *wasm_helper_doutbuf(); +int wasm_helper_decompress(uint32_t codec, uint32_t length); +} + +// (the order tests/fuzz/seeds.cpp writes its seeds in) +static chd_codec_type const s_codecs[] = { CHD_CODEC_ZLIB, CHD_CODEC_ZSTD, CHD_CODEC_LZMA, CHD_CODEC_HUFFMAN, CHD_CODEC_FLAC, + CHD_CODEC_CD_ZLIB, CHD_CODEC_CD_ZSTD, CHD_CODEC_CD_LZMA, CHD_CODEC_CD_FLAC, CHD_CODEC_AVHUFF }; + +extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) +{ + if (size < 2) + return 0; + chd_codec_type const codec = s_codecs[data[0] % std::size(s_codecs)]; + bool const cd = (codec == CHD_CODEC_CD_ZLIB) || (codec == CHD_CODEC_CD_ZSTD) || (codec == CHD_CODEC_CD_LZMA) || (codec == CHD_CODEC_CD_FLAC); + // CD hunks are whole frames of 2,448 bytes; other hunks a multiple of 512 bytes (of 4 for flac's samples) + uint32_t const unitbytes = cd ? 2448 : 512; + uint32_t const hunkbytes = (1 + data[1] % 32) * unitbytes; + if (wasm_helper_dinit(hunkbytes, unitbytes, codec, 0, 0, 0) != 0) + return 0; + uint32_t const length = uint32_t(size - 2); + if (length > hunkbytes) // a helper doesn't take it (the job worker reports it) + return 0; + std::memcpy(wasm_helper_dinbuf(), data + 2, length); + wasm_helper_decompress(0, length); + return 0; +} diff --git a/tests/fuzz/fuzz_desc.cpp b/tests/fuzz/fuzz_desc.cpp new file mode 100644 index 0000000..86a45ab --- /dev/null +++ b/tests/fuzz/fuzz_desc.cpp @@ -0,0 +1,99 @@ +// Fuzzer: a disc descriptor or image chdman reads (tests/fuzz/README.md): a cue sheet, a cdrdao TOC, +// a GDI, a Nero image or an ISO. The first byte picks which, and the options; the rest is the file. +// It is parsed (cdrom_file::parse_toc), and if its tracks are short, made into a CD CHD with chdman, +// which is then verified and extracted again. The folder holds the track files the seeds name +// (tests/fuzz/seeds.cpp): t.bin, a.bin, a.wav, odd.bin and empty.bin. + +#include "host.h" + +#include "cdrom.h" + +#include +#include +#include +#include + +namespace { + +std::string s_dir; + +std::vector pattern(size_t n, unsigned seed) +{ + std::vector v(n); + uint32_t x = seed * 2654435761u + 1; + for (size_t i = 0; i < n; i++) + { + x = x * 1103515245u + 12345u; + v[i] = uint8_t(x >> 16); + } + return v; +} + +// a RIFF WAVE file: 16-bit stereo at 44.1 kHz, frames of CD audio +std::vector wave(uint32_t frames) +{ + uint32_t const bytes = frames * 2352; + std::vector v{ 'R','I','F','F', 0,0,0,0, 'W','A','V','E', 'f','m','t',' ', 16,0,0,0, 1,0, 2,0, 0x44,0xac,0,0, 0x10,0xb1,2,0, 4,0, 16,0, 'd','a','t','a', 0,0,0,0 }; + auto put = [&v](size_t at, uint32_t n) { for (int i = 0; i < 4; i++) v[at + i] = uint8_t(n >> (8 * i)); }; + put(4, 36 + bytes); + put(40, bytes); + std::vector const pcm = pattern(bytes, 7); + v.insert(v.end(), pcm.begin(), pcm.end()); + return v; +} + +} // anonymous namespace + +extern "C" int LLVMFuzzerInitialize(int *, char ***) +{ + s_dir = fuzzhost::make_dir("desc"); + fuzzhost::write_file(s_dir + "/t.bin", pattern(40 * 2352, 1)); + fuzzhost::write_file(s_dir + "/a.bin", pattern(12 * 2352, 2)); + fuzzhost::write_file(s_dir + "/a.wav", wave(10)); + fuzzhost::write_file(s_dir + "/odd.bin", pattern(1000, 3)); + fuzzhost::write_file(s_dir + "/empty.bin", nullptr, 0); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) +{ + if (size < 1) + return 0; + // a descriptor that names a device or a FIFO would block (the page only ever gives chdman files) + std::string const text(reinterpret_cast(data + 1), size - 1); + if ((text.find("/dev") != std::string::npos) || (text.find("/proc") != std::string::npos) || (text.find("/sys") != std::string::npos)) + return 0; + + // byte 0: bits 0-2 the kind of file, bit 3 --keepcue, bits 4-5 what extractcd writes, bit 6 compression + static char const *const kinds[] = { "cue", "toc", "gdi", "nrg", "iso", "cue", "toc", "cue" }; + std::string const kind = kinds[data[0] & 7]; + std::string const in = s_dir + "/in." + kind; + fuzzhost::write_file(in, data + 1, size - 1); + + cdrom_file::toc toc = {}; + cdrom_file::track_input_info info; + if (cdrom_file::parse_toc(in, toc, info) || (toc.numtrks == 0)) + return 0; + // chdman writes every frame the descriptor says the tracks have: only a few of them + uint64_t frames = 0; + for (uint32_t t = 0; t < toc.numtrks && t < cdrom_file::MAX_TRACKS; t++) + frames += uint64_t(toc.tracks[t].frames) + toc.tracks[t].padframes + toc.tracks[t].extraframes + toc.tracks[t].splitframes + toc.tracks[t].pregap + toc.tracks[t].postgap; + if (frames > 2000) + return 0; + + std::string const chd = s_dir + "/out.chd", out = s_dir + "/x"; + std::vector create{ "createcd", "-i", in, "-o", chd, "-f", "-np", "1", "-c", ((data[0] & 0x40) && frames <= 200) ? "cdlz,cdzl,cdfl" : "none" }; + if ((data[0] & 8) && (kind == "cue")) + create.push_back("--keepcue"); + if (fuzzhost::run(create) != 0) + return 0; + fuzzhost::set_helpers(fuzzhost::helpers::all); + fuzzhost::run({ "verify", "-i", chd }); + static char const *const outs[] = { ".cue", ".toc", ".gdi", "-r.cue" }; + int const o = (data[0] >> 4) & 3; + std::vector extract{ "extractcd", "-i", chd, "-o", out + outs[o], "-f" }; + if (o == 3) + extract.insert(extract.end(), { "--redump", "-sb" }); + fuzzhost::run(extract); + return 0; +} diff --git a/tests/fuzz/fuzz_ecm.cpp b/tests/fuzz/fuzz_ecm.cpp new file mode 100644 index 0000000..a9f5483 --- /dev/null +++ b/tests/fuzz/fuzz_ecm.cpp @@ -0,0 +1,35 @@ +// Fuzzer: the stored bytes of an ECM image, rebuilt into CD sectors as the job worker asks for a +// window of the image (wasm/ecm.cpp's wasm_ecm_decode; tests/fuzz/README.md). Byte 0 says where the +// window starts: at a chunk header, or inside a chunk of the type in bits 0-1 with byte 3 sectors +// (or bytes) left in it; bytes 1-2 the window's size in bytes. The rest are the stored bytes, in a +// buffer of exactly their size, as is the window, so that a read or write past either is caught. + +#include +#include +#include +#include +#include + +extern "C" { +int wasm_ecm_decode(uint8_t const *in, uint32_t in_len, uint8_t *out, uint32_t out_len, uint32_t type, uint32_t left); +uint32_t wasm_ecm_edc(uint32_t edc, uint8_t const *p, uint32_t n); +} + +extern "C" int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) +{ + if (size < 4) + return 0; + uint32_t const type = data[0] & 3; + uint32_t const left = (data[0] & 4) ? (1 + data[3]) : 0; + uint32_t const out_len = data[1] | (data[2] << 8); + std::vector in(data + 4, data + size), out(out_len); + int const used = wasm_ecm_decode(in.data(), uint32_t(in.size()), out.data(), out_len, type, left); + if ((used < -1) || (used > int(in.size()))) + { + std::fprintf(stderr, "wasm_ecm_decode used %d of %zu bytes\n", used, in.size()); + std::abort(); + } + if (used >= 0) + wasm_ecm_edc(0, out.data(), out_len); + return 0; +} diff --git a/tests/fuzz/host.cpp b/tests/fuzz/host.cpp new file mode 100644 index 0000000..8f9516b --- /dev/null +++ b/tests/fuzz/host.cpp @@ -0,0 +1,188 @@ +// The page's side of the engine's browser-only hooks (wasm/par_lib.js and app/worker.js), natively: +// the fuzzers build the engine with __EMSCRIPTEN__ defined (scripts/fuzz.sh) and link this instead of +// the JavaScript. Compression stays in the engine (no helper workers: wasm_par_enabled says no); +// extract and verify hand the hunks they read ahead to "helpers" here, which decompress them with +// wasm/wasm_helper.cpp's functions, as the page's helper workers do, when chdman pauses. + +#include "host.h" + +#include "chd.h" + +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +// the engine's exports (chdman.cpp, chd.cpp, wasm_helper.cpp) +int chdman_begin(int argc, char *argv[]); +int chdman_resume(); +uint8_t *wasm_rd_slot(chd_file *chd, uint32_t hunknum); +void wasm_rd_done(chd_file *chd, uint32_t hunknum, int ok); +int wasm_helper_dinit(uint32_t hunkbytes, uint32_t unitbytes, uint32_t c0, uint32_t c1, uint32_t c2, uint32_t c3); +uint8_t *wasm_helper_dinbuf(); +uint8_t *wasm_helper_doutbuf(); +int wasm_helper_decompress(uint32_t codec, uint32_t length); +} + +namespace { + +struct pending +{ + uint32_t hunk; + uint32_t codec; + std::vector data; +}; + +fuzzhost::helpers s_mode = fuzzhost::helpers::all; +chd_file const *s_chd = nullptr; // the one CHD the helpers decompress for (M.rdSetup: one at a time) +uint32_t s_hunkbytes = 0; +bool s_dead = false; // the helpers' codec setup failed: every hunk comes back failed +std::vector s_queue; +unsigned s_count = 0; + +// answers every hunk handed out so far (app/worker.js: rdDispatch, the helper role's dbatch, rdResult) +bool deliver() +{ + if (s_queue.empty()) + return false; + std::vector queue = std::move(s_queue); + s_queue.clear(); + if (s_mode == fuzzhost::helpers::reversed) + std::reverse(queue.begin(), queue.end()); + for (pending const &p : queue) + { + bool ok = !s_dead && (p.data.size() <= s_hunkbytes); + if (ok) + { + std::memcpy(wasm_helper_dinbuf(), p.data.data(), p.data.size()); + ok = wasm_helper_decompress(p.codec, uint32_t(p.data.size())) == 0; + } + if ((s_mode == fuzzhost::helpers::fail_some) && ((s_count++ % 3) == 0)) + ok = false; + chd_file *const chd = const_cast(s_chd); + uint8_t *const slot = ok ? wasm_rd_slot(chd, p.hunk) : nullptr; + if (slot) + std::memcpy(slot, wasm_helper_doutbuf(), s_hunkbytes); + wasm_rd_done(chd, p.hunk, slot ? 1 : 0); + } + return true; +} + +} // anonymous namespace + + +// wasm/par_lib.js's functions, as the page answers them +extern "C" { + +int wasm_par_enabled(uint32_t, uint32_t, const uint32_t *, uint32_t) { return 0; } + +void wasm_par_submit(void *, const uint8_t *, uint32_t, uint32_t) +{ + std::fprintf(stderr, "wasm_par_submit without helpers\n"); + std::abort(); +} + +int wasm_par_active(void) { return 0; } + +int wasm_out_full(void) { return 0; } + +int wasm_rd_enabled(const void *chd, uint32_t hunkbytes, uint32_t unitbytes, const uint32_t *compression) +{ + if ((s_mode == fuzzhost::helpers::none) || s_chd) + return 0; + for (int i = 0; i < 4; i++) + if (compression[i] == CHD_MAKE_TAG('a','v','h','u')) // the LaserDisc codec stays with chdman + return 0; + s_chd = static_cast(chd); + s_hunkbytes = hunkbytes; + s_queue.clear(); + // a helper whose setup fails is dropped, and the job worker gets every hunk back failed + s_dead = wasm_helper_dinit(hunkbytes, unitbytes, compression[0], compression[1], compression[2], compression[3]) != 0; + return 1; +} + +void wasm_rd_submit(const void *chd, uint32_t hunknum, uint32_t codec, const uint8_t *data, uint32_t length) +{ + if (chd != s_chd) + return; + s_queue.push_back(pending{ hunknum, codec, std::vector(data, data + length) }); +} + +void wasm_rd_close(const void *chd) +{ + if (chd != s_chd) + return; + s_queue.clear(); + s_chd = nullptr; +} + +} // extern "C" + + +namespace fuzzhost { + +void set_helpers(helpers mode) +{ + s_mode = mode; +} + +int run(std::vector const &args) +{ + std::vector words{ "chdman" }; + words.insert(words.end(), args.begin(), args.end()); + std::vector argv; + for (std::string &w : words) + argv.push_back(w.data()); + argv.push_back(nullptr); + int code = chdman_begin(int(words.size()), argv.data()); + unsigned idle = 0; + while (code == -1) + { + if (deliver()) + idle = 0; + else if (++idle > 1000) + { + std::fprintf(stderr, "chdman %s keeps pausing with no hunk to wait for\n", args.empty() ? "" : args[0].c_str()); + std::abort(); + } + code = chdman_resume(); + } + s_queue.clear(); + return code; +} + +std::string make_dir(char const *name) +{ + char const *base = std::getenv("TMPDIR"); + struct stat st; + if (!base || !*base) + base = (stat("/dev/shm", &st) == 0) ? "/dev/shm" : "/tmp"; + std::string templ = std::string(base) + "/discpress-" + name + "-XXXXXX"; + if (!mkdtemp(templ.data())) + { + std::perror("mkdtemp"); + std::abort(); + } + return templ; +} + +void write_file(std::string const &path, uint8_t const *data, size_t size) +{ + std::FILE *f = std::fopen(path.c_str(), "wb"); + if (!f || (size && std::fwrite(data, 1, size, f) != size) || std::fclose(f)) + { + std::perror(path.c_str()); + std::abort(); + } +} + +void write_file(std::string const &path, std::vector const &data) +{ + write_file(path, data.data(), data.size()); +} + +} // namespace fuzzhost diff --git a/tests/fuzz/host.h b/tests/fuzz/host.h new file mode 100644 index 0000000..3dbb44b --- /dev/null +++ b/tests/fuzz/host.h @@ -0,0 +1,29 @@ +// What the page does for the engine in the browser, natively, for the fuzzers (tests/fuzz/README.md). +#pragma once + +#include +#include +#include +#include + +namespace fuzzhost { + +// how the stand-in for the page's helper workers handles the hunks that extract and verify read +// ahead (chd_file::wasm_read_ahead, app/worker.js's M.rdSubmit): none (chdman decompresses them by +// itself, as without helpers), each one decompressed, one in three failed (as by a helper that traps), +// or all of them answered in the reverse order +enum class helpers { none, all, fail_some, reversed }; +void set_helpers(helpers mode); + +// runs a chdman command line in this process as the job worker does (app/worker.js's runChdman: +// chdman_begin, then chdman_resume while it pauses, answering the hunks it read ahead in between) +// and returns its exit code. Aborts if chdman pauses again and again with nothing to wait for: +// in the browser it would spin forever. +int run(std::vector const &args); + +// a new folder of its own for a fuzzer's files, in $TMPDIR (else /dev/shm, else /tmp) +std::string make_dir(char const *name); +void write_file(std::string const &path, uint8_t const *data, size_t size); +void write_file(std::string const &path, std::vector const &data); + +} // namespace fuzzhost diff --git a/tests/fuzz/regressions/chd/codec-missing b/tests/fuzz/regressions/chd/codec-missing new file mode 100644 index 0000000000000000000000000000000000000000..b9dd7daf4f5928a82b08c1fe5efe6a029ac6fbfa GIT binary patch literal 13088 zcmeI3RZv}Bv#u8l_uv{>xJz&k?(Xgu+#Q0udkF6C?(V@|776YS!P)t3OYQ$uotsm2 zF1}gSHM)C_?tW*Dt65JCIT3qX2Pa8U007|e#~}aMzaFT6I3NBM|6i>CEcw??2mk>7 zpFXsWm#xvi^8dL)!2Q$xB>0~>z~2S}0oxm`X#JR=2b0j*b;66{ zNY#GH)4^ugLF{;9adL}@_ei^rF9K6Lm^@78iXv?A2qF*$K9rT%5yaX?zo-+PCa4#Z z4==KbPPjo_56K0b?`!xQLFn1I8(XwVhoicy+0Q}^Xdc30h9g0x?F{L;-$zJBEE{F<|I)+kKu08c= zS4osKi=Us7td7u+B$uvb4YQ?Js=+r;xWT!<=+m|Ib4F7<5mI_g(eLzrF4c+_9>@>$ z$EcDMPl;q+(8z||#bKX&sXcen(tIjKH{@8u1wQxNs7XGF!!TE55exYH2qQ5#vK|NA zVslJ*k0eYJi$|jkf4puIdAATrN8GKNfBYrfCMC&bMN&NND^)!_`>m}o~@=uCzU&sn?JdC9C=^;jJ&4gdL3;ZLRF=JLcg*_)NcE;U@*+1v7m=- zadx8pISb0_I^t05Blym&)&@P?H9%6x6AA_XP$7u$ZC>GZTe8nOcMbtDJzshEr4SNL zyqhc{95dj+Zz16VZIQp!g3%+lOUj>w=LHbzfy5HuieX-bVdQ#sv5;DMs*6-J6k^i% z!g92_s`wZ|HmlxDqp1PEG8GsKSin;bLCC5e;zci;wQ4L=p1K>at<7p|>WhTgftuZlh~2yqay+7>DSMdWsGcpEM~4Py;8+K7-g|U^)~qKbm4cYtSS32`SI(2@AI?wU_RsmnfBT)6`EZMg+QxOS*bK!j_! zBB6M0LjY}%wvh_zbJ!X>@m~C%5_hsT;kyiH6;yCbrw$Bj<4nTwp}zW}{_<5nvVR3E zu;D3}O1F%D`Q6$-A7{i|P-D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dwM{0?MOXIT?IvzoS`fRepYh>`{0NX-%UR|DzDuGA@v zA)Xvd%E$!>!T8kghadk<(wDoG#B6mm6FoKow41P}qOZHa3h*8E_ z!-l})P%6t8yPhfGrk0ss(2iFOx0TV}(JAPudP1nQM2+S;HT31re+5fK0RRc(u%&tK zq#VH5rnw{%J2rM!yYQ#`^JNdC`wN|K^=7$Y#qZCM?Xo|GT4Bt)$@FZLQ1(Vh5qr|K z$j^ou)_N2z#za!AGnU*?mNEs@zt?aO)&2Omx{6R8%fx~H90_>l)9w#BinYp|KqG=) zP2j8*gbM)QB-hiVkTe-5!lU+o-q-8E&S*=Ci2yrMpfF{H78n*Rn)lGmxm3}C&)j9vaO}-nNXRHn!`hqmX&69+N6~3qSzYR zCAJE2>ubrRMn((<_yI)H5;$(bbiTVhpoHGLI|~4iV4Rk+Kt?q;@mcUyvv1h}dra1F z*lOcQg>eYp?|(&}Qs10~c$m%XA}P-7`+d!$9m|Bzmvo#}&lw?&JR=(a?I%-vnW;PF z^IsPk{+HJ}4gf-VbpeY1@dm2(==sO>pF0ykIVpLmzu7-4F#bp7o~p8thz!pskgArV z7>}HSsMse1Wg!iaikk4>GxA^T99&%#i=r>5BV(rkT0YrDy8sW z(*K+4Pb?tCe+>EK{2B7+Th&?rzWd)!c?WP(H!-~$`b*kE04xE(NeBQC0tg8O4Fd}g zntA3Xx(cCqs~fbS|FgcB!21j*etTe`@N>Se){7COx@Wo5 O4ZmTaX+CHMa{Lbk>X&i= literal 0 HcmV?d00001 diff --git a/tests/fuzz/regressions/chd/huffman-uncovered-codes b/tests/fuzz/regressions/chd/huffman-uncovered-codes new file mode 100644 index 0000000000000000000000000000000000000000..74a2e7f61acb0437f85b431a40a72437ef6a3a13 GIT binary patch literal 20945 zcmeHvV~{6Lw`SY+v~Andwry+Lw()D**0gQgwrz9T+<)BNjW_oDZrr`^os6hDPexT{ zRGsIXC-ZzsSz$XHdq)WoARr*`e=cwf7c;Ye?El$-q5fB2G2>qkkp6)nK>M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~>$xk#Nwg`VebRZ$7md$voP51Uc8^W*>X<*h8$NQh zES>-9Lsj}KQKt;M8&%(OvK!Cu{OmpNoB45puXS&4b!X|X|4*V#VApXh{F(6j=H|7m zcF)i0SKhiW<9y}!)@=P%d-wc1Y&E6koFmpSW+pB42pNYb%1HE6zeB_?P5$-e%i>>GU$=L~w@<(LUk7|LW4HYvV_i9Sd-L@4ys6#$1I+f7x5tt<$g9lNMzR-^>{!n6ah3_TN}jd8pn$w;L5Khc5Y7R_rzY%B5%`pJQ9S zZR>&OcYdn+|DKJn)zGS+i|@HQPedvLzzZ7AUH%tM~u_L7lkh literal 0 HcmV?d00001 diff --git a/tests/fuzz/regressions/chd/map-length-0 b/tests/fuzz/regressions/chd/map-length-0 new file mode 100644 index 0000000000000000000000000000000000000000..1c73b37780f5596f4ab671aedfa724347dd36606 GIT binary patch literal 48115 zcmeF4V{~Rgx94NqPM+AdZQHhO+eW8j+fF*^*tTtT>~yAI+?jRn+z&G!XHB2AYSnY< z?5cg9->0+of7dQK5eGX*XGu{YARymA4cx@k+vQLHpD$od!G8@X@&Eh5A6wZ#ARy>O z+|$RSG$XoU{oQ~Wm%Z3UH#SvB>Y3~=hTjurjfj=k+g6ynx>G!>|DN}sIuS`#W? zt$z<5sVWPJ$Z#_escI>Tamy))iZK}|3uzFks0sgbL|#E&j7VHrNKQK$butK)^sCz#yPt;1H0|M6>T)1h-*izZ(WEX#eyd zCeXga$(b*7Wd6>H@c*0!1R@3m1odaq2Q?Ace_ade&sxw($SA02=>PX?0i*rcmJm`VmG|)63L=Y)`O#x37_P`9%aaQFMXp2Oi8c@;Url&B@ z5t||~vAHsPkx_G(ivJHCu zY$u57eS0nLtGC3Q$`WNgXdC}C?<8)&8)6d}k0@W?eEbrxLRmoQ_F`FpstVHc)W7qU zqNH-;F8s|SDnPm1`{?uZO`zo5tZ?=t_5;$tSL5ruTC}2iKrBu)T%g&=moy= zB9z^dUgM-+@iw~=eMJvkH`MxBo43-*3huOx+YUr$r%E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DVX#kl^0TH(`{8&QOUS_IW2j+_U~|4+23?e zb5JvA0VMaEu89UUfxy4%iofUN|4m~jC#QMr43NkE9z)<7KmZT`1ONd*;6F)VZ|8T( zW7~gt9y2$uYaaiNc`Q2me$&{$IFFG4dF(&w`vk1z-&0_=O5qCT>b_Y!Y?+NLHcHx! zKSw_D$#zDJ5@O@*r0#dt@xWCKn|0`y&TUPCDEGelFYGcTt<<^NS*b-Anrcz!bjm|E zNXt3VW>{l)$!a9ql-&T`&MHFGnDbMDAY)3LknM<;>T_98#X&6eYdmSDP-M6m!eoV# z%yQ@MMsfBV_b2O~j(JjSmCC+1v1XZ6qa4V*8#~-7g7H0qx$|B`xHaObR!J}eHjZdv z3p-eKf8O>k)yIq1@w3xF&C0(gD)6cR0YCr{_=~{a-tQ78cmD7=`O)0G8S@+Cq*Qct zQ^~(LPM!tgw<3-ncF&KRe0YtfH4~`t{hUY6 zuI?MmeU;#b+PobqVmXtWq*4u5;k|iF1uo3hD9m$CQFs3PqCdAd}wNR-qXRTWAvlZCGI^zQ%N+X zFvUUjqdfVh%54VhWG^Abowtf8J)0j)Xx>?jcNX$_)4TZ1Pu^c^GBpLYPN(V3f?OUB z{_@H;iLp}jaf7pHCZ_HYs?dS?({xayr$V)@7SpLlO7VJk>H&=$^p}&SMGovbWqsyBf5YV95*C|KNMF@&WZ=uCU7h zg7$&;`8tTQa87bqLXy^yK2y!BH5xS)84C+-!iV=vqo|aZhg@99jdN{bbSRGD>df!K zLiM{U3lv^A)xhX4R%f={?!^yLG_fgp99nD#_ZQ{Z(Nr}OGOd?Zr#*&#^wvn*t8(s6 zAJ)(q7(C`S))`@F4EHmh;d!Xp?(2YKXk+*rD+#ZbH8bQ$HUb@+1gq?v=$)SSKiM-k AivR!s literal 0 HcmV?d00001 diff --git a/tests/fuzz/regressions/chd/map-past-end b/tests/fuzz/regressions/chd/map-past-end new file mode 100644 index 0000000000000000000000000000000000000000..06f018f666a84ac17f6be556ab63b15557372399 GIT binary patch literal 5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=!4XBCzoBZ^qPzyMqf9pC{0qk`vX?8ION_JOxrKTMgAKF}_-hXtlYngR#Def&HzE zuQM303CtQ69l8dY*b+mvukgBdvW%cLAk1PW*Fbc9X!Y7vnVq zh1ecvIp!&qfw&Wc{kZ_pP!1@XYuIFM)V9xtv|$CMs0LK(deR@DIuml@ULSAaL}FM5 z@*5%kMu>nAsfbm-G#QMS$AVe^Vg~RGzB_?0PqNcjSm4TK`yEP3847>B@bNoKYC=v| LIxhwP*PH(fYm2T} literal 0 HcmV?d00001 diff --git a/tests/fuzz/regressions/chd/metadata-long-string b/tests/fuzz/regressions/chd/metadata-long-string new file mode 100644 index 0000000000000000000000000000000000000000..95af917a77cd25d0bc5177b1142752519f76a34e GIT binary patch literal 5705 zcmcgwX;f2JyN=cgjS7KMLZK2p1cE`sNkBo6N(gW=0SO`~gTX`zID$$=9NLB`q83g< z;2?uSLPS&q!2tx6!IBW541ywp1A~Zy0}cqnJ+2aN>G%85SPAbsPrY$$>(#@LA{X zWWUP6!iUH3T<7D28a)5yi4OSd-Tw0N@#gz|oq_rHj59X>l@a_a+>uo=+9T+7m1Fx=0(}w|XweV5Z&= zef2xGat5Y$W#-qgC2@Kfi^JlHufhAjKE94Vj&uH3*ctX1%=W{-Nl))ETPTWrzJ%m8 zQZO!d?CuqTPVZ$lSRCsqDywk4HZbx zQT;G~_;~f+EuQD%zGzO@KJ`uymOuHT`gX?+2XaCEu)*!+9#O@%{ZL%qiB0buYOHE{ zcP{quncI4@Tj>3cwE&*o_JpAxI(PwMz(MGznLC;R% zl4O_0Gn>jJ>i#wUnTsoP2KAjv*Dcvn^l6{h!}`sl2j(WrWTNvA9qt}p_H1BDz`bKQ zpvO1m2dAb*FBD7dVpjA0gKDpwRV!ckUon~vshnw2=@R0b_mF^u%v`i-Xw>f6px*x1 z?l1Ixs@^yxQ&XEd;{T{KdHA|+>-DP@4;PF|KaJEy%qX9qd)HZv`4@nn4wzu{R__0+ zwd{{gqvIKRE6x8JuU_^a6OA#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dP?*i>h|GM*~Ovb9pvfs5$IViYt(qI*LV4^P#`Bq(mZaG}j)nhWv?M2PIN?$hp4 zO3V4JGOTlCcl)sJTHfP2&h+#B>qm6<+RP)yd2kiTQ-r7QYRU%Pg%z|Jps-jr2R0{&+#Ot?_&kB;l$@xB&UpOe<0+6@*=nd>iSgB1M61npwZ-}-2KKiuzRqC0 zCNOJQbm$snVoMCwzQXI;$ufcho-m7yq&ory-di7dwslx6NzCN$nUiE z8?;sN|Bkk(h*iI|0 writes /chd, desc, codec and ecm + +#include "host.h" + +#include "cdrom.h" +#include "chd.h" +#include "chdcodec.h" +#include "hashing.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +int wasm_helper_init(uint32_t hunkbytes, uint32_t unitbytes, uint32_t c0, uint32_t c1, uint32_t c2, uint32_t c3, uint32_t flags); +uint8_t *wasm_helper_inbuf(); +uint8_t *wasm_helper_outbuf(); +int wasm_helper_compress(uint32_t *result, uint8_t *sha1out, uint32_t codecs); +} + +namespace { + +using bytes = std::vector; + +std::string s_out, s_tmp; +int s_count = 0; + +void seed(char const *fuzzer, std::string const &name, bytes const &data) +{ + fuzzhost::write_file(s_out + "/" + fuzzer + "/" + name, data); + s_count++; +} + +bytes text(std::string const &s) { return bytes(s.begin(), s.end()); } + +bytes cat(std::initializer_list parts) +{ + bytes v; + for (bytes const &p : parts) + v.insert(v.end(), p.begin(), p.end()); + return v; +} + +bytes noise(size_t n, unsigned seed) +{ + bytes v(n); + uint32_t x = seed * 2654435761u + 1; + for (size_t i = 0; i < n; i++) + { + x = x * 1103515245u + 12345u; + v[i] = uint8_t(x >> 16); + } + return v; +} + +// CD audio: 16-bit stereo, a sine in each channel +bytes sine(size_t n, double hz) +{ + bytes v(n & ~size_t(3)); + for (size_t i = 0; i + 3 < v.size(); i += 4) + { + int16_t const l = int16_t(9000 * std::sin(2 * M_PI * hz * double(i / 4) / 44100)); + int16_t const r = int16_t(7000 * std::sin(2 * M_PI * hz * 1.5 * double(i / 4) / 44100)); + v[i] = uint8_t(l); v[i + 1] = uint8_t(l >> 8); v[i + 2] = uint8_t(r); v[i + 3] = uint8_t(r >> 8); + } + return v; +} + +bytes prose(size_t n) +{ + std::string const s = "The quick brown fox jumps over the lazy dog; PACK YOUR BOX WITH FIVE DOZEN LIQUOR JUGS. "; + bytes v(n); + for (size_t i = 0; i < n; i++) + v[i] = uint8_t(s[i % s.size()]); + return v; +} + +void put_le32(uint8_t *p, uint32_t n) { for (int i = 0; i < 4; i++) p[i] = uint8_t(n >> (8 * i)); } +void put_be32(uint8_t *p, uint32_t n) { for (int i = 0; i < 4; i++) p[i] = uint8_t(n >> (24 - 8 * i)); } +void put_be64(uint8_t *p, uint64_t n) { for (int i = 0; i < 8; i++) p[i] = uint8_t(n >> (56 - 8 * i)); } +uint8_t bcd(unsigned n) { return uint8_t(((n / 10) << 4) | (n % 10)); } + +// 2048-byte sectors of an ISO 9660 volume: its primary volume descriptor at 16 and a root directory +// at 18 that names one file, as identification reads them (app/ident.js); at least 21 sectors +bytes iso(unsigned sectors) +{ + sectors = std::max(sectors, 21u); + bytes v = prose(size_t(sectors) * 2048); + std::fill(v.begin(), v.begin() + 16 * 2048, 0); + uint8_t *const pvd = &v[16 * 2048]; + std::memset(pvd, ' ', 2048); + pvd[0] = 1; + std::memcpy(pvd + 1, "CD001", 5); + pvd[6] = 1; + std::memcpy(pvd + 40, "DISCPRESS_FUZZ", 14); + put_le32(pvd + 80, sectors); + uint8_t *const root = pvd + 156; + std::memset(root, 0, 34); + root[0] = 34; + put_le32(root + 2, 18); + put_le32(root + 10, 2048); + root[25] = 2; + root[32] = 1; + uint8_t *const dir = &v[18 * 2048]; + std::memset(dir, 0, 2048); + uint8_t const file[] = "SYSTEM.CNF;1"; + dir[0] = uint8_t(33 + sizeof(file) - 1 + 1); + put_le32(dir + 2, 20); + put_le32(dir + 10, 64); + dir[32] = sizeof(file) - 1; + std::memcpy(dir + 33, file, sizeof(file) - 1); + std::memcpy(&v[20 * 2048], "BOOT2 = cdrom0:\\SLUS_999.99;1\r\n", 31); + return v; +} + +// raw Mode 1 sectors (2,352 bytes) of 2048-byte sectors, with their ECC, as on a CD image +bytes mode1(bytes const &user, unsigned first_lba = 150) +{ + size_t const n = user.size() / 2048; + bytes v(n * 2352); + for (size_t s = 0; s < n; s++) + { + uint8_t *const p = &v[s * 2352]; + p[0] = 0; + std::memset(p + 1, 0xff, 10); + p[11] = 0; + unsigned const lba = first_lba + unsigned(s); + p[12] = bcd(lba / 75 / 60); + p[13] = bcd(lba / 75 % 60); + p[14] = bcd(lba % 75); + p[15] = 1; + std::memcpy(p + 16, &user[s * 2048], 2048); + cdrom_file::ecc_generate(p); + } + return v; +} + +void make_chd(std::string const &name, std::vector const &args) +{ + std::string const out = s_tmp + "/" + name; + std::vector full = args; + full.insert(full.end(), { "-o", out, "-f" }); + if (fuzzhost::run(full) != 0) + { + std::fprintf(stderr, "seeds: chdman %s for %s failed\n", args[0].c_str(), name.c_str()); + std::exit(1); + } + std::FILE *f = std::fopen(out.c_str(), "rb"); + bytes v; + int c; + while ((c = std::fgetc(f)) != EOF) + v.push_back(uint8_t(c)); + std::fclose(f); + seed("chd", name, v); +} + +// A v3 or v4 CHD, which chdman 0.289 can read but no longer writes: uncompressed hunks, a "mini" +// hunk (8 bytes repeated), and a copy of another hunk +bytes old_chd(int version, uint32_t hunkbytes) +{ + uint32_t const headerbytes = (version == 3) ? 120 : 108; + bytes const data = cat({ prose(hunkbytes), noise(hunkbytes, 9) }); + uint32_t const hunks = 4; + bytes v(headerbytes + 16 * hunks); + std::memcpy(&v[0], "MComprHD", 8); + put_be32(&v[8], headerbytes); + put_be32(&v[12], version); + put_be32(&v[16], 0); // flags + put_be32(&v[20], 0); // no compression + put_be32(&v[24], hunks); + put_be64(&v[28], uint64_t(hunks) * hunkbytes); + put_be64(&v[36], 0); // no metadata + put_be32(&v[(version == 3) ? 76 : 44], hunkbytes); + uint64_t const at = v.size(); + for (uint32_t h = 0; h < hunks; h++) + { + uint8_t *const e = &v[headerbytes + 16 * h]; + if (h < 2) + { + put_be64(e, at + uint64_t(h) * hunkbytes); + put_be32(e + 8, util::crc32_creator::simple(&data[h * hunkbytes], hunkbytes)); + e[12] = uint8_t(hunkbytes >> 8); + e[13] = uint8_t(hunkbytes); + e[14] = uint8_t(hunkbytes >> 16); + e[15] = 2; // uncompressed + } + else if (h == 2) + { + std::memcpy(e, "MINIMINI", 8); // the data, repeated + e[15] = 3 | 0x10; // mini, no CRC + } + else + { + put_be64(e, 0); // a copy of hunk 0 + e[15] = 4 | 0x10; + } + } + v.insert(v.end(), data.begin(), data.end()); + return v; +} + +void chd_seeds() +{ + // raw data of every kind a codec wins on, and a hunk that repeats one (a copy in the map) + bytes const raw = cat({ bytes(4096, 0), prose(4096), sine(8192, 440), noise(4096, 1), prose(4096), iso(21) }); + fuzzhost::write_file(s_tmp + "/raw.bin", raw); + for (char const *codec : { "none", "zlib", "zstd", "lzma", "huff", "flac", "lzma,zlib,huff,flac" }) + { + std::string name = std::string("raw-") + codec + ".chd"; + std::replace(name.begin(), name.end(), ',', '+'); + make_chd(name, { "createraw", "-i", s_tmp + "/raw.bin", "-hs", "4096", "-us", "512", "-np", "1", "-c", codec }); + } + make_chd("hd.chd", { "createhd", "-i", s_tmp + "/raw.bin", "-np", "1", "-c", "zlib" }); + fuzzhost::write_file(s_tmp + "/dvd.iso", iso(40)); + make_chd("dvd.chd", { "createdvd", "-i", s_tmp + "/dvd.iso", "-np", "1" }); + make_chd("dvd-zstd.chd", { "createdvd", "-i", s_tmp + "/dvd.iso", "-np", "1", "-c", "zstd" }); + + // a CD: a data track with an ISO 9660 volume and an audio track + fuzzhost::write_file(s_tmp + "/data.bin", mode1(iso(24))); + fuzzhost::write_file(s_tmp + "/audio.bin", sine(12 * 2352, 330)); + fuzzhost::write_file(s_tmp + "/cd.cue", text("FILE \"data.bin\" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\n" + "FILE \"audio.bin\" BINARY\n TRACK 02 AUDIO\n INDEX 00 00:00:00\n INDEX 01 00:00:02\n")); + make_chd("cd.chd", { "createcd", "-i", s_tmp + "/cd.cue", "-np", "1" }); + make_chd("cd-keepcue.chd", { "createcd", "-i", s_tmp + "/cd.cue", "-np", "1", "--keepcue", "-c", "cdzl,cdfl" }); + make_chd("cd-zstd.chd", { "createcd", "-i", s_tmp + "/cd.cue", "-np", "1", "-c", "cdzs" }); + make_chd("cd-none.chd", { "createcd", "-i", s_tmp + "/cd.cue", "-np", "1", "-c", "none" }); + // a GD-ROM: its high-density area from LBA 45,000 + fuzzhost::write_file(s_tmp + "/gd.gdi", text("3\n1 0 4 2352 data.bin 0\n2 600 0 2352 audio.bin 0\n3 45000 4 2352 data.bin 0\n")); + make_chd("gd.chd", { "createcd", "-i", s_tmp + "/gd.gdi", "-np", "1" }); + + seed("chd", "v3.chd", old_chd(3, 1024)); + seed("chd", "v4.chd", old_chd(4, 2048)); +} + +// a Nero image: two tracks, then their list (a DAOX chunk) and the footer that points at it +bytes nero() +{ + bytes const t1 = mode1(iso(20)), t2 = sine(10 * 2352, 220); + auto be = [](uint64_t n, int k) { bytes v(k); for (int i = 0; i < k; i++) v[i] = uint8_t(n >> (8 * (k - 1 - i))); return v; }; + uint64_t const end1 = t1.size(), end2 = t1.size() + t2.size(); + bytes body = cat({ be(0, 4), bytes(13), bytes(1), be(0x2001, 2), bytes{ 1, 2 } }); + struct { uint32_t mode; uint64_t i0, i1, end; } const tracks[] = { { 0x600, 0, 0, end1 }, { 0x700, end1, end1 + 2 * 2352, end2 } }; + for (auto const &t : tracks) + body = cat({ body, bytes(12), be(2352, 2), be(t.mode, 2), bytes(2), be(t.i0, 8), be(t.i1, 8), be(t.end, 8) }); + bytes const chain = cat({ text("DAOX"), be(body.size(), 4), body, text("END!"), be(0, 4) }); + return cat({ t1, t2, chain, text("NER5"), be(end2, 8) }); +} + +void desc_seeds() +{ + // byte 0: the kind (0 cue, 1 toc, 2 gdi, 3 nrg, 4 iso), 8 --keepcue, 0x10-0x30 the extracted descriptor, 0x40 compression + auto put = [](std::string const &name, uint8_t kind, std::string const &s) { seed("desc", name, cat({ bytes{ kind }, text(s) })); }; + put("two-files.cue", 0x00, "FILE \"t.bin\" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\nFILE \"a.bin\" BINARY\n TRACK 02 AUDIO\n PREGAP 00:00:02\n INDEX 01 00:00:00\n"); + put("one-file.cue", 0x48, "FILE \"t.bin\" BINARY\n TRACK 01 MODE2/2352\n INDEX 01 00:00:00\n TRACK 02 AUDIO\n INDEX 00 00:00:20\n INDEX 01 00:00:30\n POSTGAP 00:00:05\n"); + put("wave.cue", 0x10, "FILE \"a.wav\" WAVE\n TRACK 01 AUDIO\n FLAGS DCP PRE\n INDEX 01 00:00:00\n"); + put("motorola.cue", 0x20, "FILE \"a.bin\" MOTOROLA\n TRACK 01 AUDIO\n INDEX 01 00:00:00\n"); + put("sessions.cue", 0x30, "REM SESSION 01\nFILE \"a.bin\" BINARY\n TRACK 01 AUDIO\n INDEX 01 00:00:00\nREM SESSION 02\nREM LEAD-OUT 00:00:02\nFILE \"t.bin\" BINARY\n TRACK 02 MODE1/2352\n INDEX 01 00:00:00\n"); + put("gdi.cue", 0x60, "REM SINGLE-DENSITY AREA\nFILE \"t.bin\" BINARY\n TRACK 01 MODE1/2352\n INDEX 01 00:00:00\nFILE \"a.bin\" BINARY\n TRACK 02 AUDIO\n INDEX 00 00:00:00\n INDEX 01 00:00:02\n" + "REM HIGH-DENSITY AREA\nFILE \"t.bin\" BINARY\n TRACK 03 MODE1/2352\n INDEX 01 00:00:00\n"); + put("cooked.cue", 0x00, "FILE \"odd.bin\" BINARY\n TRACK 01 MODE1/2048\n INDEX 01 00:00:00\nFILE 'a.bin' BINARY\n TRACK 02 MODE2/2336\n INDEX 01 00:00:00\nFILE \"t.bin\" BINARY\n TRACK 03 CDI/2352\n INDEX 01 00:00:00\n"); + put("lower.cue", 0x40, "file \"a.bin\" binary\r\n track 01 audio\r\n index 01 00:00:00\r\n"); + put("cdrdao.toc", 0x01, "CD_ROM\n// a comment\nTRACK MODE1_RAW\nDATAFILE \"t.bin\" #0 00:00:20\nTRACK AUDIO\nTWO_CHANNEL_AUDIO\nSILENCE 00:00:10\nFILE \"a.bin\" 0 00:00:10\nSTART 00:00:05\n"); + put("xa.toc", 0x51, "CD_ROM_XA\nTRACK MODE2_RAW\nDATAFILE \"t.bin\"\nTRACK AUDIO\nPREGAP 00:00:02\nZERO 00:00:03\nFILE \"a.bin\" 0\n"); + put("da.toc", 0x21, "CD_DA\nTRACK AUDIO\nCOPY\nPRE_EMPHASIS\nFILE \"a.bin\" #2352 00:00:05\nINDEX 00:00:02\nTRACK AUDIO\nFILE \"a.wav\" 0\n"); + put("cooked.toc", 0x01, "CD_ROM\nTRACK MODE1\nDATAFILE \"odd.bin\"\nTRACK MODE1_RAW RW_RAW\nDATAFILE \"t.bin\" 00:00:04\n"); + put("three.gdi", 0x02, "3\n1 0 4 2352 t.bin 0\n2 600 0 2352 a.bin 0\n3 45000 4 2352 t.bin 0\n"); + put("quoted.gdi", 0x22, "2\r\n1 0 4 2048 \"odd.bin\" 0\r\n2 150 0 2352 \"a.bin\" 0\r\n"); + seed("desc", "image.nrg", cat({ bytes{ 0x03 }, nero() })); + seed("desc", "image.iso", cat({ bytes{ 0x04 }, iso(20) })); + seed("desc", "raw.iso", cat({ bytes{ 0x04 }, mode1(iso(20)) })); +} + +void codec_seeds() +{ + // (the order of fuzz_codec.cpp's list) + chd_codec_type const codecs[] = { CHD_CODEC_ZLIB, CHD_CODEC_ZSTD, CHD_CODEC_LZMA, CHD_CODEC_HUFFMAN, CHD_CODEC_FLAC, + CHD_CODEC_CD_ZLIB, CHD_CODEC_CD_ZSTD, CHD_CODEC_CD_LZMA, CHD_CODEC_CD_FLAC }; + uint32_t const frames = 8, cdbytes = frames * 2448, bytes512 = 8 * 512; + // CD hunks: frames of 2,352 bytes, each with 96 bytes of subchannel data + auto cdhunk = [](bytes const §ors) { + bytes v; + for (size_t f = 0; f + 2352 <= sectors.size(); f += 2352) + { + v.insert(v.end(), sectors.begin() + f, sectors.begin() + f + 2352); + v.insert(v.end(), 96, 0); + } + return v; + }; + bytes const sectors = mode1(iso(24)); // from the volume descriptor on + std::vector> const cd = { { "data", cdhunk(bytes(sectors.begin() + 16 * 2352, sectors.end())) }, { "audio", cdhunk(sine(frames * 2352, 500)) } }; + std::vector> const other = { { "prose", prose(bytes512) }, { "audio", sine(bytes512, 440) }, { "mixed", cat({ bytes(1024, 0), prose(1024), noise(2048, 3) }) } }; + for (size_t c = 0; c < std::size(codecs); c++) + { + bool const iscd = c >= 5; + for (auto const &[what, hunk] : iscd ? cd : other) + { + uint32_t const hunkbytes = iscd ? cdbytes : bytes512; + if (wasm_helper_init(hunkbytes, iscd ? 2448 : 512, codecs[c], 0, 0, 0, 0)) + continue; + std::memcpy(wasm_helper_inbuf(), hunk.data(), hunkbytes); + uint32_t result[2]; + uint8_t sha1[20]; + if (wasm_helper_compress(result, sha1, 1) != 0) + continue; // stored as it is: no seed + bytes const comp(wasm_helper_outbuf(), wasm_helper_outbuf() + result[0]); + char name[64]; + std::snprintf(name, sizeof(name), "%zu-%s", c, what); + seed("codec", name, cat({ bytes{ uint8_t(c), uint8_t(frames - 1) }, comp })); + } + } +} + +// an ECM chunk header: the type, then count - 1, 5 bits and then 7 at a time +bytes ecm_header(uint32_t type, uint32_t count) +{ + uint32_t n = count - 1; + bytes v{ uint8_t(type | ((n & 0x1f) << 2)) }; + n >>= 5; + while (n) + { + v.back() |= 0x80; + v.push_back(uint8_t(n & 0x7f)); + n >>= 7; + } + return v; +} + +void ecm_seeds() +{ + // bytes 0-3: the type and "inside a chunk" (4), the window's size, the sectors or bytes left + auto put = [](std::string const &name, uint8_t mode, uint32_t window, uint8_t left, bytes const &stored) { + seed("ecm", name, cat({ bytes{ mode, uint8_t(window), uint8_t(window >> 8), left }, stored })); + }; + bytes const mode1x2 = noise(2 * (3 + 2048), 4), form1 = noise(4 + 2048, 5), form2 = noise(4 + 2324, 6); + put("bytes", 0, 100, 0, cat({ ecm_header(0, 100), prose(100) })); + put("mode1", 0, 2 * 2352, 0, cat({ ecm_header(1, 2), mode1x2 })); + put("form1", 0, 2336, 0, cat({ ecm_header(2, 1), form1 })); + put("form2", 0, 2336, 0, cat({ ecm_header(3, 1), form2 })); + put("mixed", 0, 16 + 2336 + 2352, 0, cat({ ecm_header(0, 16), noise(16, 7), ecm_header(2, 1), form1, ecm_header(1, 1), bytes(mode1x2.begin(), mode1x2.begin() + 2051) })); + put("inside", 4 | 1, 2 * 2352, 1, mode1x2); + put("inside-bytes", 4, 50, 49, prose(50)); + put("end", 0, 2352, 0, cat({ bytes{ 0xfc, 0xff, 0xff, 0xff, 0x3f }, bytes(4, 0) })); +} + +} // anonymous namespace + +int main(int argc, char **argv) +{ + if (argc != 2) + { + std::fprintf(stderr, "usage: seeds \n"); + return 2; + } + s_out = argv[1]; + for (char const *d : { "", "/chd", "/desc", "/codec", "/ecm" }) + mkdir((s_out + d).c_str(), 0755); + s_tmp = fuzzhost::make_dir("seeds"); + chd_seeds(); + desc_seeds(); + codec_seeds(); + ecm_seeds(); + std::printf("%d seeds in %s\n", s_count, s_out.c_str()); + return 0; +} diff --git a/tests/fuzz/shim/emscripten.h b/tests/fuzz/shim/emscripten.h new file mode 100644 index 0000000..601b4ed --- /dev/null +++ b/tests/fuzz/shim/emscripten.h @@ -0,0 +1,5 @@ +// The fuzzers (tests/fuzz, scripts/fuzz.sh) build the engine natively with __EMSCRIPTEN__ defined, so that +// the browser-only code is in (chd_file::wasm_read_ahead, wasm/wasm_helper.cpp, wasm/ecm.cpp). This +// stands in for Emscripten's header, of which the engine uses only EMSCRIPTEN_KEEPALIVE. +#pragma once +#define EMSCRIPTEN_KEEPALIVE __attribute__((used)) diff --git a/tests/support/chdcraft.js b/tests/support/chdcraft.js index aa1559d..10be221 100644 --- a/tests/support/chdcraft.js +++ b/tests/support/chdcraft.js @@ -1,6 +1,8 @@ // A compressed v5 CHD written by hand, for maps chdman itself never writes (damaged or crafted files). // `hunks` lists each hunk as {data} (stored with the deflate codec), {self: n} (a copy of hunk n of the -// file) or {data, length} (stored, but with another length in its map entry; the bytes are there to read). +// file), {data, length} (stored, but with another length in its map entry; the bytes are there to read) +// or {stored} (those bytes, as the first codec's data); `slot: 1` stores either with the second codec slot +// instead. `codec` names the first codec in the header (the others are empty). // The header has no SHA-1s, so `verify` finds nothing to check: read the data with extractraw. import zlib from 'node:zlib'; @@ -13,13 +15,13 @@ function crc16(buf) { return crc; } -export function craftChd(hunks, hunkbytes = 4096) { +export function craftChd(hunks, hunkbytes = 4096, { codec = 'zlib', unitbytes = 512 } = {}) { const bits = []; const put = (v, n) => { for (let i = n - 1; i >= 0; i--) bits.push(Math.floor(v / 2 ** i) & 1); }; // the map's Huffman tree (16 code lengths of 4 bits): 2-bit codes for the types 0 (first codec), 1, // 4 (not compressed) and 5 (a copy of a hunk of this file), which get the codes 0 to 3 in that order for (const len of [2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) put(len, 4); - for (const h of hunks) put(h.self !== undefined ? 3 : 0, 2); + for (const h of hunks) put(h.self !== undefined ? 3 : (h.slot ?? 0), 2); // then per hunk: a stored length and CRC-16, or the hunk copied const lengthbits = 24, selfbits = 16, first = 124; const raw = Buffer.alloc(hunks.length * 12), blobs = []; // (the map as chdman holds it: its CRC is checked) @@ -31,8 +33,8 @@ export function craftChd(hunks, hunkbytes = 4096) { put(h.self, selfbits); return; } - const packed = zlib.deflateRawSync(h.data, { level: 9 }), length = h.length ?? packed.length, crc = crc16(h.data); - e[0] = 0; e.writeUIntBE(length, 1, 3); e.writeUIntBE(offset, 4, 6); e.writeUInt16BE(crc, 10); + const packed = h.stored ?? zlib.deflateRawSync(h.data, { level: 9 }), length = h.length ?? packed.length, crc = h.data ? crc16(h.data) : 0; + e[0] = h.slot ?? 0; e.writeUIntBE(length, 1, 3); e.writeUIntBE(offset, 4, 6); e.writeUInt16BE(crc, 10); put(length, lengthbits); put(crc, 16); blobs.push(packed, Buffer.alloc(Math.max(0, length - packed.length))); offset += Math.max(length, packed.length); @@ -45,8 +47,8 @@ export function craftChd(hunks, hunkbytes = 4096) { mapHead[12] = lengthbits; mapHead[13] = selfbits; const head = Buffer.alloc(124); head.write('MComprHD', 0, 'latin1'); head.writeUInt32BE(124, 8); head.writeUInt32BE(5, 12); - head.write('zlib', 16, 'latin1'); + head.write(codec, 16, 'latin1'); head.writeBigUInt64BE(BigInt(hunks.length * hunkbytes), 32); head.writeBigUInt64BE(BigInt(offset), 40); - head.writeUInt32BE(hunkbytes, 56); head.writeUInt32BE(512, 60); + head.writeUInt32BE(hunkbytes, 56); head.writeUInt32BE(unitbytes, 60); return Buffer.concat([head, ...blobs, mapHead, map]); } diff --git a/tests/ui/engine.spec.js b/tests/ui/engine.spec.js index 26499d6..343d1c9 100644 --- a/tests/ui/engine.spec.js +++ b/tests/ui/engine.spec.js @@ -1046,6 +1046,129 @@ test('a CHD whose map goes round in a circle, or claims an oversized block, is a } }); +// A header whose sizes no data can have: 0.289 divides by a hunk or unit size of 0, and a logical size of +// more hunks than a map counts (2^32 or more) gave a map too small for them +test('a CHD header with a hunk or unit size of 0, or too many hunks, is an error, not a crash', () => { + const D = Buffer.from(Array.from({ length: 4096 }, (_, i) => (i * 7 + (i >> 5)) & 0x7f)); + const good = craftChd([{ data: D }, { self: 0 }]); + const variant = (name, edit) => { const b = Buffer.from(good); edit(b); const p = tmp(name); fs.writeFileSync(p, b); return p; }; + const files = { + 'hunk size 0': variant('hunk0.chd', b => b.writeUInt32BE(0, 56)), + 'unit size 0': variant('unit0.chd', b => b.writeUInt32BE(0, 60)), + '2^34 hunks': variant('huge.chd', b => b.writeBigUInt64BE(1n << 46n, 32)), + '2^32 hunks': variant('just over.chd', b => b.writeBigUInt64BE(1n << 44n, 32)), + }; + expect(run(UPSTREAM, ['info', '-i', files['hunk size 0']]).signal, 'upstream divides by zero').toBe('SIGFPE'); // the bug + for (const [name, file] of Object.entries(files)) { + for (const args of [['info', '-i', file], ['verify', '-i', file], ['extractraw', '-i', file, '-o', tmp('x.raw'), '-f']]) { + const r = run(ENGINE, args); + expect({ code: r.code, signal: r.signal }, `${name}: ${args[0]}`).toEqual({ code: 1, signal: null }); + expect(r.err, `${name}: ${args[0]}`).toContain('Invalid file'); + } + } +}); + +// Found by fuzzing (tests/fuzz/README.md): a v5 map whose Huffman tree has more codes than one bit has room for, +// or whose size in memory doesn't fit in 32 bits, or that runs past the end of the file +test('a damaged or crafted map is an error, not a crash', () => { + const good = makeChd('createcd', 'twine.cue'); + const g = fs.readFileSync(good); + const mapoffset = Number(g.readBigUInt64BE(40)); + const variant = (name, edit) => { const b = Buffer.from(g); edit(b); const p = tmp(name); fs.writeFileSync(p, b); return p; }; + // the tree of the map's compression types: three codes of 1 bit (4 bits each: 1, then the length 1), the rest none + const tree = variant('tree.chd', b => { b.fill(0x11, mapoffset + 16, mapoffset + 19); b.fill(0, mapoffset + 19, mapoffset + 26); }); + // 358 million hunks: 12 bytes each are more than 2^32 + const wrap = variant('wrap.chd', b => b.writeBigUInt64BE(BigInt(b.readUInt32BE(56)) * 358000000n, 32)); + // a compressed map of 1 GB, in a file of 1.5 MB + const pastEnd = variant('past end.chd', b => b.writeUInt32BE(1 << 30, mapoffset)); + // the bugs: the lookup table written past its end smashes the stack (gcc's stack protector aborts), the map too small + // for its hunks is written past its end + expect(run(UPSTREAM, ['info', '-i', tree]).signal, 'upstream, tree').toBeTruthy(); + expect(run(UPSTREAM, ['info', '-i', wrap]).signal, 'upstream, 358 million hunks').toBeTruthy(); + for (const [file, message] of [[tree, 'Decompression error'], [wrap, 'Invalid file'], [pastEnd, 'Input/output error']]) { + for (const args of [['info', '-i', file], ['verify', '-i', file], ['extractcd', '-i', file, '-o', tmp('x.cue'), '-f']]) { + const r = run(ENGINE, args); + expect({ code: r.code, signal: r.signal }, `${path.basename(file)}: ${args[0]}`).toEqual({ code: 1, signal: null }); + expect(r.err, `${path.basename(file)}: ${args[0]}`).toContain(message); + } + } +}); + +// A CD codec's hunk says how long its sector data is: more than the hunk holds made 0.289 decode past the end of it, and +// the subcode's length wrap (the fuzzer's input, a cdzs hunk of 32 frames, in a CHD; the sanitized build sees the reads) +test('a CD hunk whose sector data is longer than the hunk is an error', () => { + const stored = fs.readFileSync(path.join(ROOT, 'tests', 'fuzz', 'regressions', 'codec', 'cd-base-longer-than-hunk')).subarray(2); + const file = tmp('cd base.chd'); + fs.writeFileSync(file, craftChd([{ stored }], 2448 * 32, { codec: 'cdzs', unitbytes: 2448 })); + const r = run(ENGINE, ['extractraw', '-i', file, '-o', tmp('x.raw'), '-f']); + expect({ code: r.code, signal: r.signal }).toEqual({ code: 1, signal: null }); + expect(r.err).toContain('Decompression error'); +}); + +// A CD's track metadata that names a track twice or skips one left a track of 0-byte sectors, which extractcd divided by +test('CD metadata without one of its tracks is an error', () => { + const g = fs.readFileSync(makeChd('createcd', 'twine.cue')); + const b = Buffer.from(g), at = g.indexOf('TRACK:1 TYPE'); + expect(at).toBeGreaterThan(0); + b.write('TRACK:2', at, 'latin1'); // its one track, as track 2 + const file = tmp('track 2.chd'); + fs.writeFileSync(file, b); + expect(run(UPSTREAM, ['extractcd', '-i', file, '-o', tmp('u.cue'), '-f']).signal, 'upstream').toBe('SIGFPE'); // the bug + for (const args of [['extractcd', '-i', file, '-o', tmp('x.cue'), '-f'], ['info', '-i', file, '-v']]) { + const r = run(ENGINE, args); + expect({ code: r.code, signal: r.signal }, args[0]).toEqual({ code: args[0] === 'info' ? 0 : 1, signal: null }); + } +}); + +// The track metadata's strings were read with %s into 16-byte buffers: a longer one wrote past their end (on the stack; +// the sanitized build sees it, upstream then aborts on the error that follows) +test('CD metadata with a string longer than its buffer is an error', () => { + const g = fs.readFileSync(makeChd('createcd', 'twine.cue')); + const b = Buffer.from(g), at = g.indexOf('MODE2_RAW SUBTYPE:NONE'); + expect(at).toBeGreaterThan(0); + b.write('MODE2_RAW_SUBTYPE_NONE_FRAMES_629_PREGAP_0', at, 'latin1'); // one 42-character type, in place + const file = tmp('long type.chd'); + fs.writeFileSync(file, b); + const r = run(ENGINE, ['extractcd', '-i', file, '-o', tmp('x.cue'), '-f']); + expect({ code: r.code, signal: r.signal }).toEqual({ code: 1, signal: null }); +}); + +// A hunk stored with a codec the header doesn't list (a v3 CHD without compression whose map says a hunk is compressed, +// or a v5 map's codec slot left empty) made 0.289 call a codec it doesn't have +test('a hunk that names a codec the CHD has none for is an error', () => { + const head = Buffer.alloc(120); + head.write('MComprHD', 0, 'latin1'); head.writeUInt32BE(120, 8); head.writeUInt32BE(3, 12); + head.writeUInt32BE(1, 24); head.writeBigUInt64BE(512n, 28); head.writeUInt32BE(512, 76); // one hunk of 512 bytes, compression 0 + const entry = Buffer.alloc(16); + entry.writeBigUInt64BE(136n, 0); entry.writeUInt16BE(16, 12); entry[15] = 1 | 0x10; // compressed, 16 bytes, no CRC + const v3 = tmp('v3.chd'); + fs.writeFileSync(v3, Buffer.concat([head, entry, Buffer.alloc(16, 9)])); + // and a v5 CHD whose map gives a hunk the second codec slot, which its header leaves empty + const v5 = tmp('v5.chd'); + fs.writeFileSync(v5, craftChd([{ data: Buffer.alloc(4096, 3), slot: 1 }])); + expect(run(UPSTREAM, ['extractraw', '-i', v3, '-o', tmp('u.raw'), '-f']).signal, 'upstream').toBe('SIGSEGV'); // the bug + for (const file of [v3, v5]) { + const r = run(ENGINE, ['extractraw', '-i', file, '-o', tmp('x.raw'), '-f']); + expect({ code: r.code, signal: r.signal }, path.basename(file)).toEqual({ code: 1, signal: null }); + expect(r.err, path.basename(file)).toContain('Invalid data'); + } +}); + +// A map entry saying a hunk is in the parent, in a CHD that has none: 0.289 read through a null pointer +test('a hunk in a parent the CHD doesn\u2019t have is an error', () => { + const head = Buffer.alloc(120); + head.write('MComprHD', 0, 'latin1'); head.writeUInt32BE(120, 8); head.writeUInt32BE(3, 12); + head.writeUInt32BE(1, 24); head.writeBigUInt64BE(512n, 28); head.writeUInt32BE(512, 76); // no parent (flags 0) + const entry = Buffer.alloc(16); + entry[15] = 5 | 0x10; // hunk 0 of the parent + const file = tmp('parent.chd'); + fs.writeFileSync(file, Buffer.concat([head, entry])); + expect(run(UPSTREAM, ['extractraw', '-i', file, '-o', tmp('u.raw'), '-f']).signal, 'upstream').toBe('SIGSEGV'); // the bug + const r = run(ENGINE, ['extractraw', '-i', file, '-o', tmp('x.raw'), '-f']); + expect({ code: r.code, signal: r.signal }).toEqual({ code: 1, signal: null }); + expect(r.err).toContain('Invalid data'); +}); + // A Dreamcast disc of each GD-ROM layout (the high-density area: one data track; data, then audio; two data // tracks; data, audio, data), as Redump lays it out (a cue sheet, each track's pregap at the start of its own // file) and as TOSEC does (a GDI: track 2's pregap in no file, the others at the end of the previous file, but From 61e929c5668da02d12c703c37774a2a3b93ad785 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 02:35:39 +0000 Subject: [PATCH 02/13] Tests: damaged files of each kind the page reads itself CSO/ZSO, PBP, ECM, zip and CloneCD files cut short, with fields set to extremes and bytes changed: each ends as a card that says why, or a conversion that finishes or fails. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QUSh6AFtA1CAR66DNvjtty --- tests/ui/readers.spec.js | 199 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 tests/ui/readers.spec.js diff --git a/tests/ui/readers.spec.js b/tests/ui/readers.spec.js new file mode 100644 index 0000000..573932a --- /dev/null +++ b/tests/ui/readers.spec.js @@ -0,0 +1,199 @@ +// Damaged files of each kind the page reads itself rather than chdman (app/worker.js's CisoStore, PbpStore and +// EcmStore, app/ui.js's zipEntries, Unzip, ccdToCue and scanSub): made from the fixtures by a seeded random +// generator, cut short, with header fields set to extremes, and with bytes changed. Whatever they hold, each must +// end as a card that says what is wrong, or as a conversion that finishes or fails with a message: never an +// uncaught exception, a console error (the fixture checks both) or a card left waiting. +import fs from 'node:fs'; +import path from 'node:path'; +import zlib from 'node:zlib'; +import { test, expect } from '../support/app.js'; +import { FIXTURES } from '../support/paths.js'; +import { makeZip } from '../support/zip.js'; + +const read = n => fs.readFileSync(path.join(FIXTURES, n)); + +// mulberry32: the same variants on every run +function random(seed) { + let a = seed >>> 0; + return () => { + a = (a + 0x6d2b79f5) >>> 0; + let t = a; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }; +} +const cut = (b, at) => Buffer.from(b.subarray(0, Math.max(0, Math.floor(at)))); +function flip(b, seed, from = 0, to = b.length, count = 8) { + const r = random(seed), out = Buffer.from(b); + for (let i = 0; i < count; i++) out[from + Math.floor(r() * (Math.min(to, out.length) - from))] ^= 1 + Math.floor(r() * 255); + return out; +} +const edit = (b, f) => { const out = Buffer.from(b); f(out); return out; }; + +// A CSO v1 image (maxcso's format, as discgen.py's ciso writes it): deflate blocks, an index of their offsets +function cso(iso, block = 2048) { + const count = Math.ceil(iso.length / block), index = Buffer.alloc(4 * (count + 1)), blocks = []; + let pos = 24 + index.length; + for (let i = 0; i < count; i++) { + const raw = Buffer.alloc(block); + iso.copy(raw, 0, i * block, (i + 1) * block); + let data = zlib.deflateRawSync(raw, { level: 9 }), plain = 0; + if (data.length >= block) { data = raw; plain = 0x80000000; } + index.writeUInt32LE((pos | plain) >>> 0, 4 * i); + blocks.push(data); + pos += data.length; + } + index.writeUInt32LE(pos, 4 * count); + const head = Buffer.alloc(24); + head.write('CISO', 0, 'latin1'); head.writeUInt32LE(24, 4); head.writeBigUInt64LE(BigInt(iso.length), 8); + head.writeUInt32LE(block, 16); head[20] = 1; + return Buffer.concat([head, index, ...blocks]); +} + +// writes the variants into their own folder and returns the paths to add +function variants(kind, list) { + const dir = path.join(FIXTURES, 'gen', 'readers', kind); + fs.rmSync(dir, { recursive: true, force: true }); + fs.mkdirSync(dir, { recursive: true }); + const names = []; + for (const [name, data] of list) { + fs.writeFileSync(path.join(dir, name), data); + names.push(`gen/readers/${kind}/${name}`); + } + return names; +} + +// adds the files, waits for every card to be identified, then converts each one that can be converted. +// expected: the number of cards (without it, as many as the page makes once it has unpacked every zip) +async function exercise(app, files, expected) { + await app.open(); + await app.add(files); + const cards = app.jobs(); + if (expected === undefined) { + await expect.poll(async () => cards.count(), { timeout: 60_000 }).toBeGreaterThanOrEqual(1); + await expect.poll(async () => app.page.locator('#chipUnzip').count(), { timeout: 90_000 }).toBe(0); + expected = await cards.count(); + } + await expect(cards).toHaveCount(expected, { timeout: 60_000 }); + for (let i = 0; i < expected; i++) { + const card = cards.nth(i); + await app.waitState(card, ['ready', 'blocked', 'error'], 90_000); + await expect(card.locator('.note', { hasText: /Identifying|Checking against|Reading the ECM/ })).toHaveCount(0, { timeout: 90_000 }); + } + const ended = {}; + for (let i = 0; i < expected; i++) { + const card = cards.nth(i), button = card.locator('.job-foot button.primary'); + const title = await card.getAttribute('data-title'); + if (!(await button.count()) || !(await button.isEnabled())) { ended[title] = await card.getAttribute('data-state'); continue; } + await button.click(); + try { ended[title] = await app.waitState(card, ['done', 'error', 'canceled'], 120_000); } + catch (e) { throw new Error(`${title}: still "${await card.getAttribute('data-state')}" after 120 s:\n${await card.innerText()}`); } + // a failure says why + if (ended[title] === 'error') await expect(card, title).toContainText(/\S/); + } + return ended; +} + +test('damaged CSO and ZSO images', async ({ app }) => { + test.setTimeout(400_000); + const good = cso(read('psxexe.iso')), zso = read('umd.zso'); + const blocks = (good.readUInt32LE(16) && Math.ceil(Number(good.readBigUInt64LE(8)) / good.readUInt32LE(16))) || 1; + const files = variants('ciso', [ + ['header-only.cso', cut(good, 24 + 4 * 10)], + ['half.cso', cut(good, good.length / 2)], + ['block-0.cso', edit(good, b => b.writeUInt32LE(0, 16))], + ['block-2g.cso', edit(good, b => b.writeUInt32LE(0x80000000, 16))], + ['size-huge.cso', edit(good, b => b.writeBigUInt64LE(0xffffffffffffn, 8))], + ['version-9.cso', edit(good, b => { b[20] = 9; b[21] = 31; })], + ['index-past-end.cso', edit(good, b => b.writeUInt32LE(0x7fffffff, 24 + 4 * 5))], + ['index-backwards.cso', edit(good, b => b.writeUInt32LE(b.readUInt32LE(24 + 4 * 3), 24 + 4 * 7))], + ['index-flips.cso', flip(good, 1, 24, 24 + 4 * (blocks + 1), 12)], + ['data-flips.cso', flip(good, 2, 24 + 4 * (blocks + 1), good.length, 40)], + ['zso-half.zso', cut(zso, zso.length / 2)], + ['zso-index-flips.zso', flip(zso, 3, 24, 24 + 4 * 64, 12)], + ['zso-data-flips.zso', flip(zso, 4, 4096, zso.length, 40)], + ]); + const ended = await exercise(app, files, files.length); + expect(Object.keys(ended)).toHaveLength(files.length); +}); + +test('damaged PBP files', async ({ app }) => { + test.setTimeout(400_000); + const good = read('mgs.pbp'), psar = good.readUInt32LE(0x24); + const files = variants('pbp', [ + ['header-only.pbp', cut(good, 0x28)], + ['third.pbp', cut(good, good.length / 3)], + ['psar-past-end.pbp', edit(good, b => b.writeUInt32LE(b.length + 1, 0x24))], + ['psar-max.pbp', edit(good, b => b.writeUInt32LE(0xffffffff, 0x24))], + ['header-flips.pbp', flip(good, 5, 0, 0x28, 6)], + ['psar-flips-1.pbp', flip(good, 6, psar, psar + 0x400, 12)], + ['psar-flips-2.pbp', flip(good, 7, psar + 0x400, psar + 0x4000, 12)], + ['table-flips.pbp', flip(good, 8, psar + 0x4000, psar + 0x10000, 24)], + ['data-flips.pbp', flip(good, 9, psar + 0x10000, good.length, 40)], + ]); + const ended = await exercise(app, files, files.length); + expect(Object.keys(ended)).toHaveLength(files.length); +}); + +test('damaged ECM images', async ({ app }) => { + test.setTimeout(400_000); + const good = read('lone.bin.ecm'); + const files = variants('ecm', [ + ['magic.bin.ecm', edit(good, b => b.write('ECX', 0, 'latin1'))], + ['half.bin.ecm', cut(good, good.length / 2)], + ['no-edc.bin.ecm', cut(good, good.length - 2)], + ['edc.bin.ecm', edit(good, b => { b[b.length - 1] ^= 0x55; })], + ['count-huge.bin.ecm', edit(good, b => Buffer.from([0xfd, 0xff, 0xff, 0xff, 0x0f]).copy(b, 4))], + ['start-flips.bin.ecm', flip(good, 10, 4, 64, 4)], + ['flips-1.bin.ecm', flip(good, 11, 4, good.length, 6)], + ['flips-2.bin.ecm', flip(good, 12, 4, good.length, 60)], + ]); + const ended = await exercise(app, files, files.length); + expect(Object.keys(ended)).toHaveLength(files.length); + // a changed byte of the image's data fails its EDC: the conversion stops instead of making a CHD of other data + expect(ended.edc).toBe('error'); +}); + +test('damaged zip archives', async ({ app }) => { + test.setTimeout(400_000); + const cue = Buffer.from('FILE "lone.bin" BINARY\n TRACK 01 MODE2/2352\n INDEX 01 00:00:00\n'); + const good = makeZip([{ name: 'lone.cue', data: cue }, { name: 'lone.bin', data: read('lone.bin') }]); + const stored = makeZip([{ name: 'lone.cue', data: cue, method: 0 }, { name: 'lone.bin', data: read('lone.bin'), method: 0 }]); + const eocd = good.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06])), cd = good.readUInt32LE(eocd + 16); + const files = variants('zip', [ + ['half.zip', cut(good, good.length / 2)], + ['no-directory.zip', cut(good, cd)], + ['directory-flips.zip', flip(good, 13, cd, good.length, 10)], + ['directory-offset.zip', edit(good, b => b.writeUInt32LE(0xfffffff0, eocd + 16))], + ['sizes-huge.zip', edit(good, b => { b.writeUInt32LE(0xffffffff, cd + 20); b.writeUInt32LE(0xffffffff, cd + 24); })], + ['local-flips.zip', flip(good, 14, 0, 30, 6)], + ['data-flips.zip', flip(good, 15, 200, cd, 30)], + ['stored-flips.zip', flip(stored, 16, 0, stored.length, 30)], + ]); + // (a zip's entries make the cards, so as many as the page makes) + const ended = await exercise(app, files); + expect(Object.keys(ended).length).toBeGreaterThan(0); +}); + +test('damaged CloneCD images', async ({ app }) => { + test.setTimeout(400_000); + const ccd = read('lc.ccd').toString('latin1'), img = read('lc.img'), sub = read('lc.sub'); + const sets = { + 'toc-0': [ccd.replace(/TocEntries=\d+/, 'TocEntries=0'), img, sub], + 'toc-999': [ccd.replace(/TocEntries=\d+/, 'TocEntries=999'), img, sub], + 'plba': [ccd.replace(/PLBA=\d+/g, 'PLBA=-150'), img, sub], + 'plba-huge': [ccd.replace(/PLBA=\d+/g, 'PLBA=4294967295'), img, sub], + 'no-entries': [ccd.replace(/\[Entry[\s\S]*/, ''), img, sub], + 'garbage': [flip(Buffer.from(ccd, 'latin1'), 17, 0, ccd.length, 20).toString('latin1'), img, sub], + 'img-short': [ccd, cut(img, 1000), sub], + 'sub-short': [ccd, img, cut(sub, 100)], + 'sub-flips': [ccd, img, flip(sub, 18, 0, sub.length, 400)], + }; + const list = []; + for (const [name, [c, i, s]] of Object.entries(sets)) list.push([`${name}.ccd`, Buffer.from(c, 'latin1')], [`${name}.img`, i], [`${name}.sub`, s]); + const files = variants('ccd', list); + // (a .ccd that can't be read leaves its image and subchannel to make a card of their own: as many cards as the page makes) + const ended = await exercise(app, files); + expect(Object.keys(ended).length).toBeGreaterThanOrEqual(Object.keys(sets).length); +}); From ba273cae393e6adafdcaac538d85a8217dbd8cfe Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 02:35:40 +0000 Subject: [PATCH 03/13] Page: say when a running job makes no progress; suggest the faster preset on phones A conversion the page hears nothing from for 90 s while visible gets a note that it may be stuck, until it goes on (DEBUG.freeze and stallMs for tests). On phones and tablets the compression hint suggests Nearly as small, faster; the default stays chdman's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QUSh6AFtA1CAR66DNvjtty --- app/ui.js | 31 +- app/worker.js | 2 + dist/discpress.html | 712 +++++++++++++++++++++++++++++++----- tests/ui/edge-cases.spec.js | 30 ++ tests/ui/mobile.spec.js | 15 + 5 files changed, 687 insertions(+), 103 deletions(-) diff --git a/app/ui.js b/app/ui.js index fe5569e..e0f4558 100644 --- a/app/ui.js +++ b/app/ui.js @@ -344,6 +344,19 @@ var Engine = { } }) : null; var ackWait = null; + // no-progress watchdog: any message from the job worker is progress; only time the page is + // visible counts (a hidden page's workers may be paused, iOS pauses them all). o.onStall(true) + // after STALL_MS of none, o.onStall(false) when one comes again + var quiet = 0, lastTick = Date.now(), quietSaid = false, watch = o.onStall ? setInterval(function () { + var now = Date.now(); + if (document.visibilityState === 'visible') quiet += now - lastTick; + lastTick = now; + if (!quietSaid && quiet >= (DEBUG.stallMs || STALL_MS)) { quietSaid = true; o.onStall(true); } + }, 1000) : 0; + function heard() { + quiet = 0; + if (quietSaid) { quietSaid = false; o.onStall(false); } + } // the worker could not read an input itself: read it here and send it over async function feedStage(m) { var blob = o.inputs[m.index].blob, step = 4 << 20; @@ -360,6 +373,7 @@ var Engine = { } function cleanup() { finished = true; + if (watch) { clearInterval(watch); watch = 0; } // a chunk still waiting for the worker's answer: its staging ends (and lets go of the file) if (ackWait) { var a = ackWait; ackWait = null; a(); } if (worker) worker.terminate(); @@ -394,6 +408,7 @@ var Engine = { worker.onmessage = function (e) { var m = e.data; if (finished) return; + heard(); if (m.type.charAt(0) === 's' && m.type.charAt(1) === '-') { if (sink) sink.handle(m, worker); } else if (m.type === 'stage-request') feedStage(m); else if (m.type === 'stage-ack') { var a = ackWait; ackWait = null; if (a) a(); } @@ -425,7 +440,8 @@ var Engine = { }; self.post(worker, { type: 'run', jobId: o.jobId, dirPath: o.dirPath, args: o.args, inputs: o.inputs || [], - writable: o.writable || [], slots: o.slots || 0, outMode: o.outMode || 'mem', ports: ports, debugStage: DEBUG.stage, debugSinkMax: DEBUG.sinkMax, debugStorage: DEBUG.failStorage + writable: o.writable || [], slots: o.slots || 0, outMode: o.outMode || 'mem', ports: ports, debugStage: DEBUG.stage, debugSinkMax: DEBUG.sinkMax, debugStorage: DEBUG.failStorage, + debugFreeze: o.onStall ? DEBUG.freeze : 0 }, ports); }); return { @@ -2323,6 +2339,8 @@ function renderControls(job) { if (settings.compat && /(^|,)(cdzs|zstd)(,|$)/.test(cur[2] || '')) { chint += ' ' + ZSTD_OLD; zwarn = true; } if (cur[0] === 'mister' && pfc.noMister) { chint += ' MiSTer has no core that plays ' + sysName(job.ident.sys) + ' games from CHDs.'; zwarn = true; } if (pfc.hint && cur[0] !== 'none') chint += ' ' + pfc.hint; + // phones and tablets take minutes over a disc: the preset that saves a third of that, suggested (never chosen) + if (isMobile && cur[0] === 'default' && presets.some(function (p) { return p[0] === 'plan'; })) chint += ' ' + PLAN_HINT; var cfield = selectField('Compression', presets.map(function (p) { return [p[0], p[1]]; }), o.preset, function (v) { o.preset = v; refresh(); }, chint, busy); if (zwarn) cfield.lastChild.style.color = 'var(--warn)'; fields.append(cfield); @@ -2393,6 +2411,7 @@ function renderNotes(job) { else box.append(el('div', { class: 'note warn' }, 'This CHD depends on a parent CHD (SHA-1 ' + job.info.parent.slice(0, 12) + '…). Add the parent .chd file too.')); } if (job.state === 'error' && job.errorText) box.append(el('div', { class: 'note err' }, job.errorText)); + if (job.state === 'running' && job.noProgress) box.append(el('div', { class: 'note warn', 'data-note': 'no-progress' }, el('b', null, 'No progress for over a minute. '), STALL_HINT)); if (job.state === 'canceled') box.append(el('div', { class: 'note' }, 'Canceled.')); box.hidden = !box.childNodes.length; } @@ -2719,6 +2738,11 @@ async function pump() { } } +// the compression hint on phones and tablets (renderControls) +var PLAN_HINT = 'On a phone or tablet, Nearly as small, faster takes about a third less time, for files at most 0.3% bigger.'; +// the no-progress watchdog (Engine.run): a running job's card says so after this long without a word from it +var STALL_MS = 90000; +var STALL_HINT = 'The browser may have paused it, or it may be stuck. You can wait, or cancel it and try again, with fewer CPU threads (Settings) if it stops again.'; // chdman never finishes when it finds no data: its progress then reads "nan% complete" (0 of 0) var STALLED = 'chdman found no data to convert in these files and would never have finished, so it was stopped. Check that the .cue, .gdi or .toc file lists the disc\u2019s tracks.'; function stalledProgress(t) { return /\bnan% complete/i.test(t); } @@ -2737,6 +2761,7 @@ async function runJobNow(job) { return true; } job.stalled = false; + job.noProgress = false; job.log = job.retryLog || []; // a run again after the engine failed keeps the first run's log job.retryLog = null; job.outputs = []; @@ -2846,6 +2871,7 @@ async function runJobNow(job) { if (/^A helper thread stopped/.test(m.message)) job.helperStopped = true; }, onScan: function (f) { setProgress(job, null, 'Reading the ECM image\u2026 ' + Math.floor(f * 100) + '%'); }, + onStall: function (on) { job.noProgress = on; if (job.ui) renderNotes(job); }, onStorage: function (mode, reason) { job.storage = mode; if (spec.outMode !== 'opfs' || mode !== 'mem') return; @@ -3865,7 +3891,8 @@ async function cliRun() { var m = /([\d.]+)% complete/.exec(t); if (m) { prog.querySelector('.bar').classList.remove('indet'); prog.querySelector('.bar').setAttribute('aria-valuenow', Math.round(+m[1])); barI.style.width = m[1] + '%'; ptext.textContent = t.trim(); } }, - onNotice: function (m) { if (m.level !== 'debug') con.textContent += m.message + '\n'; } + onNotice: function (m) { if (m.level !== 'debug') con.textContent += m.message + '\n'; }, + onStall: function (on) { if (on) con.textContent += '[No progress for over a minute] ' + STALL_HINT + '\n'; } }); var r = await cli.run.promise; con.textContent += '\n[exit code ' + r.code + (r.error ? ': ' + r.error : '') + ']\n'; diff --git a/app/worker.js b/app/worker.js index 706b307..3a2a4d5 100644 --- a/app/worker.js +++ b/app/worker.js @@ -1336,6 +1336,8 @@ async function runJob(msg) { await loadModule(msg); var backing = await makeBacking(msg); postMessage({ type: 'storage', mode: backing.mode, reason: memReason }); + // testing: silent this long, as a stuck job is (the page's no-progress watchdog, DEBUG.freeze) + if (msg.debugFreeze) await new Promise(function (r) { setTimeout(r, msg.debugFreeze); }); // copies of inputs chdman must modify in place (verify --fix, addmeta, delmeta) var outFiles = []; diff --git a/dist/discpress.html b/dist/discpress.html index f9bec8b..81456be 100644 --- a/dist/discpress.html +++ b/dist/discpress.html @@ -1306,6 +1306,18 @@

About and licenses

if(bps >= 0.0) return bps; else +--- a/3rdparty/flac/src/libFLAC/stream_encoder.c ++++ b/3rdparty/flac/src/libFLAC/stream_encoder.c +@@ -4126,7 +4126,8 @@ + residual, + #endif + abs_residual_partition_sums+sum, +- raw_bits_per_partition+sum, ++ /* Discpress: null without escape coding, which C doesn't let an offset be added to */ ++ raw_bits_per_partition != NULL ? raw_bits_per_partition+sum : NULL, + residual_samples, + predictor_order, + rice_parameter_limit, --- a/3rdparty/flac/src/libFLAC/window.c +++ b/3rdparty/flac/src/libFLAC/window.c @@ -39,6 +39,7 @@ @@ -1496,6 +1508,53 @@

About and licenses

*destLen -= (SizeT)outStream.rem; if (outStream.overflow) +--- a/src/lib/util/avhuff.cpp ++++ b/src/lib/util/avhuff.cpp +@@ -67,6 +67,7 @@ + #include <cmath> + #include <cstdlib> + #include <cstring> ++#include <iterator> + #include <new> + + +@@ -723,6 +724,11 @@ + uint32_t width = get_u16be(&source[4]); + uint32_t height = get_u16be(&source[6]); + ++ // Discpress: no more channels than there are places for (the encoder takes up to 16): 0.289 went past ++ // the end of audiostart and of m_config.audio ++ if (channels > std::size(m_config.audio)) ++ return AVHERR_INVALID_DATA; ++ + // validate that the sizes make sense + if (complength < 10 + 2 * channels) + return AVHERR_INVALID_DATA; +@@ -932,6 +938,10 @@ + // if no huffman length, just copy the data + if (treesize == 0) + { ++ // Discpress: as much as the samples take (as stored): 0.289 read past the channel's ++ // data, and past the end of the hunk's ++ if (size < 2 * samples) ++ return AVHERR_INVALID_DATA; + const uint8_t *cursource = source; + for (int sampnum = 0; sampnum < samples; sampnum++) + { +--- a/src/lib/util/bitstream.h ++++ b/src/lib/util/bitstream.h +@@ -226,6 +226,11 @@ + + inline void bitstream_out::write(uint32_t newbits, int numbits) + { ++ // Discpress: nothing to write, as for a CHD map with no copies of hunks (0.289 went on to shift ++ // the value by 32 bits, which C++ leaves undefined, and then returned without using it) ++ if (numbits <= 0) ++ return; ++ + newbits <<= 32 - numbits; + + // flush the buffer if we're going to overflow it --- a/src/lib/util/cdrom.cpp +++ b/src/lib/util/cdrom.cpp @@ -28,6 +28,8 @@ @@ -1561,7 +1620,85 @@

About and licenses

const uint32_t index_offset = frame - track_start; int index = 0; -@@ -1153,204 +1177,106 @@ +@@ -897,6 +921,13 @@ + * @return A std::error_condition. + */ + ++// Discpress: chd.cpp's formats for the track metadata, with a width for each string that fills its 16-byte ++// buffer at most (the longest a CD has is 15 characters, VMODE2_FORM_MIX); a longer one, in damaged or ++// crafted metadata, doesn't match. 0.289 read them with %s, which wrote past the end of the buffer. ++static const char CDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d"; ++static const char CDROM_TRACK_METADATA2_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; ++static const char GDROM_TRACK_METADATA_FORMAT_READ[] = "TRACK:%d TYPE:%15s SUBTYPE:%15s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%15s PGSUB:%15s POSTGAP:%d"; ++ + std::error_condition cdrom_file::parse_metadata(chd_file *chd, toc &toc) + { + std::string metadata; +@@ -924,12 +955,12 @@ + // fetch the metadata for this track + if (!chd->read_metadata(CDROM_TRACK_METADATA_TAG, toc.numtrks, metadata)) + { +- if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames) != 4) ++ if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames) != 4) + return chd_file::error::INVALID_DATA; + } + else if (!chd->read_metadata(CDROM_TRACK_METADATA2_TAG, toc.numtrks, metadata)) + { +- if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) ++ if (sscanf(metadata.c_str(), CDROM_TRACK_METADATA2_FORMAT_READ, &tracknum, type, subtype, &frames, &pregap, pgtype, pgsub, &postgap) != 8) + return chd_file::error::INVALID_DATA; + } + else +@@ -944,7 +975,7 @@ + if (err) + break; + +- if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) ++ if (sscanf(metadata.c_str(), GDROM_TRACK_METADATA_FORMAT_READ, &tracknum, type, subtype, &frames, &padframes, &pregap, pgtype, pgsub, &postgap) != 9) + return chd_file::error::INVALID_DATA; + + toc.flags |= CD_FLAG_GDROM; +@@ -995,7 +1026,14 @@ + + /* if we got any tracks this way, we're done */ + if (toc.numtrks > 0) ++ { ++ // Discpress: with each track once: a track number that repeats or skips one (damaged or crafted ++ // metadata) left a track of nothing, of 0-byte sectors, which extractcd divided by ++ for (uint32_t i = 0; i < toc.numtrks; i++) ++ if (toc.tracks[i].datasize == 0) ++ return chd_file::error::INVALID_DATA; + return std::error_condition(); ++ } + + osd_printf_info("toc.numtrks = %u?!\n", toc.numtrks); + +@@ -1004,6 +1042,9 @@ + err = chd->read_metadata(CDROM_OLD_METADATA_TAG, 0, oldmetadata); + if (err) + return err; ++ // Discpress: as long as the table it holds (0.289 read past the end of shorter metadata) ++ if (oldmetadata.size() < 4 + MAX_TRACKS * 6 * 4) ++ return chd_file::error::INVALID_DATA; + + /* reconstruct the TOC from it */ + auto *mrp = reinterpret_cast<uint32_t *>(&oldmetadata[0]); +@@ -1044,6 +1085,14 @@ + } + } + ++ // Discpress: no more tracks than the table holds, with sectors a CD has: anything else (damaged or ++ // crafted metadata) made 0.289 read past the end of the table, and of a sector's buffer ++ if (toc.numtrks > MAX_TRACKS) ++ return chd_file::error::INVALID_DATA; ++ for (uint32_t i = 0; i < toc.numtrks; i++) ++ if (toc.tracks[i].datasize == 0 || toc.tracks[i].datasize > MAX_SECTOR_DATA || toc.tracks[i].subsize > MAX_SUBCODE_DATA) ++ return chd_file::error::INVALID_DATA; ++ + return std::error_condition(); + } + +@@ -1153,204 +1202,106 @@ 0x50, 0xa4, 0xa5, 0x51, 0xa7, 0x53, 0x52, 0xa6, 0xa3, 0x57, 0x56, 0xa2, 0x54, 0xa0, 0xa1, 0x55 }; @@ -1801,10 +1938,7 @@

About and licenses

+ uint64_t a[11] = { }, b[11] = { }; + int const count = q ? ECC_Q_NUM_BYTES : ECC_P_NUM_BYTES; + if (!q) - { -- val1 ^= ecc_source_byte(sector, row[component]); -- val2 ^= ecc_source_byte(sector, row[component]); -- val1 = ecclow[val1]; ++ { + // the columns of the first 24 rows (a row's last word also reads 2 bytes of the next row, unused) + for (int row = 0; row < ECC_P_COMP; row++) + for (int l = 0; l < 11; l++) @@ -1839,7 +1973,10 @@

About and licenses

+ memcpy(av, a, sizeof(a)); + memcpy(bv, b, sizeof(b)); + for (int i = 0; i < count; i++) -+ { + { +- val1 ^= ecc_source_byte(sector, row[component]); +- val2 ^= ecc_source_byte(sector, row[component]); +- val1 = ecclow[val1]; + uint8_t const p0 = ecchigh[ecclow[av[i]] ^ bv[i]]; + parity[i] = p0; + parity[count + i] = p0 ^ bv[i]; @@ -1849,7 +1986,7 @@

About and licenses

} /** -@@ -1367,24 +1293,17 @@ +@@ -1367,24 +1318,17 @@ bool cdrom_file::ecc_verify(const uint8_t *sector) { @@ -1882,7 +2019,7 @@

About and licenses

} /** -@@ -1400,13 +1319,13 @@ +@@ -1400,13 +1344,13 @@ void cdrom_file::ecc_generate(uint8_t *sector) { @@ -1902,7 +2039,7 @@

About and licenses

} /** -@@ -1578,22 +1497,25 @@ +@@ -1578,22 +1522,25 @@ int cdrom_file::msf_to_frames(const char *token) { @@ -1942,7 +2079,7 @@

About and licenses

} /*------------------------------------------------- -@@ -1604,17 +1526,18 @@ +@@ -1604,17 +1551,18 @@ -------------------------------------------------*/ /** @@ -1963,7 +2100,7 @@

About and licenses

{ unsigned long offset = 0; uint32_t length, rate, filesize; -@@ -1633,7 +1556,7 @@ +@@ -1633,7 +1581,7 @@ } /* read the core header and make sure it's a WAVE file */ @@ -1972,7 +2109,7 @@

About and licenses

offset += actual; if (offset < 4) { -@@ -1647,7 +1570,7 @@ +@@ -1647,7 +1595,7 @@ } /* get the total size */ @@ -1981,7 +2118,7 @@

About and licenses

offset += actual; if (offset < 8) { -@@ -1657,7 +1580,7 @@ +@@ -1657,7 +1605,7 @@ filesize = little_endianize_int32(filesize); /* read the RIFF file type and make sure it's a WAVE file */ @@ -1990,7 +2127,7 @@

About and licenses

offset += actual; if (offset < 12) { -@@ -1673,9 +1596,9 @@ +@@ -1673,9 +1621,9 @@ /* seek until we find a format tag */ while (true) { @@ -2002,7 +2139,7 @@

About and licenses

offset += actual; length = little_endianize_int32(length); if (memcmp(&buf[0], "fmt ", 4) == 0) -@@ -1691,7 +1614,7 @@ +@@ -1691,7 +1639,7 @@ } /* read the format -- make sure it is PCM */ @@ -2011,7 +2148,7 @@

About and licenses

offset += actual; temp16 = little_endianize_int16(temp16); if (temp16 != 1) -@@ -1701,7 +1624,7 @@ +@@ -1701,7 +1649,7 @@ } /* number of channels -- only stereo is supported */ @@ -2020,7 +2157,7 @@

About and licenses

offset += actual; temp16 = little_endianize_int16(temp16); if (temp16 != 2) -@@ -1711,7 +1634,7 @@ +@@ -1711,7 +1659,7 @@ } /* sample rate */ @@ -2029,7 +2166,7 @@

About and licenses

offset += actual; rate = little_endianize_int32(rate); if (rate != 44100) -@@ -1721,11 +1644,11 @@ +@@ -1721,11 +1669,11 @@ } /* bytes/second and block alignment are ignored */ @@ -2043,7 +2180,7 @@

About and licenses

offset += actual; bits = little_endianize_int16(bits); if (bits != 16) -@@ -1740,9 +1663,9 @@ +@@ -1740,9 +1688,9 @@ /* seek until we find a data tag */ while (true) { @@ -2055,7 +2192,7 @@

About and licenses

offset += actual; length = little_endianize_int32(length); if (memcmp(&buf[0], "data", 4) == 0) -@@ -1764,7 +1687,12 @@ +@@ -1764,7 +1712,12 @@ return 0; } @@ -2069,7 +2206,7 @@

About and licenses

return length; } -@@ -1864,11 +1792,11 @@ +@@ -1864,11 +1817,11 @@ outtoc.numsessions = 1; @@ -2085,7 +2222,7 @@

About and licenses

{ osd_printf_error("ERROR: Not a Nero 5.5 or later image!\n"); fclose(infile); -@@ -1888,8 +1816,13 @@ +@@ -1888,8 +1841,13 @@ while (!done) { @@ -2101,7 +2238,7 @@

About and licenses

chunk_size = get_u32be(&buffer[4]); -@@ -1901,15 +1834,20 @@ +@@ -1901,15 +1859,20 @@ // skip second chunk size and UPC code fseek(infile, 20, SEEK_CUR); @@ -2124,7 +2261,7 @@

About and licenses

for (int track = start; track <= end; track++) { uint32_t size, mode; -@@ -1925,7 +1863,15 @@ +@@ -1925,7 +1888,15 @@ // printf("Track %d: sector size %d mode %x index0 %llx index1 %llx track_end %llx (pregap %d sectors, length %d sectors)\n", track, size, mode, index0, index1, track_end, (uint32_t)(index1-index0)/size, (uint32_t)(track_end-index1)/size); outinfo.track[track-1].fname.assign(tocfname); @@ -2141,7 +2278,7 @@

About and licenses

outinfo.track[track-1].idx[0] = outinfo.track[track-1].idx[1] = 0; switch (mode) -@@ -1976,21 +1922,29 @@ +@@ -1976,21 +1947,29 @@ return chd_file::error::UNSUPPORTED_FORMAT; } @@ -2176,7 +2313,7 @@

About and licenses

} } -@@ -2000,7 +1954,15 @@ +@@ -2000,7 +1979,15 @@ } else { @@ -2193,7 +2330,7 @@

About and licenses

} } -@@ -2042,6 +2004,20 @@ +@@ -2042,6 +2029,20 @@ outinfo.reset(); uint64_t size = get_file_size(tocfname); @@ -2214,7 +2351,7 @@

About and licenses

fclose(infile); -@@ -2052,7 +2028,15 @@ +@@ -2052,7 +2053,15 @@ outinfo.track[0].offset = 0; outinfo.track[0].idx[0] = outinfo.track[0].idx[1] = 0; @@ -2231,7 +2368,7 @@

About and licenses

{ outtoc.tracks[0].trktype = CD_TRACK_MODE1; outtoc.tracks[0].frames = size / 2048; -@@ -2149,7 +2133,7 @@ +@@ -2149,7 +2158,7 @@ const int numtracks = atoi(token); @@ -2240,7 +2377,7 @@

About and licenses

{ osd_printf_error("GDI expects too many tracks. Expected %d tracks but only up to %zu tracks allowed\n", numtracks, std::size(outinfo.track) + 1); return chd_file::error::INVALID_DATA; -@@ -2178,7 +2162,7 @@ +@@ -2178,7 +2187,7 @@ paramcnt++; const int trknum = atoi(token) - 1; @@ -2249,7 +2386,7 @@

About and licenses

{ osd_printf_error("Track %d is out of expected range of 1 to %d\n", trknum + 1, numtracks); return chd_file::error::INVALID_DATA; -@@ -2222,7 +2206,7 @@ +@@ -2222,7 +2231,7 @@ outtoc.tracks[trknum].trktype = CD_TRACK_MODE1; outtoc.tracks[trknum].datasize = 2048; } @@ -2258,7 +2395,7 @@

About and licenses

{ outtoc.tracks[trknum].trktype = CD_TRACK_AUDIO; outtoc.tracks[trknum].datasize = 2352; -@@ -2252,6 +2236,12 @@ +@@ -2252,6 +2261,12 @@ outinfo.track[trknum].fname.assign(path).append(token); const uint64_t sz = get_file_size(outinfo.track[trknum].fname); @@ -2271,7 +2408,7 @@

About and licenses

outtoc.tracks[trknum].frames = sz / trksize; outtoc.tracks[trknum].padframes = 0; -@@ -2345,6 +2335,19 @@ +@@ -2345,6 +2360,19 @@ enum gdi_area current_area = SINGLE_DENSITY; bool is_multibin = false; int leadin = -1; @@ -2291,7 +2428,7 @@

About and licenses

FILE *infile = fopen(path.c_str(), "rt"); if (!infile) -@@ -2371,6 +2374,14 @@ +@@ -2371,6 +2399,14 @@ char linebuffer[512]; memset(linebuffer, 0, sizeof(linebuffer)); @@ -2306,7 +2443,7 @@

About and licenses

while (!feof(infile)) { /* get the next line */ -@@ -2427,6 +2438,8 @@ +@@ -2427,6 +2463,8 @@ /* get lead-out time */ TOKENIZE int leadout_offset = msf_to_frames(token); @@ -2315,7 +2452,7 @@

About and licenses

outinfo.track[trknum].leadout = leadout_offset; } else if (!strncmp(linebuffer+i, "LEAD-IN", 7)) -@@ -2472,13 +2485,18 @@ +@@ -2472,13 +2510,18 @@ /* get the file type */ TOKENIZE @@ -2336,7 +2473,7 @@

About and licenses

} else if (!strcmp(token, "WAVE")) { -@@ -2489,6 +2507,8 @@ +@@ -2489,6 +2532,8 @@ osd_printf_error("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname); return chd_file::error::INVALID_DATA; } @@ -2345,7 +2482,7 @@

About and licenses

} else { -@@ -2502,6 +2522,12 @@ +@@ -2502,6 +2547,12 @@ /* get the track number */ TOKENIZE trknum = strtoul(token, nullptr, 10) - 1; @@ -2358,7 +2495,7 @@

About and licenses

/* next token on the line is the track type */ TOKENIZE -@@ -2541,6 +2567,10 @@ +@@ -2541,6 +2592,10 @@ outinfo.track[trknum].offset = wavoffs; wavoffs = wavlen = 0; } @@ -2369,7 +2506,7 @@

About and licenses

outinfo.track[trknum].fname.assign(lastfname); /* default filename to the last one */ -@@ -2586,6 +2616,54 @@ +@@ -2586,6 +2641,54 @@ osd_printf_error("ERROR: encountered invalid index %d\n", idx); return chd_file::error::INVALID_DATA; } @@ -2424,7 +2561,7 @@

About and licenses

outinfo.track[trknum].idx[idx] = frames; -@@ -2610,6 +2688,8 @@ +@@ -2610,6 +2713,8 @@ /* get index */ TOKENIZE frames = msf_to_frames(token); @@ -2433,7 +2570,7 @@

About and licenses

outtoc.tracks[trknum].pregap = frames; } -@@ -2620,11 +2700,15 @@ +@@ -2620,11 +2725,15 @@ /* get index */ TOKENIZE frames = msf_to_frames(token); @@ -2449,7 +2586,7 @@

About and licenses

outtoc.tracks[trknum].control_flags = 0; /* keep looping over remaining tokens in FLAGS line until there's no more to read */ -@@ -2667,18 +2751,51 @@ +@@ -2667,18 +2776,51 @@ } /* this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian */ @@ -2503,7 +2640,7 @@

About and licenses

tlen = get_file_size(outinfo.track[trknum].fname); if (tlen == 0) { -@@ -2700,11 +2817,17 @@ +@@ -2700,11 +2842,17 @@ return chd_file::error::INVALID_DATA; } @@ -2522,7 +2659,7 @@

About and licenses

} else if (outtoc.tracks[trknum].frames == 0) { -@@ -2777,6 +2900,77 @@ +@@ -2777,6 +2925,77 @@ } } @@ -2600,7 +2737,7 @@

About and licenses

if (is_gdrom) { /* -@@ -2787,6 +2981,18 @@ +@@ -2787,6 +3006,18 @@ uint32_t this_pregap = outtoc.tracks[trknum].pregap; uint32_t this_offset = this_pregap * (outtoc.tracks[trknum].datasize + outtoc.tracks[trknum].subsize); @@ -2619,7 +2756,7 @@

About and licenses

outtoc.tracks[trknum-1].frames += this_pregap; outtoc.tracks[trknum-1].splitframes += this_pregap; -@@ -2808,7 +3014,7 @@ +@@ -2808,7 +3039,7 @@ outtoc.tracks[trknum].physframeofs = 45000; int dif=outtoc.tracks[trknum].physframeofs-(outtoc.tracks[trknum-1].frames+outtoc.tracks[trknum-1].physframeofs); outtoc.tracks[trknum-1].frames += dif; @@ -2628,7 +2765,7 @@

About and licenses

} else { -@@ -2965,6 +3171,67 @@ +@@ -2965,6 +3196,67 @@ char linebuffer[512]; memset(linebuffer, 0, sizeof(linebuffer)); @@ -2696,7 +2833,7 @@

About and licenses

while (!feof(infile)) { /* get the next line */ -@@ -2989,6 +3256,8 @@ +@@ -2989,6 +3281,8 @@ */ if (!strcmp(token, "NO")) { @@ -2705,7 +2842,7 @@

About and licenses

TOKENIZE if (!strcmp(token, "COPY")) outtoc.tracks[trknum].control_flags &= ~CD_FLAG_CONTROL_DIGITAL_COPY_PERMITTED; -@@ -2997,100 +3266,210 @@ +@@ -2997,100 +3291,210 @@ } else if (!strcmp(token, "COPY")) { @@ -2959,7 +3096,7 @@

About and licenses

/* next token on the line is the track type */ TOKENIZE -@@ -3117,22 +3496,128 @@ +@@ -3117,22 +3521,128 @@ } else if (!strcmp(token, "START")) { @@ -3163,7 +3300,11 @@

About and licenses

static uint64_t read_uint64(FILE *infile); --- a/src/lib/util/chd.cpp +++ b/src/lib/util/chd.cpp -@@ -28,6 +28,20 @@ +@@ -25,9 +25,24 @@ + #include <cstdlib> + #include <cstring> + #include <ctime> ++#include <limits> #include <new> #include <tuple> @@ -3184,7 +3325,7 @@

About and licenses

//************************************************************************** // CONSTANTS -@@ -853,6 +867,17 @@ +@@ -853,6 +868,17 @@ void chd_file::close() { @@ -3202,7 +3343,7 @@

About and licenses

// reset file characteristics m_file.reset(); m_allow_reads = false; -@@ -892,6 +917,48 @@ +@@ -892,6 +918,48 @@ m_cachehunk = ~0; } @@ -3251,17 +3392,20 @@

About and licenses

std::error_condition chd_file::codec_process_hunk(uint32_t hunknum) { // punt if no file -@@ -919,6 +986,9 @@ +@@ -919,6 +987,12 @@ case V34_MAP_ENTRY_TYPE_COMPRESSED: { uint32_t const blocklen = get_u16be(&rawmap[12]) | (uint32_t(rawmap[14]) << 16); + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -931,7 +1001,14 @@ +@@ -931,11 +1005,21 @@ return std::error_condition(error::UNSUPPORTED_FORMAT); case V34_MAP_ENTRY_TYPE_SELF_HUNK: @@ -3277,17 +3421,27 @@

About and licenses

case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) -@@ -958,6 +1035,9 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->codec_process_hunk(blockoffs); + } + } +@@ -958,6 +1042,12 @@ case COMPRESSION_TYPE_2: case COMPRESSION_TYPE_3: { + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -970,7 +1050,14 @@ +@@ -970,11 +1060,24 @@ return std::error_condition(error::UNSUPPORTED_FORMAT); case COMPRESSION_SELF: @@ -3303,17 +3457,30 @@

About and licenses

case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) -@@ -1045,6 +1132,9 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: a parent whose units are larger than its hunks has no hunk for a unit (0.289 divided by zero) ++ if (UNEXPECTED(m_parent->unit_bytes() > m_parent->hunk_bytes())) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->codec_process_hunk(blockoffs / (m_parent->hunk_bytes() / m_parent->unit_bytes())); + } + break; +@@ -1045,6 +1148,12 @@ case V34_MAP_ENTRY_TYPE_COMPRESSED: { uint32_t const blocklen = get_u16be(&rawmap[12]) | (uint32_t(rawmap[14]) << 16); + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a hunk compressed in a CHD that says it isn't: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[0])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -1073,7 +1163,14 @@ +@@ -1073,11 +1182,21 @@ return std::error_condition(); case V34_MAP_ENTRY_TYPE_SELF_HUNK: @@ -3329,7 +3496,14 @@

About and licenses

case V34_MAP_ENTRY_TYPE_PARENT_HUNK: if (UNEXPECTED(m_parent_missing)) -@@ -1115,6 +1212,14 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->read_hunk(blockoffs, dest); + } + } +@@ -1115,6 +1234,17 @@ case COMPRESSION_TYPE_2: case COMPRESSION_TYPE_3: { @@ -3340,11 +3514,14 @@

About and licenses

+#endif + // Discpress: more than the buffer holds (a damaged or crafted map); 0.289 read past its end + if (UNEXPECTED(blocklen > m_compressed.size())) ++ return std::error_condition(error::INVALID_DATA); ++ // Discpress: and a codec slot the header leaves empty: 0.289 called a codec it doesn't have ++ if (UNEXPECTED(!m_decompressor[rawmap[0]])) + return std::error_condition(error::INVALID_DATA); std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen); if (UNEXPECTED(err)) return err; -@@ -1130,16 +1235,29 @@ +@@ -1130,20 +1260,36 @@ case COMPRESSION_NONE: { @@ -3375,7 +3552,169 @@

About and licenses

case COMPRESSION_PARENT: if (UNEXPECTED(m_parent_missing)) -@@ -2173,6 +2291,10 @@ + return std::error_condition(error::REQUIRES_PARENT); ++ // Discpress: and a CHD without a parent whose map says so (damaged or crafted): 0.289 read through a null pointer ++ if (UNEXPECTED(!m_parent)) ++ return std::error_condition(error::INVALID_DATA); + return m_parent->read_bytes(blockoffs * m_parent->unit_bytes(), dest, m_hunkbytes); + } + } +@@ -1307,6 +1453,11 @@ + + std::error_condition chd_file::read_bytes(uint64_t offset, void *buffer, uint32_t bytes) + { ++ // Discpress: nothing to read, as for a CD track without data (a damaged or crafted CHD's): 0.289's ++ // last hunk wrapped around at offset 0, and it read a whole hunk into the buffer ++ if (bytes == 0) ++ return std::error_condition(); ++ + // iterate over hunks + uint32_t const first_hunk = offset / m_hunkbytes; + uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; +@@ -1360,6 +1511,10 @@ + + std::error_condition chd_file::write_bytes(uint64_t offset, const void *buffer, uint32_t bytes) + { ++ // Discpress: nothing to write (as read_bytes) ++ if (bytes == 0) ++ return std::error_condition(); ++ + // iterate over hunks + uint32_t const first_hunk = offset / m_hunkbytes; + uint32_t const last_hunk = (offset + bytes - 1) / m_hunkbytes; +@@ -1453,7 +1608,7 @@ + // read the metadata + try { output.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- return file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); ++ return file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty + } + + /** +@@ -1515,7 +1670,7 @@ + // read the metadata + try { output.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &output[0], metaentry.length); ++ err = file_read(metaentry.offset + METADATA_HEADER_SIZE, output.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + return err; + resulttag = metaentry.metatag; +@@ -1678,12 +1833,12 @@ + // read the metadata item + try { filedata.resize(metaentry.length); } + catch (std::bad_alloc const &) { return std::errc::not_enough_memory; } +- err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); ++ err = source.file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + return err; + + // write it to the destination +- err = write_metadata(metaentry.metatag, (uint32_t)-1, &filedata[0], metaentry.length, metaentry.flags); ++ err = write_metadata(metaentry.metatag, (uint32_t)-1, filedata.data(), metaentry.length, metaentry.flags); + if (UNEXPECTED(err)) + return err; + } +@@ -1725,14 +1880,14 @@ + + // allocate memory and read the data + filedata.resize(metaentry.length); +- err = file_read(metaentry.offset + METADATA_HEADER_SIZE, &filedata[0], metaentry.length); ++ err = file_read(metaentry.offset + METADATA_HEADER_SIZE, filedata.data(), metaentry.length); // Discpress: data(), as metadata can be empty + if (UNEXPECTED(err)) + throw err; + + // create an entry for this metadata and add it + metadata_hash hashentry; + put_u32be(hashentry.tag, metaentry.metatag); +- hashentry.sha1 = util::sha1_creator::simple(&filedata[0], metaentry.length); ++ hashentry.sha1 = util::sha1_creator::simple(filedata.data(), metaentry.length); + hasharray.push_back(hashentry); + } + if (err != error::METADATA_NOT_FOUND) +@@ -1880,6 +2035,41 @@ + } + + /** ++ * @fn bool chd_file::past_end(uint64_t offset, uint64_t length) const ++ * @brief past_end - Discpress: whether length bytes from offset would run past the end of the ++ * file (false when its length is unknown, so that the read reports it) ++ */ ++bool chd_file::past_end(uint64_t offset, uint64_t length) const ++{ ++ uint64_t filelength; ++ if (!m_file || m_file->length(filelength)) ++ return false; ++ return (offset > filelength) || (length > filelength - offset); ++} ++ ++/** ++ * @fn void chd_file::check_sizes(uint64_t hunkcount) const ++ * ++ * @brief ------------------------------------------------- ++ * check_sizes - Discpress: a header whose sizes no data can have (a damaged or ++ * crafted file) is an error. 0.289 divided by a hunk or unit size of 0, and with a ++ * logical size of more hunks than a map can count, it made a map too small for them ++ * -------------------------------------------------. ++ * ++ * @exception CHDERR_INVALID_FILE Thrown when the sizes can't be right. ++ * ++ * @param hunkcount The number of hunks the header gives or implies. ++ */ ++ ++void chd_file::check_sizes(uint64_t hunkcount) const ++{ ++ if (UNEXPECTED(m_hunkbytes == 0 || m_unitbytes == 0)) ++ throw std::error_condition(error::INVALID_FILE); ++ if (UNEXPECTED(hunkcount > std::numeric_limits<uint32_t>::max())) ++ throw std::error_condition(error::INVALID_FILE); ++} ++ ++/** + * @fn void chd_file::parse_v3_header(uint8_t *rawheader, sha1_t &parentsha1) + * + * @brief ------------------------------------------------- +@@ -1938,7 +2128,8 @@ + parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); + + // guess at the units based on snooping the metadata +- m_unitbytes = guess_unitbytes(); ++ m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; ++ check_sizes(m_hunkcount); + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + } + +@@ -2001,7 +2192,8 @@ + parentsha1 = be_read_sha1(&rawheader[m_parentsha1_offset]); + + // guess at the units based on snooping the metadata +- m_unitbytes = guess_unitbytes(); ++ m_unitbytes = (m_hunkbytes != 0) ? guess_unitbytes() : 0; ++ check_sizes(m_hunkcount); + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + } + +@@ -2029,8 +2221,9 @@ + m_mapoffset = get_u64be(&rawheader[40]); + m_metaoffset = get_u64be(&rawheader[48]); + m_hunkbytes = get_u32be(&rawheader[56]); +- m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; + m_unitbytes = get_u32be(&rawheader[60]); ++ check_sizes((m_hunkbytes != 0) ? ((m_logicalbytes / m_hunkbytes) + ((m_logicalbytes % m_hunkbytes) != 0)) : 0); ++ m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes; + m_unitcount = (m_logicalbytes + m_unitbytes - 1) / m_unitbytes; + + // determine compression +@@ -2073,7 +2266,7 @@ + try + { + // first get a CRC-16 of the original rawmap +- util::crc16_t mapcrc = util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12); ++ util::crc16_t mapcrc = util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12); + + // create a buffer to hold the RLE data + std::vector<uint8_t> compression_rle(m_hunkcount); +@@ -2173,6 +2366,10 @@ // for COMPRESSION_PARENT: parentbits // the overall size is clamped later with bitbuf.flush() int nbits_needed = (8*16) + (12 + std::max<int>({lengthbits+16, selfbits, parentbits}))*m_hunkcount; @@ -3386,7 +3725,7 @@

About and licenses

std::vector<uint8_t> compressed(nbits_needed / 8 + 1); bitstream_out bitbuf(&compressed[16], compressed.size() - 16); -@@ -2255,7 +2377,8 @@ +@@ -2255,7 +2452,8 @@ // write the map header uint32_t complen = bitbuf.flush(); @@ -3396,7 +3735,75 @@

About and licenses

put_u32be(&compressed[0], complen); put_u48be(&compressed[4], firstoffs); put_u16be(&compressed[10], mapcrc); -@@ -2966,6 +3089,13 @@ +@@ -2298,7 +2496,7 @@ + // if no offset, we haven't written it yet + if (m_mapoffset == 0) + { +- memset(&m_rawmap[0], 0xff, m_rawmap.size()); ++ memset(m_rawmap.data(), 0xff, m_rawmap.size()); + return; + } + +@@ -2314,13 +2512,21 @@ + uint8_t const lengthbits = rawbuf[12]; + uint8_t const selfbits = rawbuf[13]; + uint8_t const parentbits = rawbuf[14]; ++ // Discpress: the bit reader reads at most 32 bits at a time, as many as compress_v5_map writes: ++ // more (a damaged or crafted map) shifted by a negative count, which C++ leaves undefined ++ if (UNEXPECTED((lengthbits > 32) || (selfbits > 32) || (parentbits > 32))) ++ throw std::error_condition(error::DECOMPRESSION_ERROR); + + // now read the map ++ // Discpress: one that would run past the end of the file is the error reading it gives, without ++ // allocating it first (up to 4 GB, for a damaged or crafted header) ++ if (UNEXPECTED(past_end(m_mapoffset + 16, mapbytes))) ++ throw std::error_condition(std::errc::io_error); + std::vector<uint8_t> compressed(mapbytes); +- ioerr = file_read(m_mapoffset + 16, &compressed[0], mapbytes); ++ ioerr = file_read(m_mapoffset + 16, compressed.data(), mapbytes); + if (UNEXPECTED(ioerr)) + throw ioerr; +- bitstream_in bitbuf(&compressed[0], compressed.size()); ++ bitstream_in bitbuf(compressed.data(), compressed.size()); + + // first decode the compression types + huffman_decoder<16, 8> decoder; +@@ -2409,7 +2615,7 @@ + } + + // verify the final CRC +- if (UNEXPECTED(util::crc16_creator::simple(&m_rawmap[0], m_hunkcount * 12) != mapcrc)) ++ if (UNEXPECTED(util::crc16_creator::simple(m_rawmap.data(), m_hunkcount * 12) != mapcrc)) + throw std::error_condition(error::DECOMPRESSION_ERROR); + } + +@@ -2636,14 +2842,23 @@ + } + + // read the map; v5+ compressed drives need to read and decompress their map +- m_rawmap.resize(m_hunkcount * m_mapentrybytes); ++ // Discpress: a map larger than 32 bits can count (a damaged or crafted header) is an error: 0.289 ++ // computed its size in 32 bits, and with the size wrapped, read and wrote past the end of the map ++ uint64_t const mapsize = uint64_t(m_hunkcount) * m_mapentrybytes; ++ if (UNEXPECTED(mapsize > std::numeric_limits<uint32_t>::max())) ++ throw std::error_condition(error::INVALID_FILE); ++ // Discpress: and a stored map that would run past the end of the file is the error reading it ++ // gives, without allocating it first ++ if (!(m_version >= 5 && compressed()) && UNEXPECTED(past_end(m_mapoffset, mapsize))) ++ throw std::error_condition(std::errc::io_error); ++ m_rawmap.resize(mapsize); + if (m_version >= 5 && compressed()) + { + decompress_v5_map(); + } + else + { +- std::error_condition err = file_read(m_mapoffset, &m_rawmap[0], m_rawmap.size()); ++ std::error_condition err = file_read(m_mapoffset, m_rawmap.data(), m_rawmap.size()); + if (UNEXPECTED(err)) + throw err; + } +@@ -2966,6 +3181,13 @@ chd_file_compressor::~chd_file_compressor() { @@ -3410,7 +3817,7 @@

About and licenses

// free the work queues osd_work_queue_free(m_read_queue); osd_work_queue_free(m_work_queue); -@@ -3022,6 +3152,10 @@ +@@ -3022,6 +3244,10 @@ // reset write state m_write_hunk = 0; @@ -3421,7 +3828,7 @@

About and licenses

} /** -@@ -3123,6 +3257,11 @@ +@@ -3123,6 +3349,11 @@ } else { @@ -3433,7 +3840,7 @@

About and licenses

// otherwise, append it compressed and add to the self map hunk_write_compressed(item.m_hunknum, item.m_compression, item.m_compressed, item.m_complen, item.m_hash[0].m_crc16); m_total_out += item.m_complen; -@@ -3263,12 +3402,44 @@ +@@ -3263,12 +3494,44 @@ // TODO: data race if ((m_current_map.find(item.m_hash[0].m_crc16, item.m_hash[0].m_sha1) == hashmap::NOT_FOUND) && (m_parent_map.find(item.m_hash[0].m_crc16, item.m_hash[0].m_sha1) == hashmap::NOT_FOUND)) @@ -3479,7 +3886,7 @@

About and licenses

/** * @fn void *chd_file_compressor::async_read_static(void *param, int threadid) * -@@ -3345,6 +3516,16 @@ +@@ -3345,6 +3608,16 @@ assert(item.m_status == WS_READING); item.m_status = WS_QUEUED; item.m_hunknum = hunknum; @@ -3496,7 +3903,7 @@

About and licenses

item.m_osd = osd_work_item_queue(m_work_queue, m_walking_parent ? async_walk_parent_static : async_compress_hunk_static, &item, 0); } -@@ -3512,3 +3693,184 @@ +@@ -3512,3 +3785,184 @@ metadata_entry metaentry; return metadata_find(AV_METADATA_TAG, 0, metaentry); } @@ -3664,7 +4071,7 @@

About and licenses

+ memcpy(dest, &ahead.data[0], m_hunkbytes); + return true; + case 3: // compressed bytes -+ m_decompressor[type]->decompress(&ahead.data[0], ahead.data.size(), reinterpret_cast<uint8_t *>(dest), m_hunkbytes); ++ m_decompressor[type]->decompress(ahead.data.data(), ahead.data.size(), reinterpret_cast<uint8_t *>(dest), m_hunkbytes); + return true; + default: + return false; @@ -3724,7 +4131,16 @@

About and licenses

private: struct metadata_entry; struct metadata_hash; -@@ -391,6 +411,7 @@ +@@ -380,6 +400,8 @@ + + // internal helpers + uint32_t guess_unitbytes(); ++ void check_sizes(uint64_t hunkcount) const; // Discpress ++ bool past_end(uint64_t offset, uint64_t length) const; // Discpress + void parse_v3_header(uint8_t *rawheader, util::sha1_t &parentsha1); + void parse_v4_header(uint8_t *rawheader, util::sha1_t &parentsha1); + void parse_v5_header(uint8_t *rawheader, util::sha1_t &parentsha1); +@@ -391,6 +413,7 @@ std::error_condition verify_proper_compression_append(uint32_t hunknum) const noexcept; void hunk_write_compressed(uint32_t hunknum, int8_t compression, const uint8_t *compressed, uint32_t complength, util::crc16_t crc16); void hunk_copy_from_self(uint32_t hunknum, uint32_t otherhunk); @@ -3732,7 +4148,7 @@

About and licenses

void hunk_copy_from_parent(uint32_t hunknum, uint64_t parentunit); std::error_condition metadata_find(chd_metadata_tag metatag, int32_t metaindex, metadata_entry &metaentry, bool resume = false) const noexcept; std::error_condition metadata_set_previous_next(uint64_t prevoffset, uint64_t nextoffset) noexcept; -@@ -433,6 +454,21 @@ +@@ -433,6 +456,21 @@ // caching std::vector<uint8_t> m_cache; // single-hunk cache for partial reads/writes uint32_t m_cachehunk; // which hunk is in the cache? @@ -3754,7 +4170,7 @@

About and licenses

}; -@@ -450,11 +486,28 @@ +@@ -450,11 +488,28 @@ void compress_begin(); std::error_condition compress_continue(double &progress, double &ratio); @@ -3783,7 +4199,7 @@

About and licenses

// hash map for looking up values class hashmap { -@@ -524,6 +577,7 @@ +@@ -524,6 +579,7 @@ , m_complen(0) , m_compression(0) , m_codecs(nullptr) @@ -3791,7 +4207,7 @@

About and licenses

{ } osd_work_item * m_osd; // OSD work item running on this block -@@ -537,6 +591,7 @@ +@@ -537,6 +593,7 @@ uint32_t m_complen; // compressed data length int8_t m_compression; // type of compression used chd_compressor_group *m_codecs; // codec instance @@ -3799,7 +4215,7 @@

About and licenses

std::vector<hash_pair> m_hash; // array of hashes }; -@@ -549,6 +604,7 @@ +@@ -549,6 +606,7 @@ void async_read(); // current compression status @@ -3874,7 +4290,24 @@

About and licenses

return header_bytes + complen + m_subcode_compressor.compress(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA], frames * cdrom_file::MAX_SUBCODE_DATA, &dest[header_bytes + complen]); } -@@ -735,17 +751,33 @@ +@@ -426,8 +442,16 @@ + uint32_t ecc_bytes = (frames + 7) / 8; + uint32_t header_bytes = ecc_bytes + complen_bytes; + ++ // Discpress: a header longer than the data, or a base longer than what follows the header (a ++ // damaged or crafted hunk), is an error: 0.289 decoded past the end of the data, and the ++ // subcode's length wrapped ++ if (UNEXPECTED(complen < header_bytes)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); ++ + // extract compressed length of base + uint32_t complen_base = (complen_bytes > 2) ? get_u24be(&src[ecc_bytes]) : get_u16be(&src[ecc_bytes]); ++ if (UNEXPECTED(complen_base > complen - header_bytes)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); + + // reset and decode + m_base_decompressor.decompress(&src[header_bytes], complen_base, &m_buffer[0], frames * cdrom_file::MAX_SECTOR_DATA); +@@ -735,17 +759,33 @@ // compression for this hunk //------------------------------------------------- @@ -3910,7 +4343,7 @@

About and licenses

// if this is the best one, copy the data into the permanent buffer uint32_t compbytes = m_compressor[codecnum]->compress(src, m_hunkbytes, &m_compress_test[0]); #if CHDCODEC_VERIFY_COMPRESSION -@@ -772,7 +804,7 @@ +@@ -772,7 +812,7 @@ } printf(" codec%d=%d bytes \n", codecnum, compbytes); #endif @@ -3919,7 +4352,7 @@

About and licenses

{ compression = codecnum; complen = compbytes; -@@ -787,6 +819,7 @@ +@@ -787,6 +827,7 @@ // if the best is none, copy it over if (compression == -1) memcpy(compressed, src, m_hunkbytes); @@ -3927,7 +4360,7 @@

About and licenses

return compression; } -@@ -852,12 +885,12 @@ +@@ -852,12 +893,12 @@ { // set the low bit of the size so we don't match next time *ptr |= 1; @@ -3942,7 +4375,7 @@

About and licenses

for (int scan = 0; scan < MAX_ZLIB_ALLOCS; scan++) if (codec->m_allocptr[scan] == nullptr) { -@@ -867,7 +900,7 @@ +@@ -867,7 +908,7 @@ // set the low bit of the size so we don't match next time *ptr = size | 1; @@ -3951,7 +4384,7 @@

About and licenses

} -@@ -881,7 +914,7 @@ +@@ -881,7 +922,7 @@ auto *codec = reinterpret_cast<chd_zlib_allocator *>(opaque); // find the hunk @@ -3960,7 +4393,7 @@

About and licenses

for (int scan = 0; scan < MAX_ZLIB_ALLOCS; scan++) if (ptr == codec->m_allocptr[scan]) { -@@ -915,6 +948,13 @@ +@@ -915,6 +956,13 @@ throw std::bad_alloc(); else if (zerr != Z_OK) throw std::error_condition(chd_file::error::CODEC_ERROR); @@ -3974,7 +4407,7 @@

About and licenses

} -@@ -925,6 +965,8 @@ +@@ -925,6 +973,8 @@ chd_zlib_compressor::~chd_zlib_compressor() { deflateEnd(&m_deflater); @@ -3983,7 +4416,7 @@

About and licenses

} -@@ -934,12 +976,24 @@ +@@ -934,12 +984,24 @@ uint32_t chd_zlib_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { @@ -4009,7 +4442,7 @@

About and licenses

m_deflater.total_out = 0; int zerr = deflateReset(&m_deflater); if (zerr != Z_OK) -@@ -949,7 +1003,7 @@ +@@ -949,7 +1011,7 @@ zerr = deflate(&m_deflater, Z_FINISH); // if we ended up with more data than we started with, return an error @@ -4018,7 +4451,7 @@

About and licenses

throw std::error_condition(chd_file::error::COMPRESSION_ERROR); // otherwise, return the length -@@ -1066,7 +1120,7 @@ +@@ -1066,7 +1128,7 @@ // do it ZSTD_inBuffer input{ src, srclen, 0 }; @@ -4027,7 +4460,7 @@

About and licenses

while (output.pos < output.size) { result = ZSTD_compressStream2(m_stream, &output, &input, ZSTD_e_end); -@@ -1197,12 +1251,12 @@ +@@ -1197,12 +1259,12 @@ { // set the low bit of the size so we don't match next time *ptr |= 1; @@ -4042,7 +4475,7 @@

About and licenses

for (int scan = 0; scan < MAX_LZMA_ALLOCS; scan++) if (codec->m_allocptr[scan] == nullptr) { -@@ -1212,7 +1266,7 @@ +@@ -1212,7 +1274,7 @@ // set the low bit of the size so we don't match next time *ptr = size | 1; @@ -4051,7 +4484,7 @@

About and licenses

} -@@ -1229,7 +1283,7 @@ +@@ -1229,7 +1291,7 @@ auto *const codec = static_cast<chd_lzma_allocator *>(const_cast<ISzAlloc *>(p)); // find the hunk @@ -4060,7 +4493,7 @@

About and licenses

for (int scan = 0; scan < MAX_LZMA_ALLOCS; scan++) if (ptr == codec->m_allocptr[scan]) { -@@ -1284,8 +1338,8 @@ +@@ -1284,8 +1346,8 @@ if (res != SZ_OK) throw std::error_condition(chd_file::error::COMPRESSION_ERROR); @@ -4071,7 +4504,7 @@

About and licenses

res = LzmaEnc_MemEncode(encoder, dest, &complen, src, srclen, 0, nullptr, &m_allocator, &m_allocator); if (res != SZ_OK) throw std::error_condition(chd_file::error::COMPRESSION_ERROR); -@@ -1419,7 +1473,7 @@ +@@ -1419,7 +1481,7 @@ uint32_t chd_huffman_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { uint32_t complen; @@ -4080,7 +4513,7 @@

About and licenses

throw std::error_condition(chd_file::error::COMPRESSION_ERROR); return complen; } -@@ -1474,6 +1528,7 @@ +@@ -1474,6 +1536,7 @@ m_encoder.set_num_channels(2); m_encoder.set_block_size(blocksize(hunkbytes)); m_encoder.set_strip_metadata(true); @@ -4088,7 +4521,7 @@

About and licenses

} -@@ -1483,32 +1538,47 @@ +@@ -1483,32 +1546,47 @@ uint32_t chd_flac_compressor::compress(const uint8_t *src, uint32_t srclen, uint8_t *dest) { @@ -4149,7 +4582,7 @@

About and licenses

} return complen + 1; } -@@ -1642,14 +1712,16 @@ +@@ -1642,14 +1720,16 @@ memcpy(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA + framenum * cdrom_file::MAX_SUBCODE_DATA], &src[framenum * cdrom_file::FRAME_SIZE + cdrom_file::MAX_SECTOR_DATA], cdrom_file::MAX_SUBCODE_DATA); } @@ -4169,6 +4602,18 @@

About and licenses

// deflate the subcode data m_deflater.next_in = const_cast<Bytef *>(&m_buffer[frames * cdrom_file::MAX_SECTOR_DATA]); +@@ -1950,6 +2030,11 @@ + + void chd_avhuff_decompressor::decompress(const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) + { ++ // Discpress: a frame larger than the hunk (damaged or crafted data) is an error: 0.289 decoded it ++ // past the end of the hunk (its size, as raw_data_size has it: header, metadata, audio, video) ++ if ((complen >= 8) && (12 + uint64_t(src[0]) + 2 * uint64_t(src[1]) * get_u16be(&src[2]) + 2 * uint64_t(get_u16be(&src[4])) * get_u16be(&src[6]) > destlen)) ++ throw std::error_condition(chd_file::error::DECOMPRESSION_ERROR); ++ + // decode the audio and video + avhuff_error averr = m_decoder.decode_data(src, complen, dest); + if (averr != AVHERR_NONE) --- a/src/lib/util/chdcodec.h +++ b/src/lib/util/chdcodec.h @@ -71,6 +71,13 @@ @@ -4547,6 +4992,42 @@

About and licenses

m_accum.m_raw = crc; } +--- a/src/lib/util/huffman.cpp ++++ b/src/lib/util/huffman.cpp +@@ -631,6 +631,10 @@ + uint32_t nextstart = (curstart + bithisto[codelen]) >> 1; + if (codelen != 1 && nextstart * 2 != (curstart + bithisto[codelen])) + return HUFFERR_INTERNAL_INCONSISTENCY; ++ // Discpress: more codes than one bit has room for (a damaged or crafted tree): 0.289 gave codes ++ // more bits than their length, and build_lookup_table wrote past the end of its table ++ if (codelen == 1 && (curstart + bithisto[codelen]) > 2) ++ return HUFFERR_INTERNAL_INCONSISTENCY; + bithisto[codelen] = curstart; + curstart = nextstart; + } +@@ -654,6 +658,7 @@ + void huffman_context_base::build_lookup_table() + { + // iterate over all codes ++ uint32_t covered = 0; + for (int curcode = 0; curcode < m_numcodes; curcode++) + { + // process all nodes which have non-zero bits +@@ -669,8 +674,14 @@ + lookup_value *destend = &m_lookup[((node.m_bits + 1) << shift) - 1]; + while (dest <= destend) + *dest++ = value; ++ covered = std::max<uint32_t>(covered, (node.m_bits + 1) << shift); + } + } ++ ++ // Discpress: the entries no code covers decode as code 0 of no bits. Canonical codes cover the table ++ // from 0 up, all of it for a complete tree (what an encoder writes); 0.289 left the rest as it was, ++ // whatever was in that memory, and a damaged tree's data read bit counts from it ++ std::fill(m_lookup + covered, m_lookup + (uint32_t(1) << m_maxbits), lookup_value(0)); + } + + --- a/src/lib/util/unicode.cpp +++ b/src/lib/util/unicode.cpp @@ -16,71 +16,12 @@ @@ -8566,6 +9047,8 @@

About and licenses

await loadModule(msg); var backing = await makeBacking(msg); postMessage({ type: 'storage', mode: backing.mode, reason: memReason }); + // testing: silent this long, as a stuck job is (the page's no-progress watchdog, DEBUG.freeze) + if (msg.debugFreeze) await new Promise(function (r) { setTimeout(r, msg.debugFreeze); }); // copies of inputs chdman must modify in place (verify --fix, addmeta, delmeta) var outFiles = []; @@ -8833,8 +9316,8 @@

About and licenses

if (p) p.catch(function (err) { postMessage({ type: 'fatal', message: err && typeof err.errno === 'number' ? errText(err) : String(err && err.stack || err && err.message || err) }); }); }; - - + + - + diff --git a/tests/ui/edge-cases.spec.js b/tests/ui/edge-cases.spec.js index 60164ae..b47e4be 100644 --- a/tests/ui/edge-cases.spec.js +++ b/tests/ui/edge-cases.spec.js @@ -772,3 +772,33 @@ test('the compression picked stays when the type of CHD changes', async ({ app } await expect(card.locator('code.cmd')).toContainText('chdman createcd'); await expect(card.locator('code.cmd')).toContainText('-c cdzl,cdfl'); }); + +// The no-progress watchdog: a running job the page hears nothing from (here a job worker kept silent, DEBUG.freeze) +// gets a note saying so, which goes once progress comes again +test('a running job that makes no progress for a while says so, until it goes on', async ({ app }) => { + await app.open({ debug: { freeze: 6000, stallMs: 1500 } }); + await app.add(fixture('ps1-single').add); + const card = app.jobs().first(); + await app.settled(card); + await card.locator('.job-foot button.primary').click(); + const note = card.locator('[data-note="no-progress"]'); + await expect(note).toBeVisible({ timeout: 5000 }); + await expect(note).toContainText('No progress for over a minute'); + await expect(note).toContainText('cancel it and try again'); + await app.waitState(card, 'done', 60_000); + await expect(note).toHaveCount(0); +}); + +// time the page is hidden doesn't count: a hidden page's workers may be paused (iOS pauses them all) +test('the no-progress watchdog doesn\u2019t count time the page is hidden', async ({ app, page }) => { + await app.open({ debug: { freeze: 5000, stallMs: 1500 } }); + await app.add(fixture('ps1-single').add); + const card = app.jobs().first(); + await app.settled(card); + await page.evaluate(() => Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => 'hidden' })); + await card.locator('.job-foot button.primary').click(); + await app.waitState(card, 'running'); + await page.waitForTimeout(3500); + await expect(card.locator('[data-note="no-progress"]')).toHaveCount(0); + await app.waitState(card, 'done', 60_000); +}); diff --git a/tests/ui/mobile.spec.js b/tests/ui/mobile.spec.js index f1b5979..f962d17 100644 --- a/tests/ui/mobile.spec.js +++ b/tests/ui/mobile.spec.js @@ -244,3 +244,18 @@ test('in Safari itself, there is no such notice', async ({ app, page }, testInfo await app.open(); await expect(page.locator('#iosTip')).toBeHidden(); }); + +// Phones and tablets take minutes over a disc: the hint under Compression suggests the preset that saves about a third +// of it, without choosing it (the default stays chdman's) +test('on a phone, the compression hint suggests the faster preset, and the default stays', async ({ app }) => { + await app.open(); + await app.add(fixture('ps1-multitrack').add); + const card = app.job('mgs disc1'); + await app.settled(card); + const field = card.locator('label.field', { hasText: 'Compression' }); + await expect(field.locator('select')).toHaveValue('default'); + await expect(field.locator('.hint')).toContainText('On a phone or tablet, Nearly as small, faster takes about a third less time'); + await card.locator('details.opts summary').click(); + await field.locator('select').selectOption({ label: 'Nearly as small, faster' }); + await expect(field.locator('.hint')).not.toContainText('On a phone or tablet'); +}); From eb5701ce09b4b36a9ce8aca91701cf00db431262 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 02:35:40 +0000 Subject: [PATCH 04/13] CI: the suite in Firefox and WebKit, and benchmarks in each engine browsers.yml runs the suite with EXTRA_BROWSERS against the native references; bench.yml times the page in Chromium and Firefox (Ubuntu) and WebKit (an Apple-silicon Mac). bench.js takes --browser. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QUSh6AFtA1CAR66DNvjtty --- .github/workflows/bench.yml | 89 ++++++++++++++++++++++++++++++++++ .github/workflows/browsers.yml | 89 ++++++++++++++++++++++++++++++++++ tests/bench/bench.js | 21 +++++--- 3 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/bench.yml create mode 100644 .github/workflows/browsers.yml diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml new file mode 100644 index 0000000..c48afec --- /dev/null +++ b/.github/workflows/bench.yml @@ -0,0 +1,89 @@ +# Times conversions through the page (tests/bench/bench.js) in each browser engine: Chromium and Firefox on +# Ubuntu, against native chdman on the same machine (whose outputs each conversion must equal), and WebKit, +# Safari's engine, on an Apple-silicon Mac (the closest to an iPhone's CPU; timed only). The results are the +# run's summary and an artifact. When the page changes, weekly, and on demand. +name: Benchmarks + +on: + push: + branches: [main] + paths: ['dist/**', 'tests/bench/**', '.github/workflows/bench.yml'] + pull_request: + paths: ['dist/**', 'tests/bench/**', '.github/workflows/bench.yml'] + schedule: + - cron: '11 7 * * 4' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: bench-${{ github.ref }} + cancel-in-progress: true + +jobs: + bench: + strategy: + fail-fast: false + matrix: + include: + - browser: chromium + os: ubuntu-24.04 + native: true + - browser: firefox + os: ubuntu-24.04 + native: true + - browser: webkit + os: macos-14 + native: false + runs-on: ${{ matrix.os }} + timeout-minutes: 90 + name: bench (${{ matrix.browser }}) + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 22 + cache: npm + cache-dependency-path: tests/package-lock.json + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: '3.12' + - name: numpy, for the synthetic test discs + run: pip install numpy==2.4.6 + - name: Cache MAME 0.289 and its chdman + if: matrix.native + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: | + third_party/mame + build/chdman-0.289 + key: mame-0.289-${{ runner.os }}-${{ hashFiles('scripts/fetch-mame.sh', 'scripts/build-upstream.sh', 'wasm/**') }} + - name: Native chdman (0.289 and the engine) + if: matrix.native + run: | + [ -x build/chdman-0.289 ] || ./scripts/build-upstream.sh + ./scripts/build-native.sh + - name: Playwright and ${{ matrix.browser }} + working-directory: tests + run: | + npm ci --no-audit --no-fund + npx playwright install --with-deps ${{ matrix.browser }} + # (bench.js makes the synthetic discs, the benchmark images among them) + - name: Benchmark + working-directory: tests + run: > + node bench/bench.js --browser ${{ matrix.browser }} --label ci-${{ matrix.browser }} + --threads 1,4 --presets default,fast --cd-mb 150 --dvd-mb 512 + ${{ !matrix.native && '--no-native' || '' }} + - name: Summary + if: always() + run: cat tests/.cache/bench/*.md >> "$GITHUB_STEP_SUMMARY" 2>/dev/null || true + - name: Results + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: bench-${{ matrix.browser }} + path: tests/.cache/bench + if-no-files-found: ignore + retention-days: 90 diff --git a/.github/workflows/browsers.yml b/.github/workflows/browsers.yml new file mode 100644 index 0000000..28c93ce --- /dev/null +++ b/.github/workflows/browsers.yml @@ -0,0 +1,89 @@ +# The UI suite (tests/README.md) on dist/discpress.html in Firefox and in WebKit, Safari's engine (Playwright's +# projects behind EXTRA_BROWSERS: webkit, iphone-webkit and firefox), against unmodified chdman 0.289 and the +# engine built natively, as ci.yml runs it in Chromium. A test that fails in one of them for a reason the page +# can't fix is pinned there with test.fail() or skipped with its reason. When the page, the tests or this +# workflow change, weekly, and on demand. +name: Browsers + +on: + push: + branches: [main] + paths: ['app/**', 'dist/**', 'tests/**', 'web/**', '.github/workflows/browsers.yml'] + pull_request: + paths: ['app/**', 'dist/**', 'tests/**', 'web/**', '.github/workflows/browsers.yml'] + schedule: + - cron: '37 6 * * 2' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: browsers-${{ github.ref }} + cancel-in-progress: true + +jobs: + tests: + runs-on: ubuntu-24.04 + timeout-minutes: 75 + strategy: + fail-fast: false + matrix: + include: + - browser: firefox + projects: --project=firefox + - browser: webkit + projects: --project=webkit --project=iphone-webkit + name: tests (${{ matrix.browser }}) + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 22 + cache: npm + cache-dependency-path: tests/package-lock.json + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: '3.12' + - name: numpy, for the synthetic test discs + run: pip install numpy==2.4.6 + # the references the tests compare with, as in ci.yml (the same cache) + - name: Cache MAME 0.289 and its chdman + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: | + third_party/mame + build/chdman-0.289 + key: mame-0.289-${{ runner.os }}-${{ hashFiles('scripts/fetch-mame.sh', 'scripts/build-upstream.sh', 'wasm/**') }} + - name: Unmodified chdman 0.289 + run: '[ -x build/chdman-0.289 ] || ./scripts/build-upstream.sh' + - name: The engine built natively + run: ./scripts/build-native.sh + - name: Cache Playwright's ${{ matrix.browser }} + id: pwcache + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + with: + path: ~/.cache/ms-playwright + key: playwright-${{ matrix.browser }}-${{ runner.os }}-${{ hashFiles('tests/package-lock.json') }} + - name: Playwright and ${{ matrix.browser }} + working-directory: tests + run: | + npm ci --no-audit --no-fund + if [ '${{ steps.pwcache.outputs.cache-hit }}' = 'true' ]; then npx playwright install-deps ${{ matrix.browser }}; else npx playwright install --with-deps ${{ matrix.browser }}; fi + - name: Synthetic test discs + run: python3 tests/fixtures/make_fixtures.py + - name: UI tests in ${{ matrix.browser }} + working-directory: tests + env: + EXTRA_BROWSERS: '1' + REQUIRE_NATIVE: '1' + run: npx playwright test ${{ matrix.projects }} + - name: Report of a failed run + if: failure() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: playwright-report-${{ matrix.browser }} + path: | + tests/.cache/report + tests/.cache/test-results + retention-days: 14 diff --git a/tests/bench/bench.js b/tests/bench/bench.js index 2d5cf3b..a47894e 100644 --- a/tests/bench/bench.js +++ b/tests/bench/bench.js @@ -1,6 +1,7 @@ #!/usr/bin/env node -// Conversion benchmarks: times real conversions through the Discpress UI in headless -// Chromium and compares them with native chdman on the same machine. +// Conversion benchmarks: times real conversions through the Discpress UI in a headless browser +// (Chromium, or Firefox or WebKit once Playwright has installed them) and compares them with native +// chdman on the same machine. // // node bench/bench.js [options] // --fixtures bench-cd,bench-dvd fixture keys (see fixtures/make_fixtures.py; --quick uses small ones; @@ -16,10 +17,11 @@ // --no-native-timing still verify against native chdman, but don't time it // --no-verify skip checking outputs against native chdman // --html PATH page to test (default dist/discpress.html) +// --browser chromium chromium, firefox or webkit (Safari's engine) // --label NAME name for this run (saved as .cache/bench/