caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Toplevel crashes when trying to call external functions
@ 2004-07-12  3:39 John Prevost
  2004-07-12  4:46 ` Andy Yang
  2004-07-12 10:55 ` Gerd Stolpmann
  0 siblings, 2 replies; 14+ messages in thread
From: John Prevost @ 2004-07-12  3:39 UTC (permalink / raw)
  To: ayerkes; +Cc: caml-list

You shouldn't have to package up a non-caml managed pointer in any
sort of caml structure at all.  Take a look at section 18.6 of the
manual, specifically the definitions of curses_initscr and
curses_wrefresh.  Any pointer that's outside the caml heap (that is,
pretty much any pointer that you're getting from a non-caml function)
can simply be treated as opaque, and you can use Caml's type system to
make sure it's valid (as long as the C code always handles these
pointers correctly.)

I'd write your code like the following, based on that:

Caml code:
type sat_manager
external zchaff_InitManager : unit -> sat_manager
external zchall_ReadCnf : sat_manager -> string -> unit

C++ code:
value zchaff_InitManager(void)
{
  CAMLparam0();
  CAMLreturn((value) SAT_InitManager());
}

void zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);
  SAT_Manager solver = (void*)mng;
  cout<<"solver = "<<hex <<solver <<endl;
  assert(solver != NULL);  
  char * fn = String_val(filename);  
  cout<<"file = "<<fn <<endl;
  read_cnf(solver, fn);
  CAMLreturn0;
}

One thing I wonder about, though, is the line:

  SAT_Manager solver = (void*)mng;

shouldn't you be casting to something other than (void*) here?  Not
that I know anything about how the type SAT_Manager is represented.

John.

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12  3:39 [Caml-list] Toplevel crashes when trying to call external functions John Prevost
@ 2004-07-12  4:46 ` Andy Yang
  2004-07-12 10:55 ` Gerd Stolpmann
  1 sibling, 0 replies; 14+ messages in thread
From: Andy Yang @ 2004-07-12  4:46 UTC (permalink / raw)
  To: John Prevost, ayerkes; +Cc: caml-list


Thanks a lot! You are correct about the pointer
representation in Ocaml. However, just now I tried
your codes, it still crashes. 

Actually, SAT_Manager is a pointer to a C++ class
object. In order to use the C++ library in Ocaml,
firstly a C wrapper is added onto the C++ code( this
part has been tested. Calling from the C wrapper,
these  C++ objects work fine.) Then I tried to call
these wrapper functions from Ocaml. 

One questions: Why do you mention C++ codes here? It
seems that currently Ocaml support onlu C interfaces.

Thanks a lot!

Andy

--- John Prevost <j.prevost@gmail.com> wrote:
> You shouldn't have to package up a non-caml managed
> pointer in any
> sort of caml structure at all.  Take a look at
> section 18.6 of the
> manual, specifically the definitions of
> curses_initscr and
> curses_wrefresh.  Any pointer that's outside the
> caml heap (that is,
> pretty much any pointer that you're getting from a
> non-caml function)
> can simply be treated as opaque, and you can use
> Caml's type system to
> make sure it's valid (as long as the C code always
> handles these
> pointers correctly.)
> 
> I'd write your code like the following, based on
> that:
> 
> Caml code:
> type sat_manager
> external zchaff_InitManager : unit -> sat_manager
> external zchall_ReadCnf : sat_manager -> string ->
> unit
> 
> C++ code:
> value zchaff_InitManager(void)
> {
>   CAMLparam0();
>   CAMLreturn((value) SAT_InitManager());
> }
> 
> void zchaff_ReadCnf(value mng, value filename)
> {
>   CAMLparam2(mng, filename);
>   SAT_Manager solver = (void*)mng;
>   cout<<"solver = "<<hex <<solver <<endl;
>   assert(solver != NULL);  
>   char * fn = String_val(filename);  
>   cout<<"file = "<<fn <<endl;
>   read_cnf(solver, fn);
>   CAMLreturn0;
> }
> 
> One thing I wonder about, though, is the line:
> 
>   SAT_Manager solver = (void*)mng;
> 
> shouldn't you be casting to something other than
> (void*) here?  Not
> that I know anything about how the type SAT_Manager
> is represented.
> 
> John.
> 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12  3:39 [Caml-list] Toplevel crashes when trying to call external functions John Prevost
  2004-07-12  4:46 ` Andy Yang
@ 2004-07-12 10:55 ` Gerd Stolpmann
  2004-07-12 12:03   ` Richard Jones
  1 sibling, 1 reply; 14+ messages in thread
From: Gerd Stolpmann @ 2004-07-12 10:55 UTC (permalink / raw)
  To: John Prevost; +Cc: ayerkes, caml-list

Am Mon, 2004-07-12 um 05.39 schrieb John Prevost:
> You shouldn't have to package up a non-caml managed pointer in any
> sort of caml structure at all.  Take a look at section 18.6 of the
> manual, specifically the definitions of curses_initscr and
> curses_wrefresh.  Any pointer that's outside the caml heap (that is,
> pretty much any pointer that you're getting from a non-caml function)
> can simply be treated as opaque, and you can use Caml's type system to
> make sure it's valid (as long as the C code always handles these
> pointers correctly.)

In principle, this is correct, but there are traps. For example,
consider the case the memory block is freed by the C routine, and the
same block is allocated by the caml runtime. In this case, the pointer,
once outside the caml heap, is now inside, and the GC crashes. Because
of this, I would not recommend this technique. Custom blocks (or int32
blocks) are risk-free in this regard.

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12 10:55 ` Gerd Stolpmann
@ 2004-07-12 12:03   ` Richard Jones
  2004-07-12 12:55     ` Jean-Christophe Filliatre
  0 siblings, 1 reply; 14+ messages in thread
