Blame tal-debug.c

Packit 5c3484
/* TMP_ALLOC routines for debugging.
Packit 5c3484
Packit 5c3484
Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include <string.h>
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* This method aims to help a malloc debugger find problems.  A linked list
Packit 5c3484
   of allocated block is kept for TMP_FREE to release.  This is reentrant
Packit 5c3484
   and thread safe.
Packit 5c3484
Packit 5c3484
   Each TMP_ALLOC is a separate malloced block, so redzones or sentinels
Packit 5c3484
   applied by a malloc debugger either above or below can guard against
Packit 5c3484
   accesses outside the allocated area.
Packit 5c3484
Packit 5c3484
   A marker is a "struct tmp_debug_t *" so that TMP_DECL can initialize it
Packit 5c3484
   to NULL and we can detect TMP_ALLOC without TMP_MARK.
Packit 5c3484
Packit 5c3484
   It will work to realloc an MPZ_TMP_INIT variable, but when TMP_FREE comes
Packit 5c3484
   to release the memory it will have the old size, thereby triggering an
Packit 5c3484
   error from tests/memory.c.
Packit 5c3484
Packit 5c3484
   Possibilities:
Packit 5c3484
Packit 5c3484
   It'd be possible to keep a global list of active "struct tmp_debug_t"
Packit 5c3484
   records, so at the end of a program any TMP leaks could be printed.  But
Packit 5c3484
   if only a couple of routines are under test at any one time then the
Packit 5c3484
   likely culprit should be easy enough to spot.  */
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
__gmp_tmp_debug_mark (const char *file, int line,
Packit 5c3484
                      struct tmp_debug_t **markp, struct tmp_debug_t *mark,
Packit 5c3484
                      const char *decl_name, const char *mark_name)
Packit 5c3484
{
Packit 5c3484
  if (strcmp (mark_name, decl_name) != 0)
Packit 5c3484
    {
Packit 5c3484
      __gmp_assert_header (file, line);
Packit 5c3484
      fprintf (stderr, "GNU MP: TMP_MARK(%s) but TMP_DECL(%s) is in scope\n",
Packit 5c3484
               mark_name, decl_name);
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  if (*markp != NULL)
Packit 5c3484
    {
Packit 5c3484
      __gmp_assert_header (file, line);
Packit 5c3484
      fprintf (stderr, "GNU MP: Repeat of TMP_MARK(%s)\n", mark_name);
Packit 5c3484
      if (mark->file != NULL && mark->file[0] != '\0' && mark->line != -1)
Packit 5c3484
        {
Packit 5c3484
          __gmp_assert_header (mark->file, mark->line);
Packit 5c3484
          fprintf (stderr, "previous was here\n");
Packit 5c3484
        }
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  *markp = mark;
Packit 5c3484
  mark->file = file;
Packit 5c3484
  mark->line = line;
Packit 5c3484
  mark->list = NULL;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
void *
Packit 5c3484
__gmp_tmp_debug_alloc (const char *file, int line, int dummy,
Packit 5c3484
                       struct tmp_debug_t **markp,
Packit 5c3484
                       const char *decl_name, size_t size)
Packit 5c3484
{
Packit 5c3484
  struct tmp_debug_t        *mark = *markp;
Packit 5c3484
  struct tmp_debug_entry_t  *p;
Packit 5c3484
Packit 5c3484
  ASSERT_ALWAYS (size >= 1);
Packit 5c3484
Packit 5c3484
  if (mark == NULL)
Packit 5c3484
    {
Packit 5c3484
      __gmp_assert_header (file, line);
Packit 5c3484
      fprintf (stderr, "GNU MP: TMP_ALLOC without TMP_MARK(%s)\n", decl_name);
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  p = __GMP_ALLOCATE_FUNC_TYPE (1, struct tmp_debug_entry_t);
Packit 5c3484
  p->size = size;
Packit 5c3484
  p->block = (*__gmp_allocate_func) (size);
Packit 5c3484
  p->next = mark->list;
Packit 5c3484
  mark->list = p;
Packit 5c3484
  return p->block;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
__gmp_tmp_debug_free (const char *file, int line, int dummy,
Packit 5c3484
                      struct tmp_debug_t **markp,
Packit 5c3484
                      const char *decl_name, const char *free_name)
Packit 5c3484
{
Packit 5c3484
  struct tmp_debug_t        *mark = *markp;
Packit 5c3484
  struct tmp_debug_entry_t  *p, *next;
Packit 5c3484
Packit 5c3484
  if (mark == NULL)
Packit 5c3484
    {
Packit 5c3484
      __gmp_assert_header (file, line);
Packit 5c3484
      fprintf (stderr, "GNU MP: TMP_FREE(%s) without TMP_MARK(%s)\n",
Packit 5c3484
               free_name, decl_name);
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  if (strcmp (free_name, decl_name) != 0)
Packit 5c3484
    {
Packit 5c3484
      __gmp_assert_header (file, line);
Packit 5c3484
      fprintf (stderr, "GNU MP: TMP_FREE(%s) when TMP_DECL(%s) is in scope\n",
Packit 5c3484
               free_name, decl_name);
Packit 5c3484
      abort ();
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  p = mark->list;
Packit 5c3484
  while (p != NULL)
Packit 5c3484
    {
Packit 5c3484
      next = p->next;
Packit 5c3484
      (*__gmp_free_func) (p->block, p->size);
Packit 5c3484
      __GMP_FREE_FUNC_TYPE (p, 1, struct tmp_debug_entry_t);
Packit 5c3484
      p = next;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  *markp = NULL;
Packit 5c3484
}