Repository navigation
fix(kernel): save a deep structure tree without recursing - #645
Merged
Merged
Conversation
The six walks over the structure tree recursed once per level, so a tagged document whose tree was deeper than the thread's stack allowed ended the process in Save, and #571's cycle guard lowered that limit. Each walk is now an explicit-stack pre-order walk that visits nodes in the order the recursive code did, with the same cycle and null-child refusals. The first-leaf page of a grouping element was found by walking its whole subtree, once per element, so a chain cost the square of its depth. One post-order pass local to Build now computes it for every element.
Corrects the previous commit in this branch and its message. That message said each walk became an explicit-stack pre-order walk; five did, and the first-leaf pass is a post-order pass. CHANGELOG: a negative MCID is not refused (CollectElemsOnPage collects only MCIDs of 0 or more, so that check cannot run); "as before" now names what 2.3.3 already refused with the same message, and says the cycle and null-child refusals are the ones #521 added. Time and memory are stated per walk, with the Linearize planner called out as slower on a very large tree. Comments no longer claim a cycle walk "never finishes" and "runs out of memory" in the same breath. The veraPDF oracle test used 4,000 levels, which the CI runner's JVM could not read (StackOverflowError); it now uses 1,000 and says why.
…638) An element added under two parents is walked once per path, so the time can grow exponentially with the depth before Save refuses the tree. Probed on the previous head at depth 18 with each level's element added twice: an InvalidOperationException after 629 ms with no leaf and after 321 ms with a leaf. The CHANGELOG entry, the AddChild remarks and the helper comment now say the linear cost holds where each element has one parent.
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #638
What changed
A tagged document whose structure tree is deeper than the calling thread's stack allows overflowed the stack inside
Save, and .NET cannot catch that. The six walks inPdfStructureTreerecursed once per level. #571's cycle guard added aHashSetparameter to three of them, which lowered the depth they survive on the default stack.CollectPages,CollectElems,CollectElemsOnPage,BuildParentMap,CollectStructTypesand the first-leaf search) are now iterative. Five share one explicit-stack pre-order walk that calls its visitor on entry in the order the recursive code entered nodes. The cycle path set is added to on entry and removed when the node is popped, so a cycle still raises the sameInvalidOperationException. The null check inCollectPagesruns before anything reads the node.FirstLeafPagescomputes it for every element in one post-order pass, local to oneBuildcall (nothing survives the call). It isinternal staticand returns a step count, so a test pins the linear cost without a clock.AddChilddocuments "Do not build a tree deeper than a reader can walk" and thatSavedoes not limit depth.PublicAPIfile edit.2.3.3 against this build
Save)qpdf --checkcleanInvalidOperationException, same messageNullReferenceExceptionArgumentNullException,ParamNamechildInvalidOperationException"Object 8 was reserved but never assigned a value."InvalidOperationExceptionThe base commit (23d50e2) already crashed on a 6,500 chain that 2.3.3 saved. That regression came from #571 and never shipped, so the CHANGELOG describes only the 2.3.3 behaviour.
Tests
StructureTreeDepthTests(Kernel),DeepStructureTreeOracleTests(Conformance). Expected hashes and messages were recorded by running the same tests on a detached checkout of the base commit before the change./Type /StructElemobjects and the leaf's/Plinks reach/StructTreeRootin 100,002 hops. The same chain throughUseObjectStreams,Linearize,Encrypt,PrepareForSigningandSaveAsync, each checked withqpdf --check(linearized file: "File is linearized").FirstLeafPageson a 10,000 chain takes at most twice the number of elements in steps and names the leaf's page for every group. A second test pins first-leaf order, a shared element and leaves nested below a group.ParamNamechildfor the null rows, a cycle of 100,000 elements on the small stack./Pgcount changes from 1 to 3.--flavour ua1, 1,000 nestedDivending inP:jobEndStatus="normal", no failed rule in clause 7.1 or 7.2.On the base commit the 100,000-chain test ends the process with a stack overflow.
Run in Release with
REQUIRE_ORACLES=1,REQUIRE_VERAPDF=1, qpdf 12.4.2, veraPDF 1.30.4, poppler:Release build 0 warnings,
dotnet format --verify-no-changes, clean-room check andgenerate-roadmap.py --checkpass.Mutations
Each mutant was applied to the committed change, rebuilt, and run on net10.0 against the named test filter, then restored.
CollectElems,CollectPages,CollectElemsOnPage,BuildParentMap, first-leaf search,CollectStructTypes), one at a time-methodso it is attributable)FirstLeafPages_ofAChainOf10000Elements_takesLinearSteps_...failswide,twopages,groupnopageandfirstleafnull-then-cycle,null-childandnull-deeprefusal rows fail, and so doesCycleGuardTests.StructElem_AddChild_nullChild_isStored_andRefusedWhereTheTreeIsBuilt:NullReferenceExceptioninstead ofArgumentNullExceptionBuildSave_readsTheTreeAfresh_...fails (/Pgcount 2, expected 3)Differential run (2.3.3 against this build, net10.0)
The runner got two rows,
K15.tree.deep6500andK15.tree.deep100000(child process, since 2.3.3 dies), and a category for "2.3.3 crashed on valid input, this build saves", scoped to the second row.deep6500is a violation (2.3.3 saved a fileqpdf --checkaccepts, base crashes). That is the regression above, and it is the only violation in the run.deep100000crashes on both sides.deep6500is identical to 2.3.3;deep100000is 2.3.3 "stack overflow" against this build "saved", in that category. The run has no violations, and the only two rows whose verdict differs from the base-commit run are these two (28,633 rows compared).Not done
StackOverflowErrorin its own recursive structure-tree walk, and the depth it reads depends on the JVM. On the development machine it read 5,000 levels and overflowed at 5,500; the CI runner's JVM overflowed at 4,000 (a first version of the test used 4,000 and failed there). CI's limit below 4,000 was not measured, so the oracle test uses 1,000, well under the lowest overflow seen anywhere, which is below the depth 2.3.3 saved. It checks ordinary-depth output, not the 100,000-level row. The 100,000-level coverage isqpdf --checkand the save-path tests.InvalidOperationException"Object 8 was reserved but never assigned a value." after 629 ms with no leaf, and the duplicate-MCIDInvalidOperationExceptionafter 321 ms with a leaf. Changing it changes where its refusal is raised.PdfArrayinPdfArray(a stream dictionary) andCountVisibleDescendantsrecurse too; neither is reached by a structure tree.