[Pythonmac-SIG] Catching exceptions inside python with catch_exception_raise() doesn't work

Charlie Miller cmiller at securityevaluators.com
Mon Dec 11 23:43:06 CET 2006


Is it possible to monitor for exceptions using the Mac OS X  
catch_exception_raise function inside python?  As you may know, after  
some setup, this function gets called whenever an exception occurs.   
I have a program which works great in C but when I wrap it in python  
it fails.  This is due to the fact that it calls exc_server, which  
uses _dyld_lookup_and_bind, to try to find the location of the  
function catch_exception_raise.  When wrapped in python,  
dyld_lookup_and_bind returns 0x2030 which is NOT the correct  
address.  It then jumps there and tries executing which causes a BUS  
ERROR.

In C at least, dyld_lookup_and_bind works if the  
catch_exception_raise function is compiled into the binary or linked  
dynamically at runtime via a shared library (dylib).  However, it  
doesn't work if it is brought into the executable space via dlopen -  
which presumably is how python does it.

How can I set it up so that I can monitor applications for exceptions  
in a python script?  I could fork and exec a program with its own  
address space, but that seems ridiculous.  I could recompile the  
python interpreter and include catch_exception_raise, but that also  
seems terrible.  Or is it impossible to wrap such a program in python  
- which seems silly?

Any help is appreciated.  Thanks,

Charlie




More information about the Pythonmac-SIG mailing list