yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
370 stars 95 forks source link

Segmentation fault when using Ctypes with Async #52

Closed manzyuk closed 11 years ago

manzyuk commented 11 years ago

Here is my ocaml session (run with an empty .ocamlinit):

$ ocaml
        OCaml version 4.01.0+dev18-2013-06-13

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
# #thread;;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/threads: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/threads/threads.cma: loaded
# #require "async";;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/bigarray.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/nums.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top/num_top.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib/sexplib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit/oUnit.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit/pa_ounit_lib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib/fieldslib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot/bin_prot.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib/variantslib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/res: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/res/res.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel/core_kernel.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core/core.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core/core_top.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_core: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_core/async_core.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/comparelib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/comparelib/comparelib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_unix: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_unix/async_unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_extra: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async_extra/async_extra.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/async/async.cma: loaded
# #require "ctypes.foreign";;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes-foreign.cma: loaded
# open Ctypes;;
# open PosixTypes;;
# open Foreign;;
# let time = foreign "time" (ptr time_t @-> returning time_t);;
Segmentation fault (core dumped)

If #require "async" is skipped above, everything seems to work fine:

$ ocaml
        OCaml version 4.01.0+dev18-2013-06-13

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
# #thread;;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/threads: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/threads/threads.cma: loaded
# #require "ctypes.foreign";;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes-foreign.cma: loaded
# open Ctypes;;
# open PosixTypes;;
# open Foreign;;
# let time = foreign "time" (ptr time_t @-> returning time_t);;
val time : PosixTypes.time_t Ctypes.ptr -> PosixTypes.time_t = <fun>

However, requiring "async" immediately afterwards causes a segmentation fault again.

I'm running Ctypes 0.1 on Ubuntu:

$ opam list --installed | grep ctypes
ctypes                     0.1  Combinators for binding to C libraries without writing any C.
$ uname -a
Linux paddy 3.5.0-36-generic #57~precise1-Ubuntu SMP Thu Jun 20 15:22:35 UTC 2013 i686 i686 i386 GNU/Linux

I'd be happy to provide more information on my system if that would help to chase down this bug.

manzyuk commented 11 years ago

It actually segfaults also when I try to require "core" after "ctypes":

$ touch empty
$ ocaml -init empty
        OCaml version 4.01.0+dev18-2013-06-13

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
# #require "ctypes";;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded
# #require "core";;
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/bigarray.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot/bin_prot.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib/variantslib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/nums.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top/num_top.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/num: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib/sexplib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib/fieldslib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit/oUnit.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit/pa_ounit_lib.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/res: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/res/res.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel/core_kernel.cma: loaded
Segmentation fault (core dumped)

