From 0a1815cbb1f6fdd6a0284c82ea164553cb9b3e76 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Tue, 9 Jun 2026 21:07:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=97=A0=E9=99=90=E9=80=92=E5=BD=92=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_vm/src/dispatch/call.rs | 6 ++++++ crates/oxide_vm/src/vm.rs | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/oxide_vm/src/dispatch/call.rs b/crates/oxide_vm/src/dispatch/call.rs index 72d58c3c..f48b52b8 100644 --- a/crates/oxide_vm/src/dispatch/call.rs +++ b/crates/oxide_vm/src/dispatch/call.rs @@ -19,17 +19,23 @@ impl Vm { pub(crate) fn dispatch_native_call( &mut self, obj: &JsObject, callee: JsValue, this_reg: u8, first_arg_reg: u8, arg_count: usize, ) -> Result<(), String> { + if self.native_call_depth >= self.kernel.config.max_call_depth { + return Err("RangeError: Maximum call stack size exceeded".into()); + } let (args_buf, len) = Self::build_native_args(first_arg_reg, arg_count, this_reg); let args_slice = &args_buf[..len]; let func: NativeFn = unsafe { std::mem::transmute(obj.native_fn().unwrap()) }; self.regs[254] = callee; + self.native_call_depth += 1; match func(self, args_slice) { Ok(val) => { + self.native_call_depth -= 1; self.regs[0] = val; Ok(()) } Err(err_val) => { + self.native_call_depth -= 1; let (error, kind) = if err_val.is_object() { (err_val, self.thrown_error_kind(err_val)) } else { diff --git a/crates/oxide_vm/src/vm.rs b/crates/oxide_vm/src/vm.rs index 627ac952..8a6e8ad1 100644 --- a/crates/oxide_vm/src/vm.rs +++ b/crates/oxide_vm/src/vm.rs @@ -93,6 +93,7 @@ pub struct Vm { pub(crate) for_of_iters: Vec<*mut u8>, pub(crate) root_reg_limit: u8, pub(crate) active_reg_limit: u8, + pub(crate) native_call_depth: usize, } impl Vm { @@ -141,6 +142,7 @@ impl Vm { for_of_iters: Vec::new(), root_reg_limit: 0, active_reg_limit: 0, + native_call_depth: 0, } } @@ -171,6 +173,7 @@ impl Vm { for_of_iters: Vec::new(), root_reg_limit: 0, active_reg_limit: 0, + native_call_depth: 0, } } @@ -227,6 +230,7 @@ impl Vm { self.exception_value = None; self.pending_exception = None; self.pending_error_kind = None; + self.native_call_depth = 0; } pub fn reset(&mut self) { @@ -482,6 +486,9 @@ impl Vm { } if let Some(native_fn) = callee_obj.native_fn() { + if self.native_call_depth >= self.kernel.config.max_call_depth { + return Err("RangeError: Maximum call stack size exceeded".into()); + } let saved_regs = self.regs; let saved_this = self.regs[254]; let saved_callee = self.regs[253]; @@ -496,7 +503,9 @@ impl Vm { arg_regs[idx + 1] = 240 + idx as u8; } let func: NativeFn = unsafe { std::mem::transmute(native_fn) }; + self.native_call_depth += 1; let result = func(self, &arg_regs[..args.len().min(16) + 1]); + self.native_call_depth -= 1; self.regs = saved_regs; self.regs[254] = saved_this; self.regs[253] = saved_callee;