Back to index

glibc  2.9
tst-tls5.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 /* Check alignment, overlapping and layout of TLS variables.  */
00021 #include <stdint.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <pthread.h>
00025 #include <pthreadP.h>
00026 #include <sys/param.h>
00027 
00028 #include "tst-tls5.h"
00029 
00030 #ifdef TLS_REGISTER
00031 
00032 struct tls_obj tls_registry[64];
00033 
00034 static int
00035 tls_addr_cmp (const void *a, const void *b)
00036 {
00037   if (((struct tls_obj *)a)->addr < ((struct tls_obj *)b)->addr)
00038     return -1;
00039   if (((struct tls_obj *)a)->addr > ((struct tls_obj *)b)->addr)
00040     return 1;
00041   return 0;
00042 }
00043 
00044 static int
00045 do_test (void)
00046 {
00047   size_t cnt, i;
00048   int res = 0;
00049   uintptr_t min_addr = ~(uintptr_t) 0, max_addr = 0;
00050 
00051   for (cnt = 0; tls_registry[cnt].name; ++cnt);
00052   tls_registry[cnt].name = NULL;
00053   tls_registry[cnt].addr = (uintptr_t) pthread_self ();
00054   tls_registry[cnt].size = sizeof (struct pthread);
00055   tls_registry[cnt++].align = __alignof__ (struct pthread);
00056 
00057   qsort (tls_registry, cnt, sizeof (struct tls_obj), tls_addr_cmp);
00058 
00059   for (i = 0; i < cnt; ++i)
00060     {
00061       printf ("%s%s = %p, size %zd, align %zd",
00062              tls_registry[i].name ? "&" : "",
00063              tls_registry[i].name ?: "pthread_self ()",
00064              (void *) tls_registry[i].addr,
00065              tls_registry[i].size, tls_registry[i].align);
00066       if (tls_registry[i].addr & (tls_registry[i].align - 1))
00067        {
00068          fputs (", WRONG ALIGNMENT", stdout);
00069          res = 1;
00070        }
00071       if (i > 0
00072          && (tls_registry[i - 1].addr + tls_registry[i - 1].size
00073              > tls_registry[i].addr))
00074        {
00075          fputs (", ADDRESS OVERLAP", stdout);
00076          res = 1;
00077        }
00078       puts ("");
00079       if (tls_registry[i].name)
00080        {
00081          min_addr = MIN (tls_registry[i].addr, min_addr);
00082          max_addr = MAX (tls_registry[i].addr + tls_registry[i].size,
00083                        max_addr);
00084        }
00085     }
00086 
00087   if (cnt > 1)
00088     {
00089 #if TLS_TCB_AT_TP
00090       if (tls_registry[cnt - 1].name)
00091        {
00092          puts ("pthread_self () not larger than all TLS addresses");
00093          res = 1;
00094        }
00095       else
00096        max_addr = MAX (tls_registry[cnt - 1].addr, max_addr);
00097 #elif TLS_DTV_AT_TP
00098       if (tls_registry[0].name)
00099        {
00100          puts ("pthread_self () not smaller than all TLS addresses");
00101          res = 1;
00102        }
00103 #else
00104       abort ();
00105 #endif
00106       printf ("Initial TLS used block size %zd\n",
00107              (size_t) (max_addr - min_addr));
00108     }
00109   return res;
00110 }
00111 
00112 #define TEST_FUNCTION do_test ()
00113 
00114 #else
00115 
00116 #define TEST_FUNCTION 0
00117 
00118 #endif
00119 
00120 #include "../test-skeleton.c"