Skip to content

test: replace wall-clock cost assertions with deterministic pins - #577

Merged
Tim81 merged 9 commits into
mainfrom
test/400-deterministic-cost-pins
Nov 18, 2026
Merged

Tim81 merged 9 commits into
mainfrom
test/400-deterministic-cost-pins

Conversation

@Tim81

@Tim81 Tim81 commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Closes #400. Related: #456.

A Stopwatch checked after a call returns cannot end a hang, and a 10 s bound fails on a loaded runner: the main CI run for 1c61fa6 failed FindingHigh_A4FlateBombPadding_refusesQuickly_viaCostBudget at 11.79 s, and #400 records two 10 s Timeout cancellations of the #208 encrypt-dictionary pin. Most sites now pin something the code decides, or a bound on bytes allocated on the calling thread. All figures below are from local runs on 2026-11-18 unless stated.

The only src change is internal bool IsIndexed => _index is not null; in PdfDictionary. No PublicAPI change.

Classification of every hit of the wide grep

git grep -n -E "Stopwatch|Elapsed|TickCount|GetTimestamp|DateTime\.(Utc)?Now|Timeout *=|WaitAsync\(TimeSpan|CancelAfter|time:" -- tests (52 hits at 1c61fa6)

Site Class Before -> after Proof
Kernel MalformedInputTests cmap cost pin 10 s Timeout -> message Contains, 300 s guard no-budget mutation fails ("No exception was thrown"). Limit: a check moved to after each segment still passes
Kernel MalformedInputTests PNG bomb, interlaced PNG bomb cost pin 10 s Timeout -> message plus per-thread allocation < 256 KiB, 300 s guard cap checked after the loop: 10.5 MB, fails. Cap 245,000 bytes late: 337,032, fails. Cap 160,000 bytes late: passes (limit)
Kernel PdfDictionaryIndexTests x2 cost pin 10 s Timeout -> IsIndexed 100 keys in and after the build, entry-swap check, 300 s guard IndexThreshold=int.MaxValue fails by name in under 1 s; copy._index = null fails; TryGet ignoring the index fails ("Expected 1, Actual 0") on both tests in 1 s wall
Kernel SignatureTests 700, 704 hang guard WaitAsync(30 s) -> 300 s tests pass
Kernel EncryptionTests 509, PdfDictionaryIndexTests 32 prose unchanged or reworded not assertions
Kernel TestTimestampClient, TimestampTests (GetTimestampToken*) method name unchanged pattern false positive
Layout PaginationDepthTests x3 hang guard 10 s -> 300 s; renderers count Layout calls and Assert.Fail past 200,004 cap check disabled: all 3 fail by name in 1.4 s at a 155 MiB polled peak working set (Debug, 2026-11-18); clean run 102 MiB
Reader ContentInterpreterTests helper fuzz clock Stopwatch -> per-sample allocation ceiling, 300 s guard Run allocating 40 MiB fails both Facts
Reader EncryptDictionaryDenialOfServiceTests named #456 exception Timeout = 120_000 kept, comment names #456 and #400 #456's own measurement (cancelled at 120.2 s with IndexThreshold=int.MaxValue), not re-run
Reader EncryptedReconstructionTests A4 cost pin Stopwatch -> A4 message plus per-thread allocation < 5 MiB see below
Reader PageTreeTests shared /Kids array cost pin Stopwatch -> DoesNotContain node-limit and depth-limit codes cycle guard disabled: diagnostics are DepthExceeded and NodeLimitExceeded, no Cycle. Cycle reported but walk continues: DoesNotContain fails
Reader PageTreeTests deep malformed chain cost pin clock deleted, nothing replaces it none possible without a src hook; a quiet per-leaf rescan is not caught
Reader ParserFuzzTests (lexer, parser, reader x2) fuzz clock lexer clock deleted (token-count pin covers it); parser and reader get allocation ceilings; 300 s guards lexer not advancing fails ("cursor is not advancing"); Open +40 MiB fails both reader rows; parser constructor +40 MiB fails
Reader PdfObjectParserTests many streams cost pin 10 s Timeout -> body assertion with a trailing well-formed stream tier 1 also requiring an EOL fails; no break fails; scan on but keep first passes (limit)
Reader SaveDecryptedTests, csproj line 26 name / prose unchanged pattern false positive
TestSupport ExternalTool drain deadline, RemainingMs, IsTimeout, comment not assertions unchanged process-drain deadline and doc text

After this change the only [Fact(Timeout)] that is a regression pin and not a 300 s hang guard is the #456 test. The HttpTimestampClient 50 ms client timeouts do not match the grep and are untouched.

Allocation pins (bytes on the calling thread)

Pin Bound Clean (Debug / Release x2 / Release TC=0) Defect Margin
PNG zlib bomb, plain and interlaced 262,144 91,224 / 91,224, 91,224 / 90,928 cap after loop: 10.5 M; 245,000 bytes late: 337,032 2.9x above clean
A4 flate bomb 5,242,880 2,153,352 / 2,153,352, 2,153,352 / 2,153,056 (earlier runs: up to 2,153,888) charge after loop: 42.1 M. Refuse after 2 decodes: 4,256,528 (passes). After 3: 6,364,344 (fails). After 4: 8,466,328 (fails). Debug 2.4x above clean; the 3-decode mutation is 1.21x above the bound

Fuzz ceilings

