1 /*
2  * Copyright (c) 2012-2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Compiler stack protection (kernel part)
10  *
11  * This module provides functions to support compiler stack protection
12  * using canaries.  This feature is enabled with configuration
13  * CONFIG_STACK_CANARIES=y or CONFIG_STACK_CANARIES_STRONG=y or
14  * CONFIG_STACK_CANARIES_ALL=y or CONFIG_STACK_CANARIES_EXPLICIT=y.
15  *
16  * When this feature is enabled, the compiler generated code refers to
17  * function __stack_chk_fail and global variable __stack_chk_guard.
18  */
19 
20 #include <zephyr/toolchain.h> /* compiler specific configurations */
21 
22 #include <zephyr/kernel_structs.h>
23 #include <zephyr/toolchain.h>
24 #include <zephyr/linker/sections.h>
25 #include <zephyr/kernel.h>
26 #include <zephyr/app_memory/app_memdomain.h>
27 
28 /**
29  *
30  * @brief Stack canary error handler
31  *
32  * This function is invoked when a stack canary error is detected.
33  *
34  * @return Does not return
35  */
_StackCheckHandler(void)36 void _StackCheckHandler(void)
37 {
38 	/* Stack canary error is a software fatal condition; treat it as such.
39 	 */
40 	z_except_reason(K_ERR_STACK_CHK_FAIL);
41 	CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
42 }
43 
44 /* Global variable */
45 
46 /*
47  * Symbol referenced by GCC compiler generated code for canary value.
48  * The canary value gets initialized in z_cstart().
49  */
50 #ifdef CONFIG_STACK_CANARIES_TLS
51 Z_THREAD_LOCAL volatile uintptr_t __stack_chk_guard;
52 #elif CONFIG_USERSPACE
53 K_APP_DMEM(z_libc_partition) volatile uintptr_t __stack_chk_guard;
54 #else
55 __noinit volatile uintptr_t __stack_chk_guard;
56 #endif
57 
58 /**
59  *
60  * @brief Referenced by GCC compiler generated code
61  *
62  * This routine is invoked when a stack canary error is detected, indicating
63  * a buffer overflow or stack corruption problem.
64  */
65 FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void);
66