Aborting cleanly in C code when `caml_callback*_exn()` returns an exception
Hi,
I am maintaining a binding for the GLFW library, which provides an API for registering callback functions to be used for event handling. In order to make these kind of features available in OCaml, I use closures provided by the user in the C stubs with the caml_callback family of functions.
An issue arises when the user-provided code raises an exception that it does not handle. In that case, the exception is propagated to the OCaml code where the user called the GLFW event-polling function, skipping over the C code, and possibly leaving it in an incoherent state or leaking resources.
In order to handle this problem, I would like to handle exceptions raised by a callback function the same way the OCaml runtime would handle an uncaught exception (print it to stderr, optionally with a backtrace, and abort).
My current solution is to use the caml_fatal_uncaught_exception function found in the printexc.h header, but I am wondering if it is valid to call this function like that given its definition is guarded with #ifdef CAML_INTERNALS. A GitHub PR added those guards a few years ago and it seems to be implied that the choice was made somewhat by default for lack of users outside the runtime at that point, so maybe using it in user code might actually be fine?
I would like to hear the thoughts of someone with better knowledge of the OCaml internals about this so that we can figure out whether this approach is correct, needs refinement, or is not viable.
Cheers!
1 comment
@leostera @kartikb753 Sometimes you find bugs that have been in the oracle for 27 years.
ocaml#15157
1 like · 16 views
Note that you can also build an OCaml cross-compiler: HACKING – (experimental) Building a cross compiler.
> A cross compiler is a compiler that runs on some machine, named the host, but generates code for a different machine, named the target.
That way you'll be able to build OCaml and OCaml programs on a more powerful machine, then transfer them to your NanoPi, and execute your programs there.
Got bored of distributed systems, started hunting compiler bugs. ocaml#15202
32 likes · 1 repost · 1,067 views
[Caml-list] OCaml 5.5.2 released
Dear OCaml users,
We have the pleasure of celebrating the anniversary of the first powered flight in Europe by announcing the release of OCaml 5.5.2.
OCaml 5.5.2 fixes six minor security issues, two rare miscompilations, a configuration issue with future versions of macOS, and one runtime events library issue.
Overall, if you were already using any versions of OCaml 5.5, we are advising you to switch to OCaml 5.5.2 .
The full list of bug fixes is available below for more details.
Happy hacking,
-- Florian Angeletti, for the OCaml team.
Installation Instructions
-----------------------------------
The base compiler can be installed as an opam switch with the following commands:
opam update
opam switch create 5.5.2
The source code for the release is also directly available on:
* GitHub: github.com/ocaml/ocaml/archive/5.5.2.tar.gz
* Inria archive: caml.inria.fr
Bug Fixes
--------------
- #15045(ocaml#15045): Atomic loads whose results are unused should not be removed
during dead-code elimination.
(Xavier Clerc, review by Steven Dolan and KC Sivaramakrishnan)
- #15067(ocaml#15067), [#15071](ocaml#15071): Fix detection of functions available in the future on macOS.
(Antonin Décimo, review by Nicolás Ojeda Bär and Damien Doligez)
- #15112(ocaml#15112), OSEC-2026-22: `Unix.map_file`: protect against overflow
when computing the data size.
(Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär
and Edwin Török)
- #15113(ocaml#15113), OSEC-2026-21: `Str.string_partial_match`: make sure captured groups
are valid.
(Xavier Leroy, review by Nicolás Ojeda Bär and Edwin Török)
- #15115(ocaml#15115), OSEC-2026-23: Fix multiple issues in demarshaling
(`Marshal.from_{channel,string,bytes}`)
(Xavier Leroy, review by Nicolás Ojeda Bär)
- #15133(ocaml#15133), OSEC 2026-24: Multiple issues in command quoting on Windows
(Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär)
- #15119(ocaml#15119), OSEC-2026-25: Harden decoding of runtime events messages from
ring buffers.
(Sadiq Jaffer, report by Hannes Mehnert, review by Olivier Nicole and
Edwin Török)
- #15177(ocaml#15177), OSEC-2026-26: Maintain custom events cache integrity.
(Sadiq Jaffer, report by Hannes Mehnert, review by Edwin Török)
- #15043(ocaml#15043), [#15076](ocaml#15076): runtime events library, fix an infinite loop when a user
event is larger than half the ring buffer.
(Tim McGilchrist, review by Sadiq Jaffer)
- #15185(ocaml#15185), [#15187](ocaml#15187): fix a pattern-matching miscompilation on certain
combinations of immediate ranges and redundant patterns
(Gabriel Scherer, report by Alperer Keleş,
review by Vincent Laviron and Florian Angeletti)
github.com/ocaml/ocaml/archive/5.5.2.tar.gz: github.com/ocaml/ocaml/archive/5.5.2.tar.gz
ocaml#15045: ocaml#15045
ocaml#15067: ocaml#15067
ocaml#15071: ocaml#15071
ocaml#15112: ocaml#15112
ocaml#15113: ocaml#15113
ocaml#15115: ocaml#15115
ocaml#15133: ocaml#15133
ocaml#15119: ocaml#15119
ocaml#15177: ocaml#15177
ocaml#15043: ocaml#15043
ocaml#15076: ocaml#15076
ocaml#15185: ocaml#15185
ocaml#15187: ocaml#15187
OCaml 5.5.2 released
We have the pleasure of celebrating the anniversary of the first powered flight in Europe by announcing the release of OCaml 5.5.2.
OCaml 5.5.2 fixes various minor security issues, two rare miscompilations, a configuration issue with future versions of macOS, and one runtime events library issue.
Overall, if you were already using any versions of OCaml 5.5, we are advising you to switch to OCaml 5.5.2 .
The full list of bug fixes is available below for more details.
Happy hacking,
-- Florian Angeletti, for the OCaml team.
Installation Instructions
-------------------------------
The base compiler can be installed as an opam switch with the following commands:
opam update
opam switch create 5.5.2
The source code for the release is also directly available on:
- GitHub: github.com/ocaml/ocaml/archive/5.5.2.tar.gz
- Inria archive: caml.inria.fr
Bug Fixes
-----------
- #15045: Atomic loads whose results are unused should not be removed
during dead-code elimination.
(Xavier Clerc, review by Steven Dolan and KC Sivaramakrishnan)
- #15067, #15071: Fix detection of functions available in the future on macOS.
(Antonin Décimo, review by Nicolás Ojeda Bär and Damien Doligez)
- #15112, OSEC-2026-22: Unix.map_file: protect against overflow
when computing the data size.
(Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär
and Edwin Török)
- #15113, OSEC-2026-21: Str.string_partial_match: make sure captured groups
are valid.
(Xavier Leroy, review by Nicolás Ojeda Bär and Edwin Török)
- #15115, OSEC-2026-23: Fix multiple issues in demarshaling
(Marshal.from_{channel,string,bytes})
(Xavier Leroy, review by Nicolás Ojeda Bär)
- #15133, OSEC 2026-24: Multiple issues in command quoting on Windows
(Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär)
- #15119, OSEC-2026-25: Harden decoding of runtime events messages from
ring buffers.
(Sadiq Jaffer, report by Hannes Mehnert, review by Olivier Nicole and
Edwin Török)
- #15177, OSEC-2026-26: Maintain custom events cache integrity.
(Sadiq Jaffer, report by Hannes Mehnert, review by Edwin Török)
- #15043, #15076: runtime events library, fix an infinite loop when a user
event is larger than half the ring buffer.
(Tim McGilchrist, review by Sadiq Jaffer)
- #15185, #15187: fix a pattern-matching miscompilation on certain
combinations of immediate ranges and redundant patterns
(Gabriel Scherer, report by Alperer Keleş,
review by Vincent Laviron and Florian Angeletti)
23 likes
Mentions
Aborting cleanly in C code when `caml_callback*_exn()` returns an exception Hi, I am maintaining a binding for the GLFW library, which provides an API for registering callback functions to be used for event handling. In order to make these kind of features available in OCaml, I use closures provided by the user in the C stubs with the caml_callback family of functions. An issue arises when the user-provided code raises an exception that it does not handle. In that case, the exception is propagated to the OCaml code where the user called the GLFW event-polling function, skipping over the C code, and possibly leaving it in an incoherent state or leaking resources. In order to handle this problem, I would like to handle exceptions raised by a callback function the same way the OCaml runtime would handle an uncaught exception (print it to stderr, optionally with a backtrace, and abort). My current solution is to use the caml_fatal_uncaught_exception function found in the printexc.h header, but I am wondering if it is valid to call this function like that given its definition is guarded with #ifdef CAML_INTERNALS. A GitHub PR added those guards a few years ago and it seems to be implied that the choice was made somewhat by default for lack of users outside the runtime at that point, so maybe using it in user code might actually be fine? I would like to hear the thoughts of someone with better knowledge of the OCaml internals about this so that we can figure out whether this approach is correct, needs refinement, or is not viable. Cheers!
@leostera @kartikb753 Sometimes you find bugs that have been in the oracle for 27 years. ocaml#15157
Note that you can also build an OCaml cross-compiler: HACKING – (experimental) Building a cross compiler. > A cross compiler is a compiler that runs on some machine, named the host, but generates code for a different machine, named the target. That way you'll be able to build OCaml and OCaml programs on a more powerful machine, then transfer them to your NanoPi, and execute your programs there.
Got bored of distributed systems, started hunting compiler bugs. ocaml#15202
[Caml-list] OCaml 5.5.2 released Dear OCaml users, We have the pleasure of celebrating the anniversary of the first powered flight in Europe by announcing the release of OCaml 5.5.2. OCaml 5.5.2 fixes six minor security issues, two rare miscompilations, a configuration issue with future versions of macOS, and one runtime events library issue. Overall, if you were already using any versions of OCaml 5.5, we are advising you to switch to OCaml 5.5.2 . The full list of bug fixes is available below for more details. Happy hacking, -- Florian Angeletti, for the OCaml team. Installation Instructions ----------------------------------- The base compiler can be installed as an opam switch with the following commands: opam update opam switch create 5.5.2 The source code for the release is also directly available on: * GitHub: github.com/ocaml/ocaml/archive/5.5.2.tar.gz * Inria archive: caml.inria.fr Bug Fixes -------------- - #15045(ocaml#15045): Atomic loads whose results are unused should not be removed during dead-code elimination. (Xavier Clerc, review by Steven Dolan and KC Sivaramakrishnan) - #15067(ocaml#15067), [#15071](ocaml#15071): Fix detection of functions available in the future on macOS. (Antonin Décimo, review by Nicolás Ojeda Bär and Damien Doligez) - #15112(ocaml#15112), OSEC-2026-22: `Unix.map_file`: protect against overflow when computing the data size. (Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär and Edwin Török) - #15113(ocaml#15113), OSEC-2026-21: `Str.string_partial_match`: make sure captured groups are valid. (Xavier Leroy, review by Nicolás Ojeda Bär and Edwin Török) - #15115(ocaml#15115), OSEC-2026-23: Fix multiple issues in demarshaling (`Marshal.from_{channel,string,bytes}`) (Xavier Leroy, review by Nicolás Ojeda Bär) - #15133(ocaml#15133), OSEC 2026-24: Multiple issues in command quoting on Windows (Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär) - #15119(ocaml#15119), OSEC-2026-25: Harden decoding of runtime events messages from ring buffers. (Sadiq Jaffer, report by Hannes Mehnert, review by Olivier Nicole and Edwin Török) - #15177(ocaml#15177), OSEC-2026-26: Maintain custom events cache integrity. (Sadiq Jaffer, report by Hannes Mehnert, review by Edwin Török) - #15043(ocaml#15043), [#15076](ocaml#15076): runtime events library, fix an infinite loop when a user event is larger than half the ring buffer. (Tim McGilchrist, review by Sadiq Jaffer) - #15185(ocaml#15185), [#15187](ocaml#15187): fix a pattern-matching miscompilation on certain combinations of immediate ranges and redundant patterns (Gabriel Scherer, report by Alperer Keleş, review by Vincent Laviron and Florian Angeletti) github.com/ocaml/ocaml/archive/5.5.2.tar.gz: github.com/ocaml/ocaml/archive/5.5.2.tar.gz ocaml#15045: ocaml#15045 ocaml#15067: ocaml#15067 ocaml#15071: ocaml#15071 ocaml#15112: ocaml#15112 ocaml#15113: ocaml#15113 ocaml#15115: ocaml#15115 ocaml#15133: ocaml#15133 ocaml#15119: ocaml#15119 ocaml#15177: ocaml#15177 ocaml#15043: ocaml#15043 ocaml#15076: ocaml#15076 ocaml#15185: ocaml#15185 ocaml#15187: ocaml#15187
OCaml 5.5.2 released We have the pleasure of celebrating the anniversary of the first powered flight in Europe by announcing the release of OCaml 5.5.2. OCaml 5.5.2 fixes various minor security issues, two rare miscompilations, a configuration issue with future versions of macOS, and one runtime events library issue. Overall, if you were already using any versions of OCaml 5.5, we are advising you to switch to OCaml 5.5.2 . The full list of bug fixes is available below for more details. Happy hacking, -- Florian Angeletti, for the OCaml team. Installation Instructions ------------------------------- The base compiler can be installed as an opam switch with the following commands: opam update opam switch create 5.5.2 The source code for the release is also directly available on: - GitHub: github.com/ocaml/ocaml/archive/5.5.2.tar.gz - Inria archive: caml.inria.fr Bug Fixes ----------- - #15045: Atomic loads whose results are unused should not be removed during dead-code elimination. (Xavier Clerc, review by Steven Dolan and KC Sivaramakrishnan) - #15067, #15071: Fix detection of functions available in the future on macOS. (Antonin Décimo, review by Nicolás Ojeda Bär and Damien Doligez) - #15112, OSEC-2026-22: Unix.map_file: protect against overflow when computing the data size. (Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär and Edwin Török) - #15113, OSEC-2026-21: Str.string_partial_match: make sure captured groups are valid. (Xavier Leroy, review by Nicolás Ojeda Bär and Edwin Török) - #15115, OSEC-2026-23: Fix multiple issues in demarshaling (Marshal.from_{channel,string,bytes}) (Xavier Leroy, review by Nicolás Ojeda Bär) - #15133, OSEC 2026-24: Multiple issues in command quoting on Windows (Xavier Leroy and Nicolás Ojeda Bär, review by Nicolás Ojeda Bär) - #15119, OSEC-2026-25: Harden decoding of runtime events messages from ring buffers. (Sadiq Jaffer, report by Hannes Mehnert, review by Olivier Nicole and Edwin Török) - #15177, OSEC-2026-26: Maintain custom events cache integrity. (Sadiq Jaffer, report by Hannes Mehnert, review by Edwin Török) - #15043, #15076: runtime events library, fix an infinite loop when a user event is larger than half the ring buffer. (Tim McGilchrist, review by Sadiq Jaffer) - #15185, #15187: fix a pattern-matching miscompilation on certain combinations of immediate ranges and redundant patterns (Gabriel Scherer, report by Alperer Keleş, review by Vincent Laviron and Florian Angeletti)