From: Richard Jones @ 2004-07-12 12:03 UTC (permalink / raw)
  Cc: caml-list

I have to agree with Gerd on this.  I've tried to use pointers-
as-values but have had a lot of unexplained strangeness / crashes.  I
now wrap pointers in custom blocks every time.  You can find working
examples of this in both mod_caml and perl4caml sources.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
PTHRLIB is a library for writing small, efficient and fast servers in C.
HTTP, CGI, DBI, lightweight threads: http://www.annexia.org/freeware/pthrlib/

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12 12:03   ` Richard Jones
@ 2004-07-12 12:55     ` Jean-Christophe Filliatre
  0 siblings, 0 replies; 14+ messages in thread
From: Jean-Christophe Filliatre @ 2004-07-12 12:55 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list


Richard Jones writes:
 > I have to agree with Gerd on this.  I've tried to use pointers-
 > as-values but have had a lot of unexplained strangeness / crashes.  I
 > now wrap pointers in custom blocks every time.  You can find working
 > examples of this in both mod_caml and perl4caml sources.

Another example of this technique can be found in David Monniaux's mlgmp
(http://www.di.ens.fr/~monniaux/download/mlgmp.tar.gz)

-- 
Jean-Christophe

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12 13:33         ` Olivier Andrieu
@ 2004-07-12 15:39           ` Andy Yang
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Yang @ 2004-07-12 15:39 UTC (permalink / raw)
  To: Olivier Andrieu; +Cc: caml-list


Hi, all

Thanks a lot! With CAMLreturn(Val_unit), the toplevel
works fine. Here is the workable version:

value zchaff_InitManager(void)
{
  CAMLparam0 ();
  SAT_Manager temp = SAT_InitManager();
  CAMLreturn( (value)temp ); 
}

value zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);  
  cout<<"solver = "<<hex<<(void*)mng<<endl;   
  read_cnf((SAT_Manager)mng, String_val(filename));
  CAMLreturn(Val_unit);
}

Andy


		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12 13:16       ` Andy Yang
@ 2004-07-12 13:33         ` Olivier Andrieu
  2004-07-12 15:39           ` Andy Yang
  0 siblings, 1 reply; 14+ messages in thread
From: Olivier Andrieu @ 2004-07-12 13:33 UTC (permalink / raw)
  To: yyu08; +Cc: caml-list

 Andy Yang [Mon, 12 Jul 2004]:
 > The two functions are defines as follows in ocaml:
 >  
 > type zchaff_solver
 > external zchaff_InitManager : unit -> zchaff_solver = "zchaff_InitManager"
 > external zchaff_ReadCnf : zchaff_solver -> string -> unit = "zchaff_ReadCnf"
 >  
 > and my usage of these two functions are :
 >  
 > # let mysolver = zchaff_InitManager () ;;
 > # let _ = zchaff_ReadCnf (mysolver) ("testcaser/1.cnf");;  
 >
 > > value zchaff_InitManager(void)
 > > {
 > > CAMLparam0();
 > > CAMLlocal1(val);
 > > void* solver = SAT_InitManager();
 > > val = copy_int32((int)solver);
 > > cout<<"solver = "<> CAMLreturn ( val );
 > > }
 > >
 > > void zchaff_ReadCnf(value mng, value filename)
should be:
     value zchaff_ReadCnf(value mng, value filename)

 > > {
 > > CAMLparam2(mng, filename);
 > > SAT_Manager solver = (void*)Int32_val(mng);
 > > cout<<"solver = "<> assert(solver != NULL);
 > > char * fn = String_val(filename);
 > > cout<<"file = "<> read_cnf(solver, fn);
 > > CAMLreturn0;
should be:
     CAMLreturn(Val_unit);
 > > }

Also, you should use an abstract block for storing the pointer:

in _InitManager :
 val = alloc_small(1, Abstract_tag);
 Field(val, 0) = Val_bp(solver);

in _ReadCnf:
 solver = (SAT_Manager) Bp_val(mng);

-- 
   Olivier

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-12 11:35     ` Damien Doligez
@ 2004-07-12 13:16       ` Andy Yang
  2004-07-12 13:33         ` Olivier Andrieu
  0 siblings, 1 reply; 14+ messages in thread
