Skip to content

[Reflection] Hoist computation of error existentials to the reader. #18242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2018
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
68 changes: 10 additions & 58 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ class ReflectionContext
public:
using super::getBuilder;
using super::readDemanglingForContextDescriptor;
using super::readIsaMask;
using super::readTypeFromMetadata;
using super::readGenericArgFromMetadata;
using super::readIsaMask;
using super::readMetadataAndValueErrorExistential;
using super::readMetadataAndValueOpaqueExistential;
using super::readMetadataFromInstance;
using super::readTypeFromMetadata;
using typename super::StoredPointer;

explicit ReflectionContext(std::shared_ptr<MemoryReader> reader)
Expand Down Expand Up @@ -497,70 +498,21 @@ class ReflectionContext
return true;
}
case RecordKind::ErrorExistential: {
// We have a pointer to an error existential, which is always heap object.

auto MetadataAddress
= readMetadataFromInstance(ExistentialAddress.getAddressData());

if (!MetadataAddress)
auto OptMetaAndValue =
readMetadataAndValueErrorExistential(ExistentialAddress);
if (!OptMetaAndValue)
return false;

bool isObjC = false;

// If we can determine the Objective-C class name, this is probably an
// error existential with NSError-compatible layout.
std::string ObjCClassName;
if (readObjCClassName(*MetadataAddress, ObjCClassName)) {
if (ObjCClassName == "_SwiftNativeNSError")
isObjC = true;
} else {
// Otherwise, we can check to see if this is a class metadata with the
// kind value's least significant bit set, which indicates a pure
// Swift class.
auto Meta = readMetadata(*MetadataAddress);
auto ClassMeta = dyn_cast<TargetClassMetadata<Runtime>>(Meta);
if (!ClassMeta)
return false;

isObjC = ClassMeta->isPureObjC();
}

// In addition to the isa pointer and two 32-bit reference counts, if the
// error existential is layout-compatible with NSError, we also need to
// skip over its three word-sized fields: the error code, the domain,
// and userInfo.
StoredPointer InstanceMetadataAddressAddress
= ExistentialAddress.getAddressData() +
(isObjC ? 5 : 2) * sizeof(StoredPointer);

// We need to get the instance's alignment info so we can get the exact
// offset of the start of its data in the class.
auto InstanceMetadataAddress =
readMetadataFromInstance(InstanceMetadataAddressAddress);
if (!InstanceMetadataAddress)
return false;
RemoteAddress InstanceMetadataAddress = OptMetaAndValue->first;
RemoteAddress InstanceAddress = OptMetaAndValue->second;

auto InstanceTR = readTypeFromMetadata(*InstanceMetadataAddress);
auto InstanceTR =
readTypeFromMetadata(InstanceMetadataAddress.getAddressData());
if (!InstanceTR)
return false;

auto InstanceTI = getTypeInfo(InstanceTR);
if (!InstanceTI)
return false;

// Now we need to skip over the instance metadata pointer and instance's
// conformance pointer for Swift.Error.
StoredPointer InstanceAddress = InstanceMetadataAddressAddress +
2 * sizeof(StoredPointer);

// Round up to alignment, and we have the start address of the
// instance payload.
auto Alignment = InstanceTI->getAlignment();
InstanceAddress += Alignment - InstanceAddress % Alignment;

*OutInstanceTR = InstanceTR;
*OutInstanceAddress = RemoteAddress(InstanceAddress);

return true;
}
default:
Expand Down
69 changes: 68 additions & 1 deletion include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ class MetadataReader {
}

/// Given a pointer to the metadata, attempt to read the value
/// witness table.
/// witness table. Note that it's not safe to access any non-mandatory
/// members of the value witness table, like extra inhabitants or enum members.
llvm::Optional<TargetValueWitnessTable<Runtime>>
readValueWitnessTable(StoredPointer MetadataAddress) {
// The value witness table pointer is at offset -1 from the metadata
Expand All @@ -283,6 +284,72 @@ class MetadataReader {
return VWT;
}

/// Given a pointer to a known-error existential, attempt to discover the
/// pointer to its metadata address and its value address.
llvm::Optional<std::pair<RemoteAddress, RemoteAddress>>
readMetadataAndValueErrorExistential(RemoteAddress ExistentialAddress) {
// An pointer to an error existential is always an heap object.
auto MetadataAddress =
readMetadataFromInstance(ExistentialAddress.getAddressData());
if (!MetadataAddress)
return llvm::None;

bool isObjC = false;

// If we can determine the Objective-C class name, this is probably an
// error existential with NSError-compatible layout.
std::string ObjCClassName;
if (readObjCClassName(*MetadataAddress, ObjCClassName)) {
if (ObjCClassName == "_SwiftNativeNSError")
isObjC = true;
} else {
// Otherwise, we can check to see if this is a class metadata with the
// kind value's least significant bit set, which indicates a pure
// Swift class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, does this happen? And if so, when? Is it just when ObjCInterop is disabled, because I feel like that's something that we ought to be able to check for (and arguably ought to be part of the Runtime template argument).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to ever happen (I tried to put an assertion and the tests still pass). I might consider removing this but I want to try a little harder to see whether I can exercise this.

auto Meta = readMetadata(*MetadataAddress);
auto ClassMeta = dyn_cast<TargetClassMetadata<Runtime>>(Meta);
if (!ClassMeta)
return llvm::None;

isObjC = ClassMeta->isPureObjC();
}

// In addition to the isa pointer and two 32-bit reference counts, if the
// error existential is layout-compatible with NSError, we also need to
// skip over its three word-sized fields: the error code, the domain,
// and userInfo.
StoredPointer InstanceMetadataAddressAddress =
ExistentialAddress.getAddressData() +
(isObjC ? 5 : 2) * sizeof(StoredPointer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. It'd be nice to just have a type for this structure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Will try to introduce one.


// We need to get the instance's alignment info so we can get the exact
// offset of the start of its data in the class.
auto InstanceMetadataAddress =
readMetadataFromInstance(InstanceMetadataAddressAddress);
if (!InstanceMetadataAddress)
return llvm::None;

// Read the value witness table.
auto VWT = readValueWitnessTable(*InstanceMetadataAddress);
if (!VWT)
return llvm::None;

// Now we need to skip over the instance metadata pointer and instance's
// conformance pointer for Swift.Error.
StoredPointer InstanceAddress =
InstanceMetadataAddressAddress + 2 * sizeof(StoredPointer);

// Round up to alignment, and we have the start address of the
// instance payload.
auto AlignmentMask = VWT->getAlignmentMask();
auto Offset = (sizeof(HeapObject) + AlignmentMask) & ~AlignmentMask;
InstanceAddress += Offset;

return llvm::Optional<std::pair<RemoteAddress, RemoteAddress>>(
{RemoteAddress(*InstanceMetadataAddress),
RemoteAddress(InstanceAddress)});
}

/// Given a known-opaque existential, attemp to discover the pointer to its
/// metadata address and its value.
llvm::Optional<std::pair<RemoteAddress, RemoteAddress>>
Expand Down