Blame tal-debug.c

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