usecrate::{CustomSection, Encode, Ieee32, Ieee64, Section}; use alloc::borrow::Cow; use alloc::string::String; use alloc::vec; use alloc::vec::Vec;
/// The "core" custom section for coredumps, as described in the /// [tool-conventions /// repository](https://github.com/WebAssembly/tool-conventions/blob/main/Coredump.md). /// /// There are four sections that comprise a core dump: /// - "core", which contains the name of the core dump /// - "coremodules", a listing of modules /// - "coreinstances", a listing of module instances /// - "corestack", a listing of frames for a specific thread /// /// # Example of how these could be constructed and encoded into a module: /// /// ``` /// use wasm_encoder::{ /// CoreDumpInstancesSection, CoreDumpModulesSection, CoreDumpSection, CoreDumpStackSection, /// CoreDumpValue, Module, /// }; /// let core = CoreDumpSection::new("MyModule.wasm"); /// /// let mut modules = CoreDumpModulesSection::new(); /// modules.module("my_module"); /// /// let mut instances = CoreDumpInstancesSection::new(); /// let module_idx = 0; /// let memories = vec![1]; /// let globals = vec![2]; /// instances.instance(module_idx, memories, globals); /// /// let mut thread = CoreDumpStackSection::new("main"); /// let instance_index = 0; /// let func_index = 42; /// let code_offset = 0x1234; /// let locals = vec![CoreDumpValue::I32(1)]; /// let stack = vec![CoreDumpValue::I32(2)]; /// thread.frame(instance_index, func_index, code_offset, locals, stack); /// /// let mut module = Module::new(); /// module.section(&core); /// module.section(&modules); /// module.section(&instances); /// module.section(&thread); /// ``` #[derive(Clone, Debug, Default)] pubstruct CoreDumpSection {
name: String,
}
impl CoreDumpSection { /// Create a new core dump section encoder pubfn new(name: impl Into<String>) -> Self { let name = name.into();
CoreDumpSection { name }
}
/// View the encoded section as a CustomSection. fn as_custom<'a>(&'a self) -> CustomSection<'a> { letmut data = vec![0]; self.name.encode(&mut data);
CustomSection {
name: "core".into(),
data: Cow::Owned(data),
}
}
}
impl Section for CoreDumpStackSection { fn id(&self) -> u8 { crate::core::SectionId::Custom as u8
}
}
/// Local and stack values are encoded using one byte for the type (similar to /// Wasm's Number Types) followed by bytes representing the actual value /// See the tool-conventions repo for more details. #[derive(Clone, Debug)] pubenum CoreDumpValue { /// a missing value (usually missing because it was optimized out)
Missing, /// An i32 value
I32(i32), /// An i64 value
I64(i64), /// An f32 value
F32(Ieee32), /// An f64 value
F64(Ieee64),
}
#[cfg(test)] mod tests { usesuper::*; usecrate::Module; use wasmparser::{KnownCustom, Parser, Payload};
// Create new core dump section and test whether it is properly encoded and // parsed back out by wasmparser #[test] fn test_roundtrip_core() { let core = CoreDumpSection::new("test.wasm"); letmut module = Module::new();
module.section(&core);
let payload = parser
.next()
.expect("parser is not empty")
.expect("element is a payload"); match payload {
Payload::CustomSection(section) => {
assert_eq!(section.name(), "coreinstances"); let coreinstances = match section.as_known() {
KnownCustom::CoreDumpInstances(s) => s,
_ => panic!("not coreinstances"),
};
assert_eq!(coreinstances.instances.len(), 1); let instance = coreinstances
.instances
.first()
.expect("instance is encoded");
assert_eq!(instance.module_index, 0);
assert_eq!(instance.memories.len(), 1);
assert_eq!(instance.globals.len(), 1);
}
_ => panic!("unexpected payload"),
}
}
// Create new corestack section and test whether it is properly encoded and // parsed back out by wasmparser #[test] fn test_roundtrip_corestack() { letmut corestack = CoreDumpStackSection::new("main");
corestack.frame( 0, 12, 0,
vec![CoreDumpValue::I32(10)],
vec![CoreDumpValue::I32(42)],
); letmut module = Module::new();
module.section(&corestack); let wasm_bytes = module.finish();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.