* toplevel not executed: Bug ?
@ 2008-09-03 16:08 Romain Beauxis
2008-09-03 21:56 ` [Caml-list] " Stéphane Glondu
0 siblings, 1 reply; 2+ messages in thread
From: Romain Beauxis @ 2008-09-03 16:08 UTC (permalink / raw)
To: caml-list; +Cc: smimram
[-- Attachment #1: Type: text/plain, Size: 1351 bytes --]
Hi all !
I've encountered a strange bug while preparing a caml module with C function.
Depending on the execution of a caml-defined function, the toplevel is
evaluated or not, leading to a segfault when calling a caml callback from C.
The code of the C part is:
<---
#include <caml/callback.h>
#include <caml/custom.h>
#include <caml/memory.h>
CAMLprim value f(unit)
{
CAMLparam0();
caml_callback(*caml_named_value("test_printf"),Val_unit);
CAMLreturn(Val_unit);
}
<---
The code of the caml part is:
<---
let f () = Printf.printf "blalba\n"; flush_all ()
let () = Callback.register "test_printf" f
module Test=
struct
external f : unit -> unit = "f"
let f2 () = Printf.printf "f2\n"; flush_all ()
end
<---
The code that uses the module is:
<---
(* This needs to be executed *)
let () = Test.Test.f2 ()
let () = Test.Test.f ()
<---
Step to reproduce:
Compile all using the following sequence:
gcc -c -o test_stubs.o ./test_stubs.c -fPIC
ocamlmklib -o test_stubs ./test_stubs.o
ocamlopt -c test.ml
ocamlopt -a -cclib -ltest_stubs -o test.cmxa test.cmx
ocamlopt -c bla.ml
ocamlopt -I . test.cmxa bla.cmx -o bla
Then, if you comment the first call to f2 in bla.ml, then the toplevel is not
evaluated before executing the C function, leading to a segfault..
Is this a bug or am I missing something ?
Romain
PS: files attached
[-- Attachment #2: bla.ml --]
[-- Type: application/octet-stream, Size: 84 bytes --]
(* This needs to be executed *)
let () = Test.Test.f2 ()
let () = Test.Test.f ()
[-- Attachment #3: test.ml --]
[-- Type: text/plain, Size: 208 bytes --]
let f () = Printf.printf "blalba\n"; flush_all ()
let () = Callback.register "test_printf" f
module Test=
struct
external f : unit -> unit = "f"
let f2 () = Printf.printf "f2\n"; flush_all ()
end
[-- Attachment #4: test_stubs.c --]
[-- Type: text/x-csrc, Size: 208 bytes --]
#include <caml/callback.h>
#include <caml/custom.h>
#include <caml/memory.h>
CAMLprim value f(unit)
{
CAMLparam0();
caml_callback(*caml_named_value("test_printf"),Val_unit);
CAMLreturn(Val_unit);
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] toplevel not executed: Bug ?
2008-09-03 16:08 toplevel not executed: Bug ? Romain Beauxis
@ 2008-09-03 21:56 ` Stéphane Glondu
0 siblings, 0 replies; 2+ messages in thread
From: Stéphane Glondu @ 2008-09-03 21:56 UTC (permalink / raw)
To: Romain Beauxis; +Cc: caml-list, smimram
Romain Beauxis wrote:
> I've encountered a strange bug while preparing a caml module with C function.
> Depending on the execution of a caml-defined function, the toplevel is
> evaluated or not, leading to a segfault when calling a caml callback from C.
> [...]
I don't know whether this is a bug, but I have an explanation.
At linking phase, module Test is not linked in because it is not used.
When you use "-linkall", the segfault disappear.
Module Test is considered unused because the inferred interface for
module Test records that f is an external. You can force the usage of
module Test by providing your own .mli, where f is abstracted. When you
do so, the segfault disappear (even without -linkall).
HTH,
--
Stéphane Glondu
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-09-03 21:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-03 16:08 toplevel not executed: Bug ? Romain Beauxis
2008-09-03 21:56 ` [Caml-list] " Stéphane Glondu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox