News for hackers. Stuff that matters.
Re:GCC undefined behaviors are getting wild by proto_lambda on Monday November 28, 2022 @02:22AM (#33770653)
Lying requires intent. This was a mistake, something that humans are well-known for making, and if the compiler is designed to assume otherwise, it borders on useless in the real world.
Re:GCC undefined behaviors are getting wild by masklinn on Monday November 28, 2022 @02:29AM (#33770692)
A compiler can’t know why you fucked up, it can’t even know that you fucked up, because UBs are just ways for it to infer and propagate constraints.

If an optimising C compiler can’t rely on UBs not happening, its potential is severely cut down due to the dearth of useful information provided by C’s type system.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @02:43AM (#33770792)
> A compiler can’t know why you fucked up, it can’t even know that you fucked up, because UBs are just ways for it to infer and propagate constraints.

To be honest, that's just how compiler writers interpret UB these days.

It's perfectly possible (in principle) to use lots of more sophisticated static and dynamic analysis to recover much of what C compiler just assume. You don't have to restrict yourself to what C's type system provides.

(For an example of what's possible, have a look at all the great techniques employed to make JavaScript as fast as possible. They have basically no static types to work with at all.)

Re:GCC undefined behaviors are getting wild by masklinn on Monday November 28, 2022 @03:04AM (#33770945)
> For an example of what's possible, have a look at all the great techniques employed to make JavaScript as fast as possible. They have basically no static types to work with at all.

I’m sure people will be very happy with a C JIT. That’s definitely what they use C for.

JIT-ed code is full of runtime type and range assertions which bail if the compiler’s assumptions are incorrect.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @03:32AM (#33771123)
Oh, I didn't mean to imply that it would be practical. Only that it's possible and that the type system isn't the only thing you can rely on.

Instead of just assuming that 'x > x + 1' is always true (for signed integers), the compiler could also do the heavy lifting of static analysis (for cases where that's possible).

Re:GCC undefined behaviors are getting wild by littlestymaar on Monday November 28, 2022 @03:41AM (#33771173)
But you talked about JavaScript, where the “heavy lifting” is in fact “assume that it's always true, and switch back a less optimized version if the assumption turns out to be false”. That's exactly the kind of things you cannot do in C, because people use C in contexts were JIT isn't an option.
Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @03:45AM (#33771200)
Sorry, my general point and my example didn't perfectly match.

For something better matching: have a look at things like Coverity for what you can recover with static analysis of existing C code.

Re:GCC undefined behaviors are getting wild by Gibbon1 on Monday November 28, 2022 @04:32AM (#33771468)
Frankly what I think it we should have a compramize.

Make overflow on a signed int defined. Create a new type called unsafe_int where it is not.

So then the compiler writers get to implement their pointless optimization pit traps. And everyone else can avoid those by banning unsafe_int.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @04:49AM (#33771589)
You can already compile with `-fno-strict-overflow` to get twos-complement overflow for signed integers.

I meant to talk more abstractly about UB in general.

Re:GCC undefined behaviors are getting wild by astrange on Monday November 28, 2022 @05:27AM (#33771858)
The signed integer overflow rule is extremely important for common optimizations, mostly related to loops like knowing if they're finite or rewriting their index directions.

The way to start getting rid of it would be to add for...in... loops or something where the loop index can be a custom no-overflow type.

And "defining" it is a lame approach to safety. If you make it wraparound, you now have silent wraparounds that can't be found by static analysis. You want unintended overflows to trap, not just be defined.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @05:59AM (#33772078)
> And "defining" it is a lame approach to safety. If you make it wraparound, you now have silent wraparounds that can't be found by static analysis. You want unintended overflows to trap, not just be defined.

Yes. But even the lame approach is better than UB, because it doesn't bring the whole program down.

Re:GCC undefined behaviors are getting wild by Gibbon1 on Tuesday November 29, 2022 @04:53PM (#33793886)
I've been wondering if I should mention that using int for an index is a bad idea because the standard only guarantees it's 16 bits. You should use size_t instead. And in C size_t is unsigned.

My take is all of the low hanging fruit optimizations that the standard enables has been picked a long time ago. Everything left is problematic.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @02:26AM (#33770670)
That's C for you. If you want something saner, use Rust or Haskell or Python or even Java or Go or.. almost any other language that's not C or C++.

These days the whole point of C is this Faustian pact with the devil of speed for sanity.

Re:GCC undefined behaviors are getting wild by consp on Monday November 28, 2022 @02:40AM (#33770776)
Writing Rust and Haskell for sanity is not something I would agree with. Maybe for language characteristics but reading those make me jump out of the window.
Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @02:58AM (#33770899)
I have the same issue with Java and Go. (Which I brought up as well.)

Yet, they still compare favourably with C in this regard. Almost anything does.

Re:GCC undefined behaviors are getting wild by pca006132 on Monday November 28, 2022 @03:04AM (#33770946)
I think Rust doesn't allow integer overflow either, unless you specifically use the wrapping_* operations. Probably the same kind of thing will also happen to Rust.
Re:GCC undefined behaviors are getting wild by masklinn on Monday November 28, 2022 @03:05AM (#33770951)
Rust either allows or traps on integer overflow depending on the mode. Either way it’s defined behaviour.
Re:GCC undefined behaviors are getting wild by pca006132 on Monday November 28, 2022 @10:40AM (#33774926)
Ah, I forgot that it is also defined in release mode, but just considered undesirable.
Re:GCC undefined behaviors are getting wild by baq on Monday November 28, 2022 @02:37AM (#33770751)
And yet C is still the dominant language. Undefined behavior is actually the reason why: any defined behavior is expensive to implement in the compiler and possibly incurs a cost at runtime. The language design intentionally trades programmer’s sanity for ease of implementation.
Re:GCC undefined behaviors are getting wild by pjmlp on Monday November 28, 2022 @02:40AM (#33770774)
Legacy reasons, most embedded devs and UNIX clones won't use anything else.

In many other domains, other languages have taken their place, and this will keep on going, even if it takes a couple of generations, or goverment cybersecurity mandatates to make it happen.

Re:GCC undefined behaviors are getting wild by eru on Monday November 28, 2022 @02:50AM (#33770841)
Not sure that's actually the reason in practice?

For a real prominent counterexample: the Linux kernel is intentionally programmed in a C dialect (defined by a myriad of GCC compiler flags) that removes a lot of UB.

If they craved the Faustian bargain of UB for speed, they could immediately move in that direction by dropping some GCC options.

Re:GCC undefined behaviors are getting wild by adwn on Monday November 28, 2022 @02:54AM (#33770869)
> The language design intentionally trades programmer’s sanity for ease of implementation.

There's nothing "easy" at all about UB-exploiting performance optimizations in modern C compilers, and "ease of implementation" is absolutely not why those optimization passes have been included. In fact, the easiest thing for the compiler to do, when it sees an int * int operation, is to emit an IMUL assembly instruction (or the equivalent for your CPU architecture) and not worry about deleting overflow checking code. Which is what C compilers did before the extent of UB exploitation became excessive.

Re:GCC undefined behaviors are getting wild by baq on Monday November 28, 2022 @03:02AM (#33770936)
I agree on the top compilers but there are dozens if not hundreds architectures with their own proprietary C compilers maintained by a dinosaur and a couple intern dino chicks if they’re lucky. I postulate any other language wouldn’t be implemented or would be defanged to C-level of (non)safety anyway in a way similar to mrustc.
Re:GCC undefined behaviors are getting wild by andrewaylett on Monday November 28, 2022 @06:07AM (#33772153)
C has always considered that the programmer knows what they are doing. Programs are assumed correct unless proven invalid.

This is -- or at least was -- a feature, not a bug. You can implement any valid program, but you can also implement some invalid programs.

I know the OP mentioned Rust, but it's a valid comparison: if you don't invoke "unsafe" then all your behaviour is well-defined. But the trade-off is that Rust will only let you implement a subset of valid programs unless you invoke "unsafe", which might be better termed "assumed correct".