| 1 | /* Validate a thread handle. | 
| 2 |    Copyright (C) 1999-2022 Free Software Foundation, Inc. | 
| 3 |    This file is part of the GNU C Library. | 
| 4 |  | 
| 5 |    The GNU C Library is free software; you can redistribute it and/or | 
| 6 |    modify it under the terms of the GNU Lesser General Public | 
| 7 |    License as published by the Free Software Foundation; either | 
| 8 |    version 2.1 of the License, or (at your option) any later version. | 
| 9 |  | 
| 10 |    The GNU C Library is distributed in the hope that it will be useful, | 
| 11 |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 12 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
| 13 |    Lesser General Public License for more details. | 
| 14 |  | 
| 15 |    You should have received a copy of the GNU Lesser General Public | 
| 16 |    License along with the GNU C Library; if not, see | 
| 17 |    <https://www.gnu.org/licenses/>.  */ | 
| 18 |  | 
| 19 | #include "thread_dbP.h" | 
| 20 | #include <stdbool.h> | 
| 21 |  | 
| 22 | td_err_e | 
| 23 | __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist) | 
| 24 | { | 
| 25 |   if (__td_ta_rtld_global (ta)) | 
| 26 |     return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global, | 
| 27 | 				 rtld_global, _dl_stack_user, 0); | 
| 28 |   else | 
| 29 |     { | 
| 30 |       if (ta->ta_addr__dl_stack_user == 0 | 
| 31 | 	  && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_user, | 
| 32 | 			    &ta->ta_addr__dl_stack_user) != PS_OK) | 
| 33 | 	return TD_ERR; | 
| 34 |       *plist = ta->ta_addr__dl_stack_user; | 
| 35 |       return TD_OK; | 
| 36 |     } | 
| 37 | } | 
| 38 |  | 
| 39 | td_err_e | 
| 40 | __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist) | 
| 41 | { | 
| 42 |  | 
| 43 |   if (__td_ta_rtld_global (ta)) | 
| 44 |     return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global, | 
| 45 | 				 rtld_global, _dl_stack_used, 0); | 
| 46 |   else | 
| 47 |     { | 
| 48 |       if (ta->ta_addr__dl_stack_used == 0 | 
| 49 | 	  && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_used, | 
| 50 | 			    &ta->ta_addr__dl_stack_used) != PS_OK) | 
| 51 | 	return TD_ERR; | 
| 52 |       *plist = ta->ta_addr__dl_stack_used; | 
| 53 |       return TD_OK; | 
| 54 |     } | 
| 55 | } | 
| 56 |  | 
| 57 | static td_err_e | 
| 58 | check_thread_list (const td_thrhandle_t *th, psaddr_t head, bool *uninit) | 
| 59 | { | 
| 60 |   td_err_e err; | 
| 61 |   psaddr_t next, ofs; | 
| 62 |  | 
| 63 |   err = DB_GET_FIELD (next, th->th_ta_p, head, list_t, next, 0); | 
| 64 |   if (err == TD_OK) | 
| 65 |     { | 
| 66 |       if (next == 0) | 
| 67 | 	{ | 
| 68 | 	  *uninit = true; | 
| 69 | 	  return TD_NOTHR; | 
| 70 | 	} | 
| 71 |       err = DB_GET_FIELD_ADDRESS (ofs, th->th_ta_p, 0, pthread, list, 0); | 
| 72 |     } | 
| 73 |  | 
| 74 |   while (err == TD_OK) | 
| 75 |     { | 
| 76 |       if (next == head) | 
| 77 | 	return TD_NOTHR; | 
| 78 |  | 
| 79 |       if (next - (ofs - (psaddr_t) 0) == th->th_unique) | 
| 80 | 	return TD_OK; | 
| 81 |  | 
| 82 |       err = DB_GET_FIELD (next, th->th_ta_p, next, list_t, next, 0); | 
| 83 |     } | 
| 84 |  | 
| 85 |   return err; | 
| 86 | } | 
| 87 |  | 
| 88 |  | 
| 89 | td_err_e | 
| 90 | td_thr_validate (const td_thrhandle_t *th) | 
| 91 | { | 
| 92 |   td_err_e err; | 
| 93 |   psaddr_t list; | 
| 94 |  | 
| 95 |   LOG ("td_thr_validate" ); | 
| 96 |  | 
| 97 |   /* First check the list with threads using user allocated stacks.  */ | 
| 98 |   bool uninit = false; | 
| 99 |   err = __td_ta_stack_user (th->th_ta_p, &list); | 
| 100 |   if (err == TD_OK) | 
| 101 |     err = check_thread_list (th, list, &uninit); | 
| 102 |  | 
| 103 |   /* If our thread is not on this list search the list with stack | 
| 104 |      using implementation allocated stacks.  */ | 
| 105 |   if (err == TD_NOTHR) | 
| 106 |     { | 
| 107 |       err = __td_ta_stack_used (th->th_ta_p, &list); | 
| 108 |       if (err == TD_OK) | 
| 109 | 	err = check_thread_list (th, list, &uninit); | 
| 110 |  | 
| 111 |       if (err == TD_NOTHR && uninit && th->th_unique == 0) | 
| 112 | 	/* __pthread_initialize_minimal has not run yet. | 
| 113 | 	   There is only the special case thread handle.  */ | 
| 114 | 	err = TD_OK; | 
| 115 |     } | 
| 116 |  | 
| 117 |   return err; | 
| 118 | } | 
| 119 |  |