Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using exceptions to catch segmentation faults?

Reply
Thread Tools

using exceptions to catch segmentation faults?

 
 
Markus.Elfring@web.de
Guest
Posts: n/a
 
      12-27-2005
> In C++, is there a way for me to use exceptions to catch
> segmentation faults (e.g. when I access a location off
> the end of an array)?


Please consider this article.
http://en.wikipedia.org/wiki/Segmentation_fault

You get this error if any instruction in the code "attempts to access a
memory location in a way it is not allowed to". How much do you trust
your environment if it was tried to read or overwrite the value at an
unexpected address?
Is the infrastructure and mechanism for exception handling still
working after this kind of programming error was detected?
Should a direct program abort be the prefered reaction in this case so
that the error context can be analysed from a core dump file by a
debugger?
http://lambdacs.com/debugger/USENIX/...ENIX_2003.html
(Omniscient Debugging)

http://en.wikipedia.org/wiki/Buffer_overflow
How much effort do you put on a code structure with guarantees that out
of range accesses can not happen?

Regards,
Markus

 
Reply With Quote
 
 
 
 
Markus.Elfring@web.de
Guest
Posts: n/a
 
      12-27-2005
> In C++, is there a way for me to use exceptions to catch
> segmentation faults (e.g. when I access a location off
> the end of an array)?


Please consider this article.
http://en.wikipedia.org/wiki/Segmentation_fault

You get this error if an instruction in the code "attempts to access a
memory location in a way it is not allowed to". How much do you trust
your environment if it was tried to read or overwrite the value at an
unexpected address?
Is the infrastructure and mechanism for exception handling still
working after this kind of programming error was detected?
Should a direct program abort be the prefered reaction in this case so
that the error context can be analysed from a core dump file by a
debugger?
http://lambdacs.com/debugger/USENIX/...ENIX_2003.html
(Omniscient Debugging)

Can your program safely continue its execution if an
"access_violation_exception" will be caught?
Can any further damage from unexpected or undefined behaviour be
restricted?

How much effort do you put on a code structure with guarantees that out
of range accesses can not happen?
- http://en.wikipedia.org/wiki/Static_code_analysis
- http://en.wikipedia.org/wiki/Buffer_overflow

Regards,
Markus

 
Reply With Quote
 
 
 
 
Digital Puer
Guest
Posts: n/a
 
      12-27-2005

Ron Natalie wrote:
> Digital Puer wrote:
> > Hi, I'm coming over from Java to C++, so please bear with me.
> > In C++, is there a way for me to use exceptions to catch
> > segmentation faults (e.g. when I access a location off
> > the end of an array)?
> >
> > Thanks.
> >

> I have no clue what you mean by your allusion to Java. Segementation
> faults are a foreign concept to Java as well. There's no portable
> way of catching them in C++ (and in many implementations there's no
> good way to catch them at all). There's no indication that accessing
> off the end of an array generates a segmentation fault either. Chances
> are you just access/clobber some other piece of memory.



In Java, accessing an array beyond its allocated length generates
an exception. Since on my system (linux w/g++) accessing an
array beyond its allocation generally results in a seg fault, I am
trying to catch it with an exception handler to tell me where
the fault occurred.

 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      12-27-2005

Maxim Yegorushkin wrote:
> Digital Puer wrote:
> > Hi, I'm coming over from Java to C++, so please bear with me.
> > In C++, is there a way for me to use exceptions to catch
> > segmentation faults (e.g. when I access a location off
> > the end of an array)?

>
> It useless. After a segmentation fault, at least on unix, the behavior
> of the process is undefined. You can not reliably handle or recover
> from it. I see no reason why this wouldn't apply for windoze.
>
> http://www.opengroup.org/onlinepubs/...chap02_04.html
> ...
> The behavior of a process is undefined after it ignores a SIGFPE,
> SIGILL, SIGSEGV, or SIGBUS signal that was not generated by kill(),
> sigqueue(), or raise().
> ...



I'm not trying to recover from the seg fault. I just want to find out
where in the code it happened.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      12-27-2005
Digital Puer wrote:
>>> Hi, I'm coming over from Java to C++, so please bear with me.
>>> In C++, is there a way for me to use exceptions to catch
>>> segmentation faults


No

> I'm not trying to recover from the seg fault. I just want to find out
> where in the code it happened.


Even if it were possible, it would still not tell you where in the code
the error happened. When you catch an exception, all you know
is that it occurred somewhere inside the corresponding try-block.
So unless you were planning to wrap EVERY statement in a
try..catch block, you're out of luck.

