Back to index

glibc  2.9
tst-align2.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdbool.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <sys/wait.h>
00025 #include <tst-stack-align.h>
00026 #include <unistd.h>
00027 
00028 static int res, fds[2], result;
00029 static bool test_destructors;
00030 
00031 extern void in_dso (int *, bool *, int *);
00032 
00033 static void __attribute__ ((constructor)) con (void)
00034 {
00035   res = TEST_STACK_ALIGN () ? -1 : 1;
00036 }
00037 
00038 static void __attribute__ ((destructor)) des (void)
00039 {
00040   if (!test_destructors)
00041     return;
00042 
00043   char c = TEST_STACK_ALIGN () ? 'B' : 'A';
00044   write (fds[1], &c, 1);
00045 }
00046 
00047 static int
00048 do_test (void)
00049 {
00050   if (!res)
00051     {
00052       puts ("binary's constructor has not been run");
00053       result = 1;
00054     }
00055   else if (res != 1)
00056     {
00057       puts ("binary's constructor has been run without sufficient alignment");
00058       result = 1;
00059     }
00060 
00061   if (TEST_STACK_ALIGN ())
00062     {
00063       puts ("insufficient stack alignment in do_test");
00064       result = 1;
00065     }
00066 
00067   in_dso (&result, &test_destructors, &fds[1]);
00068 
00069   if (pipe (fds) < 0)
00070     {
00071       printf ("couldn't create pipe: %m\n");
00072       return 1;
00073     }
00074 
00075   pid_t pid = fork ();
00076   if (pid < 0)
00077     {
00078       printf ("fork failed: %m\n");
00079       return 1;
00080     }
00081 
00082   if (!pid)
00083     {
00084       close (fds[0]);
00085       test_destructors = true;
00086       exit (0);
00087     }
00088 
00089   close (fds[1]);
00090 
00091   unsigned char c;
00092   ssize_t len;
00093   int des_seen = 0, dso_des_seen = 0;
00094   while ((len = TEMP_FAILURE_RETRY (read (fds[0], &c, 1))) > 0)
00095     {
00096       switch (c)
00097         {
00098         case 'B':
00099           puts ("insufficient alignment in binary's destructor");
00100           result = 1;
00101           /* FALLTHROUGH */
00102         case 'A':
00103           des_seen++;
00104           break;
00105         case 'D':
00106           puts ("insufficient alignment in DSO destructor");
00107           result = 1;
00108           /* FALLTHROUGH */
00109         case 'C':
00110           dso_des_seen++;
00111           break;
00112         default:
00113           printf ("unexpected character %x read from pipe", c);
00114           result = 1;
00115           break;
00116         }
00117     }
00118 
00119   close (fds[0]);
00120 
00121   if (des_seen != 1)
00122     {
00123       printf ("binary destructor run %d times instead of once\n", des_seen);
00124       result = 1;
00125     }
00126 
00127   if (dso_des_seen != 1)
00128     {
00129       printf ("DSO destructor run %d times instead of once\n", dso_des_seen);
00130       result = 1;
00131     }
00132 
00133   int status;
00134   pid_t termpid;
00135   termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
00136   if (termpid == -1)
00137     {
00138       printf ("waitpid failed: %m\n");
00139       result = 1;
00140     }
00141   else if (termpid != pid)
00142     {
00143       printf ("waitpid returned %ld != %ld\n",
00144              (long int) termpid, (long int) pid);
00145       result = 1;
00146     }
00147   else if (!WIFEXITED (status) || WEXITSTATUS (status))
00148     {
00149       puts ("child hasn't exited with exit status 0");
00150       result = 1;
00151     }
00152 
00153   return result;
00154 }
00155 
00156 #define TEST_FUNCTION do_test ()
00157 #include "../test-skeleton.c"