caml-list - the Caml user's mailing list
 help / Atom feed
* [Caml-list] Global roots
@ 2020-12-20 19:57 christophe
  2020-12-20 22:23 ` christophe
  2020-12-21 18:38 ` Xavier Leroy
  0 siblings, 2 replies; 7+ messages in thread
From: christophe @ 2020-12-20 19:57 UTC (permalink / raw)
  To: caml-list

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

Hello, 

May global roots be initialized to (value)NULL ?

I have a multithreaded program, which segfault in callback_asm, while I think I respect all rules of c interfaces... Except maybe the above...

Chevets,
Christophe
-- 
Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.

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

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

* Re: [Caml-list] Global roots
  2020-12-20 19:57 [Caml-list] Global roots christophe
@ 2020-12-20 22:23 ` christophe
  2020-12-21 18:38 ` Xavier Leroy
  1 sibling, 0 replies; 7+ messages in thread
From: christophe @ 2020-12-20 22:23 UTC (permalink / raw)
  To: caml-list



Le 20 décembre 2020 09:56:13 GMT-10:00, christophe@raffalli.eu a écrit :
>Hello, 
>
>May global roots be initialized to (value)NULL ?

And, of course, i do not call NULL!

>
>I have a multithreaded program, which segfault in callback_asm, while I
>think I respect all rules of c interfaces... Except maybe the above...
>
>Chevets,
>Christophe

-- 
Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.

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

* Re: [Caml-list] Global roots
  2020-12-20 19:57 [Caml-list] Global roots christophe
  2020-12-20 22:23 ` christophe
@ 2020-12-21 18:38 ` Xavier Leroy
  2020-12-21 20:40   ` Christophe Raffalli
  1 sibling, 1 reply; 7+ messages in thread
From: Xavier Leroy @ 2020-12-21 18:38 UTC (permalink / raw)
  To: christophe; +Cc: caml users

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

On Sun, Dec 20, 2020 at 8:57 PM <christophe@raffalli.eu> wrote:

> Hello,
>
> May global roots be initialized to (value)NULL ?
>

Yes, in the default OCaml configuration as of today.

No, in the "no naked pointers" mode that was introduced in OCaml 4.02.0,
made much stricter in OCaml 4.12.0, and will be the default in OCaml 5.0.

To be future-proof and on the safe side, I would suggest to initialize to
e.g. Val_unit.


> I have a multithreaded program, which segfault in callback_asm, while I
> think I respect all rules of c interfaces... Except maybe the above...
>

There is an OPAM compiler switch "4.10.0+nnpcheck" that adds run-time
checks for ill-formed values according to the no-naked-pointers rules.
Maybe you could use it to rule out this potential problem.

Hope this helps,

- Xavier Leroy


> Chevets,
> Christophe
> --
> Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma
> brièveté.

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

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

* Re: [Caml-list] Global roots
  2020-12-21 18:38 ` Xavier Leroy
@ 2020-12-21 20:40   ` Christophe Raffalli
  2020-12-21 21:03     ` Christophe Raffalli
  0 siblings, 1 reply; 7+ messages in thread
From: Christophe Raffalli @ 2020-12-21 20:40 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: caml users

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

Le 20-12-21 19:37:54, Xavier Leroy a écrit :
> On Sun, Dec 20, 2020 at 8:57 PM <christophe@raffalli.eu> wrote:
>
>     Hello,
>
>     May global roots be initialized to (value)NULL ?
>
>
> Yes, in the default OCaml configuration as of today.
>
> No, in the "no naked pointers" mode that was introduced in OCaml 4.02.0, made
> much stricter in OCaml 4.12.0, and will be the default in OCaml 5.0.
>
> To be future-proof and on the safe side, I would suggest to initialize to e.g.
> Val_unit.
>

Hello,

Thanks for the help.

I took a stronger path and initialized to (fun _ -> assert false).

