News for hackers. Stuff that matters.
Re:Beej's Guide to C Programming by swatcoder on Sunday February 26, 2023 @05:08PM (#34950071)
The key difference to me is where you use “probably”. Rust and even WASM reflect a lot of the system behavior that’s become the norm among modern hardware platforms.

But those norms are still not universal and it’s useful to maintain an understanding of other viable architectures. C was born from a time of of extremely high innovation and experimentation in hardware architecture and it’s baked many of those possibilities into its language. Often, those are the very footguns that can make writing commercial software in C something to be considered carefully, especially because performant and safer alternatives like Rust exist.

Yet, using C is still sometimes the right choices for commercial software and remains a unique instrument for privately expanding one’s understanding of system and hardware architecture.

The quoted claim about C doesn’t have to be wrong just to argue that Rust might often be a better choice in practice for commercial bare metal programming on common systems. It’s making a subtly different claim, and the difference can matter.

Re:Beej's Guide to C Programming by saagarjha on Sunday February 26, 2023 @05:50PM (#34950530)
Amusingly Rust is needing to revisit some of the earlier choices it made that were “obvious” on modern hardware architectures, because we have even newer ones that don’t behave that way.
Re:Beej's Guide to C Programming by int_19h on Sunday February 26, 2023 @07:01PM (#34951174)
Can you give some examples?
Re:Beej's Guide to C Programming by saagarjha on Monday February 27, 2023 @12:00AM (#34953171)
Here's a recent one: https://faultlore.com/blah/fix-rust-pointers/. The problem there was essentially that Rust assumed sizeof(size_t) == sizeof(intptr_t). If bounds checking makes it into hardware then two's complement will also get thrown out the window.
Re:Beej's Guide to C Programming by tialaramex on Monday February 27, 2023 @04:12AM (#34954484)
Because C has both size_t and intptr_t and a billion other magic types which can be adjusted to suit more architectures it was able to successfully "adapt" to CHERI where Rust makes such an adaptation enormously expensive (now all your word size integers are 128-bit).

On the other hand of course, first CHERI doesn't make much sense in Rust, you would not invent all this expensive stuff if your language was memory safe, CHERI exists because people just keep writing C and C++ software and there must be a hardware mitigation - CHERI is that mitigation. If you see a lot more practical CHERI hardware, evidently it was just too hard to give up on these terrible languages.

Secondly though, the main thrust of Aria's article isn't CHERI. It's compelling to have physical hardware that shows off the problem by making usize and isize twice as big, but in practice Aria is talking about provenance which affects your machine and your C programs.

In C provenance is just... not defined. WG14 did I believe actually write (or is in the process of writing) a Technical Report explaining some sort of PNVI-ae variant (Provenance Not Via Integers, Address Exposed) two decades after DR260 but that's not the C language standard it's just a TR, and it's not the final word on the matter.

So Aria's Strict Provenance Experiment isn't Rust trying to catch up to where C was on this, it's Rust trying to do stuff C didn't get to yet in ~50 years of development. And in both languages this is about issues that are completely unrelated to your real hardware. The point here is that addresses which your memory hardware actually has, don't have provenance, but pointers which exist in your programming language do.

Re:Beej's Guide to C Programming by saagarjha on Monday February 27, 2023 @09:42AM (#34956959)
> CHERI doesn't make much sense in Rust

CHERI was obviously designed for C and C++, but that doesn't mean it doesn't make sense to use with Rust. Capabilities are generally useful primitives, and allow you to do things like eliminate bounds checks because they are not handled by the hardware.

Provenance is kind of weird in that it is very underspecified and compilers have mostly been flying by "do whatever feels right" for many years, which has mostly worked until recently, when people decided to really sit down and hammer out proper rules for the things people expect to work. CHERI has accelerated those efforts because whatever gets picked for provenance is going to be implemented on that platform and they would like capabilities to line up with whatever they decide. In a sense, this is the opposite problem, where C decided to not really specify behavior at all and now needs to clean up its act, rather than overspecifying and being unable to adapt to new hardware. It's good that it's being cleaned up but mostly aligned with what people have already been doing.

Re:Beej's Guide to C Programming by tialaramex on Monday February 27, 2023 @12:45PM (#34959503)
> Capabilities are generally useful primitives, and allow you to do things like eliminate bounds checks because they are not handled by the hardware.

Mmm. What does the actual performance look like? We can surprisingly often trim the actual performance of real software to exactly the same as no bounds checks, but while being sure it's in bounds, and so any approach that's more expensive needs a justification. In C and C++ that justification is pretty obvious, in Rust it's a tougher sell.

If it helps WUFFS can emit high performance C number crunching code that is definitely bounds safe, regardless of whether it includes what appears to be any actual bounds checking, if CHERI exhibits overhead for that code compared to no CHERI - which I think it does, that's a price for CHERI that isn't giving any benefit and you'd expect similar for a high quality Rust port to a platform with CHERI modulo Aria's suggested way forward.

> Provenance is kind of weird in that it is very underspecified and compilers have mostly been flying by "do whatever feels right" for many years, which has mostly worked until recently

I don't agree that it "mostly worked". Until Rust the main consumers of optimising compilers which heavily rely on provenance shenanigans were C and C++ and in both languages you could just scare off the repeated bug reports resulting from such shenanigans by pointing at DR/260 in which WG14 basically says "Yes pointer provenance is a thing" but declines to specify how it could possibly work and leaves it to compilers to do whatever they want. Those bug reports do exist, they just got closed NOTABUG.

