summaryrefslogtreecommitdiff
path: root/src/backend/jit/llvm
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2021-09-13 18:07:19 -0700
committerAndres Freund <andres@anarazel.de>2021-09-13 18:26:18 -0700
commit43849b65f3338acad50d9bdf607d2d573f8079c6 (patch)
tree8810af58438eeb45f62c428c81d830a68dbe6794 /src/backend/jit/llvm
parentb1de90699e73a081ac450420b7d4f3fd1d49981b (diff)
downloadpostgresql-43849b65f3338acad50d9bdf607d2d573f8079c6.tar.gz
jit: Do not try to shut down LLVM state in case of LLVM triggered errors.
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
Diffstat (limited to 'src/backend/jit/llvm')
-rw-r--r--src/backend/jit/llvm/llvmjit.c18
-rw-r--r--src/backend/jit/llvm/llvmjit_error.cpp10
2 files changed, 26 insertions, 2 deletions
diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 638574f480..120f5239c9 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -184,8 +184,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
@@ -194,6 +192,8 @@ llvm_release_context(JitContext *context)
if (proc_exit_inprogress)
return;
+ llvm_enter_fatal_on_oom();
+
if (llvm_context->module)
{
LLVMDisposeModule(llvm_context->module);
@@ -850,6 +850,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)
diff --git a/src/backend/jit/llvm/llvmjit_error.cpp b/src/backend/jit/llvm/llvmjit_error.cpp
index 9c6e8026e7..adfca4a708 100644
--- a/src/backend/jit/llvm/llvmjit_error.cpp
+++ b/src/backend/jit/llvm/llvmjit_error.cpp
@@ -84,6 +84,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.
*/