News for hackers. Stuff that matters.
Re:The initial proposal for a C++ Ecosystem International St... by otabdeveloper4 on Tuesday October 18, 2022 @12:53PM (#33250376)
Rust doesn't solve any real problem.

It's a meme language that is only used for writing blog posts and internet commentary.

Re:The initial proposal for a C++ Ecosystem International St... by Karellen on Tuesday October 18, 2022 @01:30PM (#33250986)
So, Linus Torvalds, who rejected calls for code written in C++ to be accepted into the Linux kernel for decades because he thought that the extra features it provides were not worth the added complexity it would bring, has decided to allow Rust support in Linux because... he suddenly got infected with brain worms that made him want to be a crowd-following blogger writing commentary for internet upvotes?

And not because he thinks Rust might be able to solve actual problems in kernel development?

I want whatever drugs you're taking.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @02:08PM (#33251555)
To be completely honest, I would wager it was more a matter of ego than anything rational. C++ kept improving over time and he knew that, but rather than revise his position and admit to an error, he stubbornly persisted until Rust came along, and that gave him an out. "Well, C++ is too shitty; but Rust, a language that has not been around for even a decade, that's was I want to add support for in the kernel". Rust has pretty much all the failings he accused C++ of having, it just has memory safety as an additional benefit.
Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Tuesday October 18, 2022 @02:52PM (#33252141)
> Rust has pretty much all the failings he accused C++ of having, it just has memory safety as an additional benefit.

Memory safety is one hell of a benefit. In contrast, if you look at Linux' coding style, C++ really doesn't have any benefit over C.

Not to mention, many of C++'s advantages come from the STL, which wouldn't work without extra work in kernelspace. Rust has been designed for running on bare metal from day 1.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @03:11PM (#33252406)
You're talking about migrating kernel code to C++, but I'm talking about supporting C++ code in the kernel. That is, if third parties wanted to write Linux drivers, the only supported way was to do it in C.

That aside, the kernel does do polymorphism the hard way (with unions and arrays of function pointers) in a few places; the example I saw was some filesystem code, I think. C++ is also much more type-safe than C, mostly by refusing to implicitly convert between unrelated pointer/reference types and preventing you from inadvertently doing something absurd. References also make null checks unnecessary and convey to the caller that they're supposed to pass something valid, and RAII simplifies a lot of resource management. Templates also reduce boilerplate and are type-safe, although they're also a harder sell and not everyone's cup of tea.

I agree that memory safety is a huge advantage (although on the other hand, a double linked list in Rust is currently impossible without unsafe blocks), but I refuse to believe this decision has nothing to do with stubbornness.

Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Tuesday October 18, 2022 @04:42PM (#33253596)
> the kernel does do polymorphism the hard way (with unions and arrays of function pointers) in a few places

Using C++ classes has some disadvantages compared to C-style polymorphism - especially related to memory layout and allocation - in C++, memory is allocated by the code instantiating the object, not by the object's code, so you have to know the size of the type (including private fields) in any place where you are instantiating it. Not sure how relevant this is for kernel code.

> C++ is also much more type-safe than C, mostly by refusing to implicitly convert between unrelated pointer/reference types and preventing you from inadvertently doing something absurd

This part I agree with.

> References also make null checks unnecessary and convey to the caller that they're supposed to pass something valid

I think the distinction between references and pointers is one of C++'s worse ideas. You can't generally replace pointers with references either.

> RAII simplifies a lot of resource management

RAII doesn't work without bringing in a whole lot of other heavy-duty features: mainly exceptions, but also constructors and destructors.

> Templates also reduce boilerplate and are type-safe

Templates are more type-safe than macros, but not that much more (since the template language is essentially a dynamically-typed program, even though its output is a C++ AST that will be further type-checked). Like RAII, they also require you to use other features of C++, especially operator overloading, and likely also copy (and more recently, move) constructors.

> I agree that memory safety is a huge advantage (although on the other hand, a double linked list in Rust is currently impossible without unsafe blocks), but I refuse to believe this decision has nothing to do with stubbornness.

I do agree that stubbornness may well be a component. However, I believe that there is a coherent argument that can be made that supporting C++ for kernel modules is not worth it, while supporting Rust for kernel modules is. I'm not saying that this is the argument that Linus is making, but I do believe it can be made.

Especially since we also need to remember that all kernel modules are part of the kernel source tree. Supporting an additional language has a significant cost for everyone who is maintaining the kernel, since they need to add tooling to handle that extra language for all targeted platforms.

It should also be remembered that C++ compilers are even worse than C compilers at mis-using instances of undefined behavior to mis-optimize code, especially since they typically rely even more on optimization steps to have half-decent performance.

Re:The initial proposal for a C++ Ecosystem International St... by jcranmer on Tuesday October 18, 2022 @04:49PM (#33253693)
> RAII doesn't work without bringing in a whole lot of other heavy-duty features: mainly exceptions, but also constructors and destructors.

You don't need exceptions to make RAII work. It does make fallible resource allocation a little bit trickier (the RAII object needs to be prepared to be in a live-but-failed-allocation state), but it's very doable. Constructors and destructors are absolutely not heavy-duty features.

Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Tuesday October 18, 2022 @05:19PM (#33254150)
They are heavy duty precisely because they rely on exceptions to signal error conditions (or, in the case of destructors, are completely unable to signal errors safely at all), at least in combination with RAII.

Without exceptions, RAII is quite cumbersome - especially in kernel mode, where you can't assume that memory allocation is always successful, so even memory allocation is a fallible resource allocation. Not to mention, RAII makes it impossible to check whether you successfully released the resource, which is a concern that the kernel can't always ignore the way user-space C++ code usually can.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @05:58PM (#33254599)
The key to RAII is not so much constructors as destructors. If you want to be able to signal construction failure you just have an init() member function that returns a bool or some integer. As for dynamic allocation, when I worked on driver development for Windows, I just had an allocate<T>() function that returned a properly constructed std::unique_ptr<T> with the deallocation function properly set and with the T fully constructed (you may need more than one allocation function if you need to allocate from multiple contexts that require allocation in slightly different ways).

>Not to mention, RAII makes it impossible to check whether you successfully released the resource

Actually, RAII makes it much easier by encoding that knowledge into the owning object. Using the example above, you could call allocate<T, do_something_special_on_deallocation_failure>(). Then whenever the pointer goes out of scope you'll do the special thing without the user of the object having to know what that special thing is.

Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Tuesday October 18, 2022 @06:06PM (#33254683)
All of this complicates the program significantly, and greatly reduces the advantages of RAII. If you're not using the constructors for initialization, than your desturctors also have to handle the case where the objects didn't even attempt to acquire the resource they may represent, for example.

Also, how to handle a failure to release a resource is context dependent, you can't encode it in the type of the resource unless you create a different type for each call site.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @06:16PM (#33254786)
If you really need to do something different at each release point then you can have do_something_special_on_deallocation_failure() notify the error to the release point by a number of different mechanisms.

Yes, all of this makes the code more complicated. The point of these features is not to make the code simpler, but to make it more robust. Rust is also more complicated than C.

Re:The initial proposal for a C++ Ecosystem International St... by germandiago on Wednesday October 19, 2022 @02:59AM (#33258622)
Yes, without exceptions is more involved, but way easier than any C alternative doing couple-of-functions mstching for alloc/dealloc.
Re:The initial proposal for a C++ Ecosystem International St... by jcranmer on Tuesday October 18, 2022 @06:29PM (#33254939)
If you're relying on exceptions to make destructors fallible, you are going to have a very bad time. The protocol of throwing an exception is to destroy all objects in scope, and if doing so causes more exceptions to need to be thrown, you're going to end up either with a hard crash or one of the exceptions being destroyed. If the fallibility of resource deallocation is really important, then RAII isn't a good model to use (I prefer something like a connection.with_transaction([](Transaction &T) -> Result) model myself for this).

Given that fallible destructors aren't an issue, exceptions are only an issue for RAII if constructors can only fail with exceptions. Note that Rust has fallible constructors without exceptions, and C++ could implement that too, though it is definitely less ergonomic than exceptions.

Re:The initial proposal for a C++ Ecosystem International St... by usefulcat on Tuesday October 18, 2022 @11:50PM (#33257404)
> Not to mention, RAII makes it impossible to check whether you successfully released the resource, which is a concern that the kernel can't always ignore

Serious question: what does the kernel do if it fails to release a resource? Like memory, for example?

Re:The initial proposal for a C++ Ecosystem International St... by usefulcat on Tuesday October 18, 2022 @04:57PM (#33253825)
> You can't generally replace pointers with references either.

If this were generally true, I would expect to see a lot more use of pointers than references in c++ code, but in practice it's the exact opposite--it's rare that one needs to use a pointer rather than reference. Basically only if it needs to be able to be null or if it needs to be able to be modified.

Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Tuesday October 18, 2022 @05:40PM (#33254411)
In C code, you'll find many uses of pointers for iteration, which can't be replaced with C++ references. C++ typically also uses pointers in these cases, but wraps them in a layer of iterator templates (that is, hopefully, optimized out into a raw pointer access).

Not to mention, references obscure the difference between copying an object and sending its address at the call site, and obscure the difference between accessing a local object and a pointed-to object at the access site - a major disadvantage compared to pointers. Especially for a project which heavily uses email patches for code review and discussion.

Not to mention, there are programming errors that can lead to a NULL reference, and there is no way to guard against that in code using the reference (well, none that doesn't generate Warnings, and/or is liable to be optimized out) - whereas at least with pointers you can always check in your own code.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @06:05PM (#33254673)
Note that I was talking about using pointers to pass a single object by reference. In such cases references are 100% better than pointers, if the pointer is never supposed to be null.

>Also, there are programming errors that can lead to a NULL reference, and there is no way to guard against that in code using the reference (well, none that doesn't generate Warnings, and/or is liable to be optimized out) - whereas at least with pointers you can always check in your own code.

On the other hand, if you find you've received a null reference as a parameter then you know that something has definitely gone wrong, because unlike with pointers null references are impossible in the absence of UB. If all you know is that the function has received a null pointer then you could waste a lot of time trying to figure out how the program reached that state, when in actuality the problem is that someone else wrote out of bounds.

Re:The initial proposal for a C++ Ecosystem International St... by usefulcat on Tuesday October 18, 2022 @11:47PM (#33257376)
> In C code, you'll find many uses of pointers for iteration, which can't be replaced with C++ references.

In c++, ranged for loops are both far more common and idiomatic for iteration, and will compile to the most efficient form possible. At least for gcc and clang, you definitely don't have to 'hope' that they will be optimized to the equivalent pointer-based code--they will be.

> ... whereas at least with pointers you can always check in your own code.

Regarding null references -- the only way for that to happen is by dereferencing a null pointer, which of course is equally invalid with or without references.

BUT: if all you have is pointers, then--all else being equal--you will have many more places where you may need to check whether a pointer is null.

Once you've converted a pointer to a reference by dereferencing it, you (and the compiler) can legitimately assume that it's never null. If you ever encounter a 'null reference', the problem is never with the reference, it's always guaranteed to be with a deference somewhere higher up the stack. As a result, when references are available and preferred, there are not nearly as many places where you have to decide whether to check for null in the first place.

Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Tuesday October 18, 2022 @06:12PM (#33254747)
>It should also be remembered that C++ compilers are even worse than C compilers at mis-using instances of undefined behavior to mis-optimize code, especially since they typically rely even more on optimization steps to have half-decent performance.

Nonsense. Modern toolchains treat C and C++ as almost interchangeable. For example, Clang compiles both naively to a machine-independent IR and then performs all its optimizations on that. "C is portable Assembly" has been a myth for a long, long time. Nowadays C is effectively as high level as C++, just with fewer features.

Re:The initial proposal for a C++ Ecosystem International St... by tsimionescu on Wednesday October 19, 2022 @08:19AM (#33261023)
The Linux kernel uses GCC (for a long time, it used a specific version of GCC), and it uses some flags to disable certain notions of UB, some of which were particularly added for Linux. Do these work for C++ code? Possibly, but who knows.
Re:The initial proposal for a C++ Ecosystem International St... by fluoridation on Wednesday October 19, 2022 @10:49AM (#33263048)
So they had the GCC people define the behavior based on a flag. Since undefined behavior means that all behaviors are permissible, including defined behavior, I see no reason why any C++ compiler couldn't do this.
Re:The initial proposal for a C++ Ecosystem International St... by usefulcat on Tuesday October 18, 2022 @11:54PM (#33257420)
> It should also be remembered that C++ compilers are even worse than C compilers at mis-using instances of undefined behavior to mis-optimize code, especially since they typically rely even more on optimization steps to have half-decent performance.

Could you provide a concrete example of this? Personally I've not seen anything like what you seem to be describing.

Re:The initial proposal for a C++ Ecosystem International St... by account42 on Wednesday October 19, 2022 @07:04AM (#33260340)
> You're talking about migrating kernel code to C++, but I'm talking about supporting C++ code in the kernel. That is, if third parties wanted to write Linux drivers, the only supported way was to do it in C.

That hasn't really stopped anyone who wants to write their out of tree drivers in C++.

> That aside, the kernel does do polymorphism the hard way (with unions and arrays of function pointers)

C++ virtual functions are needlessly inefficient for interface classes where you only have a handful instances for each implementation (most commonly only one). Support for something like structs of function pointers with a little bit of sugar so you don't need to explicitly pass the this pointer would be very velcome even outside kernel space.

Re:The initial proposal for a C++ Ecosystem International St... by bluGill on Tuesday October 18, 2022 @03:06PM (#33252346)
Linux would benefit from a compiler to build a vtable where plugins are needed. Linux would benefit from a STL like library that might be home grown but will support more types than whatever they are doing now .
Re:The initial proposal for a C++ Ecosystem International St... by otabdeveloper4 on Tuesday October 18, 2022 @04:53PM (#33253759)
Wake me up when some real kernel module actually gets written in Rust.

(Actually, no, strike that, I don't want to sleep forever.)

Re:The initial proposal for a C++ Ecosystem International St... by hoten on Tuesday October 18, 2022 @01:05PM (#33250595)
I suppose Figma is a meme company sold for meme dollars?

https://twitter.com/figma/status/991697942070333447?lang=en

Re:The initial proposal for a C++ Ecosystem International St... by pjmlp on Tuesday October 18, 2022 @01:39PM (#33251100)
The money maker WebAssembly part on the browser is still C++.
Re:The initial proposal for a C++ Ecosystem International St... by otabdeveloper4 on Tuesday October 18, 2022 @01:07PM (#33250612)
Unironically yes.
Re:The initial proposal for a C++ Ecosystem International St... by jobs_throwaway on Tuesday October 18, 2022 @01:30PM (#33250969)
is memory safety not a real problem?