Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
main.c File Reference
#include <zephyr.h>
#include <ztest.h>
#include <kernel_structs.h>
#include <string.h>
#include <stdlib.h>
#include <app_memory/app_memdomain.h>
#include <sys/util.h>
#include <debug/stack.h>
#include <syscall_handler.h>
#include "test_syscall.h"
#include <linker/linker-defs.h>
#include <syscalls/check_syscall_context_mrsh.c>

Macros

#define INFO(fmt, ...)   printk(fmt, ##__VA_ARGS__)
 
#define PIPE_LEN   1
 
#define BYTES_TO_READ_WRITE   1
 
#define STACKSIZE   (256 + CONFIG_TEST_EXTRA_STACKSIZE)
 
#define test_oops(provided, expected)
 

Functions

 K_SEM_DEFINE (test_revoke_sem, 0, 1)
 
 K_APPMEM_PARTITION_DEFINE (default_part)
 
 K_APP_BMEM (default_part)
 Test to pass a user object to system call. More...
 
static void set_fault (unsigned int reason)
 
void k_sys_fatal_error_handler (unsigned int reason, const z_arch_esf_t *pEsf)
 Fatal error policy handler. More...
 
static void test_is_usermode (void)
 Test to check if the thread is in user mode. More...
 
static void test_write_control (void)
 Test to write to a control register. More...
 
static void test_disable_mmu_mpu (void)
 Test to disable memory protection. More...
 
static void test_read_kernram (void)
 Test to read from kernel RAM. More...
 
static void test_write_kernram (void)
 Test to write to kernel RAM. More...
 
static void test_write_kernro (void)
 Test to write kernel RO. More...
 
static void test_write_kerntext (void)
 Test to write to kernel text section. More...
 
static void test_read_kernel_data (void)
 Test to read from kernel data section. More...
 
static void test_write_kernel_data (void)
 Test to write to kernel data section. More...
 
 K_APP_DMEM (default_part) volatile
 Test to read provileged stack. More...
 
static void test_write_priv_stack (void)
 Test to write to privilege stack. More...
 
static void test_pass_noperms_object (void)
 Test to pass object to a system call without permissions. More...
 
void thread_body (void)
 
static void test_start_kernel_thread (void)
 Test to start kernel thread from usermode. More...
 
static void test_read_other_stack (void)
 
static void test_write_other_stack (void)
 
static void test_revoke_noperms_object (void)
 Test to revoke access to kobject without permission. More...
 
static void test_access_after_revoke (void)
 Test to access object after revoking access. More...
 
static void umode_enter_func (void)
 
static void test_user_mode_enter (void)
 Test to check supervisor thread enter one-way to usermode. More...
 
 K_PIPE_DEFINE (kpipe, 1, 1)
 
static void test_read_kobject_user_pipe (void)
 Test to read from kobject using pipe. More...
 
static void user_half (void *arg1, void *arg2, void *arg3)
 
static void spawn_user (volatile bool *to_modify)
 
static void drop_user (volatile bool *to_modify)
 
static void test_init_and_access_other_memdomain (void)
 Test creation of new memory domains. More...
 
static void test_domain_add_thread_drop_to_user (void)
 
static void test_domain_add_part_drop_to_user (void)
 
static void test_domain_remove_part_drop_to_user (void)
 
static void test_domain_add_thread_context_switch (void)
 
static void test_domain_add_part_context_switch (void)
 
static void test_domain_remove_part_context_switch (void)
 
void test_unimplemented_syscall (void)
 Test unimplemented system call. More...
 
void test_bad_syscall (void)
 Test bad syscall handler. More...
 
void test_object_recycle (void)
 
void test_oops_panic (void)
 
void test_oops_oops (void)
 
void test_oops_exception (void)
 
void test_oops_maxint (void)
 
void test_oops_stackcheck (void)
 
void test_syscall_context (void)
 
static void tls_leakage_user_part (void *p1, void *p2, void *p3)
 
