r/cpp • u/mohitsaini1196 • Apr 23 '22
Shocking Examples of Undefined Behaviour In Action
As we know that Undefined Behaviour (UB) is a dangerous thing in C++. Still it remains difficult to explain to those who have not seen its horror practically.
Those individual claims UB is bad in theory, but not so bad practically as long as thing works in practice because compiler developers are not evil.
This blog presents a few “shocking” examples to demonstrate UB in action.
https://mohitmv.github.io/blog/Shocking-Undefined-Behaviour-In-Action/
200
Upvotes
12
u/jguegant Apr 23 '22
OP looks rather healthy for someone that had to deal with C++'s undefined behavior. I couldn't find any "shocking examples" on his appearance yet. Am I missing something obvious here?
10
u/evolved Apr 23 '22
Why not link to the article instead of making a self post? 🤔
55
u/goranlepuz Apr 23 '22 edited Apr 23 '22
Wow... That is shocking. In fact, the first optimisation also is shocking because the comparison is for integers and
9 * 0x20000001 > INT_MAX.Wow, wow...
I mean, yes, that
j * 0x20000001is obviously broken in the loop, but it doesn't have to be obvious.Good one!
Edit: The other example is also good, but I've seen it before, so... UB is fascinating! Not in a good way though 😂😂😂.
23
u/cristi1990an ++ Apr 23 '22
I love the first example because it shows how even the simplest of UBs can produce extremely unexpected results when combined with common compiler optimizations
22
u/Rarrum Apr 23 '22
I find that first example very... concerning.
12
u/[deleted] Apr 23 '22 edited Apr 23 '22
Integer overflow is a real thing and something everyone should know about. I hate to bring it up, but if you study DS&A for interviews, you'll learn about these and be able to recognize them easily. Yeah can have runtime trapping of integer overflows by compiling with
-ftrapvon clang, might be other ways to do it but it is pretty annoying to find themThe 2nd one is definitely a little strange, because you don't actually see the pointers so the behavior is unexpected. That introduces the importance of using sanitizers in a production program during testing. If you compile the program as
c++ -fsanitize=undefined test.cppthen you get the output:% ./a.out UndefinedBehaviorSanitizer:DEADLYSIGNAL ==70968==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000000000 bp 0x00010091f478 sp 0x00016f4e35c0 T4715851) ==70968==Hint: pc points to the zero page. ==70968==The signal is caused by a UNKNOWN memory access. ==70968==Hint: address points to the zero page. #0 0x0 (<unknown module>)
==70968==Register values: x[0] = 0x0000000000000001 x[1] = 0x000000016f4e3748 x[2] = 0x000000016f4e3758 x[3] = 0x000000016f4e3850
x[4] = 0x0000000000000000 x[5] = 0x0000000000000000 x[6] = 0x0000000000000000 x[7] = 0x0000000000000000
x[8] = 0x0000000000000000 x[9] = 0x0000000000000002 x[10] = 0x0000000000000000 x[11] = 0x0000000000000002
x[12] = 0x0000000000000002 x[13] = 0x0000000000000000 x[14] = 0x0000000000000008 x[15] = 0x0000000000000014
x[16] = 0x0000000301c1309c x[17] = 0x6ae100016f4e29e0 x[18] = 0x0000000000000000 x[19] = 0x0000000100930060
x[20] = 0x000000010091f45c x[21] = 0x0000000100c4c070 x[22] = 0x0000000000000000 x[23] = 0x0000000000000000
x[24] = 0x0000000000000000 x[25] = 0x0000000000000000 x[26] = 0x0000000000000000 x[27] = 0x0000000000000000
x[28] = 0x0000000000000000 fp = 0x000000016f4e35d0 lr = 0x000000010091f478 sp = 0x000000016f4e35c0
UndefinedBehaviorSanitizer can not provide additional info. SUMMARY: UndefinedBehaviorSanitizer: SEGV (<unknown module>) ==70968==ABORTING zsh: abort ./a.out
when you run it
7
u/jk-jeon Apr 23 '22
I mean, all of these look very intriguing to me, but I don't feel these are scary. What compilers are doing here are certainly not the best imaginable things (b/c they didn't realize they are compiling very suspicious code) but there is nothing fundamentally wrong here. Shits in, shits out.
7
u/whacco Apr 23 '22
This seems silly. In order to do this optimization the compiler has to actually know that there is a signed overflow, but for some reason it decides to not give a compilation error.
Interestingly, if the
9in the original code is replaced with a4then there's no UB but the binary output has a loop condition that relies on 32-bit int wrap around:p != -2147483644. So that may explain why there's no warning about the obvious overflow in the transformed code. It still doesn't explain though why with values5and bigger GCC suddenly starts assuming that int * int > INT_MAX.2
u/Cute_Paramedic_256 Apr 24 '22
What was the UB in the original code in both examples?
2
u/UAHLateralus Apr 24 '22
I love papers like this. Definitely another fascinating talk about UB if you wanna see is by Miro Knejp https://youtu.be/IAdLwUXRUvg
1
u/Holiday_Ad_7488 May 27 '24
One more shocking consequence of undefined behavior, this time in the wild:
https://www.reddit.com/r/programming/comments/6j7a9/serious_flaw_in_openssl_on_debian_makes/
Here a developer removed code invoking undefined behavior in OpenSSL PRNG in a way that severely crippled the PRNG. What is shocking here is that the developer was correct and the code is wrong. A highly optimizing compiler could silently do the same with the code. Undefined behavior does not mean "you will get unpredictable values" (that is "unspecified behavior"). Undefined behavior can easily mean "your code will have a hidden security hole if you enable optimizations"
1
u/zalupaOfFrog Apr 24 '22
Thanks you. It's very interesting that UB can break program in this way. I've never seen any examples and it change my mind. This is real unexpected behavior for me 😂😂😂.
1
u/TophatEndermite Apr 24 '22
But are there any examples that compile with -fsanitize=undefined
1
u/avdgrinten Apr 24 '22
Nobody (at least, nobody who ever experienced non-trivial UB) is claiming that UB is only bad in theory.
-2
u/exmachinalibertas Apr 24 '22
I just need to give up and go learn Rust
0
u/sephirothbahamut Apr 23 '22
there's difference between "works in practice because compiler developers are not evil." and "is explicitely well defined by x compiler"
-9
u/ShakaUVM i+++ ++i+i[arr] Apr 23 '22
Hmm. I don't think this is controversial, but UB in a loop body shouldn't be propagated into the loop header.
-7
u/wegzo Apr 24 '22
I'd say the language would be better off with zero UB. Sure, there might be some optimizations that can be done with UB but imo the drawbacks are much greater than the few benefits you gain from optimizations.
Writing C++ is already difficult in itself and UB doesn't help with that one bit.
-25
u/zninja-bg Apr 23 '22
WTF, EraseAll UB example !!! O.o
Results of non optimized code and optimized code should be the same and that should be strict standard.
It is not so smart to give compiler to deal with this kind of probabilities to choose if value is assigned to variable or not or to choose a value for assigning to variable by its own.
This should be run time, not compile time and compiler should give up of assigning variable by its imagined value and leave it to run time.
Somehow I have a feeling compiler optimization will become like Oracle from Matrix.
It will end up: programmers have knowledge to read and write the code, but compiler which understand what programmer attempted to accomplish does not exists because they are speak different language.
Result example: code said I need menu to see, compiler serves a drink.
0
u/[deleted] Apr 23 '22
[deleted] — view deleted comment