[PPL-devel] GMP memory allocation problem in SWI-Prolog 5.6.38 and following versions

Roberto Bagnara bagnara at cs.unipr.it
Tue Oct 2 10:25:17 CEST 2007


Jan Wielemaker wrote:
> Hi Roberto,
> 
> On Tuesday 02 October 2007 07:42, Roberto Bagnara 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(). 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,
> 
> Rebinding the allocation functions is done to allow SWI-Prolog to detect
> and act upon GMP memory overflows. Unfortunately GMP is very weak in
> allowing applications to act gracefully on overflows. I see no reason
> for problems as long as you initialize Prolog before touching any GMP
> number.

This is not always possible, unfortunately.

> If that should be the case anyway, we must add something to the
> foreign interface that allows for initializing Prolog without rebinding
> the GMP allocation.

This is certainly useful.  But what happens if, in some computation in
the C++ code that has nothing to do with SWI-Prolog,  pl_abort() is
called (I am looking at pl-gmp.c)?
Perhaps I am missing something, but this will break the overall
application completely.  I think SWI-Prolog should have his own
private version of GMP if it wants to change the allocation functions.
In other words, setting the allocation functions on a shared GMP can
only be done at the overall application level: if every component
does that on its own, things are going to break.
All the best,

    Roberto

-- 
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara at cs.unipr.it



More information about the PPL-devel mailing list