Each is 1 MiB + K x input length (the 1 MiB is ReaderLimits.MinMaxDecodedBytes), K = 128 (parser), 2,048 (reader), 1,024 (content interpreter). K comes from hostile shapes measured at 256 KiB / 1 MiB (17 shapes for parser and reader, 16 for content), at 2.4x (parser), 4.9x (reader) and 5.7x (content interpreter) the worst. Over 1,000,000 samples (Release, 2026-11-18) the reader's worst sample allocated 1,743,608 bytes on a 1,433-byte input, 0.44 of that sample's ceiling, the tightest of the three; the parser's worst allocated 143,184 bytes (0.077 of its ceiling) and the content interpreter's 25,528 (0.012). ParserFuzzTests at VELLUMPDF_FUZZ_ITER=1000000 passed 5/5, taking 28.8 s to 89 s across local runs on 2026-11-18; the two content Facts passed in 23 s. At real sample lengths (up to about 6.5 KB) the 1 MiB slack dominates every ceiling, so all three catch gross regressions only.

Stated limits

  • Built-but-unused index: caught by the entry-swap check, not by a clock.
  • cmap: a budget check moved after each segment still passes.
  • PNG: a cap up to about 160,000 bytes late still passes.
  • Many-streams parser test: a scan that visits every later candidate but keeps the first match passes.
  • Page-tree deep malformed chain: no pin.
  • Fuzz allocation ceilings do not discriminate CPU-only defects.

Discovery, not part of this change

PdfReader.Open with AllowReconstruction = true allocates hundreds of megabytes on a run of [: 26 MB at 512 bytes, 64 MB at 1 KiB, 293 MB at 4 KiB, 304 MB from 8 to 64 KiB and 609 MB at 256 KiB (MaxDecodedStreamBytes 1 MiB). A 192-byte run inside a 790-byte file already allocates 4.4 MB. With reconstruction off, about 1 KB. No tracking issue is filed yet. The reader fuzz ceiling cannot admit the long runs; the ceiling comment says so.

Verification

Release, one project at a time: Kernel.Tests 1525 passed, 0 skipped; Reader.Tests 1869 passed, 12 skipped; Layout.Tests 412 passed, 31 skipped, also under DOTNET_TieredCompilation=0. dotnet format --verify-no-changes and eng/clean-room-check.ps1 pass.

The 10 s Timeout attributes on the cmap, PNG and PdfDictionary pins could not
tell the defect from a slow runner: a Timeout is a hang guard, and the broken
work in the cmap and PNG cases is milliseconds. They also failed on loaded CI
for the wrong reason (#400).

PdfDictionary gains an internal IsIndexed so the #208 tests assert that the
index was built and that ShallowCopy carries it, 100 keys in, instead of timing
a quadratic 140,000-key build. The PNG zlib-bomb tests bound the bytes
allocated on the calling thread (91,224 measured; 10.5 MB when the cap is
checked after the loop) and read the cap's message. The cmap test reads its
budget message. Remaining Timeouts and the signing WaitAsync calls are 300 s
hang guards.
…Timeout

The continuation-cap tests ended a runaway only through a 10 s Timeout, which
a loaded runner can hit on a render that is merely slow (#400). A longer
Timeout alone would let a cap-removed mutation reach OOM with no test name, so
the renderers now count Layout calls and fail past 4 x 50,001. With the cap
check disabled, the three tests fail by name in 1.4 s at a 155 MiB polled peak
(Debug). The Timeout becomes a 300 s hang guard.
Stopwatch checks made after a call returns cannot end a hang and fail on a
loaded runner (#400). The A4 flate-bomb test reads the budget's message and
bounds per-thread allocation (8 MiB; 2.15 MB measured, 42.1 MB when the charge
lands after the loop). The parser test closes with a well-formed stream so a
scan that skips EOL-less terminators returns a wrong body. PageTree drops the
clock for diagnostic-code checks. The fuzz targets check a per-sample
allocation ceiling that scales with input length and keeps CsCheck's seed; they
carry no Timeout because the nightly runs 200,000 iterations per case. The
#456 Timeout stays, with a comment saying why.
The bound's comment pointed at the PR for its numbers. It now states them:
91,224 bytes with the cap firing, 10.5 million with it checked after the loop.
An index that is built and carried by ShallowCopy but never consulted
passed every #208 test in under a second. The tests now swap two backing
entries behind the index's back and require TryGet to follow the index,
on the source and on the copy. IsIndexed's doc now says what builds the
index and that it does not show TryGet consults it.

The PNG bound drops from 1 MiB to 256 KiB: a cap that fires 245,000 bytes
late (337,032 allocated) now fails.
The fuzz Facts get 300 s hang guards: the nightly ran all five
ParserFuzz tests at 200,000 iterations in 15 s, so the earlier claim that
a fixed limit would cancel it was wrong. The A4 bound drops from 8 MiB to
5 MiB, so a reconstruction that refuses after three decodes (6.4 million
allocated) now fails. Comments gain dates and configurations, the '['
reconstruction allocation is recorded as an uncovered shape, and a stale
reference to a removed ten-second budget is gone.
The '[' comment now states a run of 192 built by eight operations and the
measured failure (4,429,360 bytes against a 2,666,496-byte ceiling), and
the class doc's ratios come from the 1,000,000-sample run. The CHANGELOG
no longer implies the lexer target has an allocation ceiling. The
EncryptedReconstructionTests clock is cited by its CI run, and the #456
Timeout comment says why nothing deterministic replaces it.
The #208 test said the /Encrypt dictionary was not reachable from the test;
it is, through reader.Trailer. The comment now says it is not inspected and
why. One ParserFuzzTests doc line is wrapped.
@Tim81
Tim81 merged commit 10306c2 into main Nov 18, 2026
4 checks passed
@Tim81
Tim81 deleted the test/400-deterministic-cost-pins branch November 21, 2026 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests: wall-clock assertions flake on the shared CI runner

1 participant