7 ms·
> because the whole program is immediately invalidated. The problem is the program isn't invalidated, it's compiled and run. The malicious compiler introducin
by pjc50 4y ago
> because the whole program is immediately invalidated.
The problem is the program isn't invalidated, it's compiled and run.
The malicious compiler introducing security bugs from Ken Thompson's "Reflections on Trusting Trust" is real, and it's the C standard.
I will grant that trying to detect UB at runtime may impose serious performance penalties, since it's very hard to do arithmetic without risking it. But at compile time? If a situation has been statically determined to invoke UB that should be a compile time error.
Also, if an optimizer determines that an entire statement has no effect, that should be at least a warning. (C lack C#'s concept of a "code analysis hint" which have individually configurable severity levels).
- tpush 4y ago> If a situation has been statically determined to invoke UB that should be a compile time error. That's simply not how the compiler works. There is (presumably, I haven't actually looked) no boolean function in GCC called is_undefined_behavior(). It's just that each optimization part of the compiler can (and does) assume that UB doesn't happen, and results like the article's are then essentially emergent behavior. See also: https://blog.regehr.org/archives/213 https://blog.regehr.org/archives/213
- benj111 4y agoIt is undefined behaviour if I write GCC --hlep Does that mean it's acceptable for GCC to reformat my hard drive? Just because something is UD doesn't give anyone a license to do crazy things. If I misspell --help I expect the program to do something reasonable. If I invoke UD I still expect the program to do something reasonable. Removing checks for an overflow because overflows 'can't happen' is just crazy. UD is supposed to allow C to be implemented on different architectures if you don't know whether it will overflow to INT_MIN it makes sense to leave the implementation open. If I, the user knows what happens when an int overflows then I should be able to make use of that and guard against it myself. A compiler undermining that is a bug and user hostile.
- tpush 4y ago> It is undefined behaviour if I write GCC --hlep No, it's not, and I don't know why you'd think so. UB is a concept applying to C programs, not GCC invocations. > UD is supposed to allow C to be implemented on different architectures if you don't know whether it will overflow to INT_MIN it makes sense to leave the implementation open. If I, the user knows what happens when an int overflows then I should be able to make use of that and guard against it myself. I think you're confusing UB with unspecified and implementation defined behavior. It's fine if you think something shouldn't be UB, but you have to go lobbying the C standard for that. Compiler writers aren't to blame here.
- formerly_proven 4y agoThis has come up before, because, in some technical sense, the C standard does indeed not define what a "gcc" is, so "gcc --help" is undefined behavior according to the C standard, because the C standard does not define the behavior. By the same token, instrument flight rules are undefined behavior. A slightly less textualist approach to language recognizes that when we talk about C and UB, we mean behavior, which is undefined, of operations otherwise defined by the C standard.
- tpush 4y agoI think this is confusing undefined behavior with behavior of something that is undefined. And either way, the C standard explicitly applies to C programs, so even this cute "textualist" interpretation would be wrong, IMO.
- benj111 4y agoDo you know what a metaphor is? No GCC --hlep isn't in the c standard. But it is a simple example to illustrate how programs react when it receives something that isn't in the spec. GCC could do anything with Gcc --hlep just like it could do anything with INT_MAX + 1. That doesnt mean that all options open to it are reasonable. If I typed in GCC --hlep I would be reasonably pissed that it deleted my hard drive. You pointing out that GCC never made any claims about what would happen if I did that doesn't make it ok. If you come across UD, there's reasonable and unreasonable ways to deal with that. Reformatting your hard drive which is presumably allowed by the C standard isn't reasonable. I would contend that removing checks is also unreasonable.
- deleted 4y ago[deleted]
- MichaelBurge 4y agoC++ bans undefined behavior in constexpr, so you can force GCC to prove that code has no undefined behavior by sprinkling it in declarations where applicable: https://shafik.github.io/c++/undefined%20behavior/2019/05/11/explporing_undefined_behavior_using_constexpr.html https://shafik.github.io/c++/undefined%20behavior/2019/05/11...
- account42 4y agoConstant-evaluated expressions with undefined behavior are ill-formed but constexpr annotated functions which may in some invocations result in undefined behavior are not.
- eru 4y ago> The problem is the program isn't invalidated, it's compiled and run. Anything can happen with undefined behaviour, including exactly what you would expect to happen for five years, and then everything breaks. Compiling and running as if nothing is amiss is exactly how UB is allowed to look like.
- pjc50 4y ago> Compiling and running as if nothing is amiss is exactly how UB is allowed to look like. Yes, and this is a "billion-dollar mistake" that's responsible for an ongoing flow of CVEs. (the proposal to replace "undefined" with "implementation-defined" may be the only way of fixing this, and that gets slightly easier to do as the number of actively maintained C implementations shrinks)
- imtringued 4y agoCreate a Defined-C dialect.
- eru 4y agoYou can already do that to some extent. There's tons of compiler flags that make C more defined. Eg both clang and gcc support `-fno-strict-overflow` to define signed integer overflow as wraparound according to two's complement.
- Someone 4y ago> If a situation has been statically determined to invoke UB that should be a compile time error. But you typically can’t prove that. There’s lots of code where you could prove it might happen at runtime for some inputs, but proving that such inputs occur would, at least, require whole-program analysis. The moment a program reads outside data at runtime, chances are it becomes impossible. If you want to ban all code that might invoke it it boils down to requiring programmers to think about adding checks around every addition, multiplication, subtraction, etc. in their code, and add them to most of them. Programmers then would want the compiler to include such checks for them, and C would no longer be C. If, as you seem to say, you want to ban a subset that’s easily provable, I think enabling all warnings already does that. See for example https://clang.llvm.org/docs/DiagnosticsReference.html#wargument-undefined-behaviour https://clang.llvm.org/docs/DiagnosticsReference.html#wargum..., https://clang.llvm.org/docs/DiagnosticsReference.html#warray-bounds-pointer-arithmetic https://clang.llvm.org/docs/DiagnosticsReference.html#warray..., https://clang.llvm.org/docs/DiagnosticsReference.html#winteger-overflow https://clang.llvm.org/docs/DiagnosticsReference.html#winteg... , https://clang.llvm.org/docs/DiagnosticsReference.html#wcompare-distinct-pointer-types https://clang.llvm.org/docs/DiagnosticsReference.html#wcompa...
- pjc50 4y ago> But you typically can’t prove that. There’s lots of code where you could prove it might happen at runtime for some inputs, but proving that such inputs occur would, at least, require whole-program analysis. The moment a program reads outside data at runtime, chances are it becomes impossible. No, I specifically ruled out doing that in my comment. I was referring to the situation where a null check was deleted because the compiler found UB through static analysis. (Or specifically, placing a null check after a possibly-null usage. It is wrong to assume that after possibly-null usage the possibly-null variable is definitely-null.)
- dwattttt 4y agoAs I recall, the compiler didn't know it had found undefined behaviour. An optimisation pass saw "this pointer is deferenced", and from that inferred that if execution continued, the pointer can't be null. If the pointer can't be null, then code that only executes when it is null is dead code that can be pruned. Voila, null check removed. And most relevantly, it didn't at any point know "this is undefined behaviour". At worst it assumed that dereferencing a null would mean it wouldn't keep executing.
- dzaima 4y agoThe example here doesn't have compile-time known undefined behavior though; as-is, the program is well-formed assuming you give it safe arguments (which is a valid assumption in plenty of scenarios), and the check in question is even kept to an extent. Actual compile-time UB is usually reported. (also, even if the compiler didn't utilize UB and kept wrapping integer semantics, the code would still be partly broken were it instead, say, "x * 0x1f0 / 0xffff", as the multiplication could overflow to 0) The problem with making the compiler give warnings on dead code elimination (which is what deleting things after UB really boils down to) is that it just happens so much, due to macros, inlining, or anything where you may check the same condition once it has already been asserted (by a previous check, or by construction). So you'd need some way to trace back whether the dead-ness comes directly from user-written UB (as opposed to compiler-introduced UB, which a compiler can do if it doesn't change the resulting behavior; or user-intended dead code, which is gonna be extremely subjective) which is a lot more complicated. And dead code elimination isn't even the only way UB is used by a compiler.
- nwellnhof 4y ago> also, even if the compiler didn't utilize UB and kept wrapping integer semantics, the code would still be partly broken were it instead, say, "x * 0x1f0 / 0xffff", as the multiplication could overflow to 0 That's the most important point! You simply cannot detect overflow when multiplying integers in C after the fact. This is not GCC's fault. I agree that some of the optimizations exploiting UB are too aggressive, but the article presents a really bad example.
- deleted 4y ago[deleted]
- kazinator 4y ago> and it's the C standard. No, it's puerile interpretations of the C standard learned from the comp.lang.c newsgroup and such places rather than from engineering work.