Skip to content

Commit

Permalink
Enable warnings if cranelift is disabled
Browse files Browse the repository at this point in the history
Continuation of work in bytecodealliance#10131.
  • Loading branch information
alexcrichton committed Jan 30, 2025
1 parent a727985 commit f41c7ac
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 75 deletions.
2 changes: 0 additions & 2 deletions crates/environ/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ macro_rules! declare_builtin_index_constructors {
/// Returns a symbol name for this builtin.
pub fn name(&self) -> &'static str {
$(
$( #[$attr] )*
if *self == Self::$name() {
return stringify!($name);
}
Expand Down Expand Up @@ -286,7 +285,6 @@ macro_rules! declare_builtin_index_constructors {
$rest_name:ident;
)*
) => {
$( #[$this_attr] )*
#[allow(missing_docs, reason = "macro-generated")]
pub const fn $this_name() -> Self {
Self($index)
Expand Down
1 change: 1 addition & 0 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ std = [
'wasmtime-fiber?/std',
'pulley-interpreter/std',
'wasmtime-math/std',
'addr2line?/std',
]

# Enables support for the `Store::call_hook` API which enables injecting custom
Expand Down
13 changes: 6 additions & 7 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::hash_map::HashMap;
use crate::hash_set::HashSet;
use crate::prelude::*;
use alloc::sync::Arc;
use bitflags::Flags;
Expand Down Expand Up @@ -172,8 +170,8 @@ pub struct Config {
#[derive(Debug, Clone)]
struct CompilerConfig {
strategy: Option<Strategy>,
settings: HashMap<String, String>,
flags: HashSet<String>,
settings: crate::hash_map::HashMap<String, String>,
flags: crate::hash_set::HashSet<String>,
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
cache_store: Option<Arc<dyn CacheStore>>,
clif_dir: Option<std::path::PathBuf>,
Expand All @@ -185,8 +183,8 @@ impl CompilerConfig {
fn new() -> Self {
Self {
strategy: Strategy::Auto.not_auto(),
settings: HashMap::new(),
flags: HashSet::new(),
settings: Default::default(),
flags: Default::default(),
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
cache_store: None,
clif_dir: None,
Expand Down Expand Up @@ -2433,7 +2431,7 @@ impl Config {
compiler.enable(flag)?;
}

#[cfg(feature = "incremental-cache")]
#[cfg(all(feature = "incremental-cache", feature = "cranelift"))]
if let Some(cache_store) = &self.compiler_config.cache_store {
compiler.enable_incremental_compilation(cache_store.clone())?;
}
Expand Down Expand Up @@ -2629,6 +2627,7 @@ pub enum Strategy {
Winch,
}

#[cfg(any(feature = "winch", feature = "cranelift"))]
impl Strategy {
fn not_auto(&self) -> Option<Strategy> {
match self {
Expand Down
103 changes: 46 additions & 57 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ pub use crate::runtime::code_memory::CustomCodeMemory;
use crate::runtime::type_registry::TypeRegistry;
#[cfg(feature = "runtime")]
use crate::runtime::vm::GcRuntime;
use crate::sync::OnceLock;
use crate::Config;
use alloc::sync::Arc;
#[cfg(target_has_atomic = "64")]
use core::sync::atomic::{AtomicU64, Ordering};
#[cfg(any(feature = "cranelift", feature = "winch"))]
use object::write::{Object, StandardSegment};
use object::SectionKind;
#[cfg(feature = "std")]
use std::{fs::File, path::Path};
use wasmparser::WasmFeatures;
use wasmtime_environ::obj;
use wasmtime_environ::{FlagValue, ObjectKind, TripleExt, Tunables};

mod serialization;
Expand Down Expand Up @@ -68,7 +65,7 @@ struct EngineInner {
/// One-time check of whether the compiler's settings, if present, are
/// compatible with the native host.
#[cfg(any(feature = "cranelift", feature = "winch"))]
compatible_with_native_host: OnceLock<Result<(), String>>,
compatible_with_native_host: crate::sync::OnceLock<Result<(), String>>,
}

impl core::fmt::Debug for Engine {
Expand Down Expand Up @@ -134,7 +131,7 @@ impl Engine {
#[cfg(all(feature = "runtime", target_has_atomic = "64"))]
epoch: AtomicU64::new(0),
#[cfg(any(feature = "cranelift", feature = "winch"))]
compatible_with_native_host: OnceLock::new(),
compatible_with_native_host: Default::default(),
config,
tunables,
features,
Expand Down Expand Up @@ -251,64 +248,56 @@ impl Engine {
/// engine can indeed load modules for the configured compiler (if any).
/// Note that if cranelift is disabled this trivially returns `Ok` because
/// loaded serialized modules are checked separately.
#[cfg(any(feature = "cranelift", feature = "winch"))]
pub(crate) fn check_compatible_with_native_host(&self) -> Result<()> {
#[cfg(any(feature = "cranelift", feature = "winch"))]
{
self.inner
.compatible_with_native_host
.get_or_init(|| self._check_compatible_with_native_host())
.clone()
.map_err(anyhow::Error::msg)
}
#[cfg(not(any(feature = "cranelift", feature = "winch")))]
{
Ok(())
}
self.inner
.compatible_with_native_host
.get_or_init(|| self._check_compatible_with_native_host())
.clone()
.map_err(anyhow::Error::msg)
}

#[cfg(any(feature = "cranelift", feature = "winch"))]
fn _check_compatible_with_native_host(&self) -> Result<(), String> {
#[cfg(any(feature = "cranelift", feature = "winch"))]
{
use target_lexicon::Triple;

let compiler = self.compiler();

let target = compiler.triple();
let host = Triple::host();
let target_matches_host = || {
// If the host target and target triple match, then it's valid
// to run results of compilation on this host.
if host == *target {
return true;
}

// If there's a mismatch and the target is a compatible pulley
// target, then that's also ok to run.
if cfg!(feature = "pulley")
&& target.is_pulley()
&& target.pointer_width() == host.pointer_width()
&& target.endianness() == host.endianness()
{
return true;
}
use target_lexicon::Triple;

// ... otherwise everything else is considered not a match.
false
};
let compiler = self.compiler();

if !target_matches_host() {
return Err(format!(
"target '{target}' specified in the configuration does not match the host"
));
let target = compiler.triple();
let host = Triple::host();
let target_matches_host = || {
// If the host target and target triple match, then it's valid
// to run results of compilation on this host.
if host == *target {
return true;
}

// Also double-check all compiler settings
for (key, value) in compiler.flags().iter() {
self.check_compatible_with_shared_flag(key, value)?;
}
for (key, value) in compiler.isa_flags().iter() {
self.check_compatible_with_isa_flag(key, value)?;
// If there's a mismatch and the target is a compatible pulley
// target, then that's also ok to run.
if cfg!(feature = "pulley")
&& target.is_pulley()
&& target.pointer_width() == host.pointer_width()
&& target.endianness() == host.endianness()
{
return true;
}

// ... otherwise everything else is considered not a match.
false
};

if !target_matches_host() {
return Err(format!(
"target '{target}' specified in the configuration does not match the host"
));
}

// Also double-check all compiler settings
for (key, value) in compiler.flags().iter() {
self.check_compatible_with_shared_flag(key, value)?;
}
for (key, value) in compiler.isa_flags().iter() {
self.check_compatible_with_isa_flag(key, value)?;
}

// Double-check that this configuration isn't requesting capabilities
Expand Down Expand Up @@ -622,8 +611,8 @@ impl Engine {
pub(crate) fn append_bti(&self, obj: &mut Object<'_>) {
let section = obj.add_section(
obj.segment_name(StandardSegment::Data).to_vec(),
obj::ELF_WASM_BTI.as_bytes().to_vec(),
SectionKind::ReadOnlyData,
wasmtime_environ::obj::ELF_WASM_BTI.as_bytes().to_vec(),
object::SectionKind::ReadOnlyData,
);
let contents = if self.compiler().is_branch_protection_enabled() {
1
Expand Down Expand Up @@ -682,7 +671,7 @@ impl Engine {
self.inner.profiler.as_ref()
}

#[cfg(feature = "cache")]
#[cfg(all(feature = "cache", any(feature = "cranelift", feature = "winch")))]
pub(crate) fn cache_config(&self) -> &wasmtime_cache::CacheConfig {
&self.config().cache_config
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmtime/src/engine/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::str::FromStr;
use object::endian::Endianness;
#[cfg(any(feature = "cranelift", feature = "winch"))]
use object::write::{Object, StandardSegment};
use object::{read::elf::ElfFile64, FileFlags, Object as _, ObjectSection, SectionKind};
use object::{read::elf::ElfFile64, FileFlags, Object as _, ObjectSection};
use serde_derive::{Deserialize, Serialize};
use wasmtime_environ::obj;
use wasmtime_environ::{FlagValue, ObjectKind, Tunables};
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>, metadata: &Me
let section = obj.add_section(
obj.segment_name(StandardSegment::Data).to_vec(),
obj::ELF_WASM_ENGINE.as_bytes().to_vec(),
SectionKind::ReadOnlyData,
object::SectionKind::ReadOnlyData,
);
let mut data = Vec::new();
data.push(VERSION);
Expand Down
6 changes: 1 addition & 5 deletions crates/wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@
// NB: this list is currently being burned down to remove all features listed
// here to get warnings in all configurations of Wasmtime.
#![cfg_attr(
any(
not(feature = "cranelift"),
not(feature = "runtime"),
not(feature = "std"),
),
any(not(feature = "runtime"), not(feature = "std")),
allow(dead_code, unused_imports)
)]
// Allow broken links when the default features is disabled because most of our
Expand Down
8 changes: 6 additions & 2 deletions crates/wasmtime/src/runtime/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
#[cfg(feature = "std")]
use crate::runtime::vm::open_file_for_mmap;
use crate::runtime::vm::{CompiledModuleId, MmapVec, ModuleMemoryImages, VMWasmCallFunction};
use crate::runtime::vm::{CompiledModuleId, ModuleMemoryImages, VMWasmCallFunction};
use crate::sync::OnceLock;
use crate::{
code::CodeObject,
Expand Down Expand Up @@ -153,6 +153,7 @@ struct ModuleInner {
memory_images: OnceLock<Option<ModuleMemoryImages>>,

/// Flag indicating whether this module can be serialized or not.
#[cfg(any(feature = "cranelift", feature = "winch"))]
serializable: bool,

/// Runtime offset information for `VMContext`.
Expand Down Expand Up @@ -344,7 +345,7 @@ impl Module {
#[cfg(all(feature = "std", any(feature = "cranelift", feature = "winch")))]
pub unsafe fn from_trusted_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> {
let open_file = open_file_for_mmap(file.as_ref())?;
let mmap = MmapVec::from_file(open_file)?;
let mmap = crate::runtime::vm::MmapVec::from_file(open_file)?;
if &mmap[0..4] == b"\x7fELF" {
let code = engine.load_code(mmap, ObjectKind::Module)?;
return Module::from_parts(engine, code, None);
Expand Down Expand Up @@ -510,12 +511,15 @@ impl Module {
.allocator()
.validate_module(module.module(), &offsets)?;

let _ = serializable;

Ok(Self {
inner: Arc::new(ModuleInner {
engine: engine.clone(),
code,
memory_images: OnceLock::new(),
module,
#[cfg(any(feature = "cranelift", feature = "winch"))]
serializable,
offsets,
}),
Expand Down
6 changes: 6 additions & 0 deletions crates/wasmtime/src/sync_nostd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ impl<T> OnceLock<T> {
}
}

impl<T> Default for OnceLock<T> {
fn default() -> OnceLock<T> {
OnceLock::new()
}
}

#[derive(Debug, Default)]
pub struct RwLock<T> {
val: UnsafeCell<T>,
Expand Down
6 changes: 6 additions & 0 deletions crates/wasmtime/src/sync_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl<T> OnceLock<T> {
}
}

impl<T> Default for OnceLock<T> {
fn default() -> OnceLock<T> {
OnceLock::new()
}
}

/// Small wrapper around `std::sync::RwLock` which undoes poisoning.
#[derive(Debug, Default)]
pub struct RwLock<T>(std::sync::RwLock<T>);
Expand Down

0 comments on commit f41c7ac

Please sign in to comment.