Skip to content

Commit

Permalink
flambda-backend: Fix for stack checks / frame pointers (#2603)
Browse files Browse the repository at this point in the history
* CI job to test frame pointers with runtime5

* Avoid warning about rewrite_frame_pointers being unused

* Fix frame pointers tests

(cherry picked from commit 4184a7150d754a7d4903566ed5851e2d8f56e8a9)

* Fix for stack checks / frame pointers.

* Backtrace

* Handle non-segfaulting paths...

* Specialized version of ENTER_FUNCTION for caml_raise_stack_overflow_nat.

* Review

---------

Co-authored-by: Mark Shinwell <mshinwell@pm.me>
  • Loading branch information
xclerc and mshinwell authored May 22, 2024
1 parent d6b1dfe commit bbef9e5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
8 changes: 1 addition & 7 deletions runtime/amd64.S
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ ENDFUNCTION(G(caml_call_gc))
FUNCTION(G(caml_raise_stack_overflow_nat))
CFI_STARTPROC
CFI_SIGNAL_FRAME
ENTER_FUNCTION
SAVE_ALL_REGS
movq %r15, Caml_state(gc_regs)
/* We assume that we are always on the OCaml stack at this point.
Expand All @@ -558,12 +557,7 @@ CFI_STARTPROC
in practice this seems most unlikely. The current situation will
suffice as this patch is only a temporary measure in any case. */
SWITCH_OCAML_TO_C
C_call (GCALL(caml_raise_stack_overflow))
SWITCH_C_TO_OCAML
movq Caml_state(gc_regs), %r15
RESTORE_ALL_REGS
LEAVE_FUNCTION
ret
CHECK_STACK_ALIGNMENT; jmp GCALL(caml_raise_stack_overflow)
CFI_ENDPROC
ENDFUNCTION(G(caml_raise_stack_overflow_nat))

Expand Down
2 changes: 2 additions & 0 deletions runtime/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ void caml_rewrite_exception_stack(struct stack_info *old_stack,
}

#ifdef WITH_FRAME_POINTERS
#if defined(STACK_CHECKS_ENABLED)
/* Update absolute base pointers for new stack */
static void rewrite_frame_pointers(struct stack_info *old_stack,
struct stack_info *new_stack)
Expand Down Expand Up @@ -803,6 +804,7 @@ static void rewrite_frame_pointers(struct stack_info *old_stack,
}
#endif
#endif
#endif

int caml_try_realloc_stack(asize_t required_space)
{
Expand Down
4 changes: 4 additions & 0 deletions testsuite/tests/frame-pointers/c_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
native;
*)

(* Force -O3 to ensure the "_code" symbols are present (see the
reference file). *)
[@@@ocaml.flambda_o3]

external fp_backtrace : unit -> unit = "fp_backtrace"
external fp_backtrace_no_alloc : unit -> unit = "fp_backtrace" [@@noalloc]
external fp_backtrace_many_args : int -> int -> int -> int -> int -> int -> int
Expand Down
6 changes: 3 additions & 3 deletions testsuite/tests/frame-pointers/c_call.reference
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
fp_backtrace_many_args
caml_c_call_stack_args
camlC_call.f
camlC_call.f_0_1_code
camlC_call.entry
caml_program
caml_start_program
caml_main/caml_startup
main
caml_c_call
camlC_call.f
camlC_call.f_0_1_code
camlC_call.entry
caml_program
caml_start_program
caml_main/caml_startup
main
camlC_call.f
camlC_call.f_0_1_code
camlC_call.entry
caml_program
caml_start_program
Expand Down
4 changes: 4 additions & 0 deletions testsuite/tests/frame-pointers/exception_handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
native;
*)

(* Force -O3 to ensure the "_code" symbols are present (see the
reference file). *)
[@@@ocaml.flambda_o3]

(* https://github.com/ocaml/ocaml/pull/11031 *)
external fp_backtrace : unit -> unit = "fp_backtrace" [@@noalloc]

Expand Down
16 changes: 8 additions & 8 deletions testsuite/tests/frame-pointers/exception_handler.reference
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
camlException_handler.handler
camlException_handler.bare
camlException_handler.handler_2_7_code
camlException_handler.bare_4_9_code
camlException_handler.entry
caml_program
caml_start_program
caml_main/caml_startup
main
camlException_handler.handler
camlException_handler.bare
camlException_handler.handler_2_7_code
camlException_handler.bare_4_9_code
camlException_handler.entry
caml_program
caml_start_program
caml_main/caml_startup
main
camlException_handler.handler
camlException_handler.nested
camlException_handler.handler_2_7_code
camlException_handler.nested_3_8_code
camlException_handler.entry
caml_program
caml_start_program
caml_main/caml_startup
main
camlException_handler.handler
camlException_handler.nested
camlException_handler.handler_2_7_code
camlException_handler.nested_3_8_code
camlException_handler.entry
caml_program
caml_start_program
Expand Down

0 comments on commit bbef9e5

Please sign in to comment.