blob: 928fdc6ec9ad8230c2ede9a8b03aa86253ea4a4c [file] [log] [blame]
Kumar Galad12d8af2016-10-05 12:01:54 -05001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Kumar Galad12d8af2016-10-05 12:01:54 -05005 */
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
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020019#include <zephyr/toolchain.h> /* compiler specific configurations */
Kumar Galad12d8af2016-10-05 12:01:54 -050020
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020021#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>
Kumar Galad12d8af2016-10-05 12:01:54 -050026
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 */
Kumar Galabc181592019-10-03 18:32:35 -050035void _StackCheckHandler(void)
Kumar Galad12d8af2016-10-05 12:01:54 -050036{
37 /* Stack canary error is a software fatal condition; treat it as such.
38 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -070039 z_except_reason(K_ERR_STACK_CHK_FAIL);
Enjia Mai53ca7092021-01-15 17:09:58 +080040 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Kumar Galad12d8af2016-10-05 12:01:54 -050041}
42
43/* Global variable */
44
45/*
46 * Symbol referenced by GCC compiler generated code for canary value.
Patrik Flykt4344e272019-03-08 14:19:05 -070047 * The canary value gets initialized in z_cstart().
Kumar Galad12d8af2016-10-05 12:01:54 -050048 */
Flavio Ceolind16c5b92023-08-01 15:07:57 -070049#ifdef CONFIG_STACK_CANARIES_TLS
Lars-Ove Karlsson25cdda12024-06-12 12:51:21 +020050__thread volatile uintptr_t __stack_chk_guard;
Flavio Ceolind16c5b92023-08-01 15:07:57 -070051#elif CONFIG_USERSPACE
Lars-Ove Karlsson25cdda12024-06-12 12:51:21 +020052K_APP_DMEM(z_libc_partition) volatile uintptr_t __stack_chk_guard;
Andrew Boie01100ea2019-02-21 15:02:22 -080053#else
Lars-Ove Karlsson25cdda12024-06-12 12:51:21 +020054__noinit volatile uintptr_t __stack_chk_guard;
Andrew Boie01100ea2019-02-21 15:02:22 -080055#endif
Kumar Galad12d8af2016-10-05 12:01:54 -050056
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 */
64FUNC_ALIAS(_StackCheckHandler, __stack_chk_fail, void);