-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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.