# Unsafe categories

**URL:** <https://internals.rust-lang.org/t/unsafe-categories/15334>\
**Category:** Uncategorized\
**Created:** [September 14, 2021, 11:59am UTC](https://internals.rust-lang.org/t/unsafe-categories/15334 "2021-09-14T11:59:51Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![blonk](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@blonk](https://internals.rust-lang.org/u/blonk)\
**Post date:** [September 14, 2021, 11:59am UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/1 "2021-09-14T11:59:51Z")

</div>

I have been tangentially involved in a trial which was meant to try to introduce Rust in an old and large project which has regular audits for certain components and code paths.

The trial was concluded recently with a "lessons learned" meeting, and the auditors wondered if it's possible to annotate `unsafe` blocks to constrain what can be done within it. There was a specific case in the trial where an unsafe block was used to access members in an union, and the question was if it's possible to put constraints on the code block to explicitly disallow any other types of unsafe features.

I occurred to me after the conversation that this might not even be as useful as one might think, since some unsafe features may potentially be expressable using other features, which would make the strict categorization (for the intended purpose here) much less useful.

In their hypothetical example they had put an `unsafe_allowed = "enum_member_access"` (or something along that line) attribute on an `unsafe` block. Is there already any such feature, are there any plans for one, or has it already been rejected?

I think they found the idea of constraining certain types of operations within `unsafe`-blocks to be appealing to their auditing process, but on sober reflection I can't help think it's a little odd -- these auditors are quite happy to tell you that you can never trust a compiler (or even hardware..) to do what you tell it, so I'm not sure how such an `unsafe` constraint would be trusted within that frame of reference.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [September 14, 2021, 12:41pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/2 "2021-09-14T12:41:44Z")

</div>

I personally feel like it would be nicer to just add the ability to use the `unsafe` keyword in a smaller scope than a whole block, namely, on each individual `unsafe` operation.

In terms of syntax, a possibility for each `unsafe` feature, i.e. each of

> The following language level features cannot be used in the safe subset of Rust:
> 
> - Dereferencing a [raw pointer](https://doc.rust-lang.org/stable/reference/types/pointer.html).
> - Reading or writing a [mutable](https://doc.rust-lang.org/stable/reference/items/static-items.html#mutable-statics) or [external](https://doc.rust-lang.org/stable/reference/items/external-blocks.html) static variable.
> - Accessing a field of a [`union`](https://doc.rust-lang.org/stable/reference/items/unions.html), other than to assign to it.
> - Calling an unsafe function (including an intrinsic or foreign function).
> - Implementing an [unsafe trait](https://doc.rust-lang.org/stable/reference/items/traits.html#unsafe-traits).

could be

- Dereferencing a raw pointer

```rust
let x: *mut u8 = …;
let old_value = unsafe *x;
unsafe *x = new_value;

```

- Reading/writing a mutable/external static

```rust
unsafe FOO += 1;
let foo = unsafe FOO.clone();

```

- Accessing a field of a union

```rust
let some_union = …;
let f = some_union.unsafe field;

```

- Calling an unsafe function or method

```rust
let x = unsafe f(42);
let y = foo.unsafe bar(baz);

```

- Implementing an unsafe trait _already has_ fine-grained unsafe annotations. You cannot suddenly dereference an unsafe pointer in an `unsafe impl Trait for Foo {}` without using `unsafe` another time.

* * *

Note for clarification: In the proposed syntax above, the `unsafe` always only allows the single operation it applies to. For parsing, consider `unsafe` to bind to a single succeeding token-tree, nothing more: Here’s some pseudo-code using parentheses to clarify precedence: _(not valid Rust code under the proposal!)_

```rust
let x: *mut u8 = …;
let old_value = (unsafe *)x;
(unsafe *)x = new_value;

unsafe FOO += 1;
let foo = (unsafe FOO).clone();

let some_union = …;
let f = some_union.(unsafe field);

let x = unsafe f(42);
let y = foo.(unsafe bar)(baz);

```

In a chain of unsafe methods, called on a mutable static, returning a pointer which is dereferenced, could thus write

```rust
let x = unsafe *unsafe FOO.unsafe bar().unsafe baz(unsafe qux());
// FOO is a mutable static
// the * dereferences a raw pointer
// bar, baz are unsafe methods
// qux is an unsafe fn

```

instead of

```rust
let x = unsafe { *FOO.bar().baz(qux()) };

```

* * *

If `foo` and `bar` are unsafe `fn`, then `unsafe foo(bar())` won’t work, only `unsafe foo(unsafe bar())` would. With this approach, there’s no accidental or non-obvious additional usage of unsafe language features like it’s possible in an `unsafe {}` block. I.e. a reader of `unsafe { foo(bar()) }` not immediately notice that there’s _two_ unsafe functions being called here.

* * *

This feature would – of course – come with an opt-in/allow-by-default lint to disallow `unsafe {}` blocks (and I guess, that lint would/should also imply [`unsafe_op_in_unsafe_fn`](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unsafe-op-in-unsafe-fn)).

---

<div class="post-metadata">

**Author:** ![lordan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lordan/32/2107_2.png) [@lordan](https://internals.rust-lang.org/u/lordan)\
**Post date:** [September 14, 2021, 12:48pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/3 "2021-09-14T12:48:48Z")

</div>

Personally I don't think such a feature would pull its weight, since the nature of each `unsafe` block (or operation) should be pretty obvious. Any `unsafe` block where this feature would possibly catch an unwanted operation is an `unsafe` block that is too large.

If anything, such an annotation would IMHO only potentially make sense on a much larger scale: files, modules or whole projects.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [September 14, 2021, 1:09pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/4 "2021-09-14T13:09:25Z")

</div>

Wouldn't such fine-grained requirements in addition to annotating each `unsafe` usage with an explanation end up looking like this in practice?

```rust
let x =
    unsafe * // SAFETY: deref justification
    unsafe FOO // SAFETY: static mutables, yay
    .unsafe bar() // SAFETY: frobbed the latch above
    .unsafe baz( // SAFETY: we have a lock elsewhere
        unsafe qux(), // SAFETY: fun
    );

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [September 14, 2021, 1:32pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/5 "2021-09-14T13:32:26Z")

</div>

I guess, if you like it, that’s an option. Note that today, `unsafe {}` blocks can be sub-expressions, too

```rust
let x = unsafe { foo() } + unsafe { bar() }; // where does the `SAFETY` go?

```

I guess, it would also be okay, to put the `// SAFETY` comment before the entire statement.

IMO still

```rust
// SAFETY: justify all 5 unsafe usages
// either one-by-one, or by providing an argument that explains how
// all of their safety conditions are met at once.
// (Or something in-between i.e. group a few unsafe operations, etc…)
let x = unsafe *unsafe FOO.unsafe bar().unsafe baz(unsafe qux());

```

is better than

```rust
// SAFETY: justify 3 or 4 unsafe things, forget about the rest,
// nobody will notice during review anyway, unless they look up
// every single function to double-check if it’s `unsafe`,
// and check every static if it’s mutable, and every pointer type
// that gets dereferenced for if it’s a raw pointer type
let x = unsafe { *FOO.bar().baz(qux()) };

```

You could even still use a block (just not an `unsafe` block) if you want to group stuff, e.g.

```rust
// SAFETY: …
{
    …some unsafe operation;
    …another safe operation;
    …some unsafe operation;
    …another safe operation;
}
```

---

<div class="post-metadata">

**Author:** ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)\
**Post date:** [September 14, 2021, 4:50pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/6 "2021-09-14T16:50:25Z")

</div>

I'd like to affirm steffahn's perspective here - It has always seemed strange to me that `unsafe` applies to _blocks_, such that `unsafe { foo(bar()) }` gives no immediately-legible indication of whether `bar` is safe or not. (And if you want to be explicit about only doing a single operation inside the `unsafe` block, you have to say `{ let temp = bar(); unsafe { foo(temp) } }`, which is dreadfully verbose.)

I don't have an opinion about whether it's worth the churn to fix, but it definitely seems like an unfortunate wrinkle in Rust's rigor about requiring unsafe things to be explicit.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [September 14, 2021, 5:17pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/7 "2021-09-14T17:17:04Z")

</div>

> [@blonk](#):
>
> There was a specific case in the trial where an unsafe block was used to access members in an union, and the question was if it's possible to put constraints on the code block to explicitly disallow any other types of unsafe features.

I think your best bet for now is probably a linter like `clippy`. It can't do exactly what you want here, but it provides a number of lints relating to `unsafe`. An important one is always having a `// SAFETY: ...` comment to go with each usage of `unsafe`.

But perhaps you could also contribute some optional lints to `clippy` to constrain what you're allowed to do in `unsafe` code throughout your program. You could optionally allow some of the behaviors you're constraining in certain places with an annotation.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [September 14, 2021, 6:36pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/8 "2021-09-14T18:36:37Z")

</div>

> [@elidupree](#):
>
> gives no immediately-legible indication of whether `bar` is safe or not

FWIW, IDE plugins such as rust-analyzer and IntelliJ Rust are capable of highlighting individual unsafe operations within an `unsafe` block. Not fully a replacement, but an available tool.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [September 14, 2021, 6:41pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/9 "2021-09-14T18:41:07Z")

</div>

A related previous conversation:

> **[Language: annotate \`unsafe\` with invariants?](https://users.rust-lang.org/t/language-annotate-unsafe-with-invariants/64251/5?u=scottmcm)**
>
> One thing that doesn't help with, though, is the "there are three invariants but you only checked two of them" problem, which is the core thing I'd like from a feature like how I interpreted the OP. Strawman: the function could have ...

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [September 14, 2021, 7:39pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/10 "2021-09-14T19:39:40Z")

</div>

One nice thing about rust-analyzer is that it highlights exactly which operations in an unsafe block are unsafe using semantic tokens. For example in case of unsafe function calls it highlights the function name. I am not sure if you need to configure a color for the unsafe token modifier or if it is red by default though.

---

<div class="post-metadata">

**Author:** ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)\
**Post date:** [September 14, 2021, 8:44pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/11 "2021-09-14T20:44:00Z")

</div>

> [@CAD97](#):
>
> FWIW, IDE plugins such as rust-analyzer and IntelliJ Rust are capable of highlighting individual unsafe operations within an `unsafe` block. Not fully a replacement, but an available tool.

Heh, I use IntelliJ Rust a lot and I've never noticed this, which I guess is because I never write unsafe code 😅

---

<div class="post-metadata">

**Author:** ![sollyucko](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sollyucko/32/6101_2.png) [@sollyucko](https://internals.rust-lang.org/u/sollyucko)\
**Post date:** [September 15, 2021, 11:59am UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/12 "2021-09-15T11:59:33Z")

</div>

Possibly related:

- [Pre-RFC: I/O Safety](https://internals.rust-lang.org/t/pre-rfc-i-o-safety/14585)
- [Allow safety checks inside unsafe functions](https://internals.rust-lang.org/t/allow-safety-checks-inside-unsafe-functions/14458)
- [Language: annotate `unsafe` with invariants? - The Rust Programming Language Forum](https://users.rust-lang.org/t/language-annotate-unsafe-with-invariants/64251)
- [Tracking Issue for "unsafe blocks in unsafe fn" (RFC #2585) · Issue #71668 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/71668)
- [The problem of safe FFI bindings in Rust - Abubalay](https://www.abubalay.com/blog/2020/08/22/safe-bindings-in-rust)
- [Calling from Rust to C++ is not safe · Issue #1 · dtolnay/cxx · GitHub](https://github.com/dtolnay/cxx/issues/1)
- [Document why from\_bits\_unchecked is unsafe · Issue #200 · bitflags/bitflags · GitHub](https://github.com/bitflags/bitflags/issues/200)

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [September 15, 2021, 2:34pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/13 "2021-09-15T14:34:52Z")

</div>

> [@steffahn](#):
>
> IMO still […] is better than

Oh no, that looks absolutely egregious. That's way too much line noise, it doesn't help with clarity at all.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [September 15, 2021, 3:52pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/14 "2021-09-15T15:52:21Z")

</div>

> [@H2CO3](#):
>
> Oh no, that looks absolutely egregious. That's way too much line noise

And rightfully so! You shouldn’t ever _actually_ be accessing a `static mut` with a chain of 2 `unsafe` methods **_and_** call an `unsafe` fn for a method argument _ **and** _ dereference a resulting pointer, all in a single line.

If e.g. just 1 or 2 of these operations were unsafe, it’d look much less noisy (_and_ safer, so a nice correlation between more unsafety and more noise!):

**(1 unsafe operation)**

```rust
let x = unsafe *FOO.bar().baz(qux());

```

or

```rust
let x = *unsafe FOO.bar().baz(qux());

```

or

```rust
let x = *FOO.unsafe bar().baz(qux());

```

or

```rust
let x = *FOO.bar().unsafe baz(qux());

```

or

```rust
let x = *FOO.bar().baz(unsafe qux());

```

  

**(2 unsafe operations)**

```rust
let x = unsafe *unsafe FOO.bar().baz(qux());

```

or

```rust
let x = unsafe *FOO.unsafe bar().baz(qux());

```

or

> **etc… \[click to show remaining possibilites\]**
>
> ```rust
> let x = unsafe *FOO.bar().unsafe baz(qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = unsafe *FOO.bar().baz(unsafe qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *unsafe FOO.unsafe bar().baz(qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *unsafe FOO.bar().unsafe baz(qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *unsafe FOO.bar().baz(unsafe qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *FOO.unsafe bar().unsafe baz(qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *FOO.unsafe bar().baz(unsafe qux());
> 
> ```
> 
> or
> 
> ```rust
> let x = *FOO.bar().unsafe baz(unsafe qux());
> 
> ```

* * *

Also, I’m not proposing to _remove_ `unsafe {}` blocks from the language. If you’re _really_ hitting a case where you can’t avoid lots of `unsafe`ty _and_ the noise becomes too much, you can always fall back to `unsafe {}` blocks.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [September 15, 2021, 4:19pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/15 "2021-09-15T16:19:01Z")

</div>

> [@steffahn](#):
>
> You shouldn’t ever _actually_ be accessing a `static mut` with a chain of 2 `unsafe` methods _ **and** _ call an `unsafe` fn for a method argument _ **and** _ dereference a resulting pointer, all in a single line.

Then how is it supposed to back a proposal?

Anyway, I disagree that there shouldn't be more than one unsafe operations in one "line", and I don't agree that lines are a good unit of code to measure complexity with. They are a purely syntactic, _lexical,_ extremely low-level property of code.

Your example could be made clearer even without additional syntax. Just declare a new variable for every unsafe operation. In that way, you can put exactly one unsafe operation per _block_ if you insist, which is a way more well-defined and sensible unit of code. In fact I have been doing that in a C FFI wrapper project of mine. There's no need for an additional language feature in order to accomplish this sort of clarity or separation.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [September 15, 2021, 4:40pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/16 "2021-09-15T16:40:29Z")

</div>

> [@H2CO3](#):
>
> Anyway, I disagree that there shouldn't be more than one unsafe operations in one "line"

I didn’t say so. I’ve in-fact provided lots of (syntactical) examples above with 2 unsafe operations in one line.

> [@H2CO3](#):
>
> and I don't agree that lines are a good unit of code to measure complexity with

are you referring to this statement?

> [@steffahn](#):
>
> You shouldn’t ever _actually_ be accessing a `static mut` with a chain of 2 `unsafe` methods _ **and** _ call an `unsafe` fn for a method argument _ **and** _ dereference a resulting pointer, all in a single line.

What I mean here be “all in a single line” is nothing but “all in a very short span of code”. The emphasis of that statement was not supposed to be on the word “line”. I deliberately put an unrealistically high amount of unsafe operations in a small piece of code when first creating this example; among other things, with the intention to see that even in such an extreme setting, the syntax is still sensible and reasonably readyble.

In my opinion, if we’d be talking about “real”/practical Rust code, this code example

```rust
let x = unsafe *unsafe FOO.unsafe bar().unsafe baz(unsafe qux());

```

contains a _ridiculous_ amount of `unsafe` operations. As does this code example

```rust
let x = unsafe { *FOO.bar().baz(qux()) };

```

but the former one at least _also looks_ appropriately ridiculous for what’s the sequence of operations that it describes.

* * *

> [@H2CO3](#):
>
> Your example could be made clearer even without additional syntax. Just declare a new variable for every unsafe operation.

Now _you’re_ proposing to put each unsafe operation into its own line. Something that I don’t necessarily agree with.

> [@H2CO3](#):
>
> In that way, you can put exactly one unsafe operation per _block_ if you insist

I don’t insist on that. My main point is that you currently cannot easily tell how many unsafe operations there are in an `unsafe` block.

Even if I’d want to put only one unsafe operation per unsafe block, I have got no tooling that helps me enforce this convention. And also, instead of restructuring my code this drastically (inserting extra (`unsafe`) blocks and extra variables for immediate values everywhere), I’d rather simply have a straightforward way to make visible where exactly the unsafe operations are.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [September 15, 2021, 4:50pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/17 "2021-09-15T16:50:19Z")

</div>

> [@steffahn](#):
>
> I have got no tooling that helps me enforce this convention

Actually, maybe I don’t need it if I wouldn’t just

> [@H2CO3](#):
>
> declare a new variable for every unsafe operation

but instead actually declare up to two new variables for every unsafe operation.

Then

```rust
let result = foo(unsafe bar(baz()));

```

could be written as

```rust
let first = baz();
let intermediate = unsafe { bar(first) };
let result = foo(intermediate);

```

* * *

_Edit:_ Ah, now I’ve focused too much on giving every operation its own line 😅; of course

```rust
let first = baz();
let result = foo(unsafe { bar(first) });

```

works, too. So I guess just _one_ new variable per unsafe operation _is_ sufficient after all.

---

<div class="post-metadata">

**Author:** ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)\
**Post date:** [September 15, 2021, 6:08pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/18 "2021-09-15T18:08:15Z")

</div>

and if it was `let result = foo(unsafe bar(&baz()));`, then to avoid changing the lifetime of the temporary, you'd have to write

```rust
let result = {
  let first = &baz();
  let intermediate = unsafe { bar(first) };
  foo(intermediate)
};

```

([Here's a playground illustrating the drop order](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cce649dda185ff7551314d59ef913970).)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 14, 2021, 6:09pm UTC](https://internals.rust-lang.org/t/unsafe-categories/15334/19 "2021-12-14T18:09:10Z")

</div>

Closed on December 14, 2021, 6:09pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
