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 mydir = TestBase.compute_mydir(__file__) 19 20 @no_debug_info_test 21 @skipIfLLVMTargetMissing("ARM") 22 @skipIfReproducer # lldb::FileSP used in typemap cannot be instrumented. 23 def test_disassemble_invalid_vst_1_64_raw_data(self): 24 """Test disassembling invalid vst1.64 raw bytes with the API.""" 25 # Create a target from the debugger. 26 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "thumbv7-apple-macosx") 27 self.assertTrue(target, VALID_TARGET) 28 29 raw_bytes = bytearray([0xf0, 0xb5, 0x03, 0xaf, 30 0x2d, 0xe9, 0x00, 0x0d, 31 0xad, 0xf1, 0x40, 0x04, 32 0x24, 0xf0, 0x0f, 0x04, 33 0xa5, 0x46]) 34 35 assembly = """ 36 push {r4, r5, r6, r7, lr} 37 add r7, sp, #0xc 38 push.w {r8, r10, r11} 39 sub.w r4, sp, #0x40 40 bic r4, r4, #0xf 41 mov sp, r4 42 """ 43 def split(s): 44 return [x.strip() for x in s.strip().splitlines()] 45 46 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes) 47 48 if self.TraceOn(): 49 print() 50 for i in insts: 51 print("Disassembled %s" % str(i)) 52 53 if sys.version_info.major >= 3: 54 sio = StringIO() 55 insts.Print(sio) 56 self.assertEqual(split(assembly), split(sio.getvalue())) 57 58 self.assertEqual(insts.GetSize(), len(split(assembly))) 59 60 if sys.version_info.major >= 3: 61 for i,asm in enumerate(split(assembly)): 62 inst = insts.GetInstructionAtIndex(i) 63 sio = StringIO() 64 inst.Print(sio) 65 self.assertEqual(asm, sio.getvalue().strip()) 66 67 raw_bytes = bytearray([0x04, 0xf9, 0xed, 0x82]) 68 69 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes) 70 71 inst = insts.GetInstructionAtIndex(0) 72 73 if self.TraceOn(): 74 print() 75 print("Raw bytes: ", [hex(x) for x in raw_bytes]) 76 print("Disassembled%s" % str(inst)) 77 78 self.assertEqual(inst.GetMnemonic(target), "vst1.64") 79