Skip to main content

wasmtime/engine/
serialization.rs

1//! This module implements serialization and deserialization of `Engine`
2//! configuration data which is embedded into compiled artifacts of Wasmtime.
3//!
4//! The data serialized here is used to double-check that when a module is
5//! loaded from one host onto another that it's compatible with the target host.
6//! Additionally though this data is the first data read from a precompiled
7//! artifact so it's "extra hardened" to provide reasonable-ish error messages
8//! for mismatching wasmtime versions. Once something successfully deserializes
9//! here it's assumed it's meant for this wasmtime so error messages are in
10//! general much worse afterwards.
11//!
12//! Wasmtime AOT artifacts are ELF files so the data for the engine here is
13//! stored into a section of the output file. The structure of this section is:
14//!
15//! 1. A version byte, currently `VERSION`.
16//! 2. A byte indicating how long the next field is.
17//! 3. A version string of the length of the previous byte value.
18//! 4. A `postcard`-encoded `Metadata` structure.
19//!
20//! This is hoped to help distinguish easily Wasmtime-based ELF files from
21//! other random ELF files, as well as provide better error messages for
22//! using wasmtime artifacts across versions.
23
24use crate::prelude::*;
25use crate::{Engine, ModuleVersionStrategy, Precompiled};
26use core::fmt;
27use core::str::FromStr;
28use object::endian::Endianness;
29#[cfg(any(feature = "cranelift", feature = "winch"))]
30use object::write::{Object, StandardSegment};
31use object::{
32    FileFlags, Object as _,
33    elf::FileHeader64,
34    read::elf::{ElfFile64, FileHeader, SectionHeader},
35};
36use serde_derive::{Deserialize, Serialize};
37use wasmtime_environ::{FlagValue, ObjectKind, OperatorCostStrategy, Tunables, obj};
38
39const VERSION: u8 = 0;
40
41/// Verifies that the serialized engine in `mmap` is compatible with the
42/// `engine` provided.
43///
44/// This function will verify that the `mmap` provided can be deserialized
45/// successfully and that the contents are all compatible with the `engine`
46/// provided here, notably compatible wasm features are enabled, compatible
47/// compiler options, etc. If a mismatch is found and the compilation metadata
48/// specified is incompatible then an error is returned.
49pub fn check_compatible(engine: &Engine, mmap: &[u8], expected: ObjectKind) -> Result<()> {
50    // Parse the input `mmap` as an ELF file and see if the header matches the
51    // Wasmtime-generated header. This includes a Wasmtime-specific `os_abi` and
52    // the `e_flags` field should indicate whether `expected` matches or not.
53    //
54    // Note that errors generated here could mean that a precompiled module was
55    // loaded as a component, or vice versa, both of which aren't supposed to
56    // work.
57    //
58    // Ideally we'd only `File::parse` once and avoid the linear
59    // `section_by_name` search here but the general serialization code isn't
60    // structured well enough to make this easy and additionally it's not really
61    // a perf issue right now so doing that is left for another day's
62    // refactoring.
63    let header = FileHeader64::<Endianness>::parse(mmap)
64        .map_err(obj::ObjectCrateErrorWrapper)
65        .context("failed to parse precompiled artifact as an ELF")?;
66    let endian = header
67        .endian()
68        .context("failed to parse header endianness")?;
69
70    let expected_e_flags = match expected {
71        ObjectKind::Module => obj::EF_WASMTIME_MODULE,
72        ObjectKind::Component => obj::EF_WASMTIME_COMPONENT,
73    };
74    ensure!(
75        (header.e_flags(endian) & expected_e_flags) == expected_e_flags,
76        "incompatible object file format"
77    );
78
79    let section_headers = header
80        .section_headers(endian, mmap)
81        .context("failed to parse section headers")?;
82    let strings = header
83        .section_strings(endian, mmap, section_headers)
84        .context("failed to parse strings table")?;
85    let sections = header
86        .sections(endian, mmap)
87        .context("failed to parse sections table")?;
88
89    let mut section_header = None;
90    for s in sections.iter() {
91        let name = s.name(endian, strings)?;
92        if name == obj::ELF_WASM_ENGINE.as_bytes() {
93            section_header = Some(s);
94        }
95    }
96    let Some(section_header) = section_header else {
97        bail!("failed to find section `{}`", obj::ELF_WASM_ENGINE)
98    };
99    let data = section_header
100        .data(endian, mmap)
101        .map_err(obj::ObjectCrateErrorWrapper)?;
102    let (first, data) = data
103        .split_first()
104        .ok_or_else(|| format_err!("invalid engine section"))?;
105    if *first != VERSION {
106        bail!("mismatched version in engine section");
107    }
108    let (len, data) = data
109        .split_first()
110        .ok_or_else(|| format_err!("invalid engine section"))?;
111    let len = usize::from(*len);
112    let (version, data) = if data.len() < len + 1 {
113        bail!("engine section too small")
114    } else {
115        data.split_at(len)
116    };
117
118    match &engine.config().module_version {
119        ModuleVersionStrategy::None => { /* ignore the version info, accept all */ }
120        _ => {
121            let version = core::str::from_utf8(&version)?;
122            if version != engine.config().module_version.as_str() {
123                bail!("Module was compiled with incompatible version '{version}'");
124            }
125        }
126    }
127    postcard::from_bytes::<Metadata<'_>>(data)?.check_compatible(engine)
128}
129
130#[cfg(any(feature = "cranelift", feature = "winch"))]
131pub fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>, metadata: &Metadata<'_>) {
132    let section = obj.add_section(
133        obj.segment_name(StandardSegment::Data).to_vec(),
134        obj::ELF_WASM_ENGINE.as_bytes().to_vec(),
135        object::SectionKind::ReadOnlyData,
136    );
137    let mut data = Vec::new();
138    data.push(VERSION);
139    let version = engine.config().module_version.as_str();
140    // This precondition is checked in Config::module_version:
141    assert!(
142        version.len() < 256,
143        "package version must be less than 256 bytes"
144    );
145    data.push(version.len() as u8);
146    data.extend_from_slice(version.as_bytes());
147    data.extend(postcard::to_allocvec(metadata).unwrap());
148    obj.set_section_data(section, data, 1);
149}
150
151fn detect_precompiled<'data, R: object::ReadRef<'data>>(
152    obj: ElfFile64<'data, Endianness, R>,
153) -> Option<Precompiled> {
154    match obj.flags() {
155        FileFlags::Elf {
156            os_abi: obj::ELFOSABI_WASMTIME,
157            abi_version: 0,
158            e_flags,
159        } if e_flags & obj::EF_WASMTIME_MODULE != 0 => Some(Precompiled::Module),
160        FileFlags::Elf {
161            os_abi: obj::ELFOSABI_WASMTIME,
162            abi_version: 0,
163            e_flags,
164        } if e_flags & obj::EF_WASMTIME_COMPONENT != 0 => Some(Precompiled::Component),
165        _ => None,
166    }
167}
168
169pub fn detect_precompiled_bytes(bytes: &[u8]) -> Option<Precompiled> {
170    detect_precompiled(ElfFile64::parse(bytes).ok()?)
171}
172
173#[cfg(feature = "std")]
174pub fn detect_precompiled_file(path: impl AsRef<std::path::Path>) -> Result<Option<Precompiled>> {
175    let read_cache = object::ReadCache::new(std::fs::File::open(path)?);
176    let obj = ElfFile64::parse(&read_cache)?;
177    Ok(detect_precompiled(obj))
178}
179
180#[derive(Serialize, Deserialize)]
181pub struct Metadata<'a> {
182    target: TryString,
183    #[serde(borrow)]
184    shared_flags: TryVec<(&'a str, FlagValue<'a>)>,
185    #[serde(borrow)]
186    isa_flags: TryVec<(&'a str, FlagValue<'a>)>,
187    tunables: Tunables,
188    features: u64,
189}
190
191impl Metadata<'_> {
192    #[cfg(any(feature = "cranelift", feature = "winch"))]
193    pub fn new(engine: &Engine) -> Result<Metadata<'static>> {
194        let compiler = engine.try_compiler()?;
195        Ok(Metadata {
196            target: compiler.triple().to_string().into(),
197            shared_flags: compiler.flags().into(),
198            isa_flags: compiler.isa_flags().into(),
199            tunables: engine.tunables().clone(),
200            features: engine.features().bits(),
201        })
202    }
203
204    fn check_compatible(mut self, engine: &Engine) -> Result<()> {
205        self.check_triple(engine)?;
206        self.check_shared_flags(engine)?;
207        self.check_isa_flags(engine)?;
208        self.check_tunables(&engine.tunables())?;
209        self.check_features(&engine.features())?;
210        Ok(())
211    }
212
213    fn check_triple(&self, engine: &Engine) -> Result<()> {
214        let engine_target = engine.target();
215        let module_target =
216            target_lexicon::Triple::from_str(&self.target).map_err(|e| format_err!(e))?;
217
218        if module_target.architecture != engine_target.architecture {
219            bail!(
220                "Module was compiled for architecture '{}'",
221                module_target.architecture
222            );
223        }
224
225        if module_target.operating_system != engine_target.operating_system {
226            bail!(
227                "Module was compiled for operating system '{}'",
228                module_target.operating_system
229            );
230        }
231
232        Ok(())
233    }
234
235    fn check_shared_flags(&mut self, engine: &Engine) -> Result<()> {
236        for (name, val) in self.shared_flags.iter() {
237            engine
238                .check_compatible_with_shared_flag(name, val)
239                .map_err(|s| crate::Error::msg(s))
240                .context("compilation settings of module incompatible with native host")?;
241        }
242        Ok(())
243    }
244
245    fn check_isa_flags(&mut self, engine: &Engine) -> Result<()> {
246        for (name, val) in self.isa_flags.iter() {
247            engine
248                .check_compatible_with_isa_flag(name, val)
249                .map_err(|s| crate::Error::msg(s))
250                .context("compilation settings of module incompatible with native host")?;
251        }
252        Ok(())
253    }
254
255    fn check_int<T: Eq + fmt::Display>(found: T, expected: T, feature: &str) -> Result<()> {
256        if found == expected {
257            return Ok(());
258        }
259
260        bail!(
261            "Module was compiled with a {feature} of '{found}' but '{expected}' is expected for the host"
262        );
263    }
264
265    fn check_bool(found: bool, expected: bool, feature: impl fmt::Display) -> Result<()> {
266        if found == expected {
267            return Ok(());
268        }
269
270        bail!(
271            "Module was compiled {} {} but it {} enabled for the host",
272            if found { "with" } else { "without" },
273            feature,
274            if expected { "is" } else { "is not" }
275        );
276    }
277
278    fn check_cost(
279        consume_fuel: bool,
280        found: &OperatorCostStrategy,
281        expected: &OperatorCostStrategy,
282    ) -> Result<()> {
283        if !consume_fuel {
284            return Ok(());
285        }
286
287        if found != expected {
288            bail!("Module costs are incompatible");
289        }
290
291        Ok(())
292    }
293
294    fn check_tunables(&mut self, other: &Tunables) -> Result<()> {
295        let Tunables {
296            collector,
297            memory_reservation,
298            memory_guard_size,
299            debug_native,
300            debug_guest,
301            debug_symbols,
302            parse_wasm_debuginfo,
303            consume_fuel,
304            ref operator_cost,
305            epoch_interruption,
306            memory_may_move,
307            guard_before_linear_memory,
308            table_lazy_init,
309            relaxed_simd_deterministic,
310            winch_callable,
311            signals_based_traps,
312            memory_init_cow,
313            inlining,
314            inlining_small_callee_size,
315            inlining_sum_size_threshold,
316            concurrency_support,
317            recording,
318
319            // This doesn't affect compilation, it's just a runtime setting.
320            memory_reservation_for_growth: _,
321
322            // This does technically affect compilation but modules with/without
323            // trap information can be loaded into engines with the opposite
324            // setting just fine (it's just a section in the compiled file and
325            // whether it's present or not)
326            generate_address_map: _,
327
328            // Just a debugging aid, doesn't affect functionality at all.
329            debug_adapter_modules: _,
330
331            // This is a runtime GC debugging setting, doesn't affect compilation.
332            gc_zeal_alloc_counter: _,
333
334            gc_heap_reservation,
335            gc_heap_guard_size,
336            gc_heap_may_move,
337
338            // This doesn't affect compilation, it's just a runtime setting.
339            gc_heap_reservation_for_growth: _,
340
341            // No need to match whether or not this metadata is emitted, if it
342            // is or isn't then that's fine, the runtime handles it the same
343            // way.
344            metadata_for_internal_asserts: _,
345            metadata_for_gc_heap_corruption: _,
346
347            // Only affects cold-block layout; a compiled artifact loads into an
348            // engine configured either way.
349            branch_hinting: _,
350        } = self.tunables;
351
352        Self::check_collector(collector, other.collector)?;
353        Self::check_int(
354            memory_reservation,
355            other.memory_reservation,
356            "memory reservation",
357        )?;
358        Self::check_int(
359            memory_guard_size,
360            other.memory_guard_size,
361            "memory guard size",
362        )?;
363        Self::check_bool(
364            debug_native,
365            other.debug_native,
366            "native debug information support",
367        )?;
368        Self::check_bool(debug_guest, other.debug_guest, "guest debug")?;
369        Self::check_bool(debug_symbols, other.debug_symbols, "debug symbols")?;
370        Self::check_bool(
371            parse_wasm_debuginfo,
372            other.parse_wasm_debuginfo,
373            "WebAssembly backtrace support",
374        )?;
375        Self::check_bool(consume_fuel, other.consume_fuel, "fuel support")?;
376        Self::check_cost(consume_fuel, operator_cost, &other.operator_cost)?;
377        Self::check_bool(
378            epoch_interruption,
379            other.epoch_interruption,
380            "epoch interruption",
381        )?;
382        Self::check_bool(memory_may_move, other.memory_may_move, "memory may move")?;
383        Self::check_bool(
384            guard_before_linear_memory,
385            other.guard_before_linear_memory,
386            "guard before linear memory",
387        )?;
388        Self::check_bool(table_lazy_init, other.table_lazy_init, "table lazy init")?;
389        Self::check_bool(
390            relaxed_simd_deterministic,
391            other.relaxed_simd_deterministic,
392            "relaxed simd deterministic semantics",
393        )?;
394        Self::check_bool(
395            winch_callable,
396            other.winch_callable,
397            "Winch calling convention",
398        )?;
399        Self::check_bool(
400            signals_based_traps,
401            other.signals_based_traps,
402            "Signals-based traps",
403        )?;
404        Self::check_bool(
405            memory_init_cow,
406            other.memory_init_cow,
407            "memory initialization with CoW",
408        )?;
409        Self::check_int(
410            inlining_small_callee_size,
411            other.inlining_small_callee_size,
412            "function inlining small-callee size",
413        )?;
414        Self::check_int(
415            inlining_sum_size_threshold,
416            other.inlining_sum_size_threshold,
417            "function inlining sum-size threshold",
418        )?;
419        Self::check_bool(
420            concurrency_support,
421            other.concurrency_support,
422            "concurrency support",
423        )?;
424        Self::check_bool(recording, other.recording, "RR recording support")?;
425        Self::check_inlining(inlining, other.inlining)?;
426        Self::check_int(
427            gc_heap_reservation,
428            other.gc_heap_reservation,
429            "GC heap reservation",
430        )?;
431        Self::check_int(
432            gc_heap_guard_size,
433            other.gc_heap_guard_size,
434            "GC heap guard size",
435        )?;
436        Self::check_bool(gc_heap_may_move, other.gc_heap_may_move, "GC heap may move")?;
437
438        Ok(())
439    }
440
441    fn check_features(&mut self, other: &wasmparser::WasmFeatures) -> Result<()> {
442        let module_features = wasmparser::WasmFeatures::from_bits_truncate(self.features);
443        let missing_features = (*other & module_features) ^ module_features;
444        for (name, _) in missing_features.iter_names() {
445            let name = name.to_ascii_lowercase();
446            bail!(
447                "Module was compiled with support for WebAssembly feature \
448                `{name}` but it is not enabled for the host",
449            );
450        }
451        Ok(())
452    }
453
454    fn check_collector(
455        module: Option<wasmtime_environ::Collector>,
456        host: Option<wasmtime_environ::Collector>,
457    ) -> Result<()> {
458        match (module, host) {
459            // If the module doesn't require GC support it doesn't matter
460            // whether the host has GC support enabled or not.
461            (None, _) => Ok(()),
462            (Some(module), Some(host)) if module == host => Ok(()),
463
464            (Some(_), None) => {
465                bail!("module was compiled with GC however GC is disabled in the host")
466            }
467
468            (Some(module), Some(host)) => {
469                bail!(
470                    "module was compiled for the {module} collector but \
471                     the host is configured to use the {host} collector",
472                )
473            }
474        }
475    }
476
477    fn check_inlining(
478        module: wasmtime_environ::Inlining,
479        host: wasmtime_environ::Inlining,
480    ) -> Result<()> {
481        if module == host {
482            return Ok(());
483        }
484
485        let desc = |cfg| match cfg {
486            wasmtime_environ::Inlining::No => "without intra-module inlining",
487            wasmtime_environ::Inlining::Yes => "with intra-module inlining",
488            wasmtime_environ::Inlining::InterModuleAndIntraGc => {
489                "with intra-module inlining only when using GC"
490            }
491            wasmtime_environ::Inlining::Intrinsics => "with intrinsic inlining",
492            wasmtime_environ::Inlining::InterModule => "with inter-module inlining",
493        };
494
495        let module = desc(module);
496        let host = desc(host);
497
498        bail!("module was compiled {module} however the host is configured {host}")
499    }
500}
501
502#[cfg(test)]
503mod test {
504    use super::*;
505    use crate::{Cache, Config, Module, OptLevel};
506    use std::{
507        collections::hash_map::DefaultHasher,
508        hash::{Hash, Hasher},
509    };
510    use tempfile::TempDir;
511
512    #[test]
513    fn test_architecture_mismatch() -> Result<()> {
514        let engine = Engine::default();
515        let mut metadata = Metadata::new(&engine)?;
516        metadata.target = "unknown-generic-linux".to_string().into();
517
518        match metadata.check_compatible(&engine) {
519            Ok(_) => unreachable!(),
520            Err(e) => assert_eq!(
521                e.to_string(),
522                "Module was compiled for architecture 'unknown'",
523            ),
524        }
525
526        Ok(())
527    }
528
529    // Note that this test runs on a platform that is known to use Cranelift
530    #[test]
531    #[cfg(all(target_arch = "x86_64", not(miri)))]
532    fn test_os_mismatch() -> Result<()> {
533        let engine = Engine::default();
534        let mut metadata = Metadata::new(&engine)?;
535
536        metadata.target = format!(
537            "{}-generic-unknown",
538            target_lexicon::Triple::host().architecture
539        )
540        .into();
541
542        match metadata.check_compatible(&engine) {
543            Ok(_) => unreachable!(),
544            Err(e) => assert_eq!(
545                e.to_string(),
546                "Module was compiled for operating system 'unknown'",
547            ),
548        }
549
550        Ok(())
551    }
552
553    fn assert_contains(error: &Error, msg: &str) {
554        let msg = msg.trim();
555        if error.chain().any(|e| e.to_string().contains(msg)) {
556            return;
557        }
558
559        panic!("failed to find:\n\n'''{msg}\n'''\n\nwithin error message:\n\n'''{error:?}'''")
560    }
561
562    #[test]
563    fn test_cranelift_flags_mismatch() -> Result<()> {
564        let engine = Engine::default();
565        let mut metadata = Metadata::new(&engine)?;
566
567        metadata
568            .shared_flags
569            .push(("preserve_frame_pointers", FlagValue::Bool(false)))?;
570
571        match metadata.check_compatible(&engine) {
572            Ok(_) => unreachable!(),
573            Err(e) => {
574                assert_contains(
575                    &e,
576                    "compilation settings of module incompatible with native host",
577                );
578                assert_contains(
579                    &e,
580                    "setting \"preserve_frame_pointers\" is configured to Bool(false) which is not supported",
581                );
582            }
583        }
584
585        Ok(())
586    }
587
588    #[test]
589    fn test_isa_flags_mismatch() -> Result<()> {
590        let engine = Engine::default();
591        let mut metadata = Metadata::new(&engine)?;
592
593        metadata
594            .isa_flags
595            .push(("not_a_flag", FlagValue::Bool(true)))?;
596
597        match metadata.check_compatible(&engine) {
598            Ok(_) => unreachable!(),
599            Err(e) => {
600                assert_contains(
601                    &e,
602                    "compilation settings of module incompatible with native host",
603                );
604                assert_contains(
605                    &e,
606                    "don't know how to test for target-specific flag \"not_a_flag\" at runtime",
607                );
608            }
609        }
610
611        Ok(())
612    }
613
614    #[test]
615    #[cfg_attr(any(miri, not(has_native_signals)), ignore)]
616    #[cfg(target_pointer_width = "64")] // different defaults on 32-bit platforms
617    fn test_tunables_int_mismatch() -> Result<()> {
618        let engine = Engine::default();
619        let mut metadata = Metadata::new(&engine)?;
620
621        metadata.tunables.memory_guard_size = 0;
622
623        match metadata.check_compatible(&engine) {
624            Ok(_) => unreachable!(),
625            Err(e) => assert_eq!(
626                e.to_string(),
627                "Module was compiled with a memory guard size of '0' but '33554432' is expected for the host"
628            ),
629        }
630
631        Ok(())
632    }
633
634    #[test]
635    fn test_tunables_bool_mismatch() -> Result<()> {
636        let mut config = Config::new();
637        config.epoch_interruption(true);
638
639        let engine = Engine::new(&config)?;
640        let mut metadata = Metadata::new(&engine)?;
641        metadata.tunables.epoch_interruption = false;
642
643        match metadata.check_compatible(&engine) {
644            Ok(_) => unreachable!(),
645            Err(e) => assert_eq!(
646                e.to_string(),
647                "Module was compiled without epoch interruption but it is enabled for the host"
648            ),
649        }
650
651        let mut config = Config::new();
652        config.epoch_interruption(false);
653
654        let engine = Engine::new(&config)?;
655        let mut metadata = Metadata::new(&engine)?;
656        metadata.tunables.epoch_interruption = true;
657
658        match metadata.check_compatible(&engine) {
659            Ok(_) => unreachable!(),
660            Err(e) => assert_eq!(
661                e.to_string(),
662                "Module was compiled with epoch interruption but it is not enabled for the host"
663            ),
664        }
665
666        Ok(())
667    }
668
669    /// This test is only run a platform that is known to implement threads
670    #[test]
671    #[cfg(all(target_arch = "x86_64", not(miri)))]
672    fn test_feature_mismatch() -> Result<()> {
673        let mut config = Config::new();
674        config.wasm_threads(true);
675
676        let engine = Engine::new(&config)?;
677        let mut metadata = Metadata::new(&engine)?;
678        metadata.features &= !wasmparser::WasmFeatures::THREADS.bits();
679
680        // If a feature is disabled in the module and enabled in the host,
681        // that's always ok.
682        metadata.check_compatible(&engine)?;
683
684        let mut config = Config::new();
685        config.wasm_threads(false);
686
687        let engine = Engine::new(&config)?;
688        let mut metadata = Metadata::new(&engine)?;
689        metadata.features |= wasmparser::WasmFeatures::THREADS.bits();
690
691        match metadata.check_compatible(&engine) {
692            Ok(_) => unreachable!(),
693            Err(e) => assert_eq!(
694                e.to_string(),
695                "Module was compiled with support for WebAssembly feature \
696                `threads` but it is not enabled for the host"
697            ),
698        }
699
700        Ok(())
701    }
702
703    #[test]
704    fn engine_weak_upgrades() {
705        let engine = Engine::default();
706        let weak = engine.weak();
707        weak.upgrade()
708            .expect("engine is still alive, so weak reference can upgrade");
709        drop(engine);
710        assert!(
711            weak.upgrade().is_none(),
712            "engine was dropped, so weak reference cannot upgrade"
713        );
714    }
715
716    #[test]
717    #[cfg_attr(miri, ignore)]
718    fn cache_accounts_for_opt_level() -> Result<()> {
719        let _ = env_logger::try_init();
720
721        let td = TempDir::new()?;
722        let config_path = td.path().join("config.toml");
723        std::fs::write(
724            &config_path,
725            &format!(
726                "
727                    [cache]
728                    directory = '{}'
729                ",
730                td.path().join("cache").display()
731            ),
732        )?;
733        let mut cfg = Config::new();
734        cfg.cranelift_opt_level(OptLevel::None)
735            .cache(Some(Cache::from_file(Some(&config_path))?));
736        let engine = Engine::new(&cfg)?;
737        Module::new(&engine, "(module (func))")?;
738        let cache_config = engine
739            .config()
740            .cache
741            .as_ref()
742            .expect("Missing cache config");
743        assert_eq!(cache_config.cache_hits(), 0);
744        assert_eq!(cache_config.cache_misses(), 1);
745        Module::new(&engine, "(module (func))")?;
746        assert_eq!(cache_config.cache_hits(), 1);
747        assert_eq!(cache_config.cache_misses(), 1);
748
749        let mut cfg = Config::new();
750        cfg.cranelift_opt_level(OptLevel::Speed)
751            .cache(Some(Cache::from_file(Some(&config_path))?));
752        let engine = Engine::new(&cfg)?;
753        let cache_config = engine
754            .config()
755            .cache
756            .as_ref()
757            .expect("Missing cache config");
758        Module::new(&engine, "(module (func))")?;
759        assert_eq!(cache_config.cache_hits(), 0);
760        assert_eq!(cache_config.cache_misses(), 1);
761        Module::new(&engine, "(module (func))")?;
762        assert_eq!(cache_config.cache_hits(), 1);
763        assert_eq!(cache_config.cache_misses(), 1);
764
765        let mut cfg = Config::new();
766        cfg.cranelift_opt_level(OptLevel::SpeedAndSize)
767            .cache(Some(Cache::from_file(Some(&config_path))?));
768        let engine = Engine::new(&cfg)?;
769        let cache_config = engine
770            .config()
771            .cache
772            .as_ref()
773            .expect("Missing cache config");
774        Module::new(&engine, "(module (func))")?;
775        assert_eq!(cache_config.cache_hits(), 0);
776        assert_eq!(cache_config.cache_misses(), 1);
777        Module::new(&engine, "(module (func))")?;
778        assert_eq!(cache_config.cache_hits(), 1);
779        assert_eq!(cache_config.cache_misses(), 1);
780
781        let mut cfg = Config::new();
782        cfg.debug_info(true)
783            .cache(Some(Cache::from_file(Some(&config_path))?));
784        let engine = Engine::new(&cfg)?;
785        let cache_config = engine
786            .config()
787            .cache
788            .as_ref()
789            .expect("Missing cache config");
790        Module::new(&engine, "(module (func))")?;
791        assert_eq!(cache_config.cache_hits(), 0);
792        assert_eq!(cache_config.cache_misses(), 1);
793        Module::new(&engine, "(module (func))")?;
794        assert_eq!(cache_config.cache_hits(), 1);
795        assert_eq!(cache_config.cache_misses(), 1);
796
797        Ok(())
798    }
799
800    #[test]
801    fn precompile_compatibility_key_accounts_for_opt_level() {
802        fn hash_for_config(cfg: &Config) -> u64 {
803            let engine = Engine::new(cfg).expect("Config should be valid");
804            let mut hasher = DefaultHasher::new();
805            engine.precompile_compatibility_hash().hash(&mut hasher);
806            hasher.finish()
807        }
808        let mut cfg = Config::new();
809        cfg.cranelift_opt_level(OptLevel::None);
810        let opt_none_hash = hash_for_config(&cfg);
811        cfg.cranelift_opt_level(OptLevel::Speed);
812        let opt_speed_hash = hash_for_config(&cfg);
813        assert_ne!(opt_none_hash, opt_speed_hash)
814    }
815
816    #[test]
817    fn precompile_compatibility_key_accounts_for_module_version_strategy() -> Result<()> {
818        fn hash_for_config(cfg: &Config) -> u64 {
819            let engine = Engine::new(cfg).expect("Config should be valid");
820            let mut hasher = DefaultHasher::new();
821            engine.precompile_compatibility_hash().hash(&mut hasher);
822            hasher.finish()
823        }
824        let mut cfg_custom_version = Config::new();
825        cfg_custom_version.module_version(ModuleVersionStrategy::Custom("1.0.1111".to_string()))?;
826        let custom_version_hash = hash_for_config(&cfg_custom_version);
827
828        let mut cfg_default_version = Config::new();
829        cfg_default_version.module_version(ModuleVersionStrategy::WasmtimeVersion)?;
830        let default_version_hash = hash_for_config(&cfg_default_version);
831
832        let mut cfg_none_version = Config::new();
833        cfg_none_version.module_version(ModuleVersionStrategy::None)?;
834        let none_version_hash = hash_for_config(&cfg_none_version);
835
836        assert_ne!(custom_version_hash, default_version_hash);
837        assert_ne!(custom_version_hash, none_version_hash);
838        assert_ne!(default_version_hash, none_version_hash);
839
840        Ok(())
841    }
842
843    #[test]
844    #[cfg_attr(miri, ignore)]
845    #[cfg(feature = "component-model")]
846    fn components_are_cached() -> Result<()> {
847        use crate::component::Component;
848
849        let td = TempDir::new()?;
850        let config_path = td.path().join("config.toml");
851        std::fs::write(
852            &config_path,
853            &format!(
854                "
855                    [cache]
856                    directory = '{}'
857                ",
858                td.path().join("cache").display()
859            ),
860        )?;
861        let mut cfg = Config::new();
862        cfg.cache(Some(Cache::from_file(Some(&config_path))?));
863        let engine = Engine::new(&cfg)?;
864        let cache_config = engine
865            .config()
866            .cache
867            .as_ref()
868            .expect("Missing cache config");
869        Component::new(&engine, "(component (core module (func)))")?;
870        assert_eq!(cache_config.cache_hits(), 0);
871        assert_eq!(cache_config.cache_misses(), 1);
872        Component::new(&engine, "(component (core module (func)))")?;
873        assert_eq!(cache_config.cache_hits(), 1);
874        assert_eq!(cache_config.cache_misses(), 1);
875
876        Ok(())
877    }
878}