| 1 |
|
| 2 |
|
| 3 |
import array, os.path, sys |
| 4 |
from subprocess import Popen, PIPE, STDOUT |
| 5 |
from capstone import * |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
def normalize_hex(a): |
| 10 |
while(True): |
| 11 |
i = a.find('0x') |
| 12 |
if i == -1: |
| 13 |
break |
| 14 |
hexnum = '0x' |
| 15 |
for c in a[i + 2:]: |
| 16 |
if c in '0123456789abcdefABCDEF': |
| 17 |
hexnum += c |
| 18 |
else: |
| 19 |
break |
| 20 |
num = int(hexnum, 16) |
| 21 |
a = a.replace(hexnum, str(num)) |
| 22 |
return a |
| 23 |
|
| 24 |
|
| 25 |
def run_mc(arch, hexcode, option, syntax=None): |
| 26 |
def normalize(text): |
| 27 |
|
| 28 |
text = text.lower() |
| 29 |
items = text.split() |
| 30 |
text = ' '.join(items) |
| 31 |
if arch == CS_ARCH_X86: |
| 32 |
|
| 33 |
i = text.find('# ') |
| 34 |
if i != -1: |
| 35 |
return text[:i].strip() |
| 36 |
if arch == CS_ARCH_ARM64: |
| 37 |
|
| 38 |
i = text.find('// ') |
| 39 |
if i != -1: |
| 40 |
return text[:i].strip() |
| 41 |
|
| 42 |
text = text.replace('{ ', '{') |
| 43 |
text = text.replace(' }', '}') |
| 44 |
return text.strip() |
| 45 |
|
| 46 |
|
| 47 |
if syntax: |
| 48 |
if arch == CS_ARCH_MIPS: |
| 49 |
p = Popen(['llvm-mc', '-disassemble', '-print-imm-hex', '-mattr=+msa', syntax] + option, stdout=PIPE, stdin=PIPE, stderr=STDOUT) |
| 50 |
else: |
| 51 |
p = Popen(['llvm-mc', '-disassemble', '-print-imm-hex', syntax] + option, stdout=PIPE, stdin=PIPE, stderr=STDOUT) |
| 52 |
else: |
| 53 |
if arch == CS_ARCH_MIPS: |
| 54 |
p = Popen(['llvm-mc', '-disassemble', '-print-imm-hex', '-mattr=+msa'] + option, stdout=PIPE, stdin=PIPE, stderr=STDOUT) |
| 55 |
else: |
| 56 |
p = Popen(['llvm-mc', '-disassemble', '-print-imm-hex'] + option, stdout=PIPE, stdin=PIPE, stderr=STDOUT) |
| 57 |
output = p.communicate(input=hexcode)[0] |
| 58 |
lines = output.split('\n') |
| 59 |
|
| 60 |
if 'invalid' in lines[0]: |
| 61 |
|
| 62 |
return 'FAILED to disassemble (MC)' |
| 63 |
else: |
| 64 |
|
| 65 |
return normalize(lines[1].strip()) |
| 66 |
|
| 67 |
def test_file(fname): |
| 68 |
print("Test %s" %fname); |
| 69 |
f = open(fname) |
| 70 |
lines = f.readlines() |
| 71 |
f.close() |
| 72 |
|
| 73 |
if not lines[0].startswith('# '): |
| 74 |
print("ERROR: decoding information is missing") |
| 75 |
return |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
(arch, mode, option) = lines[0][2:].split(', ') |
| 82 |
mode = mode.replace(' ', '') |
| 83 |
option = option.strip() |
| 84 |
|
| 85 |
archs = { |
| 86 |
"CS_ARCH_ARM": CS_ARCH_ARM, |
| 87 |
"CS_ARCH_ARM64": CS_ARCH_ARM64, |
| 88 |
"CS_ARCH_MIPS": CS_ARCH_MIPS, |
| 89 |
"CS_ARCH_PPC": CS_ARCH_PPC, |
| 90 |
"CS_ARCH_SPARC": CS_ARCH_SPARC, |
| 91 |
"CS_ARCH_SYSZ": CS_ARCH_SYSZ, |
| 92 |
"CS_ARCH_X86": CS_ARCH_X86, |
| 93 |
"CS_ARCH_XCORE": CS_ARCH_XCORE, |
| 94 |
"CS_ARCH_RISCV": CS_ARCH_RISCV |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
modes = { |
| 99 |
"CS_MODE_16": CS_MODE_16, |
| 100 |
"CS_MODE_32": CS_MODE_32, |
| 101 |
"CS_MODE_64": CS_MODE_64, |
| 102 |
"CS_MODE_MIPS32": CS_MODE_MIPS32, |
| 103 |
"CS_MODE_MIPS64": CS_MODE_MIPS64, |
| 104 |
"0": CS_MODE_ARM, |
| 105 |
"CS_MODE_ARM": CS_MODE_ARM, |
| 106 |
"CS_MODE_THUMB": CS_MODE_THUMB, |
| 107 |
"CS_MODE_ARM+CS_MODE_V8": CS_MODE_ARM+CS_MODE_V8, |
| 108 |
"CS_MODE_THUMB+CS_MODE_V8": CS_MODE_THUMB+CS_MODE_V8, |
| 109 |
"CS_MODE_THUMB+CS_MODE_MCLASS": CS_MODE_THUMB+CS_MODE_MCLASS, |
| 110 |
"CS_MODE_LITTLE_ENDIAN": CS_MODE_LITTLE_ENDIAN, |
| 111 |
"CS_MODE_BIG_ENDIAN": CS_MODE_BIG_ENDIAN, |
| 112 |
"CS_MODE_64+CS_MODE_LITTLE_ENDIAN": CS_MODE_64+CS_MODE_LITTLE_ENDIAN, |
| 113 |
"CS_MODE_64+CS_MODE_BIG_ENDIAN": CS_MODE_64+CS_MODE_BIG_ENDIAN, |
| 114 |
"CS_MODE_MIPS32+CS_MODE_MICRO": CS_MODE_MIPS32+CS_MODE_MICRO, |
| 115 |
"CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN, |
| 116 |
"CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN+CS_MODE_MICRO": CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN, |
| 117 |
"CS_MODE_BIG_ENDIAN+CS_MODE_V9": CS_MODE_BIG_ENDIAN + CS_MODE_V9, |
| 118 |
"CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN, |
| 119 |
"CS_MODE_MIPS32+CS_MODE_LITTLE_ENDIAN": CS_MODE_MIPS32+CS_MODE_LITTLE_ENDIAN, |
| 120 |
"CS_MODE_MIPS64+CS_MODE_LITTLE_ENDIAN": CS_MODE_MIPS64+CS_MODE_LITTLE_ENDIAN, |
| 121 |
"CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN": CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN, |
| 122 |
"CS_MODE_RISCV32": CS_MODE_RISCV32, |
| 123 |
"CS_MODE_RISCV64": CS_MODE_RISCV64, |
| 124 |
} |
| 125 |
|
| 126 |
options = { |
| 127 |
"CS_OPT_SYNTAX_ATT": CS_OPT_SYNTAX_ATT, |
| 128 |
"CS_OPT_SYNTAX_NOREGNAME": CS_OPT_SYNTAX_NOREGNAME, |
| 129 |
} |
| 130 |
|
| 131 |
mc_modes = { |
| 132 |
("CS_ARCH_X86", "CS_MODE_32"): ['-triple=i386'], |
| 133 |
("CS_ARCH_X86", "CS_MODE_64"): ['-triple=x86_64'], |
| 134 |
("CS_ARCH_ARM", "CS_MODE_ARM"): ['-triple=armv7'], |
| 135 |
("CS_ARCH_ARM", "CS_MODE_THUMB"): ['-triple=thumbv7'], |
| 136 |
("CS_ARCH_ARM", "CS_MODE_ARM+CS_MODE_V8"): ['-triple=armv8'], |
| 137 |
("CS_ARCH_ARM", "CS_MODE_THUMB+CS_MODE_V8"): ['-triple=thumbv8'], |
| 138 |
("CS_ARCH_ARM", "CS_MODE_THUMB+CS_MODE_MCLASS"): ['-triple=thumbv7m'], |
| 139 |
("CS_ARCH_ARM64", "0"): ['-triple=aarch64'], |
| 140 |
("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN"): ['-triple=mips'], |
| 141 |
("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_MICRO"): ['-triple=mipsel', '-mattr=+micromips'], |
| 142 |
("CS_ARCH_MIPS", "CS_MODE_MIPS64"): ['-triple=mips64el'], |
| 143 |
("CS_ARCH_MIPS", "CS_MODE_MIPS32"): ['-triple=mipsel'], |
| 144 |
("CS_ARCH_MIPS", "CS_MODE_MIPS64+CS_MODE_BIG_ENDIAN"): ['-triple=mips64'], |
| 145 |
("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_MICRO+CS_MODE_BIG_ENDIAN"): ['-triple=mips', '-mattr=+micromips'], |
| 146 |
("CS_ARCH_MIPS", "CS_MODE_MIPS32+CS_MODE_BIG_ENDIAN+CS_MODE_MICRO"): ['-triple=mips', '-mattr=+micromips'], |
| 147 |
("CS_ARCH_PPC", "CS_MODE_BIG_ENDIAN"): ['-triple=powerpc64'], |
| 148 |
('CS_ARCH_SPARC', 'CS_MODE_BIG_ENDIAN'): ['-triple=sparc'], |
| 149 |
('CS_ARCH_SPARC', 'CS_MODE_BIG_ENDIAN+CS_MODE_V9'): ['-triple=sparcv9'], |
| 150 |
('CS_ARCH_SYSZ', '0'): ['-triple=s390x', '-mcpu=z196'], |
| 151 |
('CS_ARCH_RISCV', 'CS_MODE_RISCV32'): ['-triple=riscv32'], |
| 152 |
('CS_ARCH_RISCV', 'CS_MODE_RISCV64'): ['-triple=riscv64'], |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
md = Cs(archs[arch], modes[mode]) |
| 160 |
|
| 161 |
mc_option = None |
| 162 |
if arch == 'CS_ARCH_X86': |
| 163 |
|
| 164 |
mc_option = '-output-asm-variant=1' |
| 165 |
|
| 166 |
if arch == 'CS_ARCH_ARM' or arch == 'CS_ARCH_PPC' : |
| 167 |
md.syntax = CS_OPT_SYNTAX_NOREGNAME |
| 168 |
|
| 169 |
if fname.endswith('3DNow.s.cs'): |
| 170 |
md.syntax = CS_OPT_SYNTAX_ATT |
| 171 |
|
| 172 |
for line in lines[1:]: |
| 173 |
|
| 174 |
if line.startswith('#'): |
| 175 |
continue |
| 176 |
|
| 177 |
code = line.split(' = ')[0] |
| 178 |
asm = ''.join(line.split(' = ')[1:]) |
| 179 |
hex_code = code.replace('0x', '') |
| 180 |
hex_code = hex_code.replace(',', '') |
| 181 |
hex_data = hex_code.decode('hex') |
| 182 |
|
| 183 |
|
| 184 |
x = list(md.disasm(hex_data, 0)) |
| 185 |
if len(x) > 0: |
| 186 |
if x[0].op_str != '': |
| 187 |
cs_output = "%s %s" %(x[0].mnemonic, x[0].op_str) |
| 188 |
else: |
| 189 |
cs_output = x[0].mnemonic |
| 190 |
else: |
| 191 |
cs_output = 'FAILED to disassemble' |
| 192 |
|
| 193 |
cs_output2 = normalize_hex(cs_output) |
| 194 |
cs_output2 = cs_output2.replace(' ', '') |
| 195 |
|
| 196 |
if arch == 'CS_ARCH_MIPS': |
| 197 |
|
| 198 |
cs_output2 = cs_output2.replace('$at', '$1') |
| 199 |
cs_output2 = cs_output2.replace('$v0', '$2') |
| 200 |
cs_output2 = cs_output2.replace('$v1', '$3') |
| 201 |
|
| 202 |
cs_output2 = cs_output2.replace('$a0', '$4') |
| 203 |
cs_output2 = cs_output2.replace('$a1', '$5') |
| 204 |
cs_output2 = cs_output2.replace('$a2', '$6') |
| 205 |
cs_output2 = cs_output2.replace('$a3', '$7') |
| 206 |
|
| 207 |
cs_output2 = cs_output2.replace('$t0', '$8') |
| 208 |
cs_output2 = cs_output2.replace('$t1', '$9') |
| 209 |
cs_output2 = cs_output2.replace('$t2', '$10') |
| 210 |
cs_output2 = cs_output2.replace('$t3', '$11') |
| 211 |
cs_output2 = cs_output2.replace('$t4', '$12') |
| 212 |
cs_output2 = cs_output2.replace('$t5', '$13') |
| 213 |
cs_output2 = cs_output2.replace('$t6', '$14') |
| 214 |
cs_output2 = cs_output2.replace('$t7', '$15') |
| 215 |
cs_output2 = cs_output2.replace('$t8', '$24') |
| 216 |
cs_output2 = cs_output2.replace('$t9', '$25') |
| 217 |
|
| 218 |
cs_output2 = cs_output2.replace('$s0', '$16') |
| 219 |
cs_output2 = cs_output2.replace('$s1', '$17') |
| 220 |
cs_output2 = cs_output2.replace('$s2', '$18') |
| 221 |
cs_output2 = cs_output2.replace('$s3', '$19') |
| 222 |
cs_output2 = cs_output2.replace('$s4', '$20') |
| 223 |
cs_output2 = cs_output2.replace('$s5', '$21') |
| 224 |
cs_output2 = cs_output2.replace('$s6', '$22') |
| 225 |
cs_output2 = cs_output2.replace('$s7', '$23') |
| 226 |
|
| 227 |
cs_output2 = cs_output2.replace('$k0', '$26') |
| 228 |
cs_output2 = cs_output2.replace('$k1', '$27') |
| 229 |
|
| 230 |
|
| 231 |
if fname.endswith('thumb-fp-armv8.s.cs'): |
| 232 |
mc_output = run_mc(archs[arch], code, ['-triple=thumbv8'], mc_option) |
| 233 |
elif fname.endswith('mips64-alu-instructions.s.cs'): |
| 234 |
mc_output = run_mc(archs[arch], code, ['-triple=mips64el', '-mcpu=mips64r2'], mc_option) |
| 235 |
else: |
| 236 |
mc_output = run_mc(archs[arch], code, mc_modes[(arch, mode)], mc_option) |
| 237 |
mc_output2 = normalize_hex(mc_output) |
| 238 |
|
| 239 |
if arch == 'CS_ARCH_MIPS': |
| 240 |
mc_output2 = mc_output2.replace(' 0(', '(') |
| 241 |
|
| 242 |
if arch == 'CS_ARCH_PPC': |
| 243 |
mc_output2 = mc_output2.replace('.+', '') |
| 244 |
mc_output2 = mc_output2.replace('.', '') |
| 245 |
mc_output2 = mc_output2.replace(' 0(', '(') |
| 246 |
|
| 247 |
mc_output2 = mc_output2.replace(' ', '') |
| 248 |
mc_output2 = mc_output2.replace('opaque', '') |
| 249 |
|
| 250 |
|
| 251 |
if (cs_output2 != mc_output2): |
| 252 |
asm = asm.replace(' ', '').strip().lower() |
| 253 |
if asm != cs_output2: |
| 254 |
print("Mismatch: %s" %line.strip()) |
| 255 |
print("\tMC = %s" %mc_output) |
| 256 |
print("\tCS = %s" %cs_output) |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == '__main__': |
| 260 |
if len(sys.argv) == 1: |
| 261 |
fnames = sys.stdin.readlines() |
| 262 |
for fname in fnames: |
| 263 |
test_file(fname.strip()) |
| 264 |
else: |
| 265 |
|
| 266 |
test_file(sys.argv[1]) |
| 267 |
|