I added an issue in github for improving the documentation for typing constraints
in the foreign C interface for OCaml.

It seems I have no more segfault (not sure yet as the segfault was no reproductible).
This is strange because I am using 4.11.1+flambda and the documentation says that
using out of heap pointer (i.e. NULL ?) is depreacated for 4.11, but should work?

Christophe

>
>     I have a multithreaded program, which segfault in callback_asm, while I
>     think I respect all rules of c interfaces... Except maybe the above...
>
>
> There is an OPAM compiler switch "4.10.0+nnpcheck" that adds run-time checks
> for ill-formed values according to the no-naked-pointers rules.  Maybe you
> could use it to rule out this potential problem.
>
> Hope this helps,
>
> - Xavier Leroy
>
>
>
>     Chevets,
>     Christophe
>     --
>     Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma
>     brièveté.
>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [Caml-list] Global roots
  2020-12-21 20:40   ` Christophe Raffalli
@ 2020-12-21 21:03     ` Christophe Raffalli
  2020-12-21 22:33       ` ygrek
  0 siblings, 1 reply; 7+ messages in thread
From: Christophe Raffalli @ 2020-12-21 21:03 UTC (permalink / raw)
  To: Xavier Leroy, caml users

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

>
> Thanks for the help.
>
> I took a stronger path and initialized to (fun _ -> assert false).
>
> It seems I have no more segfault (not sure yet as the segfault was no reproductible).


In fact the segfault it still there, takes many run to observe it ...
It is still in caml_callback_asm, meaning the global root pointing
to the OCaml function has been corrupted.

Note: I use caml_register_generational_root and
caml_modify_generational_root to register and modify
the pointer to the callback.

I give my code doing the callback, following some
example I found on the net, in case someone has a comment/hint.
May be the order between Camlparam and acquire runtime is wrong ?

This code reaquires the runtime lock, which is released only by the X event loop
that performs the callback; This means that the same C thread release and acquire the
global runtime lock. I have only two threads.


--------------------------------------------------------------------
static void protect_callback(char *name, value f, value v1)
{
  caml_acquire_runtime_system();
  {
    CAMLparam2(f,v1);
    CAMLlocal1(res);
    res = caml_callback_exn(f, v1);
    if(Is_exception_result(res))
      fprintf(stderr, "Egl.main_loop: "
	      "WARNING: %s raised an exception\n", name) ;
    CAMLdrop;
  }
  caml_release_runtime_system();
}

static void protect_callback2(char *name, value f, value v1, value v2)
{
  caml_acquire_runtime_system();
  {
    CAMLparam3(f,v1,v2);
    CAMLlocal1(res);
    res = caml_callback2_exn(f, v1, v2);
    if(Is_exception_result(res))
      fprintf(stderr, "Egl.main_loop: "
	      "WARNING: %s raised an exception\n", name) ;
    CAMLdrop;
  }
  caml_release_runtime_system();
}

static void protect_callback3(char *name, value f, value v1,
			      value v2, value v3)
{
  caml_acquire_runtime_system();
  {
    CAMLparam4(f,v1,v2,v3);
    CAMLlocal1(res);
    res = caml_callback3_exn(f, v1, v2, v3);
    if(Is_exception_result(res))
      fprintf(stderr, "Egl.main_loop: "
	      "WARNING: %s raised an exception\n", name) ;
    CAMLdrop;
  }
  caml_release_runtime_system();
}

static void protect_callback4(char *name, value f, value v1,
			      value v2, value v3, value v4)
{
  caml_acquire_runtime_system();
  {
    CAMLparam5(f,v1,v2,v3,v4);
    CAMLlocal1(res);
    CAMLlocalN(tmp,4);
    tmp[0] = v1; tmp[1]=v2; tmp[2]=v3; tmp[3]=v4 ;
    res = caml_callbackN_exn(f, 4, tmp);
    if(Is_exception_result(res))
      fprintf(stderr, "Egl.main_loop: "
	      "WARNING: %s raised an exception\n", name) ;
    CAMLdrop;
  }
  caml_release_runtime_system();
}

>
> Christophe
>
> >
> >     I have a multithreaded program, which segfault in callback_asm, while I
> >     think I respect all rules of c interfaces... Except maybe the above...
> >
> >
> > There is an OPAM compiler switch "4.10.0+nnpcheck" that adds run-time checks
> > for ill-formed values according to the no-naked-pointers rules.  Maybe you
> > could use it to rule out this potential problem.
> >
> > Hope this helps,
> >
> > - Xavier Leroy
> >
> >
> >
> >     Chevets,
> >     Christophe
> >     --
> >     Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma
> >     brièveté.
> >

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [Caml-list] Global roots
  2020-12-21 21:03     ` Christophe Raffalli