It sounds like you are looking for a program that will tell you
when you access memory that you shouldn't. This is normally
called "a debugger" and there are lots of those available (valgrind
is one that springs to mind).

 
Reply With Quote
 
Tommi =?UTF-8?B?TcOka2l0YWxv?=
Guest
Posts: n/a
 
      12-27-2005
Digital Puer wrote:

>
> Ron Natalie wrote:
>> Digital Puer wrote:
>> > Hi, I'm coming over from Java to C++, so please bear with me.
>> > In C++, is there a way for me to use exceptions to catch
>> > segmentation faults (e.g. when I access a location off
>> > the end of an array)?
>> >
>> > Thanks.
>> >

>> I have no clue what you mean by your allusion to Java. Segementation
>> faults are a foreign concept to Java as well. There's no portable
>> way of catching them in C++ (and in many implementations there's no
>> good way to catch them at all). There's no indication that accessing
>> off the end of an array generates a segmentation fault either. Chances
>> are you just access/clobber some other piece of memory.

>
>
> In Java, accessing an array beyond its allocated length generates
> an exception. Since on my system (linux w/g++) accessing an
> array beyond its allocation generally results in a seg fault, I am
> trying to catch it with an exception handler to tell me where
> the fault occurred.

Enable core-files and examine them with a debugger.

To enable core-files enter "ulimit -c unlimited". When your program
segfaults it generates a file named core or core.$PID. Start gdb with 2
arguments: the first is your program and the second is the core-file. In
gdb enter "where" and you get a nice stacktrace. It will be even nicer if
you compile your program with -g (and don't use -O or -O2 or something).

Tommi
 
Reply With Quote
 
Markus.Elfring@web.de
Guest
Posts: n/a
 
      12-28-2005
> The details of structured exception handling are described in Jeffrey
> Richter's book, Programming Applications for Microsoft Windows.


See also:
Article "A Crash Course on the Depths of Win32 Structured Exception
Handling"
by Matt Pietrek
Microsoft Systems Journal (January 1997)
http://www.microsoft.com/msj/0197/ex...exception.aspx

Regards,
Markus

 
Reply With Quote
 
Markus.Elfring@web.de
Guest
Posts: n/a
 
      12-28-2005
> In Java, accessing an array beyond its allocated length generates
> an exception. Since on my system (linux w/g++) accessing an
> array beyond its allocation generally results in a seg fault, I am
> trying to catch it with an exception handler to tell me where
> the fault occurred.


I guess that the compiler should be corrected. Would you like to create
a bug report?
http://gcc.gnu.org/bugzilla/
http://en.wikipedia.org/wiki/GCJ

Do you find it under the known errors altready?

Regards,
Markus

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-28-2005
Digital Puer wrote:
> Ron Natalie wrote:
>> Digital Puer wrote:
>>> Hi, I'm coming over from Java to C++, so please bear with me.
>>> In C++, is there a way for me to use exceptions to catch
>>> segmentation faults (e.g. when I access a location off
>>> the end of an array)?
>>>
>>> Thanks.
>>>

>> I have no clue what you mean by your allusion to Java. Segementation
>> faults are a foreign concept to Java as well. There's no portable
>> way of catching them in C++ (and in many implementations there's no
>> good way to catch them at all). There's no indication that accessing
>> off the end of an array generates a segmentation fault either. Chances
>> are you just access/clobber some other piece of memory.

>
>
> In Java, accessing an array beyond its allocated length generates
> an exception.


But not a segmentation fault.

>Since on my system (linux w/g++) accessing an
> array beyond its allocation generally results in a seg fault,


Sorry, but that's not true. What causes a segmentation fault is
accessing a piece of memory that's restricted (either not mapped,
or mapped for read only and you are trying to write it). Since
memory is allocated in chunks, and array allocations are interleaved
with other memory objects, the chances are unless you have a totally
wild subscript (much larger than the end), you won't get a segmentation
fault directly from the access.

As others pointed out C (and hence by inheritance C++) does no range
checking on it's arrays. You can use a different object in C++
(such as vector) to provide that. There are also packages out there
that instrument C++ in certain ways to do the check (at a substantial
performance penalty) to do these checks.

Your initial premise is wrong (that an array index error necessarily
generates a segmentation fault in C++) and there is no way to detect
either one.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I catch segmentation fault in python? justin Python 7 11-17-2010 04:00 PM
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
How do I catch unhandled exceptions Alan Silver ASP .Net 5 03-23-2006 08:51 PM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM



Advertisments