• We've upgraded our forums. Please post any issues/requests in this thread.

compiler optimizations & code breaking

Joined
Jul 23, 2011
Messages
1,555
Likes
1,459
Location
Kaunas, Lithuania
System Name Eternal WIP ][ HP ProLiant server blade
Processor AMD FX-8320 @ 4.4/4.1 GHz ][ 2x Intel Xeon L5520 @ 2.27GHz
Motherboard Asus Crosshair V Formula-Z ][ -
Cooling Corsair H55 ][ Stock
Memory 2x4GB Kingston HyperX Genesis @ 1866MHz ][ 4x4 GiB Hynix @ 1333 MHz; ECC on
Video Card(s) 2x MSI GeForce GTX 770 DirectCU II OC ][ None
Storage 1TB WD Black + 1.5TB WD Green + 750GB Samsung + 64GB OCZ Agility 4 ][ 3x 1 TB Hitachi
Display(s) Asus VG278H + Asus VH226H ][ None
Case Antec Nine Hundred ][ it's a server blade, lol
Audio Device(s) Supreme FX II (Integrated); Using optical S/PDIF output ][ none
Power Supply Corsair AX1200i ][ [stock]
Software Gentoo Linux ][ Linux Mint 16
Benchmark Scores 65970 BogoMIPS. Ha. Ha. Ha. >=|
#1
Greetings fellow programmers! xD

Ok, ./serious_mode

Recently I came across a strange thing: code I wrote was working fine, but as soon as I decided to make a non-debug build (has compiler optimizations on), I noticed a strange thing - my program promptly segfaulted each time I ran it. If I rebuild it with compiler optimizations off, it then works fine again. That was a little shocking to me.
I then searched the web (which was mostly confined to reading stuff on stackoverflow.com). I found out that others have also encountered this. Some with long time programming experience encountered this many times and sometimes even expect this to happen sooner or later with growing projects. Though, pretty much every instance I found people mentioning this, were just small sidenotes to some discussions involving compiler optimizations, thus, not discussed any more deeper than that.

So I would like to ask You all to share Your thoughts on such a thing - "how, why, when, etc."
And I mean, in general sense. Not just about this specific case I encountered.

One of my thoughts - compiler optimizations, AFAIK, according to standard (at least on C & C++) state that it should not alter the a program works in a way that would produce different results compared to a non-optimized one. That it normally does - makes sure it runs the same. If the program does run and behave differently (like my previously mentioned segfault), I think it is not automatically a compiler's fault. If one were to write his/her code in such a way that confuses the compiler, it could accidentally make the program behave in an unintended way.
Thus, I wonder, if a person produces code that is handled by the compiler this way, could it be an indication that the said person's coding style is not good and/or that this person is writing his/her code in such a way that would make any experienced hacker frown?
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
17,021
Likes
17,871
Processor Core i7-4790K
Memory 16 GB
Video Card(s) GTX 1080
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 7
#2
what did you do that broke? that's not supposed to happen
 
Joined
Jul 23, 2011
Messages
1,555
Likes
1,459
Location
Kaunas, Lithuania
System Name Eternal WIP ][ HP ProLiant server blade
Processor AMD FX-8320 @ 4.4/4.1 GHz ][ 2x Intel Xeon L5520 @ 2.27GHz
Motherboard Asus Crosshair V Formula-Z ][ -
Cooling Corsair H55 ][ Stock
Memory 2x4GB Kingston HyperX Genesis @ 1866MHz ][ 4x4 GiB Hynix @ 1333 MHz; ECC on
Video Card(s) 2x MSI GeForce GTX 770 DirectCU II OC ][ None
Storage 1TB WD Black + 1.5TB WD Green + 750GB Samsung + 64GB OCZ Agility 4 ][ 3x 1 TB Hitachi
Display(s) Asus VG278H + Asus VH226H ][ None
Case Antec Nine Hundred ][ it's a server blade, lol
Audio Device(s) Supreme FX II (Integrated); Using optical S/PDIF output ][ none
Power Supply Corsair AX1200i ][ [stock]
Software Gentoo Linux ][ Linux Mint 16
Benchmark Scores 65970 BogoMIPS. Ha. Ha. Ha. >=|
#3
what did you do that broke? that's not supposed to happen
I am not sure, did not have time (and enough will) to debug it yet.
That reminds me - I already spent hours on TPU, while I should be studying for tomorrow ~___~
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,397
Likes
5,475
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#4
I am not sure, did not have time (and enough will) to debug it yet.
That reminds me - I already spent hours on TPU, while I should be studying for tomorrow ~___~
I have a bad habit of doing this too. If you can find the offending line(s) of code, we can try to help you out.
 
Joined
Jul 23, 2011
Messages
1,555
Likes
1,459
Location
Kaunas, Lithuania
System Name Eternal WIP ][ HP ProLiant server blade
Processor AMD FX-8320 @ 4.4/4.1 GHz ][ 2x Intel Xeon L5520 @ 2.27GHz
Motherboard Asus Crosshair V Formula-Z ][ -
Cooling Corsair H55 ][ Stock
Memory 2x4GB Kingston HyperX Genesis @ 1866MHz ][ 4x4 GiB Hynix @ 1333 MHz; ECC on
Video Card(s) 2x MSI GeForce GTX 770 DirectCU II OC ][ None
Storage 1TB WD Black + 1.5TB WD Green + 750GB Samsung + 64GB OCZ Agility 4 ][ 3x 1 TB Hitachi
Display(s) Asus VG278H + Asus VH226H ][ None
Case Antec Nine Hundred ][ it's a server blade, lol
Audio Device(s) Supreme FX II (Integrated); Using optical S/PDIF output ][ none
Power Supply Corsair AX1200i ][ [stock]
Software Gentoo Linux ][ Linux Mint 16
Benchmark Scores 65970 BogoMIPS. Ha. Ha. Ha. >=|
#5
LOL!

(the code is in C++, btw)

EDIT: In a constructor of one class, printf() is called. It appears that when compiled with optimizations, after calling prinf(), one register that is holding the address of that object, gets overwritten to 0x1
Then a couple tens of lines and some calls later, it get dereferenced... *segfault*

EDIT2: Fix'd this with a little pinch of inline-asm =]
 
Last edited:

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#6
Just out of curiosity, why are you calling printf() in a class constructor? For debugging purposes?
 
Joined
Jul 23, 2011
Messages
1,555
Likes
1,459
Location
Kaunas, Lithuania
System Name Eternal WIP ][ HP ProLiant server blade
Processor AMD FX-8320 @ 4.4/4.1 GHz ][ 2x Intel Xeon L5520 @ 2.27GHz
Motherboard Asus Crosshair V Formula-Z ][ -
Cooling Corsair H55 ][ Stock
Memory 2x4GB Kingston HyperX Genesis @ 1866MHz ][ 4x4 GiB Hynix @ 1333 MHz; ECC on
Video Card(s) 2x MSI GeForce GTX 770 DirectCU II OC ][ None
Storage 1TB WD Black + 1.5TB WD Green + 750GB Samsung + 64GB OCZ Agility 4 ][ 3x 1 TB Hitachi
Display(s) Asus VG278H + Asus VH226H ][ None
Case Antec Nine Hundred ][ it's a server blade, lol
Audio Device(s) Supreme FX II (Integrated); Using optical S/PDIF output ][ none
Power Supply Corsair AX1200i ][ [stock]
Software Gentoo Linux ][ Linux Mint 16
Benchmark Scores 65970 BogoMIPS. Ha. Ha. Ha. >=|
#7
Just out of curiosity, why are you calling printf() in a class constructor? For debugging purposes?
Yeah, I could say yes. More like, for informational purposes, though.