88#include <irq_offload.h>
99
1010#define TIMEOUT K_MSEC(100)
11+ #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
1112#define STACK_LEN 2
1213
1314static ZTEST_BMEM stack_data_t data [STACK_LEN ];
1415extern struct k_stack stack ;
16+ K_THREAD_STACK_DEFINE (threadstack2 , STACK_SIZE );
17+ struct k_thread thread_data2 ;
18+
19+ /* action after self-defined fatal handler. */
20+ static ZTEST_BMEM volatile bool valid_fault ;
21+
22+ static inline void set_fault_valid (bool valid )
23+ {
24+ valid_fault = valid ;
25+ }
26+
27+ void k_sys_fatal_error_handler (unsigned int reason , const z_arch_esf_t * pEsf )
28+ {
29+ printk ("Caught system error -- reason %d %d\n" , reason , valid_fault );
30+ if (valid_fault ) {
31+ printk ("Fatal error expected as part of test case.\n" );
32+ valid_fault = false; /* reset back to normal */
33+ } else {
34+ printk ("Fatal error was unexpected, aborting...\n" );
35+ k_fatal_halt (reason );
36+ }
37+ }
1538
1639static void stack_pop_fail (struct k_stack * stack )
1740{
@@ -28,6 +51,12 @@ static void stack_pop_fail(struct k_stack *stack)
2851 * @{
2952 */
3053
54+ /* Sub-thread entry */
55+ void tStack_pop_entry (void * p1 , void * p2 , void * p3 )
56+ {
57+ zassert_true (k_stack_pop (p1 , p2 , K_FOREVER ), "stack pop failed\n" );
58+ }
59+
3160/**
3261 * @brief Verifies stack pop functionality
3362 * @see k_stack_init(), k_stack_pop()
@@ -39,6 +68,46 @@ void test_stack_pop_fail(void)
3968 stack_pop_fail (& stack );
4069}
4170
71+ /**
72+ * @brief Verifies cleanup a stack that still be needed by another
73+ * thread.
74+ * @see k_stack_cleanup()
75+ */
76+ void test_stack_cleanup_error (void )
77+ {
78+ stack_data_t rx_data [STACK_LEN - 1 ];
79+
80+ k_stack_init (& stack , data , STACK_LEN );
81+ /* Creat a new thread */
82+ k_tid_t tid = k_thread_create (& thread_data2 , threadstack2 , STACK_SIZE ,
83+ tStack_pop_entry , & stack ,
84+ rx_data , NULL , K_PRIO_PREEMPT (0 ), 0 ,
85+ K_NO_WAIT );
86+ /* Delay for finishing some actions of the new thread */
87+ k_sleep (K_MSEC (500 ));
88+ /* Try to clean up the stack, that still be waited by the thread */
89+ zassert_true (k_stack_cleanup (& stack ) == - EAGAIN , "The stack is cleanuped successful" );
90+ /* clear the spawn thread to avoid side effect */
91+ k_thread_abort (tid );
92+ }
93+
94+ /**
95+ * @brief Verifies push a data in the full stack.
96+ * @see k_stack_push()
97+ */
98+ void test_stack_push_full (void )
99+ {
100+ stack_data_t tx_data [STACK_LEN ] = {0 };
101+ stack_data_t data_tmp = 0 ;
102+
103+ k_stack_init (& stack , data , STACK_LEN );
104+ for (int i = 0 ; i < STACK_LEN ; i ++ ) {
105+ zassert_true (k_stack_push (& stack , tx_data [i ]) == 0 , "push data into stack failed" );
106+ }
107+ /* Verify that push a data in the full stack, a nagetive value will be met */
108+ zassert_true (k_stack_push (& stack , data_tmp ) == - ENOMEM , "push data successful" );
109+ }
110+
42111#ifdef CONFIG_USERSPACE
43112/**
44113 * @brief Verifies stack pop from a user thread
@@ -54,6 +123,68 @@ void test_stack_user_pop_fail(void)
54123
55124 stack_pop_fail (alloc_stack );
56125}
126+
127+ /**
128+ * @brief Verifies stack alloc and initialize a null pointer.
129+ * @see k_stack_alloc_init()
130+ */
131+ void test_stack_user_init_null (void )
132+ {
133+ set_fault_valid (true);
134+ k_stack_alloc_init (NULL , STACK_LEN );
135+ }
136+
137+ /**
138+ * @brief Verify that alloc and initialize a stack with
139+ * 0 memory.
140+ * @see k_stack_alloc_init()
141+ */
142+ void test_stack_user_init_invalid_value (void )
143+ {
144+ set_fault_valid (true);
145+ struct k_stack * alloc_stack = k_object_alloc (K_OBJ_STACK );
146+
147+ zassert_not_null (alloc_stack , "couldn't allocate stack object" );
148+ k_stack_alloc_init (alloc_stack , 0 );
149+ }
150+
151+ /**
152+ * @brief Verify that push some data into a NULL
153+ * pointer.
154+ * @see k_stack_push()
155+ */
156+ void test_stack_user_push_null (void )
157+ {
158+ set_fault_valid (true);
159+ k_stack_push (NULL , 0 );
160+ }
161+
162+ /**
163+ * @brief Verifies pop data from a NULL pointer.
164+ * @see k_stack_pop()
165+ */
166+ void test_stack_user_pop_null (void )
167+ {
168+ set_fault_valid (true);
169+ k_stack_pop (NULL , 0 , K_NO_WAIT );
170+ }
171+
172+ /**
173+ * @brief Verifies cleanup a stack that its data still be waited by
174+ * another thread.
175+ * @see k_stack_pop()
176+ */
177+ void test_stack_user_pop_permission (void )
178+ {
179+ set_fault_valid (true);
180+ struct k_stack * alloc_stack = k_object_alloc (K_OBJ_STACK );
181+
182+ zassert_not_null (alloc_stack , "couldn't allocate stack object" );
183+ zassert_false (k_stack_alloc_init (alloc_stack , STACK_LEN ),
184+ "stack init failed" );
185+ /* Try to access and to write data at invalid address */
186+ k_stack_pop (alloc_stack , (stack_data_t * )alloc_stack , K_NO_WAIT );
187+ }
57188#endif
58189/**
59190 * @}
0 commit comments