td_thr_validate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Validate a thread handle.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include "thread_dbP.h"
  16. #include <stdbool.h>
  17. td_err_e
  18. __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist)
  19. {
  20. if (__td_ta_rtld_global (ta))
  21. return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global,
  22. rtld_global, _dl_stack_user, 0);
  23. else
  24. {
  25. if (ta->ta_addr__dl_stack_user == NULL
  26. && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_user,
  27. &ta->ta_addr__dl_stack_user) != PS_OK)
  28. return TD_ERR;
  29. *plist = ta->ta_addr__dl_stack_user;
  30. return TD_OK;
  31. }
  32. }
  33. td_err_e
  34. __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist)
  35. {
  36. if (__td_ta_rtld_global (ta))
  37. return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global,
  38. rtld_global, _dl_stack_used, 0);
  39. else
  40. {
  41. if (ta->ta_addr__dl_stack_used == NULL
  42. && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_used,
  43. &ta->ta_addr__dl_stack_used) != PS_OK)
  44. return TD_ERR;
  45. *plist = ta->ta_addr__dl_stack_used;
  46. return TD_OK;
  47. }
  48. }
  49. static td_err_e
  50. check_thread_list (const td_thrhandle_t *th, psaddr_t head, bool *uninit)
  51. {
  52. td_err_e err;
  53. psaddr_t next, ofs;
  54. err = DB_GET_FIELD (next, th->th_ta_p, head, list_t, next, 0);
  55. if (err == TD_OK)
  56. {
  57. if (next == NULL)
  58. {
  59. *uninit = true;
  60. return TD_NOTHR;
  61. }
  62. err = DB_GET_FIELD_ADDRESS (ofs, th->th_ta_p, NULL, pthread, list, 0);
  63. }
  64. while (err == TD_OK)
  65. {
  66. if (next == head)
  67. return TD_NOTHR;
  68. if (next - (ofs - (psaddr_t) 0) == th->th_unique)
  69. return TD_OK;
  70. err = DB_GET_FIELD (next, th->th_ta_p, next, list_t, next, 0);
  71. }
  72. return err;
  73. }
  74. td_err_e
  75. td_thr_validate (const td_thrhandle_t *th)
  76. {
  77. td_err_e err;
  78. psaddr_t list;
  79. LOG ("td_thr_validate");
  80. /* First check the list with threads using user allocated stacks. */
  81. bool uninit = false;
  82. err = __td_ta_stack_user (th->th_ta_p, &list);
  83. if (err == TD_OK)
  84. err = check_thread_list (th, list, &uninit);
  85. /* If our thread is not on this list search the list with stack
  86. using implementation allocated stacks. */
  87. if (err == TD_NOTHR)
  88. {
  89. err = __td_ta_stack_used (th->th_ta_p, &list);
  90. if (err == TD_OK)
  91. err = check_thread_list (th, list, &uninit);
  92. if (err == TD_NOTHR && uninit && th->th_unique == NULL)
  93. /* __pthread_initialize_minimal has not run yet.
  94. There is only the special case thread handle. */
  95. err = TD_OK;
  96. }
  97. return err;
  98. }