Stack overflow reported as segfault (?)

I believe that sometimes a stack overflow is reported as a segfault in native compiled ocaml code. (Is this right?)

Is this inevitable? Or is it possible to somehow to report it always as a proper stack overflow?

I think it will get reported as a segfault rather than an exception if it happens in C code (called from OCaml code). In that case, it is kind of inevitable.

If you’re on an ARM64 processor (like the new Macs), then reliable stack overflow detection was only added in OCaml 4.13.
On some other systems, there is a bug with stack overflow recovery that could cause segfaults after a stack overflow. This has been fixed, but not released yet.

It would be helpful to know whether your case matches one of these situations or is a different issue. Could you provide some details on your setup (OCaml version, OS, processor architecture, reproduction steps if possible) ?

Thanks for the replies. I am on ocaml4.12.0, Ubuntu 20.04 (kubuntu actually), x86-64. I don’t have a repro, but the error happens when a long running system encounters a very large list and tries to do List.map (or similar). If the list is made large to begin with, there is a stack overflow which is reported as such. But in the long running code, it appears as a segfault. And fixing the List.map (to a rev_map or similar) fixes the segfault, so I’m fairly sure the underlying bug is actually a stack overflow.

OK, so for C code, a stack overflow is always reported as a segfault? (I know very little about C.)

A similar issue on github, resolved in ocaml trunk: Segmentation fault on stack overflow - OCaml 4.12, Linux · Issue #10645 · ocaml/ocaml · GitHub

1 Like

As far as I know, yes. So, if your failing List.map happens to (indirectly) call a native function written in C (e.g., a system call or a foreign function), then OCaml might not be able to recover from it.

1 Like

The long-term goal is that stack overflows from OCaml code are properly reported as Stack_overflow exceptions, and stack overflows from C code are (1) avoided if at all possible in the OCaml run-time system, and (2) reported as SEGV signals otherwise. But catching stack overflows is tricky, both in Unix-like systems and under Windows, so the current implementation in OCaml is a best effort that is occasionally buggy.

So, with native code compilation, never rely on stack overflows being turned into exceptions. Just treat the Stack_overflow as a debugging aid.

6 Likes