# Stack overflow reported as segfault (?)

**URL:** <https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646>\
**Category:** Learning\
**Created:** [October 18, 2021, 11:48am UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646 "2021-10-18T11:48:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas\_ridge](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thomas_ridge/32/1478_2.png) [@thomas\_ridge](https://discuss.ocaml.org/u/thomas_ridge)\
**Post date:** [October 18, 2021, 11:48am UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/1 "2021-10-18T11:48:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [October 18, 2021, 11:55am UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/2 "2021-10-18T11:55:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [October 18, 2021, 11:59am UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/3 "2021-10-18T11:59:41Z")

</div>

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) ?

---

<div class="post-metadata">

**Author:** ![thomas\_ridge](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thomas_ridge/32/1478_2.png) [@thomas\_ridge](https://discuss.ocaml.org/u/thomas_ridge)\
**Post date:** [October 18, 2021, 1:30pm UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/4 "2021-10-18T13:30:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thomas\_ridge](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thomas_ridge/32/1478_2.png) [@thomas\_ridge](https://discuss.ocaml.org/u/thomas_ridge)\
**Post date:** [October 18, 2021, 1:37pm UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/5 "2021-10-18T13:37:35Z")

</div>

> [@silene](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![thomas\_ridge](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thomas_ridge/32/1478_2.png) [@thomas\_ridge](https://discuss.ocaml.org/u/thomas_ridge)\
**Post date:** [October 18, 2021, 1:47pm UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/6 "2021-10-18T13:47:40Z")

</div>

A similar issue on github, resolved in ocaml trunk: [Segmentation fault on stack overflow - OCaml 4.12, Linux · Issue #10645 · ocaml/ocaml · GitHub](https://github.com/ocaml/ocaml/issues/10645)

---

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [October 18, 2021, 3:33pm UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/7 "2021-10-18T15:33:17Z")

</div>

> [@thomas\_ridge](#):
>
> OK, so for C code, a stack overflow is always reported as a segfault? (I know very little about C.)

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.

---

<div class="post-metadata">

**Author:** ![xavierleroy](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xavierleroy](https://discuss.ocaml.org/u/xavierleroy)\
**Post date:** [October 19, 2021, 8:21am UTC](https://discuss.ocaml.org/t/stack-overflow-reported-as-segfault/8646/8 "2021-10-19T08:21:24Z")

</div>

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.
