Skip to content

Commit 0d62552

Browse files
RalfJungehuss
authored andcommitted
fix wording regarding UB due to unwinding linkage
1 parent 6ef801b commit 0d62552

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/linkage.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -271,35 +271,39 @@ binary link:
271271

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

274-
### Prohibited linkage and foreign unwinding
275-
276-
r[link.foreign-code.prohibited]
277-
278-
Undfined behavior may be caused by foreign code unwinding into a Rust crate
279-
with these characteristics:
280-
281-
* The foreign unwind enters Rust via a function or function pointer declared
282-
with an ABI that permits unwinding, that is, `"Rust"` (the default ABI) or
283-
any `-unwind` ABI
284-
* The Rust crate containing the `-unwind` ABI declaration was compiled with
285-
`panic=unwind`
286-
* The final binary is linked with [the `panic=abort` runtime][panic-runtime]
287-
288-
> **Note**: To protect against this undefined behavior, `rustc` does not permit
289-
> linking the `panic=abort` runtime against any crate that was compiled with
290-
> `panic=unwind` if that crate also contains a call to a foreign function or
291-
> function pointer declared with an `-unwind` ABI. Note that this prohibition
292-
> applies even when linking a static or dynamic library that only includes Rust
293-
> code, since the resulting library may be subsequently linked against another
294-
> library that may unwind. However, use of the `Rust` (default) ABI does not
295-
> cause a link-error, since that ABI is not expected to be used as an
296-
> entrypoint into a static or shared library.
297-
298-
> **Note**: the restriction can only be violated when mixing code with different
299-
> `-C panic` flags. This is not possible in normal use of cargo, so most users
300-
> need not be concerned about this.
301-
302-
r[link.foreign-code.prohibited.lint.ffi_unwind_calls]
274+
> [!NOTE]
275+
> Rust code compiled or linked with a different instance of the Rust runtime counts as a
276+
> "foreign code" for the purpose of this section.
277+
278+
### Prohibited linkage and unwinding
279+
280+
r[link.unwinding]
281+
If you are *not* using `rustc` to link Rust files, you must take care to ensure that unwinding is
282+
handled consistently across the entire binary. This includes using `dlopen` or similar facilities
283+
where linking is done by the system runtime without `rustc` being involved.
284+
285+
r[link.unwinding.potential]
286+
A Rust binary or `staticlib` is called *potentially unwinding* if any of the following conditions
287+
is met:
288+
- The binary or `staticlib` is linked with [the `panic=unwind` runtime][panic-runtime].
289+
- The binary or `staticlib` contains a crate built with `-Cpanic=unwind` that makes a call
290+
to a function using a `-unwind` ABI.
291+
- The binary or `staticlib` makes a `"Rust"` ABI call to code running in a separately built
292+
`staticlib` (i.e., a separate instance of the Rust runtime), and that other `staticlib` is
293+
potentially unwinding.
294+
295+
> [!NOTE]
296+
> This definition captures whether a `"Rust"` ABI call inside a Rust binary or `staticlib` can ever
297+
> unwind.
298+
299+
r[link.unwinding.prohibited]
300+
If a Rust binary or `staticlib` is potentially unwinding, then all its crates must be built with `-Cpanic=unwind`.
301+
302+
> [!NOTE]
303+
> This restriction can only be violated when mixing code with different `-C panic` flags
304+
> while also using a non-`rustc` linker. Most users to not have to be concerned about this.
305+
306+
r[link.unwinding.prohibited.lint.ffi_unwind_calls]
303307
To guarantee that a library will be sound (and linkable with `rustc`)
304308
regardless of the panic mode used at link-time, the [`ffi_unwind_calls` lint]
305309
may be used. The lint flags any calls to `-unwind` foreign functions or

src/panic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ which will occur:
8484
* The function returns a `Result::Err` containing an opaque type.
8585

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

0 commit comments

Comments
 (0)