caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: David.Mentre@irisa.fr (David Mentré)
Cc: caml-list@inria.fr (OCAML)
Subject: Re: Calling C from OCaml, GC problems
Date: Fri, 18 Feb 2000 02:26:41 +0100 (MET)	[thread overview]
Message-ID: <200002180126.CAA28941@miss.wu-wien.ac.at> (raw)
In-Reply-To: <wd8ael1noi1.fsf@parate.irisa.fr> from "David=?iso-8859-1?q?_Mentr=E9?=" at Feb 16, 2000 04:41:42 PM

>  1. the ocaml doc (paragraph 15.5) states that CAMLparam and CAMLreturn
>     macros should be used. However, example code in 15.7 does not use
>     them (even if functions have value typed parameters). The Unix
>     interfacing code ocaml-2.04/otherlibs/unix/) doesn't use those macro
>     either. Faulty doc?  Are those macros not mandatory? What is the
>     rationale?

"CAMLparam" + "CAMLreturn" are used in cases where the C-program has to
allocate memory on the OCaml-heap. This can, however, trigger a garbage
collection. If you are unlucky, the values you have received from OCaml are
not referenced from any other value in OCaml anymore, i.e. they are not
reachable.

The garbage collector may therefore reclaim the values in question. If you
still need them in the C-program and you try to access them later, you
might cause a segfault, because the memory of these values has been freed -
or even worse: you manipulate some new value which was allocated in this
place, which may cause problems much later.

To prevent this from happening, you have to "secure" the values by using
the macros mentioned above. "CAMLparam" ensures that the values stay
reachable for the time you still need them, "CAMLreturn" removes this
protection again and returns the value.

You need not protect the value parameters if you do not allocate anything
on the OCaml-heap or if you do not need them anymore before doing the next
allocation. This is probably the case in the Unix-library, where you mostly
just extract some system value (file descriptors, etc.) and pass it to the
appropriate system call.

>  2. when compiling, I've tons of warning like this:
[...]
>     Did I use wrongly those CAML* macros?

I know that the macros require parenthesis now (OCaml 2.99) - so you will
probably have to put them around "result" in "CAMLreturn result" to stay
compatible.

Otherwise the CAML-macros seem to be ok, but I am not quite sure about your
correct use of values like (e.g. "MANAGER m"). I'd have to see the whole
code, unfortunately, your host is unreachable at the moment...

>  3. The code is running on small examples but segfault on bigger
>     ones. From gdb backtrace, it seems clear that my bug is releated to
>     a GC problem:

This might be a problem of wrong "protection" of some values: small
examples are unlikely to cause many allocations. This means that the
probability of causing and finding the bug is lower.

Unfortunately, depending on what C does with the values, the problem can be
hidden for quite some time until the code really causes a segfault. So the
true location of the problem may be far from the point in the source where
the fault appears.

>     The bdd library is using itself a memory management library calling
>     sbrk(2). Can it trigger problems with OCaml GC (like the GC going
>     into bdd structures)?

Hm, no idea. The man page of "sbrk" says somewhere:

  It is unspecified whether the pointer returned by sbrk() is aligned
  suitably for any purpose.

And the OCaml manual says:

  Any word-aligned pointer to an address outside the heap can be safely
  cast to and from the type value. This includes pointers returned
  by malloc, and pointers to C variables (of size at least one word)
  obtained with the & operator.

It is beyond my understanding whether this might be the cause of problems,
but "sbrk" seems to be somewhat dangerous in this respect.

Good luck chasing the bug!

- Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



  reply	other threads:[~2000-02-18  9:47 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-16 15:41 David Mentré
2000-02-18  1:26 ` Markus Mottl [this message]
2000-02-18  9:45 ` Xavier Leroy
2000-02-21 16:54   ` David Mentré
2000-02-21 23:41     ` Max Skaller
2000-02-18 10:48 ` Juan J. Quintela
2000-02-21 13:40   ` David Mentré
2000-02-23 19:39 Damien Doligez
2000-02-24 23:36 ` Max Skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200002180126.CAA28941@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=David.Mentre@irisa.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).