473,387 Members | 1,529 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,387 software developers and data experts.

Catching access violation exceptions

I understand that access violations aren't part of the standard C++
exception handling support. On Windows, a particular MSVC compiler
option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?

Regards and TIA,

Steven
Jul 19 '05 #1
15 17951
"Steven Reddie" <sm*@essemer.com.au> wrote in message
news:f9**************************@posting.google.c om...
I understand that access violations aren't part of the standard C++
exception handling support.
OK.

On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?


How could there be? Do you think all platforms even
define 'access violation'?

E.g. remember MS-DOS, where you could poke a stick
anywhere you liked? Sometimes you'd detonate a mine
with the stick, and the OS just 'went away'. No 'access
violation', no error message, nothing.

Do you think those platforms that do define 'access violation'
mean the same thing by that term?

-Mike
Jul 19 '05 #2

Steven Reddie wrote:

I understand that access violations aren't part of the standard C++
exception handling support. On Windows, a particular MSVC compiler
option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks.


Don't use catch(...). Send an email to Abrahams/Sutter/... demanding
a fix for C++ EH. They shall mandate 2-phase EH and amended exception
specs (make ES work without totally silly catch(...)), to begin with.
Things like bool expected_exception<T>() and bool unwinding(T *) can
follow as well.

regards,
alexander.
Jul 19 '05 #3
Steven Reddie wrote:
I understand that access violations aren't part of the standard C++
exception handling support.
Right.

On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.
No. This is platform dependant.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks.
You can't.

If a process hits an access violation, you have no idea (other than in
some very special circumstances) just how corrupted things are.
It would be nice to be able to automatically unregister a user-defined callback if it is found to
cause any exception including access violations.
If data structures are in an inconsistant state, you're hosed.

Does anyone know of a platform-independant method for achieving this?


No.

You could a library to do this but it's not a trivial task.

Jul 19 '05 #4
Alexander Terekhov wrote:
Steven Reddie wrote:
I understand that access violations aren't part of the standard C++
exception handling support. On Windows, a particular MSVC compiler
option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks.

Don't use catch(...). Send an email to Abrahams/Sutter/... demanding
a fix for C++ EH. They shall mandate 2-phase EH and amended exception
specs (make ES work without totally silly catch(...)), to begin with.
Things like bool expected_exception<T>() and bool unwinding(T *) can
follow as well.


How would that fix the OP problem ?

Jul 19 '05 #5
Alexander Terekhov wrote:
[SNIP]
Don't use catch(...). Send an email to Abrahams/Sutter/... demanding
a fix for C++ EH. They shall mandate 2-phase EH and amended exception
specs (make ES work without totally silly catch(...)), to begin with.
Things like bool expected_exception<T>() and bool unwinding(T *) can
follow as well.


Care to explain? No links allowed, this is a text only newsgroup. ;-) And
please *not* so briefly that noone will understand.

--
Attila aka WW
Jul 19 '05 #6

"Steven Reddie" <sm*@essemer.com.au> wrote in message news:f9**************************@posting.google.c om...
I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?


No. It can't be. The nature of hardware faults is very implementation
specific. You'll have to investigate it for each platform.

One thing you can tend to do portably is check for obvious errors like
null pointers (even places where they ought not to be possible like the
address of references or the "this" pointer in your member functions that
are called from the outside). While undefined behavior has most certainly
occurred, you can potentially limit it's impact.
Jul 19 '05 #7

Gianni Mariani wrote:

Alexander Terekhov wrote:
Steven Reddie wrote:
I understand that access violations aren't part of the standard C++
exception handling support. On Windows, a particular MSVC compiler
option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks.

Don't use catch(...). Send an email to Abrahams/Sutter/... demanding
a fix for C++ EH. They shall mandate 2-phase EH and amended exception
specs (make ES work without totally silly catch(...)), to begin with.
Things like bool expected_exception<T>() and bool unwinding(T *) can
follow as well.


How would that fix the OP problem ?


Since there will be no catch(...) ["unprotected" via fixed ES] and no
hurting unwinding (due to currently broken ES), it would cause any
*unexpected* exception end up in the std::unexpected() invoked at throw
point. By default, std::unexpected() calls abort(). I forgot to mention
that he should send an email to Sutter demanding "SEH_exception" base
class (it can even derive from std::exception, as far as I'm concerned).

regards,
alexander.
Jul 19 '05 #8
Alexander Terekhov wrote:
Gianni Mariani wrote:

....

How would that fix the OP problem ?

Since there will be no catch(...) ["unprotected" via fixed ES] and no
hurting unwinding (due to currently broken ES), it would cause any
*unexpected* exception end up in the std::unexpected() invoked at throw
point. By default, std::unexpected() calls abort(). I forgot to mention
that he should send an email to Sutter demanding "SEH_exception" base
class (it can even derive from std::exception, as far as I'm concerned).


Apart from being off-topic on both the thread AND the NG; I think
exceptions are intended by the standard to be very simplistic.
Remember, these are a replacement of the setjmp/longjmp semantics which
had all kinds of disasters if you didn't know what you were doing.

I think if your application is unable to work correctly with EH as
defined by the standard, then EH may not be the right solution for you.


Jul 19 '05 #9

Gianni Mariani wrote:
[...]
Remember, these are a replacement of the setjmp/longjmp semantics which
had all kinds of disasters if you didn't know what you were doing.
On modern systems, setjmp() kinda "injects" a handler and longjmp
simply unwinds and transfers control to it (causing the second
setjmp's return). They call it "forced unwinding". The only problem
with forced unwinding is that it doesn't work nice with... surprise,
surprise... *catch(...)*. The right approach here is to have a known
"jump" exception, of course.

I think if your application is unable to work correctly with EH as
defined by the standard, then EH may not be the right solution for you.


EH as defined by the current standard is pretty much broken and
is nothing but a compromise influenced by "rumors" that <quote>
On other systems, it is architecturally close to impossible not
to invoke the destructors while searching for a handler </quote>
(Pg. 381, TC++PL [my pdf version])

http://groups.google.com/groups?selm...72630%40web.de
(Subject: Re: std0X::expected_exception<T>())

regards,
alexander.
Jul 19 '05 #10
Mike Wahler <mk******@mkwahler.net> spoke thus:
E.g. remember MS-DOS, where you could poke a stick
anywhere you liked? Sometimes you'd detonate a mine
with the stick, and the OS just 'went away'. No 'access
violation', no error message, nothing.


I'd be interested to hear more about this phenomenon... If you'd prefer, you
can e-mail me (minus spamtrap, of course).

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #11
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bl**********@chessie.cirr.com...
Mike Wahler <mk******@mkwahler.net> spoke thus:
E.g. remember MS-DOS, where you could poke a stick
anywhere you liked? Sometimes you'd detonate a mine
with the stick, and the OS just 'went away'. No 'access
violation', no error message, nothing.
I'd be interested to hear more about this phenomenon... If you'd prefer,

you can e-mail me (minus spamtrap, of course).


'Quick-n-dirty explanation':

MDSOS is an 'unprotected' operating system, thus does
not monitor and restrict access to memory or peripheral
devices like e.g. Windows does.

You could directly access and/or modify (e.g. from
assembly, C, BASIC, or whatever language), all of
memory space (including the ubiquitous 'interrupt
vectors'), hardware registers, etc.

Sometimes 'convenient' and almost always faster than
making OS calls, but deadly if you do it wrong.

MSDOS doesn't have conditions like 'access violations'
or 'seg faults' etc. and will let you scribble around
anywhere you like inside its internals.

If you want more details, when I get time, I can email
you a more detailed description. Let me know.
-Mike
Jul 19 '05 #12
Thanks all for the responses.

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<PY*****************@newsread4.news.pas.earth link.net>...
"Steven Reddie" <sm*@essemer.com.au> wrote in message
news:f9**************************@posting.google.c om...
I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?
How could there be? Do you think all platforms even
define 'access violation'?


Mike, why is that significant? ANSI C defines a signal() function
that can catch these things. That's the job of the
compiler/libraries, to make system dependencies useable in a system
independent way.
E.g. remember MS-DOS, where you could poke a stick
anywhere you liked? Sometimes you'd detonate a mine
with the stick, and the OS just 'went away'. No 'access
violation', no error message, nothing.

Do you think those platforms that do define 'access violation'
mean the same thing by that term?


Sure, I remember such things, and know that 'access violation' may
mean different things on different platforms, alignment exceptions
will never occur on some platforms, and writing to the wrong address
may not even cause an exception if the address is valid, however such
things are no reason why a platform independent method for catching
platform dependent exceptions with a catch(...) is impossible. I'm
not asking to be able to identify the type of exception, just a way to
catch them so that I can avoid calling the function in future that
caused the exception. However, that said, it would still be possible
to define some accessviolation_exception and alignment_exception types
and throw only the ones that make sense to a particular system.

Regards,

Steven
Jul 19 '05 #13
In article <f9**************************@posting.google.com >, Steven Reddie wrote:
I understand that access violations aren't part of the standard C++
exception handling support. On Windows, a particular MSVC compiler
option enables Microsoft's Structured Exception Handling (SEH) in C++
EH so that a catch (...) will catch an access violation. I don't know
if other platforms support something similar.

I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?

[-]
No, not really. You can use whatever signal handling is available,
though signals aren't a C++ thing.

So staying off topic of this group for a little bit more IMHO the whole
idea of trying to "fix" anything during runtime here isn't such a great
idea either as a signal may be risen *after* some damage has been already
done and so from there on all bets are off.

Say should your application be some sort of flight control system please
let me leave the plane *NOW*.

Cheers,
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ EMail Private : ju*****@manannan.org \ send money instead /
Jul 19 '05 #14
Steven Reddie wrote:

Thanks all for the responses.

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<PY*****************@newsread4.news.pas.earth link.net>...
"Steven Reddie" <sm*@essemer.com.au> wrote in message
news:f9**************************@posting.google.c om...
I'm wondering about how to best protect an application or library from
poorly written user-defined callbacks. It would be nice to be able to
automatically unregister a user-defined callback if it is found to
cause any exception including access violations. Does anyone know of
a platform-independant method for achieving this?
How could there be? Do you think all platforms even
define 'access violation'?


Mike, why is that significant? ANSI C defines a signal() function
that can catch these things. That's the job of the
compiler/libraries, to make system dependencies useable in a system
independent way.


The only portable way to enter a signal handler is to call raise.
Support for asynchronous signals (such as SIGSEGV) is not required.
E.g. remember MS-DOS, where you could poke a stick
anywhere you liked? Sometimes you'd detonate a mine
with the stick, and the OS just 'went away'. No 'access
violation', no error message, nothing.

Do you think those platforms that do define 'access violation'
mean the same thing by that term?


Sure, I remember such things, and know that 'access violation' may
mean different things on different platforms, alignment exceptions
will never occur on some platforms, and writing to the wrong address
may not even cause an exception if the address is valid, however such
things are no reason why a platform independent method for catching
platform dependent exceptions with a catch(...) is impossible.


That's a good example of undefined behavior. If your implementation
supports it, use it. But with the understanding that it isn't something
you can count on.

What more do you think the language definition should say?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #15

Pete Becker wrote:
[...]
The only portable way to enter a signal handler is to call raise.
Support for asynchronous signals (such as SIGSEGV) is not required.
If SIGSEGV would really be asynchronous, there would be no point in
raising it at all.

<Forward Inline>

-------- Original Message --------
Newsgroups: gmane.comp.lib.boost.devel
Subject: Re: boost::execution_monitor impl under windows

Al************@barclayscapital.com wrote:
-----Original Message-----
From: Alexander Terekhov [mailto:te******@web.de]
Sent: 26 September 2003 18:23

SEH doesn't "violate" sequence points, AFAIK.
I *think* I've seen it happen - but it has been a while since I've been
spelunking in this technology and it may be that I've either misremembered
or jumped to an incorrect conclusion. But it seems implausible: SEH can
arise from fetch or store which are often resequenced - so this would
restrict the available optimisations.


Raising SEH exception (or signal) is clearly a *side effect* (that
isn't "covered" by the C/C++ standard... unless it comes "on top"
of accessing an object designated by a volatile lvalue or modifying
an object, "calling a library I/O function, or calling a function
that does any of those operations" aside for a moment). As such,
implementations are indeed kinda constrained in what they can do
with respect to reordering of operations that can raise SEH or
synchronous signals (unless they can prove that reordering doesn't
impact "handling")

Anyway, I've never seen any MS documentation that would support this belief.
(And a quick search of MSDN doesn't return any promising hits.) Do you have
a reference?
Nope. But I'd consider it simply "a bug" if they don't do it right.
What advantage does that bring?


Long story. Sorry. I can drop some links. Interested?


Yes.


http://www.google.com/groups?th=f98e4fa7052aa25b
(Subject: __attribute__((cleanup(function)) versus try/finally)

http://www.google.com/groups?th=c41b1edf07790c28
(Subject: Exception handling... it's time to fix the standard)

http://www.google.com/groups?th=94e63c7613727eec
(Subject: std0X::expected_exception<T>())

http://www.google.com/groups?th=236c96ebdd0891c3
(Subject: Re: std0X::expected_exception<T>() [repost])

regards,
alexander.

--
http://www.sco.com/ibmlawsuit/ibmame...nterclaims.pdf
Jul 19 '05 #16

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

Similar topics

0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
5
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
7
by: Daniel | last post by:
I want to write a method to remove the last node of the linked list. But the error "Access Violation" exists and the error point to this method. What does it means by Access Violation and how can...
0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
2
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol...
7
by: Me | last post by:
Am new to vb dotnet and although I like what I've seen sofar, I am experiencing pblms with system access violation "attempt to read write protected memory". This error happens when I open a...
6
by: SteveB | last post by:
Hello All I'm getting this chain of errors from an app. I was wondering if anyone recognised what the problem might relate to. I really need to know if they point to hardware or software. We are...
2
by: =?Utf-8?B?c29jYXRvYQ==?= | last post by:
Hi, I have a DLL in VC6, when a specific function is called it will spawns a few threads and then return. The threads stay running and inside one of these threads an event is created using the...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.