1""" 2Use lldb Python API to disassemble raw machine code bytes 3""" 4 5from __future__ import print_function 6 7from io import StringIO 8import sys 9 10import lldb 11from lldbsuite.test.decorators import * 12from lldbsuite.test.lldbtest import * 13from lldbsuite.test import lldbutil 14 15 16class Disassemble_VST1_64(TestBase): 17 18 @no_debug_info_test 19 @skipIfLLVMTargetMissing("ARM") 20 def test_disassemble_invalid_vst_1_64_raw_data(self): 21 """Test disassembling invalid vst1.64 raw bytes with the API.""" 22 # Create a target from the debugger. 23 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "thumbv7-apple-macosx") 24 self.assertTrue(target, VALID_TARGET) 25 26 raw_bytes = bytearray([0xf0, 0xb5, 0x03, 0xaf, 27 0x2d, 0xe9, 0x00, 0x0d, 28 0xad, 0xf1, 0x40, 0x04, 29 0x24, 0xf0, 0x0f, 0x04, 30 0xa5, 0x46]) 31 32 assembly = """ 33 push {r4, r5, r6, r7, lr} 34 add r7, sp, #0xc 35 push.w {r8, r10, r11} 36 sub.w r4, sp, #0x40 37 bic r4, r4, #0xf 38 mov sp, r4 39 """ 40 def split(s): 41 return [x.strip() for x in s.strip().splitlines()] 42 43 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes) 44 45 if self.TraceOn(): 46 print() 47 for i in insts: 48 print("Disassembled %s" % str(i)) 49 50 sio = StringIO() 51 insts.Print(sio) 52 self.assertEqual(split(assembly), split(sio.getvalue())) 53 54 self.assertEqual(insts.GetSize(), len(split(assembly))) 55 56 for i,asm in enumerate(split(assembly)): 57 inst = insts.GetInstructionAtIndex(i) 58 sio = StringIO() 59 inst.Print(sio) 60 self.assertEqual(asm, sio.getvalue().strip()) 61 62 raw_bytes = bytearray([0x04, 0xf9, 0xed, 0x82]) 63 64 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes) 65 66 inst = insts.GetInstructionAtIndex(0) 67 68 if self.TraceOn(): 69 print() 70 print("Raw bytes: ", [hex(x) for x in raw_bytes]) 71 print("Disassembled%s" % str(inst)) 72 73 self.assertEqual(inst.GetMnemonic(target), "vst1.64") 74