Skip to content
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

Fix multi-payload enums with Class Existential payloads on 32-bit targets #77027

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Oct 15, 2024

Class existentials expose spare bits from all of the pointers, not just the first one. Due to a bad bug here, we were properly exposing spare bits from the first pointer, but then claiming that all bits of subsequent pointers were spare. This accidentally resulted in the correct operation on 64-bit targets (it picked the highest-order spare bit, which happened to be spare in both the broken mask and the correct mask). But on 32-bit targets, this exposed the high-order bits of pointers, which is incorrect.

Expand the test a bit while we're here as well.

Resolves rdar://132715829

…gets

Class existentials expose spare bits from all of the pointers, not just the first one.
Due to a bad bug here, we were properly exposing spare bits from the first pointer,
but then claiming that all bits of subsequent pointers were spare.
This accidentally resulted in the correct operation on 64-bit targets
(it picked the highest-order spare bit, which happened to be spare
in both the broken mask and the correct mask).  But on 32-bit targets,
this exposed the high-order bits of pointers, which is incorrect.

Expand the test a bit while we're here as well.

Resolves rdar://132715829
@tbkka tbkka requested review from slavapestov and a team as code owners October 15, 2024 16:35
@tbkka
Copy link
Contributor Author

tbkka commented Oct 15, 2024

@swift-ci Please test

auto mpePointerSpareBits = TC.getBuilder().getMultiPayloadEnumPointerMask();
mask.andMask(mpePointerSpareBits, 0);
mask.keepOnlyLeastSignificantBytes(TC.targetPointerSize());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The truncation in the previous code here inadvertently marked everything beyond the first pointer as spare bits. This accidentally worked for 64-bit because MPEs pick the most-significant spare bit.

mask.keepOnlyLeastSignificantBytes(TC.targetPointerSize());
auto mpePointerSpareBitMask = BitMask(TC.targetPointerSize(), mpePointerSpareBits);
for (int offset = 0; offset < (int)getSize(); offset += TC.targetPointerSize()) {
mask.andMask(mpePointerSpareBitMask, offset);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the new code, we loop to apply the standard pointer spare bit mask to every pointer-sized field.

@@ -10,9 +10,6 @@
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: asan

// This is broken on ARM64_32, disable it temporarily until we can fix it. rdar://137351613
// UNSUPPORTED: CPU=arm64_32
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yay! It works for 32-bit watchOS now.

@tbkka tbkka requested review from mikeash and drexin October 15, 2024 20:04
Copy link
Contributor

@drexin drexin left a comment

Choose a reason for hiding this comment

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

LGTM

@tbkka
Copy link
Contributor Author

tbkka commented Oct 15, 2024

@swift-ci Please test macOS Platform

1 similar comment
@tbkka
Copy link
Contributor Author

tbkka commented Oct 16, 2024

@swift-ci Please test macOS Platform

The first word in a class existential is the class pointer itself.
This pointer exposes spare bits differently depending
on the platform, which becomes apparent when you try to reflect
an Optional carrying such an MPE.

Add new test cases and some logic to zero out the first
word of spare bit information only on platforms with 8-byte pointers.
// itself, which can be tagged or have other mysteries on 64-bit, so
// it exposes no spare bits from the first word there...
auto pointerBytes = TC.targetPointerSize();
if (pointerBytes == 8) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If anyone is trying to get RemoteMirror to work on non-Darwin, they may need to tweak this some more.

@tbkka
Copy link
Contributor Author

tbkka commented Oct 16, 2024

@swift-ci Please test

@tbkka
Copy link
Contributor Author

tbkka commented Oct 17, 2024

@swift-ci Please test

@tbkka tbkka merged commit 5bd2c4b into swiftlang:main Oct 17, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants