MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / annotate_asm

Function annotate_asm

crates/explorer/src/lib.rs:144–251  ·  view source on GitHub ↗
(
    config: &wasmtime::Config,
    target: &target_lexicon::Triple,
    wasm: &[u8],
)

Source from the content-addressed store, hash-verified

142}
143
144fn annotate_asm(
145 config: &wasmtime::Config,
146 target: &target_lexicon::Triple,
147 wasm: &[u8],
148) -> Result<AnnotatedAsm> {
149 let engine = wasmtime::Engine::new(config)?;
150 let module = wasmtime::Module::new(&engine, wasm)?;
151
152 let text = module.text();
153 let address_map: Vec<_> = module
154 .address_map()
155 .ok_or_else(|| wasmtime::format_err!("address maps must be enabled in the config"))?
156 .collect();
157
158 let mut address_map_iter = address_map.into_iter().peekable();
159 let mut current_entry = address_map_iter.next();
160 let mut wasm_offset_for_address = |start: usize, address: u32| -> Option<WasmOffset> {
161 // Consume any entries that happened before the current function for the
162 // first instruction.
163 while current_entry.map_or(false, |cur| cur.0 < start) {
164 current_entry = address_map_iter.next();
165 }
166
167 // Next advance the address map up to the current `address` specified,
168 // including it.
169 while address_map_iter.peek().map_or(false, |next_entry| {
170 u32::try_from(next_entry.0).unwrap() <= address
171 }) {
172 current_entry = address_map_iter.next();
173 }
174 current_entry.and_then(|entry| entry.1.map(WasmOffset))
175 };
176
177 let functions = module
178 .functions()
179 .map(|function| {
180 let body = &text[function.offset..][..function.len];
181
182 let mut cs = match target.architecture {
183 target_lexicon::Architecture::Aarch64(_) => capstone::Capstone::new()
184 .arm64()
185 .mode(capstone::arch::arm64::ArchMode::Arm)
186 .build()
187 .map_err(|e| wasmtime::format_err!("{e}"))?,
188 target_lexicon::Architecture::Riscv64(_) => capstone::Capstone::new()
189 .riscv()
190 .mode(capstone::arch::riscv::ArchMode::RiscV64)
191 .build()
192 .map_err(|e| wasmtime::format_err!("{e}"))?,
193 target_lexicon::Architecture::S390x => capstone::Capstone::new()
194 .sysz()
195 .mode(capstone::arch::sysz::ArchMode::Default)
196 .build()
197 .map_err(|e| wasmtime::format_err!("{e}"))?,
198 target_lexicon::Architecture::X86_64 => capstone::Capstone::new()
199 .x86()
200 .mode(capstone::arch::x86::ArchMode::Mode64)
201 .build()

Callers 1

generateFunction · 0.85

Calls 15

OkFunction · 0.85
demangle_function_nameFunction · 0.85
collectMethod · 0.80
op_strMethod · 0.80
is_okMethod · 0.80
newFunction · 0.50
textMethod · 0.45
address_mapMethod · 0.45
into_iterMethod · 0.45
nextMethod · 0.45
peekMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected