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

50

u/i_am_not_sam Feb 20 '25 edited Feb 21 '25

Hypothetically if all existing C++ code was replaced with modern C++, only smart pointers and "strict memory safe practices" for all new code would it yield the same results?

Edit : read Google's blog about this topic. It's not simply the case of switching out C++ with Rust. It was also making sure that all NEW code adhered to strict memory safety guidelines. The language is just a tool. What you accomplish with it depends on how you use it.

3

u/GaboureySidibe Feb 21 '25

I write a lot of modern C++ and I don't have the problems that rust solves because I already can solve them with C++. Using templates and value semantics with no raw pointers (and no raw smart pointers) takes care of destruction and ownership.

Doing curated iteration and not deleting elements from a data structure or modifying keys solves a lot of the other problems. If you need bounds checking on std::vector access (because you are using computed indices rather than iterated indices), use .at().

These things basically take care of the memory problems that rust solves, then you can still use C++ with all its tools, libraries and ecosystem.

21

u/Ok-Scheme-913 Feb 21 '25

It doesn't scale, though.

I can write safe assembly as well, given I'm working alone on it and never doing anything wild. But we are past that for a good reason.

Nonetheless, using existing libraries is definitely a valid reason to keep using C++, though I do think that writing new code in rust (as the article is about) on top of existing codebase is a better choice.

1

u/GaboureySidibe Feb 21 '25 edited Feb 21 '25

It doesn't scale, though.

Says who? Values go out of scope and get destructed. Data structures are iterated. The program looks the same but you don't have to wonder if bounds checking is being secretly elided or not.

If by scale you meant harder to enforce technically across a team then I think you would have a point. I don't know how to use tools to weed out violating these techniques.

13

u/Full-Spectral Feb 21 '25

You are making the standard "just don't make mistakes" argument. And of course this is all about team based development, mostly commercial but also open source, where no amount of communications is sufficient to avoid issues over time. For someone doing a personal project, who can take all the time in the world, it's obviously less of an issue.

But still, even if you are doing a person project, you don't have to take so much time watching your own back if you just use a safe language. As someone who has written large C++ code bases for 35+ years and now is using Rust for my own personal stuff, the difference is enormous.

The argument shouldn't even be about, but wait, I can make C++ safe with a lot of effort. Tt should be why am I wasting my time doing something over and over that a compiler can do without fail every time I compile?

→ More replies (5)

5

u/Ok-Scheme-913 Feb 21 '25

I meant from a human perspective. Someone will change effectively the lifetime of an object at some place and violate some assumption made in another.

Without proper borrow-checking this can't be fixed.

2

u/GaboureySidibe Feb 21 '25

Someone will change the effective lifetime in C++ ? How do they do that if it is wrapped up as a value? You either move it out of scope or it gets destructed.

4

u/Full-Spectral Feb 21 '25

So you are passing everything by value to all calls and you never use unique pointers or allocate anything?

→ More replies (7)

1

u/Ozzy- Feb 21 '25

If by scale you meant harder to enforce technically across a team then I think you would have a point. I don't know how to use tools to weed out violating these techniques.

ASAN/UBSAN and static analysis. Level of enforcement will depend on your team and product.

4

u/i_am_not_sam Feb 21 '25 edited Feb 21 '25

Same, hence my question. I haven't seen a segv or a memory leak since I started using smart pointers. And the ownership concept with unique pointers already accomplishes Rust's borrow checking stuff. Someone else in this thread was saying even with modern pointers you can still run into memory issues and that just boggles my mind. While I'm sure Rust protects a developer from memory mistakes good coding practices will also accomplish a lot of that. Tellingly google says they used Rust and "strict memory practices". If I could rewrite any of my code bases from start knowing what I know now I'm sure I could accomplish safer code with just C++

12

u/Ok-Scheme-913 Feb 21 '25

They didn't rewrite their existing code though, and Google had a pretty strict coding standard to begin with, so the evidence doesn't agree with your take.

→ More replies (1)

0

u/[deleted] Feb 21 '25

[deleted]

9

u/GaboureySidibe Feb 21 '25

You didn't list any reasons at all here.

7

u/JustBadPlaya Feb 21 '25

I'm not a C++ dev, so I'm basing the comparisons to C++ on the literature I've read over time, but IMO:

  1. Much saner iterators. Iterator invalidation becomes a non-issue, the syntax is comfortable and I think Rust iterators are more consistently zero-cost than C++ ones.

  2. Move semantics are simple and ergonomic. Not sure how C++ does on that front, but I've heard there was a 50+ page book on its quirks or something.

  3. Sum types, be it Option/Result or others. AFAIK C++ got those but I've seen someone mention that dereferencing an std::optional can be UB which sounds annoying.

  4. On the note of UB - no UB in safe Rust, so I can trust that my code is correct.

  5. Minor language design nitpick - Rust is significantly more greppable

6

u/Full-Spectral Feb 21 '25
  1. Sum types

  2. Powerful (and exhaustive) pattern matching

  3. First class enums

  4. Destructive move (huge)

  5. Well defined and universally used project/module structure

  6. Immutability by default

  7. Implements various (practical) ideas from functional programming

  8. Very good slice support, which seems like a basic thing but it makes such a difference.

  9. Good tuple and newtype support

  10. Very flexible visibility control

  11. Full on support for Option/Result types, so no need for exceptions

  12. Non-duck typed generics

  13. Many iteration options

→ More replies (6)