Skip to content

Commit

Permalink
Restore minor heap pointer after a Stack_overflow (#10633)
Browse files Browse the repository at this point in the history
* Restore minor heap pointer after a Stack_overflow

When CONTEXT_YOUNG_PTR is defined but CONTEXT_EXCEPTION_POINTER is not, as in amd64/Linux,
Caml_state->young_ptr was not updated from CONTEXT_YOUNG_PTR.

* Update ARM and ARM64 register descriptions w.r.t. signal handling

ARM: the exception pointer is now in register r8.

ARM64: the exception pointer register was missing.  (But is not used
currently, as we use the RETURN_AFTER_STACK_OVERFLOW method on ARM64).

Co-authored-by: Xavier Leroy <xavier.leroy@college-de-france.fr>
  • Loading branch information
stedolan and xavierleroy committed Sep 14, 2021
1 parent f9fe08c commit e20fe18
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Changes
Expand Up @@ -192,6 +192,11 @@ Working version
- #10603, #10611: Fix if condition marked as inconstant in flambda
(Vincent Laviron and Pierre Chambart, report by Marcello Seri)

- #10633: Stack overflow recovery in ocamlopt for AMD64/Linux and ARM/Linux
was not restoring the minor heap pointer correctly
(Stephen Dolan, review by Xavier Leroy)


OCaml 4.13.0
-------------

Expand Down
6 changes: 4 additions & 2 deletions runtime/signals_nat.c
Expand Up @@ -224,9 +224,11 @@ DECLARE_SIGNAL_HANDLER(segv_handler)
#endif
#else
/* Raise a Stack_overflow exception straight from this signal handler */
#if defined(CONTEXT_YOUNG_PTR) && defined(CONTEXT_EXCEPTION_POINTER)
Caml_state->exception_pointer == (char *) CONTEXT_EXCEPTION_POINTER;
#if defined(CONTEXT_YOUNG_PTR)
Caml_state->young_ptr = (value *) CONTEXT_YOUNG_PTR;
#endif
#if defined(CONTEXT_EXCEPTION_POINTER)
Caml_state->exception_pointer = (char *) CONTEXT_EXCEPTION_POINTER;
#endif
caml_raise_stack_overflow();
#endif
Expand Down
6 changes: 4 additions & 2 deletions runtime/signals_osdep.h
Expand Up @@ -137,8 +137,8 @@
typedef unsigned long context_reg;
#define CONTEXT_PC (context->uc_mcontext.arm_pc)
#define CONTEXT_SP (context->uc_mcontext.arm_sp)
#define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.arm_fp)
#define CONTEXT_YOUNG_PTR (context->uc_mcontext.arm_r8)
#define CONTEXT_EXCEPTION_PTR (context->uc_mcontext.arm_r8)
#define CONTEXT_YOUNG_PTR (context->uc_mcontext.arm_r10)
#define CONTEXT_FAULTING_ADDRESS ((char *) context->uc_mcontext.fault_address)

/****************** ARM64, Linux */
Expand All @@ -158,6 +158,7 @@
#define CONTEXT_PC (context->uc_mcontext.pc)
#define CONTEXT_SP (context->uc_mcontext.sp)
#define CONTEXT_C_ARG_1 (context->uc_mcontext.regs[0])
#define CONTEXT_EXCEPTION_POINTER (context->uc_mcontext.regs[26])
#define CONTEXT_YOUNG_PTR (context->uc_mcontext.regs[27])
#define CONTEXT_FAULTING_ADDRESS ((char *) context->uc_mcontext.fault_address)

Expand All @@ -181,6 +182,7 @@
#define CONTEXT_PC (CONTEXT_STATE.__pc)
#define CONTEXT_SP (CONTEXT_STATE.__sp)
#define CONTEXT_C_ARG_1 (CONTEXT_STATE.__x[0])
#define CONTEXT_EXCEPTION_POINTER (CONTEXT_STATE.__x[26])
#define CONTEXT_YOUNG_PTR (CONTEXT_STATE.__x[27])
#define CONTEXT_FAULTING_ADDRESS ((char *) info->si_addr)

Expand Down
5 changes: 4 additions & 1 deletion testsuite/tests/runtime-errors/stackoverflow.ml
Expand Up @@ -34,17 +34,20 @@ let rec f x =
raise Stack_overflow

let _ =
let p = Sys.opaque_identity (ref 42) in
begin
try
ignore(f 0)
with Stack_overflow ->
print_string "Stack overflow caught"; print_newline()
end ;
for i = 1 to 1000 do ignore (Sys.opaque_identity (ref 1_000_000)) done;
(* GPR#1289 *)
Printexc.record_backtrace true;
begin
try
ignore(f 0)
with Stack_overflow ->
print_string "second Stack overflow caught"; print_newline()
end
end;
print_string "!p = "; print_int !p; print_newline ()
Expand Up @@ -6,3 +6,4 @@ x = 20000
x = 10000
x = 0
second Stack overflow caught
!p = 42
1 change: 1 addition & 0 deletions testsuite/tests/runtime-errors/stackoverflow.reference
Expand Up @@ -6,3 +6,4 @@ x = 20000
x = 10000
x = 0
second Stack overflow caught
!p = 42

0 comments on commit e20fe18

Please sign in to comment.