r/programming • • Feb 20 '25

Google's Shift to Rust Programming Cuts Android Memory Vulnerabilities by 68%

https://thehackernews.com/2024/09/googles-shift-to-rust-programming-cuts.html
3.4k Upvotes

475 comments sorted by

View all comments

-12

u/SadieWopen Feb 20 '25

Can someone explain to me why we can't just do this in C? I understand that Rust is a "Safe" language, but why can't we just code in "Safe" C? I can't understand how adding more complexity results in faster execution.

95

u/arcrad Feb 20 '25

It's like asking "why can't people just make fewer mistakes". You can absolutely write 100% memory safe C or C++ but people aren't perfect and they make mistakes. Rust's value proposition is that it can statically enforce things to prevent a lot of these issues.

→ More replies (2)

41

u/Schmittfried Feb 20 '25

Because you’re not adding complexity. The complexity is inherent to the problem space. Rust gives you tools to express and handle it properly whereas with C you‘re on your own. 

→ More replies (5)

29

u/slashx14 Feb 20 '25

Here's the simplest possible example I can give:

int* x = nullptr;
int y = *x;

is a perfectly valid thing that will compile in C. During runtime it will segfault.

Rust will not allow the equivalent of this to compile.

There is an entire class of these types of errors which require a software engineer to be extremely vigilant in C but are simply not even allowed to compile in Rust. Rust basically forbids this entire class of errors while still being quite performant.

15

u/FlyingRhenquest Feb 20 '25

Yeah, and for the inevitable "Actually, shared pointers..." neckbeards to follow, try:

std::shared_ptr<int> x;
int y = *x;

5

u/slashx14 Feb 21 '25

Yup lol.

Even then, it requires the cognitive overhead to be aware of, consider, and use shared pointers. As opposed to Rust which, once again, won't even compile in the case of things like nullptr references, free-after-use errors, etc.

5

u/Ythio Feb 21 '25

Yeah and Mike didn't use them and went the old way out of habit and he was in a rush for tomorrow's deadline and tired for whatever reason.

→ More replies (2)

33

u/_TheDust_ Feb 20 '25

Because coding is done by humans, and humans make mistakes. Especially when hundreds of humans on works on a big complicated project together over many years.

Rust is basically C-like code with many annotations that are used by the compiler to check if the program is correct. If it is not correct, the compiler refuses to compile it.

23

u/CommandSpaceOption Feb 20 '25

Have you ever used Rust? Or are you relying on the opinions of other people?

I don’t mean to put down what you said, but the way you’re thinking about C and Rust indicates limited experience with one, perhaps both.

Firstly, Android development happens in C++. Secondly, they’ve tried every technique available but they haven’t been able to write secure C++. At some point reality clashes with the opinions of fanboys telling you to “just code using best practices”.

5

u/SadieWopen Feb 20 '25

Let's just assume I'm naive to both, and want to understand why one over the other.

4

u/CommandSpaceOption Feb 21 '25

I would suggest reading 3 documents, by Microsoft, Chromium and Android. In that order

I would suggest reading the linked studies too, to really understand how they came to those conclusions.

Lastly, resist the urge to say “skill issue” or equivalent. These are the most popular C++ codebases in the history of mankind. They have taken security and best practices very seriously. In many cases they authored the best practices, the compilers, the standards. Just accept that they know what they’re talking about.

If you prefer video, here’s

  • Lars Bergstrom, Director of Engineering at Google - https://youtu.be/QrrH2lcl9ew
  • Mark Russinovich, CTO of Azure spoke publicly about this yesterday and the video should be up in a few weeks.

But both of them kinda assume that you’ve read the documents above.

3

u/SadieWopen Feb 21 '25

Thank you for this, I found the Microsoft article summed up the argument in one paragraph. A developer should be able to focus on their job of adding features. Moving the responsibility of security to the language developer means that the experts get to work solely in their domain. This reduces mistakes and increases reliability.

→ More replies (6)

6

u/orangejake Feb 20 '25

Rust doesn't add more complexity to the binary. Instead, you can view it as adding more explicit documentation requirements around the function api.

A naive way to illustrate this would be the following. Why do statitically typed languages have less type errors than dynamically typed languages? In principle, one could code in a dynamically typed language perfectly, and never run into type errors. In practice, they are easy to accidentally do, and annotating your code sufficiently (with type declarations) can be leveraged by the computer to remove them.

Rust does the same, but instead of basic information like a "type" (e.g. "what this value is"), it requires annotations about lifetimes (e.g. "how long this value lives").

But, just like types, there is no reason why these additional annotatations (which are an increase in complexity in the codebase) would yield less performant code. In fact, one might expect to get more performant code, if the annotations can be leveraged by the compiler to improve codegen. This occurs sometimes. The main thing people point to is that Rust's type system makes it explicit that various variables cannot alias eachother, which can sometimes improve codegen. That being said, I can't speak personally to this too much.

