473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Segfault only with optimization

I have a C program that GCC compiles without warnings and it runs just
fine. However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors).

I know nothing about compiler otimization, other than it makes my program
run faster. Any suggestions on how to debug this, or what to look for
would be greatly appreciated.

Thank you.
Mar 17 '08 #1
16 3675
On Mar 17, 10:04*am, Jeff Taylor <dev.n...@spam.netwrote:
I have a C program that GCC compiles without warnings and it runs just
fine. *However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors). *

I know nothing about compiler otimization, other than it makes my program
run faster. *Any suggestions on how to debug this, or what to look for
would be greatly appreciated.
What are the compile switches that you use?
Crank the warnings up to the maximum level.
Feed the code to splint:
http://www.splint.org/
Mar 17 '08 #2
Jeff Taylor wrote:
I have a C program that GCC compiles without warnings and it runs just
fine. However, when I compile it using any of the -O options (-O0, -O1,
etc.) it segfaults when executed (still no compiler warnings or errors).
There are several approaches to this problem

1) The approach I usually take:
1.a Include the debug information when compiling (usually with -g)
1.b Do not strip the executable
1.c Start the gdb debugger and run your program within the debugger.
1.d When it crashes, take note of where it crashes. The specific
gdb command is "backtrace" as far as I remember.
1.e Look at the local variables. Note that the values displayed
by gdb are probably wrong since optimizations and debugging
do not mix well.
1.f Recompile the module where the crash happens WITHOUT
any optimizations
1.g Relink
1.h Rerun. Does the program crash? If no, you have found the
module where the fault is. Go to step 1.j
If yes, the fault is still there. Take another module from
the modules in the backtrace and recompile. Go to step 1.g.
1.i If you have recompiled all the modules in the backtrace and
the crash still persists, recompile one by one all other
modules until the crash disappears. The last module that
you recompiled is the module with the fault.
1.j Isolate the fault within the module. This can be tricky
unless there is a way to tell the compiler to enable/disable
optimizations in a function by function basis.

2) The approach recommended by the regulars in this group:
Read the source code. If you read hard enough the bug will be
obvious to you.
I know nothing about compiler otimization, other than it makes my program
run faster. Any suggestions on how to debug this, or what to look for
would be greatly appreciated.
There is no free lunch. Optimized programs are more sensible to
programming errors than no optimized ones. Besides, you expose
yourself to the bugs of the optimizer, that are many.

If you are doing straight C though, it is highly unlikely that
there is a bug in the optimizer...

Thank you.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 17 '08 #3
jacob navia wrote:
Peter Nilsson wrote:
>Jeff Taylor <dev.n...@spam.netwrote:
>>I have a C program that GCC compiles without warnings and
it runs just fine.

Realise that output is not the only measure of correctness.

Interesting interesting...

Can you name any other measure that doesn't rely on
output?
A program that invokes a buffer overrun, but happens to produce expected
output is surely not correct?

Using a psychic medium is not allowed.

>> However, when I compile it using any of the -O options
(-O0, -O1, etc.) it segfaults when executed (still no
compiler warnings or errors).

I'm afraid you are responsible for diagnosing errors in your
code, not your compiler.
Deep thought.
He means semantic errors. Or does your compiler do the programming for
the programmer? :-)

<snip>

Mar 17 '08 #4
santosh wrote:
jacob navia wrote:
>Peter Nilsson wrote:
>>Jeff Taylor <dev.n...@spam.netwrote:
I have a C program that GCC compiles without warnings and
it runs just fine.
Realise that output is not the only measure of correctness.
Interesting interesting...

Can you name any other measure that doesn't rely on
output?

A program that invokes a buffer overrun, but happens to produce expected
output is surely not correct?
How the hell do you know that there is a buffer
overrun if the output is not affected?

To KNOW that there is a buffer overrun the program MUST
do something it should not do, i.e. produce an output
that is different than the expected output

If the output of the program is normal you have NO WAY
to know there is a buffer overrun.

And if you hook a debugger and see some buffer being
overrun that *is* output of course.
>
>Using a psychic medium is not allowed.

>>> However, when I compile it using any of the -O options
(-O0, -O1, etc.) it segfaults when executed (still no
compiler warnings or errors).
I'm afraid you are responsible for diagnosing errors in your
code, not your compiler.
Deep thought.

He means semantic errors. Or does your compiler do the programming for
the programmer? :-)

<snip>
What I mean is that such sentences are not helpful to the OP.
And we should try to be less patronizing with newcomers and
people that ask questions. I am sure the OP knows that it is the
programmer that debugs the program. He was asking us to help him
to do that, not to answer him

just do it pal...
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 17 '08 #5
jacob navia wrote:
santosh wrote:
>jacob navia wrote:
>>Peter Nilsson wrote:
Jeff Taylor <dev.n...@spam.netwrote:
I have a C program that GCC compiles without warnings and
it runs just fine.
Realise that output is not the only measure of correctness.

Interesting interesting...

Can you name any other measure that doesn't rely on
output?

A program that invokes a buffer overrun, but happens to produce
expected output is surely not correct?

How the hell do you know that there is a buffer
overrun if the output is not affected?

To KNOW that there is a buffer overrun the program MUST
do something it should not do, i.e. produce an output
that is different than the expected output

If the output of the program is normal you have NO WAY
to know there is a buffer overrun.
Not if you use a memory bounds checker. You can also "know" that there
is a buffer overrun by just looking at the source.
And if you hook a debugger and see some buffer being
overrun that *is* output of course.
No. The output of the debugger is not the output of the program being
debugged.

<snip>

Mar 17 '08 #6
jacob navia <ja***@nospam.comwrites:
How the hell do you know that there is a buffer
overrun if the output is not affected?
By inspecting the source code.
--
Ben Pfaff
http://benpfaff.org
Mar 17 '08 #7
On 17 Mar, 21:40, santosh <santosh....@gmail.comwrote:
jacob navia wrote:
Peter Nilsson wrote:
Realise that output is not the only measure of correctness.
Can you name any other measure that doesn't rely on
output?

A program that invokes a buffer overrun, but happens to produce expected
output is surely not correct?
What is the definition of "correct"? A computer program
is simply a machine for computing some function on a set
of strings from some alphabet. If the output is as expected
for all possible inputs, then the program accurately models
the function, and is thus correct. However, if it contains
a programming error, it is very likely that it does not
produce correct output on all possible inputs, but it
is generally not feasible to check all possible inputs.
So, Jacob is right in the sense that the output is the
only thing that matters in an academic sense of determining
the correctness of the program, and Santosh is right in
the practical sense that a program that contains a buffer
overflow will probably generate incorrect output on some
input string. Can't we all just get along? :)
Mar 18 '08 #8
user923005 wrote:
>
There is a guy where I work who has 500K lines of code in his head
perfectly (right down to the line number).
There are islam believers that know the koran by heart.
There are christians that know the bible by heart (down to
the line number)

Human memory can be wasted in a thousand ways. Thanks for confirming
it.

Of course, I've never seen anyone else like him.
Obviously. Not everybody wants to waste effort in a stupid
thing like that guy does.

WHAT IS THE USE?

none

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 18 '08 #9
jacob navia wrote:
user923005 wrote:
>>
There is a guy where I work who has 500K lines of code in his head
perfectly (right down to the line number).

There are islam believers that know the koran by heart.
There are christians that know the bible by heart (down to
the line number)

Human memory can be wasted in a thousand ways. Thanks for confirming
it.

>Of course, I've never seen anyone else like him.

Obviously. Not everybody wants to waste effort in a stupid
thing like that guy does.

WHAT IS THE USE?

none
Er, guaranteed job? Peer admiration?
:-)

Mar 18 '08 #10
On Mar 17, 5:45 pm, jacob navia <ja...@nospam.comwrote:
santosh wrote:
jacob navia wrote:
Peter Nilsson wrote:
Jeff Taylor <dev.n...@spam.netwrote:
I have a C program that GCC compiles without warnings and
it runs just fine.
Realise that output is not the only measure of correctness.
Interesting interesting...
Can you name any other measure that doesn't rely on
output?
A program that invokes a buffer overrun, but happens to produce expected
output is surely not correct?

How the hell do you know that there is a buffer
overrun if the output is not affected?
This points out that blind testing is never sufficient to prove
correctness of a program. In the data set used in test the program
seems to run fine, but a different data set in production causes it to
crash. The bug (buffer overrun or whatever) was always there. It just
was not manifested in test. Not that using the debugger would not have
helped since the data set used in the debugging session was the test
data.

So how do you know the bug is there BEFORE getting it in production?
Code reviews are one more tool. Note: this requires READING someone
else's code. Amazing how we get back to that. The review might at
least suggest a test case that triggers the bug.
>
To KNOW that there is a buffer overrun the program MUST
do something it should not do, i.e. produce an output
that is different than the expected output
You can find these bugs by inspection o9f the code (if you know what
to look for, ie experience).
>
If the output of the program is normal you have NO WAY
to know there is a buffer overrun.
Not from the test. Not even a test with a debugger. You find this the
hard way in production, OR by inspecting the code (There's that Read
the code situation again).
>
And if you hook a debugger and see some buffer being
overrun that *is* output of course.
IF you see the overrun in the debugger.
>

Using a psychic medium is not allowed.
>> However, when I compile it using any of the -O options
(-O0, -O1, etc.) it segfaults when executed (still no
compiler warnings or errors).
I'm afraid you are responsible for diagnosing errors in your
code, not your compiler.
Deep thought.
He means semantic errors. Or does your compiler do the programming for
the programmer? :-)
<snip>

What I mean is that such sentences are not helpful to the OP.
And we should try to be less patronizing with newcomers and
people that ask questions. I am sure the OP knows that it is the
programmer that debugs the program. He was asking us to help him
to do that, not to answer him

just do it pal...
You snipped his suggestion to reduce the program to a smaller version
that still shows the bug. So stop harping on people to do what they
already are doing.

Ed
Mar 18 '08 #11
jacob navia wrote:
Peter Nilsson wrote:
>Jeff Taylor <dev.n...@spam.netwrote:
>>I have a C program that GCC compiles without warnings and
it runs just fine.

Realise that output is not the only measure of correctness.

Interesting interesting...

Can you name any other measure that doesn't rely on
output?
I've fixed many bugs that did not affect the program's
output, but were regarded as serious enough to warrant hot
fixes or rapid-response patches.

My favorite involved a piece of code that sorted some
data so it could turn an O(N*N) search task into O(N).
Unfortunately, the sort itself ran in O(N*N*logN) time ...

(Didn't use a debugger, either. A profiler told me
where the time was going, and code-reading did the rest.)

--
Er*********@sun.com
Mar 18 '08 #12
Ben Pfaff wrote:
>
.... snip ...
>
The operating system kernel in question is over 13,000 lines of C
code. Students in the course add about 5,000 lines of their own
code to it. Not what I'd consider a large program, or even
medium size, but well above the 2,000 lines that Jacob says a
person can debug without a debugger. I developed the kernel, and
the reference solutions, without using a debugger.
I just did a rough line count on two of my packages, hashlib and
nmalloc. I found both added up to about 2500 lines. Now I avoid
extra lines, and like to put multiple statements in a single line,
etc., so I suspect most writers would find those code modules to
add up to about 5000 lines each. I also think there is little
redundancy in my code. I counted the main module, the header
files, the testing files, and the accessory files (added optional
abilities).

My point is that those units have never seen a debugger. They have
seen the equivalent of printfs, and they have been debugged.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 18 '08 #13
jacob navia wrote:
user923005 wrote:
>There is a guy where I work who has 500K lines of code in his
head perfectly (right down to the line number).

