jit: Do not try to shut down LLVM state in case of LLVM triggered errors.
authorAndres Freund <andres@anarazel.de>
Tue, 14 Sep 2021 01:07:19 +0000 (18:07 -0700)
committerAndres Freund <andres@anarazel.de>
Tue, 14 Sep 2021 01:26:15 +0000 (18:26 -0700)
If an allocation failed within LLVM it is not safe to call back into LLVM as
LLVM is not generally safe against exceptions / stack-unwinding. Thus errors
while in LLVM code are promoted to FATAL. However llvm_shutdown() did call
back into LLVM even in such cases, while llvm_release_context() was careful
not to do so.

We cannot generally skip shutting down LLVM, as that can break profiling. But
it's OK to do so if there was an error from within LLVM.

Reported-By: Jelte Fennema <Jelte.Fennema@microsoft.com>
Author: Andres Freund <andres@anarazel.de>
Author: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/AM5PR83MB0178C52CCA0A8DEA0207DC14F7FF9@AM5PR83MB0178.EURPRD83.prod.outlook.com
Backpatch: 11-, where jit was introduced

src/backend/jit/llvm/llvmjit.c
src/backend/jit/llvm/llvmjit_error.cpp
src/include/jit/llvmjit.h

index df691cbf1c539e4fc5f3034fc7303fac38e4910b..169dad96d76bf8e00fe652dc998e274dfaa58f7b 100644 (file)
@@ -172,8 +172,6 @@ llvm_release_context(JitContext *context)
 {
    LLVMJitContext *llvm_context = (LLVMJitContext *) context;
 
-   llvm_enter_fatal_on_oom();
-
    /*
     * When this backend is exiting, don't clean up LLVM. As an error might
     * have occurred from within LLVM, we do not want to risk reentering. All
@@ -182,6 +180,8 @@ llvm_release_context(JitContext *context)
    if (proc_exit_inprogress)
        return;
 
+   llvm_enter_fatal_on_oom();
+
    if (llvm_context->module)
    {
        LLVMDisposeModule(llvm_context->module);
@@ -885,6 +885,20 @@ llvm_session_initialize(void)
 static void
 llvm_shutdown(int code, Datum arg)
 {
+   /*
+    * If llvm_shutdown() is reached while in a fatal-on-oom section an error
+    * has occurred in the middle of LLVM code. It is not safe to call back
+    * into LLVM (which is why a FATAL error was thrown).
+    *
+    * We do need to shutdown LLVM in other shutdown cases, otherwise
+    * e.g. profiling data won't be written out.
+    */
+   if (llvm_in_fatal_on_oom())
+   {
+       Assert(proc_exit_inprogress);
+       return;
+   }
+
 #if LLVM_VERSION_MAJOR > 11
    {
        if (llvm_opt3_orc)
index 26bc828875ec744c0e68c63246bcbc49ea34e435..daefb3e1fd9996aa50741930cab2dc075f4971be 100644 (file)
@@ -83,6 +83,16 @@ llvm_leave_fatal_on_oom(void)
    }
 }
 
+/*
+ * Are we currently in an fatal-on-oom section? Useful to skip cleanup in case
+ * of errors.
+ */
+bool
+llvm_in_fatal_on_oom(void)
+{
+   return fatal_new_handler_depth > 0;
+}
+
 /*
  * Reset fatal error handling. This should only be called in error recovery
  * loops like PostgresMain()'s.
index a8ba5a4facd217f4cec900bd8831d1a83523b637..3560715e32921427e8e87b1bdc9c98f3ce172772 100644 (file)
@@ -84,6 +84,7 @@ extern LLVMValueRef AttributeTemplate;
 
 extern void llvm_enter_fatal_on_oom(void);
 extern void llvm_leave_fatal_on_oom(void);
+extern bool llvm_in_fatal_on_oom(void);
 extern void llvm_reset_after_error(void);
 extern void llvm_assert_in_fatal_section(void);