Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004166OCamlOCaml generalpublic2006-11-21 19:522015-08-12 11:34
Assigned To 
PlatformOSOS Version
Product Version3.09.3 
Target Version4.03.0+devFixed 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:
external foo : unit -> unit = "foo"
external init : unit -> unit = "init"

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

external foo : unit -> unit
let () =
  print_endline "bar"; ()

Since "Bar" depends on "Foo", you would expect that the toplevel expression in "" 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:

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 
has duplicate 0006956acknowledgedgarrigue Module initialization not performed for extern-only lib (? segfault) 
related to 0004374closed Failure to evaluate top level expressions in dependant module 

-  Notes
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.
ygrek (reporter)
2010-10-26 16:47

and it also bites those lazy folks (like me) who do not write mli interfaces..
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.
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
2014-09-25 17:30 doligez Target Version undecided => 4.02.2+dev / +rc1
2015-01-16 22:27 doligez Target Version 4.02.2+dev / +rc1 => 4.02.3+dev
2015-07-15 16:25 doligez Target Version 4.02.3+dev => 4.03.0+dev
2015-08-12 11:34 garrigue Relationship added has duplicate 0006956

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker