Skip to content

Commit

Permalink
Reflect the status of the naked pointer checker in the exit code (#10171
Browse files Browse the repository at this point in the history
)

If the out-of-heap pointer test is tripped at any point, don't allow the
process to exit normally with code 0. If `caml_sys_exit` is ultimately
passed 0, change it to 70 (but any other non-zero code is preserved).
  • Loading branch information
dra27 committed Mar 6, 2021
1 parent fc95347 commit bcffaef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changes
Expand Up @@ -61,6 +61,10 @@ Working version
- #10136: Minor clean-ups in runtime/io.c and runtime/caml/io.h
(Xavier Leroy, review by David Allsopp and Guillaume Munch-Maccagnoni)

- #10171: Tweak the naked pointers checker so that processes which trigger the
alarm always exit with non-zero status (i.e. exit(0) becomes exit(70)).
(David Allsopp, review by Xavier Leroy)

- #10188, #10213: Switch the default allocation policy to best-fit and adjust
the default overhead parameter accordingly.
(Damien Doligez, review by Josh Berdine and Xavier Leroy)
Expand Down
4 changes: 4 additions & 0 deletions runtime/caml/major_gc.h
Expand Up @@ -98,6 +98,10 @@ void caml_set_major_window (int);
*/
void caml_finalise_heap (void);

#ifdef NAKED_POINTERS_CHECKER
extern int caml_naked_pointers_detected;
#endif

#endif /* CAML_INTERNALiS */

#endif /* CAML_MAJOR_GC_H */
6 changes: 6 additions & 0 deletions runtime/major_gc.c
Expand Up @@ -127,6 +127,10 @@ double caml_gc_clock = 0.0;
static unsigned long major_gc_counter = 0;
#endif

#ifdef NAKED_POINTERS_CHECKER
int caml_naked_pointers_detected = 0;
#endif

void (*caml_major_gc_hook)(void) = NULL;

/* This function prunes the mark stack if it's about to overflow. It does so
Expand Down Expand Up @@ -1163,6 +1167,7 @@ static void is_naked_pointer_safe (value v, value *p)
if (Is_black_hd(h) && Wosize_hd(h) < (INT64_LITERAL(1) << 40))
return;

caml_naked_pointers_detected = 1;
if (!Is_black_hd(h)) {
fprintf (stderr, "Out-of-heap pointer at %p of value %p has "
"non-black head (tag=%d)\n", p, (void*)v, t);
Expand All @@ -1175,6 +1180,7 @@ static void is_naked_pointer_safe (value v, value *p)
return;

on_segfault:
caml_naked_pointers_detected = 1;
fprintf (stderr, "Out-of-heap pointer at %p of value %p. "
"Cannot read head.\n", p, (void*)v);
}
Expand Down
8 changes: 8 additions & 0 deletions runtime/sys.c
Expand Up @@ -49,6 +49,7 @@
#include "caml/debugger.h"
#include "caml/fail.h"
#include "caml/gc_ctrl.h"
#include "caml/major_gc.h"
#include "caml/io.h"
#include "caml/misc.h"
#include "caml/mlvalues.h"
Expand Down Expand Up @@ -159,6 +160,13 @@ CAMLprim value caml_sys_exit(value retcode_v)
caml_shutdown();
#ifdef _WIN32
caml_restore_win32_terminal();
#endif
#ifdef NAKED_POINTERS_CHECKER
if (retcode == 0 && caml_naked_pointers_detected) {
fprintf (stderr, "\nOut-of-heap pointers were detected by the runtime.\n"
"The process would otherwise have terminated normally.\n");
retcode = 70; /* EX_SOFTWARE; see sysexits.h */
}
#endif
exit(retcode);
}
Expand Down

0 comments on commit bcffaef

Please sign in to comment.