PureXML/Sources/XSLT/XSLTInstantiationSteps.swift
Mihaela Mihaljevic dc6727538d
perf(xslt): make the template evaluator iterative (#358)
The XSLT engine was mutually recursive on template-application and result-tree
nesting (instantiate, evaluate, applyTemplates), so a recursive named template
whose depth comes from source data could overflow the native stack (SIGBUS) on
attacker-controllable input, and an identity transform of a deep source
overflowed on a small (Swift-concurrency Task) stack.

The engine is now an explicit work-stack driver (XSLTDriver). Each instruction
appends its produced items to a sink or pushes sub-work, with the rest of the
body pushed first so result-tree document order is preserved; template
application, the built-in rule's source descent, and result-element nesting all
run on the heap. Only stylesheet-bounded helper evaluation (a variable's or
parameter's result-tree fragment, an attribute set, an xsl:message body)
re-enters through a nested driver call, whose native depth is bounded by the
stylesheet's own nesting.

The result-tree serialization walks the engine now exposes at depth were made
iterative too: XSLTNamespaceFixup (an explicit pre-order visit that keeps the
ns0/ns1 counter in document order), the text and string-value extractions
(iterative accumulators), and the content-type-meta and CDATA-section rewrites
(the shared Node.rebuildingBottomUp spine).

Deep recursion now succeeds on any stack: a 5000-level recursive template builds
and serializes on the test Task stack. maxTemplateDepth is retained as runaway
protection (an unbounded result tree would exhaust memory), defaulting to 40000
and throwing XSLTError.recursionLimitExceeded only past that.

Behavior is otherwise unchanged: the full Apache Xalan XSLT 1.0 conformance
corpus (1086 cases) and the in-repo XSLT/XPath suites pass identically; one
previously-failing Xalan case (mdocs07) now passes and was de-baselined. Full
suite green. Closes #356.
2026-06-27 19:29:51 +02:00

132 lines
5.9 KiB
Swift

// The step methods of the iterative XSLT evaluator (see XSLTInstantiation.swift).
// Each instruction either appends its produced items to the sink synchronously or
// pushes sub-work; the rest of the body is always pushed first so it pops last,
// keeping result-tree document order.
extension XSLTDriver {
func stepRun(_ body: [Instruction], _ cursor: Int, _ context: XSLTContext, _ sink: Sink) {
guard cursor < body.count else { return }
let instruction = body[cursor]
switch instruction {
case let .variable(name, select, variableBody):
// A variable binds for the rest of the body; thread the updated context.
var context = context
context.variables[name] = transformer.variableValue(select, variableBody, context)
stack.append(.run(body, cursor + 1, context, sink))
return
case let .fallback(fallbackBody):
stack.append(.run(body, cursor + 1, context, sink))
stack.append(.run(fallbackBody, 0, context, sink))
return
default:
break
}
// The remainder of the body runs after this instruction's items.
stack.append(.run(body, cursor + 1, context, sink))
if runSimple(instruction, context, sink) { return }
if runControl(instruction, context, sink) { return }
if runTemplate(instruction, context, sink) { return }
runConstruct(instruction, context, sink)
}
private func runSimple(_ instruction: Instruction, _ context: XSLTContext, _ sink: Sink) -> Bool {
switch instruction {
case let .literalText(text):
sink.into.items.append(.node(.text(text)))
case let .valueOf(select, raw):
let value = transformer.string(select, context)
sink.into.items.append(.node(.text(raw ? PureXML.XSLT.RawText.marked(value) : value)))
case .number:
sink.into.items.append(.node(.text(transformer.numberInstruction(instruction, context))))
case let .copyOf(select):
sink.into.items.append(contentsOf: transformer.copyOf(select, context))
case let .message(terminate, messageBody):
if terminate, transformer.termination.message == nil {
transformer.termination.message = Host.text(of: transformer.instantiate(messageBody, context))
}
default:
return false
}
return true
}
private func runControl(_ instruction: Instruction, _ context: XSLTContext, _ sink: Sink) -> Bool {
switch instruction {
case let .ifInstruction(test, thenBody):
if transformer.boolean(test, context) { stack.append(.run(thenBody, 0, context, sink)) }
case let .choose(whens, otherwise):
let branch = whens.first { transformer.boolean($0.test, context) }?.body ?? otherwise
stack.append(.run(branch, 0, context, sink))
case let .forEach(select, sorts, eachBody):
pushForEach(select, sorts, eachBody, context, sink)
default:
return false
}
return true
}
private func runTemplate(_ instruction: Instruction, _ context: XSLTContext, _ sink: Sink) -> Bool {
switch instruction {
case let .applyTemplates(select, mode, sorts, parameters):
let nodes = transformer.sorted(transformer.selectXPathNodes(select ?? "node()", context), sorts, context)
stack.append(.applyTemplates(nodes, Application(mode: mode, parameters: parameters, caller: context, sink: sink)))
case let .callTemplate(name, parameters):
pushCallTemplate(name, parameters, context, sink)
case .applyImports:
pushApplyImports(context, sink)
default:
return false
}
return true
}
private func runConstruct(_ instruction: Instruction, _ context: XSLTContext, _ sink: Sink) {
switch instruction {
case .literalElement:
pushLiteralElement(instruction, context, sink)
case .element:
pushElement(instruction, context, sink)
case let .attribute(nameTemplate, namespaceTemplate, namespaces, attributeBody):
let name = transformer.createdName(nameTemplate, namespaceTemplate, namespaces, context, isAttribute: true)
pushTextNode(.attribute(name), attributeBody, context, sink)
case let .copy(useAttributeSets, copyBody):
pushCopy(useAttributeSets, copyBody, context, sink)
case let .comment(commentBody):
pushTextNode(.comment, commentBody, context, sink)
case let .processingInstruction(nameTemplate, piBody):
pushTextNode(.processingInstruction(transformer.avt(nameTemplate, context)), piBody, context, sink)
default:
break
}
}
private func pushForEach(
_ select: String,
_ sorts: [PureXML.XSLT.Sort],
_ body: [Instruction],
_ context: XSLTContext,
_ sink: Sink,
) {
let nodes = transformer.sorted(transformer.selectXPathNodes(select, context), sorts, context)
// Reversed so the nodes' bodies pop in document order into the sink.
for (offset, xnode) in nodes.enumerated().reversed() {
guard let owner = Host.ownerNode(xnode) else { continue }
var itemContext = XSLTContext(
node: owner,
current: xnode.treeNode == nil ? xnode : nil,
position: offset + 1,
size: nodes.count,
variables: context.variables,
)
itemContext.baseURI = context.baseURI
stack.append(.run(body, 0, itemContext, sink))
}
}
private func pushTextNode(_ kind: Finish, _ body: [Instruction], _ context: XSLTContext, _ sink: Sink) {
let child = Accumulator()
stack.append(.finish(kind, child, sink.into))
stack.append(.run(body, 0, context, Sink(depth: sink.depth, into: child)))
}
}