|
| 1 | +"""Test MTE Memory Tagging on Apple platforms""" |
| 2 | + |
| 3 | +import lldb |
| 4 | +import re |
| 5 | +from lldbsuite.test.decorators import * |
| 6 | +from lldbsuite.test.lldbtest import * |
| 7 | +from lldbsuite.test import lldbutil |
| 8 | +import lldbsuite.test.cpu_feature as cpu_feature |
| 9 | + |
| 10 | +exe_name = "uaf_mte" # Must match Makefile |
| 11 | + |
| 12 | + |
| 13 | +class TestDarwinMTE(TestBase): |
| 14 | + NO_DEBUG_INFO_TESTCASE = True |
| 15 | + |
| 16 | + @skipUnlessFeature(cpu_feature.AArch64.MTE) |
| 17 | + def test_tag_fault(self): |
| 18 | + self.build() |
| 19 | + exe = self.getBuildArtifact(exe_name) |
| 20 | + |
| 21 | + target = self.dbg.CreateTarget(exe) |
| 22 | + self.assertTrue(target, VALID_TARGET) |
| 23 | + |
| 24 | + process = target.LaunchSimple(None, None, None) |
| 25 | + self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED) |
| 26 | + |
| 27 | + self.expect( |
| 28 | + "thread info", |
| 29 | + substrs=[ |
| 30 | + "stop reason = EXC_ARM_MTE_TAG_FAULT", |
| 31 | + "MTE tag mismatch detected", |
| 32 | + ], |
| 33 | + ) |
| 34 | + |
| 35 | + @skipUnlessFeature(cpu_feature.AArch64.MTE) |
| 36 | + def test_memory_region(self): |
| 37 | + self.build() |
| 38 | + lldbutil.run_to_source_breakpoint( |
| 39 | + self, "// before free", lldb.SBFileSpec("main.c"), exe_name=exe_name |
| 40 | + ) |
| 41 | + |
| 42 | + # (lldb) memory region ptr |
| 43 | + # [0x00000001005ec000-0x00000001009ec000) rw- |
| 44 | + # memory tagging: enabled |
| 45 | + # Modified memory (dirty) page list provided, 2 entries. |
| 46 | + # Dirty pages: 0x1005ec000, 0x1005fc000. |
| 47 | + self.expect("memory region ptr", substrs=["memory tagging: enabled"]) |
| 48 | + |
| 49 | + @skipUnlessFeature(cpu_feature.AArch64.MTE) |
| 50 | + def test_memory_read_with_tags(self): |
| 51 | + self.build() |
| 52 | + lldbutil.run_to_source_breakpoint( |
| 53 | + self, "// before free", lldb.SBFileSpec("main.c"), exe_name=exe_name |
| 54 | + ) |
| 55 | + |
| 56 | + # (lldb) memory read ptr-16 ptr+48 --show-tags |
| 57 | + # 0x7d2c00930: 00 00 00 00 00 00 00 00 d0 e3 a5 0a 02 00 00 00 ................ (tag: 0x3) |
| 58 | + # 0x7d2c00940: 48 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 Hello........... (tag: 0xb) |
| 59 | + # 0x7d2c00950: 57 6f 72 6c 64 00 00 00 00 00 00 00 00 00 00 00 World........... (tag: 0xb) |
| 60 | + # 0x7d2c00960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ (tag: 0x9) |
| 61 | + self.expect( |
| 62 | + "memory read ptr-16 ptr+48 --show-tags", |
| 63 | + substrs=[" Hello...........", " World..........."], |
| 64 | + patterns=[r"(.*\(tag: 0x[0-9a-f]\)\n){4}"], |
| 65 | + ) |
| 66 | + |
| 67 | + def _parse_pointer_tag(self, output): |
| 68 | + return re.search(r"Logical tag: (0x[0-9a-f])", output).group(1) |
| 69 | + |
| 70 | + def _parse_memory_tags(self, output, expected_tag_count): |
| 71 | + tags = re.findall(r"\): (0x[0-9a-f])", output) |
| 72 | + self.assertEqual(len(tags), expected_tag_count) |
| 73 | + return tags |
| 74 | + |
| 75 | + @skipUnlessFeature(cpu_feature.AArch64.MTE) |
| 76 | + def test_memory_tag_read(self): |
| 77 | + self.build() |
| 78 | + lldbutil.run_to_source_breakpoint( |
| 79 | + self, "// before free", lldb.SBFileSpec("main.c"), exe_name=exe_name |
| 80 | + ) |
| 81 | + |
| 82 | + # (lldb) memory tag read ptr-1 ptr+33 |
| 83 | + # Logical tag: 0x5 |
| 84 | + # Allocation tags: |
| 85 | + # [0x100a65a40, 0x100a65a50): 0xf (mismatch) |
| 86 | + # [0x100a65a50, 0x100a65a60): 0x5 |
| 87 | + # [0x100a65a60, 0x100a65a70): 0x5 |
| 88 | + # [0x100a65a70, 0x100a65a80): 0x2 (mismatch) |
| 89 | + self.expect( |
| 90 | + "memory tag read ptr-1 ptr+33", |
| 91 | + substrs=["Logical tag: 0x", "Allocation tags:", "(mismatch)"], |
| 92 | + patterns=[r"(\[.*\): 0x[0-9a-f].*\n){4}"], |
| 93 | + ) |
| 94 | + output = self.res.GetOutput() |
| 95 | + self.assertEqual(output.count("(mismatch)"), 2) |
| 96 | + ptr_tag = self._parse_pointer_tag(output) |
| 97 | + tags = self._parse_memory_tags(output, 4) |
| 98 | + self.assertEqual(tags[1], ptr_tag) |
| 99 | + self.assertEqual(tags[2], ptr_tag) |
| 100 | + self.assertNotEqual(tags[0], ptr_tag) # Memory that comes before/after |
| 101 | + self.assertNotEqual(tags[3], ptr_tag) # allocation has different tag. |
| 102 | + |
| 103 | + # Continue running until MTE fault |
| 104 | + self.expect("process continue", substrs=["stop reason = EXC_ARM_MTE_TAG_FAULT"]) |
| 105 | + |
| 106 | + self.runCmd("memory tag read ptr-1 ptr+33") |
| 107 | + output = self.res.GetOutput() |
| 108 | + self.assertEqual(output.count("(mismatch)"), 4) |
| 109 | + tags = self._parse_memory_tags(output, 4) |
| 110 | + self.assertTrue(all(t != ptr_tag for t in tags)) |
0 commit comments