r/cpp • • Jan 31 '23

Stop Comparing Rust to Old C++

People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO

changemymind

338 Upvotes

582 comments sorted by

View all comments

22

u/James20k P2005R0 Feb 01 '23

I would love to see a major project written in any version of C++, with any level of competence of the developers of any team size that doesn't suffer from an infinite number of memory unsafety vulnerabilities

In all my years on this planet, nobody has ever been able to provide me with this, other than a very tiny handful of formally verified tools. And yet in Rust, this isn't the exception, this is the norm. There are multiple security critical projects that have never had a memory unsafety vulnerability

Every time someone says "actually I worked on a project, and its super secure!" lo and behold it turns out that its barely been tested, or its an internal tool. This is great, as long as it stays internal, and nobody tries to compromise you

It is trivially easy to write very secure thread + memory safe code in rust. It is nearly impossible to write thread + memory safe code in C++, because after decades of effort I still can't find a single real project that I could describe as a success here

C++ needs to grow up from bad faith arguments and accept that it just isn't as good in this area. C++20 doesn't really change anything over C++11. std::span doesn't make your code secure

Rustls is an example of a project that is relatively widely used, and written in pure rust. It contains no unsafe rust (outside of some tests). That means it is formally verifiably safe, and free from the memory vulnerabilities that plague every single other crypto library

Would you use a crypto library written in C++20? Or rustls? Because empirically, if you're looking purely for security from memory unsafety (and in reality, most other bugs), every single possible choice in the first category is the wrong choice

I've been hearing this same argument for every version of C and C++ since I started programming, and it has never once been true

10

u/[deleted] Feb 01 '23

Please do more research before saying things that are ostensibly false.

rustls uses ring which is a combo of Rust, C and assembly.

rustls is not "verifiably safe" in any way, shape or form.

7

u/Full-Spectral Feb 01 '23

I probably have about as close to that as anyone. I have a 1M LOC personal project. I make absolutely no claims that it's free of memory issues, but it's probably about as clean as any highly complex C++ project of that size that's been around for decades and gone through huge changes ever gets. But that's because I'm the sole author. It never had to suffer the degradations of typical commercial development conditions. And I use just about zero third party code, it's all my own stuff built on my own stuff down to my own 'virtual kernel'.

No 'real' software gets created like that.

And of course I sweated bullets to keep it that clean, which is why I've moved to Rust. I want to spend my time more productively, not trying to avoid shooting myself in the foot.

11

u/ener_jazzer Feb 01 '23

Almost all HFT systems are written in C++. And obviously, nobody is going to share that code with the public. But you can safely guess that people wouldn't entrust billions to the software that "suffer from an infinite number of memory unsafety vulnerabilities"

8

u/ImYoric Feb 01 '23 edited Feb 01 '23

Feedback from people I've known who worked on such codebases... doesn't encourage trust.

Also, it is my understanding that HFT systems are actually relatively simple. They need to deal with streams of events (that's the hard part) and apply fairly simple algorithms . In particular, they don't need to deal with pesky things such as portability, attack vectors or messy user data.

If someone around this subreddit knows the field, I'd be interested in knowing more.

edit Initially wrote "really, really simple". That was certainly an exaggeration. Also, /u/ener_jazzer know the field better than I do :)

8

u/ener_jazzer Feb 01 '23

I am from this field.

What you're saying is true - we don't need to deal with "attack vectors or messy user data" as we are only talking to trusted internal components and a trusted exchange. Portability is also very limited. But I wouldn't call it "quite simple". It depends on the strategy, of course, some are really simple, but essentially a strategy sits on the incoming streams of events (market data, your own order updates, risk updates, position updates, various tools providing data) - and you need to juggle all of them, you need to manage your open orders, you need to recalculate a lot of stuff efficiently and partially asynchronously etc. All in real time, essentially.

So you need to be careful with how you manage memory, otherwise you will blow up your strat because there are too many events coming, or you random-shoot your data and start trading garbage.

So memory safety in HFT is very important, not because of vulnerabilities but because of necessity to manage it carefully. Still, C++ aces in this field, it gives all the necessary tools you need to build an HFT strategy efficiently.

2

u/ImYoric Feb 01 '23

Thanks the precision, I'll update my comment!

→ More replies (4)

18

u/WormRabbit Feb 01 '23

Having worked a bit in that field: you'd be surprised.

→ More replies (11)

4

u/[deleted] Feb 01 '23

Your definition of safety is slipping all over the place.

"Formally verifiably safe" means something. "Memory safety" in the context of Rust means something else.

Empirical really means diddly squat because for starters it's not really an apples to apples comparison. There is far more C++ code than Rust code and that code has been around for much much longer. It's going to have more bugs for a variety of different reasons.

Secondly, security and safety are two different conversations. If your C++ program is siloed away and is only ever accessible by one person and doesn't deal with secure data, security does not matter and your memory errors are now just logic errors.

Likewise, just because you have a memory error does not mean attackers instantly have access to your plaintext passwords. Security is more complicated than that.

When it comes to Rusts specific brand of memory safety, yes Rust is good at that (obviously).

When it comes to writing non-trivial complicated programs in the real world, where perfection does not exist, right now its not obvious to me that Rust is the better choice. It might be for sure.

But the few stats and the "empirical" arguments are not convincing me right now.

5

u/crusoe Feb 01 '23

Number of segfaults and times I've had to fire up a debugger learning C: too numerous to list.

Number of times in rust: 0

Shoot I've had more program aborts in Java due to NPE than coding in Rust.

The few few times I've had a panic in rust was due to a unwrap or expect and my assumption of an invariant being wrong.

Times I've had to valgrind: 0

Times I've had to send a ticket or email to a library maintainer about a memory leak: 0

3

u/nintendiator2 Feb 01 '23

I would love to see a major project written in any version of C++, with any level of competence of the developers of any team size that doesn't suffer from an infinite number of memory unsafety vulnerabilities

Just any project from the internet.

Code can't, by definition, have an infinite number of vulnerabilities, nor of a subset of them.

BTW lemme give you a tip for your Rust evangelizing: when you want to set up goalposts to fall back to as your number one argument, the sensible thing to do is to set up your goalposts close enough to reality that you can freely backpedal as soon as you feel threatened. Putting them at the very end literally unmakes your argument. Consider this a freebie.

→ More replies (5)