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.

4

u/UncleMeat11 Feb 21 '25

Depends on how strict you want to be.

The strictness required to actually achieve the same guarantees in C++ is unfortunately ludicrous.

10

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

I hear this a lot... what exactly are you referring to? Pointers? Smart pointers solve ownership and leak issues out of the box. Not using them as a raw pointer isn't a very difficult practice. Bounds checking on pre-allocated data structures? Not terribly hard either. There are so many compile time checks that can be achieved with templating. I could go on, but C++ has all the tools you'd need, and they're not as complicated as they're made out to be

16

u/UncleMeat11 Feb 21 '25 edited Feb 21 '25

Smart pointers solve ownership and leak issues out of the box.

No they don't.

Write a function that takes an argument by reference and returns that argument by reference. Pass a temporary to this function. Boom, use-after-free. No heap allocations necessary. [[clang_lifetime_bound]] exists, but it isn't an actual part of the C++ language.

Write a function that takes two vectors by reference. It mutates one while iterating over the other. Oops, you passed the same vector in both arguments and now you invalidated its iterators and accessed memory out of bounds.

There are oodles of such examples. The idea that if you just replace all "new" keywords with "make_shared" that you are free from memory errors is not based in reality.

Bounds checking on pre-allocated data structures?

You can do this by replacing all statically allocated raw arrays with std::array and dynamically allocated arrays with std::vector. But iterators are an incredibly common pattern in C++ code, even in the STL. You can't bounds check that your begin() and end() iterators passed to some function are safe. They are just pointers. They might not even have come from the same object.

→ More replies (4)

15

u/simonask_ Feb 21 '25

Achieving Rust-level safety in C++ is totally intractable because fundamental designs in the language prohibit it. For example, C++ standard iterators fundamentally requires aliasing a block of memory.

The next best thing is to apply a level of rigor that is simply too expensive.

The thing you get from Rust is a massive productivity boost to support that level of rigor that is required to avoid undefined behavior in a systems programming language. Several tools enable that: borrow checker, thread safety in the type system, greppable unsafe { } blocks, and low-friction encoding of invariants in the type system. These are all very, very useful things that cannot be achieved in C++, and together make it feasible to do way more with way more confidence.

3

u/KuntaStillSingle Feb 21 '25 edited Feb 21 '25

There are some cases which are technically unsafe that border on trivia, for example:

#include<utility>

struct non_copy_or_moveable {
    non_copy_or_moveable() = default;
    non_copy_or_moveable(non_copy_or_moveable const &) = delete;
    non_copy_or_moveable(non_copy_or_moveable &&) = delete;
    //non_copy_or_moveable(int) {}
};


template<typename ... Arg_ts>
auto foo(Arg_ts&& ... args){
    return non_copy_or_moveable(
        std::forward<Arg_ts>(args)...
    );
}

int main(){

}

This is ill formed-ndr, unless you uncomment the int converting constructor for non_copy_or_moveable. Can you guess why?

>(6) The validity of a templated entity may be checked prior to any instantiation. ... The program is ill-formed, no diagnostic required, if ...

(6.5) every valid specialization of a variadic template requires an empty template parameter pack, or ...

https://eel.is/c++draft/temp.res

Because the only valid specialization of foo has an empty arg pack (any non empty arg pack would result in argument's to non_copy_or_moveable's constructor, and because the copy and move constructors are deleted and there are no other constructors besides default, none of them can be well formed.) the entire program is ill formed, despite that foo is never called and one would expect it is safe to fall foo with no args (i.e. foo()), and a simple compile error otherwise (no overload matching arg set ...).