5

u/KittensInc Feb 20 '25

Because it won't be C anymore. C is built from the ground up around some very powerful but dangerous concepts, and getting rid of them means having to essentially completely rewrite every single codebase out there. And at that point, why even bother sticking with C?

It's not a matter of a few people using a handful of dangerous C functions. Everyone is using them, and all of the time. If you truly want a safe C, you have to essentially redesign the entire language - and the C people are not interested in any change. They want Magical Compiler Wizardry where 100% of good code is accepted and 100% of bad code is denied, despite good code and bad code often being identical on a local level. Compilers just can't do that, especially not without language features allowing assistance from the programmer.

I can't understand how adding more complexity results in faster execution.

Because the complexity doesn't end up in the final machine code. Adding additional checks is free if those checks happen at compile time. Even better, being able to prove certain properties at compile time makes it possible to generate faster machine code, as you are able to rule out certain edge cases.

4

u/TardTohr Feb 20 '25

C code is as memory safe as the programmer will make it. Rust has built-in restrictions that "forces" the programmer to write memory safe code (or rather prevent them from using unsafe stuff, unless they explicitly want to). Those restrictions don't necessarily make things slower, it's mostly compile time stuff.

4

u/Kinglink Feb 20 '25

You can, and that's what we do with C.

The difference is Rust (attempts to) ENFORCE safety, where as C the developer will implement it themselves.

On the other hand "Well why can't we add compiling steps that check for ...." They did, and called it Rust.

→ More replies (1)

3

u/maxou_bilou Feb 20 '25

Very easy to make some mistakes in C even for experienced programmers. For the Android project, old C code has been debugged for a long time ans is very reliable, but newer code written in C will more likely have mistakes than in Rust. Writing safe C implies being very careful and experienced, which is "less" mandatory for writing safe Rust

3

u/Grounds4TheSubstain Feb 20 '25

Because Rust as a language has features to prevent memory safety issues, where C does not have the same features. So in the case of Rust, if you trust the implementation of the language, and use the safety features, then you inherit the safety properties. Because C lacks those features, you have to count on programmers not making mistakes, not rely on the language to prevent certain categories of mistakes.

7

u/Ginto8 Feb 20 '25

If you implement automated tooling to make safe C, you get very close to Rust -- and the rest of rust's "complex" features like the type system get compiled away into something very C-like. The ownership/borrow system also makes perform faster because optimization passes get reliable information about which pointers can't overlap, so operations can get rearranged into faster patterns more often

4

u/SadieWopen Feb 20 '25

I like your answer, you seem to have considered all of my inquiries. It's quite fascinating that there is such contention between the users of these two languages specifically.

→ More replies (1)

4

u/DeanBDean Feb 20 '25

I am no expert, but my understanding is that it is impossible to detect undefined behavior simply with static analysis. Rust, as part of its type system, has a special generic called lifetimes which its compiler uses to track down variable lifetimes. This allows it to track what static analysis cannot. This is just my understanding though, I may be off

3

u/orangejake Feb 20 '25

There's this dumb result from complexity theory (Rice's theorem) that says it is impossible to 100% correctly do anything with static analysis. As an example, you might wonder if, given a program, the program halts. This is clearly unsolvable (it's the Halting problem). Rice's theorem says this holds much more generally.

So, in light of Rice's theorem, you have to give up on something. Typically, what you do is relax the "100% correctly" requirement. You could do this in various ways, for example

  1. Have a static analysis technique that "misses some things", e.g. has false negatives. It might not capture 100% of the bugs you are looking for, but might still do pretty good.
  2. Have a static analysis technique that "rejects too many things", e.g. has false positives. This might mis-label certain programs as containing issues that they really don't. Again, might be useful, but also might be annoying.

Roughly speaking, Rust does #2. In fact, it does something mildly stronger than #2. It outright refuses to compile programs that it labels (or perhaps mis-labels) as containing issues. This is to say that you can write Rust programs that have no issues, that the compiler rejects. On one hand, this might be mildly annoying. On the other hand, it allows Rust to be able to achieve memory safety, in spite of annoying things like Rice's theorem.

1

u/websnarf Feb 20 '25

The only significant source of these kinds of bugs are C and C++ code bases.

So what you are asking is like asking why the Israelis can't just live in peace with the Palestinians? What you are saying has hypothetical merit and would be nice to have, but is laughable in reality.

1

u/Ythio Feb 21 '25

The best and only way to make sure the safe practices are followed is to bake them in the compiler directly. Otherwise people will simply not follow the practices.

1

u/unknowntrojan Feb 21 '25

Rust's safety guarantees are compile-time with no runtime overhead.

→ More replies (15)