void test_tls_leakage (void)
 
void tls_entry (void *p1, void *p2, void *p3)
 
void test_tls_pointer (void)
 
void test_main (void)
 

Variables

struct k_mem_domain alternate_domain
 
static ZTEST_BMEM volatile bool expect_fault
 
static ZTEST_BMEM volatile unsigned int expected_reason
 
static int kernel_data
 
static struct k_sem ksem
 
k_thread_stack_t ztest_thread_stack []
 
static struct k_sem recycle_sem
 

Macro Definition Documentation

◆ BYTES_TO_READ_WRITE

#define BYTES_TO_READ_WRITE   1

◆ INFO

#define INFO (   fmt,
  ... 
)    printk(fmt, ##__VA_ARGS__)

◆ PIPE_LEN

#define PIPE_LEN   1

◆ STACKSIZE

#define STACKSIZE   (256 + CONFIG_TEST_EXTRA_STACKSIZE)

◆ test_oops

#define test_oops (   provided,
  expected 
)
Value:
do { \
expect_fault = true; \
expected_reason = expected; \
z_except_reason(provided); \
} while (false)
static ZTEST_BMEM struct thread_data expected

Function Documentation

◆ drop_user()

static void drop_user ( volatile bool to_modify)
static

◆ K_APP_BMEM()

K_APP_BMEM ( default_part  ) volatile

Test to pass a user object to system call.

Test to write to kobject using pipe.

◆ K_APP_DMEM()

K_APP_DMEM ( default_part  ) volatile

Test to read provileged stack.

◆ K_APPMEM_PARTITION_DEFINE()

K_APPMEM_PARTITION_DEFINE ( default_part  )

◆ K_PIPE_DEFINE()

K_PIPE_DEFINE ( kpipe  ,
,
 
)

◆ K_SEM_DEFINE()

K_SEM_DEFINE ( test_revoke_sem  ,
,
 
)

◆ set_fault()

static void set_fault ( unsigned int  reason)
static

◆ spawn_user()

static void spawn_user ( volatile bool to_modify)
static

◆ test_access_after_revoke()

static void test_access_after_revoke ( void  )
static

Test to access object after revoking access.

◆ test_bad_syscall()

void test_bad_syscall ( void  )

Test bad syscall handler.

When a system call handler decides to terminate the calling thread, the kernel will produce error which indicates the context, where the faulting system call was made from user code.

◆ test_disable_mmu_mpu()

static void test_disable_mmu_mpu ( void  )
static

Test to disable memory protection.

◆ test_domain_add_part_context_switch()

static void test_domain_add_part_context_switch ( void  )
static

◆ test_domain_add_part_drop_to_user()

static void test_domain_add_part_drop_to_user ( void  )
static

◆ test_domain_add_thread_context_switch()

static void test_domain_add_thread_context_switch ( void  )
static

Show that changing between memory domains and then switching to another thread in the same domain works as expected.

◆ test_domain_add_thread_drop_to_user()

static void test_domain_add_thread_drop_to_user ( void  )
static

Show that changing between memory domains and dropping to user mode works as expected.

◆ test_domain_remove_part_context_switch()

static void test_domain_remove_part_context_switch ( void  )
static

Show that self-removing a partition from a domain we are a member of, and then switching to another user thread in the same domain faults as expected.

◆ test_domain_remove_part_drop_to_user()

static void test_domain_remove_part_drop_to_user ( void  )
static

Show that self-removing a partition from a domain we are a member of, and then dropping to user mode faults as expected.

◆ test_init_and_access_other_memdomain()

static void test_init_and_access_other_memdomain ( void  )
static

Test creation of new memory domains.

We initialize a new memory domain and show that its partition configuration is correct. This new domain has "alt_part" in it, but not "default_part". We then try to modify data in "default_part" and show it produces an exception since that partition is not in the new domain.

