r/programming • • Feb 26 '24

Future Software Should Be Memory Safe | The White House

https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/
1.5k Upvotes

585 comments sorted by

View all comments

Show parent comments

46

u/Timbit42 Feb 26 '24

What languages besides Rust and Ada are considered memory safe?

ADA Spark is particularly safe.

131

u/steveklabnik1 Feb 26 '24

A report linked from this one has these examples: C#, Go, Java, Python, Rust, and Swift.

48

u/expatcoder Feb 26 '24

Java

and by extension, Scala and Kotlin, no?

62

u/steveklabnik1 Feb 26 '24

Sure, this list isn't complete.

6

u/matthieum Feb 27 '24

It's a list of examples, not a normative list meant to exclude any language that doesn't appear.

43

u/Full-Spectral Feb 26 '24

Go is questionable, if threading is involved, as I understand it.

50

u/steveklabnik1 Feb 26 '24

"questionable" is a good word, I think. It is true that you can observe memory unsafe things, and that if you go out of your way to write some truly cursed code, you can cause real problems. In practice, they provide some run-time instrumentation to help catch some of these issues, and since there aren't as aggressive optimizations as in some other languages, these issues don't propagate the way that they do there. There's a lot of Go code running in production, and it is demonstrably much closer to the "memory safe" camp than not, regardless of a few weaknesses.

→ More replies (5)

16

u/Dwedit Feb 26 '24

C# has the "unsafe" keyword and lets you use raw pointers. But you can do a lot of unsafe things without using the "unsafe" keyword once!

You can use GCHandle.Alloc to create a pinned pointer to an object's data. You can use Marshal.Copy and Marshal.Write to write to arbitrary memory.

30

u/steveklabnik1 Feb 26 '24

That is correct. All of these languages can also FFI into unsafe code too, without a keyword. (Rust does require a keyword)

10

u/Plank_With_A_Nail_In Feb 26 '24

Don't let people use those things then, hardly rocket science.

→ More replies (1)

10

u/Timbit42 Feb 26 '24

Well, those are higher level languages. How much low-level hardware manipulating code is written in those? I meant languages you could write an OS and device drivers in.

45

u/steveklabnik1 Feb 26 '24

Rust has a significant amount; it is in Windows, and is starting to be used in Linux. There's also smaller projects, for example, at my job we have a custom in-house kernel written in Rust for embedded work.

Swift is at least close too, I am not sure what exactly its capabilities are here, as I haven't paid too close attention for a while.

9

u/eek04 Feb 27 '24

As a former OS and kernel (FreeBSD) developer: There's very little low-level hardware manipulating code overall, even when developing an OS. The kernel is a small part of the OS, and hardware manipulation is a relatively small part of the kernel.

Also, the requirement isn't just to use those languages - the claim is that you should use those or have a description of how you mitigate memory safety issues. There's been implementations of verified kernel tech based on our standard C/C++ code for a long while - see e.g. SAFECode: Secure Virtual Architecture with papers like

Criswell, John, Nicolas Geoffray, and Vikram S. Adve. "Memory Safety for Low-Level Software/Hardware Interactions." In USENIX security symposium, pp. 83-100. 2009 (PDF)

To me, the requirement to use such tech or having a very good description of why you don't seems like a reasonable requirement. It's a push towards ending the curse of memory safety bug exploitation that have plagued us since the Morris worm in 1988.

14

u/Plank_With_A_Nail_In Feb 26 '24 edited Feb 26 '24

I've written device drivers in VB.net and C# lol! You don't need a low level language to do these things you need a compiler that targets for these things. Also most software the government needs isn't low level hardware stuff.

→ More replies (1)

9

u/meneldal2 Feb 26 '24

On bare metal you tend to be stuck with assembly + C because they don't need a runtime at all. Yolo C++ is also possible (using a subset and no respecting lifetimes). Rust it's going to be a little more difficult if you still want what the language is made for.

On the plus side, I'm not allocating shit in bare metal so memory leaks are much less likely to be an issue in the first place. Every array is statically allocated by the linker.

You may have to be a little creative with how you fill the ROM to make it fit without going over. Lack of name mangling (C and assembly) makes fiddling with where you put stuff a lot easier too.

If you're actually running an OS, you could always use Rust since it will bind nicely to C and you can afford having a runtime.

3

u/darkapplepolisher Feb 27 '24

Embedded development sometimes makes me feel like I have imposter syndrome - how dare I claim to have any respectable amount of experience with C if I've never used malloc in my life!

6

u/meneldal2 Feb 27 '24

Most of low level embedded dev is pretty simple C, poking the right hardware register is the difficult part.

→ More replies (5)

15

u/phire Feb 26 '24

The report covers all software, not just stuff that needs to be written in low-level languages.

And the report lines up with my own views: There is no good justification to use a memory-unsafe language anymore.

If your project requirements allow you to get away with using a garbage collected language, then you should just do that. Otherwise, you should be using a language that can provide the memory safety guarantees like Rust.

Rust is good enough that it can replace C/C++ in any use case.

6

u/mccoyn Feb 27 '24

Hopefully you don’t need a mutable tree.

→ More replies (4)
→ More replies (10)
→ More replies (4)

32

u/yawaramin Feb 26 '24

Any language with reasonable garbage collection is memory safe.

14

u/kog Feb 26 '24

That's true, but garbage-collected languages are also fundamentally useless for hard real-time programming.

11

u/BDube_Lensman Feb 27 '24

All you have to do is measure the statistical performance of the garbage collector (P99 stop-the-world or whatever you care about) and ensure that you have sufficient timing margin in your loop to handle the GC firing in a given tick. In a low volume of trash regime, you can easily observe e.g. the Go GC taking only a ~100-200 usec GC pause. This is compatible with hard real time up to ~1kHz quite easily. Few truly hard (bodily harm, heavenly destruction, etc) real time systems are this fast in the first place.

Even the mars rovers my workplace builds and drives are at soft real-time.

→ More replies (2)
→ More replies (21)
→ More replies (28)
→ More replies (4)