Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.

Commit 9d09352

Browse files
author
Greg Parker
authored
Merge pull request #152 from gparker42/new-refcount-representation
Adapt lldb to the new swift weak reference representation.
2 parents fcc0877 + 266eabe commit 9d09352

File tree

5 files changed

+87
-13
lines changed

5 files changed

+87
-13
lines changed

include/lldb/Target/SwiftLanguageRuntime.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ class SwiftLanguageRuntime : public LanguageRuntime {
354354
// to record useful runtime information
355355
// This API's task is to strip those bits if necessary and return
356356
// a pure pointer (or a tagged pointer)
357-
lldb::addr_t MaybeMaskNonTrivialReferencePointer(lldb::addr_t);
357+
lldb::addr_t MaybeMaskNonTrivialReferencePointer(
358+
lldb::addr_t,
359+
SwiftASTContext::NonTriviallyManagedReferenceStrategy strategy);
358360

359361
ConstString GetErrorBackstopName();
360362

source/Plugins/Language/Swift/SwiftOptional.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ ExtractSomeIfAny(ValueObject *optional,
7474
lldb::addr_t original_ptr =
7575
value_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
7676
lldb::addr_t tweaked_ptr =
77-
swift_runtime->MaybeMaskNonTrivialReferencePointer(original_ptr);
77+
swift_runtime->MaybeMaskNonTrivialReferencePointer(original_ptr,
78+
strategy);
7879
if (original_ptr != tweaked_ptr) {
7980
CompilerType value_type(value_sp->GetCompilerType());
8081
DataBufferSP buffer_sp(

source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class AppleObjCRuntime : public lldb_private::ObjCLanguageRuntime {
104104

105105
virtual void GetValuesForGlobalCFBooleans(lldb::addr_t &cf_true,
106106
lldb::addr_t &cf_false);
107+
108+
virtual bool IsTaggedPointer (lldb::addr_t addr) { return false; }
107109

108110
protected:
109111
// Call CreateInstance instead.

source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class AppleObjCRuntimeV2 : public AppleObjCRuntime {
9494

9595
EncodingToTypeSP GetEncodingToType() override;
9696

97-
bool IsTaggedPointer(lldb::addr_t ptr);
97+
bool IsTaggedPointer(lldb::addr_t ptr) override;
9898

9999
TaggedPointerVendor *GetTaggedPointerVendor() override {
100100
return m_tagged_pointer_vendor_ap.get();

source/Target/SwiftLanguageRuntime.cpp

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,8 +3327,16 @@ SwiftLanguageRuntime::MaskMaybeBridgedPointer(lldb::addr_t addr,
33273327
}
33283328

33293329
lldb::addr_t
3330-
SwiftLanguageRuntime::MaybeMaskNonTrivialReferencePointer(lldb::addr_t addr) {
3331-
if (auto objc_runtime = GetObjCRuntime()) {
3330+
SwiftLanguageRuntime::MaybeMaskNonTrivialReferencePointer(
3331+
lldb::addr_t addr,
3332+
SwiftASTContext::NonTriviallyManagedReferenceStrategy strategy) {
3333+
3334+
if (addr == 0)
3335+
return addr;
3336+
3337+
AppleObjCRuntime *objc_runtime = GetObjCRuntime();
3338+
3339+
if (objc_runtime) {
33323340
// tagged pointers don't perform any masking
33333341
if (objc_runtime->IsTaggedPointer(addr))
33343342
return addr;
@@ -3372,16 +3380,77 @@ SwiftLanguageRuntime::MaybeMaskNonTrivialReferencePointer(lldb::addr_t addr) {
33723380

33733381
lldb::addr_t mask = 0;
33743382

3375-
if (is_arm && is_64)
3376-
mask = SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS;
3377-
else if (is_intel && is_64)
3378-
mask = SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS;
3379-
else
3380-
mask = SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;
3383+
if (strategy == SwiftASTContext::NonTriviallyManagedReferenceStrategy::eWeak) {
3384+
bool is_indirect = true;
3385+
3386+
// On non-objc platforms, the weak reference pointer always pointed to a
3387+
// runtime structure.
3388+
// For ObjC platforms, the masked value determines whether it is indirect.
3389+
3390+
uint32_t value = 0;
3391+
3392+
if (objc_runtime)
3393+
{
3394+
3395+
if (is_intel) {
3396+
if (is_64) {
3397+
mask = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_MASK;
3398+
value = SWIFT_ABI_X86_64_OBJC_WEAK_REFERENCE_MARKER_VALUE;
3399+
} else {
3400+
mask = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_MASK;
3401+
value = SWIFT_ABI_I386_OBJC_WEAK_REFERENCE_MARKER_VALUE;
3402+
}
3403+
} else if (is_arm) {
3404+
if (is_64) {
3405+
mask = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_MASK;
3406+
value = SWIFT_ABI_ARM64_OBJC_WEAK_REFERENCE_MARKER_VALUE;
3407+
} else {
3408+
mask = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_MASK;
3409+
value = SWIFT_ABI_ARM_OBJC_WEAK_REFERENCE_MARKER_VALUE;
3410+
}
3411+
}
3412+
} else {
3413+
// This name is a little confusing. The "DEFAULT" marking in System.h
3414+
// is supposed to mean: the value for non-ObjC platforms. So
3415+
// DEFAULT_OBJC here actually means "non-ObjC".
3416+
mask = SWIFT_ABI_DEFAULT_OBJC_WEAK_REFERENCE_MARKER_MASK;
3417+
value = SWIFT_ABI_DEFAULT_OBJC_WEAK_REFERENCE_MARKER_VALUE;
3418+
}
3419+
3420+
is_indirect = ((addr & mask) == value);
3421+
3422+
if (!is_indirect)
3423+
return addr;
3424+
3425+
// The masked value of address is a pointer to the runtime structure.
3426+
// The first field of the structure is the actual pointer.
3427+
Process *process = GetProcess();
3428+
Error error;
3429+
3430+
lldb::addr_t masked_addr = addr & ~mask;
3431+
lldb::addr_t isa_addr = process->ReadPointerFromMemory(masked_addr, error);
3432+
if (error.Fail())
3433+
{
3434+
// FIXME: do some logging here.
3435+
return addr;
3436+
}
3437+
return isa_addr;
3438+
3439+
3440+
} else {
3441+
if (is_arm && is_64)
3442+
mask = SWIFT_ABI_ARM64_OBJC_NUM_RESERVED_LOW_BITS;
3443+
else if (is_intel && is_64)
3444+
mask = SWIFT_ABI_X86_64_OBJC_NUM_RESERVED_LOW_BITS;
3445+
else
3446+
mask = SWIFT_ABI_DEFAULT_OBJC_NUM_RESERVED_LOW_BITS;
33813447

3382-
mask = (1 << mask) | (1 << (mask + 1));
3448+
mask = (1 << mask) | (1 << (mask + 1));
33833449

3384-
return addr & ~mask;
3450+
return addr & ~mask;
3451+
}
3452+
3453+
return addr;
33853454
}
33863455

33873456
ConstString SwiftLanguageRuntime::GetErrorBackstopName() {

0 commit comments

Comments
 (0)