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            parse_wasm_debuginfo,
302            consume_fuel,
303            ref operator_cost,
304            epoch_interruption,
305            memory_may_move,
306            guard_before_linear_memory,
307            table_lazy_init,
308            relaxed_simd_deterministic,
309            winch_callable,
310            signals_based_traps,
311            memory_init_cow,
312            inlining,
313            inlining_intra_module,
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        } = self.tunables;
341
342        Self::check_collector(collector, other.collector)?;
343        Self::check_int(
344            memory_reservation,
345            other.memory_reservation,
346            "memory reservation",
347        )?;
348        Self::check_int(
349            memory_guard_size,
350            other.memory_guard_size,
351            "memory guard size",
352        )?;
353        Self::check_bool(
354            debug_native,
355            other.debug_native,
356            "native debug information support",
357        )?;
358        Self::check_bool(debug_guest, other.debug_guest, "guest debug")?;
359        Self::check_bool(
360            parse_wasm_debuginfo,
361            other.parse_wasm_debuginfo,
362            "WebAssembly backtrace support",
363        )?;
364        Self::check_bool(consume_fuel, other.consume_fuel, "fuel support")?;
365        Self::check_cost(consume_fuel, operator_cost, &other.operator_cost)?;
366        Self::check_bool(
367            epoch_interruption,
368            other.epoch_interruption,
369            "epoch interruption",
370        )?;
371        Self::check_bool(memory_may_move, other.memory_may_move, "memory may move")?;
372        Self::check_bool(
373            guard_before_linear_memory,
374            other.guard_before_linear_memory,
375            "guard before linear memory",
376        )?;
377        Self::check_bool(table_lazy_init, other.table_lazy_init, "table lazy init")?;
378        Self::check_bool(
379            relaxed_simd_deterministic,
380            other.relaxed_simd_deterministic,
381            "relaxed simd deterministic semantics",
382        )?;
383        Self::check_bool(
384            winch_callable,
385            other.winch_callable,
386            "Winch calling convention",
387        )?;
388        Self::check_bool(
389            signals_based_traps,
390            other.signals_based_traps,
391            "Signals-based traps",
392        )?;
393        Self::check_bool(
394            memory_init_cow,
395            other.memory_init_cow,
396            "memory initialization with CoW",
397        )?;
398        Self::check_bool(inlining, other.inlining, "function inlining")?;
399        Self::check_int(
400            inlining_small_callee_size,
401            other.inlining_small_callee_size,
402            "function inlining small-callee size",
403        )?;
404        Self::check_int(
405            inlining_sum_size_threshold,
406            other.inlining_sum_size_threshold,
407            "function inlining sum-size threshold",
408        )?;
409        Self::check_bool(
410            concurrency_support,
411            other.concurrency_support,
412            "concurrency support",
413        )?;
414        Self::check_bool(recording, other.recording, "RR recording support")?;
415        Self::check_intra_module_inlining(inlining_intra_module, other.inlining_intra_module)?;
416        Self::check_int(
417            gc_heap_reservation,
418            other.gc_heap_reservation,
419            "GC heap reservation",
420        )?;
421        Self::check_int(
422            gc_heap_guard_size,
423            other.gc_heap_guard_size,
424            "GC heap guard size",
425        )?;
426        Self::check_bool(gc_heap_may_move, other.gc_heap_may_move, "GC heap may move")?;
427
428        Ok(())
429    }
430
431    fn check_features(&mut self, other: &wasmparser::WasmFeatures) -> Result<()> {
432        let module_features = wasmparser::WasmFeatures::from_bits_truncate(self.features);
433        let missing_features = (*other & module_features) ^ module_features;
434        for (name, _) in missing_features.iter_names() {
435            let name = name.to_ascii_lowercase();
436            bail!(
437                "Module was compiled with support for WebAssembly feature \
438                `{name}` but it is not enabled for the host",
439            );
440        }
441        Ok(())
442    }
443
444    fn check_collector(
445        module: Option<wasmtime_environ::Collector>,
446        host: Option<wasmtime_environ::Collector>,
447    ) -> Result<()> {
448        match (module, host) {
449            // If the module doesn't require GC support it doesn't matter
450            // whether the host has GC support enabled or not.
451            (None, _) => Ok(()),
452            (Some(module), Some(host)) if module == host => Ok(()),
453
454            (Some(_), None) => {
455                bail!("module was compiled with GC however GC is disabled in the host")
456            }
457
458            (Some(module), Some(host)) => {
459                bail!(
460                    "module was compiled for the {module} collector but \
461                     the host is configured to use the {host} collector",
462                )
463            }
464        }
465    }
466
467    fn check_intra_module_inlining(
468        module: wasmtime_environ::IntraModuleInlining,
469        host: wasmtime_environ::IntraModuleInlining,
470    ) -> Result<()> {
471        if module == host {
472            return Ok(());
473        }
474
475        let desc = |cfg| match cfg {
476            wasmtime_environ::IntraModuleInlining::No => "without intra-module inlining",
477            wasmtime_environ::IntraModuleInlining::Yes => "with intra-module inlining",
478            wasmtime_environ::IntraModuleInlining::WhenUsingGc => {
479                "with intra-module inlining only when using GC"
480            }
481        };
482
483        let module = desc(module);
484        let host = desc(host);
485
486        bail!("module was compiled {module} however the host is configured {host}")
487    }
488}
489
490#[cfg(test)]
491mod test {
492    use super::*;
493    use crate::{Cache, Config, Module, OptLevel};
494    use std::{
495        collections::hash_map::DefaultHasher,
496        hash::{Hash, Hasher},
497    };
498    use tempfile::TempDir;
499
500    #[test]
501    fn test_architecture_mismatch() -> Result<()> {
502        let engine = Engine::default();
503        let mut metadata = Metadata::new(&engine)?;
504        metadata.target = "unknown-generic-linux".to_string().into();
505
506        match metadata.check_compatible(&engine) {
507            Ok(_) => unreachable!(),
508            Err(e) => assert_eq!(
509                e.to_string(),
510                "Module was compiled for architecture 'unknown'",
511            ),
512        }
513
514        Ok(())
515    }
516
517    // Note that this test runs on a platform that is known to use Cranelift
518    #[test]
519    #[cfg(all(target_arch = "x86_64", not(miri)))]
520    fn test_os_mismatch() -> Result<()> {
521        let engine = Engine::default();
522        let mut metadata = Metadata::new(&engine)?;
523
524        metadata.target = format!(
525            "{}-generic-unknown",
526            target_lexicon::Triple::host().architecture
527        )
528        .into();
529
530        match metadata.check_compatible(&engine) {
531            Ok(_) => unreachable!(),
532            Err(e) => assert_eq!(
533                e.to_string(),
534                "Module was compiled for operating system 'unknown'",
535            ),
536        }
537
538        Ok(())
539    }
540
541    fn assert_contains(error: &Error, msg: &str) {
542        let msg = msg.trim();
543        if error.chain().any(|e| e.to_string().contains(msg)) {
544            return;
545        }
546
547        panic!("failed to find:\n\n'''{msg}\n'''\n\nwithin error message:\n\n'''{error:?}'''")
548    }
549
550    #[test]
551    fn test_cranelift_flags_mismatch() -> Result<()> {
552        let engine = Engine::default();
553        let mut metadata = Metadata::new(&engine)?;
554
555        metadata
556            .shared_flags
557            .push(("preserve_frame_pointers", FlagValue::Bool(false)))?;
558
559        match metadata.check_compatible(&engine) {
560            Ok(_) => unreachable!(),
561            Err(e) => {
562                assert_contains(
563                    &e,
564                    "compilation settings of module incompatible with native host",
565                );
566                assert_contains(
567                    &e,
568                    "setting \"preserve_frame_pointers\" is configured to Bool(false) which is not supported",
569                );
570            }
571        }
572
573        Ok(())
574    }
575
576    #[test]
577    fn test_isa_flags_mismatch() -> Result<()> {
578        let engine = Engine::default();
579        let mut metadata = Metadata::new(&engine)?;
580
581        metadata
582            .isa_flags
583            .push(("not_a_flag", FlagValue::Bool(true)))?;
584
585        match metadata.check_compatible(&engine) {
586            Ok(_) => unreachable!(),
587            Err(e) => {
588                assert_contains(
589                    &e,
590                    "compilation settings of module incompatible with native host",
591                );
592                assert_contains(
593                    &e,
594                    "don't know how to test for target-specific flag \"not_a_flag\" at runtime",
595                );
596            }
597        }
598
599        Ok(())
600    }
601
602    #[test]
603    #[cfg_attr(miri, ignore)]
604    #[cfg(target_pointer_width = "64")] // different defaults on 32-bit platforms
605    fn test_tunables_int_mismatch() -> Result<()> {
606        let engine = Engine::default();
607        let mut metadata = Metadata::new(&engine)?;
608
609        metadata.tunables.memory_guard_size = 0;
610
611        match metadata.check_compatible(&engine) {
612            Ok(_) => unreachable!(),
613            Err(e) => assert_eq!(
614                e.to_string(),
615                "Module was compiled with a memory guard size of '0' but '33554432' is expected for the host"
616            ),
617        }
618
619        Ok(())
620    }
621
622    #[test]
623    fn test_tunables_bool_mismatch() -> Result<()> {
624        let mut config = Config::new();
625        config.epoch_interruption(true);
626
627        let engine = Engine::new(&config)?;
628        let mut metadata = Metadata::new(&engine)?;
629        metadata.tunables.epoch_interruption = false;
630
631        match metadata.check_compatible(&engine) {
632            Ok(_) => unreachable!(),
633            Err(e) => assert_eq!(
634                e.to_string(),
635                "Module was compiled without epoch interruption but it is enabled for the host"
636            ),
637        }
638
639        let mut config = Config::new();
640        config.epoch_interruption(false);
641
642        let engine = Engine::new(&config)?;
643        let mut metadata = Metadata::new(&engine)?;
644        metadata.tunables.epoch_interruption = true;
645
646        match metadata.check_compatible(&engine) {
647            Ok(_) => unreachable!(),
648            Err(e) => assert_eq!(
649                e.to_string(),
650                "Module was compiled with epoch interruption but it is not enabled for the host"
651            ),
652        }
653
654        Ok(())
655    }
656
657    /// This test is only run a platform that is known to implement threads
658    #[test]
659    #[cfg(all(target_arch = "x86_64", not(miri)))]
660    fn test_feature_mismatch() -> Result<()> {
661        let mut config = Config::new();
662        config.wasm_threads(true);
663
664        let engine = Engine::new(&config)?;
665        let mut metadata = Metadata::new(&engine)?;
666        metadata.features &= !wasmparser::WasmFeatures::THREADS.bits();
667
668        // If a feature is disabled in the module and enabled in the host,
669        // that's always ok.
670        metadata.check_compatible(&engine)?;
671
672        let mut config = Config::new();
673        config.wasm_threads(false);
674
675        let engine = Engine::new(&config)?;
676        let mut metadata = Metadata::new(&engine)?;
677        metadata.features |= wasmparser::WasmFeatures::THREADS.bits();
678
679        match metadata.check_compatible(&engine) {
680            Ok(_) => unreachable!(),
681            Err(e) => assert_eq!(
682                e.to_string(),
683                "Module was compiled with support for WebAssembly feature \
684                `threads` but it is not enabled for the host"
685            ),
686        }
687
688        Ok(())
689    }
690
691    #[test]
692    fn engine_weak_upgrades() {
693        let engine = Engine::default();
694        let weak = engine.weak();
695        weak.upgrade()
696            .expect("engine is still alive, so weak reference can upgrade");
697        drop(engine);
698        assert!(
699            weak.upgrade().is_none(),
700            "engine was dropped, so weak reference cannot upgrade"
701        );
702    }
703
704    #[test]
705    #[cfg_attr(miri, ignore)]
706    fn cache_accounts_for_opt_level() -> Result<()> {
707        let _ = env_logger::try_init();
708
709        let td = TempDir::new()?;
710        let config_path = td.path().join("config.toml");
711        std::fs::write(
712            &config_path,
713            &format!(
714                "
715                    [cache]
716                    directory = '{}'
717                ",
718                td.path().join("cache").display()
719            ),
720        )?;
721        let mut cfg = Config::new();
722        cfg.cranelift_opt_level(OptLevel::None)
723            .cache(Some(Cache::from_file(Some(&config_path))?));
724        let engine = Engine::new(&cfg)?;
725        Module::new(&engine, "(module (func))")?;
726        let cache_config = engine
727            .config()
728            .cache
729            .as_ref()
730            .expect("Missing cache config");
731        assert_eq!(cache_config.cache_hits(), 0);
732        assert_eq!(cache_config.cache_misses(), 1);
733        Module::new(&engine, "(module (func))")?;
734        assert_eq!(cache_config.cache_hits(), 1);
735        assert_eq!(cache_config.cache_misses(), 1);
736
737        let mut cfg = Config::new();
738        cfg.cranelift_opt_level(OptLevel::Speed)
739            .cache(Some(Cache::from_file(Some(&config_path))?));
740        let engine = Engine::new(&cfg)?;
741        let cache_config = engine
742            .config()
743            .cache
744            .as_ref()
745            .expect("Missing cache config");
746        Module::new(&engine, "(module (func))")?;
747        assert_eq!(cache_config.cache_hits(), 0);
748        assert_eq!(cache_config.cache_misses(), 1);
749        Module::new(&engine, "(module (func))")?;
750        assert_eq!(cache_config.cache_hits(), 1);
751        assert_eq!(cache_config.cache_misses(), 1);
752
753        let mut cfg = Config::new();
754        cfg.cranelift_opt_level(OptLevel::SpeedAndSize)
755            .cache(Some(Cache::from_file(Some(&config_path))?));
756        let engine = Engine::new(&cfg)?;
757        let cache_config = engine
758            .config()
759            .cache
760            .as_ref()
761            .expect("Missing cache config");
762        Module::new(&engine, "(module (func))")?;
763        assert_eq!(cache_config.cache_hits(), 0);
764        assert_eq!(cache_config.cache_misses(), 1);
765        Module::new(&engine, "(module (func))")?;
766        assert_eq!(cache_config.cache_hits(), 1);
767        assert_eq!(cache_config.cache_misses(), 1);
768
769        let mut cfg = Config::new();
770        cfg.debug_info(true)
771            .cache(Some(Cache::from_file(Some(&config_path))?));
772        let engine = Engine::new(&cfg)?;
773        let cache_config = engine
774            .config()
775            .cache
776            .as_ref()
777            .expect("Missing cache config");
778        Module::new(&engine, "(module (func))")?;
779        assert_eq!(cache_config.cache_hits(), 0);
780        assert_eq!(cache_config.cache_misses(), 1);
781        Module::new(&engine, "(module (func))")?;
782        assert_eq!(cache_config.cache_hits(), 1);
783        assert_eq!(cache_config.cache_misses(), 1);
784
785        Ok(())
786    }
787
788    #[test]
789    fn precompile_compatibility_key_accounts_for_opt_level() {
790        fn hash_for_config(cfg: &Config) -> u64 {
791            let engine = Engine::new(cfg).expect("Config should be valid");
792            let mut hasher = DefaultHasher::new();
793            engine.precompile_compatibility_hash().hash(&mut hasher);
794            hasher.finish()
795        }
796        let mut cfg = Config::new();
797        cfg.cranelift_opt_level(OptLevel::None);
798        let opt_none_hash = hash_for_config(&cfg);
799        cfg.cranelift_opt_level(OptLevel::Speed);
800        let opt_speed_hash = hash_for_config(&cfg);
801        assert_ne!(opt_none_hash, opt_speed_hash)
802    }
803
804    #[test]
805    fn precompile_compatibility_key_accounts_for_module_version_strategy() -> Result<()> {
806        fn hash_for_config(cfg: &Config) -> u64 {
807            let engine = Engine::new(cfg).expect("Config should be valid");
808            let mut hasher = DefaultHasher::new();
809            engine.precompile_compatibility_hash().hash(&mut hasher);
810            hasher.finish()
811        }
812        let mut cfg_custom_version = Config::new();
813        cfg_custom_version.module_version(ModuleVersionStrategy::Custom("1.0.1111".to_string()))?;
814        let custom_version_hash = hash_for_config(&cfg_custom_version);
815
816        let mut cfg_default_version = Config::new();
817        cfg_default_version.module_version(ModuleVersionStrategy::WasmtimeVersion)?;
818        let default_version_hash = hash_for_config(&cfg_default_version);
819
820        let mut cfg_none_version = Config::new();
821        cfg_none_version.module_version(ModuleVersionStrategy::None)?;
822        let none_version_hash = hash_for_config(&cfg_none_version);
823
824        assert_ne!(custom_version_hash, default_version_hash);
825        assert_ne!(custom_version_hash, none_version_hash);
826        assert_ne!(default_version_hash, none_version_hash);
827
828        Ok(())
829    }
830
831    #[test]
832    #[cfg_attr(miri, ignore)]
833    #[cfg(feature = "component-model")]
834    fn components_are_cached() -> Result<()> {
835        use crate::component::Component;
836
837        let td = TempDir::new()?;
838        let config_path = td.path().join("config.toml");
839        std::fs::write(
840            &config_path,
841            &format!(
842                "
843                    [cache]
844                    directory = '{}'
845                ",
846                td.path().join("cache").display()
847            ),
848        )?;
849        let mut cfg = Config::new();
850        cfg.cache(Some(Cache::from_file(Some(&config_path))?));
851        let engine = Engine::new(&cfg)?;
852        let cache_config = engine
853            .config()
854            .cache
855            .as_ref()
856            .expect("Missing cache config");
857        Component::new(&engine, "(component (core module (func)))")?;
858        assert_eq!(cache_config.cache_hits(), 0);
859        assert_eq!(cache_config.cache_misses(), 1);
860        Component::new(&engine, "(component (core module (func)))")?;
861        assert_eq!(cache_config.cache_hits(), 1);
862        assert_eq!(cache_config.cache_misses(), 1);
863
864        Ok(())
865    }
866}