r/cpp • • Dec 17 '21

Undefined Behaviour

I found out recently that UB is short for Undefined Behaviour and not Utter Bullshit as I had presumed all this time. I am too embarrassed to admit this at work so I'm going to admit it here instead. I actually thought people were calling out code being BS, and at no point did it occur to me that as harsh as code reviews can be, calling BS was a bit too extreme for a professional environment..

Edit for clarity: I know what undefined behaviour is, it just didn't register in my mind that UB is short for Undefined Behaviour. Possibly my mind was suffering from a stack overflow all these years..

407 Upvotes

98 comments sorted by

View all comments

84

u/dontyougetsoupedyet Dec 17 '21

It isn't as complicated as folks make out. UB is an agreement between you and your compiler so that the compiler can do its job better. A lot of folks don't realize that the job of the compiler in some languages is to rewrite your program into the most efficient version of your code that it can. You agree to not feed it certain code, and the compiler agrees to optimize the fuck out of the code you do feed it, and you both agree that if you do feed it code that you agreed to avoid using it means that you know what you're doing and are aware that the compiler is free to ignore that code.

Despite what some folks assert, UB is a good thing. You just have to be aware of what the compiler's job is for your language. Some compilers for some languages have a different job, but for C++ the job of the compiler is to produce a much faster version of your program than you wrote.

29

u/Zcool31 Dec 17 '21

if you do feed it code that you agreed to avoid using it means that you know what you're doing and are aware that the compiler is free to ignore that code.

Another aspect of this is the distinction between the standard and an implementation of the standard. Undefined means the standard places no requirements on what an implementation might do. But implementations, such as specific compilers or platforms, are free to make stronger guarantees. A popular example is using unions for type punning. UB according to the standard, yet explicitly supported by GCC.

Also, hardware has no undefined behavior.

20

u/almost_useless Dec 17 '21

Also, hardware has no undefined behavior.

Surely this is not true?

0

u/qoning Dec 17 '21

As far as I know, most instruction sets have clearly defined preconditions and postconditions for every instruction. Now there might be bugs or incomplete implementations, but the instruction sets themselves are fully defined.

36

u/SirClueless Dec 17 '21

most instruction sets have clearly defined preconditions and postconditions for every instruction

You're describing an instruction set with UB in it. If you violate the preconditions you get UB. The only way you don't get UB is if the spec defines what happens under all possible conditions, and as you correctly state, most instruction sets do not do this and have preconditions you are expected to satisfy.

-1

u/Orlha Dec 18 '21

Well, violating the precondition might make the operation provide an unexpected result, but that wont necessary make a whole program UB. You might also just not use the result.

In C++ model its different.

9

u/SirClueless Dec 18 '21

Are you sure about that? Violating the preconditions of an instruction set can result in writing arbitrary values to arbitrary locations in memory, jumping to arbitrary memory addresses and interpreting the data there as instructions to execute, etc.

1

u/Orlha Dec 18 '21

I guess it's possible, but can be pretty rare depending on the platform.

I've written a lot of x86-64 hand-assembly in the past and IIRC all the instructions I used were UB free. At worst they had a defined set of rules which when broken would result in a CPU exception.

5

u/SirClueless Dec 18 '21

x86-64 is full of UB. It explicitly reserves bits in flag registers and some output registers as well as any opcodes that aren't defined by the x86-64 ISA. Executing these opcodes or depending on the value of these bits is, to quote the ISA document, "not only undefined, but unpredictable". It's very easy to trigger this behavior, even in an otherwise well-formed assembly program, for example by jumping into the middle of an instruction.

https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf

I understand what you're trying to say, which is that there's a relatively simple set of rules you can follow as compared to C++ and Intel comparatively precisely defines far more exceptional behavior than C++ and leaves less room for undefined behavior. But it doesn't attempt to remove all of it.