def __init__(self, module_bytecode): self.module_bytecode = bytecode_to_bytes(module_bytecode) self.functions = list() self.basicblocks = list() self.edges = list() self.analyzer = WasmModuleAnalyzer(self.module_bytecode) self.run_static_analysis()
def disassemble_module(self, module_bytecode=None, offset=0, r_format='list'): bytecode = bytecode_to_bytes(module_bytecode) functions = self.extract_functions_code(bytecode[offset:]) self.instructions = [f.instructions for f in functions] # return instructions if r_format == 'list': return self.instructions elif r_format == 'text': text = '' for index, func in enumerate(functions): text += ('func %d\n' % index) text += ('\n'.join(map(str, func.instructions))) text += ('\n\n') return text
def disassemble(self, bytecode=None, offset=0, r_format='list'): """Generic method to disassemble bytecode :param bytecode: bytecode sequence :param offset: start offset :param r_format: output format ('list'/'text'/'reverse') :type bytecode: bytes, str :type offset: int :type r_format: list, str, dict :return: dissassembly result depending of r_format :rtype: list, str, dict """ # reinitialize class variable self.attributes_reset() self.bytecode = bytecode if bytecode else self.bytecode if not self.bytecode: raise BytecodeEmptyException() self.bytecode = bytecode_to_bytes(self.bytecode) while offset < len(self.bytecode): instr = self.disassemble_opcode(self.bytecode[offset:], offset) offset += instr.size self.instructions.append(instr) # fill reverse instructions self.reverse_instructions = { k: v for k, v in enumerate(self.instructions) } # return instructions if r_format == 'list': return self.instructions elif r_format == 'text': return '\n'.join(map(str, self.instructions)) elif r_format == 'reverse': return self.reverse_instructions
def __init__(self, module_bytecode, analysis=True): self.module_bytecode = bytecode_to_bytes(module_bytecode) self.magic = None self.version = None self.types = list() self.imports_all = list() self.imports_func = list() self.func_types = list() self.tables = list() self.memories = list() self.globals = list() self.exports = list() self.start = None self.elements = list() self.codes = list() self.datas = list() self.names = list() self.customs = list() self.func_prototypes = list() # self.strings = list() - TODO if analysis: self.analyze()