summaryrefslogtreecommitdiff
path: root/src/backend/jit/llvm/llvmjit_error.cpp
blob: ea40f5a12479c1d905fc600dd3214d2a7820e826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*-------------------------------------------------------------------------
 *
 * llvmjit_error.cpp
 *	  LLVM error related handling that requires interfacing with C++
 *
 * Unfortunately neither (re)setting the C++ new handler, nor the LLVM OOM
 * handler are exposed to C. Therefore this file wraps the necessary code.
 *
 * Copyright (c) 2016-2023, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/jit/llvm/llvmjit_error.cpp
 *
 *-------------------------------------------------------------------------
 */

extern "C"
{
#include "postgres.h"
}

#include <llvm/Support/ErrorHandling.h>

#include "jit/llvmjit.h"

#include <new>

static int fatal_new_handler_depth = 0;
static std::new_handler old_new_handler = NULL;

static void fatal_system_new_handler(void);
#if LLVM_VERSION_MAJOR > 4
static void fatal_llvm_new_handler(void *user_data, const char *reason, bool gen_crash_diag);
#if LLVM_VERSION_MAJOR < 14
static void fatal_llvm_new_handler(void *user_data, const std::string& reason, bool gen_crash_diag);
#endif
#endif
static void fatal_llvm_error_handler(void *user_data, const char *reason, bool gen_crash_diag);
#if LLVM_VERSION_MAJOR < 14
static void fatal_llvm_error_handler(void *user_data, const std::string& reason, bool gen_crash_diag);
#endif


/*
 * Enter a section in which C++ and LLVM errors are treated as FATAL errors.
 *
 * This is necessary for LLVM as LLVM's error handling for such cases
 * (exit()ing, throwing std::bad_alloc() if compiled with exceptions, abort())
 * isn't compatible with postgres error handling.  Thus in sections where LLVM
 * code, not LLVM generated functions!, is executing, standard new, LLVM OOM
 * and LLVM fatal errors (some OOM errors masquerade as those) are redirected
 * to our own error handlers.
 *
 * These error handlers use FATAL, because there's no reliable way from within
 * LLVM to throw an error that's guaranteed not to corrupt LLVM's state.
 *
 * To avoid disturbing extensions using C++ and/or LLVM, these handlers are
 * unset when not executing LLVM code. There is no need to call
 * llvm_leave_fatal_on_oom() when ERRORing out, error recovery resets the
 * handlers in that case.
 */
void
llvm_enter_fatal_on_oom(void)
{
	if (fatal_new_handler_depth == 0)
	{
		old_new_handler = std::set_new_handler(fatal_system_new_handler);
#if LLVM_VERSION_MAJOR > 4
		llvm::install_bad_alloc_error_handler(fatal_llvm_new_handler);
#endif
		llvm::install_fatal_error_handler(fatal_llvm_error_handler);
	}
	fatal_new_handler_depth++;
}

/*
 * Leave fatal error section started with llvm_enter_fatal_on_oom().
 */
void
llvm_leave_fatal_on_oom(void)
{
	fatal_new_handler_depth--;
	if (fatal_new_handler_depth == 0)
	{
		std::set_new_handler(old_new_handler);
#if LLVM_VERSION_MAJOR > 4
		llvm::remove_bad_alloc_error_handler();
#endif
		llvm::remove_fatal_error_handler();
	}
}

/*
 * Are we currently in a 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.
 */
void
llvm_reset_after_error(void)
{
	if (fatal_new_handler_depth != 0)
	{
		std::set_new_handler(old_new_handler);
#if LLVM_VERSION_MAJOR > 4
		llvm::remove_bad_alloc_error_handler();
#endif
		llvm::remove_fatal_error_handler();
	}
	fatal_new_handler_depth = 0;
}

void
llvm_assert_in_fatal_section(void)
{
	Assert(fatal_new_handler_depth > 0);
}

static void
fatal_system_new_handler(void)
{
	ereport(FATAL,
			(errcode(ERRCODE_OUT_OF_MEMORY),
			 errmsg("out of memory"),
			 errdetail("while in LLVM")));
}

#if LLVM_VERSION_MAJOR > 4
static void
fatal_llvm_new_handler(void *user_data,
					   const char *reason,
					   bool gen_crash_diag)
{
	ereport(FATAL,
			(errcode(ERRCODE_OUT_OF_MEMORY),
			 errmsg("out of memory"),
			 errdetail("While in LLVM: %s", reason)));
}
#if LLVM_VERSION_MAJOR < 14
static void
fatal_llvm_new_handler(void *user_data,
					   const std::string& reason,
					   bool gen_crash_diag)
{
	fatal_llvm_new_handler(user_data, reason.c_str(), gen_crash_diag);
}
#endif
#endif

static void
fatal_llvm_error_handler(void *user_data,
						 const char *reason,
						 bool gen_crash_diag)
{
	ereport(FATAL,
			(errcode(ERRCODE_OUT_OF_MEMORY),
			 errmsg("fatal llvm error: %s", reason)));
}

#if LLVM_VERSION_MAJOR < 14
static void
fatal_llvm_error_handler(void *user_data,
						 const std::string& reason,
						 bool gen_crash_diag)
{
	fatal_llvm_error_handler(user_data, reason.c_str(), gen_crash_diag);
}
#endif