Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004166OCamlOCaml generalpublic2006-11-21 19:522014-09-04 00:25
Reportermottl 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version3.09.3 
Target VersionundecidedFixed in Version 
Summary0004166: Incorrect module initialisation order with "external" declarations
DescriptionWe have a module (lets call it "Foo") that interfaces C-bindings and exports functions defined as "external" in its .mli-file. This module is packed with others into one module, which again is part of a .cmxa-file.

If we link this library with another module (say, "Bar") that uses only functions defined "external" in "Foo", we observed that toplevel expressions in "Foo" did not get evaluated.

Here is a schematic example:

foo.ml:
-----
external foo : unit -> unit = "foo"
external init : unit -> unit = "init"

let () =
  print_endline "init";
  init ()
-----

foo.mli:
-----
external foo : unit -> unit
-----

bar.ml:
-----
let () =
  print_endline "bar";
  Foo.foo ()
-----

Since "Bar" depends on "Foo", you would expect that the toplevel expression in "foo.ml" gets evaluated first, which should print "init" and e.g. initialise the C-bindings. But we see "bar" first, and our application crashes, because the C-bindings are not evaluated.

Interestingly, changing "foo.mli" as follows solves the problem:

foo.mli:
---
val foo : unit -> unit
---

This indicates that exclusively calling functions exported as "external" will prevent the linker from figuring out which modules should be initialized first. This seems to be a bug, it shouldn't matter whether we use "val" or "external".
TagsNo tags attached.
Attached Files

- Relationships
related to 0006063resolvedgarrigue Simplify the typing of module aliases 
related to 0004374closed Failure to evaluate top level expressions in dependant module 

-  Notes
(0003861)
doligez (administrator)
2006-11-24 15:43

I'm downgrading this to "minor" since there is an easy workaround: always use "val" instead of "external" in your .mli files.
(0005690)
ygrek (reporter)
2010-10-26 16:47

and it also bites those lazy folks (like me) who do not write mli interfaces..
(0005753)
edwin (reporter)
2010-12-23 16:10

Perhaps ocaml should give a warning if you use 'external' in .mli files.

Due to this bug exceptions are not registered in C code, which causes caml_raise_arg_with to be invoked with a NULL exception, which in turn causes a segfault.
(0010771)
garrigue (manager)
2013-12-31 01:18

Since a similar problem appears when introducing module aliases, we might want to solve the two simultaneously. A clean approach would require adding dependency information to the cmo format.

- Issue History
Date Modified Username Field Change
2006-11-21 19:52 mottl New Issue
2006-11-24 15:43 doligez Note Added: 0003861
2006-11-24 15:43 doligez Severity major => minor
2006-11-24 15:43 doligez Status new => acknowledged
2007-08-29 02:13 garrigue Relationship added related to 0004374
2010-10-26 16:47 ygrek Note Added: 0005690
2010-12-23 16:10 edwin Note Added: 0005753
2012-07-11 16:14 doligez Target Version => 4.01.0+dev
2012-07-31 13:37 doligez Target Version 4.01.0+dev => 4.00.1+dev
2012-09-14 23:49 doligez Target Version 4.00.1+dev => 4.01.0+dev
2013-07-24 12:27 doligez Target Version 4.01.0+dev => 4.01.1+dev
2013-12-31 01:16 garrigue Relationship added related to 0006063
2013-12-31 01:18 garrigue Note Added: 0010771
2014-05-25 20:20 doligez Target Version 4.01.1+dev => 4.02.0+dev
2014-08-18 15:19 doligez Target Version 4.02.0+dev => 4.02.1+dev
2014-09-04 00:25 doligez Target Version 4.02.1+dev => undecided


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker