Skip to content

Commit 66c7166

Browse files
authored
Stack switching validation (#8467)
Now that we want to fuzz stack switching against a real implementation in V8, add proper validation. Copy in the stack switching tests, commenting out parts we do not handle for other reasons, to test that the new validation logic is correct. Fix DAE2 and a couple of stack switching tests that were not actually valid to begin with.
1 parent c874f5d commit 66c7166

10 files changed

Lines changed: 2057 additions & 155 deletions

File tree

src/cfg/cfg-traversal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ struct CFGWalker : public PostWalker<SubType, VisitorType> {
453453
auto handlerBlocks = BranchUtils::getUniqueTargets(*currp);
454454
// Add branches to the targets.
455455
for (auto target : handlerBlocks) {
456-
self->branches[target].push_back(self->currBasicBlock);
456+
if (target) {
457+
self->branches[target].push_back(self->currBasicBlock);
458+
}
457459
}
458460
}
459461

src/passes/DeadArgumentElimination2.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "ir/type-updating.h"
5353
#include "pass.h"
5454
#include "support/index.h"
55+
#include "support/mixed_arena.h"
5556
#include "support/utilities.h"
5657
#include "wasm-builder.h"
5758
#include "wasm-traversal.h"
@@ -354,9 +355,25 @@ struct GraphBuilder : public WalkerPass<ExpressionStackWalker<GraphBuilder>> {
354355
}
355356
}
356357

357-
void visitResume(Resume* curr) { noteContinuation(curr->cont->type); }
358+
void visitResumeHandlers(const ArenaVector<Name>& labels) {
359+
for (Index i = 0; i < labels.size(); ++i) {
360+
if (labels[i]) {
361+
auto* target = findBreakTarget(labels[i]);
362+
assert(target->type.size() >= 1);
363+
auto newContType = target->type[target->type.size() - 1];
364+
assert(newContType.isContinuation());
365+
noteContinuation(newContType);
366+
}
367+
}
368+
}
369+
370+
void visitResume(Resume* curr) {
371+
noteContinuation(curr->cont->type);
372+
visitResumeHandlers(curr->handlerBlocks);
373+
}
358374
void visitResumeThrow(ResumeThrow* curr) {
359375
noteContinuation(curr->cont->type);
376+
visitResumeHandlers(curr->handlerBlocks);
360377
}
361378
void visitStackSwitch(StackSwitch* curr) {
362379
noteContinuation(curr->cont->type);

src/wasm/wasm-ir-builder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,9 @@ Result<> IRBuilder::makeRefTest(Type type) {
19741974
}
19751975

19761976
Result<> IRBuilder::makeRefCast(Type type, bool isDesc) {
1977+
if (!type.isCastable()) {
1978+
return Err{"ref.cast cannot cast to invalid type"};
1979+
}
19771980
std::optional<HeapType> descriptor;
19781981
if (isDesc) {
19791982
assert(type.isRef());
@@ -2027,6 +2030,9 @@ Result<> IRBuilder::makeBrOn(Index label,
20272030
curr.op = op;
20282031
curr.castType = out;
20292032
curr.desc = nullptr;
2033+
if (op != BrOnNull && op != BrOnNonNull && !out.isCastable()) {
2034+
return Err{"br_on cannot cast to invalid type"};
2035+
}
20302036
CHECK_ERR(visitBrOn(&curr));
20312037

20322038
// Validate type immediates before we forget them.

0 commit comments

Comments
 (0)