130 points · by cpeterso · 4y ago · 103 comments
I think they're trying to combine the concepts of languages that have GCs and prevent buffer overflows into a catch-all term, but "memory safe language" isn't a good choice. Non-programmers might take the term at face value.
It's kind of like using the term "safe flame thrower", except no one would be lulled into a false sense of security with that one.
Don't get me wrong, I'd rather use a "safer flame thrower" than a "flame thrower", but I also wouldn't trust anyone selling me a "safe flame thrower".
The above is my best Dijkstra impersonation.
The current set of papers for WG21 the "C++ Standards Committee" includes more than one taking this stance, but obviously the standout is P2687 from Bjarne Stroustrup and Gabriel Dos Reis. Bjarne and Gaby describe the situation (in which the US Federal Government has noticed that there's a problem) as an "Emergency"
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p26...
P2687R0 is an initial draft (even by R0 standards it looks very hastily thrown together) but it wrings its hands about the propensity of programmers to write bad code, and then it offers a list of eight kinds of (un)safety, including "delivering a result in 1.2ms to a device supposedly responding to an external event in 1ms". Alas, although C++ doesn't really help you with any of this list, other languages don't solve all of them and so why bother right?
It also takes pains to group together intractable problems (e.g. dynamic allocation but with assurance of no leaks) with solved problems (e.g. preventing use-after-free). The R0 draft doesn't really spend much time explaining how, if at all, they would solve the problem (beyond vaguely "static analysis" so it can't be critiqued on that basis.
I'm sure the committee (of which both Bjarne and Gaby are members) will look favourably on this sort of work, but nobody in government should take anything like this seriously until the actual problem gets solved, ie the programs stop being full of memory bugs.
Software can still be delivered in those languages, after all the goverments themselves have millions invested into such codebases, but it comes with an extra price tag versus other safer alternatives.
"Memory safe language" is a perfectly good term for the moment since most languages fall square into "completely unsafe" or "completely safe with the ability to be unsafe in special circumstances".
I don't know of any that are "fairly safe". Maybe something like Carbon will be like that.
Good advice. As a Lisp developer, I have used Swift and it seems like a nicely designed language, very good tooling on macOS/iPadOS and OK on Linux. Go, Rust, and others are also good.
Off topic, but I thought it sad how once 9/11 happened with the side-tracked war on terrorism, the NSA and FBI apparently (from my private citizen perspective) stopped doing much of the previous great work on public support for computer security, going after international computer crime cartels, etc. As a US taxpayer, I would like to see them prioritize that type of work.
It doesn’t make sense to teach terrorists how to secure their systems if you need to exploit these systems to achieve your mission.
Now that Chinese, Russian and North Korean actors are putting in work and causing real economic damage in the west it looks like defence is starting to hold some value again.
If I tell you not to jump off a bridge so you do it, you're still unwise because now you're just defying my instructions without thinking.
Maybe the C++ bridge isn't on fire, but if you think you can smell smoke you wouldn't be alone.
Go was designed by (among others) the father of Unix, Ken Thompson, with an understanding of the mistakes of C and C++. Despite getting hilariously little respect here on Hacker News, the language is (for many purposes) an excellent replacement for C.
(Yes, yes, I know you disagree. Tell me more about how C++ is necessary and all the time you spend fighting it is actually a huge win! Please tell me how slow garbage collection is, all evidence to the contrary!)
Go reads from, and writes to, variable-sized blocks of memory through (pointer, length, capacity) triples, known as "slices". A Go slice "[]int" is essentially a C struct like this, passed around in 3 registers:
struct IntSlice {
int* pointer;
ssize_t length;
ssize_t capacity;
};
Bounds checking in Go is easily disabled (pass -B flag to the compiler) but almost nobody does so because the performance benefit is miniscule. With bounds checking and garbage collection, Go avoids almost all memory-related bugs.You can understand Go as C redesigned for a world where parallel computing (e.g., dozens of CPU cores) is commonplace, and the benefits of safe code outweigh the tiny costs.
We're not using 80286 chips anymore. We can afford safety.
Performance stuff was written with inline Assembly, or straight Assembly with nice macro assemblers (TASM, MASM), using C or C++ in detriment of the above selection only helped bringing UNIX stuff into MS-DOS.
EDIT: See books like "PC Intern: System Programming : The Encyclopedia of DOS Programming Know How".
EDIT2: It is available at archive.org, https://archive.org/details/pcinternsystempr0000tisc/mode/2u...
Also I think the arrays had bounds checking?
Yes, it suffers from use-after-free, lets ignore everything else that is safer than C.
Bounds checking are enabled by default and if one wants to shoot themselves on the foot there is always {$R-}.
Sounds like C is not your thing, and that's ok.
No, it's not. The runtime wouldn't even fit on many platforms. GC pauses are similarly not acceptable on soft/real-time systems, etc.
Ada/Rust get closer to a replacement.
Interestingly though, I take the stance that Rust is much closer to C than C++.
There are bare metal Go runtimes being shipped in products today, e.g. TamaGo unikernel for firmware.
As there are real time Java implementations, there could exist Go ones if the market cared about having them as well.
Oh:
> The RTSJ addressed the critical issues by mandating a minimum specification for the threading model (and allowing other models to be plugged into the VM) and by providing for areas of memory that are not subject to garbage collection, along with threads that are not preemptable by the garbage collector. These areas are instead managed using region-based memory management. The latest specification, 2.0, supports direct device access and deterministic garbage collection as well.
https://en.wikipedia.org/wiki/Real_time_Java
Albeit 2.0 is still WIP at this time.
TIL
Half joking, as a C/C++ programmer here.
* Unless you run out of memory, of course. Then you have issues.
In all seriousness though it does seem to mitigate a lot of exploitable memory corruption vulns, perhaps not memory-corruption-caused Denial-of-Service vulnerabilities as well as Rust et all maybe.
Even K from K&R uses it!
For the former go won't help. Tinygo may help some day, but for any weird new chip the first thing they make is a c compiler.
The linux kernel accepts rust, or will next year. It does not accept go. So Torvalds at least disagrees it fits there.
Go has plenty of uses, but writing microcontroller firmware and device drivers are not among them. And why else would you pick c?
Go has GC and phat binaries. Frankly for a lot of stuff C# is a way better choice than Go or C++
Now, Rust is the new hotness and it doesn't make a lot of sense to use other things if you care about both safety and speed.
Go 1.17 implements a new way of passing function arguments and results using registers instead of the stack. Benchmarks for a representative set of Go packages and programs show performance improvements of about 5%, and a typical reduction in binary size of about 2%. (https://go.dev/doc/go1.17)
I think the basic problem here is that you don't know the facts.
I use Go a lot today, as a former user of both C and C++, but Rust is probably better as a C/C++ replacement.
This is a quite common memory access pattern in HPC.
I think that if Go didn't exist he would be considered "one of the greats".
Go is an extremely well designed and balanced language, it is extremely productive while still being easy to maintain and have pretty decent performance.
Chances are you probably don't want to use memory safe languages now.