Blame malloc/tst-interpose-skeleton.c

Packit 6c4009
/* Test driver for malloc interposition tests.
Packit 6c4009
   Copyright (C) 2016-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 License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   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; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
/* Fills BUFFER with a test string.  */
Packit 6c4009
static void
Packit 6c4009
line_string (int number, char *buffer, size_t length)
Packit 6c4009
{
Packit 6c4009
  for (size_t i = 0; i < length - 2; ++i)
Packit 6c4009
    buffer[i] = 'A' + ((number + i) % 26);
Packit 6c4009
  buffer[length - 2] = '\n';
Packit 6c4009
  buffer[length - 1] = '\0';
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Perform the tests.  */
Packit 6c4009
static void *
Packit 6c4009
run_tests (void *closure)
Packit 6c4009
{
Packit 6c4009
  char *temp_file_path;
Packit 6c4009
  int fd = create_temp_file ("tst-malloc-interpose", &temp_file_path);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    _exit (1);
Packit 6c4009
Packit 6c4009
  /* Line lengths excluding the line terminator.  */
Packit 6c4009
  static const int line_lengths[] = { 0, 45, 80, 2, 8201, 0, 17, -1 };
Packit 6c4009
Packit 6c4009
  /* Fill the test file with data.  */
Packit 6c4009
  {
Packit 6c4009
    FILE *fp = fdopen (fd, "w");
Packit 6c4009
    for (int lineno = 0; line_lengths[lineno] >= 0; ++lineno)
Packit 6c4009
      {
Packit 6c4009
        char buffer[line_lengths[lineno] + 2];
Packit 6c4009
        line_string (lineno, buffer, sizeof (buffer));
Packit 6c4009
        fprintf (fp, "%s", buffer);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    if (ferror (fp))
Packit 6c4009
      {
Packit 6c4009
        printf ("error: fprintf: %m\n");
Packit 6c4009
        _exit (1);
Packit 6c4009
      }
Packit 6c4009
    if (fclose (fp) != 0)
Packit 6c4009
      {
Packit 6c4009
        printf ("error: fclose: %m\n");
Packit 6c4009
        _exit (1);
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Read the test file.  This tests libc-internal allocation with
Packit 6c4009
     realloc.  */
Packit 6c4009
  {
Packit 6c4009
    FILE *fp = fopen (temp_file_path, "r");
Packit 6c4009
Packit 6c4009
    char *actual = NULL;
Packit 6c4009
    size_t actual_size = 0;
Packit 6c4009
    for (int lineno = 0; ; ++lineno)
Packit 6c4009
      {
Packit 6c4009
        errno = 0;
Packit 6c4009
        ssize_t result = getline (&actual, &actual_size, fp);
Packit 6c4009
        if (result == 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: invalid return value 0 from getline\n");
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
        if (result < 0 && errno != 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: getline: %m\n");
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
        if (result < 0 && line_lengths[lineno] >= 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: unexpected end of file after line %d\n", lineno);
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
        if (result > 0 && line_lengths[lineno] < 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: no end of file after line %d\n", lineno);
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
        if (result == -1 && line_lengths[lineno] == -1)
Packit 6c4009
          /* End of file reached as expected.  */
Packit 6c4009
          break;
Packit 6c4009
Packit 6c4009
        if (result != line_lengths[lineno] + 1)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: line length mismatch: expected %d, got %zd\n",
Packit 6c4009
                    line_lengths[lineno], result);
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
Packit 6c4009
        char expected[line_lengths[lineno] + 2];
Packit 6c4009
        line_string (lineno, expected, sizeof (expected));
Packit 6c4009
        if (strcmp (actual, expected) != 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("error: line mismatch\n");
Packit 6c4009
            printf ("error:   expected: [[%s]]\n", expected);
Packit 6c4009
            printf ("error:   actual:   [[%s]]\n", actual);
Packit 6c4009
            _exit (1);
Packit 6c4009
          }
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    if (fclose (fp) != 0)
Packit 6c4009
      {
Packit 6c4009
        printf ("error: fclose (after reading): %m\n");
Packit 6c4009
        _exit (1);
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  free (temp_file_path);
Packit 6c4009
Packit 6c4009
  /* Make sure that fork is working.  */
Packit 6c4009
  pid_t pid = fork ();
Packit 6c4009
  if (pid == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fork: %m\n");
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
  enum { exit_code = 55 };
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    _exit (exit_code);
Packit 6c4009
  int status;
Packit 6c4009
  int ret = waitpid (pid, &status, 0);
Packit 6c4009
  if (ret < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: waitpid: %m\n");
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (!WIFEXITED (status) || WEXITSTATUS (status) != exit_code)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: unexpected exit status from child process: %d\n",
Packit 6c4009
              status);
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* This is used to detect if malloc has not been successfully
Packit 6c4009
   interposed.  The interposed malloc does not use brk/sbrk.  */
Packit 6c4009
static void *initial_brk;
Packit 6c4009
__attribute__ ((constructor))
Packit 6c4009
static void
Packit 6c4009
set_initial_brk (void)
Packit 6c4009
{
Packit 6c4009
  initial_brk = sbrk (0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Terminate the process if the break value has been changed.  */
Packit 6c4009
__attribute__ ((destructor))
Packit 6c4009
static void
Packit 6c4009
check_brk (void)
Packit 6c4009
{
Packit 6c4009
  void *current = sbrk (0);
Packit 6c4009
  if (current != initial_brk)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: brk changed from %p to %p; no interposition?\n",
Packit 6c4009
              initial_brk, current);
Packit 6c4009
      _exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  check_brk ();
Packit 6c4009
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
  pthread_t thr = xpthread_create (NULL, run_tests, NULL);
Packit 6c4009
  xpthread_join (thr);
Packit 6c4009
#else
Packit 6c4009
  run_tests (NULL);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  check_brk ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}