Blame elf/tst-align2.c

Packit 6c4009
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
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 <errno.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <tst-stack-align.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int res, fds[2], result;
Packit 6c4009
static bool test_destructors;
Packit 6c4009
Packit 6c4009
extern void in_dso (int *, bool *, int *);
Packit 6c4009
Packit 6c4009
static void __attribute__ ((constructor)) con (void)
Packit 6c4009
{
Packit 6c4009
  res = TEST_STACK_ALIGN () ? -1 : 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void __attribute__ ((destructor)) des (void)
Packit 6c4009
{
Packit 6c4009
  if (!test_destructors)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  char c = TEST_STACK_ALIGN () ? 'B' : 'A';
Packit 6c4009
  write (fds[1], &c, 1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (!res)
Packit 6c4009
    {
Packit 6c4009
      puts ("binary's constructor has not been run");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (res != 1)
Packit 6c4009
    {
Packit 6c4009
      puts ("binary's constructor has been run without sufficient alignment");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (TEST_STACK_ALIGN ())
Packit 6c4009
    {
Packit 6c4009
      puts ("insufficient stack alignment in do_test");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  in_dso (&result, &test_destructors, &fds[1]);
Packit 6c4009
Packit 6c4009
  if (pipe (fds) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("couldn't create pipe: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pid_t pid = fork ();
Packit 6c4009
  if (pid < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fork failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!pid)
Packit 6c4009
    {
Packit 6c4009
      close (fds[0]);
Packit 6c4009
      test_destructors = true;
Packit 6c4009
      exit (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fds[1]);
Packit 6c4009
Packit 6c4009
  unsigned char c;
Packit 6c4009
  ssize_t len;
Packit 6c4009
  int des_seen = 0, dso_des_seen = 0;
Packit 6c4009
  while ((len = TEMP_FAILURE_RETRY (read (fds[0], &c, 1))) > 0)
Packit 6c4009
    {
Packit 6c4009
      switch (c)
Packit 6c4009
        {
Packit 6c4009
        case 'B':
Packit 6c4009
          puts ("insufficient alignment in binary's destructor");
Packit 6c4009
          result = 1;
Packit 6c4009
          /* FALLTHROUGH */
Packit 6c4009
        case 'A':
Packit 6c4009
          des_seen++;
Packit 6c4009
          break;
Packit 6c4009
        case 'D':
Packit 6c4009
          puts ("insufficient alignment in DSO destructor");
Packit 6c4009
          result = 1;
Packit 6c4009
          /* FALLTHROUGH */
Packit 6c4009
        case 'C':
Packit 6c4009
          dso_des_seen++;
Packit 6c4009
          break;
Packit 6c4009
        default:
Packit 6c4009
          printf ("unexpected character %x read from pipe", c);
Packit 6c4009
          result = 1;
Packit 6c4009
          break;
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fds[0]);
Packit 6c4009
Packit 6c4009
  if (des_seen != 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("binary destructor run %d times instead of once\n", des_seen);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dso_des_seen != 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("DSO destructor run %d times instead of once\n", dso_des_seen);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int status;
Packit 6c4009
  pid_t termpid;
Packit 6c4009
  termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
Packit 6c4009
  if (termpid == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("waitpid failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (termpid != pid)
Packit 6c4009
    {
Packit 6c4009
      printf ("waitpid returned %ld != %ld\n",
Packit 6c4009
	      (long int) termpid, (long int) pid);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (!WIFEXITED (status) || WEXITSTATUS (status))
Packit 6c4009
    {
Packit 6c4009
      puts ("child hasn't exited with exit status 0");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>