capstone

Форк
0
/
benchmark.py 
129 строк · 3.6 Кб
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

13
from capstone import *
14

15
from time import time
16
from random import randint
17
import sys
18

19

20
# file providing code to disassemble
21
FILE = '/usr/bin/python'
22

23

24
all_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
45
def to_hex(s):
46
    return " ".join("0x" + "{0:x}".format(ord(c)).zfill(2) for c in s) # <-- Python 3 is OK
47

48
def get_code(f, size):
49
    code = f.read(size)
50
    if len(code) != size:  # reached end-of-file?
51
        # then reset file position to begin-of-file
52
        f.seek(0)
53
        code = f.read(size)
54

55
    return code
56

57

58
def cs(md, code):
59
    insns = md.disasm(code, 0)
60
    # uncomment below line to speed up this function 200 times!
61
    # return
62
    for i in insns:
63
        if i.address == 0x100000:
64
            print i
65

66

67
def cs_lite(md, code):
68
    insns = md.disasm_lite(code, 0)
69
    for (addr, size, mnem, ops) in insns:
70
        if addr == 0x100000:
71
            print i
72

73

74
cfile = open(FILE)
75

76
for (arch, mode, comment, syntax) in all_tests:
77
    try:
78
        request = sys.argv[1]
79
        if not request in comment.lower():
80
            continue
81
    except:
82
        pass
83

84
    print("Platform: %s" %comment)
85

86
    try:
87
        md = Cs(arch, mode)
88
        #md.detail = True
89

90
        if syntax != 0:
91
            md.syntax = syntax
92

93
        # warm up few times
94
        cfile.seek(0)
95
        for i in xrange(3):
96
            code = get_code(cfile, 128)
97
            #print to_hex(code)
98
            #print
99
            cs(md, code)
100

101
        # start real benchmark
102
        c_t = 0
103
        for i in xrange(50000):
104
            code = get_code(cfile, 128)
105
            #print to_hex(code)
106
            #print
107

108
            t1 = time()
109
            cs(md, code)
110
            c_t += time() - t1
111

112
        print "Benchmark - full obj:", c_t, "seconds"
113
        print
114

115
        cfile.seek(0)
116
        c_t = 0
117
        for i in xrange(50000):
118
            code = get_code(cfile, 128)
119
            #print to_hex(code)
120
            #print
121

122
            t1 = time()
123
            cs_lite(md, code)
124
            c_t += time() - t1
125

126
        print "Benchmark - lite:", c_t, "seconds"
127
        print
128
    except CsError as e:
129
        print("ERROR: %s" %e)
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.