|
|
Log in / Subscribe / Register

Memory-safe C code

Memory-safe C code

Posted Nov 17, 2025 8:29 UTC (Mon) by hunger (subscriber, #36242)
In reply to: Memory-safe C code by alx.manpages
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

Think of "unsafe" to be more about limitations of the rust compiler than about properties of the code marked unsafe. Of course there are memory-safe programs written in all kinds of languages -- whether those languages are memory-safe themselves or not!

Any code the rust compiler can not proof to follow all its internal rules needs to be marked `unsafe`. It does not matter whether that is because the code does something tricky that is too complex for the rust compiler to make sense of -- or whether it is just written in some language the rust compiler can not reason about. So *all* non-rust code is by definition `unsafe` in rust, there is no way around that.

`unsafe` is IMHO a needlessly inflammatory name for a marker that basically says "The rust compiler does not fully understand this code", especially when you are communication with other language communities. I guess we are stuck with that name now though :-/


to post comments

Memory-safe C code

Posted Nov 17, 2025 9:51 UTC (Mon) by taladar (subscriber, #68407) [Link]

But it is not just "code the Rust compiler does not fully understand", it is code where the programmer has to uphold those same safety invariants. Even unsafe code is not allowed to do anything you want that the constructs you can use in unsafe make it possible to do.

Memory-safe C code

Posted Nov 17, 2025 11:38 UTC (Mon) by khim (subscriber, #9252) [Link]

> `unsafe` is IMHO a needlessly inflammatory name for a marker that basically says "The rust compiler does not fully understand this code"

It's absolutely correct and proper marker. Technically the name is TCB (trusted code base), but when I worked in a project with “correct” naming… everyone who arrived on the project invariably wanted to commit something in the trusted folder…

> especially when you are communication with other language communities

Which communities are we talking about? C# with the exact same unsafe keyword or Haskell with System.IO.Unsafe?

IMNSHO name unsafe conveys precisely the right balance between inherent danger and usability.

Rust "unsafe" is not code the compiler doesn't understand

Posted Nov 17, 2025 12:21 UTC (Mon) by farnz (subscriber, #17727) [Link] (12 responses)

Thinking about it as "code the compiler doesn't understand" leads you down the wrong path; it's not that the compiler doesn't understand the code (it does, certainly well enough to generate machine code from it), it's that the compiler cannot prove that the code complies with all language invariants (not least because some of those invariants might be affected by code written in another language), and thus that the programmer is on the hook for ensuring that the language rules are obeyed. It may, of course, be impossible for the programmer to ensure that the rules are obeyed, in which case you have a buggy program.

One of the core reasons this is a problem is that we know that non-trivial properties of programs[1] are undecidable in general. This implies that we can only ever sort programs into three categories about a non-trivial line: "property holds", "property does not hold", "property might or might not hold".

This can be made useful by having as few interesting programs as possible fall into the "property might or might not hold" category, and then approximating the true answer by asserting that the "property might or might not hold" category is equivalent to "property holds" or "property does not hold" for all programs we care about. There's just one issue with this: sometimes, the approximation we choose is "wrong" from a human perspective, and we either reject a program that "should obviously" work, or we "miscompile" a program that "should obviously" always generate a compile error.

In turn, that leads to a dilemma; if you go down the route of miscompiling programs that should obviously always generate a compile error, programmers who didn't intend their program to have a bug will accidentally write buggy code that isn't caught by review, while if you choose to reject programs that should obviously work, programmers will get frustrated that they can't find a way to express something that clearly make sense.

unsafe gives Rust a way out of the dilemma; without unsafe, Rust promises that the approximation of any property it checks will result in Rust rejecting the program erroneously, at worst, while with unsafe, the compiler may "miscompile" programs. It's then on the programmer to decide which behaviour they want - do you want the more expressive "Unsafe Rust" dialect, with the increased risk of bugs, or do you want the lower bug risk of the "Safe Rust" dialect, but with the inability to express some things that are perfectly reasonable?

[1] As an example of a non-trivial property, "does this program read from free memory, or only allocated memory" is non-trivial.

Rust "unsafe" is not code the compiler doesn't understand

Posted Nov 17, 2025 16:04 UTC (Mon) by khim (subscriber, #9252) [Link] (11 responses)

> [1] As an example of a non-trivial property, "does this program read from free memory, or only allocated memory" is non-trivial.

That's important “non-trivial property”, sure, but it's not the most devastating one. The critical “non-trivial property”, that also can not be tested is “does that programs always behave identically to naïvely generated code with zero optimizations or not” is also undecidable.

This, essentially, blows up the whole “undefined behavior is reading error” idea to spliters: we may argue about whether undefined behavior is reading error or not till we are blue in face, but the simple fact remains: it's mathematically impossible to write a C compiler that wouldn't “miscompile” some programs with undefined behaviors… the only question is which programs it may miscompile and which ones it should compile correctly… but that's question of changing the undefined behaviors list, not question of changes to the underfined behavior definition.

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 11:56 UTC (Sun) by sammythesnake (guest, #17693) [Link] (10 responses)

The problem isn't so much that a compiler will "miscompile" a program with UB - by definition, "undefined behaviour" means there is no definition of what the compiler ought to produce to point at, so any behaviour is "correct" and "miscompilation" is A Priori impossible.

The *actual* problem is the way the border around the territory of nasal demons is defined.

It is possible to define languages where it is impossible to write a program with UB. C is not that language.

It is possible to design a programming language where the realm of UB is clearly delimited by simple rules that can easily be checked by a human of mortal intellect. C is not that language.

It is possible to design a programming language where the realm of UB is technically delimited by rules that can be checked before the heat death of the universe by a computer that could, in principle, be constructed. C is not that language.

It is possible, though of questionable utility/wisdom, to design language where the realm of UB is defined in terms of mathematically undecideble, fundamentally unsolvable problems (such as the Halting Problem}. THIS is the language C is.

Writing a non-trivial C program without UB *may* be possible, using MISRA style, intrinsics, compiler extensions etc. but doing so in standard C *and proving there is no UB* is not.

That's not my idea of an effective way to communicate with the computer...

IFF you can exclude UB, then there are large classes of provably equivalent constructions that can reliably underlie optimisations. With UB, *all* transformations are "correct", including "return 0;" -> "emit_olfactory_fiends();"

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 12:10 UTC (Sun) by alx.manpages (subscriber, #145117) [Link] (3 responses)

> Writing a non-trivial C program without UB *may* be possible, using MISRA style, intrinsics, compiler extensions etc. but doing so in standard C *and proving there is no UB* is not.

Indeed, standard C is a terrible dialect of C. We're working on improving that, but I strongly recommend using extensions that provide safety that the standard can't provide.

Please don't judge C by one of its worst dialects. Those judgements feel disconnected from reality, when one can write C programs that are safe from many (admittedly, not *yet* all) of the concerns that are often associated with C.

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 13:24 UTC (Sun) by mb (subscriber, #50428) [Link] (2 responses)

>standard C is a terrible dialect of C [...]
>Please don't judge C by one of its worst dialects. Those judgements feel disconnected from reality

Judging C by its ISO standard is disconnected from reality?
Really?

Can I tell the trolls that they are disconnected from reality when they demand a Rust standard next time?

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 14:04 UTC (Sun) by alx.manpages (subscriber, #145117) [Link] (1 responses)

> Judging C by its ISO standard is disconnected from reality?
> Really?

Yes. Just like make(1) is a great language (especially, GNU make(1)), and POSIX --and ISO standard-- make(1) is unusable (actually, POSIX.1-2024 seems to have improved make(1) significantly, and some people claim it is now in a usable state; I haven't checked myself, so I can't comment).

As someone said, the standard is toilet paper. Except when it's useful, of course.

> Can I tell the trolls that they are disconnected from reality when they demand a Rust standard next time?

I don't care. I'll only say that I have never demanded a Rust standard.

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 14:05 UTC (Sun) by alx.manpages (subscriber, #145117) [Link]

> and POSIX --and ISO standard-- make(1)

I meant to write '--an ISO standard--'.

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 12:25 UTC (Sun) by malmedal (subscriber, #56172) [Link] (5 responses)

> With UB, *all* transformations are "correct", including "return 0;" -> "emit_olfactory_fiends();"

Yes. However this is not a law of nature. Or inherent in the language itself. The committee *could* have made the opposite decision is they had wanted...

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 13:17 UTC (Sun) by alx.manpages (subscriber, #145117) [Link] (4 responses)

And indeed the committee is currently revising a lot of UBs in the standard and changing it to mandatory diagnostics, which is objectively better (and in other cases, turning undefined into defined, but we're more careful about that, as it might have negative consequences).

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 14:39 UTC (Sun) by malmedal (subscriber, #56172) [Link] (3 responses)

Sounds promising. Any details available yet, or are these internal discussions?

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 14:48 UTC (Sun) by alx.manpages (subscriber, #145117) [Link] (2 responses)

> Sounds promising. Any details available yet, or are these internal discussions?

There are results already. The current draft for C2y has lost a lot of UB.

Here's a summary from earlier this year of what has been done already, what is in progress, and what may come in the future:
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3529.pdf>.

The current draft for C2y is here, in case you want to have a look at it:
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3685.pdf>.

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 15:12 UTC (Sun) by malmedal (subscriber, #56172) [Link] (1 responses)

Thank you, looks very much like an improvement, though I am confused by terms such as ghost and earthly demons :)

Rust "unsafe" is not code the compiler doesn't understand

Posted Dec 14, 2025 15:25 UTC (Sun) by alx.manpages (subscriber, #145117) [Link]

> Thank you, looks very much like an improvement,

Thanks! :)

> though I am confused by terms such as ghost and earthly demons :)

Writing standards proposals can be boring. Using these terms can make it more entertaining. These come from "nasal demons".

We started using "earthly demon" to refer to those cases of UB that can be perfectly determined at compile time, and thus can be transformed into constraints (mandatory diagnostics).

We later added the term "ghost" those cases that the standard specifies as UB, but which are impossible to trigger (because some other clause of the standard precludes the possibility entirely). These are just removed from the standard, as they are just incorrect text.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds