Using the Parma Polyhedra Library from Haskell

The problem

The Parma Polyhedra Library (PPL) is a modern and complete C++ library providing numerical abstractions useful in program analysis including (but not limited to) convex polyhedra. The latest PPL version 0.9.1 includes bindings for several languages: C++, C, several Prolog systems, Java and O'Caml.

Axel Simon wrote PPL library bindings for the Glasgow Haskell Compiler (GHC) which he was kind enough to pass on to me. These are not yet part of the standard PPL distribution because of a conflict: both the PPL and the GHC runtime system use the GNU multiple precision library (GMP) for big-num arithmetic. Unfortunately, the GHC runtime modifies the GMP memory allocator to use its own custom one. When a foreign library (in this case the PPL) uses the shared GMP library we get segmentation faults sometime during execution. My guess was that GHCs garbage collector and the FFI finalizers were somehow interacting badly.

To prove this theory I tried a quick and dirty work-around: I modified the PPL to use its own private version of the GMP, so that the final GHC+PPL binary uses two copies of the GMP. This worked and proved my theory, thought it won't win prizes for elegance.

I realize it would be better to do the trick the other around, namely, make GHC use a private version of the GMP; after all, there is no point in using a shared library if other foreign code cannot use it! However, I had compiled the PPL from source already but had not done so with GHC, so I was lazy and chose the easiest path.

By the way, I contacted Simon Marlow of the GHC development team who acknowledged the problem and said that the clean solution will come only when GMP in the Haskell compiler is replaced by a custom library, but this is not to be done in the near future. In the mean time, my hack allows circumventing the problem.

Work around

My work around solution was tested on a GNU/Linux system and uses the objcopy utility to rename symbols in PPL and GMP libraries.

This tclsh script automates this process. It produces new libraries called libmyppl.a, libmyppl_c.a, etc.; the original are not modified. For some reason, this technique worked only for the static libraries. Although nothing is modified or deleted (other than temporary files), make sure you read the code before executing to check for paths, etc.

Instructions

  1. download the PPL sources from the PPL site;
  2. download my Haskell/GHC bindings;
  3. download the library renaming script librename.tcl;
  4. compile and install the PPL as usual;
  5. run tclsh librename.tcl from the ppl-0.9 directory; this should generate the following files: libmyppl.a, libmyppl_c.a, libmygmp.a and libmygmpxx.a;
  6. manually copy the above libraries to e.g. /usr/local/lib;
  7. expand the Haskell/GHC interface from the ppl-0.9 directory
  8. change to ppl-0.9/interface/haskell and run make; this constructs a demo Haskell program that uses the PPL.

Caveats


Pedro Vasconcelos, pbv at dcc dot fc dot up dot pt
Department of Computer Science
Faculty of Science, University of Porto