mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
fix: 将 VM 步数上限改为 KernelConfig 可配置项
This commit is contained in:
@@ -16,6 +16,7 @@ pub struct KernelConfig {
|
||||
pub min_pool_size: usize,
|
||||
pub max_pool_size: Option<usize>,
|
||||
pub max_dead_strings: Option<usize>,
|
||||
pub max_steps: Option<u64>,
|
||||
pub max_call_depth: usize,
|
||||
pub warmup_builtin_shapes: bool,
|
||||
pub warmup_builtin_code: bool,
|
||||
@@ -28,6 +29,7 @@ impl KernelConfig {
|
||||
min_pool_size: 4,
|
||||
max_pool_size: Some(8),
|
||||
max_dead_strings: Some(10_000),
|
||||
max_steps: None,
|
||||
max_call_depth: 1024,
|
||||
warmup_builtin_shapes: true,
|
||||
warmup_builtin_code: false,
|
||||
@@ -40,6 +42,7 @@ impl KernelConfig {
|
||||
min_pool_size: 8,
|
||||
max_pool_size: Some(32),
|
||||
max_dead_strings: Some(10_000),
|
||||
max_steps: None,
|
||||
max_call_depth: 1024,
|
||||
warmup_builtin_shapes: true,
|
||||
warmup_builtin_code: true,
|
||||
@@ -52,6 +55,7 @@ impl KernelConfig {
|
||||
min_pool_size: 16,
|
||||
max_pool_size: None,
|
||||
max_dead_strings: Some(5_000),
|
||||
max_steps: None,
|
||||
max_call_depth: 1024,
|
||||
warmup_builtin_shapes: true,
|
||||
warmup_builtin_code: true,
|
||||
@@ -187,6 +191,9 @@ mod tests {
|
||||
fn test_kernel_config_presets() {
|
||||
assert_eq!(KernelConfig::minimal().max_pool_size, Some(8));
|
||||
assert_eq!(KernelConfig::standard().max_pool_size, Some(32));
|
||||
assert_eq!(KernelConfig::minimal().max_steps, None);
|
||||
assert_eq!(KernelConfig::standard().max_steps, None);
|
||||
assert_eq!(KernelConfig::full().max_steps, None);
|
||||
assert!(!KernelConfig::minimal().warmup_builtin_ic);
|
||||
assert!(KernelConfig::full().warmup_builtin_ic);
|
||||
assert_eq!(KernelConfig::full().max_pool_size, None);
|
||||
|
||||
@@ -556,11 +556,12 @@ impl Vm {
|
||||
|
||||
fn dispatch(&mut self) -> Result<JsValue, String> {
|
||||
let mut steps: u64 = 0;
|
||||
const MAX_STEPS: u64 = 100_000_000;
|
||||
loop {
|
||||
steps += 1;
|
||||
if steps > MAX_STEPS {
|
||||
return Err(format!("VM step limit exceeded at pc={}", self.pc));
|
||||
if let Some(max_steps) = self.kernel.config.max_steps {
|
||||
if steps > max_steps {
|
||||
return Err(format!("VM step limit exceeded at pc={}", self.pc));
|
||||
}
|
||||
}
|
||||
if self.pc >= self.bytecode.len() {
|
||||
return Err("program counter out of bounds".into());
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_compiler::compiler::Compiler;
|
||||
use oxide_kernel::kernel::{KernelConfig, OxideKernel};
|
||||
use oxide_parser::Allocator;
|
||||
use oxide_vm::vm::Vm;
|
||||
|
||||
@@ -19,6 +22,23 @@ fn eval(source: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_with_kernel(source: &str, kernel: Arc<OxideKernel>) -> String {
|
||||
let allocator = Allocator::default();
|
||||
let program = match oxide_parser::parse(&allocator, source) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return format!("parse error: {}", e[0].message),
|
||||
};
|
||||
let module = match Compiler::new().compile(&program) {
|
||||
Ok(m) => m,
|
||||
Err(e) => return format!("compile error: {e}"),
|
||||
};
|
||||
let mut vm = Vm::with_kernel(kernel);
|
||||
match vm.run(&module) {
|
||||
Ok(result) => format!("{result}"),
|
||||
Err(e) => format!("vm error: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regression_to_boolean_empty_string() {
|
||||
assert_eq!(eval("!''"), "true", "NOT empty string should be true");
|
||||
@@ -74,3 +94,15 @@ fn regression_for_in_prototype_chain() {
|
||||
"for-in should include inherited constructor from prototype"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regression_vm_step_limit_is_configurable() {
|
||||
let mut config = KernelConfig::minimal();
|
||||
config.max_steps = Some(5);
|
||||
let kernel = Arc::new(OxideKernel::new(config));
|
||||
let result = eval_with_kernel("while (true) {}", kernel);
|
||||
assert!(
|
||||
result.contains("VM step limit exceeded"),
|
||||
"expected configurable step limit error, got: {result}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user