| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
from capstone import * |
| 7 |
from capstone.wasm import * |
| 8 |
|
| 9 |
WASM_CODE = b"\x20\x00\x20\x01\x41\x20\x10\xc9\x01\x45\x0b" |
| 10 |
|
| 11 |
all_tests = ( |
| 12 |
(CS_ARCH_WASM, 0, WASM_CODE, "WASM"), |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
def print_insn_detail(insn): |
| 17 |
|
| 18 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 19 |
|
| 20 |
|
| 21 |
if insn.id == 0: |
| 22 |
return |
| 23 |
|
| 24 |
if len(insn.groups) > 0: |
| 25 |
print("\tGroups: ", end="") |
| 26 |
for group in insn.groups: |
| 27 |
print("%s " % insn.group_name(group), end="") |
| 28 |
print() |
| 29 |
|
| 30 |
if len(insn.operands) > 0: |
| 31 |
print("\tOperand count: %u" % len(insn.operands)) |
| 32 |
c = 0 |
| 33 |
for i in insn.operands: |
| 34 |
if i.type == WASM_OP_INT7: |
| 35 |
print("\t\tOperand[%u] type: int7" % c) |
| 36 |
print("\t\tOperand[%u] value: %d" % (c, i.int7)) |
| 37 |
elif i.type == WASM_OP_VARUINT32: |
| 38 |
print("\t\tOperand[%u] type: varuint32" % c) |
| 39 |
print("\t\tOperand[%u] value: %#x" % (c, i.varuint32)) |
| 40 |
elif i.type == WASM_OP_VARUINT64: |
| 41 |
print("\t\tOperand[%u] type: varuint64" % c) |
| 42 |
print("\t\tOperand[%u] value: %#x" % (c, i.varuint64)) |
| 43 |
elif i.type == WASM_OP_UINT32: |
| 44 |
print("\t\tOperand[%u] type: uint32" % c) |
| 45 |
print("\t\tOperand[%u] value: %#x" % (c, i.uint32)) |
| 46 |
elif i.type == WASM_OP_UINT64: |
| 47 |
print("\t\tOperand[%u] type: uint64" % c) |
| 48 |
print("\t\tOperand[%u] value: %#x" % (c, i.uint64)) |
| 49 |
elif i.type == WASM_OP_IMM: |
| 50 |
print("\t\tOperand[%u] type: imm" % c) |
| 51 |
print("\t\tOperand[%u] value: %#x %#x" % (c, i.immediate[0], i.immediate[1])) |
| 52 |
elif i.type == WASM_OP_BRTABLE: |
| 53 |
print("\t\tOperand[%u] type: brtable" % c) |
| 54 |
print("\t\tOperand[%u] value: length=%#x, address=%#x, default_target=%#x" % (c, i.brtable.length, i.brtable.address, i.brtable.default_target)) |
| 55 |
print("\t\tOperand[%u] size: %u" % (c, i.size)) |
| 56 |
c += 1 |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
def test_class(): |
| 62 |
for (arch, mode, code, comment) in all_tests: |
| 63 |
print("*" * 16) |
| 64 |
print("Platform: %s" % comment) |
| 65 |
print("Code: %s" % code.hex(' ')) |
| 66 |
print("Disasm:") |
| 67 |
|
| 68 |
try: |
| 69 |
md = Cs(arch, mode) |
| 70 |
md.detail = True |
| 71 |
for insn in md.disasm(code, 0xffff): |
| 72 |
print_insn_detail(insn) |
| 73 |
print() |
| 74 |
print("0x%x:\n" % (insn.address + insn.size)) |
| 75 |
except CsError as e: |
| 76 |
print("ERROR: %s" % e) |
| 77 |
|
| 78 |
|
| 79 |
if __name__ == '__main__': |
| 80 |
test_class() |