forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfail_nat.c
303 lines (251 loc) · 8.14 KB
/
fail_nat.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Raising exceptions from C. */
#include <stdio.h>
#include <signal.h>
#include "caml/alloc.h"
#include "caml/callback.h"
#include "caml/fail.h"
#include "caml/fiber.h"
#include "caml/io.h"
#include "caml/gc.h"
#include "caml/memory.h"
#include "caml/mlvalues.h"
#include "caml/printexc.h"
#include "caml/signals.h"
#include "caml/stack.h"
#include "caml/roots.h"
#include "caml/callback.h"
#include "caml/signals.h"
#include "caml/tsan.h"
/* The globals holding predefined exceptions */
typedef value caml_generated_constant[1];
extern caml_generated_constant
caml_exn_Out_of_memory,
caml_exn_Sys_error,
caml_exn_Failure,
caml_exn_Invalid_argument,
caml_exn_End_of_file,
caml_exn_Division_by_zero,
caml_exn_Not_found,
caml_exn_Match_failure,
caml_exn_Sys_blocked_io,
caml_exn_Stack_overflow,
caml_exn_Assert_failure,
caml_exn_Undefined_recursive_module;
/* Exception raising */
CAMLnoret extern
void caml_raise_exception (caml_domain_state* state, value bucket);
CAMLnoreturn_start
extern void caml_raise_async_exception (caml_domain_state* state, value bucket)
CAMLnoreturn_end;
static void unwind_local_roots(char *limit_of_current_c_stack_chunk)
{
while (Caml_state->local_roots != NULL &&
(char *)Caml_state->local_roots < limit_of_current_c_stack_chunk)
{
Caml_state->local_roots = Caml_state->local_roots->next;
}
}
void caml_raise(value v)
{
Caml_check_caml_state();
char* limit_of_current_c_stack_chunk;
CAMLassert(!Is_exception_result(v));
caml_channel_cleanup_on_raise();
/* Run callbacks here, so that a signal handler that arrived during
a blocking call has a chance to interrupt the raising of EINTR */
v = caml_process_pending_actions_with_root(v);
limit_of_current_c_stack_chunk = (char*)Caml_state->c_stack;
if (limit_of_current_c_stack_chunk == NULL) {
caml_terminate_signals();
caml_fatal_uncaught_exception(v);
}
unwind_local_roots(limit_of_current_c_stack_chunk);
#if defined(WITH_THREAD_SANITIZER)
caml_tsan_exit_on_raise_c(exception_pointer);
#endif
caml_raise_exception(Caml_state, v);
}
/* Used by the stack overflow handler -> deactivate ASAN (see
segv_handler in signals_nat.c). */
CAMLno_asan void caml_raise_async(value v)
{
Caml_check_caml_state();
char* limit_of_current_c_stack_chunk;
caml_channel_cleanup_on_raise();
CAMLassert(!Is_exception_result(v));
if (Stack_parent(Caml_state->current_stack) != NULL) {
/* CR ocaml 5 effects: implement async exns + effects */
caml_fatal_error("Effects not supported in conjunction with async exns");
}
/* Do not run callbacks here: we are already raising an async exn,
so no need to check for another one, and avoiding polling here
removes the risk of recursion in caml_raise */
limit_of_current_c_stack_chunk = (char*)Caml_state->c_stack;
if (limit_of_current_c_stack_chunk == NULL) {
caml_terminate_signals();
caml_fatal_uncaught_exception(v);
}
unwind_local_roots(limit_of_current_c_stack_chunk);
Caml_state->exn_handler = Caml_state->async_exn_handler;
Caml_state->raising_async_exn = 1;
caml_raise_exception(Caml_state, v);
}
CAMLno_asan
void caml_raise_constant(value tag)
{
caml_raise(tag);
}
void caml_raise_with_arg(value tag, value arg)
{
CAMLparam2 (tag, arg);
CAMLlocal1 (bucket);
bucket = caml_alloc_small (2, 0);
Field(bucket, 0) = tag;
Field(bucket, 1) = arg;
caml_raise(bucket);
CAMLnoreturn;
}
void caml_raise_with_args(value tag, int nargs, value args[])
{
CAMLparam1 (tag);
CAMLxparamN (args, nargs);
value bucket;
int i;
bucket = caml_alloc (1 + nargs, 0);
Field(bucket, 0) = tag;
for (i = 0; i < nargs; i++) Field(bucket, 1 + i) = args[i];
caml_raise(bucket);
CAMLnoreturn;
}
void caml_raise_with_string(value tag, char const *msg)
{
CAMLparam1(tag);
value v_msg = caml_copy_string(msg);
caml_raise_with_arg(tag, v_msg);
CAMLnoreturn;
}
void caml_failwith (char const *msg)
{
caml_raise_with_string((value) caml_exn_Failure, msg);
}
void caml_failwith_value (value msg)
{
caml_raise_with_arg((value) caml_exn_Failure, msg);
}
void caml_invalid_argument (char const *msg)
{
caml_raise_with_string((value) caml_exn_Invalid_argument, msg);
}
void caml_invalid_argument_value (value msg)
{
caml_raise_with_arg((value) caml_exn_Invalid_argument, msg);
}
void caml_raise_out_of_memory(void)
{
/* Note that this is not an async exn. */
caml_raise_constant((value) caml_exn_Out_of_memory);
}
/* Used by the stack overflow handler -> deactivate ASAN (see
segv_handler in signals_nat.c). */
CAMLno_asan
void caml_raise_stack_overflow(void)
{
caml_raise_async((value) caml_exn_Stack_overflow);
}
void caml_raise_sys_error(value msg)
{
caml_raise_with_arg((value) caml_exn_Sys_error, msg);
}
void caml_raise_end_of_file(void)
{
caml_raise_constant((value) caml_exn_End_of_file);
}
void caml_raise_zero_divide(void)
{
caml_raise_constant((value) caml_exn_Division_by_zero);
}
void caml_raise_not_found(void)
{
caml_raise_constant((value) caml_exn_Not_found);
}
void caml_raise_sys_blocked_io(void)
{
caml_raise_constant((value) caml_exn_Sys_blocked_io);
}
/* We use a pre-allocated exception because we can't
do a GC before the exception is raised (lack of stack descriptors
for the ccall to [caml_array_bound_error]). */
static value array_bound_exn(void)
{
static atomic_uintnat exn_cache = ATOMIC_UINTNAT_INIT(0);
const value* exn = (const value*)atomic_load_acquire(&exn_cache);
if (!exn) {
exn = caml_named_value("Pervasives.array_bound_error");
if (!exn) {
fprintf(stderr, "Fatal error: exception "
"Invalid_argument(\"index out of bounds\")\n");
exit(2);
}
atomic_store_release(&exn_cache, (uintnat)exn);
}
return *exn;
}
void caml_array_bound_error(void)
{
caml_raise(array_bound_exn());
}
void caml_array_bound_error_asm(void)
{
#if defined(WITH_THREAD_SANITIZER)
char* exception_pointer = (char*)Caml_state->c_stack;
caml_tsan_exit_on_raise_c(exception_pointer);
#endif
/* This exception is raised directly from ocamlopt-compiled OCaml,
not C, so we jump directly to the OCaml handler (and avoid GC) */
caml_raise_exception(Caml_state, array_bound_exn());
}
static value array_align_exn(void)
{
static atomic_uintnat exn_cache = ATOMIC_UINTNAT_INIT(0);
const value* exn = (const value*)atomic_load_acquire(&exn_cache);
if (!exn) {
exn = caml_named_value("Pervasives.array_align_error");
if (!exn) {
fprintf(stderr, "Fatal error: exception "
"Invalid_argument(\"address was misaligned\")\n");
exit(2);
}
atomic_store_release(&exn_cache, (uintnat)exn);
}
return *exn;
}
void caml_array_align_error(void)
{
caml_raise(array_align_exn());
}
void caml_array_align_error_asm(void)
{
/* This exception is raised directly from ocamlopt-compiled OCaml,
not C, so we jump directly to the OCaml handler (and avoid GC) */
caml_raise_exception(Caml_state, array_align_exn());
}
int caml_is_special_exception(value exn) {
return exn == (value) caml_exn_Match_failure
|| exn == (value) caml_exn_Assert_failure
|| exn == (value) caml_exn_Undefined_recursive_module;
}