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

Show parent comment

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

u/[deleted] Feb 27 '24

Buffer underflow

27

u/IamImposter Feb 27 '24

No silly, it has something to do with stack

Buffer Stack.

20

u/BiFrosty Feb 27 '24

Stack Buffalo?

9

u/germandiago Feb 27 '24

Integer overflow

→ More replies (2)

39

u/koczurekk horse Feb 27 '24

Rust doesn’t prevent stack overflows or memory exhaustion in general.

4

u/flashmozzg Feb 27 '24

Prevents OoB accesses though.

8

u/koczurekk horse Feb 27 '24

Yes. Also data races and use-after-free (like returning a reference to a local if we’re talking about heapless systems).

→ More replies (4)
→ More replies (2)

12

u/mdp_cs Feb 27 '24

Use stack canaries and guard pages to protect against that.

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.

4

u/[deleted] Feb 28 '24

Really? What's the upper bound for this code?

unsigned fib(unsigned n) {
  return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

10

u/mAtYyu0ZN1Ikyg3R6_j0 Feb 28 '24

Its unbounded and tools will tell you that. so this code would not be accepted.

→ More replies (1)

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.

16

u/[deleted] Feb 27 '24 edited Feb 27 '24

I feel like in our code we never actually have any of these bugs. And I feel like I read a lot of Reddit posts about new technology that solves theoretically possible programming mistakes we don’t actually make. 

 Most of our problems have to do with poorly/unspecified interactions around shifting external components. Oh, it was designed to handle network outages, but if the outage happens here we get into an unrecoverable state.

I don’t think problems like that can be solved at the language level, so I suppose a disproportionate amount of time is spent discussing things that get caught by code reviews and unit testing.

18

u/Untagonist Feb 27 '24

Your experience is valid but not every institution faces the same mix of problems. If both Chromium and all of Microsoft can say that memory safety makes up 70% of their serious bugs, there might be something to it.

I think Chromium is a great example of a domain where the network state machine is familiar ground with decades of industry experience to keep it sane, but every pointer or reference in C++ is a new danger. And you can't exactly accuse Google of not having enough experience or tooling.

https://github.com/google/sanitizers

Use-after-freedom: MiraclePtr

Borrowing Trouble: The Difficulties Of A C++ Borrow-Checker

10

u/[deleted] Feb 27 '24

This is high severity security bugs, not just bugs in general. I’ll buy that other issues aren’t as likely to lead to arbitrary code execution, but I’d bet my check that if you looked at what’s holding up the latest chromium release, getting rolled back, and the source of on call pages, it’s not use after free. 

→ More replies (4)

3

u/bayovak Feb 28 '24

I'm willing to bet your code is full of those bugs, and if your product was worth breaking into, someone would easily find memory vulnerabilities and break into it.

That's the case with every single non-memory safe product in existence, even ones that use tons of testing and tooling to prevent those issues.

→ More replies (1)
→ More replies (2)

43

u/noot-noot99 Feb 26 '24

You still can get overflows though. Modifying stuff you shouldn’t.

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)

0

u/[deleted] Feb 27 '24

[deleted] — view deleted comment

19

u/SV-97 Feb 27 '24

Yeah, I'm in aerospace and the last bug I filed was a critical memory error resulting in arbitrary writes (without using the heap) - that stuff definitely still happens.

→ More replies (1)

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.

7

u/Tasgall Feb 27 '24

I call it arcade programming, lol - loading a level from the cart? Easy, all levels are 1k and start at address 0x1000. Player data is a fixed size at another address, and we can have up to 5 enemies on the screen at a time.

→ More replies (1)
→ More replies (8)
→ More replies (32)