| 1 |
# Life-Before-Main and Other Link-Time Hazards |
| 2 |
|
| 3 |
The linker is a powerful tool that can be used to perform a number of tricks, |
| 4 |
but you, the developer, should be aware of the hazards that come with this |
| 5 |
low-level control. |
| 6 |
|
| 7 |
Rust is a systems programming language that is designed to be safe, and |
| 8 |
link-time tools may bend or break some safety guarantees. |
| 9 |
|
| 10 |
- [Rustβs model vs `ctor`/`dtor`](#rusts-model-vs-ctordtor) |
| 11 |
- [Panic handling](#panic-handling) |
| 12 |
- [I/O and the Standard Library](#io-and-the-standard-library) |
| 13 |
- [Aggressive Linker Garbage Collection](#aggressive-linker-garbage-collection) |
| 14 |
- [cdylib lifecycle](#cdylib-lifecycle) |
| 15 |
|
| 16 |
## Rustβs model vs `ctor`/`dtor` |
| 17 |
|
| 18 |
Rustβs usual model is that nothing runs before or after `main`. The `ctor` and |
| 19 |
`dtor` crates deliberately subvert that |
| 20 |
([*UCG* #598](https://github.com/rust-lang/unsafe-code-guidelines/issues/598)). |
| 21 |
|
| 22 |
Code inside `#[ctor]` and `#[dtor]` should favor **`libc`-level APIs** and logic |
| 23 |
that does **not depend on the Rust standard library** having finished |
| 24 |
initializing (constructors) or still being valid (destructors). |
| 25 |
|
| 26 |
[*UCG* #598](https://github.com/rust-lang/unsafe-code-guidelines/issues/598) β Rust `unsafe-code-guidelines`; discussion thread on lifecycle and unsafe-code implications around execution outside `main`. |
| 27 |
|
| 28 |
### Panic handling |
| 29 |
|
| 30 |
Panic handlers may not be set up in early code, so a `panic!()` may not be |
| 31 |
catchable, or may even result in undefined behavior. Generally, code that runs |
| 32 |
before main must take great pains not to panic ([π¦ |
| 33 |
#97049](https://github.com/rust-lang/rust/issues/97049), [π¦ |
| 34 |
#107381](https://github.com/rust-lang/rust/issues/107381), [π¦ |
| 35 |
#86030](https://github.com/rust-lang/rust/issues/86030)). |
| 36 |
|
| 37 |
_References_ |
| 38 |
|
| 39 |
[π¦ #97049](https://github.com/rust-lang/rust/issues/97049) β `rust-lang/rust`; Miri and discussion of panicking inside `#[start]` before the runtime can catch unwinding. |
| 40 |
|
| 41 |
[π¦ #107381](https://github.com/rust-lang/rust/issues/107381) β `rust-lang/rust`; unwinding and whether escaping `lang_start` is undefined behavior. |
| 42 |
|
| 43 |
[π¦ #86030](https://github.com/rust-lang/rust/issues/86030) β `rust-lang/rust`; `lang_start` soundness when panic payloads panic during drop (`failed to initiate panic`). |
| 44 |
|
| 45 |
### I/O and the Standard Library |
| 46 |
|
| 47 |
`std::io` is known to be problematic after main |
| 48 |
([π¦ #29488](https://github.com/rust-lang/rust/issues/29488), |
| 49 |
[SO #35980148](https://stackoverflow.com/questions/35980148/why-does-an-atexit-handler-panic-when-it-accesses-stdout)). |
| 50 |
`println!` uses thread-local stdout machinery which may not be initialized yet, or may be |
| 51 |
closed/broken after `main` exits. The |
| 52 |
[`libc_print`](https://crates.io/crates/libc-print) crate provides a safe |
| 53 |
alternative for this that uses `libc` functions directly and will not panic if |
| 54 |
used before or after `main`. |
| 55 |
|
| 56 |
The standard library does not make any particular guarantees about the state of |
| 57 |
the system after `main` exits or before it starts and code that works in one |
| 58 |
version of Rust may or may not work in another. |
| 59 |
|
| 60 |
_References_ |
| 61 |
|
| 62 |
[π¦ #29488](https://github.com/rust-lang/rust/issues/29488) β `rust-lang/rust`; `println!` panics from `Drop` / TLS (`cannot access stdout during shutdown`). |
| 63 |
|
| 64 |
[SO #35980148](https://stackoverflow.com/questions/35980148/why-does-an-atexit-handler-panic-when-it-accesses-stdout) β `println!` / stdout access from a libc `atexit` handler vs Rust runtime teardown order. |
| 65 |
|
| 66 |
## Aggressive Linker Garbage Collection |
| 67 |
|
| 68 |
Some linker configurations can **strip** the underlying registration data for |
| 69 |
`#[ctor]`, `#[dtor]` and `#[in_section]` registrations from the final binary, |
| 70 |
resulting in them not being called |
| 71 |
([rust-ctor #280](https://github.com/mmastrac/linktime/issues/280), |
| 72 |
[π¦ #99721](https://github.com/rust-lang/rust/issues/99721)). |
| 73 |
|
| 74 |
Building with `--cfg linktime_used_linker` for the `ctor` and `dtor` crates |
| 75 |
_may_ help β it applies `used(linker)` to the linker-generated items, but it |
| 76 |
requires nightly Rust and `#![feature(used_with_arg)]` on the crate root. |
| 77 |
|
| 78 |
Often a **`use` of the module** that contains the missing registration is enough |
| 79 |
for the linker to retain the code. |
| 80 |
|
| 81 |
_References_ |
| 82 |
|
| 83 |
[rust-ctor #280](https://github.com/mmastrac/linktime/issues/280) β `mmastrac/linktime`; linker / LTO stripping ctor registrations. |
| 84 |
|
| 85 |
[π¦ #99721](https://github.com/rust-lang/rust/issues/99721) β `rust-lang/rust`; rustc / linkage behavior relevant to similar stripping (`used`, linker GC). |
| 86 |
|
| 87 |
## `cdylib` lifecycle |
| 88 |
|
| 89 |
A `cdylib` is a dynamic library that is loaded (and potentially unloaded) at |
| 90 |
runtime, independent of the main executable. |
| 91 |
|
| 92 |
On some platforms, **unloading a shared library may not occur when you expect**; |
| 93 |
behavior can be deferred until **process exit**. The rules are described as |
| 94 |
arcane. **Thread-local storage on macOS** is called out as influencing this; see |
| 95 |
[this comment on π¦ #28794](https://github.com/rust-lang/rust/issues/28794#issuecomment-368693049). |
| 96 |
|
| 97 |
Care should be taken to ensure that the `#[dtor]` functions are called before |
| 98 |
the library is unloaded. While the `#[dtor]` macro supports registering |
| 99 |
"termination" functions - which are called when the main binary process |
| 100 |
terminates - inside of `cdylib`s, it is not recommended to use them as the code |
| 101 |
that will perform the cleanup may have been unloaded and unmapped from memory, |
| 102 |
causing random crashes. |
| 103 |
|
| 104 |
_References_ |
| 105 |
|
| 106 |
[π¦ #28794 (comment)](https://github.com/rust-lang/rust/issues/28794#issuecomment-368693049) β `rust-lang/rust`; thread-local storage and dynamic-library unload behavior on macOS. |