Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCaml 4.13 memory leak in caml_setup_stack_overflow_detection #10698

Closed
rwmjones opened this issue Oct 13, 2021 · 3 comments · Fixed by #10726
Closed

OCaml 4.13 memory leak in caml_setup_stack_overflow_detection #10698

rwmjones opened this issue Oct 13, 2021 · 3 comments · Fixed by #10726

Comments

@rwmjones
Copy link
Contributor

Run any ocamlopt-compiled program under valgrind --leak-check=full and you will see:

==2560078== 8,192 bytes in 1 blocks are definitely lost in loss record 25 of 33
==2560078==    at 0x484086F: malloc (vg_replace_malloc.c:380)
==2560078==    by 0x13C969: caml_setup_stack_overflow_detection (in /var/tmp/test)
==2560078==    by 0x13C9E3: caml_init_signals (in /var/tmp/test)
==2560078==    by 0x13B684: caml_startup_common (in /var/tmp/test)
==2560078==    by 0x13B76E: caml_main (in /var/tmp/test)
==2560078==    by 0x11C6D1: main (in /var/tmp/test)

This leak seems to be new in OCaml 4.13.1.

In nbdkit we maintain a set of valgrind suppressions for OCaml, and the only ones we expect are functions matching caml_stat_alloc*: https://gitlab.com/nbdkit/nbdkit/-/blob/master/valgrind/ocaml.suppressions

libguestfs pushed a commit to libguestfs/nbdkit that referenced this issue Oct 13, 2021
libguestfs pushed a commit to libguestfs/libnbd that referenced this issue Oct 13, 2021
Reported upstream as ocaml/ocaml#10698

(cherry picked from nbdkit commit 676c193ba05e479c145cf872e4912c576d1461d3)
@xavierleroy
Copy link
Contributor

Following an unhelpful change in glibc (#10250), there was an emergency fix in #10266, in particular to unblock the Rawhide builds.

The emergency fix allocates the alternate signal stack dynamically, but does not free it because it's not entirely obvious how to do so safely (#10250 (comment)).

Until this is addressed, your temporary fix (adding caml_setup_stack_overflow to the valgrind suppressions) should work. Make sure to thank the glibc developers for this mess.

@rwmjones
Copy link
Contributor Author

I don't disagree! That change has caused a huge amount of churn everywhere.

Back to this bug though, since the alternate stack is not intended to be freed, would it make sense to rename the function caml_stat_alloc_stack_overflow (or anything beginning caml_stat_alloc_) as that appears to be the convention for static allocations?

libguestfs pushed a commit to libguestfs/nbdkit that referenced this issue Oct 19, 2021
Reported upstream as ocaml/ocaml#10698

(cherry picked from commit 676c193)
libguestfs pushed a commit to libguestfs/nbdkit that referenced this issue Oct 19, 2021
Reported upstream as ocaml/ocaml#10698

(cherry picked from commit 676c193)
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Oct 25, 2021
Commit fc95347 introduced dynamic allocation of the alternate stack
used to process signals.

This commit makes sure the dynamically-allocated stack is freed when
the main OCaml code terminates or an OCaml thread terminates.

In passing, we also reset relevant signal handlers to their default behavior
when the main OCaml code terminates.

Fixes: ocaml#10698
@xavierleroy
Copy link
Contributor

xavierleroy commented Oct 25, 2021

Tentative fix in #10726. Needs testing.

xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Oct 25, 2021
Commit fc95347 introduced dynamic allocation of the alternate stack
used to process signals.

This commit makes sure the dynamically-allocated stack is freed when
the main OCaml code terminates or an OCaml thread terminates.

In passing, we also reset relevant signal handlers to their default behavior
when the main OCaml code terminates.

Fixes: ocaml#10698
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Nov 29, 2021
Commit fc95347 introduced dynamic allocation of the alternate stack
used to process signals.

This commit makes sure the dynamically-allocated stack is freed when
the main OCaml code terminates or an OCaml thread terminates.

In passing, we also reset relevant signal handlers to their default behavior
when the main OCaml code terminates.

Fixes: ocaml#10698
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Dec 6, 2021
Commit fc95347 introduced dynamic allocation of the alternate stack
used to process signals.

This commit makes sure the dynamically-allocated stack is freed when
the main OCaml code terminates or an OCaml thread terminates.

In passing, we also reset relevant signal handlers to their default behavior
when the main OCaml code terminates.

Refactoring: add declaration of caml_init_signals and
caml_terminate_signals to <caml/signals.h>.

Fixes: ocaml#10698
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants