Skip to content

panic runtime and C-unwind documentation #1226

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 42 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5f63e46
panic runtime and C-unwind documentation
BatmanAoD Oct 2, 2024
c8fc0e8
Rust version number for destructors guarantee
BatmanAoD Nov 24, 2024
edd5b18
Fix identifeirs in functions.md and move the guarantee about destruct…
chorman0773 Nov 25, 2024
39de76c
Add identifier syntax to panic.md
chorman0773 Nov 25, 2024
6413901
Move note in linkage.md
chorman0773 Nov 25, 2024
70f8292
Fix whitespace issues in previous commit
chorman0773 Nov 25, 2024
d08e406
Fix omitted whitespace issue (thanks VSC)
chorman0773 Nov 25, 2024
a20802e
Update src/items/functions.md
chorman0773 Nov 26, 2024
2525ab4
fix wording regarding UB due to unwinding linkage
RalfJung Nov 29, 2024
5bff1e3
improve terminology
RalfJung Dec 21, 2024
7453efd
Do not document a rustc-lint as a language rule
ehuss Jan 10, 2025
c3cc7fa
Use built-in std linking
ehuss Jan 10, 2025
860691e
Fix misspelled word
ehuss Jan 10, 2025
543bc27
Remove vectorcall-unwind, it is unstable
ehuss Jan 14, 2025
4bb0248
Sort unwind list
ehuss Jan 14, 2025
386b2f3
Switch link for ABI to the list of ABIs
ehuss Jan 14, 2025
c7a7961
Tweak wording of "beyond rust main function"
ehuss Jan 14, 2025
20af0b1
Make sure a terminal rule is used for this definition
ehuss Jan 14, 2025
da53531
Remove historical note
ehuss Jan 14, 2025
8fd8208
Clarify that this is unspecified
ehuss Jan 14, 2025
0f3c015
Fix rule name to follow style
ehuss Jan 14, 2025
63677c1
Add missing rule name
ehuss Jan 14, 2025
caa17fc
Reword the title of this section
ehuss Jan 14, 2025
0b37538
Unwrap the panic chapter
ehuss Jan 14, 2025
eafe95d
Update rules to be with headers
ehuss Jan 14, 2025
5f21a1a
Apply suggestions from RalfJung
ehuss Jan 14, 2025
79cc53f
Move panic runtimes towards the top of the chapter
ehuss Jan 15, 2025
c38044d
Add the concept of panic strategy
ehuss Jan 15, 2025
20bd38f
Fix typo
ehuss Jan 15, 2025
f309b2f
Drop sentence about UB of how to violate the Rust runtime
ehuss Jan 15, 2025
2617cd5
Add "panic" to "unwind runtime"
ehuss Jan 16, 2025
d49c90c
Clarify optimization for abort strategy
ehuss Jan 16, 2025
6eaa1fa
Add another link for panic strategy
ehuss Jan 16, 2025
a740002
Normatively define the abort and unwind runtimes
ehuss Jan 16, 2025
f7cb25e
Rework link.unwinding
ehuss Jan 16, 2025
34e0b68
Apply suggestions from RalfJung
ehuss Jan 17, 2025
8f4dc2b
Clarify function pointer variadics also support `-unwind`
ehuss Jan 21, 2025
6e76b5c
Fix minor typo
ehuss Jan 30, 2025
7049886
Simplify wording of standard ABI strings
ehuss Feb 10, 2025
003a8a4
Remove "panic runtime" and emphasize panic_handler more
ehuss Feb 10, 2025
cdd00de
no unnecessary line-breaks in new text
BatmanAoD Feb 15, 2025
ffc056c
fix link
BatmanAoD Feb 16, 2025
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
Prev Previous commit
Next Next commit
fix wording regarding UB due to unwinding linkage
  • Loading branch information
RalfJung authored and BatmanAoD committed Feb 15, 2025
commit 2525ab4b6a32502fa9914afa0909bcfb125a40cc
62 changes: 33 additions & 29 deletions src/linkage.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,39 @@ binary link:

Passing `rlib`s directly into your foreign linker is currently unsupported.

### Prohibited linkage and foreign unwinding

r[link.foreign-code.prohibited]

Undfined behavior may be caused by foreign code unwinding into a Rust crate
with these characteristics:

* The foreign unwind enters Rust via a function or function pointer declared
with an ABI that permits unwinding, that is, `"Rust"` (the default ABI) or
any `-unwind` ABI
* The Rust crate containing the `-unwind` ABI declaration was compiled with
`panic=unwind`
* The final binary is linked with [the `panic=abort` runtime][panic-runtime]

> **Note**: To protect against this undefined behavior, `rustc` does not permit
> linking the `panic=abort` runtime against any crate that was compiled with
> `panic=unwind` if that crate also contains a call to a foreign function or
> function pointer declared with an `-unwind` ABI. Note that this prohibition
> applies even when linking a static or dynamic library that only includes Rust
> code, since the resulting library may be subsequently linked against another
> library that may unwind. However, use of the `Rust` (default) ABI does not
> cause a link-error, since that ABI is not expected to be used as an
> entrypoint into a static or shared library.

> **Note**: the restriction can only be violated when mixing code with different
> `-C panic` flags. This is not possible in normal use of cargo, so most users
> need not be concerned about this.

r[link.foreign-code.prohibited.lint.ffi_unwind_calls]
> [!NOTE]
> Rust code compiled or linked with a different instance of the Rust runtime counts as a
> "foreign code" for the purpose of this section.

### Prohibited linkage and unwinding

r[link.unwinding]
If you are *not* using `rustc` to link Rust files, you must take care to ensure that unwinding is
handled consistently across the entire binary. This includes using `dlopen` or similar facilities
where linking is done by the system runtime without `rustc` being involved.

r[link.unwinding.potential]
A Rust binary or `staticlib` is called *potentially unwinding* if any of the following conditions
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a name for "the final artifact of Rust linkage, with a single Rust runtime included"? Always saying "Rust binary or staticlib" is awkward...

Copy link
Member

Choose a reason for hiding this comment

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

I believe this also needs to account for cdylib?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah we really need a proper term for this, not an enumeration. "final Rust artifact"? Or can we say "Rust binary" since staticlib and cdylib are binaries but in some sense rlib is not? Not sure if that makes any sense...

Copy link
Member

Choose a reason for hiding this comment

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

@bjorn3 maybe you have a good idea for what to call this?

Copy link
Member

@bjorn3 bjorn3 Dec 1, 2024

Choose a reason for hiding this comment

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

I don't have a great name for it either unfortunately. bin, dylib, cdylib and proc-macro are all linked crates, but staticlib isn't.

Copy link
Member

Choose a reason for hiding this comment

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

I decided to just directly talk about artifacts and whether they share the same copy of the Rust runtime.

is met:
- The binary or `staticlib` is linked with [the `panic=unwind` runtime][panic-runtime].
- The binary or `staticlib` contains a crate built with `-Cpanic=unwind` that makes a call
to a function using a `-unwind` ABI.
- The binary or `staticlib` makes a `"Rust"` ABI call to code running in a separately built
`staticlib` (i.e., a separate instance of the Rust runtime), and that other `staticlib` is
potentially unwinding.

> [!NOTE]
> This definition captures whether a `"Rust"` ABI call inside a Rust binary or `staticlib` can ever
> unwind.

r[link.unwinding.prohibited]
If a Rust binary or `staticlib` is potentially unwinding, then all its crates must be built with `-Cpanic=unwind`.

> [!NOTE]
> This restriction can only be violated when mixing code with different `-C panic` flags
> while also using a non-`rustc` linker. Most users to not have to be concerned about this.

r[link.unwinding.prohibited.lint.ffi_unwind_calls]
To guarantee that a library will be sound (and linkable with `rustc`)
regardless of the panic mode used at link-time, the [`ffi_unwind_calls` lint]
may be used. The lint flags any calls to `-unwind` foreign functions or
Expand Down
2 changes: 1 addition & 1 deletion src/panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ which will occur:
* The function returns a `Result::Err` containing an opaque type.

> [!NOTE]
> Rust code compiled or linked with a different runtime counts as a
> Rust code compiled or linked with a different instance of the Rust runtime counts as a
> "foreign exception" for the purpose of this guarantee. Thus, a library that
> uses `panic!` and is linked against one version of the Rust standard library,
> invoked from an application that uses a different version of the standard
Expand Down