From: Andy Yang @ 2004-07-12 13:16 UTC (permalink / raw)
  To: caml-list Caml

[-- Attachment #1: Type: text/plain, Size: 1540 bytes --]

Hi, 
 
The two functions are defines as follows in ocaml:
 
type zchaff_solver
external zchaff_InitManager : unit -> zchaff_solver = "zchaff_InitManager"
external zchaff_ReadCnf : zchaff_solver -> string -> unit = "zchaff_ReadCnf"
 
and my usage of these two functions are :
 
# let mysolver = zchaff_InitManager () ;;
# let _ = zchaff_ReadCnf (mysolver) ("testcaser/1.cnf");;  
   
Thanks !
 
Andy
Damien Doligez <damien.doligez@inria.fr> wrote:
On Jul 11, 2004, at 20:58, Andy Yang wrote:

> value zchaff_InitManager(void)
> {
> CAMLparam0();
> CAMLlocal1(val);
> void* solver = SAT_InitManager();
> val = copy_int32((int)solver);
> cout<<"solver = "<> CAMLreturn ( val );
> }
>
> void zchaff_ReadCnf(value mng, value filename)
> {
> CAMLparam2(mng, filename);
> SAT_Manager solver = (void*)Int32_val(mng);
> cout<<"solver = "<> assert(solver != NULL);
> char * fn = String_val(filename);
> cout<<"file = "<> read_cnf(solver, fn);
> CAMLreturn0;
> }

This code looks OK. How did you declare these two functions on
the OCaml side of things?

> Thus I should follows ocaml's Tag rules. However, the
> problem still exists. Tracing with gdb, I noticed that
> the toplevel still crashes in function obj_tag.

Sounds like the toplevel is trying to pretty-print one of your
values.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs 
		
---------------------------------
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!

[-- Attachment #2: Type: text/html, Size: 2275 bytes --]

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

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-11 18:58   ` Andy Yang
  2004-07-12 11:04     ` Gerd Stolpmann
@ 2004-07-12 11:35     ` Damien Doligez
  2004-07-12 13:16       ` Andy Yang
  1 sibling, 1 reply; 14+ messages in thread
From: Damien Doligez @ 2004-07-12 11:35 UTC (permalink / raw)
  To: caml-list Caml

On Jul 11, 2004, at 20:58, Andy Yang wrote:

> value zchaff_InitManager(void)
> {
>   CAMLparam0();
>   CAMLlocal1(val);
>   void* solver = SAT_InitManager();
>   val = copy_int32((int)solver);
>   cout<<"solver = "<<hex<<solver<<endl;
>   CAMLreturn ( val );
> }
>
> void zchaff_ReadCnf(value mng, value filename)
> {
>   CAMLparam2(mng, filename);
>   SAT_Manager solver = (void*)Int32_val(mng);
>   cout<<"solver = "<<hex <<solver <<endl;
>   assert(solver != NULL);
>   char * fn = String_val(filename);
>   cout<<"file = "<<fn <<endl;
>   read_cnf(solver, fn);
>   CAMLreturn0;
> }

This code looks OK.  How did you declare these two functions on
the OCaml side of things?

> Thus I should follows ocaml's Tag rules. However, the
> problem still exists. Tracing with gdb, I noticed that
> the toplevel still crashes in function obj_tag.

Sounds like the toplevel is trying to pretty-print one of your
values.

-- Damien

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-11 18:58   ` Andy Yang
@ 2004-07-12 11:04     ` Gerd Stolpmann
  2004-07-12 11:35     ` Damien Doligez
  1 sibling, 0 replies; 14+ messages in thread
From: Gerd Stolpmann @ 2004-07-12 11:04 UTC (permalink / raw)
  To: Andy Yang; +Cc: art yerkes, caml-list

Of course, the problem can be anywhere, we don't see the code you are
calling. The stubs look reasonable, and it is very likely that your
problem is an unlucky interference with the C++ part.

Am Son, 2004-07-11 um 20.58 schrieb Andy Yang:
> Hi, 
> 
> Since at this point, I am not concerning much about
> codes' portability, so I just assume the program run 
> on x86 machines, on which 32 bits pointers are
> allowed.I modified code as follows:
> 
> value zchaff_InitManager(void)
> {
>   CAMLparam0();  
>   CAMLlocal1(val);
>   void* solver = SAT_InitManager();
>   val = copy_int32((int)solver);
>   cout<<"solver = "<<hex<<solver<<endl;
>   CAMLreturn ( val );
> }
> 
> void zchaff_ReadCnf(value mng, value filename)
> {
>   CAMLparam2(mng, filename);
>   SAT_Manager solver = (void*)Int32_val(mng);
>   cout<<"solver = "<<hex <<solver <<endl;
>   assert(solver != NULL);  
>   char * fn = String_val(filename);  

For example, this line can be problematic if fn is modified by read_cnf.

>   cout<<"file = "<<fn <<endl;
>   read_cnf(solver, fn);
>   CAMLreturn0;
> }
> 
> 
> Thus I should follows ocaml's Tag rules. However, the
> problem still exists. Tracing with gdb, I noticed that
> the toplevel still crashes in function obj_tag.
> However, I cannot find the obj_tag 's sourcecode in
> Ocaml's source. There is only a mapping table in
> ocaml_dir/byterun/prims.c, in which obj_tag is one of
> the primitives.
> 
> Thanks a lot!
> 
> Andy
> 
> > 
> > > Hi, all
> > > 
> > > I am relatively new to Ocaml. Sorry about the spam
> > if
> > > this is a trivial problem. I am trying to give
> > call
> > > some external functions. 
> > > 
> > > Some codes are as follows:
> > > 
> > > value zchaff_InitManager(void)
> > > {
> > >   CAMLparam0();  
> > >   void * solver = SAT_InitManager();
> > >   value val = alloc(1, Custom_tag);
> > >   Int32_val(val) = (int) solver;
> > >   CAMLreturn ( val );
> > > }
> > > 
> > > }
> > > 
> > 
> > These may not solve this specific problem but I
> > think they are good
> > advice:
> > 
> > First:
> > 17.9.2. Custom blocks must be allocated via the
> > alloc_custom function.
> > (And remember that the custom block size argument is
> > in bytes).
> > 
> > Second:
> > Passing 1 as the block size to alloc allocates a
> > block with one word
> > as a tail (at offset 0), but Data_custom_val
> > accesses a value in the
> > tail at offset 1.  This is probably the error you're
> > looking for.
> > 
> > Third:
> > I don't understand why you're going through
> > Int32_val and Val_int32
> > to get and set your pointer.  You probably want to
> > write a new macro
> > that will access the custom area as a pointer-sized
> > chunk on every
> > architecture.  Assuming pointers to be 32-bits
> > really is a bad idea.
> > -- 
> > Hey, Adam Smith, keep your invisible hands to
> > yourself!
> > 
> 
> 
> 
> 
> 
> 		
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail is new and improved - Check it out!
> http://promotions.yahoo.com/new_mail
> 
> -------------------
> 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
> 
> 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-11  1:20 Andy Yang
  2004-07-11 18:14 ` art yerkes
@ 2004-07-12  6:55 ` Anne Pacalet
  1 sibling, 0 replies; 14+ messages in thread
From: Anne Pacalet @ 2004-07-12  6:55 UTC (permalink / raw)
  To: caml-list

Andy Yang a écrit :
> I am relatively new to Ocaml. 

I am VERY new to Ocaml...

> When I call these functions in a .ml file. It is okay.
> .However, when I tried to call these functions
> interactively, segmentation fault happens.

and I have to same problem with even the simplest functions.
For instance :

extern "C" void Caml_load_project(value v_0){
   CAMLparam1(v_0);
   String x_0 = String_val (v_0);
   C_load_project (x_0);
   CAMLreturn0;
}
extern "C" value Caml_show_refs(value v_0, value v_1){
   CAMLparam2(v_0, v_1);
   CAMLlocal1 (v_res);
   int x_0 = Int_val (v_0);
   int x_1 = Int_val (v_1);
   std::list<int> res = C_show_refs (x_0, x_1);
   v_res = Val_listInt (res);
   CAMLreturn (v_res);
}

And I noticed that if I add "flush" or "print" calls, it works.
So, I did :

external c_load_project : string -> unit = "Caml_load_project"
external c_show_refs : int -> int -> int list = "Caml_show_refs"

let load_project a_0 =
   c_load_project a_0 ; flush_all()
;;
let show_refs a_0 a_1 =
   let r = c_show_refs a_0 a_1 in flush_all() ; r
;;

and the functions 'load_project' and 'show_refs' work fine even 
interactively. I don't know why (I would be happy to know)... but maybe 
this workaround can help !

Bye,
Anne.

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-11 18:14 ` art yerkes
@ 2004-07-11 18:58   ` Andy Yang
  2004-07-12 11:04     ` Gerd Stolpmann
  2004-07-12 11:35     ` Damien Doligez
  0 siblings, 2 replies; 14+ messages in thread
From: Andy Yang @ 2004-07-11 18:58 UTC (permalink / raw)
  To: art yerkes; +Cc: caml-list

Hi, 

Since at this point, I am not concerning much about
codes' portability, so I just assume the program run 
on x86 machines, on which 32 bits pointers are
allowed.I modified code as follows:

value zchaff_InitManager(void)
{
  CAMLparam0();  
  CAMLlocal1(val);
  void* solver = SAT_InitManager();
  val = copy_int32((int)solver);
  cout<<"solver = "<<hex<<solver<<endl;
  CAMLreturn ( val );
}

void zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);
  SAT_Manager solver = (void*)Int32_val(mng);
  cout<<"solver = "<<hex <<solver <<endl;
  assert(solver != NULL);  
  char * fn = String_val(filename);  
  cout<<"file = "<<fn <<endl;
  read_cnf(solver, fn);
  CAMLreturn0;
}


Thus I should follows ocaml's Tag rules. However, the
problem still exists. Tracing with gdb, I noticed that
the toplevel still crashes in function obj_tag.
However, I cannot find the obj_tag 's sourcecode in
Ocaml's source. There is only a mapping table in
ocaml_dir/byterun/prims.c, in which obj_tag is one of
the primitives.

Thanks a lot!

Andy

> 
> > Hi, all
> > 
> > I am relatively new to Ocaml. Sorry about the spam
> if
> > this is a trivial problem. I am trying to give
> call
> > some external functions. 
> > 
> > Some codes are as follows:
> > 
> > value zchaff_InitManager(void)
> > {
> >   CAMLparam0();  
> >   void * solver = SAT_InitManager();
> >   value val = alloc(1, Custom_tag);
> >   Int32_val(val) = (int) solver;
> >   CAMLreturn ( val );
> > }
> > 
> > }
> > 
> 
> These may not solve this specific problem but I
> think they are good
> advice:
> 
> First:
> 17.9.2. Custom blocks must be allocated via the
> alloc_custom function.
> (And remember that the custom block size argument is
> in bytes).
> 
> Second:
> Passing 1 as the block size to alloc allocates a
> block with one word
> as a tail (at offset 0), but Data_custom_val
> accesses a value in the
> tail at offset 1.  This is probably the error you're
> looking for.
> 
> Third:
> I don't understand why you're going through
> Int32_val and Val_int32
> to get and set your pointer.  You probably want to
> write a new macro
> that will access the custom area as a pointer-sized
> chunk on every
> architecture.  Assuming pointers to be 32-bits
> really is a bad idea.
> -- 
> Hey, Adam Smith, keep your invisible hands to
> yourself!
> 





		
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

-------------------
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] 14+ messages in thread

* Re: [Caml-list] Toplevel crashes when trying to call external functions
  2004-07-11  1:20 Andy Yang
@ 2004-07-11 18:14 ` art yerkes
  2004-07-11 18:58   ` Andy Yang
  2004-07-12  6:55 ` Anne Pacalet
  1 sibling, 1 reply; 14+ messages in thread
From: art yerkes @ 2004-07-11 18:14 UTC (permalink / raw)
  To: Andy Yang; +Cc: caml-list

On Sat, 10 Jul 2004 18:20:47 -0700 (PDT)
Andy Yang <yyu08@yahoo.com> wrote:

> Hi, all
> 
> I am relatively new to Ocaml. Sorry about the spam if
> this is a trivial problem. I am trying to give call
> some external functions. 
> 
> Some codes are as follows:
> 
> value zchaff_InitManager(void)
> {
>   CAMLparam0();  
>   void * solver = SAT_InitManager();
>   value val = alloc(1, Custom_tag);
>   Int32_val(val) = (int) solver;
>   CAMLreturn ( val );
> }
> 
> void zchaff_ReadCnf(value mng, value filename)
> {
>   CAMLparam2(mng, filename);
>   void* solver = (void*)Int32_val(mng);
>   char * fn = String_val(filename);
>   read_cnf(solver, fn);  
>   CAMLreturn0;
> }
> 

These may not solve this specific problem but I think they are good
advice:

First:
17.9.2. Custom blocks must be allocated via the alloc_custom function.
(And remember that the custom block size argument is in bytes).

Second:
Passing 1 as the block size to alloc allocates a block with one word
as a tail (at offset 0), but Data_custom_val accesses a value in the
tail at offset 1.  This is probably the error you're looking for.

Third:
I don't understand why you're going through Int32_val and Val_int32
to get and set your pointer.  You probably want to write a new macro
that will access the custom area as a pointer-sized chunk on every
architecture.  Assuming pointers to be 32-bits really is a bad idea.
-- 
Hey, Adam Smith, keep your invisible hands to yourself!

-------------------
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] 14+ messages in thread

* [Caml-list] Toplevel crashes when trying to call external functions
@ 2004-07-11  1:20 Andy Yang
  2004-07-11 18:14 ` art yerkes
  2004-07-12  6:55 ` Anne Pacalet
  0 siblings, 2 replies; 14+ messages in thread
From: Andy Yang @ 2004-07-11  1:20 UTC (permalink / raw)
  To: caml-list

Hi, all

I am relatively new to Ocaml. Sorry about the spam if
this is a trivial problem. I am trying to give call
some external functions. 

Some codes are as follows:

value zchaff_InitManager(void)
{
  CAMLparam0();  
  void * solver = SAT_InitManager();
  value val = alloc(1, Custom_tag);
  Int32_val(val) = (int) solver;
  CAMLreturn ( val );
}

void zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);
  void* solver = (void*)Int32_val(mng);
  char * fn = String_val(filename);
  read_cnf(solver, fn);  
  CAMLreturn0;
}

