wasmtime_environ/tunables.rs
1use crate::TripleExt;
2use anyhow::{anyhow, bail, Result};
3use core::fmt;
4use serde_derive::{Deserialize, Serialize};
5use target_lexicon::{PointerWidth, Triple};
6
7macro_rules! define_tunables {
8 (
9 $(#[$outer_attr:meta])*
10 pub struct $tunables:ident {
11 $(
12 $(#[$field_attr:meta])*
13 pub $field:ident : $field_ty:ty,
14 )*
15 }
16
17 pub struct $config_tunables:ident {
18 ...
19 }
20 ) => {
21 $(#[$outer_attr])*
22 pub struct $tunables {
23 $(
24 $(#[$field_attr])*
25 pub $field: $field_ty,
26 )*
27 }
28
29 /// Optional tunable configuration options used in `wasmtime::Config`
30 #[derive(Default, Clone)]
31 #[allow(missing_docs, reason = "macro-generated fields")]
32 pub struct $config_tunables {
33 $(pub $field: Option<$field_ty>,)*
34 }
35
36 impl $config_tunables {
37 /// Formats configured fields into `f`.
38 pub fn format(&self, f: &mut fmt::DebugStruct<'_,'_>) {
39 $(
40 if let Some(val) = &self.$field {
41 f.field(stringify!($field), val);
42 }
43 )*
44 }
45
46 /// Configure the `Tunables` provided.
47 pub fn configure(&self, tunables: &mut Tunables) {
48 $(
49 if let Some(val) = self.$field {
50 tunables.$field = val;
51 }
52 )*
53 }
54 }
55 };
56}
57
58define_tunables! {
59 /// Tunable parameters for WebAssembly compilation.
60 #[derive(Clone, Hash, Serialize, Deserialize, Debug)]
61 pub struct Tunables {
62 /// The garbage collector implementation to use, which implies the layout of
63 /// GC objects and barriers that must be emitted in Wasm code.
64 pub collector: Option<Collector>,
65
66 /// Initial size, in bytes, to be allocated for linear memories.
67 pub memory_reservation: u64,
68
69 /// The size, in bytes, of the guard page region for linear memories.
70 pub memory_guard_size: u64,
71
72 /// The size, in bytes, to allocate at the end of a relocated linear
73 /// memory for growth.
74 pub memory_reservation_for_growth: u64,
75
76 /// Whether or not to generate native DWARF debug information.
77 pub generate_native_debuginfo: bool,
78
79 /// Whether or not to retain DWARF sections in compiled modules.
80 pub parse_wasm_debuginfo: bool,
81
82 /// Whether or not fuel is enabled for generated code, meaning that fuel
83 /// will be consumed every time a wasm instruction is executed.
84 pub consume_fuel: bool,
85
86 /// Whether or not we use epoch-based interruption.
87 pub epoch_interruption: bool,
88
89 /// Whether or not linear memories are allowed to be reallocated after
90 /// initial allocation at runtime.
91 pub memory_may_move: bool,
92
93 /// Whether or not linear memory allocations will have a guard region at the
94 /// beginning of the allocation in addition to the end.
95 pub guard_before_linear_memory: bool,
96
97 /// Whether to initialize tables lazily, so that instantiation is fast but
98 /// indirect calls are a little slower. If false, tables are initialized
99 /// eagerly from any active element segments that apply to them during
100 /// instantiation.
101 pub table_lazy_init: bool,
102
103 /// Indicates whether an address map from compiled native code back to wasm
104 /// offsets in the original file is generated.
105 pub generate_address_map: bool,
106
107 /// Flag for the component module whether adapter modules have debug
108 /// assertions baked into them.
109 pub debug_adapter_modules: bool,
110
111 /// Whether or not lowerings for relaxed simd instructions are forced to
112 /// be deterministic.
113 pub relaxed_simd_deterministic: bool,
114
115 /// Whether or not Wasm functions target the winch abi.
116 pub winch_callable: bool,
117
118 /// Whether or not the host will be using native signals (e.g. SIGILL,
119 /// SIGSEGV, etc) to implement traps.
120 pub signals_based_traps: bool,
121
122 /// Whether CoW images might be used to initialize linear memories.
123 pub memory_init_cow: bool,
124 }
125
126 pub struct ConfigTunables {
127 ...
128 }
129}
130
131impl Tunables {
132 /// Returns a `Tunables` configuration assumed for running code on the host.
133 pub fn default_host() -> Self {
134 if cfg!(miri) {
135 Tunables::default_miri()
136 } else if cfg!(target_pointer_width = "32") {
137 Tunables::default_u32()
138 } else if cfg!(target_pointer_width = "64") {
139 Tunables::default_u64()
140 } else {
141 panic!("unsupported target_pointer_width");
142 }
143 }
144
145 /// Returns the default set of tunables for the given target triple.
146 pub fn default_for_target(target: &Triple) -> Result<Self> {
147 if cfg!(miri) {
148 return Ok(Tunables::default_miri());
149 }
150 let mut ret = match target
151 .pointer_width()
152 .map_err(|_| anyhow!("failed to retrieve target pointer width"))?
153 {
154 PointerWidth::U32 => Tunables::default_u32(),
155 PointerWidth::U64 => Tunables::default_u64(),
156 _ => bail!("unsupported target pointer width"),
157 };
158
159 // Pulley targets never use signals-based-traps and also can't benefit
160 // from guard pages, so disable them.
161 if target.is_pulley() {
162 ret.signals_based_traps = false;
163 ret.memory_guard_size = 0;
164 }
165 Ok(ret)
166 }
167
168 /// Returns the default set of tunables for running under MIRI.
169 pub const fn default_miri() -> Tunables {
170 Tunables {
171 collector: None,
172
173 // No virtual memory tricks are available on miri so make these
174 // limits quite conservative.
175 memory_reservation: 1 << 20,
176 memory_guard_size: 0,
177 memory_reservation_for_growth: 0,
178
179 // General options which have the same defaults regardless of
180 // architecture.
181 generate_native_debuginfo: false,
182 parse_wasm_debuginfo: true,
183 consume_fuel: false,
184 epoch_interruption: false,
185 memory_may_move: true,
186 guard_before_linear_memory: true,
187 table_lazy_init: true,
188 generate_address_map: true,
189 debug_adapter_modules: false,
190 relaxed_simd_deterministic: false,
191 winch_callable: false,
192 signals_based_traps: false,
193 memory_init_cow: true,
194 }
195 }
196
197 /// Returns the default set of tunables for running under a 32-bit host.
198 pub const fn default_u32() -> Tunables {
199 Tunables {
200 // For 32-bit we scale way down to 10MB of reserved memory. This
201 // impacts performance severely but allows us to have more than a
202 // few instances running around.
203 memory_reservation: 10 * (1 << 20),
204 memory_guard_size: 0x1_0000,
205 memory_reservation_for_growth: 1 << 20, // 1MB
206 signals_based_traps: true,
207
208 ..Tunables::default_miri()
209 }
210 }
211
212 /// Returns the default set of tunables for running under a 64-bit host.
213 pub const fn default_u64() -> Tunables {
214 Tunables {
215 // 64-bit has tons of address space to static memories can have 4gb
216 // address space reservations liberally by default, allowing us to
217 // help eliminate bounds checks.
218 //
219 // A 32MiB default guard size is then allocated so we can remove
220 // explicit bounds checks if any static offset is less than this
221 // value. SpiderMonkey found, for example, that in a large corpus of
222 // wasm modules 20MiB was the maximum offset so this is the
223 // power-of-two-rounded up from that and matches SpiderMonkey.
224 memory_reservation: 1 << 32,
225 memory_guard_size: 32 << 20,
226
227 // We've got lots of address space on 64-bit so use a larger
228 // grow-into-this area, but on 32-bit we aren't as lucky. Miri is
229 // not exactly fast so reduce memory consumption instead of trying
230 // to avoid memory movement.
231 memory_reservation_for_growth: 2 << 30, // 2GB
232
233 signals_based_traps: true,
234 ..Tunables::default_miri()
235 }
236 }
237}
238
239/// The garbage collector implementation to use.
240#[derive(Clone, Copy, Hash, Serialize, Deserialize, Debug, PartialEq, Eq)]
241pub enum Collector {
242 /// The deferred reference-counting collector.
243 DeferredReferenceCounting,
244 /// The null collector.
245 Null,
246}
247
248impl fmt::Display for Collector {
249 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
250 match self {
251 Collector::DeferredReferenceCounting => write!(f, "deferred reference-counting"),
252 Collector::Null => write!(f, "null"),
253 }
254 }
255}