Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class MetadataReader {
StoredPointer TaggedPointerExtendedSlotShift;
StoredPointer TaggedPointerExtendedSlotMask;
StoredPointer TaggedPointerExtendedClasses;
StoredPointer TaggedPointerObfuscator;

Demangle::NodeFactory Factory;

Expand Down Expand Up @@ -502,14 +503,14 @@ class MetadataReader {
if (!readObjCClassName(MetadataAddress, className))
return BuiltType();

auto BuiltObjCClass = Builder.createObjCClassType(std::move(className));
BuiltType BuiltObjCClass = Builder.createObjCClassType(std::move(className));
if (!BuiltObjCClass) {
// Try the superclass.
if (!classMeta->Superclass)
return BuiltType();

return readTypeFromMetadata(classMeta->Superclass,
skipArtificialSubclasses);
BuiltObjCClass = readTypeFromMetadata(classMeta->Superclass,
skipArtificialSubclasses);
}

TypeCache[MetadataAddress] = BuiltObjCClass;
Expand Down Expand Up @@ -711,7 +712,7 @@ class MetadataReader {
if (getTaggedPointerEncoding() != TaggedPointerEncodingKind::Extended)
return false;

return objectAddress & TaggedPointerMask;
return (objectAddress ^ TaggedPointerObfuscator) & TaggedPointerMask;
}

/// Read the isa pointer of an Object-C tagged pointer value.
Expand All @@ -728,8 +729,8 @@ class MetadataReader {

// Extended pointers have a tag of 0b111, using 8 additional bits
// to specify the class.
if (TaggedPointerExtendedMask != 0 &&
((objectAddress & TaggedPointerExtendedMask)
if (TaggedPointerExtendedMask != 0 &&
(((objectAddress ^ TaggedPointerObfuscator) & TaggedPointerExtendedMask)
== TaggedPointerExtendedMask)) {
auto tag = ((objectAddress >> TaggedPointerExtendedSlotShift) &
TaggedPointerExtendedSlotMask);
Expand Down Expand Up @@ -2436,6 +2437,9 @@ class MetadataReader {
TaggedPointerExtendedClasses =
TaggedPointerExtendedClassesAddr.getAddressData();

tryFindAndReadSymbol(TaggedPointerObfuscator,
"objc_debug_taggedpointer_obfuscator");

# undef tryFindSymbol
# undef tryReadSymbol
# undef tryFindAndReadSymbol
Expand Down
17 changes: 17 additions & 0 deletions test/RemoteAST/existentials_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,22 @@ func printDynamicTypeAndAddressForExistential<T>(_: T)
// CHECK: NSObject
printDynamicTypeAndAddressForExistential(NSObject() as AnyObject)

// Print tagged pointer types three times to ensure the caching works.

// CHECK: NSNumber
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)

// CHECK: NSNumber
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)

// CHECK: NSNumber
printDynamicTypeAndAddressForExistential(NSNumber(123) as AnyObject)

// CHECK: NSString
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)

// CHECK: NSString
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)

// CHECK: NSString
printDynamicTypeAndAddressForExistential(NSString("hello") as AnyObject)
36 changes: 17 additions & 19 deletions tools/swift-remoteast-test/swift-remoteast-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,24 @@ using namespace swift::remoteAST;
/// The context for the code we're running. Set by the observer.
static ASTContext *Context = nullptr;

/// The RemoteAST for the code we're running.
std::shared_ptr<MemoryReader> reader;
std::unique_ptr<RemoteASTContext> remoteContext;

static RemoteASTContext &getRemoteASTContext() {
if (remoteContext)
return *remoteContext;

std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
remoteContext.reset(new RemoteASTContext(*Context, std::move(reader)));
return *remoteContext;
}

// FIXME: swiftcall
/// func printType(forMetadata: Any.Type)
LLVM_ATTRIBUTE_USED extern "C" void SWIFT_REMOTEAST_TEST_ABI
printMetadataType(const Metadata *typeMetadata) {
assert(Context && "context was not set");

std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));

auto &remoteAST = getRemoteASTContext();
auto &out = llvm::outs();

auto result =
Expand All @@ -66,11 +75,7 @@ printMetadataType(const Metadata *typeMetadata) {
/// func printDynamicType(_: AnyObject)
LLVM_ATTRIBUTE_USED extern "C" void SWIFT_REMOTEAST_TEST_ABI
printHeapMetadataType(void *object) {
assert(Context && "context was not set");

std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));

auto &remoteAST = getRemoteASTContext();
auto &out = llvm::outs();

auto metadataResult =
Expand All @@ -94,11 +99,7 @@ printHeapMetadataType(void *object) {

static void printMemberOffset(const Metadata *typeMetadata,
StringRef memberName, bool passMetadata) {
assert(Context && "context was not set");

std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));

auto &remoteAST = getRemoteASTContext();
auto &out = llvm::outs();

// The first thing we have to do is get the type.
Expand Down Expand Up @@ -147,10 +148,7 @@ printTypeMetadataMemberOffset(const Metadata *typeMetadata,
LLVM_ATTRIBUTE_USED extern "C" void SWIFT_REMOTEAST_TEST_ABI
printDynamicTypeAndAddressForExistential(void *object,
const Metadata *typeMetadata) {
assert(Context && "context was not set");
std::shared_ptr<MemoryReader> reader(new InProcessMemoryReader());
RemoteASTContext remoteAST(*Context, std::move(reader));

auto &remoteAST = getRemoteASTContext();
auto &out = llvm::outs();

// First, retrieve the static type of the existential, so we can understand
Expand Down