Skip to content

Commit 39aeda0

Browse files
committed
Allow to set stack limits from env variables
1 parent f7235e1 commit 39aeda0

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ num-traits = { version = "0.2.8", default-features = false }
2121
libc = { version = "0.2.58", optional = true}
2222
errno = { version = "0.2.4", optional = true }
2323
downcast-rs = { version = "1.2.0", default-features = false }
24+
once_cell = { version = "1.4.1", default-features = false }
2425

2526
[dev-dependencies]
2627
assert_matches = "1.1"

src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl ModuleInstance {
477477
/// a module which `start` function is not called.
478478
/// In order to complete instantiatiation `start` function must be called. However, there are
479479
/// situations where you might need to do additional setup before calling `start` function.
480-
/// For such sitations this separation might be useful.
480+
/// For such situations this separation might be useful.
481481
///
482482
/// See [`NotStartedModuleRef`] for details.
483483
///

src/runner.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@ use alloc::{boxed::Box, vec::Vec};
1414
use core::fmt;
1515
use core::ops;
1616
use core::{u32, usize};
17+
use once_cell::unsync::Lazy;
1718
use parity_wasm::elements::Local;
1819
use validation::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX};
1920

2021
/// Maximum number of bytes on the value stack.
21-
pub const DEFAULT_VALUE_STACK_LIMIT: usize = 1024 * 1024;
22+
pub const DEFAULT_VALUE_STACK_LIMIT: Lazy<usize> = Lazy::new(|| {
23+
option_env!("WASMI_VALUE_STACK_LIMIT")
24+
.and_then(|x| x.parse::<usize>().ok())
25+
.unwrap_or(1024)
26+
* 1024
27+
});
2228

2329
/// Maximum number of levels on the call stack.
24-
pub const DEFAULT_CALL_STACK_LIMIT: usize = 64 * 1024;
30+
pub const DEFAULT_CALL_STACK_LIMIT: Lazy<usize> = Lazy::new(|| {
31+
option_env!("WASMI_CALL_STACK_LIMIT")
32+
.and_then(|x| x.parse::<usize>().ok())
33+
.unwrap_or(64)
34+
* 1024
35+
});
2536

2637
/// This is a wrapper around u64 to allow us to treat runtime values as a tag-free `u64`
2738
/// (where if the runtime value is <64 bits the upper bits are 0). This is safe, since
@@ -1517,16 +1528,16 @@ impl StackRecycler {
15171528
fn recreate_value_stack(this: &mut Option<&mut Self>) -> ValueStack {
15181529
let limit = this
15191530
.as_ref()
1520-
.map_or(DEFAULT_VALUE_STACK_LIMIT, |this| this.value_stack_limit)
1531+
.map_or(DEFAULT_VALUE_STACK_LIMIT.clone(), |this| {
1532+
this.value_stack_limit
1533+
})
15211534
/ ::core::mem::size_of::<RuntimeValueInternal>();
15221535

15231536
let buf = this
15241537
.as_mut()
15251538
.and_then(|this| this.value_stack_buf.take())
15261539
.unwrap_or_else(|| {
1527-
let mut buf = Vec::new();
1528-
buf.reserve_exact(limit);
1529-
buf.resize(limit, RuntimeValueInternal(0));
1540+
let buf = vec![RuntimeValueInternal(0); limit];
15301541
buf.into_boxed_slice()
15311542
});
15321543

@@ -1536,7 +1547,9 @@ impl StackRecycler {
15361547
fn recreate_call_stack(this: &mut Option<&mut Self>) -> CallStack {
15371548
let limit = this
15381549
.as_ref()
1539-
.map_or(DEFAULT_CALL_STACK_LIMIT, |this| this.call_stack_limit);
1550+
.map_or(DEFAULT_CALL_STACK_LIMIT.clone(), |this| {
1551+
this.call_stack_limit
1552+
});
15401553

15411554
let buf = this
15421555
.as_mut()
@@ -1556,6 +1569,9 @@ impl StackRecycler {
15561569

15571570
impl Default for StackRecycler {
15581571
fn default() -> Self {
1559-
Self::with_limits(DEFAULT_VALUE_STACK_LIMIT, DEFAULT_CALL_STACK_LIMIT)
1572+
Self::with_limits(
1573+
DEFAULT_VALUE_STACK_LIMIT.clone(),
1574+
DEFAULT_CALL_STACK_LIMIT.clone(),
1575+
)
15601576
}
15611577
}

0 commit comments

Comments
 (0)