audk/MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c
Michael D Kinney 9344f09215 MdePkg: Replace BSD License with BSD+Patent License
https://bugzilla.tianocore.org/show_bug.cgi?id=1373

Replace BSD 2-Clause License with BSD+Patent License.  This change is
based on the following emails:

  https://lists.01.org/pipermail/edk2-devel/2019-February/036260.html
  https://lists.01.org/pipermail/edk2-devel/2018-October/030385.html

RFCs with detailed process for the license change:

  V3: https://lists.01.org/pipermail/edk2-devel/2019-March/038116.html
  V2: https://lists.01.org/pipermail/edk2-devel/2019-March/037669.html
  V1: https://lists.01.org/pipermail/edk2-devel/2019-March/037500.html

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
2019-04-09 10:58:13 -07:00

50 lines
1.5 KiB
C

/** @file
Base Stack Check library for GCC/clang.
Use -fstack-protector-all compiler flag to make the compiler insert the
__stack_chk_guard "canary" value into the stack and check the value prior
to exiting the function. If the "canary" is overwritten __stack_chk_fail()
is called. This is GCC specific code.
Copyright (c) 2012, Apple Inc. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Base.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
/// "canary" value that is inserted by the compiler into the stack frame.
VOID *__stack_chk_guard = (VOID*)0x0AFF;
// If ASLR was enabled we could use
//void (*__stack_chk_guard)(void) = __stack_chk_fail;
/**
Error path for compiler generated stack "canary" value check code. If the
stack canary has been overwritten this function gets called on exit of the
function.
**/
VOID
__stack_chk_fail (
VOID
)
{
UINT8 DebugPropertyMask;
DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow in function %a.\n", __builtin_return_address(0)));
//
// Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even if
// BaseDebugLibNull is in use.
//
DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask);
if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
CpuBreakpoint ();
} else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
CpuDeadLoop ();
}
}