A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasmtime: add Config::asan_compatible_code to override cfg!(asan) (#15156)
Pat Hickeye64003e6dbf1
diff --git a/crates/c-api/include/wasmtime/config.h b/crates/c-api/include/wasmtime/config.hindex eca01949e7..263b583caf 100644--- a/crates/c-api/include/wasmtime/config.h+++ b/crates/c-api/include/wasmtime/config.h@@ -472,6 +472,19 @@ WASMTIME_CONFIG_PROP(void, memory_reservation_for_growth, uint64_t)*/WASMTIME_CONFIG_PROP(void, native_unwind_info, bool)+/**+ * \brief Configures whether compiled code is compatible with a Wasmtime runtime+ * built with AddressSanitizer.+ *+ * This option defaults to whether this Wasmtime was itself built with+ * AddressSanitizer. Compiled artifacts can only be deserialized by an engine+ * with the same setting.+ *+ * For more information see the Rust documentation at+ * https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.asan_compatible_code+ */+WASMTIME_CONFIG_PROP(void, asan_compatible_code, bool)+#ifdef WASMTIME_FEATURE_CACHE/**diff --git a/crates/c-api/include/wasmtime/config.hh b/crates/c-api/include/wasmtime/config.hhindex 81177b282d..3d453b4fa7 100644--- a/crates/c-api/include/wasmtime/config.hh+++ b/crates/c-api/include/wasmtime/config.hh@@ -635,6 +635,14 @@ class Config {wasmtime_config_native_unwind_info_set(ptr.get(), enable);}+ /// \brief Configures whether compiled code is compatible with a Wasmtime+ /// runtime built with AddressSanitizer.+ ///+ /// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.asan_compatible_code+ void asan_compatible_code(bool enable) {+ wasmtime_config_asan_compatible_code_set(ptr.get(), enable);+ }+/// \brief Configures whether mach ports are used on macOS////// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.macos_use_mach_portsdiff --git a/crates/c-api/src/config.rs b/crates/c-api/src/config.rsindex c889486366..fca6549f7d 100644--- a/crates/c-api/src/config.rs+++ b/crates/c-api/src/config.rs@@ -298,6 +298,11 @@ pub extern "C" fn wasmtime_config_native_unwind_info_set(c: &mut wasm_config_t,c.config.native_unwind_info(enabled);}+#[unsafe(no_mangle)]+pub extern "C" fn wasmtime_config_asan_compatible_code_set(c: &mut wasm_config_t, enabled: bool) {+ c.config.asan_compatible_code(enabled);+}+#[unsafe(no_mangle)]pub unsafe extern "C" fn wasmtime_config_target_set(c: &mut wasm_config_t,diff --git a/crates/c-api/tests/config.cc b/crates/c-api/tests/config.ccindex 6a36489a7b..3aaf9593f1 100644--- a/crates/c-api/tests/config.cc+++ b/crates/c-api/tests/config.cc@@ -67,6 +67,7 @@ TEST(Config, Smoke) {config.memory_may_move(false);config.memory_init_cow(false);config.native_unwind_info(false);+ config.asan_compatible_code(false);config.macos_use_mach_ports(false);config.cranelift_flag_enable("foo");config.cranelift_flag_set("foo", "bar");diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rsindex ac681f55f7..ac0ebf803e 100644--- a/crates/cli-flags/src/lib.rs+++ b/crates/cli-flags/src/lib.rs@@ -272,6 +272,10 @@ wasmtime_option_group! {/// Controls whether native unwind information is present in compiled/// object files.pub native_unwind_info: Option<bool>,+ /// Whether compiled code includes the hooks needed to run in a+ /// Wasmtime built with AddressSanitizer (defaults to whether this+ /// Wasmtime was built with AddressSanitizer).+ pub asan_compatible_code: Option<bool>,/// Whether to perform function inlining during compilation.#[serde(default)]@@ -1297,6 +1301,9 @@ impl CommonOptions {if let Some(enable) = self.codegen.native_unwind_info {config.native_unwind_info(enable);}+ if let Some(enable) = self.codegen.asan_compatible_code {+ config.asan_compatible_code(enable);+ }if let Some(enable) = self.codegen.inlining {config.compiler_inlining(enable);}@@ -1643,6 +1650,7 @@ impl CommonOptions {cranelift_debug_verifier: engine.get_cranelift_debug_verifier(),inlining: Some(engine.get_compiler_inlining()),native_unwind_info: engine.get_native_unwind_info(),+ asan_compatible_code: Some(engine.get_asan_compatible_code()),parallel_compilation: Some(engine.get_parallel_compilation()),metadata_for_internal_asserts: Some(engine.get_metadata_for_internal_asserts()),metadata_for_gc_heap_corruption: Some(engine.get_metadata_for_gc_heap_corruption()),diff --git a/crates/environ/src/tunables.rs b/crates/environ/src/tunables.rsindex 20ee50e19f..7ae58ed680 100644--- a/crates/environ/src/tunables.rs+++ b/crates/environ/src/tunables.rs@@ -165,10 +165,11 @@ define_tunables! {/// Whether compiled stack-switching instructions must emit the/// AddressSanitizer fiber-switch handshake expected by this runtime.///- /// This setting is derived from how Wasmtime was built. It- /// ensures that the compiled artifact and runtime are ABI- /// compatible, e.g. it prevents running a non-ASan artifact- /// on an ASan-enabled runtime.+ /// This setting defaults to how Wasmtime was built (`cfg(asan)`)+ /// and can be overridden with `Config::asan_compatible_code`. It is+ /// checked when deserializing artifacts so that the compiled+ /// artifact and runtime agree, e.g. it prevents running a non-ASan+ /// artifact on an ASan-enabled runtime.pub asan_stack_switching: bool,/// An allocation counter that triggers GC when it reaches zero.diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rsindex aec4cbade8..92fa6a98cf 100644--- a/crates/wasmtime/src/config.rs+++ b/crates/wasmtime/src/config.rs@@ -2842,7 +2842,8 @@ impl Config {// Stack switching is emitted inline in compiled Wasm. In// ASan-enabled builds the compiler must arrange the// corresponding fiber switch handshake around every such- // instruction.+ // instruction. This defaults to how Wasmtime itself was built+ // but can be overridden with `Config::asan_compatible_code`.tunables.asan_stack_switching = cfg!(asan);// By default this is enabled with the Cargo feature, and if the feature@@ -3620,6 +3621,36 @@ impl Config {self}+ /// Configures whether compiled code is compatible with a Wasmtime runtime+ /// built with AddressSanitizer (ASan) enabled.+ ///+ /// Some code Wasmtime generates must cooperate with ASan's bookkeeping+ /// when it runs inside an ASan-instrumented host. For example,+ /// stack-switching instructions must tell ASan about every switch between+ /// stacks. When this option is enabled the generated code includes those+ /// hooks. A runtime built without ASan treats them as no-ops.+ ///+ /// This option is part of a compiled artifact's compatibility key.+ /// Artifacts produced by [`Module::serialize`] or+ /// [`Component::serialize`] can only be deserialized by an [`Engine`]+ /// with the same setting. Code compiled without this option must not run+ /// in an ASan-enabled runtime.+ ///+ /// This option is useful when compiling code outside of ASan, for+ /// example with `Engine::precompile_module`, that will later run in a+ /// Wasmtime built with `-Zsanitizer=address`.+ ///+ /// This option defaults to `true` if Wasmtime itself was built with ASan+ /// and `false` otherwise.+ ///+ /// [`Engine`]: crate::Engine+ /// [`Module::serialize`]: crate::Module::serialize+ /// [`Component::serialize`]: crate::component::Component::serialize+ pub fn asan_compatible_code(&mut self, enable: bool) -> &mut Self {+ self.tunables.asan_stack_switching = Some(enable);+ self+ }+/// Validate if the current configuration has conflicting overrides that prevent/// execution determinism. Returns an error if a conflict exists.///@@ -5271,6 +5302,11 @@ impl Engine {self.tunables().concurrency_support}+ /// Returns the configured [`Config::asan_compatible_code`] value.+ pub fn get_asan_compatible_code(&self) -> bool {+ self.tunables().asan_stack_switching+ }+/// Returns the configured [`Config::epoch_interruption`] value.pub fn get_epoch_interruption(&self) -> bool {self.tunables().epoch_interruptiondiff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rsindex 18ea398649..b6fad7b614 100644--- a/crates/wasmtime/src/engine.rs+++ b/crates/wasmtime/src/engine.rs@@ -437,6 +437,12 @@ information about this check\.into());}+ // Use of asan compatible codegen required in an asan build, optional+ // otherwise.+ if cfg!(asan) && !self.tunables().asan_stack_switching {+ return Err("asan enabled at compile time: asan stack switching required".into());+ }+Ok(())}