Elektrine lite

← Feed

@thephd@pony.social

Post #2505877

2023-11-03 18:23 UTC

Got Stun Locked Again, so it's time to dump a rant I made elsewhere: C is not a language for direct control of the hardware. C is a langauge that is coded to the semantics of an Abstract Machine specified in a document. It's no more capable of hardware control that e.g. Rust or Zig. What it has is a wide variety of pre-existing implementations that allow you to touch that hardware, but most of that control was programmed in an assembly language or worked into the hardware/firmware by somebody. C is not more suitable for hardware and your computer is not a PDP-11. (This is part of why "Nobody writes ISO C" is a thing. C, the language K&R made, the language that got standardized, at any point in its lifetime, was never good enough for a kernel. It just let people coordinate stuff in the cheapest way possible and accepted extensions.) The part that's extra nutso is that the last 20 years were people who held this exact belief -- that C was just a thin layer over the hardware -- get bodied, over and over again. Compiler vendors gave them the big middle finger every time they said "wait, no, UB is for hardware!", and compiler vendors traded in UB for (sometimes negligible) speed ups. To still believe C is "for the hardware" in today's day and age where GCC will literally run your for loop for eternity because you tried to access an array out-of-bounds and it found out about it, or Clang will solve fermat's last theorem due to a loop, is magic shroom thinking. You can access the hardware just as good with Java by using the Pointer class, you can hit the same register that the shitty ISA Manual fucking lied about by writing the same integer address into the Pointer class and dumping out a 2-byte integer to the right place. C is not magic and you're not improving its design by insisting it is, for the love of God start Evaluating Your Tools Properly. Yer an engineer, not a fucking wizard, Harry.

Replies (20)

  • @atax1a@infosec.exchange 2023-11-03 18:24

    @thephd@pony.social as we continue to insist, a modern "language that lets you access the hardware" would allow you to annotate your data structures for cache-tier-occupancy.

    Open ##3213133

  • @gsuberland@chaos.social 2023-11-03 18:28

    @thephd@pony.social I have literally run C# on a microcontroller and it had no problem poking at registers and memory, so yeah, C is definitely not special here.

    Open ##3213148

  • @jaycie@tech.lgbt 2023-11-03 18:29

    @thephd@pony.social This! Yes, this! Ahhhhhhhh.

    Open ##3213159

  • @jrose@social.belkadan.com 2023-11-03 18:30

    @thephd@pony.social My brain is saying “but volatile exists!” and I’m responding with “no, it doesn’t”. Which is not true but it’s close enough.

    Open ##3213160

  • @ids1024@fosstodon.org 2023-11-03 18:34

    @thephd@pony.social "your computer is not a PDP-11" - Hm, has anyone made a Mastadon client for the PDP-11 yet? Then they can yell at their VT100 when you make a comment like this. Not only is pure conformant ISO C insufficient for kernel or embedded development. How many non-trivial C applications or libraries at all are actually portable to all conformant implementations? (No OS specific calls, doesn't assume 8 bit bytes, works with 16-bit ints...) You see enough trouble with endianness and alignment.

    Open ##3213161

  • @shac@ioc.exchange 2023-11-03 18:40

    @thephd@pony.social When you’re telling your users they’re using it wrong, perhaps it’s time to reevaluate what product you’re actually making.

    Open ##3213167

  • @icedquinn@blob.cat 2023-11-03 18:42

    @thephd@pony.social it was supposed to be 'readable assembly' i use nim these days though its comfy

    Open ##3213170

  • @srtcd424@mas.to 2023-11-03 18:56

    @thephd@pony.social I was a C stalwart for a long time, probably too long, but agreed the time to make the switch to something better is well overdue. Hell, even Linus 'Mr Grumpy' Torvalds is letting rust into the kernel!

    Open ##3213172

  • @duk@nothing-ever.works 2023-11-03 18:56

    @thephd@pony.social

    Open ##3213173

  • @sudo200@layer8.space 2023-11-03 18:56

    @thephd@pony.social C is not magic, its semi-portable assembly, it is useful for abstracting away the platform-specifics, like registers and instructions. And while it might sometimes be useful to return a stack address from a function or do other equally unsafe operations, it would at least help, if you had to explicitly define them as unsafe, like you have to in Rust.

    Open ##3213175

  • @sjolsen@tech.lgbt 2023-11-03 19:01

    @thephd@pony.social oh but C's language constructs map almost one-to-one to ISA constructs (vector extensions? fences? never heard of them), which are themselves definitely representative of how modern hardware works (register renaming? what's that?)

    Open ##3213176

  • @lanodan@queer.hacktivis.me 2023-11-03 19:20

    @thephd@pony.social Which is quite why I'm strongly on "low-level is assembly or 1:1 translation of it, like with macros" (with still acknowledging that binaries are themselves interpreted+optimised by the hardware) and then having distinctions like system-oriented, applications, data processing, … And to me a big part of why C and the like are not low-level is how there's a lot of "I cannot know what the result will look like, unless I use workarounds" like with how bitwise operators are used for manipulating bitfields, instead of directly using a struct, as those are reordered and padded for alignment (and efficiency).

    Open ##3213177

  • @a1ba@suya.place 2023-11-03 19:30

    @thephd@pony.social write in C because you like it, not because it supposedly makes the computer go fast, when that could be achieved with other tools.

    Open ##3213178

  • @Tathar@dragon.style 2023-11-03 19:48

    @thephd@pony.social Case in point: Embedded C exists *because* regular C doesn't directly control the hardware.

    Open ##3213179

  • @bersl2@furry.engineer 2023-11-03 20:00

    @thephd@pony.social What, then, is your vision of a future without C?

    Open ##3213180

  • @gsuberland@chaos.social 2023-11-03 20:05

    @thephd@pony.social on this note, do you think it would even be feasible to create a language that has "real" direct control of the hardware, insofar as the microarchitectures allow, with more than one architecture being supported? and would that even be recognisable as a "language" in the traditional sense of expressing types and goal-orientated abstractions, rather than essentially just being a procedural IL assembler/preprocessor toolchain that supports trivial structs?

    Open ##3213182

  • @orc@eightpoint.app 2023-11-03 20:24

    @thephd@pony.social Selfishly, I wish that some modern computers were pdp-11s, because the iron shackles of minimal easily addressable core tends to encourage tighter code.

    Open ##3213188

  • @LoganDark@tech.lgbt 2023-11-03 20:42

    @thephd@pony.social "magic shroom thinking" actually even psychedelics don't usually impact your judgement this much

    Open ##3213189

  • @datenwolf@chaos.social 2023-11-03 21:14

    @thephd@pony.social All true and right. Yet, I still long for C compilers to just *tell* me (as a warning, that I can lift to an error), if code gets optimized away, that was "proven" to have no side effects, or is considered "redundant" within what's considered defined behavior. If I write int something(...) { char b[32]; /* ... */ memset(b, 0, sizeof(b)); return 0; } and the compiler optimizes away that memset, because "d'uh", then please tell me.

    Open ##3213190

  • @thatgeoguy@coales.co 2023-11-03 22:10

    @thephd@pony.social I need to frame this rant on my desk

    Open ##3213192