News for hackers. Stuff that matters.
Re:Undefined Behavior in 2017 by wereHamster on Wednesday July 05, 2017 @03:59AM (#14700625)
Why don't compilers have an option to reject UB with an compiler error? Surely the compiler must know when it comes across a piece of source code whose semantic is undefined.
Re:Undefined Behavior in 2017 by masklinn on Wednesday July 05, 2017 @05:10AM (#14700916)
Because compiler don't generally look for UB then gleefully cackle as they fuck up your code.

Rather they are written under the assumption that there are no UB in the program, and the chips fall where they may as these assumptions are used and propagated.

Re:Undefined Behavior in 2017 by lomnakkus on Wednesday July 05, 2017 @04:06AM (#14700660)
> Surely the compiler must know when it comes across a piece of source code whose semantic is undefined.

Absolutely not. Many instances of UB depend on runtime values. (E.g. overflows, out-of-bounds shifts, etc. etc.)

Re:Undefined Behavior in 2017 by dom0 on Wednesday July 05, 2017 @04:17AM (#14700706)
Because a lot of UB cannot be efficiently determined at compile time. Hence, UBSan.
Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @04:24AM (#14700732)
It is very easy to be efficiently determined at compile time, other systems programming languages do it, by not having UB on their language standard to start with.

If people prefer "performance trumps correctness" and "winning compiler benchmarks is what counts" then they should happily keep getting exploited.

Re:Undefined Behavior in 2017 by gpderetta on Wednesday July 05, 2017 @04:54AM (#14700846)
> It is very easy to be efficiently determined at compile time

for many cases it is in fact equivalent to the halting problem. What you can do is check at runtime, which has performance implications (from trivial to expensive depending on the check).

Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @05:00AM (#14700876)
Not if the language standard doesn't allow for UB to begin with, then there isn't any halting problem to worry about.

This is what the alternatives in the memory safe systems programming language since Algol days do.

Surely they still allow for developers to disable some of those checks explicitly, but then it is the programmers fault to choose "performance trumps correctness" instead of "correctness trumps performance".

Re:Undefined Behavior in 2017 by caf on Wednesday July 05, 2017 @06:48AM (#14701345)
It doesn't matter if the language defines the behaviour of, say, offsetting an array beyond its declared bounds. That doesn't change the general undecidability of statically proving the offset will always be within the bounds at compile-time - in other words, you need runtime checks in some cases (it's just a matter of whether the compiler or the programmer supplies them).
Re:Undefined Behavior in 2017 by eru on Wednesday July 05, 2017 @09:54AM (#14702386)
Nah, with some help from the programmer, you can produce those proofs. It's not too hard.

The halting problem is impossible to solve for arbitrarily evil programs, but that doesn't mean that it's impossible to write programs that are easy to analyse.

Re:Undefined Behavior in 2017 by gpderetta on Wednesday July 05, 2017 @10:12AM (#14702504)
I think that statically guaranteeing something like in-bound indexing or absence of division by zero you need depended types, which is not exactly easy nor mainstream. And even with dependent types, many non-evil, otherwise correct programs will be rejected.
Re:Undefined Behavior in 2017 by eru on Thursday July 06, 2017 @04:15PM (#14713551)
Yes, for the general problem you want dependent types (or similar).

There are middle grounds, of course: you can let the compiler figure out on its own what it can, and let it insert runtime checks for the other cases.

And add some way for the programmer to declare that eg "warn me if this can't be optimized" or "trust me, this is safe" or even "here's a proof for you".

But in any case, dependent types are possible---undecidability is not a hard barrier here.

Re:Undefined Behavior in 2017 by yorwba on Wednesday July 05, 2017 @12:27PM (#14703786)
Aren't dependent types able to express any statement formalizable in predicate logic? This should include most things you might want to say about the correctness of non-evil programs. I think the real problem with them is that type-checking itself becomes undecidable, so you get the additional problem of proving that your program is indeed correctly typed.
Re:Undefined Behavior in 2017 by tom_mellior on Wednesday July 05, 2017 @06:12PM (#14706520)
Here are two nice blog posts on the state of the art of this "not too hard" problem: https://maniagnosis.crsr.net/2017/06/AFL-brute-force-search.... https://maniagnosis.crsr.net/2017/06/AFL-bug-in-quicksearch....

Possible? Yes. Too time-consuming and fiddly for the economic realities of 99.9% of all programmers? Yes, that too.

Re:Undefined Behavior in 2017 by pjmlp on Thursday July 06, 2017 @01:54AM (#14708420)
The economic realities would be quite different if software companies were legally accountable for their defects, just like in many other industries.
Re:Undefined Behavior in 2017 by eru on Monday July 10, 2017 @04:20PM (#14739286)
That would mostly mean that only big companies that can afford a herd of lawyers would be able to produce software.

At least that how bank regulation works out in practice.

Re:Undefined Behavior in 2017 by dom0 on Wednesday July 05, 2017 @04:28AM (#14700747)
I do see your point, but that'd be a different language. With the standardised C we have, it's not easy.
Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @04:35AM (#14700776)
Which is why we should move away from it.

However I don't see it ever happening in the UNIX world, only on OSes whose architecture is not bound to the traditional UNIX/C culture.

Which is why see as positive that Apple, Google and Microsoft are pushing C and C++ down into the very lowest layers of their OS stacks, mostly visible on Google's case.

Even straight pure Assembly programming has less UB surprises.

Re:Undefined Behavior in 2017 by jabot on Wednesday July 05, 2017 @05:42AM (#14701039)
"Very easy" - well, if you think garbage collection is easy...

And yes, if you want to allow manual allocation of objects and setting pointers to arbitrary objects, then your two choices are: 1) garbage collection 2) undefined behavior (use-after-free, double-free, dangling pointers, etc.)

Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @06:06AM (#14701140)
> garbage collection is easy...

Garbage collection isn't the only way of writing mostly safe systems programs.

ESPOL, NEWP are 10 year older than C.

PL/8 was the systems programming language used by IBM for their first RISC mainframe, followed by PL/S.

Mesa used at Xerox PARC.

Modula-2 used at ETHZ and many 16 bit systems.

Object Pascal used to write the first versions of Mac OS and Lisa.

Ada and SPARK used by high integrity systems where human lives can be at risk.

All of them with not even a quarter of C's UB collection.

Re:Undefined Behavior in 2017 by jabot on Wednesday July 05, 2017 @08:33AM (#14701867)
Well, I don't know about half of these programming languages. However:

- ADA requires garbage collection

- Modula-2 and Object pascal have manual memory management, i.e.: "new" and "delete" - together with double-free, use-after-free, and dangling pointers...

I agree that C is by far worse, but please recognize that at least some of the undefined behavior problems are really hard to solve.

EDIT: Oh, and we haven't started talking about multi threading yet, have we?

Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @10:24AM (#14702621)
What about learning to understand English?

"writing mostly safe systems programs."

Do you understand what mostly means?

Ada and Modula-2 have multi-threading as part of their ISO/ANSI language standard.

Also you don't seem to know much about Ada given your GC remark, but here is some learning.

https://archive.fosdem.org/2016/schedule/event/ada_memory/

Of course there are still use cases where Algol derived languages are still unsafe, however those use cases are a tiny portion of what happens in C land.

Re:Undefined Behavior in 2017 by jabot on Thursday July 06, 2017 @02:40AM (#14708564)
> What about learning to understand English?

That's uncalled for, man...

> Of course there are still use cases where Algol derived languages are still unsafe, however those use cases are a tiny portion of what happens in C land.

On that I agree - there are many problems we wouldn't have if we were using, say, pascal or modula.

> Also you don't seem to know much about Ada given your GC remark, but here is some learning.

I'm sorry, I don't have time to watch that. However, I know that ADA has either GC or manual memory management (with the deallocation method explicitly marked as unsafe). Thus, as far as i know, constructing "use-after-free" in Ada is possible - which is undefined behavior (and that was my point).

Re:Undefined Behavior in 2017 by unrealhoang on Wednesday July 05, 2017 @09:06AM (#14702052)
Or RAII, a la Rust
Re:Undefined Behavior in 2017 by lorenzhs on Wednesday July 05, 2017 @09:47AM (#14702332)
"RAII is associated most prominently with C++ where it originated" (https://en.wikipedia.org/wiki/RAII)
Re:Undefined Behavior in 2017 by unrealhoang on Wednesday July 05, 2017 @10:55AM (#14702849)
But Rust is where it's taken to the extreme (use on memory resource), and used to prevent a class of UBs (use-after-free, double-free, dangling pointer...)
Re:Undefined Behavior in 2017 by burntsushi on Wednesday July 05, 2017 @11:15AM (#14703033)
Not quite. Destructors aren't guaranteed to run, so you can't actually rely on them for memory safety guarantees.

For example, leaking data in Rust is a perfectly safe thing to do. But while destructors help to prevent it, they cannot guarantee that nothing ever leaks. (And some of this depends on what you mean by "leaking" and so on.)

use-after-free, double-free and dangling pointers are all handled by Rust's ownership and borrowing systems.

Re:Undefined Behavior in 2017 by jcelerier on Wednesday July 05, 2017 @07:51AM (#14701629)
The compiler would reject the following function :

int res(int a, int b) { return a + b; }

since it can be UB, for instance if a + b > INT_MAX.

Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @08:09AM (#14701722)
Then by all means reject it, way better than blowing up in production.
Re:Undefined Behavior in 2017 by jerf on Wednesday July 05, 2017 @08:24AM (#14701798)
You just destroyed all C code in the world. At that level of paranoia, you've basically defined a brand new language, and if you're going to do that, why make it C at all?

Smart people with a ton of experience in C have already examined the possibility of defining a C dialect with much less undefined behavior in it. In fact, I believe it's been seriously tried more than once by independent groups. And the result has been the same each time; initial excitement and optimism gives way to total failure. It's simply way too deeply ingrained in the definition of C, backed by all the code in the world written in C. If you want less undefined behavior, your only choice is to leave C.

Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @09:09AM (#14702075)
OP was talking about a compiler option, that most likely would not break anything unless you enable the flag.

As for choice, I'll take "here's what happens" vs "your code is in the hands of fate now" any day. Glad we do have choice.

Re:Undefined Behavior in 2017 by sullyj3 on Wednesday July 05, 2017 @08:42AM (#14701911)
I can't tell if this is satirical, but to be clear, you're proposing to disallow addition?
Re:Undefined Behavior in 2017 by sp332 on Wednesday July 05, 2017 @10:47AM (#14702780)
You could test whether overflow is going to happen first. Or use a math library that explicitly overflows with predictable results.
Re:Undefined Behavior in 2017 by dullgiulio on Wednesday July 05, 2017 @01:50PM (#14704627)
I know very few languages that do that. Even Rust doesn't when compiled for release. All for performance reasons.
Re:Undefined Behavior in 2017 by steveklabnik on Wednesday July 05, 2017 @02:22PM (#14704911)
Some additional points:

* It's not UB in Rust, it's guaranteed to be two's compliment overflow.

* It's still a "program error" and debug builds are required to panic.

* Non-debug builds are allowed to either panic or do the overflow, for performance reasons, as you mention. If it was feasible to always panic we'd do that; the current wording allows us to change it to do so in the future if it becomes feasible.

* If you want to do a checked add, you can, it's just not the default.

Re:Undefined Behavior in 2017 by sp332 on Wednesday July 05, 2017 @03:01PM (#14705215)
I might have lost the point in this long thread, but I'm pretty sure the suggestion was just to add it as an optional flag.
Re:Undefined Behavior in 2017 by wamaral on Wednesday July 05, 2017 @09:01AM (#14702029)
Rather define the behaviour of what happens when addition of two ints exceed the maximum allowed value.
Re:Undefined Behavior in 2017 by rspeer on Wednesday July 05, 2017 @12:17PM (#14703683)
You're changing your plan here. Maybe you could at least concede that the issue of UB is difficult and is not going to be solved by saying "why don't they just" on HN?
Re:Undefined Behavior in 2017 by adrianN on Wednesday July 05, 2017 @04:01AM (#14700636)
Some UB depends on the input (out of bounds access is a popular example). Some UB is created during optimization passes, for example by inlining. It's hard to produce useful error messages.
Re:Undefined Behavior in 2017 by chrisdew on Wednesday July 05, 2017 @04:04AM (#14700651)
Some undefined behaviours relate to runtime values. !iirc! signed 2+2 is safe but signed SIGNED_INT_MAX + 1 is not.
Re:Undefined Behavior in 2017 by Peaker on Wednesday July 05, 2017 @04:11AM (#14700681)
But it is easy to give it a defined behavior on a particular architecture. For example, on x86-64, it can be defined to overflow just like unsigned does. The compiler can emit errors about the code if it is compiled to a different architecture where the same defined behavior is too expensive to implement.
Re:Undefined Behavior in 2017 by petergeoghegan on Wednesday July 05, 2017 @01:27PM (#14704398)
It can be, but it isn't. All contemporary ISAs use two's complement arithmetic for signed integers, including x86-64, and so I think signed overflow could be made well defined on all archs without particularly disadvantaging one arch over the other.

The reason that that doesn't happen, here as elsewhere, is that there are a number of specific compiler optimizations that rely on it being undefined. See GCC's -fwrapv flag.

Re:Undefined Behavior in 2017 by scatters on Wednesday July 05, 2017 @04:29AM (#14700757)
How would that help? The behavior would be defined, but it still wouldn't be what you /want/.
Re:Undefined Behavior in 2017 by Peaker on Wednesday July 05, 2017 @04:42AM (#14700798)
Defined good behavior > defined bad behavior > undefined behavior.

This article is about attacking the last category.

Unsigned overflow is far less of a problem than signed overflow because it is defined.

It is very cheap to make signed overflow defined. It is more expensive to detect it and fail at runtime.

Re:Undefined Behavior in 2017 by maxlybbert on Wednesday July 05, 2017 @06:31AM (#14701264)
The argument I've seen is "macros often expand into unintuitive code, especially when multiple macros are involved, so these kinds of warnings would be confusing if there were such macros."

It might be possible to do the same thing if you didn't check any code generated from macros.

Re:Undefined Behavior in 2017 by fazkan on Wednesday July 05, 2017 @04:22AM (#14700723)
They are called undefined behaviors because the compiler developers are free to do whatever they want when they come across a code that instigates an undefined behavior.

Mostly new compilers now a days do handle such behavior with a compiler error or warning. So if you try to do an out of bound shift a new compiler might throw a warning, but according to the C standard it does not have to. In fact instead of throwing a warning it can wipe your complete hardware and that too is allowed according to the C standard...

In contrast there is implementation defined behavior, and unspecified behavior. Where the compiler developers have to document and take care of certain things.

I wrote a detailed article about the subtle difference you can check that out....

https://medium.com/@faizan10114/understanding-c-terminologie...

Re:Undefined Behavior in 2017 by dom0 on Wednesday July 05, 2017 @04:26AM (#14700740)
> So if you try to do an out of bound shift a new compiler might throw a warning, but according to the C standard it does not have to.

Right, for doing so needs to determine that this is happening, which neither GCC and Clang can do for fairly trivial cases like

    for (i = 0; i < 100; i ++) {
       ... x << i ...
    }
Re:Undefined Behavior in 2017 by fazkan on Thursday July 06, 2017 @04:21AM (#14708892)
Yes they don't, I assumed they would, but they dont. I just wanted to emphasize the fact that they don't have to. Perhaps an unbound shift was a wrong example.. thanks for catching that...
Re:Undefined Behavior in 2017 by pjmlp on Wednesday July 05, 2017 @04:13AM (#14700688)
Because of benchmarks fights.

UB is the result of C compiler vendors not wanting to give up on their precious optimizations or hardware configurations back when ANSI C was initially being done.

They could have done like in other languages and have ANSI C without UB, but then they would need to give up on some hardware features or lose in one or other benchmark.