When I call these functions in a .ml file. It is okay.
.However, when I tried to call these functions
interactively, segmentation fault happens. The
interactive way is as follows:

# let solver = zchaff_InitManager () ;;
solver = 0x8092328
val solver : zchaff_solver = <abstr>
# Gc.full_major();;
- : unit = ()
# solver ;;
- : zchaff_solver = <abstr>
# let _ = zchaff_ReadCnf solver "testcase/4.cnf";;
solver = 0x400
file = testcase/4.cnf
Segmentation fault

It seems that the variable "solver" is not registered
in Gc's root tree. So solver's value changes after
Gc.full_major(). Where are wrong in my codes? Tracing
with gdb, the stack is as follows when segmentation
fault happens.

#0  0x0807120f in obj_tag ()
#1  0x08074c8f in interprete ()
#2  0x08075dbe in caml_main ()
#3  0x08067a9c in main ()

Thanks a lot!

Andy





		
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

-------------------
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] 14+ messages in thread

end of thread, other threads:[~2004-07-12 15:39 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-12  3:39 [Caml-list] Toplevel crashes when trying to call external functions John Prevost
2004-07-12  4:46 ` Andy Yang
2004-07-12 10:55 ` Gerd Stolpmann
2004-07-12 12:03   ` Richard Jones
2004-07-12 12:55     ` Jean-Christophe Filliatre
  -- strict thread matches above, loose matches on Subject: below --
2004-07-11  1:20 Andy Yang
2004-07-11 18:14 ` art yerkes
2004-07-11 18:58   ` Andy Yang
2004-07-12 11:04     ` Gerd Stolpmann
2004-07-12 11:35     ` Damien Doligez
2004-07-12 13:16       ` Andy Yang
2004-07-12 13:33         ` Olivier Andrieu
2004-07-12 15:39           ` Andy Yang
2004-07-12  6:55 ` Anne Pacalet

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).