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