Skip to content

Commit fbeafb9

Browse files
authored
flambda-backend: Asynchronous exceptions for 5.x runtime (#2007)
1 parent ee9570b commit fbeafb9

30 files changed

+485
-125
lines changed

otherlibs/systhreads/st_stubs.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ struct caml_thread_struct {
8989
value * gc_regs; /* saved value of Caml_state->gc_regs */
9090
value * gc_regs_buckets; /* saved value of Caml_state->gc_regs_buckets */
9191
void * exn_handler; /* saved value of Caml_state->exn_handler */
92-
92+
char * async_exn_handler; /* saved value of Caml_state->async_exn_handler */
9393
#ifndef NATIVE_CODE
9494
intnat trap_sp_off; /* saved value of Caml_state->trap_sp_off */
9595
intnat trap_barrier_off; /* saved value of Caml_state->trap_barrier_off */
9696
struct caml_exception_context* external_raise;
9797
/* saved value of Caml_state->external_raise */
98+
struct caml_exception_context* external_raise_async;
99+
/* saved value of Caml_state->external_raise_async */
98100
#endif
99101

100102
#ifdef POSIX_SIGNALS
@@ -199,6 +201,7 @@ static void save_runtime_state(void)
199201
this_thread->gc_regs = Caml_state->gc_regs;
200202
this_thread->gc_regs_buckets = Caml_state->gc_regs_buckets;
201203
this_thread->exn_handler = Caml_state->exn_handler;
204+
this_thread->async_exn_handler = Caml_state->async_exn_handler;
202205
this_thread->local_roots = Caml_state->local_roots;
203206
this_thread->local_arenas = caml_get_local_arenas(Caml_state);
204207
this_thread->backtrace_pos = Caml_state->backtrace_pos;
@@ -208,6 +211,7 @@ static void save_runtime_state(void)
208211
this_thread->trap_sp_off = Caml_state->trap_sp_off;
209212
this_thread->trap_barrier_off = Caml_state->trap_barrier_off;
210213
this_thread->external_raise = Caml_state->external_raise;
214+
this_thread->external_raise_async = Caml_state->external_raise_async;
211215
#endif
212216
}
213217

@@ -220,6 +224,7 @@ static void restore_runtime_state(caml_thread_t th)
220224
Caml_state->gc_regs = th->gc_regs;
221225
Caml_state->gc_regs_buckets = th->gc_regs_buckets;
222226
Caml_state->exn_handler = th->exn_handler;
227+
Caml_state->async_exn_handler = th->async_exn_handler;
223228
Caml_state->local_roots = th->local_roots;
224229
caml_set_local_arenas(Caml_state, th->local_arenas);
225230
Caml_state->backtrace_pos = th->backtrace_pos;
@@ -229,6 +234,7 @@ static void restore_runtime_state(caml_thread_t th)
229234
Caml_state->trap_sp_off = th->trap_sp_off;
230235
Caml_state->trap_barrier_off = th->trap_barrier_off;
231236
Caml_state->external_raise = th->external_raise;
237+
Caml_state->external_raise_async = th->external_raise_async;
232238
#endif
233239
}
234240

@@ -296,11 +302,13 @@ static caml_thread_t caml_thread_new_info(void)
296302
th->gc_regs = NULL;
297303
th->gc_regs_buckets = NULL;
298304
th->exn_handler = NULL;
305+
th->async_exn_handler = NULL;
299306

300307
#ifndef NATIVE_CODE
301308
th->trap_sp_off = 1;
302309
th->trap_barrier_off = 2;
303310
th->external_raise = NULL;
311+
th->external_raise_async = NULL;
304312
#endif
305313

306314
return th;
@@ -321,6 +329,8 @@ void caml_thread_free_info(caml_thread_t th)
321329
use in this variable nor on the stack)
322330
exn_handler: stack-allocated
323331
external_raise: stack-allocated
332+
async_exn_handler: stack-allocated
333+
external_raise_async: stack-allocated
324334
init_mask: stack-allocated
325335
*/
326336
caml_free_stack(th->current_stack);
@@ -752,11 +762,11 @@ CAMLprim value caml_thread_yield(value unit)
752762
with saving errno.)
753763
*/
754764

755-
caml_raise_if_exception(caml_process_pending_signals_exn());
765+
(void) caml_raise_async_if_exception(caml_process_pending_signals_exn (), "");
756766
save_runtime_state();
757767
st_thread_yield(m);
758768
restore_runtime_state(This_thread);
759-
caml_raise_if_exception(caml_process_pending_signals_exn());
769+
(void) caml_raise_async_if_exception(caml_process_pending_signals_exn (), "");
760770

