tst-tls5.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Check alignment, overlapping and layout of TLS variables. */
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. #include <sys/param.h>
  21. #include "tst-tls5.h"
  22. #ifdef TLS_REGISTER
  23. struct tls_obj tls_registry[64];
  24. static int
  25. tls_addr_cmp (const void *a, const void *b)
  26. {
  27. if (((struct tls_obj *)a)->addr < ((struct tls_obj *)b)->addr)
  28. return -1;
  29. if (((struct tls_obj *)a)->addr > ((struct tls_obj *)b)->addr)
  30. return 1;
  31. return 0;
  32. }
  33. static int
  34. do_test (void)
  35. {
  36. size_t cnt, i;
  37. int res = 0;
  38. uintptr_t min_addr = ~(uintptr_t) 0, max_addr = 0;
  39. for (cnt = 0; tls_registry[cnt].name; ++cnt);
  40. tls_registry[cnt].name = NULL;
  41. tls_registry[cnt].addr = (uintptr_t) pthread_self ();
  42. tls_registry[cnt].size = sizeof (struct pthread);
  43. tls_registry[cnt++].align = __alignof__ (struct pthread);
  44. qsort (tls_registry, cnt, sizeof (struct tls_obj), tls_addr_cmp);
  45. for (i = 0; i < cnt; ++i)
  46. {
  47. printf ("%s%s = %p, size %zd, align %zd",
  48. tls_registry[i].name ? "&" : "",
  49. tls_registry[i].name ?: "pthread_self ()",
  50. (void *) tls_registry[i].addr,
  51. tls_registry[i].size, tls_registry[i].align);
  52. if (tls_registry[i].addr & (tls_registry[i].align - 1))
  53. {
  54. fputs (", WRONG ALIGNMENT", stdout);
  55. res = 1;
  56. }
  57. if (i > 0
  58. && (tls_registry[i - 1].addr + tls_registry[i - 1].size
  59. > tls_registry[i].addr))
  60. {
  61. fputs (", ADDRESS OVERLAP", stdout);
  62. res = 1;
  63. }
  64. puts ("");
  65. if (tls_registry[i].name)
  66. {
  67. min_addr = MIN (tls_registry[i].addr, min_addr);
  68. max_addr = MAX (tls_registry[i].addr + tls_registry[i].size,
  69. max_addr);
  70. }
  71. }
  72. if (cnt > 1)
  73. {
  74. #if defined(TLS_TCB_AT_TP)
  75. if (tls_registry[cnt - 1].name)
  76. {
  77. puts ("pthread_self () not larger than all TLS addresses");
  78. res = 1;
  79. }
  80. else
  81. max_addr = MAX (tls_registry[cnt - 1].addr, max_addr);
  82. #elif defined(TLS_DTV_AT_TP)
  83. if (tls_registry[0].name)
  84. {
  85. puts ("pthread_self () not smaller than all TLS addresses");
  86. res = 1;
  87. }
  88. #else
  89. abort ();
  90. #endif
  91. printf ("Initial TLS used block size %zd\n",
  92. (size_t) (max_addr - min_addr));
  93. }
  94. return res;
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #else
  98. #define TEST_FUNCTION 0
  99. #endif
  100. #include "../test-skeleton.c"