Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007179OCamldocumentationpublic2016-03-13 16:052017-04-10 14:51
Assigned To 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0007179: Interfacing with C missing documentation and helper macros and functions
DescriptionWhen an ocaml function is used as callback from C code and the last thing the callback does is call a C stub returning unit and the return register happens to contain ((x & 3) == 2) then this is taken as an exception. When caml_callback() is used this carshes and when caml_callback_exn is garbage is returned from Extract_exception().

Ocaml should sanitize the return register after calling the C stub.
Steps To Reproduce% make
ocamlopt -g -c -o main.cmx
gcc -O2 -W -Wall -g -c -o stubs.o stubs.c
ocamlopt -o test main.cmx stubs.o
test: callback got exception 0x0

==> Makefile <==
        ocamlopt -o $@ $+

        ocamlopt -g -c -o $@ $<

%.o: %.c
        gcc -O2 -W -Wall -g -c -o $@ $<

        rm -f test *.o *.cmx *.cmi *~

==> <==
external fail : unit -> unit = "fail"
external test : (unit -> unit) -> unit = "test"

let callback () =
  fail ()
let () =
  test callback

==> stubs.c <==
    printf("%s(0x%lx)\n", __PRETTY_FUNCTION__, callback);
    res = caml_callback_exn(callback, Val_unit);
    if (Is_exception_result(res)) {
        res = Extract_exception(res);
        fprintf(stderr, "%s: callback got exception 0x%lx\n", __PRETTY_FUNCTION__, res);
Additional Information [^]
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
mfp (reporter)
2016-03-13 18:54

AFAICS CAMLreturn0 is not supposed to be in the outermost C function
(the one called by OCaml), but only in void-returning "procedures".
Indeed, as per 19.1.2 in the manual, primitives should always be declared
with CAMLprim value and would thus use CAMLreturn(Val_unit).
shinwell (developer)
2016-03-14 09:13

Hmm, I wonder if all this needs is a clarification in the manual that you must use one of the macros that actually returns a value at the return point to OCaml.
goswin (reporter)
2016-03-14 12:03

It does. I would like to re-title this issue to "Interfacing with C missing documentation and helper macros and functions"

I just looked over the chapter and spotted a few other things missing:

- Nowhere in 19.1.2 does it say that returning value is a requirement. All the examples return a non-void result so that gives no hint.

- In 19.5.1 the docs say to use CAMLreturn0 for methods returning void. It does not mention that that is not allowed for the interface between C and ocaml.

- There is no example for CAMLreturnT.

- CAMLnoreturn is not explained

- Weak.t is not mentioned (see other issue)

- 19.3.4 lists unit as Val_int(0) instead of Val_unit, true/false as Val_int but there is Val_bool, Val-true, Val_false, Val_not, Bool_val, [] not as Val_emptylist, x::xs not with Tag_cons

- option types and references are not explained

- mlvalues.h: Val_none and Tag_some missing, Some_val(), Is_none() or Is_some() missing

- alloc.h: caml_alloc_some(value) missing
doligez (administrator)
2016-03-14 14:34

Indeed this part of the documentation needs some work.

I've re-titled this PR.

- Issue History
Date Modified Username Field Change
2016-03-13 16:05 goswin New Issue
2016-03-13 18:54 mfp Note Added: 0015479
2016-03-14 09:13 shinwell Note Added: 0015480
2016-03-14 12:03 goswin Note Added: 0015486
2016-03-14 14:34 doligez Note Added: 0015493
2016-03-14 14:34 doligez Severity crash => minor
2016-03-14 14:34 doligez Status new => acknowledged
2016-03-14 14:34 doligez Target Version => 4.03.0+dev / +beta1
2016-03-14 14:34 doligez Summary caml_callback crashes together with with C stubs returning void => Interfacing with C missing documentation and helper macros and functions
2016-04-14 17:49 doligez Target Version 4.03.0+dev / +beta1 => 4.03.1+dev
2016-12-07 19:08 shinwell Category OCaml general => OCaml documentation
2017-02-16 14:00 doligez Target Version 4.03.1+dev => undecided
2017-02-23 16:35 doligez Category OCaml documentation => Documentation
2017-02-23 16:44 doligez Category Documentation => documentation
2017-04-10 14:51 doligez Target Version undecided =>

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker