Skip to content

Commit

Permalink
Add additional checks and extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Oct 3, 2023
1 parent ad67731 commit 961d67e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
6 changes: 6 additions & 0 deletions boa_engine/src/object/internal_methods/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub(crate) fn native_function_call(
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
// We technically don't need this since native functions don't push any new frames to the
// vm, but we'll eventually have to combine the native stack with the vm stack.
context.check_runtime_limits()?;
let this_function_object = obj.clone();
let object = obj.borrow();

Expand Down Expand Up @@ -135,6 +138,9 @@ fn native_function_construct(
new_target: &JsObject,
context: &mut Context<'_>,
) -> JsResult<JsObject> {
// We technically don't need this since native functions don't push any new frames to the
// vm, but we'll eventually have to combine the native stack with the vm stack.
context.check_runtime_limits()?;
let this_function_object = obj.clone();
let object = obj.borrow();

Expand Down
22 changes: 2 additions & 20 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,16 +1019,7 @@ impl JsObject {
args: &[JsValue],
context: &mut Context<'_>,
) -> JsResult<JsValue> {
if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum number of recursive calls")
.into());
}
if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum call stack length")
.into());
}
context.check_runtime_limits()?;
let old_realm = context.realm().clone();

let context = &mut context.guard(move |ctx| {
Expand Down Expand Up @@ -1180,16 +1171,7 @@ impl JsObject {
this_target: &JsValue,
context: &mut Context<'_>,
) -> JsResult<Self> {
if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum number of recursive calls")
.into());
}
if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum call stack length")
.into());
}
context.check_runtime_limits()?;
let old_realm = context.realm().clone();
let context = &mut context.guard(move |ctx| {
ctx.enter_realm(old_realm);
Expand Down
20 changes: 19 additions & 1 deletion boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
environments::{DeclarativeEnvironment, EnvironmentStack},
script::Script,
vm::code_block::Readable,
Context, JsError, JsNativeErrorKind, JsObject, JsResult, JsValue, Module,
Context, JsError, JsNativeErrorKind, JsObject, JsResult, JsValue, Module, JsNativeError,
};

use boa_gc::{custom_trace, Finalize, Gc, Trace};
Expand Down Expand Up @@ -412,4 +412,22 @@ impl Context<'_> {
}
}
}

/// Checks if we haven't exceeded the defined runtime limits.
pub(crate) fn check_runtime_limits(&self) -> JsResult<()> {
// Must throw if the number of recursive calls exceeds the defined limit.
if self.vm.runtime_limits.recursion_limit() <= self.vm.frames.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum number of recursive calls")
.into());
}
// Must throw if the stack size exceeds the defined maximum length.
if self.vm.runtime_limits.stack_size_limit() <= self.vm.stack.len() {
return Err(JsNativeError::runtime_limit()
.with_message("exceeded maximum call stack length")
.into());
}

Ok(())
}
}

0 comments on commit 961d67e

Please sign in to comment.