* Caml interface with C on x86_64
@ 2006-03-08 22:06 Li-Thiao-Té Sébastien
2006-03-13 14:53 ` Allocating caml lists from C : possible bug on amd64 "Sayan (Sébastien Li-Thiao-Té)"
0 siblings, 1 reply; 8+ messages in thread
From: Li-Thiao-Té Sébastien @ 2006-03-08 22:06 UTC (permalink / raw)
To: caml-list
Hi,
I am using OCaml on two different Debian systems, a P4 machine at home
and an Opteron machine at work. The following C function works well on
the P4 but crashes on the Opteron with "Signal -10" :
value test_liste (value str) {
// test function to return a list to caml
CAMLparam1(str);
CAMLlocal1(cons);
printf("This is test_liste.\n");
printf(String_val(str));
fflush(stdout);
cons = caml_alloc (2,0);
Store_field (cons, 0, Val_int(3));
Store_field (cons, 1, Val_int(0));
if (Is_block(cons)) { printf("true\n");};
fflush(stdout);
printf("cons has size %i \n",Wosize_val(cons));
fflush(stdout);
CAMLreturn (Val_int(0));
}
called from a custom toplevel system :
external test_liste : string -> int list = "test_liste";;
test_liste "oaue";;
The function works with gcc-3.4 and gcc-4.0 on the P4 but crash with
both on the opteron machine. Can anybody explain this error?
P4 configuration :
uname -a
Linux 2.6.15-1-686-smp #1 SMP Tue Jan 3 10:19:10 UTC 2006 i686 GNU/Linux
ocamlc -v
The Objective Caml compiler, version 3.09.1
gcc-3.4 -v
gcc version 3.4.6 20060302 (prerelease) (Debian 3.4.5-3)
gcc -v
Target: i486-linux-gnu
gcc version 4.0.3 20060304 (prerelease) (Debian 4.0.2-10)
Opteron configuration :
uname -a
Linux 2.6.14-2-amd64-k8-smp #1 SMP Sun Nov 27 02:29:11 UTC 2005 x86_64
GNU/Linux
ocamlc -v
The Objective Caml compiler, version 3.09.1
gcc-3.4 -v
gcc version 3.4.6 20060302 (prerelease) (Debian 3.4.5-3)
gcc -v
Target: x86_64-linux-gnu
gcc version 4.0.3 20060304 (prerelease) (Debian 4.0.2-10)
--
Li-Thiao-Té Sébastien
^ permalink raw reply [flat|nested] 8+ messages in thread
* Allocating caml lists from C : possible bug on amd64
2006-03-08 22:06 Caml interface with C on x86_64 Li-Thiao-Té Sébastien
@ 2006-03-13 14:53 ` "Sayan (Sébastien Li-Thiao-Té)"
2006-03-13 15:13 ` [Caml-list] " Markus Mottl
0 siblings, 1 reply; 8+ messages in thread
From: "Sayan (Sébastien Li-Thiao-Té)" @ 2006-03-13 14:53 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1244 bytes --]
Hi list,
I am trying to learn how to allocate a list in C and pass the result to
Caml on an opteron Debian box. Here is the function that I use :
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/bigarray.h>
value test_liste (value str) {
// test function to return a list to caml
value cons;
cons = caml_alloc_small (2,0);
caml_modify(&Field(cons,0),Val_int(0));
caml_modify(&Field(cons,1),Val_int(0));
printf("This is test_liste.\n");
printf(String_val(str));
fflush(stdout);
if (Is_block(cons)) { printf("true\n");};
fflush(stdout);
printf("cons has size %i \n",Wosize_val(cons));
fflush(stdout);
return (cons);
}
I call it from (test_liste.ml) :
external test_liste : string -> int list = "test_liste";;
test_liste "oaue";;
Makefile :
gcc -c -I/usr/lib64/ocaml readPeaks.c
ocamlc -custom readPeaks.o test_liste.ml -o a.out
The a.out file is compiled without problems but it crashes with a
segfault. However, if I compile on the same machine in a 32-bit chroot,
the program works.
Can anyone reproduce this behavior? Is that a bug linked to the 64bit
implementation of Ocaml or is it a gcc bug?
--
Li-Thiao-Té Sébastien
[-- Attachment #2: Makefile --]
[-- Type: text/plain, Size: 187 bytes --]
OUTPUT = a.out
all : top
echo "Make finished."
clean :
rm *.cm* *.o $(OUTPUT)
top :
gcc -c -I/usr/lib64/ocaml readPeaks.c
ocamlc -custom readPeaks.o test_liste.ml -o $(OUTPUT)
[-- Attachment #3: readPeaks.c --]
[-- Type: text/x-csrc, Size: 552 bytes --]
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/bigarray.h>
value test_liste (value str) {
// test function to return a list to caml
value cons;
cons = caml_alloc_small (2,0);
caml_modify(&Field(cons,0),Val_int(0));
caml_modify(&Field(cons,1),Val_int(0));
printf("This is test_liste.\n");
printf(String_val(str));
fflush(stdout);
if (Is_block(cons)) { printf("true\n");};
fflush(stdout);
printf("cons has size %i \n",Wosize_val(cons));
fflush(stdout);
return (cons);
}
[-- Attachment #4: test_liste.ml --]
[-- Type: text/plain, Size: 79 bytes --]
external test_liste : string -> int list = "test_liste";;
test_liste "oaue";;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 14:53 ` Allocating caml lists from C : possible bug on amd64 "Sayan (Sébastien Li-Thiao-Té)"
@ 2006-03-13 15:13 ` Markus Mottl
2006-03-13 15:54 ` "Sayan (Sébastien Li-Thiao-Té)"
0 siblings, 1 reply; 8+ messages in thread
From: Markus Mottl @ 2006-03-13 15:13 UTC (permalink / raw)
To: Sayan (Sébastien Li-Thiao-Té); +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 649 bytes --]
On 3/13/06, "Sayan (Sébastien Li-Thiao-Té)" <sayan@crans.org> wrote:
>
> I am trying to learn how to allocate a list in C and pass the result to
> Caml on an opteron Debian box. Here is the function that I use :
Your function does not protect "str" from being reclaimed by the GC (which
can happen in "caml_alloc_small"), and you should use the Field-macro only
to overwrite the contents of the cons-block in this particular case, because
you had used "caml_alloc_small" as allocation function, and there was no
intermediate allocation.
Regards,
Markus
--
Markus Mottl http://www.ocaml.info markus.mottl@gmail.com
[-- Attachment #2: Type: text/html, Size: 1130 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 15:13 ` [Caml-list] " Markus Mottl
@ 2006-03-13 15:54 ` "Sayan (Sébastien Li-Thiao-Té)"
2006-03-13 16:39 ` Markus Mottl
2006-03-13 17:42 ` Xavier Leroy
0 siblings, 2 replies; 8+ messages in thread
From: "Sayan (Sébastien Li-Thiao-Té)" @ 2006-03-13 15:54 UTC (permalink / raw)
To: caml-list
Markus Mottl wrote:
> On 3/13/06, *"Sayan (Sébastien Li-Thiao-Té)"* <sayan@crans.org
> <mailto:sayan@crans.org>> wrote:
>
> I am trying to learn how to allocate a list in C and pass the result to
> Caml on an opteron Debian box. Here is the function that I use :
>
>
> Your function does not protect "str" from being reclaimed by the GC
> (which can happen in "caml_alloc_small"), and you should use the
> Field-macro only to overwrite the contents of the cons-block in this
> particular case, because you had used "caml_alloc_small" as allocation
> function, and there was no intermediate allocation.
>
I have already tried to be GC-friendly, and use the "standard" way to do
things. For example the following function also works using a 32-bit
chroot, but also fails with the 64-bit compiler. The question is : why
does it fail with the 64-bit compiler?
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/bigarray.h>
test_liste (value str) {
// test function to return a list to caml
// value cons;
// cons = caml_alloc_small (2,0);
// caml_modify(&Field(cons,0),Val_int(0));
// caml_modify(&Field(cons,1),Val_int(0));
CAMLparam1(str);
CAMLlocal1(cons);
cons = caml_alloc (2,0);
Store_field(cons,0, Val_int(1));
Store_field(cons,1, Val_int(0));
printf("This is test_liste.\n");
printf(String_val(str));
fflush(stdout);
if (Is_block(cons)) { printf("true\n");};
fflush(stdout);
printf("cons has size %i \n",Wosize_val(cons));
fflush(stdout);
CAMLreturn (cons);
}
--
Li-Thiao-Té Sébastien
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 15:54 ` "Sayan (Sébastien Li-Thiao-Té)"
@ 2006-03-13 16:39 ` Markus Mottl
2006-03-13 17:42 ` Xavier Leroy
1 sibling, 0 replies; 8+ messages in thread
From: Markus Mottl @ 2006-03-13 16:39 UTC (permalink / raw)
To: Sayan (Sébastien Li-Thiao-Té); +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 733 bytes --]
On 3/13/06, "Sayan (Sébastien Li-Thiao-Té)" <sayan@crans.org> wrote:
>
> I have already tried to be GC-friendly, and use the "standard" way to do
> things.
Well, you shouldn't expect the GC to become more friendly towards you when
you become less friendly towards the GC ;-)
For example the following function also works using a 32-bit
> chroot, but also fails with the 64-bit compiler. The question is : why
> does it fail with the 64-bit compiler?
Obviously because of a bug: the allocation functions return negative values
on 64-bit machines. You should submit a bug report here:
http://www.ocaml.org/mantis
Regards,
Markus
--
Markus Mottl http://www.ocaml.info markus.mottl@gmail.com
[-- Attachment #2: Type: text/html, Size: 1626 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 15:54 ` "Sayan (Sébastien Li-Thiao-Té)"
2006-03-13 16:39 ` Markus Mottl
@ 2006-03-13 17:42 ` Xavier Leroy
2006-03-13 18:05 ` Li-Thiao-Té Sébastien
2006-03-13 18:53 ` Markus Mottl
1 sibling, 2 replies; 8+ messages in thread
From: Xavier Leroy @ 2006-03-13 17:42 UTC (permalink / raw)
To: "Sayan (Sébastien Li-Thiao-Té)"; +Cc: caml-list
> I have already tried to be GC-friendly, and use the "standard" way to do
> things. For example the following function also works using a 32-bit
> chroot, but also fails with the 64-bit compiler. The question is : why
> does it fail with the 64-bit compiler?
Because your C code is wrong. gcc -Wall is your friend.
> #include <stdio.h>
> #include <caml/mlvalues.h>
> #include <caml/memory.h>
> #include <caml/bigarray.h>
>
> test_liste (value str) {
Should be "value test_list(value str)".
> // test function to return a list to caml
> // value cons;
> // cons = caml_alloc_small (2,0);
> // caml_modify(&Field(cons,0),Val_int(0));
> // caml_modify(&Field(cons,1),Val_int(0));
>
> CAMLparam1(str);
> CAMLlocal1(cons);
> cons = caml_alloc (2,0);
You haven't declared caml_alloc (include <caml/alloc.h>), so the C
compiler assumes it returns an int instead of a value, and generates
wrong code.
> Store_field(cons,0, Val_int(1));
> Store_field(cons,1, Val_int(0));
>
> printf("This is test_liste.\n");
> printf(String_val(str));
Should be: printf("%s", String_val(str));
(Hint: what happens if str contains "%s" ?)
> fflush(stdout);
>
> if (Is_block(cons)) { printf("true\n");};
> fflush(stdout);
> printf("cons has size %i \n",Wosize_val(cons));
> fflush(stdout);
> CAMLreturn (cons);
> }
>
- Xavier Leroy
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 17:42 ` Xavier Leroy
@ 2006-03-13 18:05 ` Li-Thiao-Té Sébastien
2006-03-13 18:53 ` Markus Mottl
1 sibling, 0 replies; 8+ messages in thread
From: Li-Thiao-Té Sébastien @ 2006-03-13 18:05 UTC (permalink / raw)
To: caml-list
Xavier Leroy wrote:
>>I have already tried to be GC-friendly, and use the "standard" way to do
>>things. For example the following function also works using a 32-bit
>>chroot, but also fails with the 64-bit compiler. The question is : why
>>does it fail with the 64-bit compiler?
>
> Because your C code is wrong. gcc -Wall is your friend.
>
>>#include <stdio.h>
>>#include <caml/mlvalues.h>
>>#include <caml/memory.h>
>>#include <caml/bigarray.h>
>>
>>test_liste (value str) {
>
> Should be "value test_list(value str)".
>
Indeed, but my previous mail to this list read "value test_liste (value
str)". No problem noticed without it.
>> // test function to return a list to caml
>>// value cons;
>>// cons = caml_alloc_small (2,0);
>>// caml_modify(&Field(cons,0),Val_int(0));
>>// caml_modify(&Field(cons,1),Val_int(0));
>>
>> CAMLparam1(str);
>> CAMLlocal1(cons);
>> cons = caml_alloc (2,0);
>
> You haven't declared caml_alloc (include <caml/alloc.h>), so the C
> compiler assumes it returns an int instead of a value, and generates
> wrong code.
>
That was the main problem.
>> Store_field(cons,0, Val_int(1));
>> Store_field(cons,1, Val_int(0));
>>
>> printf("This is test_liste.\n");
>> printf(String_val(str));
>
> Should be: printf("%s", String_val(str));
>
> (Hint: what happens if str contains "%s" ?)
>
Well, that one was rather a non-production-ready test anyway. I was
blocked before even reaching that point.
Thanks very much for the help and the -Wall flag. I should have known :)
--
Li-Thiao-Té Sébastien
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] Allocating caml lists from C : possible bug on amd64
2006-03-13 17:42 ` Xavier Leroy
2006-03-13 18:05 ` Li-Thiao-Té Sébastien
@ 2006-03-13 18:53 ` Markus Mottl
1 sibling, 0 replies; 8+ messages in thread
From: Markus Mottl @ 2006-03-13 18:53 UTC (permalink / raw)
To: Xavier Leroy; +Cc: "Sayan (Sébastien Li-Thiao-Té)", caml-list
[-- Attachment #1: Type: text/plain, Size: 618 bytes --]
On 3/13/06, Xavier Leroy <Xavier.Leroy@inria.fr> wrote:
>
> You haven't declared caml_alloc (include <caml/alloc.h>), so the C
> compiler assumes it returns an int instead of a value, and generates
> wrong code.
Funny that I didn't spot this one. Good to know that one should look out
for this problem when making sure that code remains portable to 64-bit
machines.
I think C-compilers should by default issue a warning on platforms where the
size of int may be different from the size of a pointer instead of silently
inventing a function prototype that is very likely to crash.
Regards,
Markus
[-- Attachment #2: Type: text/html, Size: 927 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2006-03-13 18:53 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-08 22:06 Caml interface with C on x86_64 Li-Thiao-Té Sébastien
2006-03-13 14:53 ` Allocating caml lists from C : possible bug on amd64 "Sayan (Sébastien Li-Thiao-Té)"
2006-03-13 15:13 ` [Caml-list] " Markus Mottl
2006-03-13 15:54 ` "Sayan (Sébastien Li-Thiao-Té)"
2006-03-13 16:39 ` Markus Mottl
2006-03-13 17:42 ` Xavier Leroy
2006-03-13 18:05 ` Li-Thiao-Té Sébastien
2006-03-13 18:53 ` Markus Mottl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox