While investigating a dubious claim, I wrote this little test program noway.c
int proveit()
{
unsigned int n = 0;
while (1) n++;
return 0;
}
int main()
{
proveit();
return 0;
}
Testing this, I get:
$ clang -O noway.c
$ ./a.out
zsh: illegal hardware instruction ./a.out
Wat.
If I compile without optimizations it hangs as expected. I looked at the assembly, and without all the bells and whistles the main
function looks like this:
_main: ## @main
pushq %rbp
movq %rsp, %rbp
ud2
Where ud2
is apparently is an instruction specifically for undefined behavior. The aforementioned dubious claim, "A function that never returns is UB", is reinforced. I still find it hard to believe though. Really!? You can't safely write a spin loop?
So I guess my questions are:
- Is this a correct reading of what is going on?
- If so, can someone point me to some official resource that verifies it?
- What is a situation in which you would want this type of optimization to occur?
Relevant info
$ clang --version
Apple clang version 11.0.0 (clang-1100.0.20.17)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin