GMP memory allocation problem in SWI-Prolog 5.6.38 and following versions

Starting from version 5.6.38 of SWI-Prolog, applications using SWI-Prolog and GMP started failing. The problem is due to the fact that SWI-Prolog overrides the memory allocation functions used by GMP by calling mp_set_memory_functions(). I have observed two instances of the problem, one of which was reported by users of the Parma Polyhedra Library:
1) a variable allocated in the C++ code before SWI-Prolog changes the allocation functions is destroyed after SWI-Prolog has done that (this results in a segmentation fault); 2) while executing the C++ code, the realloc function set by SWI-Prolog is called (this results into an invalid pointer detected by glibc).
Both instances are caused by the fact that memory allocated by GMP's default functions is reallocated/freed by SWI-Prolog's functions. Something that is guaranteed to cause problems. However, even if all memory allocation was done by the SWI-Prolog's functions, I am sure we would have other troubles. I am not sure what is the way out, but the problem is rather serious. All the best,
Roberto

On Tue, 02 Oct 2007 07:42:28 +0200 Roberto Bagnara bagnara@cs.unipr.it wrote:
Starting from version 5.6.38 of SWI-Prolog, applications using SWI-Prolog and GMP started failing. The problem is due to the fact that SWI-Prolog overrides the memory allocation functions used by GMP by calling mp_set_memory_functions().
Hello,
I have experienced the same issue a couple of years ago with Andy King's experimental PPL bindings for the Glasgow Haskell compiler (GHC). The problem was (and still is, I think) the reason why there is no "official" Haskell/GHC binding in the PPL distribution. I also contacted Simon Marlow on the GHC development side and he acknowledged the problem but didnt't have a solution for it in the near horizon (in the longer run, they might stop using GMP inside the Haskell runtime system).
Since I desperately needed to use the PPL with my Haskell programs I did a quick and dirty hack that worked (under Linux at least): 1) create a duplicate libmygmp with renamed symbols (with a prefix); 2) create a special libmyppl that uses libmygmp instead of libgmp; 3) link my Haskell code with libmyppl.
I used objcopy on the library binaries rather than modify the sources. But for some reason I could only get this to work with the static libraries. Anyway, I can pass you a short script that does the library renaming if you are interested.
Best regards,
Pedro Vasconcelos

Pedro Baltazar Vasconcelos wrote:
Hello,
Hello Pedro! (Long time, no see :-)
I have experienced the same issue a couple of years ago with Andy King's experimental PPL bindings for the Glasgow Haskell compiler (GHC). The problem was (and still is, I think) the reason why there is no "official" Haskell/GHC binding in the PPL distribution. I also contacted Simon Marlow on the GHC development side and he acknowledged the problem but didnt't have a solution for it in the near horizon (in the longer run, they might stop using GMP inside the Haskell runtime system).
Yes, I have read http://hackage.haskell.org/trac/ghc/ticket/311 and http://www.nabble.com/FFI-Bindings-to-Libraries-using-GMP-t4420123.html
Since I desperately needed to use the PPL with my Haskell programs I did a quick and dirty hack that worked (under Linux at least):
- create a duplicate libmygmp with renamed symbols (with a prefix);
- create a special libmyppl that uses libmygmp instead of libgmp;
- link my Haskell code with libmyppl.
I used objcopy on the library binaries rather than modify the sources. But for some reason I could only get this to work with the static libraries. Anyway, I can pass you a short script that does the library renaming if you are interested.
Yes, please! Why not posting it here? Ciao,
Roberto

On Wed, 03 Oct 2007 14:20:21 +0200 Roberto Bagnara bagnara@cs.unipr.it wrote:
I used objcopy on the library binaries rather than modify the sources. But for some reason I could only get this to work with the static libraries. Anyway, I can pass you a short script that does the library renaming if you are interested.
Yes, please! Why not posting it here?
OK, here is my library renaming script. Please read the comments for instructions and feel free to contact me if you have any questions or sugestions.
Best regards,
Pedro
#!/usr/bin/tclsh # # This script renames symbols in the static GMP and PPL libraries # this avoid issues with FFI linking to other systems that # modify the GMP allocator e.g. the Glasgow Haskell compiler. # Tested under Unbuntu Linux 6.06. # # Requires libgmp.a, libgmpxx.a, libppl.a, libppl_c.a (location below) # Creates copies libmy*.a with renamed GMP symbols in current directory # Does *not* modify the original libraries. # # To run: "tclsh librename.tcl" # # To install the libraries: manually copy libmy*.a to a suitable # system wide directory (e.g. /usr/local/lib). # # You should then link the foreign code with libmy*.a; # the external PPL interface is the same, but libmygmp is private. # # Pedro Vasconcelos, 2004, 2007
# location of the GMP and PPL *static* libraries # modify this according to your installation set PPL_DIR /usr/local/lib set GMP_DIR /usr/lib
# location for temporary files set TMP_DIR /tmp
# auxiliary procedure to create a renaming map # of all defined symbols in a library # $obj : the library archive file # $prefix : a string prepended to each symbol # $out : the output text file with the renaming map proc scan_object {obj prefix out} { set fd [open $out w] # execute nm to extract the symbols foreach line [split [exec nm -A --defined-only $obj 2> /dev/null] \n] { # process each line set line [split $line] set type [lindex $line 1] set symbol [lindex $line 2] if {[string first $type "BDTR"] >= 0} { puts $fd ${symbol}\t${prefix}${symbol} } } close $fd }
# create maps for C and C++ GMP libraries puts "Scanning libraries..." scan_object ${GMP_DIR}/libgmp.a "my" ${TMP_DIR}/libgmp.ren scan_object ${GMP_DIR}/libgmpxx.a "my" ${TMP_DIR}/libgmpxx.ren
# combine the renaming maps into a unique one exec cat libgmp.ren libgmpxx.ren | sort | uniq > ${TMP_DIR}/libgmpall.ren
# create renamed versions of the libraries # (in the current working directory) puts "Creating renamed libraries..." exec objcopy --redefine-syms=${TMP_DIR}/libgmp.ren \ ${GMP_DIR}/libgmp.a libmygmp.a exec objcopy --redefine-syms=${TMP_DIR}/libgmpxx.ren \ ${GMP_DIR}/libgmpxx.a libmygmpxx.a
exec objcopy --redefine-syms=${TMP_DIR}/libgmpall.ren \ ${PPL_DIR}/libppl.a libmyppl.a exec objcopy --redefine-syms=${TMP_DIR}/libgmpall.ren \ ${PPL_DIR}/libppl_c.a libmyppl_c.a
# remove temporary files file delete ${TMP_DIR}/libgmp.ren ${TMP_DIR}/libgmpxx.ren ${TMP_DIR}/libgmpall.ren
puts "Done!"
# -- end of file --
participants (2)
-
Pedro Baltazar Vasconcelos
-
Roberto Bagnara