Only Rust finally starts asking what the LLVM IR actually means and there are a lot of awkward questions resulting from that, continuing to this day. I think it would have been a lot easier to keep kicking the ball into the long grass (as WG21 agreed to do yet again this year for C++ itself) if there wasn't a language with a whole lot of users asking these awkward questions.

Re:Beej's Guide to C Programming by saagarjha on Monday March 06, 2023 @02:17AM (#35038323)
> What does the actual performance look like?

Hard to say, production hardware isn’t out yet. Anyways, I think you misunderstand my point, CHERI is definitely designed for running C/C++ code but obviously people will want to run Rust on it. In that scenario capabilities are “free” because they exist by default (just like MMUs give you protection for no cost because they’re always on for modern OSes), so Rust can work with it.

> If it helps WUFFS can emit high performance C number crunching code that is definitely bounds safe

WUFFS is cool but it’s not really meant for general-purpose code.

> Until Rust the main consumers of optimising compilers which heavily rely on provenance shenanigans were C and C++ and in both languages you could just scare off the repeated bug reports resulting from such shenanigans by pointing at DR/260 in which WG14 basically says "Yes pointer provenance is a thing" but declines to specify how it could possibly work and leaves it to compilers to do whatever they want.

I think this lacks context. The compilers of yesteryear and even mostly the compilers of today can’t do much with provenance, because doing optimizations on it often requires global program analysis that is infeasible to perform. The examples that people knew were problematic were almost all centered around surprising compilation results in small snippets showing things like “off the end” pointers having the same bit pattern as the address of the stack object next to it, but comparing different. The responses to bug reports like these was not that provenance was broken but mostly “the standard doesn’t really want you doing stuff like this anyways, so go away”.

The actual legitimate use of provenance mostly did work, which was stuff like pointer tagging and stuffing (requiring round trips through various integer representations) for language runtimes and the like. Authors of that code typically worked really close to the hardware to questions like “does this pointer have provenance” doesn’t really mean much to them, they’d expect that it compiled to a load instruction like they expected and for the most part compilers would play it conservative and assign arbitrary provenance to operations like that. The exploration of “hmm, maybe we should specify this better” is a recent phenomenon that was done mostly independently of Rust the language but obviously a lot of those people were involved because the compiler/LLVM community is comprised of Rust people too.

Re:Beej's Guide to C Programming by tialaramex on Sunday February 26, 2023 @08:00PM (#34951680)
I think that I'd have said this claim about C (being closer to the metal) was wrong even 10-15 years ago when I was mostly writing C for a living. Ignore the Rust stuff entirely if you prefer.

What you can learn about in C is the C abstract machine, but the abstract machine is not any modern computer. It starts as a greatly simplified model of the PDP-11, a computer which hasn't been commercially relevant in decades.

So, on the one hand it's not much like the machines we own today, yet on the other hand it's also not like any real machine ever because all the complexities the C designers didn't express in their language are absent from the abstract machine. Sometimes this is an intentional deviation to make C simpler than a PDP-11, which is understandable but not so useful. Much worse though are places where they just didn't know.

The actual PDP-11 has specific semantics because it was a real thing, you could try stuff and find out. You could experiment. However the abstract machine is imaginary, and so where its properties were under-specified you can't just experiment there actually is no answer. WG14 (the C language committee) has tried, since it came into existence, to straight out some of this and choose definite answers, but it's a long way from finished.

So on the one hand, C is bad theoretically because it ignores much of what theoretical CS discovered in subsequent decades, but then on the other hand C is bad practically because its abstract machine models how somebody in the 1970s thought a 1970s computer worked, so it's wrong and it's 50 years old.

And again, I write these things as, IMNSHO an expert C programmer. I'm somebody who has found bugs in GNU libc and who wrote their own debugging tools so they could live debug problems which only happened in a production service. I'm not speaking out of ignorance, but out of understanding.

Re:Beej's Guide to C Programming by swatcoder on Sunday February 26, 2023 @08:16PM (#34951788)
I respect what you're saying here about a "C abstract machine" that's disjoint from any particular real hardware, and that it introduces its own troubles through the abstraction, and I think with agree with all that.

But I'm not sure how to draw that out of your original comment, or even how to interpret the original comment while "ignoring the Rust stuff entirely". That comment seems to be suggesting that Rust exposes its users to the bare metal just as much as C does, so if you take that comparison out, it doesn't seem like it's saying anything at all. C is flawed, and is not even ideal as a window into bare metal (because of its own abstract machine), but Beej noted that it's more connected than any "present-day" language, and that claim does seem to hold.

Re:Beej's Guide to C Programming by tialaramex on Sunday February 26, 2023 @09:20PM (#34952188)
Well, let's just try ripping all mention of Rust out:

""While obviously C is closer to the metal than say, Python, I am increasingly dubious about the value of any of these high level languages (yes, even C) to learn "How the machine really works". I reckon it's probably more instructive to write a little bit of ARM assembly than to learn enough C to get to the same place today.""

Now, why am I talking about "How the machine really works". Well, here's the next line from Beej:

> When you learn C, you learn about how software interfaces with computer memory at a low level

You don't. You're learning about how C software interfaces with the memory of C's abstract machine, but that's not how your machine's memory works, not even close. For example C just magically ensures the alignment is correct all the time, but the actual memory relies on programmers to ensure they only perform aligned access, unaligned access may blow up your whole program or make it enormously slower, which is worse ?

On the other hand C's Pointers are not just addresses. Even though C makes them look more like they are than many languages with a pointer type, they still aren't just addresses. If you mistake them for addresses, you'll get punished by your compiler, although just exactly when is hard to predict - this is a game of Russian Roulette. However the actual memory in your computer really does just have addresses and they're just numbers like any other numbers.