I suspect there might be something peculiar about my setup causing this problem, and I welcome suggestions as to how to debug it. (I'm a novice OCaml user. In fact, I bumped into this issue while reading "Real World OCaml" and trying out examples.)

avsm commented 11 years ago

The main difference from my setup is that you're on a 32-bit Ubuntu, I notice. I'm installing a VM to see if I can reproduce this there.

-anil

On 7 Aug 2013, at 22:51, Oleksandr Manzyuk notifications@github.com wrote:

It actually segfaults also when I try to require "core" after "ctypes":

$ touch empty $ ocaml -init empty OCaml version 4.01.0+dev18-2013-06-13

use "topfind";;

  • : unit = () Findlib has been successfully loaded. Additional directives:

    require "package";; to load a package

    list;; to list the available packages

    camlp4o;; to load camlp4 (standard syntax)

    camlp4r;; to load camlp4 (revised syntax)

    predicates "p,q,...";; to set these predicates

    Topfind.reset();; to force that packages will be reloaded

    thread;; to enable threads

  • : unit = ()

    require "ctypes";;

    /home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded

    require "core";;

    /home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/bigarray.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/bin_prot/bin_prot.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/variantslib/variantslib.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/nums.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/num-top/num_top.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/num: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/sexplib/sexplib.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/fieldslib/fieldslib.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/oUnit/oUnit.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/pa_ounit/pa_ounit_lib.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/res: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/res/res.cma: loaded /home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel: added to search path /home/manzyuk/.opam/4.01.0dev+trunk/lib/core_kernel/core_kernel.cma: loaded Segmentation fault (core dumped) I suspect there might be something peculiar about my setup causing this problem, and I welcome suggestions as to how to debug it. (I'm a novice OCaml user. In fact, I bumped into this issue while reading "Real World OCaml" and trying out examples.)

— Reply to this email directly or view it on GitHub.

manzyuk commented 11 years ago

Things are getting even more bizarre:

$ cat buggy
#use "topfind"
#require "ctypes"
#camlp4o
$ ocaml -init buggy
        OCaml version 4.01.0+dev18-2013-06-13

Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/dynlink.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/camlp4: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/camlp4/camlp4o.cma: loaded
Segmentation fault (core dumped)

Swapping the directive #camlp4o and #require "ctypes.foreign" in the init file seems to allow me to get further:

$ cat buggy2
#use "topfind"
#camlp4o
#require "ctypes.foreign"
$ ocaml -init buggy2
        OCaml version 4.01.0+dev18-2013-06-13

Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/dynlink.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/camlp4: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/camlp4/camlp4o.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ocaml/unix.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes: added to search path
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes.cma: loaded
/home/manzyuk/.opam/4.01.0dev+trunk/lib/ctypes/ctypes-foreign.cma: loaded
    Camlp4 Parsing version 4.01.0+dev18-2013-06-13

# open Ctypes;;
# open PosixTypes;;
# open Foreign;;
# let time = foreign "time" (ptr time_t @-> returning time_t);;
val time : PosixTypes.time_t Ctypes.ptr -> PosixTypes.time_t = <fun>
# let time' () = time (from_voidp time_t null);;
val time' : unit -> PosixTypes.time_t = <fun>
# let difftime = foreign "difftime" (time_t @-> time_t @-> returning double);;
val difftime : PosixTypes.time_t -> PosixTypes.time_t -> float = <fun>
# let t1 = time' () in
  Unix.sleep 2;
  let t2 = time' () in
  difftime t2 t1;;
Segmentation fault (core dumped)

This is all really unfortunate and I'd really like to have this fixed. Ctypes looks like a very neat library.

Please let me know if you manage to reproduce this issue in a 32-bit Ubuntu VM. If not, I'll probably try to reinstall all packages.

yallop commented 11 years ago

I can reproduce the crash in an Ubuntu 32-bit chroot.

yallop commented 11 years ago

Thanks for the report, @manzyuk!

Could you give 0.1.1 a try, please, to see whether it fixes the crash?

Something along the following lines should replace the opam-installed ctypes with ctypes 0.1.1.

$ wget https://github.com/ocamllabs/ocaml-ctypes/archive/ocaml-ctypes-0.1.1.zip
$ unzip ocaml-ctypes-0.1.1.zip
$ cd ocaml-ctypes-ocaml-ctypes-0.1.1/
$ make
$ opam remove ctypes
$ make install
avsm commented 11 years ago

Also simply:

opam remove ctypes opam pin ctypes git://github.com/ocamllabs/ocaml-ctypes opam install ctypes

On 10 Aug 2013, at 12:59, yallop notifications@github.com wrote:

Thanks for the report, @manzyuk!

Could you give 0.1.1 a try, please, to see whether it fixes the crash?

Something along the following lines should replace the opam-installed ctypes with ctypes 0.1.1.

$ wget https://github.com/ocamllabs/ocaml-ctypes/archive/ocaml-ctypes-0.1.1.zip $ unzip ocaml-ctypes-0.1.1.zip $ cd ocaml-ctypes-ocaml-ctypes-0.1.1/ $ make $ opam remove ctypes $ make install — Reply to this email directly or view it on GitHub.

manzyuk commented 11 years ago

Using ctypes 0.1.1 does fix the crash for me. Many thanks for such a prompt response! Please feel free to close the ticket.

yallop commented 11 years ago

Closed. ctypes 0.1.1 now available in opam.