1 //! Generate instance limits for the pooling allocation strategy.
2 
3 use arbitrary::{Arbitrary, Unstructured};
4 use wasmtime::MpkEnabled;
5 
6 /// Configuration for `wasmtime::PoolingAllocationStrategy`.
7 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
8 #[allow(missing_docs)]
9 pub struct PoolingAllocationConfig {
10     pub total_component_instances: u32,
11     pub total_core_instances: u32,
12     pub total_memories: u32,
13     pub total_tables: u32,
14     pub total_stacks: u32,
15 
16     pub max_memory_size: usize,
17     pub table_elements: usize,
18 
19     pub component_instance_size: usize,
20     pub max_memories_per_component: u32,
21     pub max_tables_per_component: u32,
22 
23     pub core_instance_size: usize,
24     pub max_memories_per_module: u32,
25     pub max_tables_per_module: u32,
26 
27     pub table_keep_resident: usize,
28     pub linear_memory_keep_resident: usize,
29 
30     pub decommit_batch_size: usize,
31     pub max_unused_warm_slots: u32,
32 
33     pub async_stack_zeroing: bool,
34     pub async_stack_keep_resident: usize,
35 
36     pub memory_protection_keys: MpkEnabled,
37     pub max_memory_protection_keys: usize,
38 }
39 
40 impl PoolingAllocationConfig {
41     /// Convert the generated limits to Wasmtime limits.
42     pub fn to_wasmtime(&self) -> wasmtime::PoolingAllocationConfig {
43         let mut cfg = wasmtime::PoolingAllocationConfig::default();
44 
45         cfg.total_component_instances(self.total_component_instances);
46         cfg.total_core_instances(self.total_core_instances);
47         cfg.total_memories(self.total_memories);
48         cfg.total_tables(self.total_tables);
49         cfg.total_stacks(self.total_stacks);
50 
51         cfg.max_memory_size(self.max_memory_size);
52         cfg.table_elements(self.table_elements);
53 
54         cfg.max_component_instance_size(self.component_instance_size);
55         cfg.max_memories_per_component(self.max_memories_per_component);
56         cfg.max_tables_per_component(self.max_tables_per_component);
57 
58         cfg.max_core_instance_size(self.core_instance_size);
59         cfg.max_memories_per_module(self.max_memories_per_module);
60         cfg.max_tables_per_module(self.max_tables_per_module);
61 
62         cfg.table_keep_resident(self.table_keep_resident);
63         cfg.linear_memory_keep_resident(self.linear_memory_keep_resident);
64 
65         cfg.decommit_batch_size(self.decommit_batch_size);
66         cfg.max_unused_warm_slots(self.max_unused_warm_slots);
67 
68         cfg.async_stack_zeroing(self.async_stack_zeroing);
69         cfg.async_stack_keep_resident(self.async_stack_keep_resident);
70 
71         cfg.memory_protection_keys(self.memory_protection_keys);
72 
73         cfg
74     }
75 }
76 
77 impl<'a> Arbitrary<'a> for PoolingAllocationConfig {
78     fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
79         const MAX_COUNT: u32 = 100;
80         const MAX_TABLES: u32 = 100;
81         const MAX_MEMORIES: u32 = 100;
82         const MAX_ELEMENTS: usize = 1000;
83         const MAX_MEMORY_SIZE: usize = 10 * (1 << 20); // 10 MiB
84         const MAX_SIZE: usize = 1 << 20; // 1 MiB
85         const MAX_INSTANCE_MEMORIES: u32 = 10;
86         const MAX_INSTANCE_TABLES: u32 = 10;
87 
88         let total_memories = u.int_in_range(1..=MAX_MEMORIES)?;
89 
90         Ok(Self {
91             total_component_instances: u.int_in_range(1..=MAX_COUNT)?,
92             total_core_instances: u.int_in_range(1..=MAX_COUNT)?,
93             total_memories,
94             total_tables: u.int_in_range(1..=MAX_TABLES)?,
95             total_stacks: u.int_in_range(1..=MAX_COUNT)?,
96 
97             max_memory_size: u.int_in_range(0..=MAX_MEMORY_SIZE)?,
98             table_elements: u.int_in_range(0..=MAX_ELEMENTS)?,
99 
100             component_instance_size: u.int_in_range(0..=MAX_SIZE)?,
101             max_memories_per_component: u.int_in_range(1..=MAX_INSTANCE_MEMORIES)?,
102             max_tables_per_component: u.int_in_range(1..=MAX_INSTANCE_TABLES)?,
103 
104             core_instance_size: u.int_in_range(0..=MAX_SIZE)?,
105             max_memories_per_module: u.int_in_range(1..=MAX_INSTANCE_MEMORIES)?,
106             max_tables_per_module: u.int_in_range(1..=MAX_INSTANCE_TABLES)?,
107 
108             table_keep_resident: u.int_in_range(0..=1 << 20)?,
109             linear_memory_keep_resident: u.int_in_range(0..=1 << 20)?,
110 
111             decommit_batch_size: u.int_in_range(1..=1000)?,
112             max_unused_warm_slots: u.int_in_range(0..=total_memories + 10)?,
113 
114             async_stack_zeroing: u.arbitrary()?,
115             async_stack_keep_resident: u.int_in_range(0..=1 << 20)?,
116 
117             memory_protection_keys: *u.choose(&[MpkEnabled::Auto, MpkEnabled::Disable])?,
118             max_memory_protection_keys: u.int_in_range(0..=20)?,
119         })
120     }
121 }
122