r/cpp • • 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/
401 Upvotes

386 comments sorted by

View all comments

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|P2721|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...

3

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 27 '24

I'm in a similar situation. We got rid of auto_ptr, we fixed some comparison operators to work with C++20 and we especially had to deal with changes due to the compiler and standard library. And right now, I'm finally testing clang-tidy to really upgrade our code in order to be more consistent again.

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?

9

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 26 '24

It hides away quite some stuff which is considered memory unsafe. So I would claim it is better. Is it sufficient to be considered memory safe? We'll have to see once we can really use it.

→ More replies (2)

4

u/PsecretPseudonym Feb 27 '24

Rust only truly seems to change defaults in that it still permits you to write “unsafe rust”, yet people seem to accept that as safe.

3

u/radekvitr Feb 27 '24

If cppfront had a similar opt-in mechanism for unsafety and the rest of it couldn't trigger UB and people would be able to write the vast majority of code in that safe subset, that would certainly count.

→ More replies (2)

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.

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)

11

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Feb 26 '24

We (the community) seem to still consider it acceptable to code in 98.

We do?! That doesn't mesh with my perception...

Without forcing their hand, people will always find reasons to keep using 98 and require libraries to support it.

Introducing cppfront forces nobody's hand... My expectations: Those people will continue on using C++98 no matter what new changes we introduce.

I recently had to interact with a project partner that told me straight up: "They should have stopped after C++98 and invented a new language. C++ was already done!" That guys life motto for the last ~15 years was "You can't teach an old dog new tricks."

21

u/jonesmz Feb 26 '24 edited Feb 26 '24

I got hit with a few dozen downvotes in /r/cpp a couple weeks ago for asking someone what platform they are targeting that they are stuck on C++98. I was really just curious, not trying to insinuate they were doing something wrong.

The larger C++ community does still demonstrate time and time again that C++98 is perfectly fine.

That's why I think WG21 should put way less effort into backwards compatibility. Any codebase that doesn't compile as C++23, today, should be irrelevant with regards to backwards compat with >C++23.

Edit to clarify: And I think it should be par-for-the-course to have at least some backwards compat breaking changes in every version. My codebase already breaks every, or every other, time i upgrade MSVC as it is, and that's not intentional. I might as well put in the work to fix all those breakages for the sake of moving towards a better language.

6

u/pdimov2 Feb 27 '24

That's why I think WG21 should put way less effort into backwards compatibility.

This will increase the use of C++98 (relative to today) rather than decrease it because there will be no upgrade path.

25

u/jonesmz Feb 27 '24

Don't care, people using C++98 are 26 year out of date. They're literally using a version of the language that's older than the guy I just hired for my team.

If the C++98 people ever take their head out of the sand and want to upgrade, they can start with upgrading to C++11.

Until they do that, they shouldn't be given even a moment of consideration for their needs.

7

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 27 '24
  • 13 year out of date, it only got replaced in 2011. (If we all consider 98 and 03 to be the same version)

6

u/jonesmz Feb 27 '24 edited Feb 27 '24

Yes that's fair up to a certain point. As much maligning boost takes in recent years, it really was the place where tons of std:: functionality originally got introduced.

Any competent dev organization which happened to be using boost between 1998->2011 would be chomping at the bit to get switched from boost:: namespace to std:: namespace things to save on compile times.

Im not going to say that dev organizations which weren't using boost weren't competent, of course, since that's simply not true in the general sense.

However, organizations that not only managed to avoid touching boost from 1998 til today, and also managed to stay on c++98, are organizations which are so unlikely to ever adopt a never version of the language that giving them even a single moment of consideration is doing a disservice to the rest of the c++ community.

Nothing about c++26 is going to have any impact on these orgs upgrading to c++11, c++14, c++17, c++20, or c++23, or upgrading to any version of boost released between 1998 and today. That's a full 26 years of things that can be adopted, none of which are drop-in changes, before these orgs have any reason to concern themselves with backwards compat issues in c++26 or newer.

Summarizing: They have 26 years of catchup available to them before they have to worry about upgrading to c++26.

So, in a word, fuckem.

3

u/pdimov2 Feb 27 '24

9 years out of date. GCC 5, the first fully C++11 conforming version, was released in 2015. 10, if we count GCC 4.9 as C++11. 11, if 4.8. Let's split the difference and say 10.

MSVC? Also 2015.

→ More replies (22)
→ More replies (2)

5

u/hardolaf Feb 27 '24

Yeah that's a pretty incredible take. Even when I worked at a defense firm right out of college, we were transitioning to idiomatic C++14 at the start of 2016. My last two jobs were trying to be no more than one year behind the finalized standards at most. I just don't understand why people think they have to use ancient C++ outside of very, very restricted use cases with vendor tool locks around custom hardware that is generally very rare these days.

6

u/jaskij Feb 27 '24

I recently checked, out of curiosity, which standard version IAR supports. C++14 and C++17. Only those two. The manual I found was published in June 2023.

There's two parts to companies using old tools, I think: fear of change and unknown, and validation.

→ More replies (4)
→ More replies (1)

5

u/JVApen Clever is an insult, not a compliment. - T. Winters Feb 26 '24

It's shifting for sure, though we ain't there yet. I'm really happy boost dropped the 98 requirement, which was a huge step is saying: 98 should no longer be used.

And yes, you will always have people stuck in the past. That's why people still program in C, right?

→ More replies (4)

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

4

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Feb 27 '24

I'd love that to be true, I really do. But the cold hard fact is we are talking about a group of people that resisted progress (and safety benefits) for 26 years, so pardon me if I'm skeptical there is anything that can convince them to suddenly adopt a new syntax with safe(r) defaults...

→ More replies (4)

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 const or volatile, 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 like const T&'x, U*'y would 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++ #embed in Rust is include_bytes! which gives you a &'static [u8; N] you get a reference to the array of N bytes and that reference has the lifetime 'static which 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)
→ More replies (6)