From bb600d27d3e13b06dda14a1f488c409a868a8d05 Mon Sep 17 00:00:00 2001 From: increasing Date: Sat, 26 Sep 2026 21:27:48 +0200 Subject: [PATCH 1/8] add unsafe_global_asm rfc --- text/4014-unsafe-global-asm.md | 123 +++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 text/4014-unsafe-global-asm.md diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md new file mode 100644 index 00000000000..09611573f78 --- /dev/null +++ b/text/4014-unsafe-global-asm.md @@ -0,0 +1,123 @@ +- Feature Name: `unsafe_global_asm` +- Start Date: 2026-10-06 +- RFC PR: [rust-lang/rfcs#4014](https://github.com/rust-lang/rfcs/pull/4014) +- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) + +## Summary +[summary]: #summary + +Require `unsafe` for usage of the built-in `global_asm` macro. + +## Motivation +[motivation]: #motivation + +The `global_asm` macro can be used to cause undefined behaviour by overwriting symbols. +The use of this macro is currently not marked as unsafe in any way. +New syntax is required to close this unsoundness, because it is currently not possible to mark a macro +invocation as unsafe. + +## Guide-level explanation +[guide-level-explanation]: #guide-level-explanation + +When declaring a function in a `global_asm` macro like this + +```rust +global_asm!(" +.globl write +write: +ret +"); +``` +it will cause Rust to generate a globally visible function with the linker name `write`. +Code that wants to call the POSIX `write` function might call this one instead. +This can lead to UB if: +- the assembly does not follow the ABI of the `write` function correctly. +- the assembly behaves in a way that is incompatible with the contract of the `write` function. + +To avoid this `global_asm` must not declare functions or globals whose names clash with other functions or globals. +Since the compiler in general cannot check this it must be done by the programmer. +The syntax for discharging the safety obligation is the same as the syntax for unsafe trait implementations, where the `unsafe` +is also used to mark the safety obligations. + +```rust +/// SAFETY: there is no other global function named `my_own_write`. No other symbols are declared. +unsafe global_asm!(" +.globl my_own_write +my_own_write: +ret +"); +``` + +## Reference-level explanation +[reference-level-explanation]: #reference-level-explanation + +The `global_asm` macro is considered unsafe and (starting from the next edition) can only be invoked by +using the `unsafe` keyword. +Other macros can't be invoked in this way. + +To keep backwards compatibility using the `global_asm` macro without `unsafe` on older editions is not +a hard error, but is linted against. + +The (currently unimplemented) feature [`global_asm_in_statement_position`](https://github.com/rust-lang/rust/issues/156965) +allows using `global_asm` in more positions. It will be adjusted to follow the same syntax laid out here. +The `unsafe` keyword is required, even if `global_asm` is invoked in an unsafe block. +```rust +fn outer() { + unsafe { + global_asm!(""); // ERROR + } +} +``` + +Because `global_asm` is an item and not a statement this behaviour is the same as other items (unsafe trait +implementations and functions with unsafe attributes). + +## Drawbacks +[drawbacks]: #drawbacks + +- Disallowing the old syntax is a breaking change. +- It makes `global_asm` more special since users can't require the use of `unsafe` for their own macros. +- If unsafe macros would ever be allowed the syntax should be consistent, so this limits the possibilities. + +## Rationale and alternatives +[rationale-and-alternatives]: #rationale-and-alternatives + +- **Do Nothing**: This unsoundness stays open. +- **Disallow the old syntax on all editions**: This would break all rust code that uses `global_asm`. +- **Other syntax**: There are a couple of alternatives. + - It could be renamed to something like `unsafe_global_asm`. + - It could require `unsafe` as part of its syntax (`global_asm!(unsafe {".."})` or `global_asm!(unsafe "..")`) + - Unsafe blocks in item position could be added. + - It could require an `#[unsafe]` or `#[unsafe()]` attribute. + +The chosen syntax mirrors the one chosen for unsafe attributes in that it does not rename the problematic +item and in that it defines a syntax for unsafe macros. + +## Prior art +[prior-art]: #prior-art + +Especially [RFC 3324](https://github.com/rust-lang/rfcs/blob/master/text/3325-unsafe-attributes.md), which made `no_mangle` +and similar attributes require `unsafe` is very similar to this RFC. +In both cases +- the unsoundness is related to symbols of items. +- the problematic feature was stabilised a long time ago. +- new syntax had to be added to mark the unsafety. +- the new syntax could be extended to unsafe macros in the future. +- the old syntax is only disallowed on a new edition. + +The `unsafe_code` lint currently already fires on uses of `global_asm` with the note: +"using this macro is unsafe even though it does not need an `unsafe` block". +Something that is considered unsafe, but does not require an unsafe annotation to use is not a useful +concept. That `unsafe` marks *every* part of the code that requires special care is a big part of rusts safety story. + +## Unresolved questions +[unresolved-questions]: #unresolved-questions + +- While this syntax seems the most sensible to me i am not opposed to another syntax. +- **Rollout of the lint / if this should be done across all editions**: + I don't see much advantage of breaking this code on older editions. + +## Future possibilities +[future-possibilities]: #future-possibilities + +Users could be allowed to require `unsafe` for their own macros, which would reuse this syntax. From dddfbbb2ba69f6079ee0cab286d3bd7ad6a0efb4 Mon Sep 17 00:00:00 2001 From: increasing Date: Wed, 7 Oct 2026 08:44:24 +0200 Subject: [PATCH 2/8] unsoundness + argue against lint solution --- text/4014-unsafe-global-asm.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index 09611573f78..fb4a111f992 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -83,6 +83,8 @@ implementations and functions with unsafe attributes). [rationale-and-alternatives]: #rationale-and-alternatives - **Do Nothing**: This unsoundness stays open. + As [RFC 3324](https://github.com/rust-lang/rfcs/blob/master/text/3325-unsafe-attributes.md) argues, having the `unsafe_code` lint warn against this is not enough, + because the lint is opt-in, while Rust has "safety by default". - **Disallow the old syntax on all editions**: This would break all rust code that uses `global_asm`. - **Other syntax**: There are a couple of alternatives. - It could be renamed to something like `unsafe_global_asm`. From 4dd8c9f71e91e3ef8d7ad16b04c00187718700f9 Mon Sep 17 00:00:00 2001 From: increasing Date: Wed, 7 Oct 2026 09:02:21 +0200 Subject: [PATCH 3/8] rationale against #[unsafe] --- text/4014-unsafe-global-asm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index fb4a111f992..9440e1ac238 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -90,7 +90,7 @@ implementations and functions with unsafe attributes). - It could be renamed to something like `unsafe_global_asm`. - It could require `unsafe` as part of its syntax (`global_asm!(unsafe {".."})` or `global_asm!(unsafe "..")`) - Unsafe blocks in item position could be added. - - It could require an `#[unsafe]` or `#[unsafe()]` attribute. + - It could require an `#[unsafe]` or `#[unsafe()]` attribute. This syntax implies maybe even more generality, because one could expect `#[unsafe] impl Send for MyType {}` to work. The chosen syntax mirrors the one chosen for unsafe attributes in that it does not rename the problematic item and in that it defines a syntax for unsafe macros. From 26f7c956727cd53f125c2782e3ae0df3d995b2a8 Mon Sep 17 00:00:00 2001 From: increasing Date: Wed, 7 Oct 2026 10:22:20 +0200 Subject: [PATCH 4/8] more details in reference section --- text/4014-unsafe-global-asm.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index 9440e1ac238..35f1b9bd435 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -55,6 +55,12 @@ The `global_asm` macro is considered unsafe and (starting from the next edition) using the `unsafe` keyword. Other macros can't be invoked in this way. +```rust +unsafe global_asm!(""); // ERROR now +global_asm!(""); // ERROR on next edition +unsafe my_macro!(); // ERROR now and on next edition +``` + To keep backwards compatibility using the `global_asm` macro without `unsafe` on older editions is not a hard error, but is linted against. @@ -72,6 +78,15 @@ fn outer() { Because `global_asm` is an item and not a statement this behaviour is the same as other items (unsafe trait implementations and functions with unsafe attributes). +The syntax of [MacroItem](https://doc.rust-lang.org/nightly/reference/items.html#grammar-MacroItem) is changed to the following: +```grammar,items +MacroItem -> + `unsafe`? MacroInvocationSemi + | MacroRulesDefinition +``` + +Using `unsafe` for macros in non-item positions is still disallowed in the grammar. + ## Drawbacks [drawbacks]: #drawbacks @@ -122,4 +137,5 @@ concept. That `unsafe` marks *every* part of the code that requires special care ## Future possibilities [future-possibilities]: #future-possibilities -Users could be allowed to require `unsafe` for their own macros, which would reuse this syntax. +- Users could be allowed to require `unsafe` for their own macros, which would reuse this syntax. +- After that it could be allowed to use `unsafe` for more macros, not just item macros. From 4f515bb56e870c5a05145a6072c930f162cf5df9 Mon Sep 17 00:00:00 2001 From: increasing Date: Wed, 7 Oct 2026 10:04:23 +0200 Subject: [PATCH 5/8] more syntax arguments --- text/4014-unsafe-global-asm.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index 35f1b9bd435..4e67a395421 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -100,12 +100,12 @@ Using `unsafe` for macros in non-item positions is still disallowed in the gramm - **Do Nothing**: This unsoundness stays open. As [RFC 3324](https://github.com/rust-lang/rfcs/blob/master/text/3325-unsafe-attributes.md) argues, having the `unsafe_code` lint warn against this is not enough, because the lint is opt-in, while Rust has "safety by default". -- **Disallow the old syntax on all editions**: This would break all rust code that uses `global_asm`. +- **Disallow the old syntax on all editions**: This would break all Rust code that uses `global_asm`. - **Other syntax**: There are a couple of alternatives. - - It could be renamed to something like `unsafe_global_asm`. - - It could require `unsafe` as part of its syntax (`global_asm!(unsafe {".."})` or `global_asm!(unsafe "..")`) - - Unsafe blocks in item position could be added. - - It could require an `#[unsafe]` or `#[unsafe()]` attribute. This syntax implies maybe even more generality, because one could expect `#[unsafe] impl Send for MyType {}` to work. + - It could be renamed to something like `unsafe_global_asm`. This is less helpful when the macro is imported with a different name. + - It could require `unsafe` as part of its syntax (`global_asm!(unsafe {".."})` or `global_asm!(unsafe "..")`). + - Unsafe blocks in item position could be added. This is a big change that would require it's own RFC. I don't think `global_asm` is enough motivation for this. + - It could require an `#[unsafe]` or `#[unsafe()]` attribute, which wouldn't require the grammar of Rust to change. This syntax implies maybe even more generality, because one could expect `#[unsafe] impl Send for MyType {}` to work. The chosen syntax mirrors the one chosen for unsafe attributes in that it does not rename the problematic item and in that it defines a syntax for unsafe macros. From f1c60b2e5f5e2eedf351f15cd1bdbcba4f3bf2dd Mon Sep 17 00:00:00 2001 From: increasing Date: Wed, 7 Oct 2026 18:37:44 +0200 Subject: [PATCH 6/8] reword invoke with unsafe --- text/4014-unsafe-global-asm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index 4e67a395421..850272758cb 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -51,8 +51,8 @@ ret ## Reference-level explanation [reference-level-explanation]: #reference-level-explanation -The `global_asm` macro is considered unsafe and (starting from the next edition) can only be invoked by -using the `unsafe` keyword. +The `global_asm` macro is considered unsafe and (starting from the next edition) can only be invoked if the `unsafe` keyword appears +immediately before it. Other macros can't be invoked in this way. ```rust From eb78e5ea576f5953defb7a7d88cf51cec9d00fc5 Mon Sep 17 00:00:00 2001 From: increasing Date: Sun, 11 Oct 2026 09:12:16 +0200 Subject: [PATCH 7/8] possibilities for future unsafe macros --- text/4014-unsafe-global-asm.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index 850272758cb..bdb94b2d14b 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -137,5 +137,11 @@ concept. That `unsafe` marks *every* part of the code that requires special care ## Future possibilities [future-possibilities]: #future-possibilities -- Users could be allowed to require `unsafe` for their own macros, which would reuse this syntax. -- After that it could be allowed to use `unsafe` for more macros, not just item macros. +Users could be allowed to require `unsafe` for their own macros, which would at least partially reuse this syntax. +This RFC implies that it should be possible to invoke unsafe macros in item positions and that this should use the `unsafe my_macro!();` syntax. +Otherwise all directions, especially in regard to interaction with unsafe blocks stay open. + +- If unsafe macros in expression positions should use the same syntax or require an `unsafe` block can be decided later. +- If unsafe macros should have to either expand to items or statements, but not both to differentiate between having to use an unsafe block or `unsafe my_macro!()` + can be decided later. + From 35adb4459626892d1c8ebb1551ef8c7eb073e971 Mon Sep 17 00:00:00 2001 From: increasing Date: Tue, 13 Oct 2026 11:30:29 +0200 Subject: [PATCH 8/8] searchability --- text/4014-unsafe-global-asm.md | 1 + 1 file changed, 1 insertion(+) diff --git a/text/4014-unsafe-global-asm.md b/text/4014-unsafe-global-asm.md index bdb94b2d14b..f54da5e4934 100644 --- a/text/4014-unsafe-global-asm.md +++ b/text/4014-unsafe-global-asm.md @@ -93,6 +93,7 @@ Using `unsafe` for macros in non-item positions is still disallowed in the gramm - Disallowing the old syntax is a breaking change. - It makes `global_asm` more special since users can't require the use of `unsafe` for their own macros. - If unsafe macros would ever be allowed the syntax should be consistent, so this limits the possibilities. +- This syntax is harder to search for than the currently existing markers for unsafe code. ## Rationale and alternatives [rationale-and-alternatives]: #rationale-and-alternatives