]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c
d3efb8e4622c14ddda1196fcb28d248e82c9ede4
[mirror_edk2.git] / MdePkg / Library / BaseStackCheckLib / BaseStackCheckGcc.c
1 /** @file
2 Base Stack Check library for GCC/clang.
3
4 Use -fstack-protector-all compiler flag to make the compiler insert the
5 __stack_chk_guard "canary" value into the stack and check the value prior
6 to exiting the function. If the "canary" is overwritten __stack_chk_fail()
7 is called. This is GCC specific code.
8
9 Copyright (c) 2012, Apple Inc. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
11
12 **/
13
14 #include <Base.h>
15 #include <Library/BaseLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/PcdLib.h>
18
19 /// "canary" value that is inserted by the compiler into the stack frame.
20 VOID *__stack_chk_guard = (VOID*)0x0AFF;
21
22 // If ASLR was enabled we could use
23 //void (*__stack_chk_guard)(void) = __stack_chk_fail;
24
25 /**
26 Error path for compiler generated stack "canary" value check code. If the
27 stack canary has been overwritten this function gets called on exit of the
28 function.
29 **/
30 VOID
31 __stack_chk_fail (
32 VOID
33 )
34 {
35 UINT8 DebugPropertyMask;
36
37 DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow in function %a.\n", __builtin_return_address(0)));
38
39 //
40 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even if
41 // BaseDebugLibNull is in use.
42 //
43 DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask);
44 if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
45 CpuBreakpoint ();
46 } else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
47 CpuDeadLoop ();
48 }
49 }