Skip to content

Commit 8cebd38

Browse files
committed
[lldb] [Mach-O] ProcessMachCore needs to strip TBI data from addrs (llvm#84998)
Darwin AArch64 application processors are run with Top Byte Ignore mode enabled so metadata may be stored in the top byte, it needs to be ignored when reading/writing memory. David Spickett handled this already in the base class Process::ReadMemory but ProcessMachCore overrides that method (to avoid the memory cache) and did not pick up the same change. I add a test case that creates a pointer with metadata in the top byte and dereferences it with a live process and with a corefile. rdar://123784501 (cherry picked from commit 52557bc)
1 parent 518f28d commit 8cebd38

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ size_t ProcessMachCore::ReadMemory(addr_t addr, void *buf, size_t size,
652652
Status &error) {
653653
// Don't allow the caching that lldb_private::Process::ReadMemory does since
654654
// in core files we have it all cached our our core file anyway.
655-
return DoReadMemory(addr, buf, size, error);
655+
return DoReadMemory(FixAnyAddress(addr), buf, size, error);
656656
}
657657

658658
size_t ProcessMachCore::DoReadMemory(addr_t addr, void *buf, size_t size,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C_SOURCES := main.c
2+
3+
include Makefile.rules
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Test that lldb on Darwin ignores metadata in the top byte of addresses, both corefile and live."""
2+
3+
import lldb
4+
from lldbsuite.test.decorators import *
5+
from lldbsuite.test.lldbtest import *
6+
from lldbsuite.test import lldbutil
7+
8+
9+
class TestTBIHonored(TestBase):
10+
NO_DEBUG_INFO_TESTCASE = True
11+
12+
def do_variable_access_tests(self, frame):
13+
self.assertEqual(
14+
frame.variables["pb"][0]
15+
.GetChildMemberWithName("p")
16+
.Dereference()
17+
.GetValueAsUnsigned(),
18+
15,
19+
)
20+
addr = frame.variables["pb"][0].GetChildMemberWithName("p").GetValueAsUnsigned()
21+
# Confirm that there is metadata in the top byte of our pointer
22+
self.assertEqual((addr >> 56) & 0xFF, 0xFE)
23+
self.expect("expr -- *pb.p", substrs=["15"])
24+
self.expect("frame variable *pb.p", substrs=["15"])
25+
self.expect("expr -- *(int*)0x%x" % addr, substrs=["15"])
26+
27+
# This test is valid on AArch64 systems with TBI mode enabled,
28+
# and an address mask that clears the top byte before reading
29+
# from memory.
30+
@skipUnlessDarwin
31+
@skipIf(archs=no_match(["arm64", "arm64e"]))
32+
@skipIfRemote
33+
def test(self):
34+
corefile = self.getBuildArtifact("process.core")
35+
self.build()
36+
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
37+
self, "// break here", lldb.SBFileSpec("main.c")
38+
)
39+
40+
# Test that we can dereference a pointer with TBI data
41+
# in a live process.
42+
self.do_variable_access_tests(thread.GetFrameAtIndex(0))
43+
44+
# Create a corefile, delete this process
45+
self.runCmd("process save-core -s stack " + corefile)
46+
process.Destroy()
47+
self.dbg.DeleteTarget(target)
48+
49+
# Now load the corefile
50+
target = self.dbg.CreateTarget("")
51+
process = target.LoadCore(corefile)
52+
thread = process.GetSelectedThread()
53+
self.assertTrue(process.GetSelectedThread().IsValid())
54+
55+
# Test that we can dereference a pointer with TBI data
56+
# in a corefile process.
57+
self.do_variable_access_tests(thread.GetFrameAtIndex(0))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
union ptrbytes {
4+
int *p;
5+
uint8_t bytes[8];
6+
};
7+
int main() {
8+
int c = 15;
9+
union ptrbytes pb;
10+
pb.p = &c;
11+
pb.bytes[7] = 0xfe;
12+
printf("%d\n", *pb.p); // break here
13+
}

0 commit comments

Comments
 (0)