wasmi/engine/limits/
stack.rs1use crate::core::UntypedVal;
2use core::{
3 fmt::{self, Display},
4 mem::size_of,
5};
6
7const DEFAULT_MIN_VALUE_STACK_HEIGHT: usize = 1024;
9
10const DEFAULT_MAX_VALUE_STACK_HEIGHT: usize = 1024 * DEFAULT_MIN_VALUE_STACK_HEIGHT;
12
13const DEFAULT_MAX_RECURSION_DEPTH: usize = 1024;
15
16#[derive(Debug, Copy, Clone)]
18pub struct StackLimits {
19 pub initial_value_stack_height: usize,
21 pub maximum_value_stack_height: usize,
23 pub maximum_recursion_depth: usize,
25}
26
27#[derive(Debug)]
29pub enum LimitsError {
30 InitialValueStackExceedsMaximum,
32}
33
34impl Display for LimitsError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 LimitsError::InitialValueStackExceedsMaximum => {
38 write!(f, "initial value stack height exceeds maximum stack height")
39 }
40 }
41 }
42}
43
44impl StackLimits {
45 pub fn new(
51 initial_value_stack_height: usize,
52 maximum_value_stack_height: usize,
53 maximum_recursion_depth: usize,
54 ) -> Result<Self, LimitsError> {
55 if initial_value_stack_height > maximum_value_stack_height {
56 return Err(LimitsError::InitialValueStackExceedsMaximum);
57 }
58 Ok(Self {
59 initial_value_stack_height,
60 maximum_value_stack_height,
61 maximum_recursion_depth,
62 })
63 }
64}
65
66impl Default for StackLimits {
67 fn default() -> Self {
68 let register_len = size_of::<UntypedVal>();
69 let initial_value_stack_height = DEFAULT_MIN_VALUE_STACK_HEIGHT / register_len;
70 let maximum_value_stack_height = DEFAULT_MAX_VALUE_STACK_HEIGHT / register_len;
71 Self {
72 initial_value_stack_height,
73 maximum_value_stack_height,
74 maximum_recursion_depth: DEFAULT_MAX_RECURSION_DEPTH,
75 }
76 }
77}