Improve robustness of cache loading/storing (#974)
* Improve robustness of cache loading/storing
Today wasmtime incorrectly loads compiled compiled modules from the
global cache when toggling settings such as optimizations. For example
if you execute `wasmtime foo.wasm` that will cache globally an
unoptimized version of the wasm module. If you then execute `wasmtime -O
foo.wasm` it would then reload the unoptimized version from cache, not
realizing the compilation settings were different, and use that instead.
This can lead to very surprising behavior naturally!
This commit updates how the cache is managed in an attempt to make it
much more robust against these sorts of issues. This takes a leaf out of
rustc's playbook and models the cache with a function that looks like:
fn load<T: Hash>(
&self,
data: T,
compute: fn(T) -> CacheEntry,
) -> CacheEntry;
The goal here is that it guarantees that all the `data` necessary to
`compute` the result of the cache entry is hashable and stored into the
hash key entry. This was previously open-coded and manually managed
where items were hashed explicitly, but this construction guarantees
that everything reasonable `compute` could use to compile the module is
stored in `data`, which is itself hashable.
This refactoring then resulted in a few workarounds and a few fixes,
including the original issue:
* The `Module` type was split into `Module` and `ModuleLocal` where only
the latter is hashed. The previous hash function for a `Module` left
out items like the `start_func` and didn't hash items like the imports
of the module. Omitting the `start_func` was fine since compilation
didn't actually use it, but omitting imports seemed uncomfortable
because while compilation didn't use the import values it did use the
*number* of imports, which seems like it should then be put into the
cache key. The `ModuleLocal` type now derives `Hash` to guarantee that
all of its contents affect the hash key.
* The `ModuleTranslationState` from `cranelift-wasm` doesn't implement
`Hash` which means that we have a manual wrapper to work around that.
This will be fixed with an upstream implementation, since this state
affects the generated wasm code. Currently this is just a map of
signatures, which is present in `Module` anyway, so we should be good
for the time being.
* Hashing `dyn TargetIsa` was also added, where previously it was not
fully hashed. Previously only the target name was used as part of the
cache key, but crucially the flags of compilation were omitted (for
example the optimization flags). Unfortunately the trait object itself
is not hashable so we still have to manually write a wrapper to hash
it, but we likely want to add upstream some utilities to hash isa
objects into cranelift itself. For now though we can continue to add
hashed fields as necessary.
Overall the goal here was to use the compiler to expose what we're not
hashing, and then make sure we organize data and write the right code to
ensure everything is hashed, and nothing more.
* Update crates/environ/src/module.rs
Co-Authored-By: Peter Huene <peterhuene@protonmail.com>
* Fix lightbeam
* Fix compilation of tests
* Update the expected structure of the cache
* Revert "Update the expected structure of the cache"
This reverts commit 2b53fee426a4e411c313d8c1e424841ba304a9cd.
* Separate the cache dir a bit
* Add a test the cache is busted with opt levels
* rustfmt
Co-authored-by: Peter Huene <peterhuene@protonmail.com>
This commit is contained in:
@@ -34,6 +34,7 @@ pretty_env_logger = "0.3.0"
|
||||
rayon = "1.2.1"
|
||||
file-per-thread-logger = "0.1.1"
|
||||
wat = "1.0"
|
||||
tempfile = "3.1"
|
||||
|
||||
[badges]
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
@@ -69,7 +69,7 @@ impl GlobalFrameInfo {
|
||||
if end > max {
|
||||
max = end;
|
||||
}
|
||||
let func_index = module.module().func_index(i);
|
||||
let func_index = module.module().local.func_index(i);
|
||||
assert!(functions.insert(end, (start, func_index)).is_none());
|
||||
}
|
||||
if functions.len() == 0 {
|
||||
|
||||
@@ -507,3 +507,71 @@ fn _assert_send_sync() {
|
||||
_assert::<Engine>();
|
||||
_assert::<Config>();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Module;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn cache_accounts_for_opt_level() -> Result<()> {
|
||||
let td = TempDir::new()?;
|
||||
let config_path = td.path().join("config.toml");
|
||||
std::fs::write(
|
||||
&config_path,
|
||||
&format!(
|
||||
"
|
||||
[cache]
|
||||
enabled = true
|
||||
directory = '{}'
|
||||
",
|
||||
td.path().join("cache").display()
|
||||
),
|
||||
)?;
|
||||
let mut cfg = Config::new();
|
||||
cfg.cranelift_opt_level(OptLevel::None)
|
||||
.cache_config_load(&config_path)?;
|
||||
let store = Store::new(&Engine::new(&cfg));
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 0);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 1);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
|
||||
let mut cfg = Config::new();
|
||||
cfg.cranelift_opt_level(OptLevel::Speed)
|
||||
.cache_config_load(&config_path)?;
|
||||
let store = Store::new(&Engine::new(&cfg));
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 0);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 1);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
|
||||
let mut cfg = Config::new();
|
||||
cfg.cranelift_opt_level(OptLevel::SpeedAndSize)
|
||||
.cache_config_load(&config_path)?;
|
||||
let store = Store::new(&Engine::new(&cfg));
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 0);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 1);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
|
||||
let mut cfg = Config::new();
|
||||
cfg.debug_info(true).cache_config_load(&config_path)?;
|
||||
let store = Store::new(&Engine::new(&cfg));
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 0);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
Module::new(&store, "(module (func))")?;
|
||||
assert_eq!(store.engine().config.cache_config.cache_hits(), 1);
|
||||
assert_eq!(store.engine().config.cache_config.cache_misses(), 1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ pub(crate) fn create_handle(
|
||||
|
||||
// Compute indices into the shared signature table.
|
||||
let signatures = module
|
||||
.local
|
||||
.signatures
|
||||
.values()
|
||||
.map(|sig| store.compiler().signatures().register(sig))
|
||||
|
||||
@@ -81,7 +81,8 @@ unsafe extern "C" fn stub_fn(
|
||||
|
||||
let (args, returns_len) = {
|
||||
let module = instance.module_ref();
|
||||
let signature = &module.signatures[module.functions[FuncIndex::new(call_id as usize)]];
|
||||
let signature =
|
||||
&module.local.signatures[module.local.functions[FuncIndex::new(call_id as usize)]];
|
||||
|
||||
let mut args = Vec::new();
|
||||
for i in 2..signature.params.len() {
|
||||
@@ -101,7 +102,8 @@ unsafe extern "C" fn stub_fn(
|
||||
state.func.call(&args, &mut returns)?;
|
||||
|
||||
let module = instance.module_ref();
|
||||
let signature = &module.signatures[module.functions[FuncIndex::new(call_id as usize)]];
|
||||
let signature =
|
||||
&module.local.signatures[module.local.functions[FuncIndex::new(call_id as usize)]];
|
||||
for (i, ret) in returns.iter_mut().enumerate() {
|
||||
if ret.ty().get_wasmtime_type() != Some(signature.returns[i].value_type) {
|
||||
return Err(Trap::new(
|
||||
@@ -266,8 +268,8 @@ pub fn create_handle_with_function(
|
||||
PrimaryMap::new();
|
||||
let mut code_memory = CodeMemory::new();
|
||||
|
||||
let sig_id = module.signatures.push(sig.clone());
|
||||
let func_id = module.functions.push(sig_id);
|
||||
let sig_id = module.local.signatures.push(sig.clone());
|
||||
let func_id = module.local.functions.push(sig_id);
|
||||
module
|
||||
.exports
|
||||
.insert("trampoline".to_string(), Export::Function(func_id));
|
||||
@@ -313,8 +315,8 @@ pub unsafe fn create_handle_with_raw_function(
|
||||
let mut module = Module::new();
|
||||
let mut finished_functions = PrimaryMap::new();
|
||||
|
||||
let sig_id = module.signatures.push(sig.clone());
|
||||
let func_id = module.functions.push(sig_id);
|
||||
let sig_id = module.local.signatures.push(sig.clone());
|
||||
let func_id = module.local.functions.push(sig_id);
|
||||
module
|
||||
.exports
|
||||
.insert("trampoline".to_string(), Export::Function(func_id));
|
||||
|
||||
@@ -25,7 +25,7 @@ pub fn create_global(store: &Store, gt: &GlobalType, val: Val) -> Result<Instanc
|
||||
},
|
||||
};
|
||||
let mut module = Module::new();
|
||||
let global_id = module.globals.push(global);
|
||||
let global_id = module.local.globals.push(global);
|
||||
module.exports.insert(
|
||||
"global".to_string(),
|
||||
wasmtime_environ::Export::Global(global_id),
|
||||
|
||||
@@ -17,7 +17,7 @@ pub fn create_handle_with_memory(store: &Store, memory: &MemoryType) -> Result<I
|
||||
let tunable = Default::default();
|
||||
|
||||
let memory_plan = wasmtime_environ::MemoryPlan::for_memory(memory, &tunable);
|
||||
let memory_id = module.memory_plans.push(memory_plan);
|
||||
let memory_id = module.local.memory_plans.push(memory_plan);
|
||||
module.exports.insert(
|
||||
"memory".to_string(),
|
||||
wasmtime_environ::Export::Memory(memory_id),
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn create_handle_with_table(store: &Store, table: &TableType) -> Result<Inst
|
||||
let tunable = Default::default();
|
||||
|
||||
let table_plan = wasmtime_environ::TablePlan::for_table(table, &tunable);
|
||||
let table_id = module.table_plans.push(table_plan);
|
||||
let table_id = module.local.table_plans.push(table_plan);
|
||||
module.exports.insert(
|
||||
"table".to_string(),
|
||||
wasmtime_environ::Export::Table(table_id),
|
||||
|
||||
Reference in New Issue
Block a user