1 | /* $Id: stacksup-vcc.cpp 95870 2022-07-27 02:38:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - No-CRT - Basic allocators, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/asm-amd64-x86.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 |
|
---|
37 | #include "internal/compiler-vcc.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /** Variable descriptor. */
|
---|
44 | typedef struct RTC_VAR_DESC_T
|
---|
45 | {
|
---|
46 | int32_t offFrame;
|
---|
47 | uint32_t cbVar;
|
---|
48 | const char *pszName;
|
---|
49 | } RTC_VAR_DESC_T;
|
---|
50 |
|
---|
51 | /** Frame descriptor. */
|
---|
52 | typedef struct RTC_FRAME_DESC_T
|
---|
53 | {
|
---|
54 | uint32_t cVars;
|
---|
55 | RTC_VAR_DESC_T const *paVars;
|
---|
56 | } RTC_FRAME_DESC_T;
|
---|
57 |
|
---|
58 | #define VARIABLE_MARKER_PRE 0xcccccccc
|
---|
59 | #define VARIABLE_MARKER_POST 0xcccccccc
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Alloca allocation entry.
|
---|
64 | * @note For whatever reason the pNext and cb members are misaligned on 64-bit
|
---|
65 | * targets. 32-bit targets OTOH adds padding to keep the structure size
|
---|
66 | * and pNext + cb offsets the same.
|
---|
67 | */
|
---|
68 | #pragma pack(4)
|
---|
69 | typedef struct RTC_ALLOC_ENTRY
|
---|
70 | {
|
---|
71 | uint32_t uGuard1;
|
---|
72 | RTC_ALLOC_ENTRY *pNext;
|
---|
73 | #if ARCH_BITS == 32
|
---|
74 | uint32_t pNextPad;
|
---|
75 | #endif
|
---|
76 | size_t cb;
|
---|
77 | #if ARCH_BITS == 32
|
---|
78 | uint32_t cbPad;
|
---|
79 | #endif
|
---|
80 | uint32_t auGuard2[3];
|
---|
81 | } RTC_ALLOC_ENTRY;
|
---|
82 | #pragma pack()
|
---|
83 |
|
---|
84 | #define ALLOCA_FILLER_BYTE 0xcc
|
---|
85 | #define ALLOCA_FILLER_32 0xcccccccc
|
---|
86 |
|
---|
87 |
|
---|
88 | /*********************************************************************************************************************************
|
---|
89 | * External Symbols *
|
---|
90 | *********************************************************************************************************************************/
|
---|
91 | extern "C" void __fastcall _RTC_CheckStackVars(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar); /* nocrt-stack.asm */
|
---|
92 | extern "C" uintptr_t __security_cookie;
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Initializes the security cookie value.
|
---|
97 | *
|
---|
98 | * This must be called as the first thing by the startup code. We must also no
|
---|
99 | * do anything fancy here.
|
---|
100 | */
|
---|
101 | void rtVccInitSecurityCookie(void) RT_NOEXCEPT
|
---|
102 | {
|
---|
103 | __security_cookie = (uintptr_t)ASMReadTSC() ^ (uintptr_t)&__security_cookie;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | DECLASM(void) _RTC_StackVarCorrupted(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar)
|
---|
108 | {
|
---|
109 | RTAssertMsg2("\n\n!!Stack corruption!!\n\n"
|
---|
110 | "%p LB %#x - %s\n",
|
---|
111 | pbFrame + pVar->offFrame, pVar->cbVar, pVar->pszName);
|
---|
112 | RT_BREAKPOINT();
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | DECLASM(void) _RTC_SecurityCookieMismatch(uintptr_t uCookie)
|
---|
117 | {
|
---|
118 | RTAssertMsg2("\n\n!!Stack cookie corruption!!\n\n"
|
---|
119 | "expected %p, found %p\n",
|
---|
120 | __security_cookie, uCookie);
|
---|
121 | RT_BREAKPOINT();
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | extern "C" void __cdecl _RTC_UninitUse(const char *pszVar)
|
---|
126 | {
|
---|
127 | RTAssertMsg2("\n\n!!Used uninitialized variable %s at %p!!\n\n",
|
---|
128 | pszVar ? pszVar : "", ASMReturnAddress());
|
---|
129 | RT_BREAKPOINT();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /** @todo reimplement in assembly (feeling too lazy right now). */
|
---|
134 | extern "C" void __fastcall _RTC_CheckStackVars2(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar, RTC_ALLOC_ENTRY *pHead)
|
---|
135 | {
|
---|
136 | while (pHead)
|
---|
137 | {
|
---|
138 | if ( pHead->uGuard1 == ALLOCA_FILLER_32
|
---|
139 | #if 1 && ARCH_BITS == 32
|
---|
140 | && pHead->pNextPad == ALLOCA_FILLER_32
|
---|
141 | && pHead->cbPad == ALLOCA_FILLER_32
|
---|
142 | #endif
|
---|
143 | && pHead->auGuard2[0] == ALLOCA_FILLER_32
|
---|
144 | && pHead->auGuard2[1] == ALLOCA_FILLER_32
|
---|
145 | && pHead->auGuard2[2] == ALLOCA_FILLER_32
|
---|
146 | && *(uint32_t const *)((uint8_t const *)pHead + pHead->cb - sizeof(uint32_t)) == ALLOCA_FILLER_32)
|
---|
147 | { /* likely */ }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | RTAssertMsg2("\n\n!!Stack corruption (alloca)!!\n\n"
|
---|
151 | "%p LB %#x\n",
|
---|
152 | pHead, pHead->cb);
|
---|
153 | RT_BREAKPOINT();
|
---|
154 | }
|
---|
155 | pHead = pHead->pNext;
|
---|
156 | }
|
---|
157 |
|
---|
158 | _RTC_CheckStackVars(pbFrame, pVar);
|
---|
159 | }
|
---|
160 |
|
---|