Skip to content

Commit 5ce408b

Browse files
rbbrnsjhedberg
authored andcommitted
kernel: assert if k_current_get is called pre-kernel
k_current_get is not valid pre-kernel. It will return an invalid dummy thread or invalid memory. The invalid memory case can occur when CURRENT_THREAD_USE_TLS is enabled. Assert in k_current_get when called pre-kernel so offending code may be identified. k_is_pre_kernel is moved up in kernel.h to avoid needing a prototype for k_is_pre_kernel. Signed-off-by: Rob Barnes <[email protected]>
1 parent ef844ce commit 5ce408b

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

include/zephyr/kernel.h

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,33 @@ __syscall void k_wakeup(k_tid_t thread);
687687
__attribute_const__
688688
__syscall k_tid_t k_sched_current_thread_query(void);
689689

690+
/**
691+
* @brief Test whether startup is in the before-main-task phase.
692+
*
693+
* This routine allows the caller to customize its actions, depending on
694+
* whether it being invoked before the kernel is fully active.
695+
*
696+
* @funcprops \isr_ok
697+
*
698+
* @return true if invoked before post-kernel initialization
699+
* @return false if invoked during/after post-kernel initialization
700+
*/
701+
static inline bool k_is_pre_kernel(void)
702+
{
703+
extern bool z_sys_post_kernel; /* in init.c */
704+
705+
/*
706+
* If called from userspace, it must be post kernel.
707+
* This guard is necessary because z_sys_post_kernel memory
708+
* is not accessible to user threads.
709+
*/
710+
if (k_is_user_context()) {
711+
return false;
712+
}
713+
714+
return !z_sys_post_kernel;
715+
}
716+
690717
/**
691718
* @brief Get thread ID of the current thread.
692719
*
@@ -696,6 +723,8 @@ __syscall k_tid_t k_sched_current_thread_query(void);
696723
__attribute_const__
697724
static inline k_tid_t k_current_get(void)
698725
{
726+
__ASSERT(!k_is_pre_kernel(), "k_current_get called pre-kernel");
727+
699728
#ifdef CONFIG_CURRENT_THREAD_USE_TLS
700729

701730
/* Thread-local cache of current thread ID, set in z_thread_entry() */
@@ -1274,33 +1303,6 @@ bool k_is_in_isr(void);
12741303
*/
12751304
__syscall int k_is_preempt_thread(void);
12761305

1277-
/**
1278-
* @brief Test whether startup is in the before-main-task phase.
1279-
*
1280-
* This routine allows the caller to customize its actions, depending on
1281-
* whether it being invoked before the kernel is fully active.
1282-
*
1283-
* @funcprops \isr_ok
1284-
*
1285-
* @return true if invoked before post-kernel initialization
1286-
* @return false if invoked during/after post-kernel initialization
1287-
*/
1288-
static inline bool k_is_pre_kernel(void)
1289-
{
1290-
extern bool z_sys_post_kernel; /* in init.c */
1291-
1292-
/*
1293-
* If called from user mode, it must already be post kernel.
1294-
* This guard is necessary because z_sys_post_kernel memory
1295-
* is not accessible to user threads.
1296-
*/
1297-
if (k_is_user_context()) {
1298-
return false;
1299-
}
1300-
1301-
return !z_sys_post_kernel;
1302-
}
1303-
13041306
/**
13051307
* @}
13061308
*/

subsys/tracing/test/tracing_string_format_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ void sys_trace_k_thread_switched_out(void)
1515
{
1616
struct k_thread *thread;
1717

18-
thread = k_current_get();
18+
thread = k_sched_current_thread_query();
1919
TRACING_STRING("%s: %p\n", __func__, thread);
2020
}
2121

2222
void sys_trace_k_thread_switched_in(void)
2323
{
2424
struct k_thread *thread;
2525

26-
thread = k_current_get();
26+
thread = k_sched_current_thread_query();
2727
TRACING_STRING("%s: %p\n", __func__, thread);
2828
}
2929

0 commit comments

Comments
 (0)