Skip to content

Commit 652fe23

Browse files
Translator: Fix a miscompilation for unreachable br_table
1 parent e0e3823 commit 652fe23

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Sources/WasmKit/Translator.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,13 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
13131313

13141314
let defaultFrame = try controlStack.branchTarget(relativeDepth: targets.defaultIndex)
13151315

1316-
preserveOnStack(depth: Int(defaultFrame.copyCount))
1316+
// If this instruction is unreachable, copyCount might be greater than the actual stack height
1317+
try preserveOnStack(
1318+
depth: min(
1319+
Int(defaultFrame.copyCount),
1320+
valueStack.height - controlStack.currentFrame().stackHeight
1321+
)
1322+
)
13171323
let allLabelIndices = targets.labelIndices + [targets.defaultIndex]
13181324
let tableBuffer = allocator.allocateBrTable(capacity: allLabelIndices.count)
13191325
let operand = Instruction.BrTableOperand(

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.wast",
2625
"br_if.wast",
2726
"call.wast",
2827
"call_indirect.wast",

0 commit comments

Comments
 (0)