761771
return Val_unit;
762772
}

runtime/amd64.S

+22-13
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,19 @@
145145

146146
/* struct c_stack_link */
147147
#if defined(SYS_mingw64) || defined (SYS_cygwin)
148-
#define Cstack_stack 32
149-
#define Cstack_sp 40
150-
#define Cstack_prev 48
151-
#define SIZEOF_C_STACK_LINK 56
148+
#define Cstack_stack 32
149+
#define Cstack_sp 40
150+
#define Cstack_prev 48
151+
#define Cstack_async_exn_handler 56
152+
#define SIZEOF_C_STACK_LINK 64
153+
#error "Asynchronous exceptions support for Windows isn't finished"
152154
#else
153-
#define Cstack_stack 0
154-
#define Cstack_sp 8
155-
#define Cstack_prev 16
156-
#define SIZEOF_C_STACK_LINK 24
155+
#define Cstack_stack 0
156+
#define Cstack_sp 8
157+
#define Cstack_prev 16
158+
#define Cstack_async_exn_handler 24
159+
/* 8 bytes are left empty to ensure the stack is 16-aligned */
160+
#define SIZEOF_C_STACK_LINK 40
157161
#endif
158162

159163
/******************************************************************************/
@@ -514,10 +518,9 @@ CFI_STARTPROC
514518
RESTORE_ALL_REGS
515519
LEAVE_FUNCTION
516520
ret
517-
1: RESTORE_ALL_REGS
518-
LEA_VAR(caml_exn_Stack_overflow, %rax)
519-
add $16, %rsp /* pop argument, retaddr */
520-
jmp GCALL(caml_raise_exn)
521+
1: SWITCH_OCAML_TO_C
522+
LEA_VAR(caml_exn_Stack_overflow, %rdi)
523+
C_call (GCALL(caml_raise_async))
521524
CFI_ENDPROC
522525
ENDFUNCTION(G(caml_call_realloc_stack))
523526

@@ -700,6 +703,8 @@ LBL(caml_start_program):
700703
movq $0, Cstack_sp(%rsp)
701704
movq Caml_state(c_stack), %r10
702705
movq %r10, Cstack_prev(%rsp)
706+
movq Caml_state(async_exn_handler), %r10
707+
movq %r10, Cstack_async_exn_handler(%rsp)
703708
movq %rsp, Caml_state(c_stack)
704709
CHECK_STACK_ALIGNMENT
705710
/* Load the OCaml stack. */
@@ -719,6 +724,7 @@ LBL(caml_start_program):
719724
movq Caml_state(exn_handler), %r11
720725
movq %r11, 0(%r10)
721726
movq %r10, Caml_state(exn_handler)
727+
movq %r10, Caml_state(async_exn_handler)
722728
/* Switch stacks and call the OCaml code */
723729
movq %r10, %rsp
724730
#ifdef ASM_CFI_SUPPORTED
@@ -728,7 +734,7 @@ LBL(caml_start_program):
728734
/* %rsp + 16 contains the C_STACK_SP */ \
729735
DW_OP_breg + DW_REG_rsp, 16 /* exn handler */, DW_OP_deref, \
730736
DW_OP_plus_uconst, \
731-
24 /* struct c_stack_link */ + \
737+
SIZEOF_C_STACK_LINK /* struct c_stack_link */ + \
732738
6*8 /* callee save regs */ + \
733739
8 /* ret addr */
734740
#endif
@@ -750,6 +756,8 @@ LBL(108):
750756
movq Caml_state(c_stack), %rsp
751757
CFI_RESTORE_STATE
752758
/* Pop the struct c_stack_link */
759+
movq Cstack_async_exn_handler(%rsp), %r10
760+
movq %r10, Caml_state(async_exn_handler)
753761
movq Cstack_prev(%rsp), %r10
754762
movq %r10, Caml_state(c_stack)
755763
addq $SIZEOF_C_STACK_LINK, %rsp; CFI_ADJUST(-SIZEOF_C_STACK_LINK)
@@ -804,6 +812,7 @@ LBL(117):
804812
CFI_ENDPROC
805813
ENDFUNCTION(G(caml_raise_exn))
806814

815+
807816
FUNCTION(G(caml_reraise_exn))
808817
CFI_STARTPROC
809818
ENTER_FUNCTION

0 commit comments

Comments
 (0)