News for hackers. Stuff that matters.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by jhoechtl on Monday November 06, 2023 @07:31AM (#38161675)
There are many funny comments in here but I think they are all valid in a tragic-comic way. Rust is actually that hard that only a small share of developers will ever master it and those who master will be certainly the more intelligent one.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by agluszak on Monday November 06, 2023 @07:36AM (#38161748)
Rust isn't hard. Programming in general is hard. Rust makes this difficulty explicit.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by jvanderbot on Monday November 06, 2023 @07:38AM (#38161782)
This is a very good way of looking at it, actually.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by anonzzzies on Monday November 06, 2023 @07:55AM (#38161937)
Agreed, however, Rust is now treated like the hammer that also plagues js/ts. A GCd language is still just easier to use.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by cyber_kinetist on Monday November 06, 2023 @07:56AM (#38161948)
Or I would say, programming in general is already hard. Rust makes it even more difficult…

The reason is that actual common memory safety mitigation strategies in real life involve using some kind of smart pointers (same with Rust and C++), or using indices with bound checking (same with Rust and C++, it’s just that C++ STL has this off in Release mode by default…). For both languages, trying to avoid these runtime mitigation strategies (which affects performance) will require doing some level of unsafe stuff which the compiler doesn’t help with you (the unsafe semantics of the two languages are actually similar since Rust relies on LLVM which relies on C semantics). So ultimately, the borrow checker doesn’t seem worth it if you actually think about the cognitive overhead and headaches it brings. The only reason Rust has an edge over C++ is that it has sane defaults and a better standard library (ah only if C++ had a sane way of initializing values…)

Re:Rust newcomers are 70x less likely to create vulnerabilit... by MrJohz on Monday November 06, 2023 @08:38AM (#38162399)
One of the things I found interesting about this paper is that they find that there were more contributors in Rust projects. The first headline I saw posted from this paper was that first time contributors to Rust projects are 70× less likely to introduce vulnerabilities than first time contributors to C++ projects, and my immediate gut reaction was that this could be explained if the Rust developers were generally more experienced programmers. But as the abstract points out:

> We also found that the rate of new contributors increased overall after switching to Rust, implying that this decrease in vulnerabilities from new contributors does not result from a smaller pool of more skilled developers, and that Rust can in fact facilitate new contributors.

Which seems to imply the opposite of what you're suggesting, i.e. it implies that Rust makes contributing easier and safer than C++.

I also want to point out that your comment isn't accurate about how Rust's unsafe works:

> [T]rying to avoid these runtime mitigation strategies [...] will require doing some level of unsafe stuff which the compiler doesn’t help with you

(Emphasis my own.) This is inaccurate — the borrow checker is not turned off inside of unsafe blocks, but instead you are given a handful of extra APIs to use. Those extra APIs can be used to do things that the borrow checker would not normally allow (e.g. by converting checked pointers into raw pointers, and then inside the unsafe block, dereferencing those raw pointers), but the borrow checker is still active, and still catches all the same errors as before.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by cyber_kinetist on Monday November 06, 2023 @08:57AM (#38162612)
> the borrow checker is not turned off inside of unsafe blocks, but instead you are given a handful of extra APIs to use. Those extra APIs can be used to do things that the borrow checker would not normally allow (e.g. by converting checked pointers into raw pointers, and then inside the unsafe block, dereferencing those raw pointers), but the borrow checker is still active, and still catches all the same errors as before.

Yup I know that the borrow checker does also work inside unsafe blocks. But when what you primarily do as a systems programmer is establishing invariants in your systems so that you can exploit (or circumvent) the UB-ness of the underlying system (OS/driver/hardware/etc.), and when the borrow checker doesn't really help you with verifying these invariants... The Rustonomicon states that whenever you open up an unsafe block, it doesn't pollute only the scope, but the whole module (https://doc.rust-lang.org/nomicon/working-with-unsafe.html). So there can be a correctness issue outside the unsafe block because it disobeys the invariants implicitly set up by the developer inside the unsafe block. And you can't do anything about this other than carefully reasoning about all the various ways your implicit invariants can break inside the whole module. So unsafe is ultimately more of a convention (or a promise) that you have designed and verified your invariants correctly, so that you will not produce undefined behavior no matter how you use the module as an outside user. If you want to verify your invariants any further than that, you need to check UB at runtime using the Miri interpreter (which is really slow and still incomplete), or just use Ada SPARK.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by nicoburns on Monday November 06, 2023 @09:09AM (#38162760)
It does pollute the whole module, but this is much less of a big deal than it might seem because Rust modules are so lightweight - you don't even need a new file to create a new module, you can just do:

    mod name_of_module {
        // code goes here
    }
And it's often possible to wrap the tricky unsafe bits in a safe interface (that e.g. uses a mutex to enforce safety). So that anyone who is contributing to higher-level code doesn't need to worry about it. This is much better than C or C++ code where it's trivially easy to introduce memory unsafety or even Undefined Behaviour in even the boring "glue" parts of the codebase.

This leads to a really nice gentle onboarding flow where inexperienced users can start out contributing to the safe parts of the project, and (optionally) move on to gnarly unsafe bits later when they are already familiar with the project's codebase. It also dramatically reduces review workload for maintainers as they can rely on the compiler enforcing invariants outside of unsafe modules.

This works less well for really low-level code like embedded code or kernel code. But it's still a lot better than nothing.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by cyber_kinetist on Monday November 06, 2023 @09:34AM (#38163122)
Though my usual experience with writing performance-sensitive code is: if you just write naive inefficient code on the first try, there's a high probably that you need to rearchitect the whole system to get a more performant design, it's not something you can do incrementally. Maybe in these kinds of projects it's not wise to let random contributors handle your code though... (I'm geared more into graphics and numerical computing so the experience might be different from others.)

Maybe there's a reason why game developers have primarily used scripting languages - give out a safe managed GC-backed runtime for the majority of developers, and let only a select few who understand the system to develop the core C++ engine. Maybe Safe Rust can be used this way (as a "fast" scripting language), to separate between these two worlds... but the problems is even Safe Rust is just really difficult to grok for newcomers, and the hoops they go through to circumvent the borrow checker either falls into using Copy/Clone all over the place (slow) or smart pointers (slow) or array indices with bound checking (maybe less slow but more cumbersome, and also prone to logical invalidation errors if you're not careful)

Re:Rust newcomers are 70x less likely to create vulnerabilit... by estebank on Monday November 06, 2023 @11:22AM (#38164619)
> Maybe Safe Rust can be used this way (as a "fast" scripting language), to separate between these two worlds... but the problems is even Safe Rust is just really difficult to grok for newcomers, and the hoops they go through to circumvent the borrow checker either falls into using Copy/Clone all over the place (slow) or smart pointers (slow) or array indices with bound checking (maybe less slow but more cumbersome, and also prone to logical invalidation errors if you're not careful)

Are smart pointers like Box, Rc and even Arc any slower than any scripting system you'd "hand out" to most developers from your tightly written C++ core engine?

One thing that I see is that 90% of code is simple enough that you don't need to have any ceremony around ownership beyond writing a & in front of a value or type, 5% is harder than that but doable, and 5% requires extensive expertise to avoid allocations, or using Arc. I'd wager that the distribution of code that a GC can optimize during runtime is comparable, if not worse, at higher memory consumption.

I've compared some simple networked applications written in Java and Rust for that purpose, and performance ended up being comparable, but with 100x memory consumption, even when using GraalVM.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by jvanderbot on Monday November 06, 2023 @10:44AM (#38164073)
Rust is new. Low hanging features and bugs are going to be everywhere in new Crates. Contributing to, say, libcurl or openssl sure is more difficult than contributing to yet another rewrite of a mostly mature tool.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by hmfrh on Monday November 06, 2023 @08:23AM (#38162256)
> So ultimately, the borrow checker doesn’t seem worth it if you actually think about the cognitive overhead and headaches it brings.

You quite literally also have to "borrow check" C/C++ in order to have a well formed program, just without any compiler assistance.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by cyber_kinetist on Monday November 06, 2023 @08:44AM (#38162464)
For heap-allocated objects, there is a way to do borrow checks at runtime if you want (there's something called constraint references, explained in https://verdagon.dev/blog/raii-next-steps.)

For structs that I do not want to heap-allocate, they're usually POD types and in arrays (which you can bound check), so there's not much to think about borrowing. The more concerning issue I usually have is about about initializing the values correctly (which usually Rust doesn't help, when reasoning about performance-sensitive code).

Re:Rust newcomers are 70x less likely to create vulnerabilit... by estebank on Monday November 06, 2023 @11:28AM (#38164708)
> For structs that I do not want to heap-allocate, they're usually POD types and in arrays (which you can bound check), so there's not much to think about borrowing.

Both situations which also apply to Rust. Whenever the borrow checker complains you can do exactly the same thing.

> The more concerning issue I usually have is about about initializing the values correctly (which usually Rust doesn't help, when reasoning about performance-sensitive code).

You can use a type-state machine, where the only way to construct the final value is by calling all of the appropriate methods that change the type parameters on the Self type. When that gets compiled it ends up as either a single memcopy of values, or you can make the Self type hold a MaybeUninit value to make the partial construction with no copy at the end possible. I actually implemented that for fun and as it turns out already existing crates that held every field in an Option and then built the final value from those ended up being faster. C'est la vie.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by Tade0 on Monday November 06, 2023 @08:00AM (#38161994)
The only difficulty that I see in Rust is that sometimes I know that something is safe, but the compiler has to disagree based on what it is able to infer.

It's been a while since I last did something in Rust, but I remember that the shift to Rust 2018 was a huge step forward in this regard.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by rgoulter on Monday November 06, 2023 @08:11AM (#38162130)
I feel like Rust makes you pay a big cost upfront, and the benefits are often not worth that cost.

It's easier to program if I don't have to worry about ownership/lifetimes. -- Garbage Collection allows not needing to worry about lifetimes; and preventing the same value from being mutated from multiple places in the code generally isn't such a significant problem.

With high level programming (in JavaScript), you don't have to pay those costs.

I like to believe that Rust's strict discipline about ownership/lifetimes encourages code that's fast/lightweight, though.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by oneshtein on Monday November 06, 2023 @09:34AM (#38163112)
If programmers will learn Rust as first language, it will be natural to them. I use Rust since version 1.0, and I like that borrow checker helps me to find bugs at design state, where they are least expensive to fix.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Monday November 06, 2023 @12:49PM (#38165938)
> If programmers will learn Rust as first language

Imagine a person who just started learning about programming. A person who may still be tripped up by the fact that `=` and `==` are completely different things here.

Imagine that person.

And now imagine explaining to that person a language, that even many experienced and skilled software developers say is significantly harder to learn than the average programming language.

Sorry, but when I think about a good teaching language, Rust isn't the first thing that comes to my mind.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by baq on Monday November 06, 2023 @01:46PM (#38166900)
And neither is C/C++, in which you have to follow all the borrowing rules except the compiler maybe sometimes warns you about something instead of always erroring out.

Python, go or (a bit less so) typescript are nice beginner languages.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Tuesday November 07, 2023 @03:25AM (#38174483)
C++ isn't, but C was my first programming language, and I am thankful for this to this day.

Because C in and of itself, is an incredibly simple language. It is made complex in libs, usually by abusing macros, or by trying to shoehorn it into OOP, FP, or whatever other paradigm is currently being idolized.

> Python, go

Go yes, Python no, and I am saying that as someone who does alot of work in Python and loves both languages.

Because Python isn't simple. It looks simple in trivial code, but hides an enormeous amount of complexity and magic not-so-far beneath the surface. In addition, it completely fails at teaching core concepts of memory, and dances around the topic of types.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by baq on Tuesday November 07, 2023 @08:26AM (#38176452)
But C is anything but simple! It's small but it's so full of undefined behaviors it requires considerable knowledge and experience to stay within the bounds of 'predictable'. It's better nowadays if you are taught to enable all possible warnings and even then you'd better use a static analyzer and valgrind for anything more than hello world.

Python's hiding of memory is a feature when teaching beginners programming IMHO unless you want to start teaching close to the metal. I prefer to start from the algorithmic CS-y side.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Tuesday November 07, 2023 @08:48AM (#38176642)
> it requires considerable knowledge and experience to stay within the bounds of 'predictable'

No it really doesn't. Initialize all variables, always check boundaries and free memory when you're done with it. That about covers 90% of it.

Yes, this becomes more difficult the less trivial the codebase becomes. But beginners don't have non-trivial codebases, and teaching these concepts isn't hard.

> Python's hiding of memory is a feature when teaching beginners programming IMHO unless you want to start teaching close to the metal. I prefer to start from the algorithmic CS-y side.

And I prefer to start from the systems-programming, practical side. And in the practical world, memory isn't some abstract thing that may or may not exist, function calls cause overhead, strict typing is a friend, and networks fail.

So yes, teaching a bit closer to the metal has its advantages.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by oneshtein on Monday November 06, 2023 @02:03PM (#38167165)
Pascal, C, Java are used as first languages successfully. Why Rust cannot? It's even easier to use than C and Pascal.

Rust is hard for older programmers because it's hard to FORGET old habits from other languages, which allowed too much.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Tuesday November 07, 2023 @07:30AM (#38176039)
> It's even easier to use than C

No, it isn't, by any metric. I personally know people who learned C in a week. I learned all basics of the language in 2 weeks as a teenager, with nothing but K&R as learning materials.

If you disagree, point out by which metrics you consider Rust to be easier to learn than C, and we can have a discussion.

> Rust is hard for older programmers because it's hard to FORGET old habits from other languages, which allowed too much.

No it isn't. First, both newcomers and experienced programmers consider Rust a more complicated language than most of its contemporaries, and if anything a solif experience in multiple programming languages makes it easier to learn the advanced concepts Rust relies on.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by oneshtein on Thursday November 09, 2023 @03:47AM (#38202575)
Rust can be used as C, because it has pointers, like C. However, Rust has better syntax, more syntax sugar, automatic memory management, automatic memory manager and borrow checker. So, Rust is simpler than C to learn.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Saturday November 11, 2023 @08:17AM (#38230052)
> However, Rust has better syntax

Better by what metric?

> more syntax sugar

More features and syntactic concepts don't make a language easier to learn.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by db48x on Monday November 06, 2023 @09:36AM (#38163169)
Absolutely. Most programs should be written in a safe language, but that doesn’t mean Rust. Most programs should be written in a garbage–collected language like Javascript or Python or Lisp precisely because those languages make developers faster and more efficient.

Rust is only appropriate for programs that are too expensive to run when they are written in a garbage–collected language. That means operating systems, because small inefficiencies there will add costs for billions of people. It means big server–side services, where small inefficiencies cost you millions in compute time or ram usage or whatever.

Even if you think you’re going to hit that scale one day, writing your prototype in a garbage–collected language can net you a huge advantage in time saved. You can rewrite it in Rust later, after you know exactly what features will be needed and exactly what they cost to run, and exactly how much you’ll save by rewriting it in Rust.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by FrustratedMonky on Monday November 06, 2023 @09:19AM (#38162886)
Agree. Python is "easy" because it will gloss over a large number of 'errors' and just call it a day and let it run.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by robertlagrant on Monday November 06, 2023 @07:36AM (#38161746)
I don't know if this is true compared to C++. It's not as though it's easy to pick up a C++ codebase. Some languages, yes, but I wouldn't say C++ is one of them.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by ecshafer on Monday November 06, 2023 @07:38AM (#38161777)
Rust is harder than C++ to write a linked list in, or other “more complex than hell world” programs. But large applications i think it ends up being easier than C++. As far as mastering goes, i think far fewer people master c++ just on account of c++ being a massive language. Not only is it a massive language, but its a language with a spec, 3 major compilers, and not all of them agree on everything.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by oneshtein on Monday November 06, 2023 @09:47AM (#38163320)
Single linked list is easy to implement in safe Rust. Double linked list is the single linked list with additional pointer (instead of reference) back to parent node.

Pointers are unsafe, so use "unsafe" keyword when dereferencing them.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by anon23432343 on Monday November 06, 2023 @07:37AM (#38161765)
I'm not smart at all. Like avg at best.

But still I'm learning and using Rust for a project.

Its about the willingness to break that wall down you have in your head not about how intelligent you think you are.

The most intelligent devs at least from there own opinion have written the most shity code I have seen.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by EspressoGPT on Monday November 06, 2023 @07:33AM (#38161712)
More persistent, not more intelligent.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by rapsey on Monday November 06, 2023 @07:36AM (#38161750)
You don't need to master all aspects of a language to write code for it. The point is beginners who do not master all of it are not shooting themselves in the foot anywhere near as much as C++ beginners.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by baq on Monday November 06, 2023 @07:48AM (#38161870)
Um what does it say about C++ then? Getting a program to compile doesn’t mean it’s an easier language, it’s just deceiving
Re:Rust newcomers are 70x less likely to create vulnerabilit... by dvt on Monday November 06, 2023 @07:48AM (#38161878)
> Rust is actually that hard that only a small share of developers will ever master it

Kind of disagree here. Don't really think it's that much harder than C++, just hard in a very "annoying" way. Which, given its purpose, is a good thing. Making the borrow checker happy is mostly an exercise of going up and down method call chains to see where you screwed up. Or verbosely wrapping stuff in Arc<Mutex<T>> to make it a shareable resource.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by Run_DOS_Run on Monday November 06, 2023 @07:53AM (#38161916)
C++ is memory-unsafe and a Google search will not always return the latest and safest features, but raw pointers and C-like code. As a beginner, you often don't realize this. Rust makes it a little more difficult for you with "unsafe" blocks and explicitly points this out to you.
Re:Rust newcomers are 70x less likely to create vulnerabilit... by usrbinbash on Monday November 06, 2023 @07:56AM (#38161950)
> and those who master will be certainly the more intelligent one.

Rust isn't about being more intelligent, Rust is about being willing to jump through more hoops to get a guarantee that small subset of errors may not be in the executable.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by bborud on Monday November 06, 2023 @09:34AM (#38163117)
A good language amplifies talent - it doesn't require it. If, as you allude to, Rust requires above average talent for producing quality code, I'd say that as a language, it can only make a limited contribution. The only meaningful target audience for a language you hope will see mass adoption is the average programmer.

I don't have sufficient experience with Rust to determine whether or not only a small proportion of programmers will master it. However, if that is indeed correct, it also means that it wouldn't be worth the investment for most of the software industry to switch to Rust. Because mass adoption is, by its very nature, about what average programmers can master.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by kibwen on Monday November 06, 2023 @11:21AM (#38164603)
Rust fundamentally doesn't require any more intelligence than is required to write correct C or C++ code. It actually requires less, because you're offloading the mental labor of memory management onto the compiler.

If the assertion is that Rust is unsuitable for the average programmer, then the consequence would be that C and C++ are unsuitable for the average programmer... and yet average programmers have been writing C and C++ for decades.

I'm closer to an idiot than I am to a genius, and yet I write Rust all the time. You'll be fine.

Re:Rust newcomers are 70x less likely to create vulnerabilit... by bborud on Monday November 06, 2023 @01:40PM (#38166810)
I'm fairly certain you are correct.