1 | /* $Id: stacksup-vcc.cpp 96388 2022-08-20 23:08:15Z 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.215389.xyz. 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/nocrt.h"
|
---|
32 |
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/asm-amd64-x86.h>
|
---|
35 | #ifndef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
36 | # include <iprt/assert.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include "internal/compiler-vcc.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /** Variable descriptor. */
|
---|
46 | typedef struct RTC_VAR_DESC_T
|
---|
47 | {
|
---|
48 | int32_t offFrame;
|
---|
49 | uint32_t cbVar;
|
---|
50 | const char *pszName;
|
---|
51 | } RTC_VAR_DESC_T;
|
---|
52 |
|
---|
53 | /** Frame descriptor. */
|
---|
54 | typedef struct RTC_FRAME_DESC_T
|
---|
55 | {
|
---|
56 | uint32_t cVars;
|
---|
57 | RTC_VAR_DESC_T const *paVars;
|
---|
58 | } RTC_FRAME_DESC_T;
|
---|
59 |
|
---|
60 | #define VARIABLE_MARKER_PRE 0xcccccccc
|
---|
61 | #define VARIABLE_MARKER_POST 0xcccccccc
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Alloca allocation entry.
|
---|
66 | * @note For whatever reason the pNext and cb members are misaligned on 64-bit
|
---|
67 | * targets. 32-bit targets OTOH adds padding to keep the structure size
|
---|
68 | * and pNext + cb offsets the same.
|
---|
69 | */
|
---|
70 | #pragma pack(4)
|
---|
71 | typedef struct RTC_ALLOC_ENTRY
|
---|
72 | {
|
---|
73 | uint32_t uGuard1;
|
---|
74 | RTC_ALLOC_ENTRY *pNext;
|
---|
75 | #if ARCH_BITS == 32
|
---|
76 | uint32_t pNextPad;
|
---|
77 | #endif
|
---|
78 | size_t cb;
|
---|
79 | #if ARCH_BITS == 32
|
---|
80 | uint32_t cbPad;
|
---|
81 | #endif
|
---|
82 | uint32_t auGuard2[3];
|
---|
83 | } RTC_ALLOC_ENTRY;
|
---|
84 | #pragma pack()
|
---|
85 |
|
---|
86 | #define ALLOCA_FILLER_BYTE 0xcc
|
---|
87 | #define ALLOCA_FILLER_32 0xcccccccc
|
---|
88 |
|
---|
89 |
|
---|
90 | /*********************************************************************************************************************************
|
---|
91 | * External Symbols *
|
---|
92 | *********************************************************************************************************************************/
|
---|
93 | extern "C" void __fastcall _RTC_CheckStackVars(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar); /* nocrt-stack.asm */
|
---|
94 | extern "C" uintptr_t __security_cookie;
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Initializes the security cookie value.
|
---|
99 | *
|
---|
100 | * This must be called as the first thing by the startup code. We must also no
|
---|
101 | * do anything fancy here.
|
---|
102 | */
|
---|
103 | void rtVccInitSecurityCookie(void) RT_NOEXCEPT
|
---|
104 | {
|
---|
105 | __security_cookie = (uintptr_t)ASMReadTSC() ^ (uintptr_t)&__security_cookie;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | DECLASM(void) _RTC_StackVarCorrupted(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar)
|
---|
110 | {
|
---|
111 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
112 | RTAssertMsg2("\n\n!!Stack corruption!!\n\n"
|
---|
113 | "%p LB %#x - %s\n",
|
---|
114 | pbFrame + pVar->offFrame, pVar->cbVar, pVar->pszName);
|
---|
115 | #else
|
---|
116 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack corruption!!\r\n\r\n"));
|
---|
117 | rtNoCrtFatalWritePtr(pbFrame + pVar->offFrame);
|
---|
118 | rtNoCrtFatalWrite(RT_STR_TUPLE(" LB "));
|
---|
119 | rtNoCrtFatalWriteX32(pVar->cbVar);
|
---|
120 | rtNoCrtFatalWrite(RT_STR_TUPLE(" - "));
|
---|
121 | rtNoCrtFatalWriteStr(pVar->pszName);
|
---|
122 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
123 | #endif
|
---|
124 | RT_BREAKPOINT();
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | DECLASM(void) _RTC_SecurityCookieMismatch(uintptr_t uCookie)
|
---|
129 | {
|
---|
130 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
131 | RTAssertMsg2("\n\n!!Stack cookie corruption!!\n\n"
|
---|
132 | "expected %p, found %p\n",
|
---|
133 | __security_cookie, uCookie);
|
---|
134 | #else
|
---|
135 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack cookie corruption!!\r\n\r\n"
|
---|
136 | "expected"));
|
---|
137 | rtNoCrtFatalWritePtr((void *)__security_cookie);
|
---|
138 | rtNoCrtFatalWrite(RT_STR_TUPLE(", found "));
|
---|
139 | rtNoCrtFatalWritePtr((void *)uCookie);
|
---|
140 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
141 | #endif
|
---|
142 | RT_BREAKPOINT();
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | #ifdef RT_ARCH_X86
|
---|
147 | DECLASM(void) _RTC_CheckEspFailed(uintptr_t uEip, uintptr_t uEsp, uintptr_t uEbp)
|
---|
148 | {
|
---|
149 | # ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
150 | RTAssertMsg2("\n\n!!ESP check failed!!\n\n"
|
---|
151 | "eip=%p esp=%p ebp=%p\n",
|
---|
152 | uEip, uEsp, uEbp);
|
---|
153 | # else
|
---|
154 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!ESP check failed!!\r\n\r\n"
|
---|
155 | "eip="));
|
---|
156 | rtNoCrtFatalWritePtr((void *)uEip);
|
---|
157 | rtNoCrtFatalWrite(RT_STR_TUPLE(" esp="));
|
---|
158 | rtNoCrtFatalWritePtr((void *)uEsp);
|
---|
159 | rtNoCrtFatalWrite(RT_STR_TUPLE(" ebp="));
|
---|
160 | rtNoCrtFatalWritePtr((void *)uEbp);
|
---|
161 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
162 | # endif
|
---|
163 | RT_BREAKPOINT();
|
---|
164 | }
|
---|
165 | #endif
|
---|
166 |
|
---|
167 |
|
---|
168 | extern "C" void __cdecl _RTC_UninitUse(const char *pszVar)
|
---|
169 | {
|
---|
170 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
171 | RTAssertMsg2("\n\n!!Used uninitialized variable %s at %p!!\n\n",
|
---|
172 | pszVar ? pszVar : "", ASMReturnAddress());
|
---|
173 | #else
|
---|
174 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Used uninitialized variable "));
|
---|
175 | rtNoCrtFatalWriteStr(pszVar);
|
---|
176 | rtNoCrtFatalWrite(RT_STR_TUPLE(" at "));
|
---|
177 | rtNoCrtFatalWritePtr(ASMReturnAddress());
|
---|
178 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("!!\r\n\r\n"));
|
---|
179 | #endif
|
---|
180 | RT_BREAKPOINT();
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /** @todo reimplement in assembly (feeling too lazy right now). */
|
---|
185 | extern "C" void __fastcall _RTC_CheckStackVars2(uint8_t *pbFrame, RTC_VAR_DESC_T const *pVar, RTC_ALLOC_ENTRY *pHead)
|
---|
186 | {
|
---|
187 | while (pHead)
|
---|
188 | {
|
---|
189 | if ( pHead->uGuard1 == ALLOCA_FILLER_32
|
---|
190 | #if 1 && ARCH_BITS == 32
|
---|
191 | && pHead->pNextPad == ALLOCA_FILLER_32
|
---|
192 | && pHead->cbPad == ALLOCA_FILLER_32
|
---|
193 | #endif
|
---|
194 | && pHead->auGuard2[0] == ALLOCA_FILLER_32
|
---|
195 | && pHead->auGuard2[1] == ALLOCA_FILLER_32
|
---|
196 | && pHead->auGuard2[2] == ALLOCA_FILLER_32
|
---|
197 | && *(uint32_t const *)((uint8_t const *)pHead + pHead->cb - sizeof(uint32_t)) == ALLOCA_FILLER_32)
|
---|
198 | { /* likely */ }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | #ifdef IPRT_NOCRT_WITHOUT_FATAL_WRITE
|
---|
202 | RTAssertMsg2("\n\n!!Stack corruption (alloca)!!\n\n"
|
---|
203 | "%p LB %#x\n",
|
---|
204 | pHead, pHead->cb);
|
---|
205 | #else
|
---|
206 | rtNoCrtFatalWriteBegin(RT_STR_TUPLE("\r\n\r\n!!Stack corruption (alloca)!!\r\n\r\n"));
|
---|
207 | rtNoCrtFatalWritePtr(pHead);
|
---|
208 | rtNoCrtFatalWrite(RT_STR_TUPLE(" LB "));
|
---|
209 | rtNoCrtFatalWriteX64(pHead->cb);
|
---|
210 | rtNoCrtFatalWriteEnd(RT_STR_TUPLE("\r\n"));
|
---|
211 | #endif
|
---|
212 | RT_BREAKPOINT();
|
---|
213 | }
|
---|
214 | pHead = pHead->pNext;
|
---|
215 | }
|
---|
216 |
|
---|
217 | _RTC_CheckStackVars(pbFrame, pVar);
|
---|
218 | }
|
---|
219 |
|
---|