[lldb] Add SBValue::GetValueAsAddress API (#90144)

I previously added this API via https://reviews.llvm.org/D142792 in
2023, along with changes to the ValueObject class to treat pointer types
as addresses, and to annotate those ValueObjects with the original
uint64_t byte sequence AND the name of the symbol once stripped, if that
points to a symbol.

I did this unconditionally for all pointer type ValueObjects, and it
caused several regressions in the Objective-C data formatters which have
a ValueObject of an object, it has the address of its class -- but with
ObjC, sometimes it is a "tagged pointer" which is metadata, not an
actual pointer. (e.g. a small NSInteger value is stored entirely in the
tagged pointer, instead of a separate object) Treating these
not-addresses as addresses -- clearing the non-addressable-bits -- is
invalid.

The original version of this patch we're using downstream only does this
bits clearing for pointer types that are specifically decorated with the
pointerauth typequal, but not all of those clang changes are upstreamed
to github main yet, so I tried this simpler approach and hit the tagged
pointer issue and bailed on the whole patch.

This patch, however, is simply adding SBValue::GetValueAsAddress so
script writers who know that an SBValue has an address in memory, can
strip off any metadata. It's an important API to have for script writers
when AArch64 ptrauth is in use, so I'm going to put this part of the
patch back on github main now until we can get the rest of that original
patch upstreamed.
This commit is contained in:
Jason Molenda 2024-04-25 16:42:33 -07:00 committed by GitHub
parent 5f67ce5611
commit eb7dc99184
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 130 additions and 0 deletions

View File

@ -135,6 +135,26 @@ linked list."
%feature("docstring", "Expands nested expressions like .a->b[0].c[1]->d." %feature("docstring", "Expands nested expressions like .a->b[0].c[1]->d."
) lldb::SBValue::GetValueForExpressionPath; ) lldb::SBValue::GetValueForExpressionPath;
%feature("docstring", "
Return the value as an address. On failure, LLDB_INVALID_ADDRESS
will be returned. On architectures like AArch64, where the
top (unaddressable) bits can be used for authentication,
memory tagging, or top byte ignore, this method will return
the value with those top bits cleared.
GetValueAsUnsigned returns the actual value, with the
authentication/Top Byte Ignore/Memory Tagging Extension bits.
Calling this on a random value which is not a pointer is
incorrect. Call GetType().IsPointerType() if in doubt.
An SB API program may want to show both the literal byte value
and the address it refers to in memory. These two SBValue
methods allow SB API writers to behave appropriately for their
interface."
) lldb::SBValue::GetValueAsAddress;
%feature("doctstring", " %feature("doctstring", "
Returns the number for children. Returns the number for children.

View File

@ -68,6 +68,8 @@ public:
uint64_t GetValueAsUnsigned(uint64_t fail_value = 0); uint64_t GetValueAsUnsigned(uint64_t fail_value = 0);
lldb::addr_t GetValueAsAddress();
ValueType GetValueType(); ValueType GetValueType();
// If you call this on a newly created ValueObject, it will always return // If you call this on a newly created ValueObject, it will always return

View File

@ -909,6 +909,25 @@ uint64_t SBValue::GetValueAsUnsigned(uint64_t fail_value) {
return fail_value; return fail_value;
} }
lldb::addr_t SBValue::GetValueAsAddress() {
addr_t fail_value = LLDB_INVALID_ADDRESS;
ValueLocker locker;
lldb::ValueObjectSP value_sp(GetSP(locker));
if (value_sp) {
bool success = true;
uint64_t ret_val = fail_value;
ret_val = value_sp->GetValueAsUnsigned(fail_value, &success);
if (!success)
return fail_value;
ProcessSP process_sp = m_opaque_sp->GetProcessSP();
if (!process_sp)
return ret_val;
return process_sp->FixDataAddress(ret_val);
}
return fail_value;
}
bool SBValue::MightHaveChildren() { bool SBValue::MightHaveChildren() {
LLDB_INSTRUMENT_VA(this); LLDB_INSTRUMENT_VA(this);

View File

@ -0,0 +1,3 @@
C_SOURCES := main.c
include Makefile.rules

View File

@ -0,0 +1,59 @@
"""Test that SBValue clears non-addressable bits"""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class TestClearSBValueNonAddressableBits(TestBase):
NO_DEBUG_INFO_TESTCASE = True
# On AArch64 systems, the top bits that are not used for
# addressing may be used for TBI, MTE, and/or pointer
# authentication.
@skipIf(archs=no_match(["aarch64", "arm64", "arm64e"]))
# Only run this test on systems where TBI is known to be
# enabled, so the address mask will clear the TBI bits.
@skipUnlessPlatform(["linux"] + lldbplatformutil.getDarwinOSTriples())
def test(self):
self.source = "main.c"
self.build()
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec(self.source, False)
)
if self.TraceOn():
self.runCmd("frame variable")
self.runCmd("frame variable &count &global")
frame = thread.GetFrameAtIndex(0)
count_p = frame.FindVariable("count_p")
count_invalid_p = frame.FindVariable("count_invalid_p")
self.assertEqual(
count_p.GetValueAsUnsigned(), count_invalid_p.GetValueAsAddress()
)
self.assertNotEqual(
count_invalid_p.GetValueAsUnsigned(), count_invalid_p.GetValueAsAddress()
)
self.assertEqual(5, count_p.Dereference().GetValueAsUnsigned())
self.assertEqual(5, count_invalid_p.Dereference().GetValueAsUnsigned())
global_p = frame.FindVariable("global_p")
global_invalid_p = frame.FindVariable("global_invalid_p")
self.assertEqual(
global_p.GetValueAsUnsigned(), global_invalid_p.GetValueAsAddress()
)
self.assertNotEqual(
global_invalid_p.GetValueAsUnsigned(), global_invalid_p.GetValueAsAddress()
)
self.assertEqual(10, global_p.Dereference().GetValueAsUnsigned())
self.assertEqual(10, global_invalid_p.Dereference().GetValueAsUnsigned())
main_p = frame.FindVariable("main_p")
main_invalid_p = frame.FindVariable("main_invalid_p")
self.assertEqual(
main_p.GetValueAsUnsigned(), main_invalid_p.GetValueAsAddress()
)

View File

@ -0,0 +1,27 @@
#include <stdint.h>
int global = 10;
int main() {
int count = 5;
int *count_p = &count;
// Add some metadata in the top byte (this will crash unless the
// test is running with TBI enabled, but we won't dereference it)
intptr_t scratch = (intptr_t)count_p;
scratch |= (3ULL << 60);
int *count_invalid_p = (int *)scratch;
int (*main_p)() = main;
scratch = (intptr_t)main_p;
scratch |= (3ULL << 60);
int (*main_invalid_p)() = (int (*)())scratch;
int *global_p = &global;
scratch = (intptr_t)global_p;
scratch |= (3ULL << 60);
int *global_invalid_p = (int *)scratch;
return count; // break here
}