tests: mem protection new test inherit resource pool

To improve Zephyr tests, I think that it will necessary to have test,
that verifies a child thread inherits resource pool assignment
of their parent.

Signed-off-by: Maksim Masalski <maksim.masalski@intel.com>
This commit is contained in:
Maksim Masalski 2020-05-15 19:39:22 +03:00 committed by Carles Cufí
commit 2893aa0bc1
4 changed files with 88 additions and 2 deletions

View file

@ -3,3 +3,4 @@ CONFIG_ZTEST=y
CONFIG_ZTEST_STACKSIZE=2048
CONFIG_MAX_THREAD_BYTES=4
CONFIG_USERSPACE=y
CONFIG_APPLICATION_DEFINED_SYSCALL=y

View file

@ -1,10 +1,11 @@
/*
* Copyright (c) 2017 Intel Corporation
* Copyright (c) 2020 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mem_protect.h"
#include <syscall_handler.h>
/* function prototypes */
static inline void dummy_start(struct k_timer *timer)
@ -18,11 +19,15 @@ static inline void dummy_end(struct k_timer *timer)
/* Kernel objects */
K_THREAD_STACK_DEFINE(test_1_stack, INHERIT_STACK_SIZE);
K_THREAD_STACK_DEFINE(parent_thr_stack, STACK_SIZE);
K_THREAD_STACK_DEFINE(child_thr_stack, STACK_SIZE);
K_MEM_POOL_DEFINE(res_pool, BLK_SIZE_MIN, BLK_SIZE_MAX, BLK_NUM_MAX, BLK_ALIGN);
K_SEM_DEFINE(inherit_sem, SEMAPHORE_INIT_COUNT, SEMAPHORE_MAX_COUNT);
K_SEM_DEFINE(sync_sem, SEM_INIT_VAL, SEM_MAX_VAL);
K_MUTEX_DEFINE(inherit_mutex);
K_TIMER_DEFINE(inherit_timer, dummy_start, dummy_end);
K_MSGQ_DEFINE(inherit_msgq, MSG_Q_SIZE, MSG_Q_MAX_NUM_MSGS, MSG_Q_ALIGN);
struct k_thread test_1_tid;
struct k_thread test_1_tid, parent_thr, child_thr;
uint8_t MEM_DOMAIN_ALIGNMENT inherit_buf[MEM_REGION_ALLOC]; /* for mem domain */
@ -100,3 +105,63 @@ void test_permission_inheritance(void *p1, void *p2, void *p3)
k_thread_join(&test_1_tid, K_FOREVER);
}
struct k_mem_pool *z_impl_ret_resource_pool_ptr(void)
{
return _current->resource_pool;
}
static inline struct k_mem_pool *z_vrfy_ret_resource_pool_ptr(void)
{
return z_impl_ret_resource_pool_ptr();
}
#include <syscalls/ret_resource_pool_ptr_mrsh.c>
struct k_mem_pool *child_res_pool_ptr;
struct k_mem_pool *parent_res_pool_ptr;
void child_handler(void *p1, void *p2, void *p3)
{
child_res_pool_ptr = ret_resource_pool_ptr();
k_sem_give(&sync_sem);
}
void parent_handler(void *p1, void *p2, void *p3)
{
parent_res_pool_ptr = ret_resource_pool_ptr();
k_thread_create(&child_thr, child_thr_stack,
K_THREAD_STACK_SIZEOF(child_thr_stack),
child_handler,
NULL, NULL, NULL,
PRIORITY, 0, K_NO_WAIT);
}
/**
* @brief Test child thread inherits parent's thread resource pool
*
* @details - Create a resource pool res_pool for the parent thread.
* - Then special system call ret_resource_pool_ptr() returns pointer
* to the resource pool of the current thread.
* - Call it in the parent_handler() and in the child_handler()
* - Then in the main test function test_inherit_resource_pool()
* compare returned addresses
* - If the addresses are the same, it means that child thread inherited
* resource pool of the parent's thread -test passed.
*
* @ingroup kernel_memprotect_tests
*
* @see k_thread_resource_pool_assign()
*/
void test_inherit_resource_pool(void *p1, void *p2, void *p3)
{
k_sem_reset(&sync_sem);
k_thread_create(&parent_thr, parent_thr_stack,
K_THREAD_STACK_SIZEOF(parent_thr_stack),
parent_handler,
NULL, NULL, NULL,
PRIORITY, 0, K_NO_WAIT);
k_thread_resource_pool_assign(&parent_thr, &res_pool);
k_sem_take(&sync_sem, K_FOREVER);
zassert_true(parent_res_pool_ptr == child_res_pool_ptr,
"Resource pool of the parent thread not inherited,"
" by child thread");
}

View file

@ -48,6 +48,7 @@ extern void test_create_new_supervisor_thread_from_user(void);
extern void test_create_new_essential_thread_from_user(void);
extern void test_create_new_higher_prio_thread_from_user(void);
extern void test_create_new_invalid_prio_thread_from_user(void);
extern void test_inherit_resource_pool(void);
void test_main(void)
{
@ -90,6 +91,7 @@ void test_main(void)
ztest_unit_test(test_create_new_supervisor_thread_from_user),
ztest_unit_test(test_create_new_essential_thread_from_user),
ztest_unit_test(test_create_new_higher_prio_thread_from_user),
ztest_unit_test(test_inherit_resource_pool),
ztest_unit_test(test_create_new_invalid_prio_thread_from_user)
);
ztest_run_test_suite(memory_protection_test_suite);

View file

@ -30,6 +30,15 @@ static inline void set_fault_valid(bool valid)
#define MSG_Q_SIZE (10)
#define MSG_Q_MAX_NUM_MSGS (10)
#define MSG_Q_ALIGN (2)
#define PRIORITY 5
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
#define BLK_SIZE_MIN 16
#define BLK_SIZE_MAX 64
#define BLK_NUM_MIN 8
#define BLK_NUM_MAX 2
#define BLK_ALIGN BLK_SIZE_MIN
#define SEM_INIT_VAL (0U)
#define SEM_MAX_VAL (1U)
/* For mem_domain.c */
#define MEM_DOMAIN_STACK_SIZE CONFIG_MAIN_STACK_SIZE
@ -48,3 +57,12 @@ static inline void set_fault_valid(bool valid)
/* for kobject.c */
#define KOBJECT_STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
#ifndef _TEST_SYSCALLS_H_
#define _TEST_SYSCALLS_H_
__syscall struct k_mem_pool *ret_resource_pool_ptr(void);
#include <syscalls/mem_protect.h>
#endif /* _TEST_SYSCALLS_H_ */