News for hackers. Stuff that matters.
Re:Advancing Memory Safety by hu3 on Tuesday October 15, 2024 @03:44PM (#41852374)
It's more than that because rust evangelists often preach replacing GC'd languages with rust in cases where it is not the best fit (mundane web dev for example).

So it's not only about C/C++.

Re:Advancing Memory Safety by timschmidt on Tuesday October 15, 2024 @04:12PM (#41852627)
Part of the magic of learning Rust is realizing that it's a fast compiled language that one doesn't fear to apply to services connected to the open internet. This is so unusual, it may be unique. And I think that leads to a lot of experimentation and application of the language to the sorts of problem which typically receive attention in memory safe languages, but not compiled languages. Rust, by definition, straddles both.
Re:Advancing Memory Safety by hu3 on Tuesday October 15, 2024 @04:19PM (#41852682)
> Part of the magic of learning Rust is realizing that it's a fast compiled language that one doesn't fear to apply to services connected to the open internet. This is so unusual, it may be unique.

Is that sarcasm? If not, thanks for proving my point.

From the top of my head, Go/.NET/Java are fast and safe choices. Also safe in the sense that you won't have a hard time hiring devs that are productive from the get go.

Re:Advancing Memory Safety by timschmidt on Tuesday October 15, 2024 @04:25PM (#41852736)
> often preach replacing GC'd languages with rust in cases where it is not the best fit (mundane web dev for example).

> proving my point.

Was your point misunderstanding that garbage collection != memory safety? Having garbage collection doesn't make a language any better for web dev. Being memory safe does. If you do not understand the difference and needlessly associate the two, you are validating the work put in by the evangelists.

Re:Advancing Memory Safety by hu3 on Tuesday October 15, 2024 @04:32PM (#41852795)
> Was your point misunderstanding that garbage collection != memory safety?

What?! You really think memory safety is binary?

> Having garbage collection doesn't make a language any better for web dev.

It does though. It's more ergonomic than fighting rust compiler and does add safety over manual memory management from the likes of C/C++.

I just want to thank you for demonstrating so clearly how rust evangelists think.

Re:Advancing Memory Safety by timschmidt on Tuesday October 15, 2024 @04:38PM (#41852845)
> What?! You really think memory safety is binary?

Not sure why you'd assume that. The languages you offered are only as memory safe as their runtimes, which are usually implemented in something C flavored, and the compiler for same. Rust identifies memory unsafe areas instead with the unsafe tag. Functionally equivalent but I know which I like more.

> It does though. It's more ergonomic and does adds safety over manual memory management.

This is the misunderstanding. Rust manages to be wonderfully ergonomic without GC. The type system is really the only thing that makes it wordier than C++. And I've written large Rust applications with no manual memory management whatsoever. Occasionally I'll have to wrap something in an Arc Mutex because something needs to run in another thread. One or two variables in a whole application.

Your complaints apply to other languages.

Re:Advancing Memory Safety by hu3 on Tuesday October 15, 2024 @04:55PM (#41853011)
> This is the misunderstanding. Rust manages to be wonderfully ergonomic without GC. The type system is really the only thing that makes it wordier than C++

Thanks for the laugh. Maybe lifetimes/borrow checking syntax got invisible after a while to you. The only explanation.

I'll stop here. Don't have the time nor desire to argue against what seems like passionate opinions.

Re:Advancing Memory Safety by timschmidt on Tuesday October 15, 2024 @04:59PM (#41853056)
> Maybe lifetimes/borrow checking syntax got invisible after a while to you.

I just did a quick scan, in a couple thousand lines of Rust I see two, maybe three lifetime annotations. Nah, not a huge contributor to language verbosity. Most Rust folk seem to work with the compiler's notion of what lifetimes should be, and everything's implicit.

Re:Advancing Memory Safety by vlovich123 on Tuesday October 15, 2024 @09:11PM (#41854705)
Lifetime syntax is only more visible if you try to write high performance code. If you wrap everything in Rc<RefCell> or Arc<Mutex> or Box you’ll not see it show up anywhere but you’ll probably still get a performance profile better than tracing GC because time and time again it’s been shown that reliable tail latencies and peak throughputs are generally harder to achieve with a tracing GC. Where lifetimes start to pop up more is in more complicated situations where you are trying to avoid memory allocations and have even higher performing code.

And op is correct. In terms of memory safety, Java and C# aren’t as safe as you might hope because significant chunks of the runtime are still in C/C++ (not true for Go) and because of JIT miscompilation bugs (also not relevant for go). Of course JIT miscompilation in theory should be the same as a compiler miscompilation, but in practice it turns out to be worse because you have to maintain consistency between the interpreted and all the tiers of JIT at runtime and in practice humans have a hard time doing that which is why V8 constantly has security vulnerabilities in this area. That’s why GraalVM is interesting because it proposes a JIT design where you only have the compiler implemented once.

Re:Advancing Memory Safety by neonsunset on Wednesday October 16, 2024 @12:31AM (#41855619)
Go is not magically exempt from constraints other languages are subject to, no matter how much blind faith is put into this mantra.

Java has different compiler implementations not all of which necessarily go through interpreter stage. .NET CoreCLR does not have one in the first place (there is technically interpreter code in the codebase but it has bitrotted and is unused). JIT or not does not make it inherently more prone to miscompilation bugs. One thing that Go has going for its compiler is it is a massively simpler design. The downside of that, of course, is much worse compiler output that cannot cope with complex code and is inadequate at high-performance scenarios, where the users of Go either have to try to work around this with goasm or might not even have a solution at all, short of rewriting the component in a different language and praying that the FFI call overhead is low enough for the workload in question.

In addition to that, runtime implemented in C/C++ vs Java/C# or other language does not make it inherently safer. It is always a lower-level flavour of the language, where it cannot depend on itself, and with likely numerous relaxations in assurances to stay performant that the authors uphold manually. Even when C or C++ are used - it's usually a limited, focused subset of the language. And it is a bold claim that these runtimes are not extensively tested, fuzzed and otherwise verified against defects before going to production. If you look at bug and CVE history of .NET - pretty much all issues happen in complex "non-core" APIs, often at the unmanaged side provided by operating system rather than .NET itself (the cryptography bits). I can't recall any that are directly or indirectly related to a compiler bugs.

I feel like this reply ignores the wider context, which is plain old web dev, where the use of Rust and the decision fatigue that it carries is likely to be misplaced. Just pick C# or Kotlin.

Re:Advancing Memory Safety by pjmlp on Wednesday October 16, 2024 @02:50AM (#41856271)
Like the Rust compilers partially written in C++.
Re:Advancing Memory Safety by consteval on Wednesday October 16, 2024 @01:30PM (#41861582)
> If you wrap everything in Rc<RefCell> or Arc<Mutex> ... performance profile better than tracing GC

I seriously doubt it. Naively incrementing Ref counters is extremely expensive. Tracing GCs in modern languages are very advanced. The least you could do is try to elide some of the ref increments - but you can't do that in Rust. Also boxing every object is very unpleasant. If you're gonna do that, use a language with reference semantics, like Java or C#

> significant chunks of the runtime are still in C/C++

Means absolutely nothing about the safety of the languages themselves. Just because C# has a runtime written in C++ does not mean that you can now magically compile and run use after frees or whatever.

Re:Advancing Memory Safety by steveklabnik on Wednesday October 16, 2024 @01:46PM (#41861735)
I don’t fundamentally disagree with your points but I do want to point out that in Rust, you have far less recount traffic than in a language that uses reference counting as a GC strategy, because you only increment and decrement on new owners, and not on every access. You can take a regular old &T to the interior of an Rc<T> at any time.
Re:Advancing Memory Safety by consteval on Wednesday October 16, 2024 @06:11PM (#41864345)
I didn't know this, that's pretty cool. Definitely more manual than what Swift is trying to do with RC elision since you have to then deal with runtimes, but also, I'd imagine much more performant due to a finer-grained nature.
Re:Advancing Memory Safety by steveklabnik on Wednesday October 16, 2024 @07:51PM (#41865070)
Yes, and beyond that, you also have the Rc<T> vs Arc<T> split, so if you're not doing multi-threaded, it's not even an atomic increment/decrement.

That said, this is just my intuition here, I've never seen a good benchmark trying to do the comparison, so I don't want to over sell it either. But a lot of times, it's like, "okay I have four threads so I need the refcount to go to four" and then it's just using normal references to actually use the value inside.

Re:Advancing Memory Safety by vlovich123 on Friday October 18, 2024 @08:48PM (#41884754)
I’ve done thorough benchmarking of Rc and Arc.

Rc is cheap to the point of free unless it frees - it’s literally just an addition or subtraction with no data dependency in the path. Your CPU can do > 6 billion of these per second on a 6Ghz CPU (if I recall correctly you could do 3 per clock cycle in a micro benchmark due to out-of-order but at a minimum you should be able to do 1 per cycle).

Arc is quite expensive, particularly on x86 because of its memory model. You can maybe do a few million of them per second. But as you said, you shouldn’t be incrementing ownership once you’ve sent it to a new thread and instead using references directly or using something Rc-like to track the lifetime (i.e. when the current thread’s Rc count hits 0, release your Arc reference).

Re:Advancing Memory Safety by timschmidt on Wednesday October 16, 2024 @02:34PM (#41862230)
> Means absolutely nothing about the safety of the languages themselves. Just because C# has a runtime written in C++ does not mean that you can now magically compile and run use after frees or whatever.

Runtime bugs are potentially exploitable, even from within the language implemented by the runtime. But I don't think anyone's suggested that they'd present as simply as you suggest. Many such bugs have been found in runtimes of many languages.

Compiler bugs can be similarly exploitable, with the caveat that any code eliminated at compile time won't be accessible at runtime, so compiled languages will tend to have a smaller surface area for exploitation in general.

Re:Advancing Memory Safety by vlovich123 on Friday October 18, 2024 @09:01PM (#41884812)
> Means absolutely nothing about the safety of the languages themselves. Just because C# has a runtime written in C++ does not mean that you can now magically compile and run use after frees or whatever.

As security experts like to repeat, it’s a numbers game. You only have to make a mistake once to result in an insecure system. And C++ encourages you to make those mistakes.

Same with JIT - because the compilation happens at runtime, a common attack mechanism is to exploit a particular bug in the JIT implementation so that the generated code violates some assumption and results in unsafe execution. This happens all the time with V8 and a large part of that problem comes from having to implement the language twice (once in the interpreter and once in the JIT and any deviation between the two implementations is an opportunity to exploit a vulnerability). This isn’t something I made up. This is from talking to people who are familiar with this and reading analyses by people who know what they’re talking about.

Compilers btw also have miscompilation bugs all over the place. The reason it’s less of a problem is because the code being executed isn’t “malicious” - it’s your code and you’re not going out of your way to structure code to find bugs in the compiler. This also protects most JIT and why most C# programs don’t have this problem - they’re not loading arbitrary code at runtime to execute. It is a particular problem for V8 though where that’s an expected behavior.

Re:Advancing Memory Safety by surgical_fire on Tuesday October 15, 2024 @04:55PM (#41853008)
Thanks for proving my original point.

I rest my case.

Re:Advancing Memory Safety by timschmidt on Tuesday October 15, 2024 @05:06PM (#41853128)
OK, dude. If you never correct your misconception, you can be annoyed when people attempt to educate you forever X)

No amount of complaining about Rust evangelists will change how they use the language, but you could learn to understand why they're using it the way they are.

Re:Advancing Memory Safety by pjmlp on Wednesday October 16, 2024 @02:50AM (#41856267)
Rust runtime is partially implemented in a C++ toolchain, should we use the same arguement then?
Re:Advancing Memory Safety by timschmidt on Wednesday October 16, 2024 @03:48AM (#41856602)
Yes, Rust does depend on Clang for the lowest level of compilation at the moment. MIR (where the borrow checker lives and most of memory safety is implemented) and HLR are both implemented in Rust.

What argument? Seems more like a misunderstanding to me.

Re:Advancing Memory Safety by pjmlp on Wednesday October 16, 2024 @05:18AM (#41857108)
Using your argument, Rust can't be fully trusted as long as its compilation model and runtime depends on having C++ somewhere.

In fact, that has broken Rust compilation semantics quite a few times already.

Re:Advancing Memory Safety by timschmidt on Wednesday October 16, 2024 @07:13AM (#41857783)
I don't think I've made any contradictory argument. Perhaps someone else did? Or maybe you assumed a binary where I see gradients. I'm quite sure I didn't claim anywhere that any language was perfectly memory safe. With Rust and Cheri we are getting closer, but there is still progress to be made.

I can say that Rust is well architected such that the borrow checker and most of memory safety is implemented in the midlevel intermediate representation, which is implemented using Rust, and the bits of Clang upon which MIR and HLR are built are both well tested by virtue of being used by multiple language frontends, and limited in scope and modularity. It'd be great to see an all-rust replacement though, as bugs in Clang have been found. I believe there are several projects in the works and existing pure-Rust bootstrap compilers sufficient to get the toolchain working.

Re:Advancing Memory Safety by germandiago on Wednesday October 16, 2024 @06:56AM (#41857670)
Rust, like absolutely any language, needs an unsafe base on which to implement safe abstractions.
Re:Advancing Memory Safety by germandiago on Wednesday October 16, 2024 @06:56AM (#41857673)
> Rust manages to be wonderfully ergonomic without GC

Hahahahaha! That's funny.

Re:Advancing Memory Safety by germandiago on Wednesday October 16, 2024 @06:54AM (#41857652)
Java, C#, Go...

Not the only one. Maybe the fastest, yes. But... at what cost?

Also, I think people seem to believe that memory safety is all safeties. Attacks come in many shapes and forms, from supply chain injection, to SQL injection, to unsanitized input, overflows, secret leaking... memory safety is one of the weak points of a chain, but not the only one.

Re:Advancing Memory Safety by timschmidt on Wednesday October 16, 2024 @07:18AM (#41857810)
> Java, C#, Go...

All require runtimes. Which means they're not in the same class of language as C, C++, and Rust.

> Also, I think people seem to believe that memory safety is all safeties.

It's 70% of CVEs in C and C++. People focus on it because it's the biggest single category. Enums and Option types and the whole type system for that matter goes a long way toward addressing logic bugs in Rust as well.

Re:Advancing Memory Safety by consteval on Wednesday October 16, 2024 @12:59PM (#41861292)
> All require runtimes. Which means they're not in the same class of language as C, C++, and Rust

Right, which is why it makes no sense to say "no don't use C# for webdev!! Use Rust!"

They're different classes of languages. If C# is good for that use case then use it. Also there are genuine benefits to having a runtime.

Re:Advancing Memory Safety by timschmidt on Wednesday October 16, 2024 @01:42PM (#41861688)
> it makes no sense to say "no don't use C# for webdev!! Use Rust!"

Well, first of all, I don't see anywhere where anyone said that. Perhaps in another thread? People did argue against the use of Rust for web dev, for which it's a perfectly reasonable choice among many.

Second, I have an embedded HTTP server running on a microcontroller with 500kb of ram in my current project. Works great in Rust. Simply not possible in C#.

> If [any language] is good for that use case then use it.

Agreed. The best camera is the one in your pocket, and the best language is the one you know.

> Also there are genuine benefits to having a runtime.

Things which are commonly associated with runtimes - memory safety, automatic memory management, various type systems, high level language abstractions - are all genuinely useful. Folks in this thread seem to want to hold on to a categorization of languages which Rust breaks by virtue of providing features typically only found in languages with runtimes. What Rust shows is that those benefits can be a part of a well designed language, with or without a runtime. And not having a runtime is a hard requirement in areas like embedded, OS development, realtime, etc. There is typically a separation between languages you'd use for that sort of thing and languages you might use for web dev, but the reasons for that separation do not apply to Rust. Which is fun.

Some people seem to be salty about it though, lol.

One significant downside to having a runtime is that it significantly complicates the process of program verification. Making it much harder to do statically. Necessitating certain things happen at runtime like error handling and performance profiling. Rust mostly has what it needs to get that work done at compile time, which means the developer has an opportunity to fix it, before some user runs into it.

Re:Advancing Memory Safety by consteval on Wednesday October 16, 2024 @05:12PM (#41863901)
> with or without a runtime

This is the point - no, you actually DON'T get all the benefits of a runtime. C# famously, and extensively, used runtime code generation. Not possible in Rust, kind of possible in C++.

> Necessitating certain things happen at runtime

Not how it works. The language is still compiled. You can 100% implement a borrow checker for C#, there's nothing stopping anyone. It's just not what they want to do.

For example, C++ and C are BOTH compiled languages. But C++ is able to verify the type of various operations at compile-time, but C has to wait until runtime. For example, C's qsort takes void pointers and then you cast and dereference them at runtime. In C++, this is physically baked into the type of function template std::sort.

But they're both compiled languages with no runtime. But because C++ has a much more complete type system, it can do those things at compile-time while C cannot.

Another example: while C# has polymorphized generics, it checks them at compile time! The generics are not monomorphized like C++ or Rust, which allows smaller linked libraries that you can load and use at runtime, which is not possible in C++ templates or Rust generics. The trade-off is then that generics can't be checked until runtime - but actually no. The C# compiler will go out of its way to check generics when it can and will fail if they don't meet the type constraints, just like C++ or Rust.

Rust is very powerful, BUT:

1. It doesn't cover all the usecases of more powerful languages with rich runtimes like C# or Java

2. Using async rust, which is typically a requirement for webdev, requires a runtime anyway.

Re:Advancing Memory Safety by neonsunset on Wednesday October 16, 2024 @07:11PM (#41864841)
> Another example: while C# has polymorphized generics, it checks them at compile time! The generics are not monomorphized like C++ or Rust, which allows smaller linked libraries that you can load and use at runtime, which is not possible in C++ templates or Rust generics. The trade-off is then that generics can't be checked until runtime - but actually no. The C# compiler will go out of its way to check generics when it can and will fail if they don't meet the type constraints, just like C++ or Rust.

Generics are not monomorphized at bytecode level, but whenever generic argument is a struct, the methods and types that have it are monomorphized at the stage of compiling to machine code, be it with JIT or ILC. This is identical to Rust, and is why C# has "zero-cost" abstractions.

Method bodies and types for class-type generic arguments are indeed shared however. Dispatch on generic constraints is still quite efficient in that case, even if no longer zero-cost (unless the compiler is able to see the exact type and inline such calls, or emit a guarded devirtualized fast-path when compiled by JIT, or prove that only few types implement a particular interface or subclass a specific parent when compiled by NativeAOT).

Re:Advancing Memory Safety by germandiago on Wednesday October 16, 2024 @01:24PM (#41861516)
Yes, the infamous C/C++... what a trick...
Re:Advancing Memory Safety by tormeh on Wednesday October 16, 2024 @05:25AM (#41857141)
I think Rust is a good fit for webdev, if you have programmers who already know or actively want to learn Rust, and if you don't need libraries not available for Rust. Admittedly those are some big caveats, but better latency and throughput are never bad things. The lack of GC is not actually a problem once you have internalized the rules of the borrow checker.

The only big problem I see with Rust that makes it a bad fit for webdev is the young ecosystem. The compile times are not great, but I don't think that's a significant issue when writing APIs.