Skip to content
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 by Bors] - Fix unreachable panics in compile_access #1861

Closed
Closed
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
65 changes: 50 additions & 15 deletions boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,13 @@ impl<'b> ByteCompiler<'b> {
}

#[inline]
fn compile_access(node: &'_ Node) -> Access<'_> {
fn compile_access(node: &Node) -> Option<Access<'_>> {
match node {
Node::Identifier(name) => Access::Variable { name: name.sym() },
Node::GetConstField(node) => Access::ByName { node },
Node::GetField(node) => Access::ByValue { node },
Node::This => Access::This,
_ => unreachable!(),
Node::Identifier(name) => Some(Access::Variable { name: name.sym() }),
Node::GetConstField(node) => Some(Access::ByName { node }),
Node::GetField(node) => Some(Access::ByValue { node }),
Node::This => Some(Access::This),
_ => None,
}
}

Expand Down Expand Up @@ -571,23 +571,33 @@ impl<'b> ByteCompiler<'b> {
self.compile_expr(unary.target(), true)?;
self.emit(Opcode::Inc, &[]);

let access = Self::compile_access(unary.target());
let access = Self::compile_access(unary.target()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid increment operand")
})?;
self.access_set(access, None, true)?;
None
}
UnaryOp::DecrementPre => {
self.compile_expr(unary.target(), true)?;
self.emit(Opcode::Dec, &[]);

let access = Self::compile_access(unary.target());
let access = Self::compile_access(unary.target()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid decrement operand")
})?;
self.access_set(access, None, true)?;
None
}
UnaryOp::IncrementPost => {
self.compile_expr(unary.target(), true)?;
self.emit(Opcode::Dup, &[]);
self.emit(Opcode::Inc, &[]);
let access = Self::compile_access(unary.target());

let access = Self::compile_access(unary.target()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid increment operand")
})?;
self.access_set(access, None, false)?;

None
Expand All @@ -596,7 +606,11 @@ impl<'b> ByteCompiler<'b> {
self.compile_expr(unary.target(), true)?;
self.emit(Opcode::Dup, &[]);
self.emit(Opcode::Dec, &[]);
let access = Self::compile_access(unary.target());

let access = Self::compile_access(unary.target()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid decrement operand")
})?;
self.access_set(access, None, false)?;

None
Expand Down Expand Up @@ -744,23 +758,38 @@ impl<'b> ByteCompiler<'b> {
AssignOp::BoolAnd => {
let exit = self.jump_with_custom_opcode(Opcode::LogicalAnd);
self.compile_expr(binary.rhs(), true)?;
let access = Self::compile_access(binary.lhs());
let access =
Self::compile_access(binary.lhs()).ok_or_else(|| {
self.context.construct_syntax_error(
"Invalid left-hand side in assignment",
)
})?;
self.access_set(access, None, use_expr)?;
self.patch_jump(exit);
None
}
AssignOp::BoolOr => {
let exit = self.jump_with_custom_opcode(Opcode::LogicalOr);
self.compile_expr(binary.rhs(), true)?;
let access = Self::compile_access(binary.lhs());
let access =
Self::compile_access(binary.lhs()).ok_or_else(|| {
self.context.construct_syntax_error(
"Invalid left-hand side in assignment",
)
})?;
self.access_set(access, None, use_expr)?;
self.patch_jump(exit);
None
}
AssignOp::Coalesce => {
let exit = self.jump_with_custom_opcode(Opcode::Coalesce);
self.compile_expr(binary.rhs(), true)?;
let access = Self::compile_access(binary.lhs());
let access =
Self::compile_access(binary.lhs()).ok_or_else(|| {
self.context.construct_syntax_error(
"Invalid left-hand side in assignment",
)
})?;
self.access_set(access, None, use_expr)?;
self.patch_jump(exit);
None
Expand All @@ -770,7 +799,10 @@ impl<'b> ByteCompiler<'b> {
if let Some(opcode) = opcode {
self.compile_expr(binary.rhs(), true)?;
self.emit(opcode, &[]);
let access = Self::compile_access(binary.lhs());
let access = Self::compile_access(binary.lhs()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid left-hand side in assignment")
})?;
self.access_set(access, None, use_expr)?;
}
}
Expand Down Expand Up @@ -903,7 +935,10 @@ impl<'b> ByteCompiler<'b> {
if let Node::Object(_) = assign.lhs() {
self.emit_opcode(Opcode::PushUndefined);
} else {
let access = Self::compile_access(assign.lhs());
let access = Self::compile_access(assign.lhs()).ok_or_else(|| {
self.context
.construct_syntax_error("Invalid left-hand side in assignment")
})?;
self.access_set(access, Some(assign.rhs()), use_expr)?;
}
}
Expand Down
45 changes: 45 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,16 @@ fn unary_pre() {
assert_eq!(&exec(execs_before_dec), "true");
}

#[test]
fn invalid_unary_access() {
check_output(&[
TestAction::TestStartsWith("++[];", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("[]++;", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("--[];", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("[]--;", "Uncaught \"SyntaxError\": "),
]);
}

#[test]
fn typeof_string() {
let typeof_string = r#"
Expand Down Expand Up @@ -690,6 +700,7 @@ fn unary_delete() {
mod in_operator {
use super::*;
use crate::forward_val;

#[test]
fn propery_in_object() {
let p_in_o = r#"
Expand Down Expand Up @@ -1335,6 +1346,21 @@ fn assignment_to_non_assignable() {
}
}

#[test]
fn assignment_to_non_assignable_ctd() {
check_output(&[
TestAction::TestStartsWith("(()=>{})() -= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() *= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() /= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() %= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() &= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() ^= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() |= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() += 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() = 5", "Uncaught \"SyntaxError\": "),
]);
}

#[test]
fn multicharacter_assignment_to_non_assignable() {
// Relates to the behaviour described at
Expand All @@ -1351,6 +1377,15 @@ fn multicharacter_assignment_to_non_assignable() {
}
}

#[test]
fn multicharacter_assignment_to_non_assignable_ctd() {
check_output(&[
TestAction::TestStartsWith("(()=>{})() **= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() <<= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() >>= 5", "Uncaught \"SyntaxError\": "),
]);
}

#[test]
fn multicharacter_bitwise_assignment_to_non_assignable() {
let mut context = Context::default();
Expand All @@ -1366,6 +1401,16 @@ fn multicharacter_bitwise_assignment_to_non_assignable() {
}
}

#[test]
fn multicharacter_bitwise_assignment_to_non_assignable_ctd() {
check_output(&[
TestAction::TestStartsWith("(()=>{})() >>>= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() &&= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() ||= 5", "Uncaught \"SyntaxError\": "),
TestAction::TestStartsWith("(()=>{})() ??= 5", "Uncaught \"SyntaxError\": "),
]);
}

#[test]
fn assign_to_array_decl() {
check_output(&[
Expand Down