A flat chain of <xs:group> references is not bounded by the parser's maxDepth (the definitions are siblings, not nested), so the compositional UPA determinism check's lazy per-reference recursion overflowed the native 8 MB stack while compiling a legal schema. Group summaries form an acyclic graph, so they are now precomputed bottom-up in dependency order (GroupDependencyOrder): the walk's group lookup is a cache hit and recursion is bounded by a single group's own model nesting. The precomputed reachable set is identical to the lazy walk's, so the verdict is unchanged. Verified verdict-equivalent over the full W3C XSTS corpus, the libxml2 determinism differential (0 FP, same 3 FN), the full suite, and an adversarial critic. Residual, tracked: the particle compiler that inlines a group reference still overflows on a far longer chain; closing it needs a content-model nesting-depth limit with a located error.
64 lines
3.2 KiB
Swift
64 lines
3.2 KiB
Swift
#if !os(WASI)
|
|
import Foundation
|
|
import Testing
|
|
@testable import PureXML
|
|
|
|
/// A `<xs:group>` reference chain (`g0 = (e0, g1)`, `g1 = (e1, g2)`, ...) is FLAT in
|
|
/// the schema document: the group definitions are siblings, not nested, so the chain
|
|
/// is NOT bounded by the parser's `maxDepth`, and compiling it recurses once per
|
|
/// link. The Unique Particle Attribution determinism check summarised groups lazily
|
|
/// by recursing through each reference, so a long chain overflowed the stack while
|
|
/// compiling a legal (if hostile) schema. It now summarises every reachable group
|
|
/// once in dependency order, so the walk's group lookup is a cache hit and recursion
|
|
/// is bounded by a single group's own model nesting.
|
|
///
|
|
/// Runs on an 8 MB stack, the native main-thread budget a real caller has: swift
|
|
/// testing executes a test on a much smaller Task stack, where the separately
|
|
/// tracked particle-compiler group expansion (which still recurses on the chain)
|
|
/// would overflow first and mask the determinism fix. WASI cannot size a thread
|
|
/// stack, so the case is gated off there; its budget is a link-time setting.
|
|
@Suite("Schema group-chain depth safety")
|
|
struct SchemaGroupChainDepthSafetyTests {
|
|
@Test("a deep group-reference chain compiles without overflowing the stack")
|
|
func test_deepGroupChainCompiles() {
|
|
let depth = 1500
|
|
var groups = ""
|
|
for index in 0 ..< depth {
|
|
let next = index < depth - 1 ? "<xs:group ref=\"g\(index + 1)\"/>" : ""
|
|
groups += "<xs:group name=\"g\(index)\"><xs:sequence>"
|
|
+ "<xs:element name=\"e\(index)\" type=\"xs:string\"/>\(next)</xs:sequence></xs:group>"
|
|
}
|
|
let schema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\(groups)"
|
|
+ "<xs:element name=\"doc\"><xs:complexType><xs:sequence>"
|
|
+ "<xs:group ref=\"g0\"/></xs:sequence></xs:complexType></xs:element></xs:schema>"
|
|
|
|
// The semaphore makes the worker's write happen-before the read, so the box's
|
|
// unchecked-Sendable mutation is safe (the closure must be Sendable for
|
|
// Foundation's Thread on Linux).
|
|
let result = ResultBox()
|
|
runOnNativeStack { result.compiled = (try? PureXML.Schema.Document(schema)) != nil }
|
|
|
|
// The chain is deterministic, so it must compile (not be rejected) and, above
|
|
// all, must not crash: before the dependency-order precompute the determinism
|
|
// check overflowed the stack on this shape well before this depth.
|
|
#expect(result.compiled)
|
|
}
|
|
|
|
private final class ResultBox: @unchecked Sendable {
|
|
var compiled = false
|
|
}
|
|
|
|
/// Runs `work` on a thread with the native main-thread stack budget and waits.
|
|
private func runOnNativeStack(_ work: @escaping @Sendable () -> Void) {
|
|
let done = DispatchSemaphore(value: 0)
|
|
let thread = Thread {
|
|
work()
|
|
done.signal()
|
|
}
|
|
thread.stackSize = 8 << 20
|
|
thread.start()
|
|
done.wait()
|
|
}
|
|
}
|
|
#endif
|