197 points · by pjmlp · 3w ago · 209 comments
The Rust project as a whole currently has 178 open issues with the tag I-unsound[1]. Many of these describe issues that only exist in the nightly compiler, of course, but it does happen that bugs are not caught before shipping. That's software.
[1]: https://github.com/rust-lang/rust/issues?q=is%3Aissue+state%...
What do you call a programming language which claims to be memory-safe that has memory safety issues in it's standard library?
Rust unfortunately is renowned for it's toxic community (see actix-web). Within that community, some clueless but vocal members routinely attack people who have the audacity of not rewriting all their projects in Rust, and they parrot cargo cult mantras such as memory safety as their main attack points.
Consequently, discussions on memory issues plaguing Rust often draw these types, with the expected consequences.
Yes, Rust also suffers from memory safety issues. Yes, there are CVEs caused by Rust code. Yes, using Rust obviously does not make your code better or bug-free. But these discussions are not grounded on a technical ground.
The issue with actix-web, which played out in 2018-20, was that it played very liberally with memory safety, prioritizing speed over everything else, with the author closing PRs solving memory bugs.
Basing your definition on "techinical ground" you can dismiss anything that isn't perfect.
Rust's solution is the human response. Memory unsafety are treated as bugs, listed and fixed. Some fast, some more slowly. Depending on impact and how deep they go. C and C++'s solution is listing them in the specification, or having it be implementation defined, and telling you to not step on the land mine.
Given the research on this area introducing Rust in some of the most battle hardened codebases globally leads to far fewer memory issues and high impact vulnerabilities in the Rust parts. And the parts converted to Rust often are the ones which has had an history of bugs and vulnerabilities becuase they are non-trivial.
Thanks to that Rust community behaviour, C23 has several nice improvements, with several others to possibly land on C2y.
No, unfortunately Rust's toxic community tends to flock to these topics to flood them with their blend cargo cult claims. Topics which happen to question and even refute their claims is like their bat signal.
The actix-web episode is a good example of how far these types can take even the most harmless claim, at the expense of everyone.
Meanwhile, anyone who actually works in the field for a living is hardly surprised by this. When this sort of news pops up, we check if we need to start an unplanned update and that's it.
Their beloved language is most likely decades away from ever having a fully bootstraped reference implementation.
Which even if it ever happens, projects like Linux would most likely never adopt, as that new reference implementation has to provide the same hardware support guarantees as LLVM and GCC.
The mutable aliasing ecosystem is holding more advanced hardware back.
You say "vocal", yet they remain eerily silent in this and every other thread about Rust on HN, as well as /r/rust over on Reddit.
If you didn't know better, you could almost think they were imaginary.
You seem to have a mental picture of a raging Toxic Rust Evangelist consumed by shame and rage over Rust's Dark Secret: It's using the best codegen backend on the planet (with a compatible license), LLVM, which is written in - gasp - C++!! Dun dun dunnnn!
What a dramatic plot twist! I understand how that might feel very engaging.
The boring truth is that everybody is pretty fine with LLVM, because no reasonable (or real) person believes that this is a problem. As long as it generates pretty decent code in a reasonable amount of time for all the important architectures, it's a great choice.
We're trying to get work done.
Why must Rust have "a fully bootstraped reference implementation"?
This is weird, dude.
The main problem I have if I accept Rust as a "better C-like" with "safer memory" still ends up being the safety mechanism makes some code rather unergonomic.
What I would most like to see is a reduction of all `unsafe` throughout. The reduction implies two things: safer, and more ergonomic.
But the goal was to bring verification techniques (type systems, ownership and lifetime tracking, etc) to a practical language that also supported efficient interop with existing C libraries and APIs. Hence the 'unsafe' keyword. At this, Rust has succeeded wildly.
I've been writing Rust for years and almost never need to use 'unsafe'.
The `variable[i]` notation is intended to function like in other languages, where it returns an unwrapped value. It would be ugly, noisy, unergonomic, and annoying to have to properly handle `Some` (like in `variable[i].unwrap()`) every single time you ever used it. Imagine doing `c[i].unwrap()[j].unwrap() += a[i].unwrap()[k].unwrap() * b[k].unwrap()[j].unwrap();`.
If you really want the result to be wrapped in `Some`, you can use `variable.get(i)`.
The ? operator can be nicer to read then all those .unwraps() as an alternative and doesn't panic.
I guess the concern with panics is you're not forced to think about how you want to handle them like you do when you get a `Result` or `Option` returned.
As a result it could be used as a DDOS against your app if it's network accessible.
I would go so far as to say that an out-of-bounds index is always a logic error (to be handled by panicking), not an input error (to be handled with Result). And if the out-of-bounds access was caused by invalid input, then logic error consists of not checking the input before indexing.
For "fallible" variant indexing, you have `the_slice.get(index)`: https://doc.rust-lang.org/stable/std/primitive.slice.html#me...
Although its method signature is kind of cursed due to generics, it matches your requirement that allows graceful OOB handling.
If the default indexing behavior were like `.get()` it would be too cumbersome to express repeated use of indexing, or multi-dimensional array indexing, etc.
Also there is get_unchecked which is unsafe. Trivially you'll trigger OOB access when misused.
Maybe optional fatigue is Rusty but it's not a good style in any language
If you are doing this then you've made a mistake in your code. Rust is consistent in catching these early.
Likewise regarding the storage for the length, if one is willing to go the way C went down a few years later, declare a pointer and make the array storage be pointer based, e.g. BASED(MY_ARRAY_PTR).
I don't know if it's ever been in Rust's threat model to handle malicious code.
Note that incorrect code can still cause
1. incorrect outputs, or
2. arbitrary performance degredations
safe rust doesn't have to work magic. All it must do is prevent UB.
I thought maybe I explain this well in the README for https://github.com/tialaramex/misfortunate/ but apparently not, so that's on my FIXME list now.
When I write such an explanation it will probably use Comte as an example. Comte implements ExactSizeIterator which is a safe trait for claiming your iterator gives accurate information about how many items are left. It's lying (but equally a safe implementer might just be mistaken), and Comte is not in fact giving accurate size information. However, if you need to rely on a promise that the iterator length is correct Rust has an unsafe trait for such promises named TrustedLen.
Unlike Comte, mis-implementing TrustedLen could cause Memory Unsafety, but you would need to write the `unsafe` keyword and be lying or mistaken, which is what Rust promised you will happen.
To give a concrete example, if I tell Rust I want to put the results from this iterator over Goose into an array [Goose; 6] in usual cases it will emit code to check for overflow and panic if the Iterator makes the wrong number of Goose, even if the iterator implements ExactSizeIterator and claims to have six Goose, that panic check happens. But if it implements TrustedLen and makes the same claim, the compiler will believe it and skip the check. If it was wrong about having six Goose, your program is nonsense and anything might happen.
* Non-fused iterators, implementing FusedIterator
* Iterators that, when cloned, produce an iterator with a different set of elements
* DoubleEndedIterator which is inconsistent when iterated from each end
* A trait such that cloning a Box<dyn Trait> produces a value with a different concrete type inside
* A Display type that always produces an error from fmt()
* A Display type that replaces the Formatter with a different one
* A type that always hashes to different values.
That's why I think having unwinding in Rust is a mistake. It gives a false sense of safety, but it's very hard to write unwinding-correct code. So, it would be better to handle panics as immediate process termination instead.
Your plugin can still crash in ways other than a panic, like a segfault inside an unsafe block.
It's also likely possible to DoS the application from a plugin in ways other than a crash, e.g. add a billion entries to the right-click menu.
And if you're loading a plugin, how do you know the plugin was actually written in Rust, anyway? It could be written in C and scribble over all your stack memory as soon as you load it.
It's very hard to guarantee stability in the presence of arbitrary extensions, and you might not even want to because someone might have a real use case for an extremely large right-click menu (probably not a billion though). I don't think even language server protocol guarantees it.
Usually it's either safety-via-language (Lua, JS etc.) or just YOLO (dynamically linked libraries), at best lightweight isolation like WebAssembly but that's modern projects, <1%.
The process isolation approach is only widely used for Go (Kubernetes, Terraform), because Go doesn't support DLLs (nicely)... (and the systems themselves are very slow anyway)
I don’t think that an unwind-less rust would have fit within the weirdness budget 10 years ago, today it would definitely be something to consider. Although it would have an unwind effect to contend with.
If you don't have any way to do non local control flow you get what web developers call prop drilling. Code gets more complex because you need to check and handle every possible error condition inline.
Maybe you could get away with it by supporting union types and changing the `?` operator to accumulate error types into a `Result<T, E1|E2|E3|...>` then short circuit on failure. That way your code can decide how to handle each one instead of choosing between immediately return error or do verbose manual handling.
Testing for panics is just a silly feature itself anyway.
reminds me of the tales from https://oxide.computer/blog/iddqd-unsafe
Effect systems usually fail because effects are very different from each other and can't be generalised.
Most Rust learning material teaches panic as if it's an abort. They don't lie about it unwinding, but 99% of uses of panic use it like a fatal error with no expectation of recovery. The fact you can _recover_ from panics is even worse because you can then continue running a program after one of these "fatal" errors failed to abort the process.
There's some powerful things panic/catch_unwind let you do. I'm yet to be convinced the complexity imposed on all code is worth the squeeze on the cases where panic = unwind is a genuine requirement.
I wish the default was panic = abort, but as a whole I still find Rust the best tool for where I use it.
I tend to agree with the general principle that panicking or throwing an exception should terminate the process in most cases. But those last microseconds of a process' lifetime matter quite a lot: Are you able to save the user's work? Are you able to log the error so the crash can be diagnosed?
With Undefined Behavior in the picture, you cannot do anything. The whole process is toast, and its entire memory state is invalid.
You can’t rely on it for correctness because for your software to be reliable it needs to deal with an abrupt power outage and recover.
Best effort diagnostics make sense. And the “Rust 2.0” or successor language will with certainty close this gap.
But I also understand why we are here. A decade ago when 1.0 came out Rust had already used up most of its complexity budget for traits, enums, borrow checker, generics, functional style programming and so on.
Diverging even further from what was expected by the majority of programmers would very likely have left Rust as an esoteric language with a tiny following. Instead of what it became.
And back then unsafe was thought to work more like “just do like in C”.
There has been years of research formalizing how it actually works that’s brought us to the current understanding where it’s not trivial to write it correctly.
It is just too difficult to write unwind safe code and it is not much of a benefit even if you have it.
You seem confused. The "exception safety" topic in C++, which clueless people often cite Google's style guide as an appeal to authority, is that it's undesirable to modify old legacy code that does not use exceptions to call libraries that do. The sole reason this is not desirable is that calling code that throws exceptions introduces code paths that the legacy code was not designed to consider and cleanups that leave it in a state it was not designed to be in (i.e., all stack unwinding and RAII cleanup in surprising points with surprising implications).
And project managers tasked with a project that involves legacy C++ code can't simply spare time and resources to redesigns legacy project to accommodate for these changes, or the risk of having stable production code one day crash just because someone linked to a library that now throws.
Therefore, the "exception safety" discussion in C++ is not a technical one. It is a project management one, and one which involves high reliable legacy code.
It is just the sketchiest feature in a very realiable language.
It doesn't make sense to me why anyone whould use rust with panic handling.
And the of course the language trusts the operating system, how else can you program?
This kind of thing is basically why all projects blocked AI issues, it is just slop that kind of makes sense if you don't know what you are doing.
I don't quite like AI, but I think from an objective point of view one can reason that AI led to the discovery of more bugs, if we deduct the non-bugs from those reports.
How much code's inside the CAS loop? If it's being held for less than (sticks finger in the wind) a few thousand of instructions then backoff or limits are actively counterproductive. That's how you're supposed to use a CAS loop.
If it's more, you shouldn't try to invent a more clever CAS loop, you should replace it with a mutex.