by wereHamster · 9y ago · 0 comments
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.
Absolutely not. Many instances of UB depend on runtime values. (E.g. overflows, out-of-bounds shifts, etc. etc.)
If people prefer "performance trumps correctness" and "winning compiler benchmarks is what counts" then they should happily keep getting exploited.
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).
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".
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.
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.)
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.
int res(int a, int b) { return a + b; }
since it can be UB, for instance if a + b > INT_MAX.
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.
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.
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.
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.
It might be possible to do the same thing if you didn't check any code generated from macros.
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...
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 ...
}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.