Fix exception handling not working reliably on x86/x86-64 Linux (Part 1)
authorSimon Hausmann <simon.hausmann@digia.com>
Sun, 12 May 2013 10:07:15 +0000 (12:07 +0200)
committerLars Knoll <lars.knoll@digia.com>
Wed, 15 May 2013 21:30:42 +0000 (23:30 +0200)
The registration of the unwind tables is done through the interposition
of the _Unwind_Find_FDE symbol from libgcc. Unfortunately that interposition
breaks when libgcc happens to come first in the linker scope. As it turns
out, the order is not for us to control, therefore the interposition may not
always work and our JIT generated functions may not get their unwind information
found at exception throwing time. That results in the program aborting with
an uncaught exception.

The proposed solution of replacing the interposition approach is two-fold:

(1) Go back to calling __register_frame explicitly, but only for functions that
exception _may_ pass through. In addition the performance of scalability of the
objects registered with __register_frame is a known issue upstream, as the LLVM
JIT also triggers the issue. It is being tracked at
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56460

(2) Instead of registering one FDE per function, we can theoretically register
one FDE for _all_ JIT generate functions, because they all use the same stack
layout that has exactly the same unwinding requirements from any function call
site. Since we can't guarantee the presence of all JIT generated code within
the same contiguous memory area, we can at least do it per executable memory
allocation chunk (page size). One chunk can contain many functions.

This patch implements part (1) by delaying the registration of the FDE to right
before the exception gets thrown and then walks up the context chain. The test
cases that were previously slowed down before commit
b83696f9896401dd5e99eed35e36628710e44eeb are not affected because the many
temporary "eval" functions are never subject to exceptions.

Change-Id: I7cb8262e3e1f27d6de71a1e42be8a538cba6513f
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/qml/v4/qv4runtime.cpp
src/qml/qml/v4/qv4unwindhelper.cpp
src/qml/qml/v4/qv4unwindhelper_p-arm.h
src/qml/qml/v4/qv4unwindhelper_p-dw2.h
src/qml/qml/v4/qv4unwindhelper_p.h

index 4d66312..f0df925 100644 (file)
@@ -49,6 +49,7 @@
 #include "qv4stringobject_p.h"
 #include "qv4lookup_p.h"
 #include "qv4function_p.h"
+#include "qv4unwindhelper_p.h"
 #include "private/qlocale_tools_p.h"
 
 #include <QtCore/qmath.h>
@@ -951,6 +952,19 @@ void __qmljs_throw(ExecutionContext *context, const Value &value)
     if (context->engine->debugger)
         context->engine->debugger->aboutToThrow(value);
 
+    for (ExecutionContext *ctx = context; ctx; ctx = ctx->parent) {
+        if (CallContext *callCtx = ctx->asCallContext())
+            if (FunctionObject *fobj = callCtx->function)
+                if (Function *fun = fobj->function)
+                    UnwindHelper::ensureUnwindInfo(fun);
+        for (ExecutionContext::EvalCode *code = ctx->currentEvalCode;
+             code; code = code->next)
+            UnwindHelper::ensureUnwindInfo(code->function);
+    }
+
+    if (context->engine->globalCode)
+        UnwindHelper::ensureUnwindInfo(context->engine->globalCode);
+
 #if USE(LIBUNWIND_DEBUG)
     printf("about to throw exception. walking stack first with libunwind:\n");
     unw_cursor_t cursor; unw_context_t uc;
index 0476eb3..90750e9 100644 (file)
@@ -29,6 +29,7 @@
 
 #ifdef USE_NULL_HELPER
 using namespace QV4;
+void UnwindHelper::ensureUnwindInfo(Function *function) {Q_UNUSED(function);}
 void UnwindHelper::registerFunction(Function *function) {Q_UNUSED(function);}
 void UnwindHelper::registerFunctions(QVector<Function *> functions) {Q_UNUSED(functions);}
 void UnwindHelper::deregisterFunction(Function *function) {Q_UNUSED(function);}
