Bill Allombert on Mon, 26 Jun 2017 15:04:29 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: GMP memory allocation and Macaulay2 (cont.)


On Fri, Jun 23, 2017 at 12:15:26PM -0400, Doug Torrance wrote:
> On 06/23/2017 03:53 AM, Bill Allombert wrote:
> >On Wed, Jun 21, 2017 at 04:33:12PM -0400, Doug Torrance wrote:
> >>Hello!
> >>A while ago [1], I inquired about an segfaults when Macaulay2 is linked
> >>against both PARI and GMP due to memory allocation issues.
> >>
> >>Note that I tried using the new-ish INIT_noINTGMPm option to pari_init_opts
> >>when calling from Macaulay2 to no avail.
> >>
> >>I promised a backtrace to help identify the problem, but am only now getting
> >>around to it.
> >>
> >>Here is the output from gdb:
> >>(gdb) bt
> >>#0  __GI___libc_free (mem=0xfffffffffffffff8) at malloc.c:2966
> >>#1  0x00007ffff40be12f in pari_var_close ()
> >>    from /usr/lib/x86_64-linux-gnu/libpari-gmp-tls.so.5
> >>#2  0x00007ffff4102d7c in pari_close_opts ()
> >>    from /usr/lib/x86_64-linux-gnu/libpari-gmp-tls.so.5
> >
> >Hello Doug, I found a potential bug in pari_close_opts().
> >
> >Could you try to comment out the call to pari_kernel_close() in
> >pari_close_opts() ?
> 
> That appears to work!

Do you know why ? 
First pari_kernel_close is called after pari_var_close(), and 
pari_kernel_close() does:

void
pari_kernel_close(void)
{
  void *(*new_gmp_malloc)(size_t new_size);
  void *(*new_gmp_realloc)(void *ptr, size_t old_size, size_t new_size);
  void (*new_gmp_free)(void *ptr, size_t old_size);
  mp_get_memory_functions (&new_gmp_malloc, &new_gmp_realloc,
&new_gmp_free);
  if (new_gmp_malloc==pari_malloc) new_gmp_malloc = old_gmp_malloc;
  if (new_gmp_realloc==pari_gmp_realloc) new_gmp_realloc =
old_gmp_realloc;
  if (new_gmp_free==pari_gmp_free) new_gmp_free = old_gmp_free;
  mp_set_memory_functions(new_gmp_malloc, new_gmp_realloc,
new_gmp_free);
}
so normally, if you do not call pari_kernel_init, it should just do
  mp_get_memory_functions (&new_gmp_malloc, &new_gmp_realloc, &new_gmp_free);
  mp_set_memory_functions(new_gmp_malloc, new_gmp_realloc, new_gmp_free);
which should be harmless.

Cheers,
Bill.