Skip to content

Commit e61d05c

Browse files
authored
[fix] the risk for function exit() when open pthread support. (#6229)
* [fix] the risk for function exit() when open pthread support. * [update] modify annotation from "user data" to "pthread_data".
1 parent 7993477 commit e61d05c

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

components/libc/compilers/common/cstdlib.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ void __rt_libc_exit(int status)
2121
if (self != RT_NULL)
2222
{
2323
#ifdef RT_USING_PTHREADS
24-
extern void pthread_exit(void *value);
25-
pthread_exit((void *)status);
24+
if(self->pthread_data != RT_NULL)
25+
{
26+
extern void pthread_exit(void *value);
27+
pthread_exit((void *)status);
28+
}
2629
#else
2730
LOG_E("thread:%s exit:%d!", self->name, status);
2831
rt_thread_control(self, RT_THREAD_CTRL_CLOSE, RT_NULL);

components/libc/posix/pthreads/pthread.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
135135
/* clean magic */
136136
ptd->magic = 0x0;
137137

138-
/* clear the "ptd->tid->user_data" */
139-
ptd->tid->user_data = RT_NULL;
138+
/* clear the "ptd->tid->pthread_data" */
139+
ptd->tid->pthread_data = RT_NULL;
140140

141141
/* free ptd */
142142
rt_free(ptd);
@@ -281,7 +281,7 @@ int pthread_create(pthread_t *pid,
281281

282282
/* set pthread cleanup function and ptd data */
283283
ptd->tid->cleanup = _pthread_cleanup;
284-
ptd->tid->user_data = (rt_ubase_t)ptd;
284+
ptd->tid->pthread_data = (void *)ptd;
285285

286286
/* start thread */
287287
if (rt_thread_startup(ptd->tid) == RT_EOK)
@@ -394,8 +394,8 @@ pthread_t pthread_self (void)
394394
tid = rt_thread_self();
395395
if (tid == NULL) return PTHREAD_NUM_MAX;
396396

397-
/* get pthread data from user data of thread */
398-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
397+
/* get pthread data from pthread_data of thread */
398+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
399399
RT_ASSERT(ptd != RT_NULL);
400400

401401
return _pthread_data_get_pth(ptd);
@@ -477,8 +477,8 @@ void pthread_exit(void *value)
477477
return;
478478
}
479479

480-
/* get pthread data from user data of thread */
481-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
480+
/* get pthread data from pthread_data of thread */
481+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
482482

483483
rt_enter_critical();
484484
/* disable cancel */
@@ -595,8 +595,8 @@ void pthread_cleanup_pop(int execute)
595595

596596
if (rt_thread_self() == NULL) return;
597597

598-
/* get pthread data from user data of thread */
599-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
598+
/* get pthread data from pthread_data of thread */
599+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
600600
RT_ASSERT(ptd != RT_NULL);
601601

602602
if (execute)
@@ -624,8 +624,8 @@ void pthread_cleanup_push(void (*routine)(void *), void *arg)
624624

625625
if (rt_thread_self() == NULL) return;
626626

627-
/* get pthread data from user data of thread */
628-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
627+
/* get pthread data from pthread_data of thread */
628+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
629629
RT_ASSERT(ptd != RT_NULL);
630630

631631
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
@@ -676,8 +676,8 @@ int pthread_setcancelstate(int state, int *oldstate)
676676

677677
if (rt_thread_self() == NULL) return EINVAL;
678678

679-
/* get pthread data from user data of thread */
680-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
679+
/* get pthread data from pthread_data of thread */
680+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
681681
RT_ASSERT(ptd != RT_NULL);
682682

683683
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
@@ -699,8 +699,8 @@ int pthread_setcanceltype(int type, int *oldtype)
699699

700700
if (rt_thread_self() == NULL) return EINVAL;
701701

702-
/* get pthread data from user data of thread */
703-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
702+
/* get pthread data from pthread_data of thread */
703+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
704704
RT_ASSERT(ptd != RT_NULL);
705705

706706
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
@@ -721,8 +721,8 @@ void pthread_testcancel(void)
721721

722722
if (rt_thread_self() == NULL) return;
723723

724-
/* get pthread data from user data of thread */
725-
ptd = (_pthread_data_t *)rt_thread_self()->user_data;
724+
/* get pthread data from pthread_data of thread */
725+
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
726726
RT_ASSERT(ptd != RT_NULL);
727727

728728
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)

include/rtdef.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,13 @@ struct rt_thread
690690
rt_ubase_t remaining_tick; /**< remaining tick */
691691

692692
#ifdef RT_USING_CPU_USAGE
693-
rt_uint64_t duration_tick; /**< cpu usage tick */
693+
rt_uint64_t duration_tick; /**< cpu usage tick */
694694
#endif /* RT_USING_CPU_USAGE */
695695

696+
#ifdef RT_USING_PTHREADS
697+
void *pthread_data; /**< the handle of pthread data, adapt 32/64bit */
698+
#endif /* RT_USING_PTHREADS */
699+
696700
struct rt_timer thread_timer; /**< built-in thread timer */
697701

698702
void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */

src/thread.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ static rt_err_t _thread_init(struct rt_thread *thread,
252252
thread->duration_tick = 0;
253253
#endif /* RT_USING_CPU_USAGE */
254254

255+
#ifdef RT_USING_PTHREADS
256+
thread->pthread_data = RT_NULL;
257+
#endif /* RT_USING_PTHREADS */
258+
255259
#ifdef RT_USING_MODULE
256260
thread->module_id = 0;
257261
#endif /* RT_USING_MODULE */

0 commit comments

Comments
 (0)