Skip to content

Commit 82fc5ff

Browse files
Validation: Fix br_table instruction type checking
1 parent 56221fc commit 82fc5ff

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Sources/WasmKit/Translator.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,10 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
987987
case .local(let localIndex):
988988
// Re-push local variables to the stack
989989
_ = try valueStack.pushLocal(localIndex, locals: &locals)
990-
case .vreg, nil:
990+
case .vreg:
991991
_ = valueStack.push(type)
992+
case nil:
993+
_ = valueStack.push(.unknown)
992994
case .const(let index, let type):
993995
valueStack.pushConst(index, type: type)
994996
}
@@ -1408,6 +1410,13 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
14081410
// +---> [0x0b] ...
14091411
for (entryIndex, labelIndex) in allLabelIndices.enumerated() {
14101412
let frame = try controlStack.branchTarget(relativeDepth: labelIndex)
1413+
1414+
// Check copyTypes consistency
1415+
guard frame.copyTypes.count == defaultFrame.copyTypes.count else {
1416+
throw ValidationError("Expected the same copy types for all branches in `br_table` but got \(frame.copyTypes) and \(defaultFrame.copyTypes)")
1417+
}
1418+
try popPushValues(frame.copyTypes)
1419+
14111420
do {
14121421
let relativeOffset = iseqBuilder.insertingPC.offsetFromHead - brTableAt.offsetFromHead
14131422
tableBuffer[entryIndex] = Instruction.BrTableOperand.Entry(
@@ -1427,6 +1436,10 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
14271436
}
14281437
}
14291438
}
1439+
// Pop branch copy values for type checking
1440+
for type in defaultFrame.copyTypes.reversed() {
1441+
_ = try popOperand(type)
1442+
}
14301443
try markUnreachable()
14311444
}
14321445

Tests/WasmKitTests/SpectestTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ final class SpectestTests: XCTestCase {
2222
path: Self.testPaths,
2323
include: [],
2424
exclude: [
25-
"/br_table.wast",
2625
"table_get.wast",
2726
"table_grow.wast",
2827
"table_size.wast",

0 commit comments

Comments
 (0)