1use 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
41pub fn check_compatible(engine: &Engine, mmap: &[u8], expected: ObjectKind) -> Result<()> {
50 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 => { }
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 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 memory_reservation_for_growth: _,
321
322 generate_address_map: _,
327
328 debug_adapter_modules: _,
330
331 gc_zeal_alloc_counter: _,
333
334 gc_heap_reservation,
335 gc_heap_guard_size,
336 gc_heap_may_move,
337
338 gc_heap_reservation_for_growth: _,
340
341 metadata_for_internal_asserts: _,
345 metadata_for_gc_heap_corruption: _,
346
347 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 (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 #[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")] 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 #[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 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}