There are islam believers that know the koran by heart. There
are christians that know the bible by heart (down to the line
number). Human memory can be wasted in a thousand ways. Thanks
for confirming it.
>Of course, I've never seen anyone else like him.

Obviously. Not everybody wants to waste effort in a stupid
thing like that guy does.

WHAT IS THE USE?

none
Well, for one thing, he probably doesn't need a printed listing.
At 50 lines per page, that adds up to 10K pages for a single
listing. It would appear fair economies are possible.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Mar 18 '08 #14
CBFalconer wrote:
jacob navia wrote:
>user923005 wrote:
>>There is a guy where I work who has 500K lines of code in his
head perfectly (right down to the line number).
There are islam believers that know the koran by heart. There
are christians that know the bible by heart (down to the line
number). Human memory can be wasted in a thousand ways. Thanks
for confirming it.
>>Of course, I've never seen anyone else like him.
Obviously. Not everybody wants to waste effort in a stupid
thing like that guy does.

WHAT IS THE USE?

none

Well, for one thing, he probably doesn't need a printed listing.
At 50 lines per page, that adds up to 10K pages for a single
listing. It would appear fair economies are possible.
FANTASTIC FALCONER!

At this price, your whole brain is worth only a few bucks!
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 18 '08 #15
William Pursell <bill.purs...@gmail.comwrote:
santosh <santosh....@gmail.comwrote:
jacob navia wrote:
Peter Nilsson wrote:
Realise that output is not the only measure of
correctness.
>
Can you name any other measure that doesn't rely on
output?
A program that invokes a buffer overrun, but happens
to produce expected output is surely not correct?

What is the definition of "correct"?
There are many, but here's a topical one...

A strictly conforming program shall use only those
features of the language and library specified in
this International Standard. It shall not produce
output dependent on any unspecified, undefined, or
implementation-defined behavior, and shall not
exceed any minimum implementation limit.

You'll note that it includes one non-output criteria
that Jacob was asking for.

In 2005 I posted 21 lines of code that (at the time)
crashed gcc and lcc-win32. It had no output statements,
so was highly deterministic! It was quite simply a
declaration of an array. It was syntactically and
semantically valid, but it violated minimum limits.

<snip>
but it is generally not feasible to check all possible
inputs.
Precisely. However... it _is_ generally possible to prove
program correctness without having to trial all inputs.
[Or at least write code where this is generally possible.]
So, Jacob is right in the sense that the output is the
only thing that matters in an academic sense of determining
the correctness of the program,
Ever heard of denotational semantics?

Of course, you don't have to go to that extreme, but the
fact is, there are plenty of ways to determine correctness
that don't require running the program and checking the
output.

[And yes, I've heard of the Knuth quote: "I have only
proved it correct, not tried it." ;-]
and Santosh is right in
the practical sense that a program that contains a buffer
overflow will probably generate incorrect output on some
input string.
I agree that Santosh was right, but would point out that
what he said is not the same as what you've said he
said.

--
Peter
Mar 19 '08 #16
On Mar 18, 2:27*am, jacob navia <ja...@nospam.comwrote:
user923005 wrote:
There is a guy where I work who has 500K lines of code in his head
perfectly (right down to the line number).

There are islam believers that know the koran by heart.
There are christians that know the bible by heart (down to
the line number)

Human memory can be wasted in a thousand ways. Thanks for confirming
it.
Have you ever seen someone solve a bug in ten seconds without even
looking at the code? Larry does it all the time.
Of course, I've never seen anyone else like him.

Obviously. Not everybody wants to waste effort in a stupid
thing like that guy does.
What makes you think he put effort into it? It just comes naturally
to him.
WHAT IS THE USE?

none
He's the best programmer I have ever met, and nobody else is even
close.
Mar 19 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
11
by: H.A. Sujith | last post by:
The following code is causing a segfault at the first if statement. Am I doing something wrong or is it a compiler bug? //---------- #include <stdio.h> int main(int argc, char *argv) { int...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
14
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.