Skip to content

Commit 2bd4470

Browse files
committed
Add some basic docs for unsafe attrs
1 parent dad8233 commit 2bd4470

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/abi.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ item's name.
6666
Additionally, the item will be publicly exported from the produced library or
6767
object file, similar to the [`used` attribute](#the-used-attribute).
6868

69+
This attribute is unsafe as an unmangled symbol may collide with another
70+
symbol with the same name (or a well-known symbol), leading to undefined behavior.
71+
6972
## The `link_section` attribute
7073

7174
The *`link_section` attribute* specifies the section of the object file that a
7275
[function] or [static]'s content will be placed into. It uses the
7376
[_MetaNameValueStr_] syntax to specify the section name.
7477

78+
This attribute is unsafe as using `link_section` allows users to place data
79+
and code into sections of memory not expecting them, such as mutable data
80+
into read-only areas.
81+
7582
<!-- no_run: don't link. The format of the section name is platform-specific. -->
7683
```rust,no_run
7784
#[no_mangle]
@@ -85,6 +92,9 @@ The *`export_name` attribute* specifies the name of the symbol that will be
8592
exported on a [function] or [static]. It uses the [_MetaNameValueStr_] syntax
8693
to specify the symbol name.
8794

95+
This attribute is unsafe as a symbol with a custom name may collide with another
96+
symbol with the same name (or a well-known symbol), leading to undefined behavior.
97+
8898
```rust
8999
#[export_name = "exported_symbol_name"]
90100
pub fn name_in_rust() { }

src/attributes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
> **<sup>Syntax</sup>**\
55
> _InnerAttribute_ :\
66
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
7+
> &nbsp;&nbsp; `#` `!` `[` `unsafe` `(` _Attr_ `)` `]`
78
>
89
> _OuterAttribute_ :\
910
> &nbsp;&nbsp; `#` `[` _Attr_ `]`
11+
> &nbsp;&nbsp; `#` `[` `unsafe` `(` _Attr_ `)` `]`
12+
> &nbsp;&nbsp; `#` `!` `[` _Attr_ `]`
13+
> &nbsp;&nbsp; `#` `!` `[` `unsafe` `(` _Attr_ `)` `]`
1014
>
1115
> _Attr_ :\
1216
> &nbsp;&nbsp; [_SimplePath_] _AttrInput_<sup>?</sup>
@@ -29,6 +33,16 @@ Attributes other than macro attributes also allow the input to be an equals
2933
sign (`=`) followed by an expression. See the [meta item
3034
syntax](#meta-item-attribute-syntax) below for more details.
3135

36+
An attribute may be considered unsafe. Unsafe attributes must *satisfy* extra
37+
safety conditions that cannot be checked by the compiler, and are specified
38+
as the attribute wrapped in `unsafe(...)`, for instance, `#[unsafe(no_mangle)]`.
39+
40+
The following attributes are unsafe:
41+
42+
* [`export_name`]
43+
* [`link_section`]
44+
* [`no_mangle`]
45+
3246
Attributes can be classified into the following kinds:
3347

3448
* [Built-in attributes]

0 commit comments

Comments
 (0)