wasmtime_environ/compile/mod.rs
1//! A `Compilation` contains the compiled function bodies for a WebAssembly
2//! module.
3
4use crate::prelude::*;
5use crate::{obj, Tunables};
6use crate::{
7 BuiltinFunctionIndex, DefinedFuncIndex, FlagValue, FuncIndex, FunctionLoc, ObjectKind,
8 PrimaryMap, StaticModuleIndex, TripleExt, WasmError, WasmFuncType, WasmFunctionInfo,
9};
10use anyhow::Result;
11use object::write::{Object, SymbolId};
12use object::{Architecture, BinaryFormat, FileFlags};
13use std::any::Any;
14use std::borrow::Cow;
15use std::fmt;
16use std::path;
17use std::sync::Arc;
18
19mod address_map;
20mod module_artifacts;
21mod module_environ;
22mod module_types;
23mod trap_encoding;
24
25pub use self::address_map::*;
26pub use self::module_artifacts::*;
27pub use self::module_environ::*;
28pub use self::module_types::*;
29pub use self::trap_encoding::*;
30
31/// An error while compiling WebAssembly to machine code.
32#[derive(Debug)]
33pub enum CompileError {
34 /// A wasm translation error occurred.
35 Wasm(WasmError),
36
37 /// A compilation error occurred.
38 Codegen(String),
39
40 /// A compilation error occurred.
41 DebugInfoNotSupported,
42}
43
44impl fmt::Display for CompileError {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 CompileError::Wasm(_) => write!(f, "WebAssembly translation error"),
48 CompileError::Codegen(s) => write!(f, "Compilation error: {s}"),
49 CompileError::DebugInfoNotSupported => {
50 write!(f, "Debug info is not supported with this configuration")
51 }
52 }
53 }
54}
55
56impl From<WasmError> for CompileError {
57 fn from(err: WasmError) -> CompileError {
58 CompileError::Wasm(err)
59 }
60}
61
62impl core::error::Error for CompileError {
63 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64 match self {
65 CompileError::Wasm(e) => Some(e),
66 _ => None,
67 }
68 }
69}
70
71/// What relocations can be applied against.
72///
73/// Each wasm function may refer to various other `RelocationTarget` entries.
74#[derive(Copy, Clone, Debug, PartialEq, Eq)]
75pub enum RelocationTarget {
76 /// This is a reference to another defined wasm function in the same module.
77 Wasm(FuncIndex),
78 /// This is a reference to a trampoline for a builtin function.
79 Builtin(BuiltinFunctionIndex),
80 /// A compiler-generated libcall.
81 HostLibcall(obj::LibCall),
82 /// A pulley->host call from the interpreter.
83 PulleyHostcall(u32),
84}
85
86/// Implementation of an incremental compilation's key/value cache store.
87///
88/// In theory, this could just be Cranelift's `CacheKvStore` trait, but it is not as we want to
89/// make sure that wasmtime isn't too tied to Cranelift internals (and as a matter of fact, we
90/// can't depend on the Cranelift trait here).
91pub trait CacheStore: Send + Sync + std::fmt::Debug {
92 /// Try to retrieve an arbitrary cache key entry, and returns a reference to bytes that were
93 /// inserted via `Self::insert` before.
94 fn get(&self, key: &[u8]) -> Option<Cow<[u8]>>;
95
96 /// Given an arbitrary key and bytes, stores them in the cache.
97 ///
98 /// Returns false when insertion in the cache failed.
99 fn insert(&self, key: &[u8], value: Vec<u8>) -> bool;
100}
101
102/// Abstract trait representing the ability to create a `Compiler` below.
103///
104/// This is used in Wasmtime to separate compiler implementations, currently
105/// mostly used to separate Cranelift from Wasmtime itself.
106pub trait CompilerBuilder: Send + Sync + fmt::Debug {
107 /// Sets the target of compilation to the target specified.
108 fn target(&mut self, target: target_lexicon::Triple) -> Result<()>;
109
110 /// Enables clif output in the directory specified.
111 fn clif_dir(&mut self, _path: &path::Path) -> Result<()> {
112 anyhow::bail!("clif output not supported");
113 }
114
115 /// Returns the currently configured target triple that compilation will
116 /// produce artifacts for.
117 fn triple(&self) -> &target_lexicon::Triple;
118
119 /// Compiler-specific method to configure various settings in the compiler
120 /// itself.
121 ///
122 /// This is expected to be defined per-compiler. Compilers should return
123 /// errors for unknown names/values.
124 fn set(&mut self, name: &str, val: &str) -> Result<()>;
125
126 /// Compiler-specific method for configuring settings.
127 ///
128 /// Same as [`CompilerBuilder::set`] except for enabling boolean flags.
129 /// Currently cranelift uses this to sometimes enable a family of settings.
130 fn enable(&mut self, name: &str) -> Result<()>;
131
132 /// Returns a list of all possible settings that can be configured with
133 /// [`CompilerBuilder::set`] and [`CompilerBuilder::enable`].
134 fn settings(&self) -> Vec<Setting>;
135
136 /// Enables Cranelift's incremental compilation cache, using the given `CacheStore`
137 /// implementation.
138 ///
139 /// This will return an error if the compiler does not support incremental compilation.
140 fn enable_incremental_compilation(&mut self, cache_store: Arc<dyn CacheStore>) -> Result<()>;
141
142 /// Set the tunables for this compiler.
143 fn set_tunables(&mut self, tunables: Tunables) -> Result<()>;
144
145 /// Builds a new [`Compiler`] object from this configuration.
146 fn build(&self) -> Result<Box<dyn Compiler>>;
147
148 /// Enables or disables wmemcheck during runtime according to the wmemcheck CLI flag.
149 fn wmemcheck(&mut self, _enable: bool) {}
150}
151
152/// Description of compiler settings returned by [`CompilerBuilder::settings`].
153#[derive(Clone, Copy, Debug)]
154pub struct Setting {
155 /// The name of the setting.
156 pub name: &'static str,
157 /// The description of the setting.
158 pub description: &'static str,
159 /// The kind of the setting.
160 pub kind: SettingKind,
161 /// The supported values of the setting (for enum values).
162 pub values: Option<&'static [&'static str]>,
163}
164
165/// Different kinds of [`Setting`] values that can be configured in a
166/// [`CompilerBuilder`]
167#[derive(Clone, Copy, Debug)]
168pub enum SettingKind {
169 /// The setting is an enumeration, meaning it's one of a set of values.
170 Enum,
171 /// The setting is a number.
172 Num,
173 /// The setting is a boolean.
174 Bool,
175 /// The setting is a preset.
176 Preset,
177}
178
179/// An implementation of a compiler which can compile WebAssembly functions to
180/// machine code and perform other miscellaneous tasks needed by the JIT runtime.
181pub trait Compiler: Send + Sync {
182 /// Compiles the function `index` within `translation`.
183 ///
184 /// The body of the function is available in `data` and configuration
185 /// values are also passed in via `tunables`. Type information in
186 /// `translation` is all relative to `types`.
187 ///
188 /// This function returns a tuple:
189 ///
190 /// 1. Metadata about the wasm function itself.
191 /// 2. The function itself, as an `Any` to get downcasted later when passed
192 /// to `append_code`.
193 fn compile_function(
194 &self,
195 translation: &ModuleTranslation<'_>,
196 index: DefinedFuncIndex,
197 data: FunctionBodyData<'_>,
198 types: &ModuleTypesBuilder,
199 ) -> Result<(WasmFunctionInfo, Box<dyn Any + Send>), CompileError>;
200
201 /// Compile a trampoline for an array-call host function caller calling the
202 /// `index`th Wasm function.
203 ///
204 /// The trampoline should save the necessary state to record the
205 /// host-to-Wasm transition (e.g. registers used for fast stack walking).
206 fn compile_array_to_wasm_trampoline(
207 &self,
208 translation: &ModuleTranslation<'_>,
209 types: &ModuleTypesBuilder,
210 index: DefinedFuncIndex,
211 ) -> Result<Box<dyn Any + Send>, CompileError>;
212
213 /// Compile a trampoline for a Wasm caller calling a array callee with the
214 /// given signature.
215 ///
216 /// The trampoline should save the necessary state to record the
217 /// Wasm-to-host transition (e.g. registers used for fast stack walking).
218 fn compile_wasm_to_array_trampoline(
219 &self,
220 wasm_func_ty: &WasmFuncType,
221 ) -> Result<Box<dyn Any + Send>, CompileError>;
222
223 /// Creates a tramopline that can be used to call Wasmtime's implementation
224 /// of the builtin function specified by `index`.
225 ///
226 /// The trampoline created can technically have any ABI but currently has
227 /// the native ABI. This will then perform all the necessary duties of an
228 /// exit trampoline from wasm and then perform the actual dispatch to the
229 /// builtin function. Builtin functions in Wasmtime are stored in an array
230 /// in all `VMContext` pointers, so the call to the host is an indirect
231 /// call.
232 fn compile_wasm_to_builtin(
233 &self,
234 index: BuiltinFunctionIndex,
235 ) -> Result<Box<dyn Any + Send>, CompileError>;
236
237 /// Returns the list of relocations required for a function from one of the
238 /// previous `compile_*` functions above.
239 fn compiled_function_relocation_targets<'a>(
240 &'a self,
241 func: &'a dyn Any,
242 ) -> Box<dyn Iterator<Item = RelocationTarget> + 'a>;
243
244 /// Appends a list of compiled functions to an in-memory object.
245 ///
246 /// This function will receive the same `Box<dyn Any>` produced as part of
247 /// compilation from functions like `compile_function`,
248 /// `compile_host_to_wasm_trampoline`, and other component-related shims.
249 /// Internally this will take all of these functions and add information to
250 /// the object such as:
251 ///
252 /// * Compiled code in a `.text` section
253 /// * Unwind information in Wasmtime-specific sections
254 /// * Relocations, if necessary, for the text section
255 ///
256 /// Each function is accompanied with its desired symbol name and the return
257 /// value of this function is the symbol for each function as well as where
258 /// each function was placed within the object.
259 ///
260 /// The `resolve_reloc` argument is intended to resolving relocations
261 /// between function, chiefly resolving intra-module calls within one core
262 /// wasm module. The closure here takes two arguments:
263 ///
264 /// 1. First, the index within `funcs` that is being resolved,
265 ///
266 /// 2. and next the `RelocationTarget` which is the relocation target to
267 /// resolve.
268 ///
269 /// The return value is an index within `funcs` that the relocation points
270 /// to.
271 fn append_code(
272 &self,
273 obj: &mut Object<'static>,
274 funcs: &[(String, Box<dyn Any + Send>)],
275 resolve_reloc: &dyn Fn(usize, RelocationTarget) -> usize,
276 ) -> Result<Vec<(SymbolId, FunctionLoc)>>;
277
278 /// Creates a new `Object` file which is used to build the results of a
279 /// compilation into.
280 ///
281 /// The returned object file will have an appropriate
282 /// architecture/endianness for `self.triple()`, but at this time it is
283 /// always an ELF file, regardless of target platform.
284 fn object(&self, kind: ObjectKind) -> Result<Object<'static>> {
285 use target_lexicon::Architecture::*;
286
287 let triple = self.triple();
288 let mut obj = Object::new(
289 BinaryFormat::Elf,
290 match triple.architecture {
291 X86_32(_) => Architecture::I386,
292 X86_64 => Architecture::X86_64,
293 Arm(_) => Architecture::Arm,
294 Aarch64(_) => Architecture::Aarch64,
295 S390x => Architecture::S390x,
296 Riscv64(_) => Architecture::Riscv64,
297 // XXX: the `object` crate won't successfully build an object
298 // with relocations and such if it doesn't know the
299 // architecture, so just pretend we are riscv64. Yolo!
300 Pulley32 | Pulley64 | Pulley32be | Pulley64be => Architecture::Riscv64,
301 architecture => {
302 anyhow::bail!("target architecture {:?} is unsupported", architecture,);
303 }
304 },
305 match triple.endianness().unwrap() {
306 target_lexicon::Endianness::Little => object::Endianness::Little,
307 target_lexicon::Endianness::Big => object::Endianness::Big,
308 },
309 );
310 obj.flags = FileFlags::Elf {
311 os_abi: obj::ELFOSABI_WASMTIME,
312 e_flags: match kind {
313 ObjectKind::Module => obj::EF_WASMTIME_MODULE,
314 ObjectKind::Component => obj::EF_WASMTIME_COMPONENT,
315 },
316 abi_version: 0,
317 };
318 Ok(obj)
319 }
320
321 /// Returns the target triple that this compiler is compiling for.
322 fn triple(&self) -> &target_lexicon::Triple;
323
324 /// Returns the alignment necessary to align values to the page size of the
325 /// compilation target. Note that this may be an upper-bound where the
326 /// alignment is larger than necessary for some platforms since it may
327 /// depend on the platform's runtime configuration.
328 fn page_size_align(&self) -> u64 {
329 // Conservatively assume the max-of-all-supported-hosts for pulley
330 // and round up to 64k.
331 if self.triple().is_pulley() {
332 return 0x10000;
333 }
334
335 use target_lexicon::*;
336 match (self.triple().operating_system, self.triple().architecture) {
337 (
338 OperatingSystem::MacOSX { .. }
339 | OperatingSystem::Darwin(_)
340 | OperatingSystem::IOS(_)
341 | OperatingSystem::TvOS(_),
342 Architecture::Aarch64(..),
343 ) => 0x4000,
344 // 64 KB is the maximal page size (i.e. memory translation granule size)
345 // supported by the architecture and is used on some platforms.
346 (_, Architecture::Aarch64(..)) => 0x10000,
347 _ => 0x1000,
348 }
349 }
350
351 /// Returns a list of configured settings for this compiler.
352 fn flags(&self) -> Vec<(&'static str, FlagValue<'static>)>;
353
354 /// Same as [`Compiler::flags`], but ISA-specific (a cranelift-ism)
355 fn isa_flags(&self) -> Vec<(&'static str, FlagValue<'static>)>;
356
357 /// Get a flag indicating whether branch protection is enabled.
358 fn is_branch_protection_enabled(&self) -> bool;
359
360 /// Returns a suitable compiler usable for component-related compilations.
361 ///
362 /// Note that the `ComponentCompiler` trait can also be implemented for
363 /// `Self` in which case this function would simply return `self`.
364 #[cfg(feature = "component-model")]
365 fn component_compiler(&self) -> &dyn crate::component::ComponentCompiler;
366
367 /// Appends generated DWARF sections to the `obj` specified.
368 ///
369 /// The `translations` track all compiled functions and `get_func` can be
370 /// used to acquire the metadata for a particular function within a module.
371 fn append_dwarf<'a>(
372 &self,
373 obj: &mut Object<'_>,
374 translations: &'a PrimaryMap<StaticModuleIndex, ModuleTranslation<'a>>,
375 get_func: &'a dyn Fn(
376 StaticModuleIndex,
377 DefinedFuncIndex,
378 ) -> (SymbolId, &'a (dyn Any + Send)),
379 dwarf_package_bytes: Option<&'a [u8]>,
380 tunables: &'a Tunables,
381 ) -> Result<()>;
382
383 /// Creates a new System V Common Information Entry for the ISA.
384 ///
385 /// Returns `None` if the ISA does not support System V unwind information.
386 fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
387 // By default, an ISA cannot create a System V CIE.
388 None
389 }
390}