Skip to content

Commit

Permalink
kernel: stack: error handling
Browse files Browse the repository at this point in the history
Add runtime error checking for both k_stack_push and k_stack_cleanup.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information
nashif authored and jaizpuruzin committed Jul 13, 2023
1 parent a941ea1 commit a81cbe2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 6 additions & 3 deletions include/kernel.h
Expand Up @@ -2708,9 +2708,11 @@ __syscall s32_t k_stack_alloc_init(struct k_stack *stack,
* if the buffer wasn't dynamically allocated.
*
* @param stack Address of the stack.
* @retval 0 on success
* @retval -EAGAIN when object is still in use
* @req K-STACK-001
*/
void k_stack_cleanup(struct k_stack *stack);
int k_stack_cleanup(struct k_stack *stack);

/**
* @brief Push an element onto a stack.
Expand All @@ -2722,10 +2724,11 @@ void k_stack_cleanup(struct k_stack *stack);
* @param stack Address of the stack.
* @param data Value to push onto the stack.
*
* @return N/A
* @retval 0 on success
* @retval -ENOMEM if stack is full
* @req K-STACK-001
*/
__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);

/**
* @brief Pop an element from a stack.
Expand Down
24 changes: 14 additions & 10 deletions kernel/stack.c
Expand Up @@ -15,7 +15,7 @@
#include <linker/sections.h>
#include <ksched.h>
#include <wait_q.h>
#include <sys/__assert.h>
#include <sys/check.h>
#include <init.h>
#include <syscall_handler.h>
#include <kernel_internal.h>
Expand Down Expand Up @@ -81,23 +81,28 @@ static inline s32_t z_vrfy_k_stack_alloc_init(struct k_stack *stack,
#include <syscalls/k_stack_alloc_init_mrsh.c>
#endif

void k_stack_cleanup(struct k_stack *stack)
int k_stack_cleanup(struct k_stack *stack)
{
__ASSERT_NO_MSG(z_waitq_head(&stack->wait_q) == NULL);
CHECKIF(z_waitq_head(&stack->wait_q) != NULL) {
return -EAGAIN;
}

if ((stack->flags & K_STACK_FLAG_ALLOC) != (u8_t)0) {
k_free(stack->base);
stack->base = NULL;
stack->flags &= ~K_STACK_FLAG_ALLOC;
}
return 0;
}

void z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
int z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
{
struct k_thread *first_pending_thread;
k_spinlock_key_t key;

__ASSERT(stack->next != stack->top, "stack is full");
CHECKIF(stack->next == stack->top) {
return -ENOMEM;
}

key = k_spin_lock(&stack->lock);

Expand All @@ -109,22 +114,21 @@ void z_impl_k_stack_push(struct k_stack *stack, stack_data_t data)
z_thread_return_value_set_with_data(first_pending_thread,
0, (void *)data);
z_reschedule(&stack->lock, key);
return;
} else {
*(stack->next) = data;
stack->next++;
k_spin_unlock(&stack->lock, key);
}

return 0;
}

#ifdef CONFIG_USERSPACE
static inline void z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
static inline int z_vrfy_k_stack_push(struct k_stack *stack, stack_data_t data)
{
Z_OOPS(Z_SYSCALL_OBJ(stack, K_OBJ_STACK));
Z_OOPS(Z_SYSCALL_VERIFY_MSG(stack->next != stack->top,
"stack is full"));
z_impl_k_stack_push(stack, data);

return z_impl_k_stack_push(stack, data);
}
#include <syscalls/k_stack_push_mrsh.c>
#endif
Expand Down

0 comments on commit a81cbe2

Please sign in to comment.