tst-tls5.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /* Check alignment, overlapping and layout of TLS variables. */
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <pthread.h>
  21. #include <sys/param.h>
  22. #include "tst-tls5.h"
  23. #ifdef TLS_REGISTER
  24. struct tls_obj tls_registry[64];
  25. static int
  26. tls_addr_cmp (const void *a, const void *b)
  27. {
  28. if (((struct tls_obj *)a)->addr < ((struct tls_obj *)b)->addr)
  29. return -1;
  30. if (((struct tls_obj *)a)->addr > ((struct tls_obj *)b)->addr)
  31. return 1;
  32. return 0;
  33. }
  34. static int
  35. do_test (void)
  36. {
  37. size_t cnt, i;
  38. int res = 0;
  39. uintptr_t min_addr = ~(uintptr_t) 0, max_addr = 0;
  40. for (cnt = 0; tls_registry[cnt].name; ++cnt);
  41. tls_registry[cnt].name = NULL;
  42. tls_registry[cnt].addr = (uintptr_t) pthread_self ();
  43. tls_registry[cnt].size = sizeof (struct pthread);
  44. tls_registry[cnt++].align = __alignof__ (struct pthread);
  45. qsort (tls_registry, cnt, sizeof (struct tls_obj), tls_addr_cmp);
  46. for (i = 0; i < cnt; ++i)
  47. {
  48. printf ("%s%s = %p, size %zd, align %zd",
  49. tls_registry[i].name ? "&" : "",
  50. tls_registry[i].name ?: "pthread_self ()",
  51. (void *) tls_registry[i].addr,
  52. tls_registry[i].size, tls_registry[i].align);
  53. if (tls_registry[i].addr & (tls_registry[i].align - 1))
  54. {
  55. fputs (", WRONG ALIGNMENT", stdout);
  56. res = 1;
  57. }
  58. if (i > 0
  59. && (tls_registry[i - 1].addr + tls_registry[i - 1].size
  60. > tls_registry[i].addr))
  61. {
  62. fputs (", ADDRESS OVERLAP", stdout);
  63. res = 1;
  64. }
  65. puts ("");
  66. if (tls_registry[i].name)
  67. {
  68. min_addr = MIN (tls_registry[i].addr, min_addr);
  69. max_addr = MAX (tls_registry[i].addr + tls_registry[i].size,
  70. max_addr);
  71. }
  72. }
  73. if (cnt > 1)
  74. {
  75. #if defined(TLS_TCB_AT_TP)
  76. if (tls_registry[cnt - 1].name)
  77. {
  78. puts ("pthread_self () not larger than all TLS addresses");
  79. res = 1;
  80. }
  81. else
  82. max_addr = MAX (tls_registry[cnt - 1].addr, max_addr);
  83. #elif defined(TLS_DTV_AT_TP)
  84. if (tls_registry[0].name)
  85. {
  86. puts ("pthread_self () not smaller than all TLS addresses");
  87. res = 1;
  88. }
  89. #else
  90. abort ();
  91. #endif
  92. printf ("Initial TLS used block size %zd\n",
  93. (size_t) (max_addr - min_addr));
  94. }
  95. return res;
  96. }
  97. #define TEST_FUNCTION do_test ()
  98. #else
  99. #define TEST_FUNCTION 0
  100. #endif
  101. #include "../test-skeleton.c"