@@ -14,14 +14,25 @@ use alloc::{boxed::Box, vec::Vec};
14
14
use core:: fmt;
15
15
use core:: ops;
16
16
use core:: { u32, usize} ;
17
+ use once_cell:: unsync:: Lazy ;
17
18
use parity_wasm:: elements:: Local ;
18
19
use validation:: { DEFAULT_MEMORY_INDEX , DEFAULT_TABLE_INDEX } ;
19
20
20
21
/// 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
+ } ) ;
22
28
23
29
/// 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
+ } ) ;
25
36
26
37
/// This is a wrapper around u64 to allow us to treat runtime values as a tag-free `u64`
27
38
/// (where if the runtime value is <64 bits the upper bits are 0). This is safe, since
@@ -1517,16 +1528,16 @@ impl StackRecycler {
1517
1528
fn recreate_value_stack ( this : & mut Option < & mut Self > ) -> ValueStack {
1518
1529
let limit = this
1519
1530
. 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
+ } )
1521
1534
/ :: core:: mem:: size_of :: < RuntimeValueInternal > ( ) ;
1522
1535
1523
1536
let buf = this
1524
1537
. as_mut ( )
1525
1538
. and_then ( |this| this. value_stack_buf . take ( ) )
1526
1539
. 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] ;
1530
1541
buf. into_boxed_slice ( )
1531
1542
} ) ;
1532
1543
@@ -1536,7 +1547,9 @@ impl StackRecycler {
1536
1547
fn recreate_call_stack ( this : & mut Option < & mut Self > ) -> CallStack {
1537
1548
let limit = this
1538
1549
. 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
+ } ) ;
1540
1553
1541
1554
let buf = this
1542
1555
. as_mut ( )
@@ -1556,6 +1569,9 @@ impl StackRecycler {
1556
1569
1557
1570
impl Default for StackRecycler {
1558
1571
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
+ )
1560
1576
}
1561
1577
}
0 commit comments