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

Truncate environment stack on non-caught native error #3331

Merged
merged 2 commits into from
Oct 1, 2023
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
14 changes: 2 additions & 12 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,8 @@ impl JsObject {
),
);

let environment = context.vm.environments.current();

if code.has_parameters_env_bindings() {
last_env -= 1;
context
Expand Down Expand Up @@ -1391,8 +1393,6 @@ impl JsObject {
let argument_count = args.len();
let parameters_count = code.params.as_ref().len();

let has_binding_identifier = code.has_binding_identifier();

context.vm.push_frame(
CallFrame::new(code, script_or_module, Some(self.clone()))
.with_argument_count(argument_count as u32)
Expand All @@ -1411,16 +1411,6 @@ impl JsObject {

std::mem::swap(&mut environments, &mut context.vm.environments);

let environment = if has_binding_identifier {
environments.truncate(environments_len + 2);
let environment = environments.pop();
environments.pop();
environment
} else {
environments.truncate(environments_len + 1);
environments.pop()
};

let result = record
.consume()
.map_err(|err| err.inject_realm(context.realm().clone()))?;
Expand Down
10 changes: 10 additions & 0 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,16 @@ impl Context<'_> {
match native_error.kind {
#[cfg(feature = "fuzz")]
JsNativeErrorKind::NoInstructionsRemain => {
self.vm
.environments
.truncate(self.vm.frame().env_fp as usize);
self.vm.stack.truncate(self.vm.frame().fp as usize);
return CompletionRecord::Throw(err);
}
JsNativeErrorKind::RuntimeLimit => {
self.vm
.environments
.truncate(self.vm.frame().env_fp as usize);
self.vm.stack.truncate(self.vm.frame().fp as usize);
return CompletionRecord::Throw(err);
}
Expand All @@ -394,6 +401,9 @@ impl Context<'_> {
continue;
}

self.vm
.environments
.truncate(self.vm.frame().env_fp as usize);
self.vm.stack.truncate(self.vm.frame().fp as usize);
return CompletionRecord::Throw(err);
}
Expand Down
9 changes: 9 additions & 0 deletions boa_engine/src/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,12 @@ fn empty_return_values() {
"#}),
]);
}

#[test]
fn truncate_environments_on_non_caught_native_error() {
let source = "with (new Proxy({}, {has: p => false})) {a}";
run_test_actions([
TestAction::assert_native_error(source, JsNativeErrorKind::Reference, "a is not defined"),
TestAction::assert_native_error(source, JsNativeErrorKind::Reference, "a is not defined"),
]);
}