This caught a bug once where an MMU system copied page tables for the new domain and accidentally copied memory partition permissions from the source page tables, allowing the write to "default_part" to work.

◆ test_is_usermode()

static void test_is_usermode ( void  )
static

Test to check if the thread is in user mode.

◆ test_main()

void test_main ( void  )

test case main entry

◆ test_object_recycle()

void test_object_recycle ( void  )

◆ test_oops_exception()

void test_oops_exception ( void  )

◆ test_oops_maxint()

void test_oops_maxint ( void  )

◆ test_oops_oops()

void test_oops_oops ( void  )

◆ test_oops_panic()

void test_oops_panic ( void  )

◆ test_oops_stackcheck()

void test_oops_stackcheck ( void  )

◆ test_pass_noperms_object()

static void test_pass_noperms_object ( void  )
static

Test to pass object to a system call without permissions.

◆ test_read_kernel_data()

static void test_read_kernel_data ( void  )
static

Test to read from kernel data section.

◆ test_read_kernram()

static void test_read_kernram ( void  )
static

Test to read from kernel RAM.

◆ test_read_kobject_user_pipe()

static void test_read_kobject_user_pipe ( void  )
static

Test to read from kobject using pipe.

◆ test_read_other_stack()

static void test_read_other_stack ( void  )
static

◆ test_revoke_noperms_object()

static void test_revoke_noperms_object ( void  )
static

Test to revoke access to kobject without permission.

User thread can only revoke their own access to an object. In that test user thread to revokes access to unathorized object, as a result the system will assert.

◆ test_start_kernel_thread()

static void test_start_kernel_thread ( void  )
static

Test to start kernel thread from usermode.

◆ test_syscall_context()

void test_syscall_context ( void  )

◆ test_tls_leakage()

void test_tls_leakage ( void  )

◆ test_tls_pointer()

void test_tls_pointer ( void  )

◆ test_unimplemented_syscall()

void test_unimplemented_syscall ( void  )

Test unimplemented system call.

Created a syscall with name missing_syscall() without a verification function. The kernel shall safety handle invocations of unimplemented system calls.

◆ test_user_mode_enter()

static void test_user_mode_enter ( void  )
static

Test to check supervisor thread enter one-way to usermode.

A thread running in supervisor mode must have one-way operation ability to drop privileges to user mode.

◆ test_write_control()

static void test_write_control ( void  )
static

Test to write to a control register.

◆ test_write_kernel_data()

static void test_write_kernel_data ( void  )
static

Test to write to kernel data section.

◆ test_write_kernram()

static void test_write_kernram ( void  )
static

Test to write to kernel RAM.

◆ test_write_kernro()

static void test_write_kernro ( void  )
static

Test to write kernel RO.

◆ test_write_kerntext()

static void test_write_kerntext ( void  )
static

Test to write to kernel text section.

◆ test_write_other_stack()

static void test_write_other_stack ( void  )
static

◆ test_write_priv_stack()

static void test_write_priv_stack ( void  )
static

Test to write to privilege stack.

◆ thread_body()

void thread_body ( void  )

◆ tls_entry()

void tls_entry ( void p1,
void p2,
void p3 
)

◆ tls_leakage_user_part()

static void tls_leakage_user_part ( void p1,
void p2,
void p3 
)
static

◆ umode_enter_func()

static void umode_enter_func ( void  )
static

◆ user_half()

static void user_half ( void arg1,
void arg2,
void arg3 
)
static

Variable Documentation

◆ alternate_domain

struct k_mem_domain alternate_domain

◆ expect_fault

ZTEST_BMEM volatile bool expect_fault
static

◆ expected_reason

ZTEST_BMEM volatile unsigned int expected_reason
static

◆ kernel_data

int kernel_data
static

◆ ksem

struct k_sem ksem
static

◆ recycle_sem

struct k_sem recycle_sem
static

◆ ztest_thread_stack

k_thread_stack_t ztest_thread_stack[]
extern