Exception not caught on -O2

100 views Asked by At

I am currently adding some functions to the OpenTTD NoAI API. However, I have a problem which seems to originate from the OpenTTD code. The problem is that the code works on all systems I have tested it on, except for one.

In the ScriptObject::DoCommand method an instance of the Script_Suspend class is thrown. This should be caught in the ScriptInstance::GameLoop method. On one system, when the program is compiled with -O2 or -O3, the exception is not caught and the program crashes. It works fine on -O0 and -O1.

To debug the program I printed the stack trace at multiple points using the backtrace and backtrace_symbols functions:

Stack traces -O0

In Script_Instance::GameLoop
./openttd-build/openttd(_ZN14ScriptInstance8GameLoopEv+0x156) [0x75f3ae]
./openttd-build/openttd(_ZN2AI8GameLoopEv+0xef) [0x52c455]
./openttd-build/openttd(_Z13StateGameLoopv+0xc26) [0x6a93a3]
./openttd-build/openttd(_Z8GameLoopv+0x7a) [0x6a967c]
./openttd-build/openttd(_ZN16VideoDriver_Null8MainLoopEv+0x19) [0x80ea6d]
./openttd-build/openttd(_Z12openttd_mainiPPc+0x145b) [0x6a7ff2]
./openttd-build/openttd(main+0x3a) [0x6bcebe]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x3f0801d8a4]
./openttd-build/openttd [0x4f39f9]
In ScriptObject::DoCommand:
./openttd-build/openttd(_ZN12ScriptObject9DoCommandEjjjjPKcPFvP14ScriptInstanceE+0x23a) [0x74b420]
./openttd-build/openttd(_ZN13ScriptCompany7SetNameEP4Text+0x81) [0x738d8d]
./openttd-build/openttd(_ZN9SQConvert19DefSQStaticCallbackI13ScriptCompanyPFbP4TextEEExP4SQVM+0x185) [0x5525d5]
./openttd-build/openttd(_ZN4SQVM10CallNativeEP15SQNativeClosurexxR11SQObjectPtrRb+0x334) [0x528fc4]
./openttd-build/openttd(_ZN4SQVM7ExecuteER11SQObjectPtrxxxS1_yNS_13ExecutionTypeE+0x6dc) [0x52348c]
./openttd-build/openttd(_ZN4SQVM4CallER11SQObjectPtrxxS1_yy+0xbc) [0x525992]
./openttd-build/openttd(_Z7sq_callP4SQVMxyyi+0x99) [0x4f88c1]
./openttd-build/openttd(_ZN8Squirrel10CallMethodE11tagSQObjectPKcPS0_i+0xf6) [0x7615c0]
./openttd-build/openttd(_ZN14ScriptInstance8GameLoopEv+0x326) [0x75f57e]
./openttd-build/openttd(_ZN2AI8GameLoopEv+0xef) [0x52c455]
./openttd-build/openttd(_Z13StateGameLoopv+0xc26) [0x6a93a3]
./openttd-build/openttd(_Z8GameLoopv+0x7a) [0x6a967c]
./openttd-build/openttd(_ZN16VideoDriver_Null8MainLoopEv+0x19) [0x80ea6d]
./openttd-build/openttd(_Z12openttd_mainiPPc+0x145b) [0x6a7ff2]
./openttd-build/openttd(main+0x3a) [0x6bcebe]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x3f0801d8a4]
./openttd-build/openttd [0x4f39f9]
In Script_Suspend Constructor:
./openttd-build/openttd(_ZN14Script_SuspendC2EiPFvP14ScriptInstanceE+0x8f) [0x73a81b]
./openttd-build/openttd(_ZN12ScriptObject9DoCommandEjjjjPKcPFvP14ScriptInstanceE+0x290) [0x74b476]
./openttd-build/openttd(_ZN13ScriptCompany7SetNameEP4Text+0x81) [0x738d8d]
./openttd-build/openttd(_ZN9SQConvert19DefSQStaticCallbackI13ScriptCompanyPFbP4TextEEExP4SQVM+0x185) [0x5525d5]
./openttd-build/openttd(_ZN4SQVM10CallNativeEP15SQNativeClosurexxR11SQObjectPtrRb+0x334) [0x528fc4]
./openttd-build/openttd(_ZN4SQVM7ExecuteER11SQObjectPtrxxxS1_yNS_13ExecutionTypeE+0x6dc) [0x52348c]
./openttd-build/openttd(_ZN4SQVM4CallER11SQObjectPtrxxS1_yy+0xbc) [0x525992]
./openttd-build/openttd(_Z7sq_callP4SQVMxyyi+0x99) [0x4f88c1]
./openttd-build/openttd(_ZN8Squirrel10CallMethodE11tagSQObjectPKcPS0_i+0xf6) [0x7615c0]
./openttd-build/openttd(_ZN14ScriptInstance8GameLoopEv+0x326) [0x75f57e]
./openttd-build/openttd(_ZN2AI8GameLoopEv+0xef) [0x52c455]
./openttd-build/openttd(_Z13StateGameLoopv+0xc26) [0x6a93a3]
./openttd-build/openttd(_Z8GameLoopv+0x7a) [0x6a967c]
./openttd-build/openttd(_ZN16VideoDriver_Null8MainLoopEv+0x19) [0x80ea6d]
./openttd-build/openttd(_Z12openttd_mainiPPc+0x145b) [0x6a7ff2]
./openttd-build/openttd(main+0x3a) [0x6bcebe]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x3f0801d8a4]
./openttd-build/openttd [0x4f39f9]

Stack traces -O2

In Script_Instance::GameLoop
./openttd-build/openttd(_ZN14ScriptInstance8GameLoopEv+0x19f) [0x7584bf]
In SciptObject::DoCommand:
./openttd-build/openttd(_ZN12ScriptObject9DoCommandEjjjjPKcPFvP14ScriptInstanceE+0x26d) [0x74660d]
./openttd-build/openttd [0x116e700]
In Script_Suspend:
./openttd-build/openttd(_ZN14Script_SuspendC2EiPFvP14ScriptInstanceE+0x7e) [0x735aee]
./openttd-build/openttd(_ZN12ScriptObject9DoCommandEjjjjPKcPFvP14ScriptInstanceE+0x2c0) [0x746660]
./openttd-build/openttd [0x116e700]

I noticed the stack trace in -O2 is a lot shorter. Could this be some kind of corrupted call stack and if so, what could it be caused by?

The compiler used is GCC 4.7 on CentOS 5.11.

0

There are 0 answers