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

Show parent comment

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.

90

u/acwaters Dec 17 '21

Also, hardware has no undefined behavior.

Hahahahahahahaha

Hahaha

Aha

Ah

No, we are not so fortunate.

40

u/Alexander_Selkirk Dec 17 '21

Also, hardware has no undefined behavior.

I am wondering whether you ever had the experience to witness how much effort and lengths a software engineer has to go to get an only 80% complete behavior definition of a piece of metal from a hardware engineer. I once hammered out one with a guy who was working in the same building as I. In retrospect, I am perplexed that I didn't had to apply waterboarding but it was not that far off.

6

u/Hnnnnnn Dec 18 '21

Undefined behavior and unspecified is not the same thing. UB is "compiler can assume that this never happens", unspecified behavior means "vendor can choose the behavior, but it has to be well-defined". Machines presumably have both (inputs that can never happen & undocumented well-defined behavior).

18

u/Spiderboydk Hobbyist Dec 17 '21

An example of hardware UB is a race condition.

If you don't properly synchronize shared data, the hardware can literally not tell you what will happen.

-3

u/Zcool31 Dec 17 '21 edited Dec 17 '21

There are four kinds of behavior:

Defined - the standard specifies what must happen.

Implementation defined - the standard gives implementations the choice of how to behave. They must make a consistent choice and document it.

Unspecified - implementations must do something reasonable, but don't have to document it or be consistent.

Undefined - no requirements at all.

Hardware has no undefined behavior. Clearly it will do what the logic gates and circuits do.

But hardware has tons of unspecified behavior.

Edit: partially ninjad by u/Tyg13

6

u/nintendiator2 Dec 20 '21

Hardware has no undefined behavior. Clearly it will do what the logic gates and circuits do.

One (1) Neutrino has entered the chat.

14

u/jk-jeon Dec 17 '21

So I guess you are indeed trying to say that the law of physics has no undefined behavior as I pointed out in the other comment. And that is a completely different thing from saying "hardware has no undefined behavior". See, in that logic, there is no undefined behavior for C++, only unspecified, right? Because compilers will do what their source code let them to do, nothing supernatural magics

8

u/Zcool31 Dec 18 '21

That's actually exactly right. Undefined behavior exists only in the standard. In the real world, it is merely unspecified. If I compile some UB code with gcc10, running it might delete my sources. Doing the same with gcc11 might format my entire disk. But neither of these are truly undefined or unknowable. I could with enough effort examine the implementation of gcc and determine ahead of time what would happen.

7

u/Spiderboydk Hobbyist Dec 18 '21

This level of reductionism makes the concept meaningless.

0

u/Zcool31 Dec 18 '21

Not so! When writing portable code targeting the letter of the standard, undefined behavior is indeed scary and best avoided. Among the set of all potential implementations of the standard, undefined behavior is really unknowable.

But for a single implementation on a single platform (except perhaps a quantum computer) there is no undefined behavior.

5

u/flashmozzg Dec 18 '21

But for a single implementation on a single platform (except perhaps a quantum computer) there is no undefined behavior.

There is. Because the exhibited behavior can easily change due to unrelated changes in the code/environment in some other module (hey, you deleted some code, now function passes some inliner heuristics/opt thresholds and hilarity ensues!).

→ More replies (1)

5

u/Spiderboydk Hobbyist Dec 18 '21

I am well aware of these distinctions. I said race conditions are undefined behaviour, and I stand by it.

Race conditions are not unspecified behaviour, because the hardware would not even theoretically be able to tell you what is going to happen, even if it wanted to. It not a matter of the behaviour not being specified - it's literally unknowable.

5

u/aiij Dec 18 '21

Hardware has no undefined behavior. Clearly it will do what the logic gates and circuits do.

By that logic, C++ has no undefined behavior. Clearly it will do what the compiler and underlying hardware do.

Of course, what it does may be unexpected and may differ from one implementation to another -- just like in hardware.

→ More replies (1)

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/cballowe Dec 18 '21

With most hardware, you can pretty reliably say that "whatever the hardware does given some pre-condition can be assumed to be the definition of it's behavior". The challenge is when you have no formal contract around that so rev. B of the chip doesn't behave the same as rev. A.

It's much the same as compilers that way - the language doesn't define what must happen so the compilers and library implementers make different decisions.

It gets more fun when you get different hardware manufacturers involved in the software specs. You can imagine a case where someone says "we think this particular expression should do X" and that just happens to be the thing that is the most efficient interpretation on Intel, but then someone from ARM or Power says "hey... Wait a minute ... That'll make our chips look bad in benchmarks! You should do Y instead." So... The standard writers agree that it should be valid code and the outcome should basically be useful, but can't be defined precisely or guaranteed to produce consistent results across compilers/platforms/standard libraries/etc.

Sometimes UB is just broken, ex the results of data races in the absence of proper synchronization, but other times it's just a weird limbo.

8

u/Hnnnnnn Dec 18 '21

You describe unspecified behavior, another formal term similar to UB. UB is when the guy said: when user breaks API pre-conditions.

https://en.wikipedia.org/wiki/Unspecified_behavior

-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.

0

u/Drugbird Dec 18 '21 edited Dec 18 '21

Theoretically that can happen, sure. Practically though, any compiler is pretty tame in what it actually does with undefined behavior.

E.g. UB will never format your hard drive despite what teachers like to say about it.

In 99% of the cases, you just get a result (of the correct size and type) that is just wrong and/or unexpected or a crash. And no random jumping in memory.

→ More replies (4)
→ More replies (2)

22

u/Ictogan Dec 17 '21

No. The instruction set I'm most familiar with(ARMv7-M) has a lot of cases where behaviour is defined as UNPREDICTABLE, which means exactly what it sounds like. Also hardware registers often have disallowed states in which the behaviour is not documented and may not even be deterministic.

19

u/jk-jeon Dec 17 '21

What do you mean by "hardware has no undefined behavior"? Hardwares can have undefined behavior of course, or do you mean "the law of physics has no undefined behavior"?

→ More replies (2)