@ 2020-12-21 22:33       ` ygrek
  2020-12-21 23:11         ` Christophe Raffalli
  0 siblings, 1 reply; 7+ messages in thread
From: ygrek @ 2020-12-21 22:33 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: Xavier Leroy, caml users

On Mon, 21 Dec 2020 22:03:32 +0100
Christophe Raffalli <christophe@raffalli.eu> wrote:

> static void protect_callback(char *name, value f, value v1)
> {
>   caml_acquire_runtime_system();
>   {
>     CAMLparam2(f,v1);
>     CAMLlocal1(res);
>     res = caml_callback_exn(f, v1);
>     if(Is_exception_result(res))
>       fprintf(stderr, "Egl.main_loop: "
> 	      "WARNING: %s raised an exception\n", name) ;
>     CAMLdrop;
>   }
>   caml_release_runtime_system();
> }

Values pointed by f and v1 may be moved by gc prior to caml_acquire_runtime_system call, but the values passed to the function (in registers or via stack)
will be pointing to old locations still. IOW need to pass ocaml values here by-reference (where the reference itself is registered with gc somehow), not by-value.

-- 

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

* Re: [Caml-list] Global roots
  2020-12-21 22:33       ` ygrek
@ 2020-12-21 23:11         ` Christophe Raffalli
  0 siblings, 0 replies; 7+ messages in thread
From: Christophe Raffalli @ 2020-12-21 23:11 UTC (permalink / raw)
  To: ygrek; +Cc: Xavier Leroy, caml users

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

Le 20-12-21 15:32:13, ygrek a écrit :
> On Mon, 21 Dec 2020 22:03:32 +0100
> Christophe Raffalli <christophe@raffalli.eu> wrote:
>
> > static void protect_callback(char *name, value f, value v1)
> > {
> >   caml_acquire_runtime_system();
> >   {
> >     CAMLparam2(f,v1);
> >     CAMLlocal1(res);
> >     res = caml_callback_exn(f, v1);
> >     if(Is_exception_result(res))
> >       fprintf(stderr, "Egl.main_loop: "
> > 	      "WARNING: %s raised an exception\n", name) ;
> >     CAMLdrop;
> >   }
> >   caml_release_runtime_system();
> > }
>
> Values pointed by f and v1 may be moved by gc prior to caml_acquire_runtime_system call, but the values passed to the function (in registers or via stack)
> will be pointing to old locations still. IOW need to pass ocaml values here by-reference (where the reference itself is registered with gc somehow), not by-value.
>
> --

Many thanks, this is clearly my problem and sounds completely logical! I should have see this myself!

Many the documentation should say this and show an example of callback acquiring the runtime system ? I update my issue in github.

Thanks again,
Christophe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-20 19:57 [Caml-list] Global roots christophe
2020-12-20 22:23 ` christophe
2020-12-21 18:38 ` Xavier Leroy
2020-12-21 20:40   ` Christophe Raffalli
2020-12-21 21:03     ` Christophe Raffalli
2020-12-21 22:33       ` ygrek
2020-12-21 23:11         ` Christophe Raffalli

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox