caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Presumed bug in OCaml's garbage collector or in its Weak module...
@ 2016-06-17 18:09 "Martin R. Neuhäußer"
  2016-06-17 19:23 ` Gabriel Scherer
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: "Martin R. Neuhäußer" @ 2016-06-17 18:09 UTC (permalink / raw)
  To: OCaml List

Dear all,

after an intense week of debugging some large C-bindings, I presume to have found a bug in OCaml’s garbage collection code or in its Weak module. 
To summarize, it seems as if OCaml’s garbage collector and its Weak module sometimes release a custom block (by calling its finalizer) too early, i.e. when it is still reachable.

I hesitate a bit before opening an „official“ issue on Mantis as I might very well overlook some detail. Therefore I’d like to double-check some of the assumptions that I have made when writing my C-stubs. Any corrections are highly welcome:
1. Custom blocks may be moved around in memory by the GC, but they are never duplicated.
2. The finalizer for each custom block that is allocated by caml_alloc_custom is called at most once.
3. The finalizer is never called for blocks that are still alive; stated otherwise, a block that has been finalized can never been presented as a value to a C-stub anymore.

There is a small example program available on github: https://github.com/martin-neuhaeusser/ocaml_bug where the above assumptions seem to be violated.

The idea is as follows:
Each custom block that the example allocates contains a pointer to structure that is malloc’ed outside the OCaml heap; each custom block uniquely refers to such a structure.
Upon creation of a custom block, a special flag-value is stored in its corresponding (newly created) structure to indicate that the block is alive. Once a custom block’s finalizer is called, the flag is changed to indicate finalization of the block. I assume that after the finalizer returns, the custom block is unreachable (and perhaps its memory reclaimed by the GC); however, for debugging, I intentionally keep the custom block’s structure allocated on the unmanaged heap. Whenever a custom block is presented to the C-stubs, they check that the corresponding flag-value within its corresponding non-managed heap structure indicates liveness of the block. As it turns out, this invariant is violated, if the Weak module and the GC are stressed.

Of course, it might very well be that there is a bug in my C-stubs that is responsible for that behavior. I tested with different versions of OCaml, with different outcomes: The example program terminates correctly with OCaml 3.12.1, 4.00.0, 4.00.1, 4.01.0, and 4.03.0+beta1 whereas it crashes due to the above error when compiled with OCaml 4.02.0, 4.02.1, 4.02.2, 4.02.3, 4.03.0+beta2, and the released 4.03.0. The tests were run on 64bit Debian Linux and 64bit MacOS X.

I'd very much appreciate any help, hints to bugs in my code, or requests for clarification.

Thanks and best regards,
Martin

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

end of thread, other threads:[~2016-06-27 11:36 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-17 18:09 [Caml-list] Presumed bug in OCaml's garbage collector or in its Weak module "Martin R. Neuhäußer"
2016-06-17 19:23 ` Gabriel Scherer
2016-06-18 15:15   ` "Martin R. Neuhäußer"
2016-06-17 19:41 ` Török Edwin
2016-06-17 20:29   ` "Martin R. Neuhäußer"
2016-06-18 10:59     ` Josh Berdine
2016-06-18 15:11   ` "Martin R. Neuhäußer"
2016-06-18 16:54   ` Leo White
2016-06-21 12:43     ` François Bobot
2016-06-21 19:37       ` Alain Frisch
2016-06-22  8:12         ` François Bobot
2016-06-18 17:39 ` "Martin R. Neuhäußer"
2016-06-21 11:55   ` François Bobot
2016-06-27 11:35     ` AW: " Neuhaeusser, Martin

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