|
| 1 | +""" |
| 2 | +Test that lldb removes non-address bits in situations where they would cause |
| 3 | +failures if not removed. Like when reading memory. Tests are done at command |
| 4 | +and API level because commands may remove non-address bits for display |
| 5 | +reasons which can make it seem like the operation as a whole works but at the |
| 6 | +API level it won't if we don't remove them there also. |
| 7 | +""" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +import lldb |
| 12 | +from lldbsuite.test.decorators import * |
| 13 | +from lldbsuite.test.lldbtest import * |
| 14 | +from lldbsuite.test import lldbutil |
| 15 | + |
| 16 | + |
| 17 | +class AArch64LinuxNonAddressBitMemoryAccessTestCase(TestBase): |
| 18 | + |
| 19 | + mydir = TestBase.compute_mydir(__file__) |
| 20 | + |
| 21 | + NO_DEBUG_INFO_TESTCASE = True |
| 22 | + |
| 23 | + def setup_test(self): |
| 24 | + if not self.isAArch64PAuth(): |
| 25 | + self.skipTest('Target must support pointer authentication.') |
| 26 | + |
| 27 | + self.build() |
| 28 | + self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) |
| 29 | + |
| 30 | + lldbutil.run_break_set_by_file_and_line(self, "main.c", |
| 31 | + line_number('main.c', '// Set break point at this line.'), |
| 32 | + num_expected_locations=1) |
| 33 | + |
| 34 | + self.runCmd("run", RUN_SUCCEEDED) |
| 35 | + |
| 36 | + if self.process().GetState() == lldb.eStateExited: |
| 37 | + self.fail("Test program failed to run.") |
| 38 | + |
| 39 | + self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| 40 | + substrs=['stopped', |
| 41 | + 'stop reason = breakpoint']) |
| 42 | + |
| 43 | + def check_cmd_read_write(self, write_to, read_from, data): |
| 44 | + self.runCmd("memory write {} {}".format(write_to, data)) |
| 45 | + self.expect("memory read {}".format(read_from), |
| 46 | + substrs=[data]) |
| 47 | + |
| 48 | + @skipUnlessArch("aarch64") |
| 49 | + @skipUnlessPlatform(["linux"]) |
| 50 | + def test_non_address_bit_memory_read_write_cmds(self): |
| 51 | + self.setup_test() |
| 52 | + |
| 53 | + # Writes should be visible through either pointer |
| 54 | + self.check_cmd_read_write("buf", "buf", "01 02 03 04") |
| 55 | + self.check_cmd_read_write("buf_with_non_address", "buf_with_non_address", "02 03 04 05") |
| 56 | + self.check_cmd_read_write("buf", "buf_with_non_address", "03 04 05 06") |
| 57 | + self.check_cmd_read_write("buf_with_non_address", "buf", "04 05 06 07") |
| 58 | + |
| 59 | + # Printing either should get the same result |
| 60 | + self.expect("expression -f hex -- *(uint32_t*)buf", substrs=["0x07060504"]) |
| 61 | + self.expect("expression -f hex -- *(uint32_t*)buf_with_non_address", |
| 62 | + substrs=["0x07060504"]) |
| 63 | + |
| 64 | + def get_ptr_values(self): |
| 65 | + frame = self.process().GetThreadAtIndex(0).GetFrameAtIndex(0) |
| 66 | + buf = frame.FindVariable("buf").GetValueAsUnsigned() |
| 67 | + buf_with_non_address = frame.FindVariable("buf_with_non_address").GetValueAsUnsigned() |
| 68 | + return buf, buf_with_non_address |
| 69 | + |
| 70 | + def check_api_read_write(self, write_to, read_from, data): |
| 71 | + error = lldb.SBError() |
| 72 | + written = self.process().WriteMemory(write_to, data, error) |
| 73 | + self.assertTrue(error.Success()) |
| 74 | + self.assertEqual(len(data), written) |
| 75 | + buf_content = self.process().ReadMemory(read_from, 4, error) |
| 76 | + self.assertTrue(error.Success()) |
| 77 | + self.assertEqual(data, buf_content) |
| 78 | + |
| 79 | + @skipUnlessArch("aarch64") |
| 80 | + @skipUnlessPlatform(["linux"]) |
| 81 | + def test_non_address_bit_memory_read_write_api_process(self): |
| 82 | + self.setup_test() |
| 83 | + buf, buf_with_non_address = self.get_ptr_values() |
| 84 | + |
| 85 | + # Writes are visible through either pointer |
| 86 | + self.check_api_read_write(buf, buf, bytes([0, 1, 2, 3])) |
| 87 | + self.check_api_read_write(buf_with_non_address, buf_with_non_address, bytes([1, 2, 3, 4])) |
| 88 | + self.check_api_read_write(buf, buf_with_non_address, bytes([2, 3, 4, 5])) |
| 89 | + self.check_api_read_write(buf_with_non_address, buf, bytes([3, 4, 5, 6])) |
| 90 | + |
| 91 | + # Now check all the "Read<type>FromMemory" don't fail |
| 92 | + error = lldb.SBError() |
| 93 | + # Last 4 bytes are just for the pointer read |
| 94 | + data = bytes([0x4C, 0x4C, 0x44, 0x42, 0x00, 0x12, 0x34, 0x56]) |
| 95 | + written = self.process().WriteMemory(buf, data, error) |
| 96 | + self.assertTrue(error.Success()) |
| 97 | + self.assertEqual(len(data), written) |
| 98 | + |
| 99 | + # C string |
| 100 | + c_string = self.process().ReadCStringFromMemory(buf_with_non_address, 5, error) |
| 101 | + self.assertTrue(error.Success()) |
| 102 | + self.assertEqual("LLDB", c_string) |
| 103 | + |
| 104 | + # Unsigned |
| 105 | + unsigned_num = self.process().ReadUnsignedFromMemory(buf_with_non_address, 4, error) |
| 106 | + self.assertTrue(error.Success()) |
| 107 | + self.assertEqual(0x42444c4c, unsigned_num) |
| 108 | + |
| 109 | + # Pointer |
| 110 | + ptr = self.process().ReadPointerFromMemory(buf_with_non_address, error) |
| 111 | + self.assertTrue(error.Success()) |
| 112 | + self.assertEqual(0x5634120042444c4c, ptr) |
| 113 | + |
| 114 | + @skipUnlessArch("aarch64") |
| 115 | + @skipUnlessPlatform(["linux"]) |
| 116 | + def test_non_address_bit_memory_read_write_api_target(self): |
| 117 | + self.setup_test() |
| 118 | + buf, buf_with_non_address = self.get_ptr_values() |
| 119 | + |
| 120 | + # Target only has ReadMemory |
| 121 | + error = lldb.SBError() |
| 122 | + data = bytes([1, 2, 3, 4]) |
| 123 | + written = self.process().WriteMemory(buf, data, error) |
| 124 | + self.assertTrue(error.Success()) |
| 125 | + self.assertEqual(len(data), written) |
| 126 | + |
| 127 | + addr = lldb.SBAddress() |
| 128 | + addr.SetLoadAddress(buf, self.target()) |
| 129 | + buf_read = self.target().ReadMemory(addr, 4, error) |
| 130 | + self.assertTrue(error.Success()) |
| 131 | + self.assertEqual(data, buf_read) |
| 132 | + |
| 133 | + addr.SetLoadAddress(buf_with_non_address, self.target()) |
| 134 | + buf_non_address_read = self.target().ReadMemory(addr, 4, error) |
| 135 | + self.assertTrue(error.Success()) |
| 136 | + self.assertEqual(data, buf_non_address_read) |
| 137 | + |
| 138 | + # Read<type>FromMemory are in Target but not SBTarget so no tests for those. |
| 139 | + |
| 140 | + @skipUnlessArch("aarch64") |
| 141 | + @skipUnlessPlatform(["linux"]) |
| 142 | + def test_non_address_bit_memory_caching(self): |
| 143 | + # The read/write tests above do exercise the cache but this test |
| 144 | + # only cares that the cache sees buf and buf_with_non_address |
| 145 | + # as the same location. |
| 146 | + self.setup_test() |
| 147 | + buf, buf_with_non_address = self.get_ptr_values() |
| 148 | + |
| 149 | + # Enable packet logging so we can see when reads actually |
| 150 | + # happen. |
| 151 | + log_file = self.getBuildArtifact("lldb-non-address-bit-log.txt") |
| 152 | + # This defaults to overwriting the file so we don't need to delete |
| 153 | + # any existing files. |
| 154 | + self.runCmd("log enable gdb-remote packets -f '%s'" % log_file) |
| 155 | + |
| 156 | + # This should fill the cache by doing a read of buf_with_non_address |
| 157 | + # with the non-address bits removed (which is == buf). |
| 158 | + self.runCmd("p buf_with_non_address") |
| 159 | + # This will read from the cache since the two pointers point to the |
| 160 | + # same place. |
| 161 | + self.runCmd("p buf") |
| 162 | + |
| 163 | + # Open log ignoring utf-8 decode errors |
| 164 | + with open(log_file, 'r', errors='ignore') as f: |
| 165 | + read_packet = "send packet: $x{:x}" |
| 166 | + read_buf_packet = read_packet.format(buf) |
| 167 | + read_buf_with_non_address_packet = read_packet.format(buf_with_non_address) |
| 168 | + |
| 169 | + # We expect to find 1 and only 1 read of buf. |
| 170 | + # We expect to find no reads using buf_with_no_address. |
| 171 | + found_read_buf = False |
| 172 | + for line in f: |
| 173 | + if read_buf_packet in line: |
| 174 | + if found_read_buf: |
| 175 | + self.fail("Expected 1 read of buf but found more than one.") |
| 176 | + found_read_buf = True |
| 177 | + |
| 178 | + if read_buf_with_non_address_packet in line: |
| 179 | + self.fail("Unexpected read of buf_with_non_address found.") |
| 180 | + |
| 181 | + if not found_read_buf: |
| 182 | + self.fail("Did not find any reads of buf.") |
0 commit comments