caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Hybrid ML/C programs and exceptions
@ 2002-04-11 21:34 Tom Buscher
  2002-04-11 23:25 ` Remi VANICAT
  2002-04-12  7:39 ` Xavier Leroy
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Buscher @ 2002-04-11 21:34 UTC (permalink / raw)
  To: caml-list; +Cc: jpolakow, tfoucher

Hi,

We have a hybrid O'Caml/C application in which ML calls C,
which calls ML, like this:

  ML -> C -> ML
   1    2    3

The "lower" ML(3) raises an exception, which is dealt with
by C(2) using one of the callback_exn functions. When the C
function returns, the "upper" ML function generates a bus
error on Solaris 8, on writing to a mutable global variable.
We are using O'Caml 3.04 with the bytecode compiler.

Attached is a sample program of the same structure that
reproduces this problem. The problem appears to be caused by
the handling of the RAISE opcode in interprete(), executing
the "lower" ML bytecode. It sets extern_sp to a value which
reflects the program state before the C layer was called.

Is this program structure allowed?

Regards,

Tom, Trevor and Jeff

------------------------------------------------------------
FILE: top.ml
------------------------------------------------------------
exception Bot;;
let bot() = raise Bot 
let _ = Callback.register "bot" bot

external foo : unit -> unit = "foo"

let r = ref 1;;
r := 2;
foo();
r := 3

------------------------------------------------------------
FILE: foo.c
------------------------------------------------------------
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/memory.h>

CAMLprim void
foo(value unit)
{
        CAMLparam1(unit);
        callback_exn(*caml_named_value("bot"), Val_int(0));
        CAMLreturn0;
}

------------------------------------------------------------
Build command (with gcc-2.95.3):

  ocamlc -custom -o top top.ml foo.c
------------------------------------------------------------
Tom Buscher                       "For every complex problem,
GNP Computers, Inc.                there is a simple solution.
tbuscher@gnp.com                   And it is wrong."
fon: +1 626.305.8484
fax: +1 626.599.3115                         -- H. L. Mencken
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Hybrid ML/C programs and exceptions
  2002-04-11 21:34 [Caml-list] Hybrid ML/C programs and exceptions Tom Buscher
@ 2002-04-11 23:25 ` Remi VANICAT
  2002-04-12  7:39 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Remi VANICAT @ 2002-04-11 23:25 UTC (permalink / raw)
  To: caml-list

Tom Buscher <tbuscher@gnp.com> writes:

> Hi,
> 
> We have a hybrid O'Caml/C application in which ML calls C,
> which calls ML, like this:
> 
>   ML -> C -> ML
>    1    2    3
> 
> The "lower" ML(3) raises an exception, which is dealt with
> by C(2) using one of the callback_exn functions. When the C
> function returns, the "upper" ML function generates a bus
> error on Solaris 8, on writing to a mutable global variable.
> We are using O'Caml 3.04 with the bytecode compiler.
> 
> Attached is a sample program of the same structure that
> reproduces this problem. The problem appears to be caused by
> the handling of the RAISE opcode in interprete(), executing
> the "lower" ML bytecode. It sets extern_sp to a value which
> reflects the program state before the C layer was called.
> 
> Is this program structure allowed?
> 
> Regards,
> 
> Tom, Trevor and Jeff
> 
> ------------------------------------------------------------
> FILE: top.ml
> ------------------------------------------------------------
> exception Bot;;
> let bot() = raise Bot 
> let _ = Callback.register "bot" bot
> 
> external foo : unit -> unit = "foo"
> 
> let r = ref 1;;
> r := 2;
> foo();
> r := 3
> 
> ------------------------------------------------------------
> FILE: foo.c
> ------------------------------------------------------------
> #include <caml/mlvalues.h>
> #include <caml/callback.h>
> #include <caml/memory.h>
> 
> CAMLprim void
> foo(value unit)
> {
>         CAMLparam1(unit);
>         callback_exn(*caml_named_value("bot"), Val_int(0));
>         CAMLreturn0;
> }

this seem suspicious, CAMLprim should have type

CAMLprim value foo(value)

CAMLreturn0 is only usefull in the case of something like

ML call C witch call C witch call ML

(for example it useful for Gtk binding, where there are C callback
which then call ML).

by the way, changing foo.c with :

#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/memory.h>

CAMLprim value
foo(value unit)
{
        CAMLparam1(unit);
        callback_exn(*caml_named_value("bot"), Val_int(0));
        CAMLreturn(Val_unit);
}

doesn't change the problem...

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Hybrid ML/C programs and exceptions
  2002-04-11 21:34 [Caml-list] Hybrid ML/C programs and exceptions Tom Buscher
  2002-04-11 23:25 ` Remi VANICAT
@ 2002-04-12  7:39 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2002-04-12  7:39 UTC (permalink / raw)
  To: Tom Buscher; +Cc: caml-list, jpolakow, tfoucher

> We have a hybrid O'Caml/C application in which ML calls C,
> which calls ML, like this:
> The "lower" ML(3) raises an exception, which is dealt with
> by C(2) using one of the callback_exn functions. When the C
> function returns, the "upper" ML function generates a bus
> error on Solaris 8, on writing to a mutable global variable.
> We are using O'Caml 3.04 with the bytecode compiler.
> 
> Is this program structure allowed?

Yes, it is, and if you compile with the native-code compiler ocamlopt,
it works fine.  However, you've hit a bug in the bytecode interpreter
that causes callback_exn to misbehave.  This will be fixed shortly.
Thanks for reporting the problem.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-04-12  7:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-11 21:34 [Caml-list] Hybrid ML/C programs and exceptions Tom Buscher
2002-04-11 23:25 ` Remi VANICAT
2002-04-12  7:39 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).