index 69d02a2..92e38f7 100644 (file)
@@ -116,6 +116,11 @@ void UnwindHelper::registerFunctions(QVector<Function *> functions)
         allFunctions.insert(reinterpret_cast<quintptr>(f->code), f);
 }
 
+void UnwindHelper::ensureUnwindInfo(Function *function)
+{
+    Q_UNUSED(function);
+}
+
 int UnwindHelper::unwindInfoSize()
 {
     return 2 * sizeof(unsigned int) // 2 extbl entries
index 9760631..841636d 100644 (file)
@@ -81,8 +81,13 @@ void writeIntPtrValue(unsigned char *addr, intptr_t val)
 }
 } // anonymous namespace
 
-static void ensureUnwindInfo(Function *f)
+extern "C" void __register_frame(void *fde);
+extern "C" void __deregister_frame(void *fde);
+
+void UnwindHelper::ensureUnwindInfo(Function *f)
 {
+    if (!f->codeRef)
+        return; // Not a JIT generated function
     if (!f->unwindInfo.isEmpty())
         return;
     QByteArray info;
@@ -97,29 +102,22 @@ static void ensureUnwindInfo(Function *f)
     writeIntPtrValue(cie_and_fde + address_range_offset, f->codeSize);
 
     f->unwindInfo = info;
+    __register_frame(f->unwindInfo.data() + fde_offset);
 }
 
-#if defined(Q_OS_DARWIN)
-extern "C" void __register_frame(void *fde);
-extern "C" void __deregister_frame(void *fde);
-#endif
-
 static void registerFunctionUnlocked(Function *f)
 {
-    allFunctions.insert(reinterpret_cast<quintptr>(f->code), f);
-#if defined(Q_OS_DARWIN)
-    ensureUnwindInfo(f);
-    __register_frame(f->unwindInfo.data() + fde_offset);
-#endif
+    quintptr addr = reinterpret_cast<quintptr>(f->code);
+    if (allFunctions.contains(addr))
+        return;
+    allFunctions.insert(addr, f);
 }
 
 static void deregisterFunctionUnlocked(Function *f)
 {
     allFunctions.remove(reinterpret_cast<quintptr>(f->code));
-#if defined(Q_OS_DARWIN)
     if (!f->unwindInfo.isEmpty())
         __deregister_frame(f->unwindInfo.data() + fde_offset);
-#endif
 }
 
 void UnwindHelper::registerFunction(Function *function)
@@ -150,39 +148,4 @@ void UnwindHelper::deregisterFunctions(QVector<Function *> functions)
 
 }
 
-#if defined(Q_OS_LINUX)
-extern "C" {
-
-struct bases
-{
-    void *tbase;
-    void *dbase;
-    void *func;
-};
-
-Q_QML_EXPORT void *_Unwind_Find_FDE(void *pc, struct bases *bases)
-{
-    typedef void *(*Old_Unwind_Find_FDE)(void *pc, struct bases *bases);
-    static Old_Unwind_Find_FDE oldFunction = 0;
-    if (!oldFunction)
-        oldFunction = (Old_Unwind_Find_FDE)dlsym(RTLD_NEXT, "_Unwind_Find_FDE");
-
-    {
-        QMutexLocker locker(&QV4::functionProtector);
-        QV4::Function *function = QV4::lookupFunction(pc);
-        if (function) {
-            bases->tbase = 0;
-            bases->dbase = 0;
-            bases->func = reinterpret_cast<void*>(function->code);
-            QV4::ensureUnwindInfo(function);
-            return function->unwindInfo.data() + QV4::fde_offset;
-        }
-    }
-
-    return oldFunction(pc, bases);
-}
-
-}
-#endif
-
 #endif // QV4UNWINDHELPER_PDW2_H
index 04c8c0a..72bf56c 100644 (file)
@@ -10,6 +10,7 @@ struct Function;
 class UnwindHelper
 {
 public:
+    static void ensureUnwindInfo(Function *function);
     static void registerFunction(Function *function);
     static void registerFunctions(QVector<Function *> functions);
     static void deregisterFunction(Function *function);