Skip to content

Harden exnref literals #3092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class Literal {
};

public:
Type type;
// Type of the literal. Immutable because the literal's payload depends on it.
const Type type;

public:
Literal() : v128(), type(Type::none) {}
explicit Literal(Type type) : v128(), type(type) {
assert(type != Type::unreachable);
assert(type != Type::unreachable && type != Type::funcref &&
type != Type::exnref);
}
explicit Literal(Type::BasicID typeId) : Literal(Type(typeId)) {}
explicit Literal(int32_t init) : i32(init), type(Type::i32) {}
explicit Literal(uint32_t init) : i32(init), type(Type::i32) {}
explicit Literal(int64_t init) : i64(init), type(Type::i64) {}
Expand All @@ -67,7 +69,7 @@ class Literal {
explicit Literal(const std::array<Literal, 4>&);
explicit Literal(const std::array<Literal, 2>&);
explicit Literal(Name func) : func(func), type(Type::funcref) {}
explicit Literal(std::unique_ptr<ExceptionPackage> exn)
explicit Literal(std::unique_ptr<ExceptionPackage>&& exn)
: exn(std::move(exn)), type(Type::exnref) {}
Literal(const Literal& other);
Literal& operator=(const Literal& other);
Expand Down Expand Up @@ -105,7 +107,7 @@ class Literal {

static Literal makeNullref() { return Literal(Type(Type::nullref)); }
static Literal makeFuncref(Name func) { return Literal(func.c_str()); }
static Literal makeExnref(std::unique_ptr<ExceptionPackage> exn) {
static Literal makeExnref(std::unique_ptr<ExceptionPackage>&& exn) {
return Literal(std::move(exn));
}

Expand Down
4 changes: 2 additions & 2 deletions src/passes/DeadArgumentElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ struct DAE : public Pass {
value = c->value;
} else if (value != c->value) {
// Not identical, give up
value.type = Type::none;
value = Literal(Type::none);
break;
}
} else {
// Not a constant, give up
value.type = Type::none;
value = Literal(Type::none);
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/passes/RemoveImports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ struct RemoveImports : public WalkerPass<PostWalker<RemoveImports>> {
if (type == Type::none) {
replaceCurrent(getModule()->allocator.alloc<Nop>());
} else {
Literal nopLiteral;
nopLiteral.type = type;
Literal nopLiteral(type);
replaceCurrent(getModule()->allocator.alloc<Const>()->set(nopLiteral));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
for (auto item : arguments) {
exn->values.push_back(item);
}
throwException(Literal(std::move(exn)));
throwException(Literal::makeExnref(std::move(exn)));
WASM_UNREACHABLE("throw");
}
Flow visitRethrow(Rethrow* curr) {
Expand Down
16 changes: 8 additions & 8 deletions src/wasm/literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,29 @@ ExceptionPackage Literal::getExceptionPackage() const {

Literal Literal::castToF32() {
assert(type == Type::i32);
Literal ret(i32);
ret.type = Type::f32;
Literal ret(Type::f32);
ret.i32 = i32;
return ret;
}

Literal Literal::castToF64() {
assert(type == Type::i64);
Literal ret(i64);
ret.type = Type::f64;
Literal ret(Type::f64);
ret.i64 = i64;
return ret;
}

Literal Literal::castToI32() {
assert(type == Type::f32);
Literal ret(i32);
ret.type = Type::i32;
Literal ret(Type::i32);
ret.i32 = i32;
return ret;
}

Literal Literal::castToI64() {
assert(type == Type::f64);
Literal ret(i64);
ret.type = Type::i64;
Literal ret(Type::i64);
ret.i64 = i64;
return ret;
}

Expand Down