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.
14  *
15  * When this feature is enabled, the compiler generated code refers to
16  * function __stack_chk_fail and global variable __stack_chk_guard.
17  */
18 
19 #include <zephyr/toolchain.h> /* compiler specific configurations */
20 
21 #include <zephyr/kernel_structs.h>
22 #include <zephyr/toolchain.h>
23 #include <zephyr/linker/sections.h>
24 #include <zephyr/kernel.h>
25 #include <zephyr/app_memory/app_memdomain.h>
26 
27 /**
28  *
29  * @brief Stack canary error handler
30  *
31  * This function is invoked when a stack canary error is detected.
32  *
33  * @return Does not return
34  */
_StackCheckHandler(void)35 void _StackCheckHandler(void)
36 {
37 	/* Stack canary error is a software fatal condition; treat it as such.
38 	 */
39 	z_except_reason(K_ERR_STACK_CHK_FAIL);
40 	CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
41 }
42 
43 /* Global variable */
44 
45 /*
46  * Symbol referenced by GCC compiler generated code for canary value.
47  * The canary value gets initialized in z_cstart().
48  */
49 #ifdef CONFIG_STACK_CANARIES_TLS
50 __thread uintptr_t __stack_chk_guard;
51 #elif CONFIG_USERSPACE
52 K_APP_DMEM(z_libc_partition) uintptr_t __stack_chk_guard;
53 #else
54 __noinit uintptr_t __stack_chk_guard;
55 #endif
56 
57 /**
58  *
59  * @brief Referenced by GCC compiler generated code
60  *
61  * This routine is invoked when a stack canary error is detected, indicating
62  * a buffer overflow or stack corruption problem.
63  */
64 FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void);
65