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

no documented way to never raise in C bindings #7572

Closed
vicuna opened this issue Jun 28, 2017 · 2 comments
Closed

no documented way to never raise in C bindings #7572

vicuna opened this issue Jun 28, 2017 · 2 comments

Comments

@vicuna
Copy link

vicuna commented Jun 28, 2017

Original bug ID: 7572
Reporter: @sliquister
Status: acknowledged (set by @xavierleroy on 2017-07-18T20:19:38Z)
Resolution: open
Priority: normal
Severity: minor
Version: 4.04.2
Category: runtime system and C interface
Monitored by: @gasche @hcarty @dbuenzli

Bug description

When writing ocaml plugins for C programs, you end with
functions that look like this:

void called_from_the_C_program (char* str) {
  caml_acquire_lock();
  {
    CAMLparam0();
    CAMLlocal1(v, res);
    v = caml_copy_string(str);
    caml_callback(.., v);
    CAMLdrop;
  }
  caml_release_lock();
}

The problem is that this function is most likely broken, because if
the caml function raises, the ocaml runtime will either exit or unwind
the stack past the C program.
We have caml_callback_exn to help with this, but it's not enough
because caml_alloc_string can also raise.

There seems to be no way of catching ocaml exceptions from a block of
C code, short of exporting the C code, writing an ocaml function that
calls it, registering the ocaml function and calling it from C, which
is a bit ridiculous.

There's this caml_alloc_shr_no_raise function, which may be usable
for this purpose but it's undocumented, so I am not sure it's meant
to be used from the outside (and it's probably not very nice to
have to duplicate all the allocation functions with a _no_raise
suffix).

So is there a recommended way to not throw ocaml exceptions in C
bindings? If not, can we create one?

@vicuna
Copy link
Author

vicuna commented Jul 18, 2017

Comment author: @xavierleroy

The analysis is correct. For the bytecode system (ocamlrun) it would be relatively easy to catch Caml exceptions from C code. The native-code system is a different story, however.

@github-actions
Copy link

github-actions bot commented May 7, 2020

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@github-actions github-actions bot added the Stale label May 7, 2020
@github-actions github-actions bot closed this as completed Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant