capstone
1#!/usr/bin/python
2
3# Simple benchmark for Capstone by disassembling random code. By Nguyen Anh Quynh, 2014
4# Syntax:
5# ./suite/benchmark.py --> Benchmark all archs
6# ./suite/benchmark.py x86 --> Benchmark all X86 (all 16bit, 32bit, 64bit)
7# ./suite/benchmark.py x86-32 --> Benchmark X86-32 arch only
8# ./suite/benchmark.py arm --> Benchmark all ARM (arm, thumb)
9# ./suite/benchmark.py aarch64 --> Benchmark ARM-64
10# ./suite/benchmark.py mips --> Benchmark all Mips (32bit, 64bit)
11# ./suite/benchmark.py ppc --> Benchmark PPC
12
13from capstone import *14
15from time import time16from random import randint17import sys18
19
20# file providing code to disassemble
21FILE = '/usr/bin/python'22
23
24all_tests = (25(CS_ARCH_X86, CS_MODE_16, "X86-16 (Intel syntax)", 0),26(CS_ARCH_X86, CS_MODE_32, "X86-32 (ATT syntax)", CS_OPT_SYNTAX_ATT),27(CS_ARCH_X86, CS_MODE_32, "X86-32 (Intel syntax)", 0),28(CS_ARCH_X86, CS_MODE_64, "X86-64 (Intel syntax)", 0),29(CS_ARCH_ARM, CS_MODE_ARM, "ARM", 0),30(CS_ARCH_ARM, CS_MODE_THUMB, "THUMB (ARM)", 0),31(CS_ARCH_MIPS, CS_MODE_MIPS32 + CS_MODE_BIG_ENDIAN, "MIPS-32 (Big-endian)", 0),32(CS_ARCH_MIPS, CS_MODE_MIPS64 + CS_MODE_LITTLE_ENDIAN, "MIPS-64-EL (Little-endian)", 0),33(CS_ARCH_AARCH64, CS_MODE_ARM, "ARM-64 (AArch64)", 0),34(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, "PPC", 0),35(CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, "PPC, print register with number only", CS_OPT_SYNTAX_NOREGNAME),36(CS_ARCH_SPARC, CS_MODE_BIG_ENDIAN, "Sparc", 0),37(CS_ARCH_SYSZ, 0, "SystemZ", 0),38(CS_ARCH_XCORE, 0, "XCore", 0),39(CS_ARCH_M68K, 0, "M68K", 0),40(CS_ARCH_RISCV, 0, "RISCV", 0),41)42
43
44# for debugging
45def to_hex(s):46return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK47
48def get_code(f, size):49code = f.read(size)50if len(code) != size: # reached end-of-file?51# then reset file position to begin-of-file52f.seek(0)53code = f.read(size)54
55return code56
57
58def cs(md, code):59insns = md.disasm(code, 0)60# uncomment below line to speed up this function 200 times!61# return62for i in insns:63if i.address == 0x100000:64print i65
66
67def cs_lite(md, code):68insns = md.disasm_lite(code, 0)69for (addr, size, mnem, ops) in insns:70if addr == 0x100000:71print i72
73
74cfile = open(FILE)75
76for (arch, mode, comment, syntax) in all_tests:77try:78request = sys.argv[1]79if not request in comment.lower():80continue81except:82pass83
84print("Platform: %s" %comment)85
86try:87md = Cs(arch, mode)88#md.detail = True89
90if syntax != 0:91md.syntax = syntax92
93# warm up few times94cfile.seek(0)95for i in xrange(3):96code = get_code(cfile, 128)97#print to_hex(code)9899cs(md, code)100
101# start real benchmark102c_t = 0103for i in xrange(50000):104code = get_code(cfile, 128)105#print to_hex(code)106107
108t1 = time()109cs(md, code)110c_t += time() - t1111
112print "Benchmark - full obj:", c_t, "seconds"113114
115cfile.seek(0)116c_t = 0117for i in xrange(50000):118code = get_code(cfile, 128)119#print to_hex(code)120121
122t1 = time()123cs_lite(md, code)124c_t += time() - t1125
126print "Benchmark - lite:", c_t, "seconds"127128except CsError as e:129print("ERROR: %s" %e)130