Skip to content

[Runtime][IRGen] Trap C++ exceptions on *throw*, not catch. #71056

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 4 commits into from
Feb 8, 2024

Conversation

al45tair
Copy link
Contributor

The previous approach was effectively to catch the exception and then run a trap instruction. That has the unfortunate feature that we end up with a crash at the catch site, not at the throw site, which leaves us with very little information about which exception was thrown or where from.

(Strictly we do have the exception pointer and could obtain exception information, but it still won't tell us what threw it.)

Instead of that, set a personality function for Swift functions that call potentially throwing code, and have that personality function trap the exception during phase 1 (i.e. before the original stack has been unwound).

rdar://120952971

@al45tair al45tair requested a review from mikeash as a code owner January 22, 2024 11:29
@al45tair
Copy link
Contributor Author

We might need to do something to make sure that the Linux build still works for people while we're landing this — building with host tools is probably going to cause a problem because of the extra API that the compiler is using.

@al45tair
Copy link
Contributor Author

@swift-ci Please test

@egorzhdan egorzhdan requested a review from hyp January 22, 2024 16:39
@mikeash
Copy link
Contributor

mikeash commented Jan 22, 2024

Someday, can we emit this unconditionally to prevent people trying to throw through Swift frames and catch them on the other side?

@al45tair al45tair requested review from a team, zoecarver and egorzhdan as code owners January 23, 2024 16:52
@al45tair
Copy link
Contributor Author

@swift-ci Please test

@al45tair
Copy link
Contributor Author

There's also an issue in that gating on availability means the test needs to pass the availability flags. I'm going to fix the availability situation on Linux and then come back to this PR.

@al45tair al45tair marked this pull request as draft February 2, 2024 12:00
@al45tair
Copy link
Contributor Author

al45tair commented Feb 2, 2024

@swift-ci Please smoke test

@al45tair
Copy link
Contributor Author

al45tair commented Feb 2, 2024

@swift-ci Please smoke test

@al45tair al45tair force-pushed the eng/PR-120952971 branch 3 times, most recently from 89ee1f5 to 16f27d6 Compare February 2, 2024 22:31
@al45tair
Copy link
Contributor Author

al45tair commented Feb 2, 2024

@swift-ci Please smoke test

@al45tair al45tair marked this pull request as ready for review February 3, 2024 09:47
@al45tair al45tair requested a review from tshortli as a code owner February 3, 2024 09:47
@al45tair al45tair requested review from Azoy and mikeash February 3, 2024 09:48
The previous approach was effectively to catch the exception and then
run a trap instruction.  That has the unfortunate feature that we end
up with a crash at the catch site, not at the throw site, which leaves
us with very little information about which exception was thrown or
where from.

(Strictly we do have the exception pointer and could obtain exception
information, but it still won't tell us what threw it.)

Instead of that, set a personality function for Swift functions that
call potentially throwing code, and have that personality function
trap the exception during phase 1 (i.e. *before* the original stack
has been unwound).

rdar://120952971
The exception handling tests should check for the new personality, and
the ABI check needs to know about the new entry point in the runtime.

rdar://120952971
The other new runtime functions appear to have a leading underscore.
It makes sense in this case because we don't expect anything to call
this directly (other than the unwinder).

rdar://120952971
We need to only generate references to `_swift_exceptionPersonality`
if we're building for a new enough runtime.  The previous code was
good on Darwin, but would have resulted in build problems on Linux.

rdar://120952971
@al45tair
Copy link
Contributor Author

al45tair commented Feb 6, 2024

Rebased.

@al45tair
Copy link
Contributor Author

al45tair commented Feb 6, 2024

@swift-ci Please smoke test

Copy link
Contributor

@hyp hyp left a comment

Choose a reason for hiding this comment

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

Thanks, that should be fine

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.

4 participants