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

Prevent foreign Rust exceptions from being caught #102721

Merged
merged 7 commits into from
Oct 29, 2022
Merged

Conversation

nbdd0121
Copy link
Contributor

@nbdd0121 nbdd0121 commented Oct 5, 2022

Fix #102715

Use the address of a static variable (which is guaranteed to be unique per copy of std) to tell apart if a Rust exception comes from local or foreign Rust code, and abort for the latter.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Oct 5, 2022
@rustbot
Copy link
Collaborator

rustbot commented Oct 5, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 5, 2022
@@ -47,7 +47,12 @@ static EXCEPTION_TYPE_INFO: TypeInfo = TypeInfo {
name: b"rust_panic\0".as_ptr(),
Copy link
Member

@bjorn3 bjorn3 Oct 6, 2022

Choose a reason for hiding this comment

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

Maybe change this to avoid unsoundness in combination with older rust versions? Same for the exception type for the other unwinding mechanisms.

Copy link
Contributor Author

@nbdd0121 nbdd0121 Oct 6, 2022

Choose a reason for hiding this comment

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

I think it's worth mentioning that catching a foreign unwind in Rust is UB. We are providing guards against this but it's not part of guarantee.

Currently there are no ways to hit this on stable, and if you hit this using nightly you are already in UB territory. So we can change this string (and the exception class), but we don't have to.

library/panic_unwind/src/gcc.rs Show resolved Hide resolved
library/panic_unwind/src/gcc.rs Show resolved Hide resolved
@m-ou-se
Copy link
Member

m-ou-se commented Oct 11, 2022

r? @Amanieu

@rust-highfive rust-highfive assigned Amanieu and unassigned m-ou-se Oct 11, 2022
@Amanieu
Copy link
Member

Amanieu commented Oct 11, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Oct 11, 2022

📌 Commit c33cef6 has been approved by Amanieu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 11, 2022
Prevent foreign Rust exceptions from being caught

Fix rust-lang#102715

Use the address of a static variable (which is guaranteed to be unique per copy of std) to tell apart if a Rust exception comes from local or foreign Rust code, and abort for the latter.
@joboet
Copy link
Member

joboet commented Oct 12, 2022

I hope I am correct in doing this, but this should probably not be merged with the unsoundness above (I think that also caused the rollup to fail).

@bors r-

@bors
Copy link
Contributor

bors commented Oct 12, 2022

@joboet: 🔑 Insufficient privileges: Not in reviewers

@Amanieu
Copy link
Member

Amanieu commented Oct 12, 2022

Nice catch!

@bors r+

@bors
Copy link
Contributor

bors commented Oct 12, 2022

📌 Commit 65c7d94d94e575edc934de5081d674856baf67df has been approved by Amanieu

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Oct 13, 2022

⌛ Testing commit 65c7d94d94e575edc934de5081d674856baf67df with merge d3129761e1defe2e2c5878b84934fa9664e251a7...

@bors
Copy link
Contributor

bors commented Oct 13, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 13, 2022
@Dylan-DPC
Copy link
Member

failed in rollup

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 25, 2022
@nbdd0121
Copy link
Contributor Author

Hmm, I tested the test case on nightly i686-pc-windows-gnu and it doesn't work either. I am not familiar enough with windows-gnu targets to troubleshoot it, unfortunately.

However, given that the problem is not related to the main PR itself, I'll just mark the test as ignore-windows-gnu. Is that okay? @Amanieu

@Amanieu
Copy link
Member

Amanieu commented Oct 27, 2022

i686-mingw requires unwind tables to be registered with the unwinder when the module is loaded. We do this, but the problem in this case is that the cdylib has its own copy of libgcc statically linked into it. This is somewhat by design so that mingw executables don't have a runtime dependency on libgcc_s.dll, but it breaks cross-DLL unwinding since the respective unwinders are not aware of the unwind tables in other modules.

I think ignoring the test is fine in this case, but only on i686. Other architectures do not have this limitation.

@nbdd0121
Copy link
Contributor Author

I think ignoring the test is fine in this case, but only on i686. Other architectures do not have this limitation.

Yeah, I tested this on x86_64 MinGW and it works fine. However we only have ignore-x86 and ignore-windows-gnu, but not ignore-(x86 and windows-gnu). Which directive should I use?

@Amanieu
Copy link
Member

Amanieu commented Oct 27, 2022

You can pass a full target triple: // ignore-i686-pc-windows-gnu

@Amanieu
Copy link
Member

Amanieu commented Oct 27, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Oct 27, 2022

📌 Commit bfac2da has been approved by Amanieu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 27, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 28, 2022
Prevent foreign Rust exceptions from being caught

Fix rust-lang#102715

Use the address of a static variable (which is guaranteed to be unique per copy of std) to tell apart if a Rust exception comes from local or foreign Rust code, and abort for the latter.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 28, 2022
Prevent foreign Rust exceptions from being caught

Fix rust-lang#102715

Use the address of a static variable (which is guaranteed to be unique per copy of std) to tell apart if a Rust exception comes from local or foreign Rust code, and abort for the latter.
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 29, 2022
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#102634 (compiletest: Refactor test rustcflags)
 - rust-lang#102721 (Prevent foreign Rust exceptions from being caught)
 - rust-lang#103415 (filter candidates in pick probe for diagnostics)
 - rust-lang#103618 (Rename some `OwnerId` fields.)
 - rust-lang#103625 (Accept `TyCtxt` instead of `TyCtxtAt` in `Ty::is_*` functions)
 - rust-lang#103653 (Add missing impl blocks for item reexported from private mod in JSON output)
 - rust-lang#103699 (Emit proper error when casting to `dyn*`)
 - rust-lang#103719 (fix typo in `try_reserve` method from `HashMap` and `HashSet`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6dd64d3 into rust-lang:master Oct 29, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 29, 2022
@nbdd0121 nbdd0121 deleted the panic branch October 30, 2022 21:14
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Prevent foreign Rust exceptions from being caught

Fix rust-lang#102715

Use the address of a static variable (which is guaranteed to be unique per copy of std) to tell apart if a Rust exception comes from local or foreign Rust code, and abort for the latter.
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#102634 (compiletest: Refactor test rustcflags)
 - rust-lang#102721 (Prevent foreign Rust exceptions from being caught)
 - rust-lang#103415 (filter candidates in pick probe for diagnostics)
 - rust-lang#103618 (Rename some `OwnerId` fields.)
 - rust-lang#103625 (Accept `TyCtxt` instead of `TyCtxtAt` in `Ty::is_*` functions)
 - rust-lang#103653 (Add missing impl blocks for item reexported from private mod in JSON output)
 - rust-lang#103699 (Emit proper error when casting to `dyn*`)
 - rust-lang#103719 (fix typo in `try_reserve` method from `HashMap` and `HashSet`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unsoundness when a panic Rust code is caught by separetely compiled Rust code through FFI-unwind