Memory safety is the new black, fashionable and fit for any occasion
theregister.com
theregister.com
I’m convinced that future generations will look at us using C the same way that we look at old race cars withouts seat belts: it’s amazing that people ever managed to use it without immediately dying, but that fatal accidents were horribly common, and that we’d never go back.
memory safety, speed, ergonomics
pick two.
What are some ergonomic issues, in your opinion?
Looking at you Bjarne
There's still a tradeoff, but they push the boundaries quite a bit
It might be an interesting strategy to wrap shared objects with Gc<> or Rc<> by default to sidestep the fight. Once you've discovered the slowest parts of your codebase, you could refactor to replace Gc/Rc with zero-cost borrows or well-placed copies.
I haven't tried this myself, but I'm surprised it isn't the standard operating procedure for (non-embedded) Rust programmers.
Now, one might argue that this explicitness is good! But it's definitely an extra bunch of things to type when you're building something where you don't care about memory management.
[0] https://manishearth.github.io/blog/2021/04/05/a-tour-of-safe...
unsafe { std::transmute(x) } is my favorite go to if I’m sure I have the lifetime mechanics sorted and don’t want to fight the borrow checker right now (oh, and usually the borrow checker is right and I think I’ve done this like maybe 2-3 times where it’s really impossible to convey to the compiler what I’m trying to do, at least with my knowledge of Rust)
Lots of people confusing "memory safety" with the high degree of thread safety offered by Rust. Memory safety is in everything modern except C and C++, and ASM. It's the present baseline, except those.
Nobody is proposing new memory unsafe languages.
Only a few straggler developers, possibly with the same confusion, are loudly insisting they somehow need memory unsafe languages. When you consider what "memory unsafety" means, the claims are transparently risible. Nobody needs the ability to access arrays out of bounds. Nobody needs access to unallocated memory. It's not a serious desire, it's just contrarianism or confusion about what it is.
Can you say the same with other languages like C/C++/zig/... ? Van you literally enforce with a compiler switch and/or linter that you're 100 using a safe subset of the language?
You can view unsafe rust as a convenient way to go write in language that resembles rust what you'd otherwise write in C and call via a FFI
There is no sensible reason in 2023 for a language to be memory unsafe. What tiny performance advantages can be eked out are completely and utterly trounced by the multi-decade history of the catastrophic errors made and security vulnerabilities created. Whatever performance optimizations could be made are either A: a bad idea anyhow or B: if not actually a bad idea, available through the use of "unsafe" blocks in an otherwise safe language anyhow.
The world decided to ignore the likes of Modula-2, compiled BASIC and Pascal dialects, embrace UNIX and C, and so eventually we landed here.
"First, he advises that developers stop creating more unsafe code by writing new projects in memory unsafe languages" ...
"Second, he says, not everything needs to be rewritten at once. Focus first on security-critical modules."
"Third, he says, open source maintainers don't necessarily need to learn Rust to help with the memory safety transition because many Rust-based modules come with C APIs."
"Finally, he argues that the open source community should understand that the current status quo – an endless parade of memory errors – does not have to continue."
I disagree with point 3. Knowing Rust is key because it is being used to replace C and C++ in performance-critical and low-level applications to ensure memory safety. It has the most momentum and is getting inroads in the Linux kernel, the Chrome web browser and its derivatives, and other places.
C and C++ were great because they were initially simple and let us build fast infrastructure with minimal fuss. They will be with us for years to come as legacy languages. Rust is the present and future because it gives the speed of C and C++ without the recklessness of undefined behavior and "do whatever you want" resource management. It is a choice to embrace a bit more discipline and upfront fuss in exchange for memory safety and data race safety.
From your example, the idea of porting existing C and C++ code in the kernel or Chrome that has been stable in production for years, or sometimes decades, to Rust. The rewrite itself can create new exploits via the logic, especially considering the level of code being rewritten combined with Hyrum's Law. So is it a higher or lower risk to continue to solidify existing code with better practices and tools (asan, msan, smart pointers, etc), or to rewrite it all in Rust?
I don't know the answer. Because I don't think there's a clear way to evaluate and compare that risk profile ahead of time for any particular piece of non-trivial code. But I do think too many people are discussing rewrites at scale with the justification of memory safety, without considering the risk the nature of a rewrite adds back.
You are correct. Rewriting working code is a risk, especially for stable, mature projects.
The "panacea-like degree" is probably due to the refreshment, relief, and liberation that developers feel when working in a memory safe environment. When an estimated 70% of errors are due to memory safety issues, the excitement that developers feel is palpable.
>> The rewrite itself can create new exploits via the logic, especially considering the level of code being rewritten combined with Hyrum's Law. So is it a higher or lower risk to continue to solidify existing code with better practices and tools (asan, msan, smart pointers, etc), or to rewrite it all in Rust?
The risks are real and vary on a per-project basis. I think it depends on the needs and expectations of the project.
For example, a mature project that is not adding new features and is not in a critical role or likely to be targetted for security exploitation probably does not need to be changed.
Other projects under active development or in more critical roles may need to be partially rewritten to protect sensitive functionality (e.g. network handling code, input handling, parsers, etc.)
New projects should choose memory safe languages unless there is simply no other option.
>> I don't think there's a clear way to evaluate and compare that risk profile ahead of time for any particular piece of non-trivial code.
Sometimes we don't fully understand the problems until we start writing code. It is part of the nature of software development.
I'm in robotics: A field that absolutely fits the Rust paradigm of safety critical, performance-oriented, and (at least somewhat) progressive (being willing to adopt new things to succeed). And yet Rust is absolutely nowhere to be found in real systems, mostly, I believe, because C++ libraries and frameworks dominate everything and are the foundations on which systems are prototyped and built. These systems are supported by large cloud simulation and metrics frameworks, which are of course built with Python and Javascript.
Industry practices do not change overnight.
Legacy code written in Fortran and COBOL is still in use today. Some of the legacy code we use may not be rewritten. That is fine.
If the benefits outweigh the risks, new code will be written in Rust or another memory safe language.
Rust is too complex to go mainstream.
C++, if released today, would not result in the same user base.
C++ is an exercise in trying to make one language do everything and support every paradigm. The explosion of new languages recently shows that that approach is not winning. IMO because it inevitably means a leaky abstraction over a more complicated language, and lots of discipline to avoid the leaky areas. (JS seems to be edging in the same direction, although they have the excuse that browser support for alternate languages is still limited)
So not mainstream yet, if usage in companies is a measure.
I think citation needed there. Rust encourages reference counting, which is a very costly form of garbage collection.
IMO reference counting(be it in C, C++, Swift/ObjC or Rust) is a sign that you have ambiguous lifetimes and strongly correlates with a design that has punted on what components "own" what parts of memory.
Even in my C++ designs I push more strongly toward single owner patterns(member vars, std::unique_ptr) and if an adjacent system needs access usually an opaque handle(that can fail to return a value) is the better than using something like std::shared_ptr.
To the point at hand, I would argue that Rust pushes more towards the single owner model with some sets of escape hatches(Rc/Arc, RefCell, etc) when upstream rigid requirements force some of this ambiguity. I've yet to see a non-GC'd language that does a better job here, and I've found the default Rust guard rails to be quite good, even at scale across developers who may not have the depth of background to understand all of the tradeoffs involved.
Separately, there's a good argument to be made that Rust does reference counting better than C++: `shared_ptr` has to pessimistically assume use across multiple threads, while Rust allows users to safely select `Rc` instead of `Arc` when the compiler can prove that the inner type is `Sync`/`Send`. Consequently, Rust programs that do use reference counting get "free" performance compared to the equivalent C++ APIs.
and you can use non-atomic counts if you’re not multithreaded, which is also cheaper.
Also, AI assistance will make it alot easier to get stuff done in Rust.
Equivalent algorithms are built into the Rust compiler.
Tracking the lifetime of a variable and ensuring that the underlying resources are properly managed does not require artificial intelligence, just good bookkeeping.
Agreed. Unsafe Rust is needed to handle specific use cases.
However, unchecked / unsafe blocks are not needed most of the time and should be used sparingly and judiciously.
I'd say it's more likely that a future AI can write perfectly good C++ but, to soothe the poor C++ superfans it will let them type nonsense into a window for a while, then "analyse" their program and substitute one it wrote that's correct. This doesn't involve solving any intractable problems, just confidently presenting a lie.
> A future where humans write C++ but then the machine makes it memory safe seems extremely unlikely.
> I'd say it's more likely... it will let them type nonsense into a window for a while, then "analyse" their program and substitute one it wrote that's correct.
I'm assuming we're telling the machine what the program is for, if we're not it can't analyse anything, maybe my program is supposed to spit out the first million primes, then six more large numbers which aren't prime, and then segfault.