Skip to content

Commit 7a9106b

Browse files
rcseacordPLeVasseur
authored andcommitted
Update guidelines on unsafe attribute usage in Rust
Clarify issues with using #[export_name] and #[link_section] attributes without unsafe wrappers in Rust 2024.
1 parent afa1cf4 commit 7a9106b

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/coding-guidelines/attributes/gui_ZDLZzjeOwLSU.rst.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,22 @@
128128
The ``#[export_name]`` and ``#[link_section]`` attributes can cause undefined behavior if misused,
129129
as they affect symbol resolution and memory layout at link time.
130130
Without the ``unsafe`` keyword, these hazards are not visible to reviewers or tools.
131+
132+
This noncompliant example has two problems.
133+
First, it uses an ``#[export_name]`` attribute without an unsafe wrapper.
134+
This attribute controls the symbol name used during linking.
135+
If another symbol with the same name exists, it causes undefined behavior.
136+
Rust 2024 requires it to be marked ``unsafe``.
137+
138+
The second problem is that this noncompliant example uses a ``#[link_section]`` attribute without unsafe wrapper.
139+
This attribute places the item in a specific linker section.
140+
Incorrect section placement can cause undefined behavior
141+
(e.g., placing mutable data in read-only sections, or interfering with special sections like ``.init``).
142+
131143
This noncompliant example requires Rust Edition 2021 or earlier to compile.
132144
133145
.. rust-example::
146+
:compile_fail:
134147
135148
// Missing unsafe marker - noncompliant in Rust 2024
136149
#[export_name = "custom_symbol"]

0 commit comments

Comments
 (0)