Zephyr Project API 3.7.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
main.c File Reference
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>

Macros

#define STACKSIZE   (2048 + CONFIG_TEST_EXTRA_STACK_SIZE)
 

Functions

void k_sys_fatal_error_handler (unsigned int reason, const struct arch_esf *esf)
 Fatal error policy handler.
 
void check_input (const char *name, const char *input)
 check_input
 
void print_loop (const char *name)
 print_loop
 
void alternate_thread (void *p1, void *p2, void *p3)
 This thread passes a long string to check_input function.
 
 K_THREAD_STACK_DEFINE (alt_thread_stack_area,(2048+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 ZTEST_USER (stackprot, test_stackprot)
 test Stack Protector feature using canary
 
 ZTEST (stackprot, test_create_alt_thread)
 Test optional mechanism to detect stack overflow.
 
void alternate_thread_canary (void *arg1, void *arg2, void *arg3)
 This thread checks its canary value against its parent canary.
 
 ZTEST (stackprot, test_canary_value)
 Test stack canaries behavior.
 
 ZTEST_SUITE (stackprot, NULL, NULL, NULL, NULL, NULL)
 

Variables

static ZTEST_BMEM int count
 
static ZTEST_BMEM int ret = TC_PASS
 
static struct k_thread alt_thread_data
 

Macro Definition Documentation

◆ STACKSIZE

#define STACKSIZE   (2048 + CONFIG_TEST_EXTRA_STACK_SIZE)

Function Documentation

◆ alternate_thread()

void alternate_thread ( void *  p1,
void *  p2,
void *  p3 
)

This thread passes a long string to check_input function.

It terminates due to stack overflow and reports "Stack Check Fail" when stack protection feature is enabled. Hence it will not execute the print_loop function and will not set ret to TC_FAIL.

◆ alternate_thread_canary()

void alternate_thread_canary ( void *  arg1,
void *  arg2,
void *  arg3 
)

This thread checks its canary value against its parent canary.

If CONFIG_STACK_CANARIES_TLS is enabled, it is expected that the canaries have different values, otherwise there is only one global canary and the value should be the same.

◆ check_input()

void check_input ( const char *  name,
const char *  input 
)

check_input

This function copies the input string to a buffer of 16 characters and prints the name and buffer as a string. If the input string is longer than the buffer, an error condition is detected.

When stack protection feature is enabled (see prj.conf file), the system error handler is invoked and reports a "Stack Check Fail" error. When stack protection feature is not enabled, the system crashes with error like: Trying to execute code outside RAM or ROM.

◆ K_THREAD_STACK_DEFINE()

K_THREAD_STACK_DEFINE ( alt_thread_stack_area  ,
(2048+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ print_loop()

void print_loop ( const char *  name)

print_loop

This function calls check_input 6 times with the input name and a short string, which is printed properly by check_input.

Parameters
namecaller identification string

◆ ZTEST() [1/2]

ZTEST ( stackprot  ,
test_canary_value   
)

Test stack canaries behavior.

Test that canaries value are different between threads when CONFIG_STACK_CANARIES_TLS is enabled.

◆ ZTEST() [2/2]

ZTEST ( stackprot  ,
test_create_alt_thread   
)

Test optional mechanism to detect stack overflow.

Test that the system provides an optional mechanism to detect when supervisor threads overflow stack memory buffer.

◆ ZTEST_SUITE()

ZTEST_SUITE ( stackprot  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_USER()

ZTEST_USER ( stackprot  ,
test_stackprot   
)

test Stack Protector feature using canary

This is the test program to test stack protection using canary. The main thread starts a second thread, which generates a stack check failure. By design, the second thread will not complete its execution and will not set ret to TC_FAIL. This is the entry point to the test stack protection feature. It starts the thread that tests stack protection, then prints out a few messages before terminating.

Variable Documentation

◆ alt_thread_data

struct k_thread alt_thread_data
static

◆ count

ZTEST_BMEM int count
static

◆ ret

ZTEST_BMEM int ret = TC_PASS
static