Skip to content

Commit 63441ea

Browse files
committed
add rfc text
1 parent 2b78d7b commit 63441ea

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

text/0000-unsafe-extern-blocks.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
- Feature Name: `unsafe_extern`
3+
- Start Date: 2023-05-23
4+
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000)
5+
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000)
6+
7+
# Summary
8+
[summary]: #summary
9+
10+
In Edition 2024 it is `unsafe` to declare an `extern` function or static, but external functions and statics *can* be safe to use after the initial declaration.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
Simply declaring extern items, even without ever using them, can cause Undefined Behavior.
16+
When performing cross-language compilation, attributes on one function declaration can flow to the foreign declaration elsewhere within LLVM and cause a miscompilation.
17+
In Rust we consider all sources of Undefined Behavior to be `unsafe`, and so we must make declaring extern blocks be `unsafe`.
18+
The up-side to this change is that in the new style it will be possible to declare an extern fn that's safe to call after the initial unsafe declaration.
19+
20+
# Guide-level explanation
21+
[guide-level-explanation]: #guide-level-explanation
22+
23+
Rust can utilize functions and statics from foreign code that are provided during linking, though it is `unsafe` to do so.
24+
25+
An `extern` block can be placed anywhere a function declaration could appear (generally at the top level of a module), and must always be prefixed with the keyword `unsafe`.
26+
27+
Within the block you can declare the exernal functions and statics that you want to make visible within the current scope.
28+
Each function declaration gives only the function's signature, similar to how methods for traits are declared.
29+
If calling a foreign function is `unsafe` then you must declare the function as `unsafe fn`, otherwise you can declare it as a normal `fn`.
30+
Each static declaration gives the name and type, but no initial value.
31+
32+
* If the `unsafe_code` lint is denied or forbidden at a particular scope it will cause the `unsafe extern` block to be a compilation error within that scope.
33+
* Declaring an incorrect external item signature can cause Undefined Behavior during compilation, even if Rust never accesses the item.
34+
35+
```rust
36+
unsafe extern {
37+
// sqrt (from libm) can be called with any `f64`
38+
pub fn sqrt(x: f64) -> f64;
39+
40+
// strlen (from libc) requires a valid pointer,
41+
// so we mark it as being an unsafe fn
42+
pub unsafe fn strlen(p: *const c_char) -> usize;
43+
44+
pub static IMPORTANT_BYTES: [u8; 256];
45+
46+
pub static LINES: UnsafeCell<i32>;
47+
}
48+
```
49+
50+
Note: other rules for extern blocks, such as optionally including an ABI, are unchanged from previous editions, so those parts of the guide would remain.
51+
52+
# Reference-level explanation
53+
[reference-level-explanation]: #reference-level-explanation
54+
55+
This adjusts the grammar of the language to *require* the `unsafe` keyword before an `extern` block declaration (currently it's optional and syntatically allowed but semantically rejected).
56+
57+
Replace the *Functions* and *Statics* sections with the following:
58+
59+
### Functions
60+
Functions within external blocks are declared in the same way as other Rust functions, with the exception that they must not have a body and are instead terminated by a semicolon. Patterns are not allowed in parameters, only IDENTIFIER or _ may be used. The function qualifiers `const`, `async`, and `extern` are not allowed. If the function is unsafe to call, then the function must use the `unsafe` qualifier.
61+
62+
If the function signature declared in Rust is incompatible with the function signature as declared in the foreign code it is Undefined Behavior.
63+
64+
Functions within external blocks may be called by Rust code, just like functions defined in Rust. The Rust compiler will automatically use the correct foreign ABI when making the call.
65+
66+
When coerced to a function pointer, a function declared in an extern block has type
67+
```rust
68+
extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R
69+
```
70+
where `'l1`, ... `'lm` are its lifetime parameters, `A1`, ..., `An` are the declared types of its parameters and `R` is the declared return type.
71+
72+
### Statics
73+
Statics within external blocks are declared in the same way as statics outside of external blocks, except that they do not have an expression initializing their value. It is unsafe to declare a static item in an extern block, whether or not it's mutable, because there is nothing guaranteeing that the bit pattern at the static's memory is valid for the type it is declared with.
74+
75+
Extern statics can be either immutable or mutable just like statics outside of external blocks. An immutable static must be initialized before any Rust code is executed. It is not enough for the static to be initialized before Rust code reads from it. A mutable extern static is unsafe to access, the same as a Rust mutable static.
76+
77+
# Drawbacks
78+
[drawbacks]: #drawbacks
79+
80+
* It is very unfortunate to have to essentially reverse the status quo.
81+
* Hopefully, allowing people to safely call some foreign functions will make up for the churn caused by this change.
82+
83+
# Rationale and alternatives
84+
[rationale-and-alternatives]: #rationale-and-alternatives
85+
86+
Incorrect extern declarations can cause UB in current Rust, but we have no way to automatically check that all declarations are correct, nor is such a thing likely to be developed. Making the declarations `unsafe` so that programmers are aware of the dangers and can give extern blocks the attention they deserve is the minimum step.
87+
88+
# Prior art
89+
[prior-art]: #prior-art
90+
91+
None we are aware of.
92+
93+
# Unresolved questions
94+
[unresolved-questions]: #unresolved-questions
95+
96+
* Extern declarations are actually *always* unsafe and able to cause UB regardless of edition. This RFC doesn't have a specific answer on how to improve pre-2024 code.
97+
98+
# Future possibilities
99+
[future-possibilities]: #future-possibilities
100+
101+
None are apparent at this time.

0 commit comments

Comments
 (0)