[CMAKE/MSVC]
[reactos.git] / reactos / lib / sdk / RunTmChk / rtcapi.c
1 /*
2 * PROJECT: MSVC runtime check support library
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * PURPOSE: Provides support functions for MSVC runtime checks
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
6 */
7
8 #include <rtcapi.h>
9
10 unsigned long
11 __cdecl
12 DbgPrint(
13 const char *fmt, ...);
14
15 void
16 _RTC_InitBase(void)
17 {
18 __debugbreak();
19 }
20
21 void
22 _RTC_Shutdown(void)
23 {
24 __debugbreak();
25 }
26
27 void
28 __cdecl
29 _RTC_Failure(
30 void* retaddr,
31 int errnum)
32 {
33 DbgPrint("Invalid stack pointer value caught at %p, error %d\n", retaddr, errnum);
34 __debugbreak();
35 }
36
37 void
38 __cdecl
39 _RTC_UninitUse(
40 const char *_Varname)
41 {
42 DbgPrint("Use of uninitialized variable %s!\n", _Varname);
43 __debugbreak();
44 }
45
46 void
47 __fastcall
48 _RTC_CheckStackVars(
49 void *_Esp,
50 _RTC_framedesc *_Fd)
51 {
52 int i, *guard1, *guard2;
53
54 /* Loop all variables in the descriptor */
55 for (i = 0; i < _Fd->varCount; i++)
56 {
57 /* Get the 2 guards below and above the variable */
58 guard1 = (int*)((char*)_Esp + _Fd->variables[i].addr - sizeof(*guard1));
59 guard2 = (int*)((char*)_Esp + _Fd->variables[i].addr +_Fd->variables[i].size);
60
61 /* Check if they contain the guard bytes */
62 if ((*guard1 != 0xCCCCCCCC) || (*guard2 != 0xCCCCCCCC))
63 {
64 DbgPrint("Stack corruption near '%s'\n", _Fd->variables[i].name);
65 __debugbreak();
66 }
67 }
68 }
69
70 void
71 __fastcall
72 _RTC_CheckStackVars2(
73 void *_Esp,
74 _RTC_framedesc *_Fd,
75 _RTC_ALLOCA_NODE *_AllocaList)
76 {
77 _RTC_ALLOCA_NODE *current;
78 int *guard;
79
80 /* Process normal variables */
81 _RTC_CheckStackVars(_Esp, _Fd);
82
83 /* Process the alloca list */
84 for (current = _AllocaList; current != 0; current = current->next)
85 {
86 /* Get the upper guard */
87 guard = (int*)((char*)current + current->allocaSize - sizeof(*guard));
88
89 /* Check if all guard locations are still ok */
90 if ((current->guard1 != 0xCCCCCCCC) ||
91 (current->guard2[0] != 0xCCCCCCCC) ||
92 (current->guard2[1] != 0xCCCCCCCC) ||
93 (current->guard2[2] != 0xCCCCCCCC) ||
94 (*guard != 0xCCCCCCCC))
95 {
96 __debugbreak();
97 }
98 }
99 }
100
101 void
102 __fastcall
103 _RTC_AllocaHelper(
104 _RTC_ALLOCA_NODE *_PAllocaBase,
105 size_t _CbSize,
106 _RTC_ALLOCA_NODE **_PAllocaInfoList)
107 {
108 unsigned long i;
109
110 /* Check if we got any allocation */
111 if ((_PAllocaBase != 0) &&
112 (_CbSize != 0) &&
113 (_PAllocaInfoList != 0))
114 {
115 /* Mark the whole range */
116 char *guard = (char*)_PAllocaBase;
117 for (i = 0; i < _CbSize; i++)
118 {
119 guard[i] = 0xCC;
120 }
121
122 /* Initialize the alloca base frame */
123 _PAllocaBase->allocaSize = _CbSize;
124
125 /* Insert this frame into the alloca list */
126 _PAllocaBase->next = *_PAllocaInfoList;
127 *_PAllocaInfoList = _PAllocaBase;
128 }
129 }
130