1 | /* $Id: stacksup-vcc.cpp 96420 2022-08-23 02:14:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Visual C++ Compiler - Stack Checking C/C++ Support.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.alldomusa.eu.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/nocrt.h"
|
---|
42 |
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #include <iprt/asm-amd64-x86.h>
|
---|
45 | #ifndef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
46 | # include <iprt/assert.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #include "internal/compiler-vcc.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /** Variable descriptor. */
|
---|
56 | typedef struct RTC_VAR_DESC_T
|
---|
57 | {
|
---|
58 | int32_t offFrame;
|
---|
59 | uint32_t cbVar;
|
---|
60 | const char *pszName;
|
---|
61 | } RTC_VAR_DESC_T;
|
---|
62 |
|
---|
63 | /** Frame descriptor. */
|
---|
64 | typedef struct RTC_FRAME_DESC_T
|
---|
65 | {
|
---|
66 | uint32_t cVars;
|
---|
67 | RTC_VAR_DESC_T const *paVars;
|
---|
68 | } RTC_FRAME_DESC_T;
|
---|
69 |
|
---|
70 | #define VARIABLE_MARKER_PRE 0xcccccccc
|
---|
71 | #define VARIABLE_MARKER_POST 0xcccccccc
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Alloca allocation entry.
|
---|
76 | * @note For whatever reason the pNext and cb members are misaligned on 64-bit
|
---|
77 | * targets. 32-bit targets OTOH adds padding to keep the structure size
|
---|
78 | * and pNext + cb offsets the same.
|
---|
79 | */
|
---|
80 | #pragma pack(4)
|
---|
81 | typedef struct RTC_ALLOC_ENTRY
|
---|
82 | {
|
---|
83 | uint32_t uGuard1;
|
---|
84 | RTC_ALLOC_ENTRY *pNext;
|
---|
85 | #if ARCH_BITS == 32
|
---|
86 | uint32_t pNextPad;
|
---|
87 | #endif
|
---|
88 | size_t cb;
|
---|
89 | #if ARCH_BITS == 32
|
---|
90 | uint32_t cbPad;
|
---|
91 | #endif
|
---|
92 | uint32_t auGuard2[3];
|
---|
93 | } RTC_ALLOC_ENTRY;
|
---|
94 | #pragma pack()
|
---|
95 |
|
---|
96 | #define ALLOCA_FILLER_BYTE 0xcc
|
---|
97 | #define ALLOCA_FILLER_32 0xcccccccc
|
---|
98 |
|
---|
99 |
|
---|
100 | /*********************************************************************************************************************************
|
---|
101 | * External Symbols *
|
---|
102 | *********************************************************************************************************************************/
|
---|
103 | extern "C" void __fastcall _RTC_CheckStackVars(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar); /* nocrt-stack.asm */
|
---|
104 | extern "C" uintptr_t __security_cookie;
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Initializes the security cookie value.
|
---|
109 | *
|
---|
110 | * This must be called as the first thing by the startup code. We must also no
|
---|
111 | * do anything fancy here.
|
---|
112 | */
|
---|
113 | void rtVccInitSecurityCookie(void) RT_NOEXCEPT
|
---|
114 | {
|
---|
115 | __security_cookie = (uintptr_t)ASMReadTSC() ^ (uintptr_t)&__security_cookie;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | DECLASM(void) _RTC_StackVarCorrupted(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar)
|
---|
120 | {
|
---|
121 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
122 | RTAssertMsg2("\n\n!!Stack corruption!!\n\n"
|
---|
123 | "%p LB %#x - %s\n",
|
---|
124 | pbFrame + pVar->offFrame, pVar->cbVar, pVar->pszName);
|
---|
125 | #else
|
---|
126 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack corruption!!\r\n\r\n"));
|
---|
127 | rtNoCrtFatalWritePtr(pbFrame + pVar->offFrame);
|
---|
128 | rtNoCrtFatalWrite(RT_STR_TUPLE(" LB "));
|
---|
129 | rtNoCrtFatalWriteX32(pVar->cbVar);
|
---|
130 | rtNoCrtFatalWrite(RT_STR_TUPLE(" - "));
|
---|
131 | rtNoCrtFatalWriteStr(pVar->pszName);
|
---|
132 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
133 | #endif
|
---|
134 | RT_BREAKPOINT();
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | DECLASM(void) _RTC_SecurityCookieMismatch(uintptr_t uCookie)
|
---|
139 | {
|
---|
140 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
141 | RTAssertMsg2("\n\n!!Stack cookie corruption!!\n\n"
|
---|
142 | "expected %p, found %p\n",
|
---|
143 | __security_cookie, uCookie);
|
---|
144 | #else
|
---|
145 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack cookie corruption!!\r\n\r\n"
|
---|
146 | "expected"));
|
---|
147 | rtNoCrtFatalWritePtr((void *)__security_cookie);
|
---|
148 | rtNoCrtFatalWrite(RT_STR_TUPLE(", found "));
|
---|
149 | rtNoCrtFatalWritePtr((void *)uCookie);
|
---|
150 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
151 | #endif
|
---|
152 | RT_BREAKPOINT();
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | #ifdef RT_ARCH_X86
|
---|
157 | DECLASM(void) _RTC_CheckEspFailed(uintptr_t uEip, uintptr_t uEsp, uintptr_t uEbp)
|
---|
158 | {
|
---|
159 | # ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
160 | RTAssertMsg2("\n\n!!ESP check failed!!\n\n"
|
---|
161 | "eip=%p esp=%p ebp=%p\n",
|
---|
162 | uEip, uEsp, uEbp);
|
---|
163 | # else
|
---|
164 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!ESP check failed!!\r\n\r\n"
|
---|
165 | "eip="));
|
---|
166 | rtNoCrtFatalWritePtr((void *)uEip);
|
---|
167 | rtNoCrtFatalWrite(RT_STR_TUPLE(" esp="));
|
---|
168 | rtNoCrtFatalWritePtr((void *)uEsp);
|
---|
169 | rtNoCrtFatalWrite(RT_STR_TUPLE(" ebp="));
|
---|
170 | rtNoCrtFatalWritePtr((void *)uEbp);
|
---|
171 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
172 | # endif
|
---|
173 | RT_BREAKPOINT();
|
---|
174 | }
|
---|
175 | #endif
|
---|
176 |
|
---|
177 |
|
---|
178 | extern "C" void __cdecl _RTC_UninitUse(const char *pszVar)
|
---|
179 | {
|
---|
180 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
181 | RTAssertMsg2("\n\n!!Used uninitialized variable %s at %p!!\n\n",
|
---|
182 | pszVar ? pszVar : "", ASMReturnAddress());
|
---|
183 | #else
|
---|
184 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Used uninitialized variable "));
|
---|
185 | rtNoCrtFatalWriteStr(pszVar);
|
---|
186 | rtNoCrtFatalWrite(RT_STR_TUPLE(" at "));
|
---|
187 | rtNoCrtFatalWritePtr(ASMReturnAddress());
|
---|
188 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("!!\r\n\r\n"));
|
---|
189 | #endif
|
---|
190 | RT_BREAKPOINT();
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /** @todo reimplement in assembly (feeling too lazy right now). */
|
---|
195 | extern "C" void __fastcall _RTC_CheckStackVars2(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar, RTC_ALLOC_ENTRY *pHead)
|
---|
196 | {
|
---|
197 | while (pHead)
|
---|
198 | {
|
---|
199 | if ( pHead->uGuard1 == ALLOCA_FILLER_32
|
---|
200 | #if 1 && ARCH_BITS == 32
|
---|
201 | && pHead->pNextPad == ALLOCA_FILLER_32
|
---|
202 | && pHead->cbPad == ALLOCA_FILLER_32
|
---|
203 | #endif
|
---|
204 | && pHead->auGuard2[0] == ALLOCA_FILLER_32
|
---|
205 | && pHead->auGuard2[1] == ALLOCA_FILLER_32
|
---|
206 | && pHead->auGuard2[2] == ALLOCA_FILLER_32
|
---|
207 | && *(uint32_t const *)((uint8_t const *)pHead + pHead->cb - sizeof(uint32_t)) == ALLOCA_FILLER_32)
|
---|
208 | { /* likely */ }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
212 | RTAssertMsg2("\n\n!!Stack corruption (alloca)!!\n\n"
|
---|
213 | "%p LB %#x\n",
|
---|
214 | pHead, pHead->cb);
|
---|
215 | #else
|
---|
216 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack corruption (alloca)!!\r\n\r\n"));
|
---|
217 | rtNoCrtFatalWritePtr(pHead);
|
---|
218 | rtNoCrtFatalWrite(RT_STR_TUPLE(" LB "));
|
---|
219 | rtNoCrtFatalWriteX64(pHead->cb);
|
---|
220 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
221 | #endif
|
---|
222 | RT_BREAKPOINT();
|
---|
223 | }
|
---|
224 | pHead = pHead->pNext;
|
---|
225 | }
|
---|
226 |
|
---|
227 | _RTC_CheckStackVars(pbFrame, pVar);
|
---|
228 | }
|
---|
229 |
|
---|