Blame misc/tst-allocate_once.c

Packit 6c4009
/* Test the allocate_once function.
Packit 6c4009
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <allocate_once.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
/* Allocate a new string.  */
Packit 6c4009
static void *
Packit 6c4009
allocate_string (void *closure)
Packit 6c4009
{
Packit 6c4009
  return xstrdup (closure);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Allocation and deallocation functions which are not expected to be
Packit 6c4009
   called.  */
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
allocate_not_called (void *closure)
Packit 6c4009
{
Packit 6c4009
  FAIL_EXIT1 ("allocation function called unexpectedly (%p)", closure);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
deallocate_not_called (void *closure, void *ptr)
Packit 6c4009
{
Packit 6c4009
  FAIL_EXIT1 ("deallocate function called unexpectedly (%p, %p)",
Packit 6c4009
              closure, ptr);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Counter for various function calls.  */
Packit 6c4009
static int function_called;
Packit 6c4009
Packit 6c4009
/* An allocation function which returns NULL and records that it has
Packit 6c4009
   been called.  */
Packit 6c4009
static void *
Packit 6c4009
allocate_return_null (void *closure)
Packit 6c4009
{
Packit 6c4009
  /* The function should only be called once.  */
Packit 6c4009
  TEST_COMPARE (function_called, 0);
Packit 6c4009
  ++function_called;
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The following is used to check the retry logic, by causing a fake
Packit 6c4009
   race condition.  */
Packit 6c4009
static void *fake_race_place;
Packit 6c4009
static char fake_race_region[3]; /* To obtain unique addresses.  */
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
fake_race_allocate (void *closure)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY (closure == &fake_race_region[0]);
Packit 6c4009
  TEST_COMPARE (function_called, 0);
Packit 6c4009
  ++function_called;
Packit 6c4009
  /* Fake allocation by another thread.  */
Packit 6c4009
  fake_race_place = &fake_race_region[1];
Packit 6c4009
  return &fake_race_region[2];
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fake_race_deallocate (void *closure, void *ptr)
Packit 6c4009
{
Packit 6c4009
  /* Check that the pointer returned from fake_race_allocate is
Packit 6c4009
     deallocated (and not the one stored in fake_race_place).  */
Packit 6c4009
  TEST_VERIFY (ptr == &fake_race_region[2]);
Packit 6c4009
Packit 6c4009
  TEST_VERIFY (fake_race_place == &fake_race_region[1]);
Packit 6c4009
  TEST_VERIFY (closure == &fake_race_region[0]);
Packit 6c4009
  TEST_COMPARE (function_called, 1);
Packit 6c4009
  ++function_called;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Similar to fake_race_allocate, but expects to be paired with free
Packit 6c4009
   as the deallocation function.  */
Packit 6c4009
static void *
Packit 6c4009
fake_race_allocate_for_free (void *closure)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY (closure == &fake_race_region[0]);
Packit 6c4009
  TEST_COMPARE (function_called, 0);
Packit 6c4009
  ++function_called;
Packit 6c4009
  /* Fake allocation by another thread.  */
Packit 6c4009
  fake_race_place = &fake_race_region[1];
Packit 6c4009
  return xstrdup ("to be freed");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  /* Simple allocation.  */
Packit 6c4009
  void *place1 = NULL;
Packit 6c4009
  char *string1 = allocate_once (&place1, allocate_string,
Packit 6c4009
                                   deallocate_not_called,
Packit 6c4009
                                   (char *) "test string 1");
Packit 6c4009
  TEST_VERIFY_EXIT (string1 != NULL);
Packit 6c4009
  TEST_VERIFY (strcmp ("test string 1", string1) == 0);
Packit 6c4009
  /* Second call returns the first pointer, without calling any
Packit 6c4009
     callbacks.  */
Packit 6c4009
  TEST_VERIFY (string1
Packit 6c4009
               == allocate_once (&place1, allocate_not_called,
Packit 6c4009
                                 deallocate_not_called,
Packit 6c4009
                                 (char *) "test string 1a"));
Packit 6c4009
Packit 6c4009
  /* Different place should result in another call.  */
Packit 6c4009
  void *place2 = NULL;
Packit 6c4009
  char *string2 = allocate_once (&place2, allocate_string,
Packit 6c4009
                                 deallocate_not_called,
Packit 6c4009
                                 (char *) "test string 2");
Packit 6c4009
  TEST_VERIFY_EXIT (string2 != NULL);
Packit 6c4009
  TEST_VERIFY (strcmp ("test string 2", string2) == 0);
Packit 6c4009
  TEST_VERIFY (string1 != string2);
Packit 6c4009
Packit 6c4009
  /* Check error reporting (NULL return value from the allocation
Packit 6c4009
     function).  */
Packit 6c4009
  void *place3 = NULL;
Packit 6c4009
  char *string3 = allocate_once (&place3, allocate_return_null,
Packit 6c4009
                                 deallocate_not_called, NULL);
Packit 6c4009
  TEST_VERIFY (string3 == NULL);
Packit 6c4009
  TEST_COMPARE (function_called, 1);
Packit 6c4009
Packit 6c4009
  /* Check that the deallocation function is called if the race is
Packit 6c4009
     lost.  */
Packit 6c4009
  function_called = 0;
Packit 6c4009
  TEST_VERIFY (allocate_once (&fake_race_place,
Packit 6c4009
                              fake_race_allocate,
Packit 6c4009
                              fake_race_deallocate,
Packit 6c4009
                              &fake_race_region[0])
Packit 6c4009
               == &fake_race_region[1]);
Packit 6c4009
  TEST_COMPARE (function_called, 2);
Packit 6c4009
  function_called = 3;
Packit 6c4009
  TEST_VERIFY (allocate_once (&fake_race_place,
Packit 6c4009
                              fake_race_allocate,
Packit 6c4009
                              fake_race_deallocate,
Packit 6c4009
                              &fake_race_region[0])
Packit 6c4009
               == &fake_race_region[1]);
Packit 6c4009
  TEST_COMPARE (function_called, 3);
Packit 6c4009
Packit 6c4009
  /* Similar, but this time rely on that free is called.  */
Packit 6c4009
  function_called = 0;
Packit 6c4009
  fake_race_place = NULL;
Packit 6c4009
  TEST_VERIFY (allocate_once (&fake_race_place,
Packit 6c4009
                                fake_race_allocate_for_free,
Packit 6c4009
                                NULL,
Packit 6c4009
                                &fake_race_region[0])
Packit 6c4009
               == &fake_race_region[1]);
Packit 6c4009
  TEST_COMPARE (function_called, 1);
Packit 6c4009
  function_called = 3;
Packit 6c4009
  TEST_VERIFY (allocate_once (&fake_race_place,
Packit 6c4009
                              fake_race_allocate_for_free,
Packit 6c4009
                              NULL,
Packit 6c4009
                              &fake_race_region[0])
Packit 6c4009
               == &fake_race_region[1]);
Packit 6c4009
  TEST_COMPARE (function_called, 3);
Packit 6c4009
Packit 6c4009
  free (place2);
Packit 6c4009
  free (place1);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>