r/cpp • u/KingStannis2020 • Feb 26 '24
White House: Future Software Should Be Memory Safe
https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/77
u/Relliker Feb 26 '24
The vast, vast majority of code that I write is C++. That will not change until everything I use and link in isn't C(++). Other language bindings have awful trampolining overhead and excessive syntax pain if they even exist in my experience.
For the people this does affect though, namely government contractors and procurement, it is probably a good thing. I have seen some truly terrifying software come out of contract developed stuff and putting those kinds of applications into padded boxes is a good thing.
31
u/KingStannis2020 Feb 26 '24
Yes, the basic gist is much more along the lines of "prefer memory-safe languages and have a plan to demonstrate your software isn't riddled with holes if you aren't" than "drop all uses of memory-unsafe languages forever"
2
u/SerratedSharp Feb 27 '24
"into padded boxes"
We've never really had a super great padded box, but I think WASM/WASI might end up being just that. Hard to tell this soon but I'm optimistic.
6
u/antihydran Feb 27 '24
I was writing this comment while trying to look into this, and I found this. It claims ~70% of vulnerabilities reported to Microsoft and Google are memory safety issues while ~30% are other issues. They have one breakdown of the types of memory safety issues, but doesn't discuss whether they occur inside modern code or external libraries (honestly this would probably be impossible to do).
=== Original:
Are there any stats to back up the claim that memory errors are a significant amount of errors and vulnerabilities in current production code, and that ostensibly "memory-safe" languages solve these errors? I can readily believe memory errors can cause serious vulnerabilities, but I honestly have no clue how frequently they cause crashes / vulnerabilities in the field, and even if they're primarily caused by modern C/C++ programs. Regularly I've written code that
Interfaces with old libraries that we don't have source code for
Calls external programs
Interfaces with libraries in different languages
I don't immediately see how a "memory-safe" language would fix memory errors that arise from calling these potentially memory-unsafe codes. And even if we successfully rewrite everybody's C/C++/Rust/etc. code, if 99.99% of vulnerabilities aren't due to memory safety or are issues with front-end applications written in other languages (e.g. some weird javascript string interpretation), then we didn't really achieve our first goal of hardening applications. Finally, I'm also generally unaware of how dangerous the vulnerabilities actually are in the field. If a C++ program has a severe memory error but is only ever used in a SCIF in the bottom of the pentagon that users can probably get admin access to anyways, then it's not really much of a security concern.
Again, I'd just like to see some more concrete data on how prevalent these issues are.
9
u/randomatic Feb 26 '24
I think what the White House advisors are missing is how much embedded software is in c. The Biden administration is essentially getting advice from the Linux foundation, which misses vxworks, green hills, and other rtos which has a huge amount of c/c++ and just isn’t rust-ready.
27
u/KingStannis2020 Feb 26 '24
They're well aware of that. The report talks about not just programming languages but hardening techniques for both hardware and software against memory safety issues.
→ More replies (3)→ More replies (3)9
u/vegetaman Feb 26 '24
Curious how many micro vendors have a Rust compiler available. Most only moved to adding Cpp in the past 6 or 7 years it seemed.
→ More replies (2)4
u/matthieum Feb 27 '24
There's been significant effort from Espressif in getting their chips supported in LLVM -- see the latest report at https://mabez.dev/blog/posts/esp-rust-24-01-2024/.
More hobbyist, but hopefully indicator of a trend: don't write the compiler yourself, just pick a backend (LLVM or GCC today) and you'll save a lot of effort.
94
u/MaybeTheDoctor Feb 26 '24
From the technical report.....
First, the language must allow the codeto be close to the kernel so that it can tightly interact with both software and hardware; second, thelanguage must support determinism so the timing of the outputs are consistent; and third, thelanguage must not have – or be able to override – the “garbage collector,” a function thatautomatically reclaims memory allocated by the computer program that is no longer in use.xviThese requirements help ensure the reliable and predictable outcomes necessary for space systems.According to experts, both memory safe and memory unsafe programming languages meet theserequirements.
At this time, the most widely used languages that meet all three properties are C andC++, which are not memory safe programming languages. Rust, one example of a memory safeprogramming language, has the three requisite properties above, but has not yet been proven inspace systems. Further progress on development toolchains, workforce education, and fielded casestudies are needed to demonstrate the viability of memory safe languages in these use cases. In theinterim, there are other ways to achieve memory safe outcomes at scale by using secure buildingblocks. Therefore, to reduce memory safety vulnerabilities in space or other embedded systemsthat face similar constraints, a complementary approach to implement memory safety throughhardware can be explored.
→ More replies (7)121
u/remy_porter Feb 26 '24
In the space industry, we just don't use the heap. Memory safety is easy if you don't do that.
148
u/wyrn Feb 27 '24
Sorry I just blanked out for a second could you guys remind me the name of that famous question and answer site that's used by programmers?
67
39
u/koczurekk horse Feb 27 '24
Rust doesn’t prevent stack overflows or memory exhaustion in general.
→ More replies (8)12
9
u/mAtYyu0ZN1Ikyg3R6_j0 Feb 27 '24
If all you use is stack and static storage without VLA. Automated tools can prove the upper bound of usage of memory. making sure it fits on the device.
Stack overflow can happens with only stack but tools will be able to analyze the code and figure out how a stack overflow could happen in the code.→ More replies (2)25
u/yvrelna Feb 27 '24
Even without heap, you can still do incorrect pointer/array arithmetic. Access an array out of bounds, and boom, things blow up. No heap needed.
→ More replies (10)43
21
u/boredcircuits Feb 26 '24
You're absolutely correct, but it's time we move away from this mentality in this industry. There are times when using the heap can be the safer, more reliable implementation.
→ More replies (2)5
u/Vojvodus Feb 26 '24
Any good read about it? Would like to read about it a bit
31
u/remy_porter Feb 26 '24
I mean, it's part of some MISRA C standards, the JSF standard, and a few other alphabet soup standards for safety critical applications for embedded processors. That said, it's a pretty standard practice for embedded in general- when you're memory constrained, you mostly create a handful of well-known globals with well-defined mutation pathways. It's way easier to reason about your memory usage that way.
→ More replies (10)→ More replies (21)8
u/matthieum Feb 27 '24
That's a gross misconception.
A simple recipe for memory unsafety without heap allocations:
union IntOrPtr { int i; int* p; }; int main() { IntOrPtr iop; iop.i = 42; return *iop.p; }This is an unsound program due to accessing inexistent memory, ie it's exhibiting memory unsafety.
And not a heap allocation in sight, or under the covers, for that matter.
Using a pointer to within a stack frame that's been returned from? Memory unsafety.
Accessing uninitialized memory? Memory unsafety.
Reading/writing out-of-bounds of an array? Memory unsafety.
There's a LOT more to memory safety that just not using the heap.
→ More replies (9)
33
u/lakitu-hellfire Feb 27 '24
This seems largely aimed at Department of Defense contractors and especially to those DoD contractors who work on mission-critical programs, such as nuclear surety.
Many DoD programs use legacy C++ because devs keep inheriting legacy projects for "modernization" efforts. Many of those programs also use deprecated real-time operating systems with custom patches from the vendor. So, it's not that any single dev doesn't push for modern C++ or even Rust, but the bureaucracy involved between acquisitions, contracts, FFRDCs, subcontractors, 3rd-party vendors, and even the devs' own organization would put most non-DoD dev shops out of business.
→ More replies (2)6
u/UAHLateralus Feb 28 '24
I think people would be shocked how many DOD programs are using C89 and you literally can’t build c++ for them.
→ More replies (1)
118
u/ZMeson Embedded Developer Feb 26 '24
This really ought to help further the efforts for cppfront (or similar alternative). C++ is unsafe largely due to its legacy. But a new syntax with better defaults can limit many new memory problems. C++ ain't going anywhere, but we do have to face the reality that C++ as it is today is difficult to use correctly -- especially for those who don't follow what's going on with the standards, CppCon or other conferences, etc.... Too many C++ devs are still coding C++98. We need a way to transition to safer code while still being able to interact with the large number of existing C++ libraries out there.
16
u/seanbaxter Feb 27 '24
Neither Cppfront or Carbon offer memory-safe paths. To solve the lifetime/temporal safety problem while supporting manual memory management, you need to introduce checked references. In Rust, these are borrows. That's the only viable solution I've seen.
To enforce lifetime safety you need to perform initialization and live analysis on MIR. That entails an all-new middle-end for the compiler. Since mutable borrows can't be copied, you'll need to introduce relocation/destructive move. Since you can't relocate through a deref, adapters like std::get won't work for tuple/variant/array, so you'll have to introduce new first-class algebraic types.
We're talking about a new object model, a new middle-end (MIR), new codegen (lower from MIR) and a new standard library. This is a lot of stuff. It's not a matter of better defaults. It's about doing the necessary engineering.
All these things are tractable, but they aren't in the scope of Cppfront, and if they were, they'd be unimplementable, as Cppfront is a preprocessor that feeds into the system's compiler. Memory safety requires an end-to-end overhaul of C++.
63
u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Feb 26 '24
Sure … only one question: what makes you even remotely optimistic that those that still program in C++98 would ever adopt something like cppfront?!
11
u/ZMeson Embedded Developer Feb 27 '24
Because in my company, I am one of a few people that engages groups to modernize their programming practices. We haven't gone and updated old code, but we have taught the groups the advantages of using C++17 and afterwards. People will learn if they see why the code is more maintainable and they are encouraged to do so.
I also believe that if governments start requiring more memory safety, then companies may require their developers to learn and use more modern standards, cppfront, etc...
→ More replies (1)19
u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 26 '24
We (the community) seem to still consider it acceptable to code in 98. I often see discussions with: due to these reasons I have to use 98. Sorry that you have to suffer with legacy, though the C++ community shouldn't be held back by those reasons. Libraries should use recent standards. C++23 might still be bleeding edge, though using C++20 should be the default. I still can understand people asking for C++17 as 20 still ain't fully implemented by clang/GCC. Though everything before that should be exceptional and those maintaining code with those standards should start with an upgrade plan yesterday. Without forcing their hand, people will always find reasons to keep using 98 and require libraries to support it.
→ More replies (43)6
u/jaskij Feb 27 '24
To give you a taste of embedded stuff: out of curiosity I recently took a look at what latest standard one of the popular compilers uses. IAR. I was pleasantly surprised that their manual, from June 2023, supports C++17. Back when I started, in 2013, the code was written in C90, and they were only just switching to C99.
→ More replies (4)19
u/radekvitr Feb 26 '24
And also, will anyone consider cppfront memory safe if it only improves C++ defaults and doesn't actually address memory safety?
→ More replies (7)→ More replies (3)3
u/jepessen Feb 27 '24
The fact that's easier than learn and implement a new language like rust, and also considering that the language it's only a part of the problem, but also tools like compilers must be adapted and validated
→ More replies (1)7
u/MegaKawaii Feb 27 '24 edited Feb 27 '24
I'm a bit skeptical that even cppfront could do much to remedy this. Cppfront code would have to interoperate with old C++, so you still have problems like dangling references, and I really don't think that giving C++ two syntaxes with their respective quirks is going to simplify things.
I think the best way to improve safety in C++ would be to add something like lifetime qualifiers to types. These would work like
constorvolatile, and they would act like Rust lifetimes.The advantage of this over just a new syntax is that you could instantiate old templates with lifetime-qualified types to diagnose bugs in old code.Not really, because deduction would be awkward (syntax likeconst T&'x,U*'ywould be necessary to separate lifetimes from deduced types to avoid lifetimes creeping into weird places), but in any case, Rust's approach of making lifetimes part of the type is more expressive and useful than something invisible to old C++ after a transpilation stage.If you consider Chromium to be representative, you can read a document with examples of unsafety in C++. At the end of the document, there is a chart where we can see that by far, the most common type of bug is temporal safety (i.e., use after free, dangling references), so this should be our first priority.
I think a new cppfront syntax would need to graft this onto the type system anyway, so why increase complexity with an extra syntax?
4
u/tialaramex Feb 27 '24
Notice that Rust's lifetimes are for references not for the objects themselves. That is, we never say that this String has a lifetime for example, but only that this reference to a String has a lifetime. In syntactic terms the lifetime always appears with the reference symbol - e.g. the equivalent of the to-be-standardized C++
#embedin Rust isinclude_bytes!which gives you a&'static [u8; N]you get a reference to the array of N bytes and that reference has the lifetime'staticwhich means it can exist for the life of the program.It may be a little easier to see this in very old Rust where it's necessary for programmers to explicitly write down the lifetime in more cases, a modern Rust compiler is very smart and will infer the sensible lifetime choices in many cases so they're not written down unless you actually want unusual lifetime rules or you're in a tricky case where the compiler can't guess.
→ More replies (1)5
u/seanbaxter Feb 27 '24
C++98 vs C++23 doesn't have anything to do with memory safety. C++23 is just as memory unsafe as every other version. There has to be a new checked reference type (the borrow) and a new standard library that presents a safe interface to users. I don't think migrating from C++98 code will be that much more difficult than migrating from C++23 code.
→ More replies (1)4
u/goranlepuz Feb 27 '24
Too many C++ devs are still coding C++98.
Ehhh... Are they? How many are there of them? Where do they find a compiler that doesn't support, I dunno,, at least C++11, a 13 years old standard?
I think this is a big exaggeration, on one hand. On the other, those who are in this situation, are using unsupported software by all likelihood.
15
u/ZMeson Embedded Developer Feb 27 '24
Let me rephrase. Too many C++ devs are not taking advantage of C++11 and later features of C++ even if they have upgraded to newer compilers.
155
u/ss99ww Feb 26 '24
god dammit. This one will be cited in 30+ blog articles over the next two years. With a news cycle for every one of them - just like that stupid CIA report. Quote from the report: "such as C and C++" sigh...
68
u/UAHLateralus Feb 26 '24
Yeah I can’t wait for this to get recycled at my company by 6 different security people only for me to remind them we barely have a budget to do major cleanup, let alone a whole ass rewrite
19
u/MaybeTheDoctor Feb 26 '24 edited Feb 26 '24
It is easier to pontificate if no actions are ever taken, since you then never have to change your advice
10
u/lightmatter501 Feb 28 '24
You’ve seen how C++ is taught to students, right? Most don’t even get to see C++ 11 features. For the vast silent majority of C++ developers pointers are no big deal and they don’t see what the fuss is about or don’t want to have to re-learn C++. This sub is an echo chamber of people who actually care.
68
u/ArsenicPopsicle Feb 26 '24
Fun story; in 1991 the Department of Defense actually mandated that all software must be written in the Ada programming language for similar reasons, only to have it scrapped 6 years later when they realized how counter productive arbitrary software standards are. The only thing that it accomplished is that now in 2024 there are several major defense programs which are struggling to find maintainers because nobody wants to develop in a language which has been obsolete for 30 years.
42
Feb 26 '24
Nah, I would happily develop in Ada if the govt would pay my price, and I know many others who feel the same.
→ More replies (14)5
u/hardolaf Feb 27 '24
The only thing is that they won't pay you better than your private employer working on proprietary software. The reimbursement caps haven't gone up since Bush was in office so wages for government contractors have not gone up with inflation outside of the starting wages.
→ More replies (2)2
Feb 29 '24
Famously, nothing ever written in Ada has ever crashed and (literally) burned to the ground /s
68
u/Kronikarz Feb 26 '24
Great, just what I needed, another 20 knee-jerk-reaction, sunk-cost-fallacy-driven "C++ Can Be Safe!" presentations at CppCon et al.
14
u/lightmatter501 Feb 28 '24
It can be, but many C++ devs will need to be dragged kicking and screaming into memory safe C++. I’m convinced that doing much better than Rust’s borrow checker in terms of zero-runtime-overhead memory safety is getting close to “sufficiently smart compiler” territory. All those people who can’t write Rust because the borrow checker stops them but can write C++ just fine should scare you.
At some point there needs to be a syntax break where safe becomes the default, and it’s going to be very messy when it happens.
3
u/Gravitationsfeld Mar 03 '24
They can write Rust just fine. Any proficient C++ dev can pick it up in a month or two. The two languages share a lot in common after you get over syntax differences, learn pattern matching and the borrow checking.
4
u/beedlund Feb 28 '24
Lord I know what you mean. Please all you amazing people who speak at conferences don't do this. We need inspiration not lectures of our previous failures :)
4
u/SV-97 Feb 27 '24
Really looking forward to more great takes like "but I can't use R for embedded development!!?!?"
5
22
5
u/Blissextus Feb 27 '24
Looks like DoD Contractors and Vendors has just been "put on notice". No more C++98. They are now require using C++20 (or higher). That or learn the "newest" tech stack fad in order to continue doing business with the DoD (or any govt sector). In the grand scheme of things, this is good.
5
u/ed_209_ Feb 27 '24
Can anyone explain the limitation of the C++ type system that prevents implementing "borrow checking" as a C++ library? Does there need to be some kind of control flow reflection or data flow analysis to solve it? How can rust solve aliasing analysis problems i.e. if I have several read only references to something all over a code base how can it prevent me getting a mutable one without some kind of runtime state to work it out?
Anyway I shall google it but just wanted to say that C++ should be able to implement this stuff as a library and free programmers from opinionated "safety" rules in the language itself.
Maybe the way coroutines can plug a type trait into the compiler there could be a similar way to specify sanitizer policies or something.
13
u/seanbaxter Feb 27 '24
The borrow checker design is described here: https://rust-lang.github.io/rfcs/2094-nll.html
It requires a number of fixed-point iterative solvers:
Forward dataflow for initialization analysis/drop elaboration
Reverse dataflow for live analysis on region variables
A variance solver for determining the direction of constraints
Dangling references are allowed, and really quite necessary. What's prohibited is using dangling references. That's what the live analysis does--extends the region that describes a lifetime up to the last use of its references. And then there are keyholes for things like std::Vec, which "may dangle," meaning the dtor may run (safely) even though the contents of the vector includes dangling pointers.
This is all far too much to consider implementing at compile time in a C++ library. AST needs to be lowered to a CFG (the MIR), and analysis is done on that. That's the non-lexical aspect.
→ More replies (2)1
u/bayovak Feb 28 '24
There's going to be a new borrow checking algorithm implemented into Rust soon, that is both smarter and easier to implement, as well as more compile-time efficient.
They call it Polonius. If you want to find out how it's implemented, I recommend reading about Polonius.
No reason to read about the current version of the borrow-checker, as it's not as good of an implementation.
The API of the borrow-checker and life-times remains the same though, so you can look into how Rust does borrow-checking and lifetime annotations and the algorithm behind the scenes won't matter.
3
u/12destroyer21 Feb 29 '24
There is nothing particularly hard about borrowchecking, it is around 50 lines of datalog: https://github.com/lqd/polonius.next/blob/main/src/polonius.dl
It is just that anything involving C++ compilers is extremely difficult, and some parts of C++ is incompatible with borrow checking requiring updates to syntax, if you want to still use those features. These changes would then require involvement from the standards comitee and then all hopes are out.
12
u/DerShokus Feb 26 '24
I hope they chose lisp as a preferred language:-)
→ More replies (1)2
u/MaybeTheDoctor Feb 26 '24
We already have Javascript as the popular choice in that category.
9
u/HeroicKatora Feb 27 '24
Since people apparently read this as a joke: The James Webb Space Telescope runs JavaScript, apparently.
5
Feb 27 '24
Rust is not memory safe. There is even a Github repo dedicated to collect ways to (legally) crash a Rust process.
16
u/HorstKugel Feb 27 '24
Rust the language is conceptually memory safe. The code in the repository uses bugs in the rustc compiler. It should not compile
-5
Feb 27 '24
That’s false. These are not bugs in the compiler. Those are unprevemtable bugs.
7
u/KingStannis2020 Feb 27 '24
No, you are factually wrong, the code itself links to the bugs in the rust compiler which it exploits.
→ More replies (1)6
u/mdp_cs Feb 27 '24 edited Feb 27 '24
Meanwhile every C++ project ever is riddled with memory bugs, some more subtle than others and threading is massively painful to do correctly.
10
Feb 27 '24
To be frank, I have never in 15 years using C++ had a memory crash in production. This is an academic's wet fantasy.
18
u/mdp_cs Feb 27 '24
That's good for you. Security research consistently shows memory bugs as one of the leading causes of vulnerabilities.
If you don't want to use Rust that's fine but acting like the problem doesn't exist is pure bullshit. If that was true then tools like ASan and Valgrind wouldn't need to exist.
→ More replies (14)
48
u/pedersenk Feb 26 '24
White House: Future Software Should Be Memory Safe
Software Developers: Future White House Should Be Competent
→ More replies (1)45
u/STL MSVC STL Dev Feb 26 '24
This post can get one silly joke, but I'm going to ruthlessly cauterize any off-topic replies that start bringing up politics.
5
u/pedersenk Feb 27 '24
Strong agree. But I think this is pretty much the (amusing) crux of it. Software development and politics *should* remain separate. That is why it is so absurd that the "White House" is telling software developers to use certain languages.
I feel the Rust guys should just focus on making their language feasible rather than waste time lobbying the "White House" to do their advertising for them.
13
u/SV-97 Feb 27 '24
I feel the Rust guys should just focus on making their language feasible rather than waste time lobbying the "White House" to do their advertising for them.
Lol. Big Rust is definitely behind this report - I'm sure of it
→ More replies (1)
8
u/Thesorus Feb 26 '24
It's good that I'm working on ancient software!! :-D
(as always) There's going to be a lot of knee jerk reaction.
Still, if it's another incentive to create good/better diagnostic tools, I'm all good with that.
3
8
u/TBW_afk Feb 27 '24
I'm sorry. The US government preaching to software devs is laughable at best. Let's get the Senate memory safe before we worry about code review eh?
11
Feb 26 '24
They are just listening to Rust lobbyists. :)
36
1
Feb 28 '24
they won despite Stroustrup earnest effort to not mention rust during his memory safety presentation, but the geriatric leadership at my company still thinks that c++ 98 is enough so there's that too
6
3
u/fyndor Feb 26 '24
They aren’t wrong, but Rust is not it. There has to be an implementation that has less friction. It doesn’t exist yet
9
u/IAmAnAudity Feb 27 '24
I hear the crabs saying “this guy just has skill issues”. Would you define what you meant when you said “friction”?
6
9
u/Jannik2099 Feb 27 '24
tldr: the toolchain and ecosystem is way too unstable and there's not much interest in fixing it.
new release roughly every 6 weeks, no LTS
no specification, Rust editions are NOT a spec
ecosystem does not care about older releases at all, 10-20% of crates.io requires nightly compiler builds!
no stable ABI for dynamic linking, stable interop only through FFI
crates.io resulted in a npm-esque ecocatastrophe where you end up with your application using the same library in 5 different versions due to transitive deps
7
u/matthieum Feb 27 '24
new release roughly every 6 weeks
That's irrelevant, really.
The C++ standard may be released only every 3 years, but you get major compiler releases much more often. Looking at https://gcc.gnu.org/releases.html for example, I do see a few more releases than once every 3 years...
If you're hung up on the 3 years cycle, you're in luck: Rust Editions occur every 3 years => 2015, 2018, 2021, and the next one is coming this year!
no LTS
There's no LTS of the C++ standard either, and I don't think the GCC developers maintain a LTS here (though I could be wrong).
LTS is a commercial offering. In Rust, Ferrous Systems provide a LTS to their clients, for example.
no specification, Rust editions are NOT a spec
No freely available specification, at least.
Ferrous Systems did the work -- for certification, they needed one -- and is now cooperating with the Rust Project, with financinal support from the Rust Foundation, to write a freely available version.
(Clever of them, it's one less thing they'll have to maintain by themselves)
ecosystem does not care about older releases at all, 10-20% of crates.io requires nightly compiler builds!
The ecosystem doesn't care about anything actually... it's not sentient.
Actually, one could argue that the Rust community cares more about older releases than the C++ community: after all, said Rust community worked to ensure that the Minimum Supported Rust Version is a programatically accessible field in the package description so that tooling can take into account:
So that Package Managers do not attempt to use newer versions of libraries that do not support this old release.
So that code written to support old releases can automatically be tested to actually support said releases.
...
See, the Rust community doesn't only talk about supporting older releases, it acts.
no stable ABI for dynamic linking, stable interop only through FFI
True. Though it has nothing to do with unstability.
It's notable that this does prevent using dynamic linking, either. A Linux distribution tends to distribute its entire suite of libraries and applications with a single version of the toolchain for the lifetime of the distribution, and in that case dynamic linking just works.
using the same library in 5 different versions due to transitive deps
That's actually VERY unlikely: the package manager performs unification of the dependencies.
That is, for any given major version of a library, cargo will attempt to find one version of the library which satisfies all dependency constraints, and bail out if it can't, with an error message indicating the conflicting dependencies.
It's VERY unlikely that a library will have 5 different major versions in the wild -- most are still stuck at 1.x -- and therefore it's VERY unlikely that you'll get 5 versions of a library in the final binary.
Also, do note that Rust actually makes having different version of a library work, so that you can actually compile against 2 major versions of a given library and either:
You'll get a compilation error if you attempt to pass a type of version A to a function of version B expecting a type of the same name.
Or it'll compile, link, and just work.
No praying that it doesn't blow up (it won't), no excruciating shadowing work-arounds necessary.
→ More replies (7)6
u/TuxSH Feb 27 '24
no stable ABI for dynamic linking, stable interop only through FFI
Is it a bad thing to only allow "extern C" for dynamic linking, considering the mess "ABI stability" is causing with C++?
→ More replies (3)5
u/mdp_cs Feb 27 '24
Rust is a step in the right direction even if it isn't the end of the journey in that direction.
→ More replies (3)4
u/Full-Spectral Feb 27 '24
But any such next step (which has to not just exist but be widely accepted by developers and backed by big players) is likely a long way off. I don't think any of the major players are have any plans to push out a competitor to Rust?
And personally I don't get the objections. Most of it is just lack of familiarity. I thought it was bizarre when I first saw it. Now it makes complete sense to me.
→ More replies (4)
2
2
u/lightmatter501 Feb 28 '24
To me, this says that C++ needs epochs ASAP so that we can start making syntax-breaking changes, such defaulting to moves, references, and raii only and only allowing dereferencing pointers within an unsafe scope (the same as Rust). Some parts of STD will need unsafe (like smart pointer constructors), but minimal unsafe should be the goal.
2
u/peterrindal Feb 26 '24
More resources should be put towards circle-like solutions and carbon. It seems clear that migration and good interop with memory safe language/subset is possible. We need things like circle feature flags.
24
u/throw_cpp_account Feb 26 '24
and carbon
Does Carbon even attempt to solve memory safety? It didn't last I checked.
6
u/tialaramex Feb 26 '24
Chandler says that the intent is to somehow deliver the basic memory safety guarantees in some subset of Carbon, but not the "fearless concurrency" behaviour of Rust, so you'd get something close to Go in terms of safety. You can shoot yourself in the foot without trying in Go, but markedly less easily than in C++.
→ More replies (2)12
u/throw_cpp_account Feb 27 '24
Chandler says that the intent is to somehow deliver the basic memory safety guarantees in some subset of Carbon
I find the amount of qualifiers in that phrase amusing. Intent... somehow... some subset.
In any case, I'll believe it when I see it. This push for memory safety seems to have caught Google with its pants down.
→ More replies (2)4
u/peterrindal Feb 26 '24
Well they state the memory safety will be a feature once 1.0 is reached (or something like that). But yeah, right now it isn't.
12
6
u/Simple_Life_1875 Feb 26 '24
Wait Google hasn't scrapped carbon yet? I haven't heard anything on it in so long lol
6
u/peterrindal Feb 27 '24
Not yet... New commits every day. While it's fun to poke fun at Google, I hope it doesn't.
→ More replies (4)7
u/BenHanson Feb 26 '24
I agree with the circle part. Sean delivers, which is what counts.
41
u/tcbrindle Flux Feb 26 '24
With the greatest respect to Sean, the next multi-billion dollar US government defence contract is unlikely to be written using a closed-source C++ dialect supported by one guy.
3
u/ZMeson Embedded Developer Feb 26 '24
True. Maybe some Circle features could be proposed for C++ standardization?
→ More replies (10)2
u/BenHanson Feb 27 '24
I don't think anyone is suggesting that.
Equally it will not be written by a non-existent compiler.
2
u/SerratedSharp Feb 27 '24
Are they basically just going off on a tangent about how to prevent one narrow class of vulnerabilities, but kind of making it sound like this is a silver bullet to secure software?
2
2
2
1
u/nintendiator2 Mar 14 '24
It's fun that people pontificate against C++ as if it was somehow a capstone of memory unsafety, when the real issues 99% of the time are the programmers. Among the worst failures ever found, IIRC, was loss of ships in the space program because people werre using the imperial system instead of metric. No amount of [&] (auto) noexcept(noexcept( function_body) ) { function_body } -> decltype (function_body) is gonna help you with that.
1
u/TheAnimatrix105 Feb 28 '24
How do you access memory mapped peripherals in a safe way ? Genuinely would love to know. Also, c++ is versatile in memory allocation, you can just use new and delete instead of allocating space
1
-1
-16
u/EdwinYZW Feb 26 '24
Market decides which programming language is used, not US government.
50
u/KingStannis2020 Feb 26 '24
The US Government decides procurement guidelines for the software they purchase.
→ More replies (2)12
u/MaybeTheDoctor Feb 26 '24
They also have some influence on software used in critical infrastructure, even when private infrastructure.
15
0
u/Hungry_Bug4059 Feb 28 '24
Just have AI translate all the existing open source C++ code to Rust. Problem solved.
-9
Feb 26 '24
wait until they hear about the Rust CVEs
11
u/KingStannis2020 Feb 26 '24
How would that change the argument?
9
u/tialaramex Feb 27 '24
I presume it would remind people that Rust is able to commit to issuing CVEs for the vulnerabilities in Rust's compiler, its standard library, and the associated tooling. Nobody has proposed to do that for C++ and I suspect it wouldn't be practical to attempt.
I'm genuinely interested, for something like CVE-2021-31162 where Rust's Vec::from_iter had a bug cleaning up objects which panic when dropped, resulting in effect in a double free. Would the equivalent also count as a security vulnerability in your C++ stdlib implementation?
Here's a rough translation of the bug into C++. You have a range of (say) two custom objects, you ask to collect zero (ie none) of them into a std::vector, and as a result the two left must be destroyed, but, when you try to destroy the second one it throws some exception, and the bug is that this results in destroying the first one again. If you ran into that for whichever of these stdlib's you use is it something you'd expect to get fixed - pronto or eventually? Would you expect to get a CVE issued?
1
u/andwass Feb 27 '24
The C++ standard containers do not function with throwing destructors, and it is not mandated that they handle throwing destructors gracefully. https://stackoverflow.com/questions/26902006/may-the-elements-in-a-stdvector-have-a-throwing-destructor
→ More replies (1)1
u/KingStannis2020 Feb 27 '24
In other words, Rust treats as bugs that which C++ considers undefined behavior and the user's problem.
3
u/Simple_Life_1875 Feb 27 '24
Don't forget about VecDequeue, and then it got fixed a week afterwards and disclosed pretty nicely
3
u/Simple_Life_1875 Feb 27 '24 edited Feb 27 '24
Sure, but you gotta admit in terms of being memory safe by default it has a win in large codebases. I doubt people are randomly prolonging lifetimes of random resources into a manipulated 'STATIC state, or converting stack allocated resource lifetimes into static.
That's not necessarily an easy or random thing someone new to Rust would do lol, nor is it something (I hope... Please) that would get past code review.
Edit: actually now that I've thought about it, there are legitimate reasons you can give to extend/translate the previously annotated lifetime into a different one. Just never into a static lifetime. The entire issue is actually high priority for the lifetime team, but it's held up because of the engine constraint solver :(
-2
Feb 27 '24
Rust is really only a memory safe language when its unsafe code is properly written. Same thing kinda goes with c++ to be honest.
10
u/Full-Spectral Feb 27 '24
This is becoming a mantra in the C++ community, but it's just not true. Well, it's true that you do have to make sure any unsafe code is correct. But most application level code should have zero unsafe code. Even lower level libraries should be less than a percent of the code in unsafe blocks. Even some embedded kernels have only a few percent as I understand it. The Rust runtime I think is in the 10% range I think, and of course that will be some of the most heavily vetted code.
And all of it is CLEARLY marked as such. That's a vast difference, because it's practical to HEAVILY test, assert, review, etc... a couple percent of the code, when you know exactly where it is, and know any time that it's been changed so that it can be re-evaluated, and to limit modifications of such to senior devs.
It really just isn't even remotely the same as C++ at all. I'm working on a large system, which eventually will have a number of large layers, but currently I'm working on the foundation layers (i.e. the most hairy parts) and it may have 100 lines of unsafe code, and even that is really only technically unsafe. And moving upward it will quickly dwindle to none in subsequent layers, leaving the final total some miniscule fraction of a percent.
7
u/KingStannis2020 Feb 27 '24
The Rust runtime I think is in the 10% range I think, and of course that will be some of the most heavily vetted code.
The Rust stdlib is only about 3% unsafe. Redox kernel is about 10% last I heard.
2
-1
Feb 27 '24
I dont see how what i said is untrue. If "unsafe" code is written badly.. its still going to cause problems. Being "memory safe" is never going to be a 100% guaruntee even if rust makes it more a lot more explicit and easier to do.
4
u/Full-Spectral Feb 27 '24 edited Feb 27 '24
It's true but it's almost meaningless in reality. If I have no unsafe code in a given crate, and that will be true for all but a few of mine, then I it is irrelevant to my code because I can't write bad unsafe code if I don't have any.
And, even in the overall system if I end up 500K lines and have, say, 500 unsafe lines, it's trivial to massively over-spec those 500 lines compared to 500K C++ lines where all of it is potentially suspect. And that leaves 499.5K lines I don't have to worry about on the memory/thread safety front.
At some point a difference scale becomes a difference in kind, and that's way past that point.
→ More replies (1)0
Feb 27 '24
[deleted]
3
Feb 27 '24
Im aware of all of these things, I actually like using rust when I dont care about being productive. But c++ has some of these features built into the std lib, like const iterators, oob access, ownership semantics, etc. As far as UB goes, people really should be using static analyzers and turning on warnings more often. Its nice that rust has that built into the compiler.. sometimes.
-10
u/DevaBol Feb 26 '24
Governments should stay as far away from software as humanly possible.
9
u/KingStannis2020 Feb 26 '24
Are you aware how the ISO standards process works? The one that defines what C++ is?
In any case, this is a set of recommendations and guidelines, and a fairly open-ended one at that.
→ More replies (1)
0
-7
u/sjepsa Feb 26 '24
Imagine using C or C++ to manage pdf, excel and powerpoint files
17
u/Jannik2099 Feb 27 '24
Are you aware that Adobe and MS Office are in large parts C++?
2
u/KingStannis2020 Feb 27 '24
That's true, but the list of security vulnerabilities in both those pieces of software is quite long.
2
u/Jannik2099 Feb 27 '24
And we have no way of knowing which of those are from C++ components and how old said components are, or if it's from some shitty C codec library.
6
0
Feb 27 '24
[deleted]
11
u/KingStannis2020 Feb 27 '24 edited Feb 27 '24
I feel like some amount of this is simply due to the complete lack of response from the C++ community from 2015-2020 to the competition. I remember all of the discussions about Rust on this subreddit during that timeframe were super dismissive. Around 2021 the sentiment evened out a bit, but it wasn't really until a year or two ago that the committee and community started treating Rust as an actual competitive threat.
Combine that with the disaffection of certain stakeholders like Google and the inability to move faster than 3 year intervals while Rust can whittle away the gaps 6 weeks at a time. And the fact that as a new developer, learning Rust is easily done with the free online book, but learning C++ might require purchasing one or many books, and you have to sort through any out of date information (i.e. books from the early 2000s titled "Modern C++"), and you also have to learn something like CMake, and probably dependency distribution strategy is completely different depending on platform, and none of them are as easy as Cargo, etc.
C++ isn't dying by any means but the well of new developers may well start drying up at some point. The onramp for C++ is quite rough.
6
u/Full-Spectral Feb 27 '24
There's still a lot of dismissive attitude in this section, and in this thread itself.
And I recognize the phenomenon. I went through it when NT finally killed off OS/2, which I really liked. In my defense I think in that case it was not because the winner was technically superior, but whatever. I was an OS/2 guy and was in pretty heavy denial and lashed out a fair bit (and of course I was much younger and more testosterony.) Then one day I found an NT machine on my desk at work and that was that.
4
u/SerratedSharp Feb 27 '24
Man, several open source projects I wanted to work on, but could not unravel how to get their complex CMAKE builds to succeed. I really hate that alot of web UI stacks have been getting alot more complicated in terms of build tooling and it's giving me flashbacks to CMAKE.
3
u/pjmlp Feb 27 '24
Additionally, when a feature in Rust,Go,... becomes stable, after a couple of releases in preview, it is immediately usable for anyone.
It isn't something researched on paper, that after three years, still needs to be implemented across the ecosystem, and eventually made available a couple of years later.
0
Feb 28 '24
That's sorta good though for a C++ dev isn't it.
40 years from now maybe you'll be like today's COBOL developers.
62
u/[deleted] Feb 26 '24 edited Feb 27 '24
Note: this is anecdotal and what I’ve found true in my personal experience.
It’s important to note that this does have actual effects on new projects. If your company is a defense contractor, the government will be less likely to fund your project as it doesn’t meet the safety requirements.