Hacker News new | ask | show | jobs
Attention people who think UB is fine (blog.habets.se)
7 points by aszlig 41 days ago
2 comments

This is only true because compiler authors are interpreting "undefined behavior" as a license for "user-hostile behavior". There are other choices one can make.
Author here: yes and no.

UB can also be things that the compiler is unable to express. It's NOT "haha, now I can do anything", it's like asking what the correct answer is to a logically inconsistent question. The compiler cannot even express the question.

Let's say the compiler encodes some GC information in the low two bits of int pointers. What does it mean if a function is called with an unaligned pointer, then? Mu, unask the question. It's UB, and no diagnostic required nor even feasible.

Deciding to omit the "return" opcode for an unreachable piece of code is not deliberately user-hostile behavior.
The GCC compiler is very unpredictable about what it considers unreachable code. Once, I wanted to avoid reading an array x[i] to save L1 cache space, so I used a computed goto (goto base + delta * i;) followed by return x1; return x2; ... return xn;. However, the compiler decided that only the first two return values were reachable and optimized away the rest of the code
I'm not surprised it didn't like that. There's a lot of reasons those returns might not be in the exact positions you expect.

Did you calculate delta from the first two return values?

I put labels on each return, and took their addresses to get the base and delta. The creator of c++ always says that such kind of code has to work "because it is c++", meaning the programmer should be able to do whatever he wants. Anyways I also used assembler to make it work (had to include nop instructions to pad the addresses, because the goto can't go to any byte. So it consumed a lot of instruction l1 cache, which is not as bad as data cache, but it ended being slow anyways because gotos trigger branch prediction. In the end the fastest method was to pack the array in uint64 and do (x >> i) & 111b
But did you use the address of every single return or did you assume they all had the same delta?

I can't find the quote you're talking about but I'll be surprised if it extends to assuming memory locations. If any pointer has to support touching any variable, even ones you never took the address of, that ruins so many basic and sensible optimizations.

Unless I'm misunderstanding, a normal computed goto would have an array of all the destinations. And hopefully it would compile to a delta multiply. But if you need to force it... maybe a compile time check that all the labels are delta bytes apart would let you do that math? I don't know, it's not part of the actual standard, it might be janky.

Did a plain old switch statement compile into something you didn't like?

Everything fun in life makes you fat, gets her pregnant, or is undefined behavior
Since undefined behavior is impossible, that means there are only two options.