Improve handling of strings for backtraces (#843)

* Improve handling of strings for backtraces

Largely avoid storing strings at all in the `wasmtime-*` internal
crates, and instead only store strings in a separate global cache
specific to the `wasmtime` crate itself. This global cache is inserted
and removed from dynamically as modules are created and deallocated, and
the global cache is consulted whenever a `Trap` is created to
symbolicate any wasm frames.

This also avoids the need to thread `module_name` through the jit crates
and back, and additionally removes the need for `ModuleSyncString`.

* Run rustfmt
This commit is contained in:
Alex Crichton
2020-01-24 11:53:55 -06:00
committed by GitHub
parent 21e0a99884
commit 3db1074c15
17 changed files with 139 additions and 156 deletions

View File

@@ -3,15 +3,16 @@
use crate::module_environ::FunctionBodyData;
use crate::tunables::Tunables;
use cranelift_codegen::ir;
use cranelift_entity::{EntityRef, PrimaryMap, SecondaryMap};
use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::{
DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, Global,
GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
};
use indexmap::IndexMap;
use more_asserts::assert_ge;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
/// A WebAssembly table initializer.
#[derive(Clone, Debug, Hash)]
@@ -129,33 +130,14 @@ impl TablePlan {
}
}
/// Allows module strings to be cached as reused across
/// multiple threads. Useful for debug/trace information.
#[derive(Debug, Clone)]
pub struct ModuleSyncString(Option<Arc<String>>);
impl ModuleSyncString {
/// Gets optional string reference.
pub fn get(&self) -> Option<&str> {
self.0.as_deref().map(|s| s.as_str())
}
/// Constructs the string.
pub fn new(s: Option<&str>) -> Self {
ModuleSyncString(s.map(|s| Arc::new(s.to_string())))
}
}
impl Default for ModuleSyncString {
fn default() -> Self {
ModuleSyncString(None)
}
}
/// A translated WebAssembly module, excluding the function bodies and
/// memory initializers.
// WARNING: when modifying, make sure that `hash_for_cache` is still valid!
#[derive(Debug)]
pub struct Module {
/// A unique identifier (within this process) for this module.
pub id: usize,
/// Unprocessed signatures exactly as provided by `declare_signature()`.
pub signatures: PrimaryMap<SignatureIndex, ir::Signature>,
@@ -193,17 +175,17 @@ pub struct Module {
/// WebAssembly table initializers.
pub table_elements: Vec<TableElements>,
/// Module name.
pub name: ModuleSyncString,
/// Function names.
pub func_names: SecondaryMap<FuncIndex, ModuleSyncString>,
/// WebAssembly table initializers.
pub func_names: HashMap<FuncIndex, String>,
}
impl Module {
/// Allocates the module data structures.
pub fn new() -> Self {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
Self {
id: NEXT_ID.fetch_add(1, SeqCst),
signatures: PrimaryMap::new(),
imported_funcs: PrimaryMap::new(),
imported_tables: PrimaryMap::new(),
@@ -216,8 +198,7 @@ impl Module {
exports: IndexMap::new(),
start_func: None,
table_elements: Vec::new(),
name: Default::default(),
func_names: SecondaryMap::new(),
func_names: HashMap::new(),
}
}