Package: wasm-tob
wasm-tobPython module capable of decoding and disassembling WebAssembly modules
and bytecode, according to the MVP specification of the WASM binary
format.As there is no official text format defined yet, the text format
implemented doesn't correspond to any existing definition and is a
simplemnemonic op1, op2, ...format. Functions are formatted in a
way similar to how Google Chrome does in the debug console.❗ ImportantThis is a fork of the original project that the author was no longer able to
spend time on:https://github.com/athre0z/wasm. The changes made here are
primarily to support theManticoreproject.New issues and pull requests will be reviewed on a best-effort basis. Please
open an issue first if you think fixing the problem will be complex; this is so
we can evaluate whether a fix or feature is in scope before comitting time to
review. When opening an issue, please include information on how to reproduce
what you are seeing. If you feel comfortable, please submit a well-crafted,
minimal pull request that we can review.Installation# From PyPipipinstallwasm-tob# From GitHubpipinstallgit+https://github.com/trail-of-forks/wasm-tob.gitExamplesParsing a WASM module, printing the types of sections found.fromwasm_tobimportdecode_modulewithopen('input-samples/hello/hello.wasm','rb')asraw:raw=raw.read()mod_iter=iter(decode_module(raw))header,header_data=next(mod_iter)forcur_sec,cur_sec_datainmod_iter:print(cur_sec_data.get_decoder_meta()['types']['payload'])Possible output:<wasm_tob.modtypes.TypeSection object at 0x108249b90>
<wasm_tob.modtypes.ImportSection object at 0x108249bd0>
<wasm_tob.modtypes.FunctionSection object at 0x108249c10>
<wasm_tob.modtypes.GlobalSection object at 0x108249cd0>
<wasm_tob.modtypes.ExportSection object at 0x108249d10>
<wasm_tob.modtypes.ElementSection object at 0x108249d90>
<wasm_tob.modtypes.CodeSection object at 0x108249dd0>
<wasm_tob.modtypes.DataSection object at 0x108249e10>
<wasm_tob.types.BytesField object at 0x108249b10>Parsing specific sections (eg. GlobalSection, ElementSection, DataSection) in WASM module, printing each section's content:fromwasm_tobimport(decode_module,format_instruction,format_lang_type,format_mutability,SEC_DATA,SEC_ELEMENT,SEC_GLOBAL,)withopen('input-samples/hello/hello.wasm','rb')asraw:raw=raw.read()mod_iter=iter(decode_module(raw))header,header_data=next(mod_iter)forcur_sec,cur_sec_datainmod_iter:ifcur_sec_data.id==SEC_GLOBAL:print("GlobalSection:")foridx,entryinenumerate(cur_sec_data.payload.globals):print(format_mutability(entry.type.mutability),format_lang_type(entry.type.content_type),)forcur_insninentry.init:print(format_instruction(cur_insn))ifcur_sec_data.id==SEC_ELEMENT:print("ElementSection:")foridx,entryinenumerate(cur_sec_data.payload.entries):print(entry.index,entry.num_elem,entry.elems)forcur_insninentry.offset:print(format_instruction(cur_insn))ifcur_sec_data.id==SEC_DATA:print("DataSection:")foridx,entryinenumerate(cur_sec_data.payload.entries):print(entry.index,entry.size,entry.data.tobytes())forcur_insninentry.offset:print(format_instruction(cur_insn))Output:GlobalSection:
mut i32
get_global 0
end
mut i32
get_global 1
end
[...]
mut f32
f32.const 0x0
end
mut f32
f32.const 0x0
end
ElementSection:
0 12576 [856, 856, 856, [...], 888]
i32.const 0
end
DataSection:
0 16256 b'\x98&\x00\x00\xfe4\x00\x00\x10\x04\x00\x00\x00...\x00N10__cxxabiv121__vmi_class_type_infoE'
get_global 8
endManually disassemble WASM bytecode, printing each instruction.fromwasm_tobimport(decode_bytecode,format_instruction,INSN_ENTER_BLOCK,INSN_LEAVE_BLOCK,)raw=bytearray([2,127,65,24,16,28,65,0,15,11])indent=0forcur_insnindecode_bytecode(raw):ifcur_insn.op.flags&INSN_LEAVE_BLOCK:indent-=1print('  '*indent+format_instruction(cur_insn))ifcur_insn.op.flags&INSN_ENTER_BLOCK:indent+=1Output:block -1
  i32.const 24
  call 28
  i32.const 0
  return
endwasmdumpcommand-line toolThe module also comes with a simple command-line tool calledwasmdump,
dumping all module struct in tree format. Optionally, it also
disassembles all functions found when invoked with--disas(slow).Version supportThis project aims to support all Python releases that are still actively
supported and maintained. If you encounter issues with a particular Python
version, please open an issue.