Thread: Which GCC O2 optimization flags are likely to prevent an Segmentation fault?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    Which GCC O2 optimization flags are likely to prevent an Segmentation fault?

    I am building an FLOSS project and getting Segmentation faults during build-time (where it runs an project created exe).

    The project is created using MinGW64 GCC version (Rev3, Built by MSYS2 project) 10.2.0.

    The project is SDCC; I am trying to package it under MSys2 64 bit MinGW.

    It has the Segmentation faults when option "-O1" is used.
    But, does not have the Segmentation faults when option "-O2" is used.

    From Optimize Options (Using the GNU Compiler Collection (GCC))

    The flags added by "-O2" are:
    Code:
    -falign-functions  
    -falign-jumps 
    -falign-labels  
    -falign-loops 
    -fcaller-saves 
    -fcode-hoisting 
    -fcrossjumping 
    -fcse-follow-jumps  
    -fcse-skip-blocks 
    -fdelete-null-pointer-checks 
    -fdevirtualize  
    -fdevirtualize-speculatively 
    -fexpensive-optimizations 
    -ffinite-loops 
    -fgcse  -fgcse-lm  
    -fhoist-adjacent-loads 
    -finline-functions 
    -finline-small-functions 
    -findirect-inlining 
    -fipa-bit-cp  
    -fipa-cp 
    -fipa-icf 
    -fipa-ra  
    -fipa-sra  
    -fipa-vrp 
    -fisolate-erroneous-paths-dereference 
    -flra-remat 
    -foptimize-sibling-calls 
    -foptimize-strlen 
    -fpartial-inlining 
    -fpeephole2 
    -freorder-blocks-algorithm=stc 
    -freorder-blocks-and-partition  
    -freorder-functions 
    -frerun-cse-after-loop  
    -fschedule-insns  
    -fschedule-insns2 
    -fsched-interblock  
    -fsched-spec 
    -fstore-merging 
    -fstrict-aliasing 
    -fthread-jumps 
    -ftree-builtin-call-dce 
    -ftree-pre 
    -ftree-switch-conversion 
    -ftree-tail-merge 
    -ftree-vrp
    I am trying to build with these added flags to see if the seg fault is present or not.
    Code:
        _debug_FLAGS+=" -finline-functions"
        _debug_FLAGS+=" -finline-small-functions"
        _debug_FLAGS+=" -findirect-inlining"
        _debug_FLAGS+=" -foptimize-strlen"
        _debug_FLAGS+=" -ftree-switch-conversion"
    It takes several hours for the build to finish.

    Edit: My question is which of the 5 I should keep trying and which of the others I should try? I am guessing I will hit the max command line length if I try to use all the options.

    Tim S.
    Last edited by stahta01; 10-05-2020 at 03:50 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the answer is: the ones you use when you are lucky (or unlucky, as the case may be).

    After all, it is probably just some bug that happens to disappear when a particular optimisation is applied; in another case it could very well have been some bug that only occurs when a particular optimisation is applied. In my understanding, the latter is more often true, i.e., it is more likely that programmers might code with flawed assumptions that don't hold when optimisations are applied, and then it is also more likely for a compiler bug to cause an optimisation to be applied incorrectly, so optimisation flags are usually more likely to result in a segmentation fault than to prevent one.
    Last edited by laserlight; 10-05-2020 at 03:58 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Ones of these appear to be hiding the seg fault.
    My guess is "-fdelete-null-pointer-checks".
    Building with the last six removed to see if one of them might be the cause.
    Edit: The problem was not in the last six.

    Code:
    -fcaller-saves
    -fcode-hoisting
    -fdelete-null-pointer-checks
    -ffinite-loops
    -finline-functions
    -finline-small-functions
    -findirect-inlining
    -fisolate-erroneous-paths-dereference
    -foptimize-sibling-calls
    -foptimize-strlen
    -fpartial-inlining
    -ftree-switch-conversion
    Tim S.
    Last edited by stahta01; 10-05-2020 at 09:19 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The winner appears to be.

    -fcaller-saves
    Enable allocation of values to registers that are clobbered by function calls, by emitting extra instructions to save and restore the registers around such calls. Such allocation is done only when it seems to result in better code.

    This option is always enabled by default on certain machines, usually those which have no call-preserved registers to use instead.

    Enabled at levels -O2, -O3, -Os.
    I had hoped for a result that gave me a hint on where to look in the source code. I still need to confirm the problem still exists after removing this option that I believe is hiding the segfault. Since, I am building an SVN project it might have added an upstream fix. Or some other minor package change I did fixed the problem.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    -Og -ftree-bit-ccp -ftree-pta -fif-conversion2 -finline-functions-called-once -fcaller-saves
    The above options avoids the segment fault error; now, I need to find how to avoid the error by fixing the project source code or just decide to use these options.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by stahta01 View Post
    Code:
    -Og -ftree-bit-ccp -ftree-pta -fif-conversion2 -finline-functions-called-once -fcaller-saves
    The above options avoids the segment fault error; now, I need to find how to avoid the error by fixing the project source code or just decide to use these options.

    Tim S.
    Comment out everything except a line in main which prints out "Hello world". If it still segfaults then the problem is not in your source. Then gradually comment in to "Hello world" and comment out in the crashing program until you can make "Hello world" segfault and the crashing program not segfault. That should enable you to toggle between the two conditions with a few simple code changes, and tell you where the fault lies.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Malcolm McLean View Post
    Comment out everything except a line in main which prints out "Hello world". If it still segfaults then the problem is not in your source. Then gradually comment in to "Hello world" and comment out in the crashing program until you can make "Hello world" segfault and the crashing program not segfault. That should enable you to toggle between the two conditions with a few simple code changes, and tell you where the fault lies.
    Please read a thread before replying to them!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The segment faults are in sdar which is in folder sdbinutils of SDCC.
    sdbinutils is a fork of binutils version 2.30 sdar is same as binutils ar.

    I have decided I am going to have to read the source code of sdar to look for possible problems in the files listed in the gdb backtrace.
    But, first, I am going to have to do a dozen or so backtraces. It is possible that there is many different causes of the segment faults.
    And, who knows maybe only one file will be listed in common ( not counting ar.c ) and I will be able to find the problem quickly.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I have tried to update sdbinutils to be based on binutils version 2.31.1 and for 5 of the about 25 backends that use sdbinutils it no longer seg faults when compiled with
    "-Og -fcaller-saves" I consider this to be a good enough fix; but, only once I verify that the majority of the rest of the backends are also fixed from needing these options to avoid seg faults (-ftree-bit-ccp -ftree-pta -fif-conversion2 -finline-functions-called-once).

    The option "-fcaller-saves" likely will only interfere slightly if the debugger is used. While (-ftree-bit-ccp -ftree-pta -fif-conversion2 -finline-functions-called-once) would have major effect on debugging.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prevent optimization of zero-ed memory.
    By Sammun-Mak in forum C Programming
    Replies: 6
    Last Post: 10-18-2019, 03:47 PM
  2. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  3. optimization flags
    By markucd in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2006, 09:08 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread