ReactOS  0.4.14-dev-52-g6116262
rtcapi.c
Go to the documentation of this file.
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 #if defined(_M_IX86)
11 #pragma comment(linker, "/alternatename:__CRT_RTC_INITW=__CRT_RTC_INITW0")
12 #elif defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM)
13 #pragma comment(linker, "/alternatename:_CRT_RTC_INITW=_CRT_RTC_INITW0")
14 #else
15 #error Unsupported platform
16 #endif
17 
18 // Provide a fallback memset for libraries like kbdrost.dll
19 #if defined(_M_ARM)
20 void* __cdecl memset_fallback(void* src, int val, size_t count)
21 {
22  char *char_src = (char *)src;
23  while(count > 0)
24  {
25  *char_src = val;
26  char_src++;
27  count--;
28  }
29  return src;
30 }
31 #pragma comment(linker, "/alternatename:memset=memset_fallback")
32 #endif
33 
34 int
35 __cdecl
37  int errType,
38  const wchar_t *file,
39  int line,
40  const wchar_t *module,
41  const wchar_t *format,
42  ...)
43 {
44  /* Simple fallback function */
45  __debugbreak();
46  return 0;
47 }
48 
50 
51 /*
52  Default CRT RTC init, if we don't link to CRT
53 */
55 __cdecl
57  void *_Res0,
58  void **_Res1,
59  int _Res2,
60  int _Res3,
61  int _Res4)
62 {
63  return &_RTC_DefaultErrorFuncW;
64 }
65 
66 void
67 __cdecl
69 {
70  static char initialized = 0;
71  _RTC_error_fnW errorFunc;
72 
73  if (!initialized)
74  {
75  errorFunc = _CRT_RTC_INITW(0, 0, 0, 1, 0);
76  _RTC_SetErrorFuncW(errorFunc);
77  initialized = 1;
78  }
79 }
80 
81 void
82 __cdecl
84 {
85  __debugbreak();
86 }
87 
88 void
89 __cdecl
91 {
92  /* Usually this function would walk an array of function pointers and call
93  each of these, like done with global ctors, but since these are currently
94  only _RTC_InitBase, we simply call that function once. */
95  _RTC_InitBase();
96 }
97 
98 void
99 __cdecl
101  void* retaddr,
102  int errnum)
103 {
104  _RTC_pErrorFuncW(errnum,
105  L"unknown file",
106  -1,
107  L"unknown module",
108  L"Invalid stack pointer value caught at %p, error %d\n",
109  retaddr,
110  errnum);
111 }
112 
113 void
114 __cdecl
116  const char *_Varname)
117 {
119  L"unknown file",
120  -1,
121  L"unknown module",
122  L"Use of uninitialized variable %S!\n",
123  _Varname);
124 }
125 
126 void
129  void *_Esp,
130  _RTC_framedesc *_Fd)
131 {
132  int i, *guard1, *guard2;
133 
134  /* Loop all variables in the descriptor */
135  for (i = 0; i < _Fd->varCount; i++)
136  {
137  /* Get the 2 guards below and above the variable */
138  guard1 = (int*)((char*)_Esp + _Fd->variables[i].addr - sizeof(*guard1));
139  guard2 = (int*)((char*)_Esp + _Fd->variables[i].addr +_Fd->variables[i].size);
140 
141  /* Check if they contain the guard bytes */
142  if ((*guard1 != 0xCCCCCCCC) || (*guard2 != 0xCCCCCCCC))
143  {
145  L"unknown file",
146  -1,
147  L"unknown module",
148  L"Stack corruption near '%s'\n",
149  _Fd->variables[i].name);
150  }
151  }
152 }
153 
154 void
157  void *_Esp,
158  _RTC_framedesc *_Fd,
159  _RTC_ALLOCA_NODE *_AllocaList)
160 {
162  int *guard;
163 
164  /* Process normal variables */
165  _RTC_CheckStackVars(_Esp, _Fd);
166 
167  /* Process the alloca list */
168  for (current = _AllocaList; current != 0; current = current->next)
169  {
170  /* Get the upper guard */
171  guard = (int*)((char*)current + current->allocaSize - sizeof(*guard));
172 
173  /* Check if all guard locations are still ok */
174  if ((current->guard1 != 0xCCCCCCCC) ||
175  (current->guard2[0] != 0xCCCCCCCC) ||
176  (current->guard2[1] != 0xCCCCCCCC) ||
177  (current->guard2[2] != 0xCCCCCCCC) ||
178  (*guard != 0xCCCCCCCC))
179  {
181  L"unknown file",
182  -1,
183  L"unknown module",
184  L"Stack corruption in alloca frame\n");
185  }
186  }
187 }
188 
189 void
192  _RTC_ALLOCA_NODE *_PAllocaBase,
193  size_t _CbSize,
194  _RTC_ALLOCA_NODE **_PAllocaInfoList)
195 {
196  unsigned long i;
197 
198  /* Check if we got any allocation */
199  if ((_PAllocaBase != 0) &&
200  (_CbSize != 0) &&
201  (_PAllocaInfoList != 0))
202  {
203  /* Mark the whole range */
204  char *guard = (char*)_PAllocaBase;
205  for (i = 0; i < _CbSize; i++)
206  {
207  guard[i] = 0xCC;
208  }
209 
210  /* Initialize the alloca base frame */
211  _PAllocaBase->allocaSize = _CbSize;
212 
213  /* Insert this frame into the alloca list */
214  _PAllocaBase->next = *_PAllocaInfoList;
215  *_PAllocaInfoList = _PAllocaBase;
216  }
217 }
218 
#define __cdecl
Definition: accygwin.h:79
#define __fastcall
Definition: sync.c:41
int size
Definition: rtcapi.h:41
GLuint GLuint GLsizei count
Definition: gl.h:1545
void __cdecl _RTC_InitBase(void)
Definition: rtcapi.c:68
void __cdecl _RTC_Shutdown(void)
Definition: rtcapi.c:83
_RTC_error_fnW _RTC_pErrorFuncW
Definition: rtcapi.c:49
int varCount
Definition: rtcapi.h:46
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
size_t allocaSize
Definition: rtcapi.h:58
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_RTC_error_fnW __cdecl _RTC_SetErrorFuncW(_RTC_error_fnW)
Definition: rtcuserapi.c:78
void __cdecl _RTC_Initialize(void)
Definition: rtcapi.c:90
void __cdecl _RTC_Failure(void *retaddr, int errnum)
Definition: rtcapi.c:100
Definition: parser.c:48
void __fastcall _RTC_CheckStackVars(void *_Esp, _RTC_framedesc *_Fd)
Definition: rtcapi.c:128
GLuint GLfloat * val
Definition: glext.h:7180
static BOOL initialized
Definition: syslog.c:39
int addr
Definition: rtcapi.h:40
char * name
Definition: rtcapi.h:42
static const WCHAR L[]
Definition: oid.c:1250
GLenum src
Definition: glext.h:6340
_RTC_error_fnW __cdecl _CRT_RTC_INITW0(void *_Res0, void **_Res1, int _Res2, int _Res3, int _Res4)
Definition: rtcapi.c:56
_RTC_vardesc * variables
Definition: rtcapi.h:47
int(__cdecl * _RTC_error_fnW)(int, const wchar_t *, int, const wchar_t *, const wchar_t *,...)
Definition: rtcapi.h:27
struct _RTC_ALLOCA_NODE * next
Definition: rtcapi.h:54
void __fastcall _RTC_CheckStackVars2(void *_Esp, _RTC_framedesc *_Fd, _RTC_ALLOCA_NODE *_AllocaList)
Definition: rtcapi.c:156
void __cdecl _RTC_UninitUse(const char *_Varname)
Definition: rtcapi.c:115
void __fastcall _RTC_AllocaHelper(_RTC_ALLOCA_NODE *_PAllocaBase, size_t _CbSize, _RTC_ALLOCA_NODE **_PAllocaInfoList)
Definition: rtcapi.c:191
int __cdecl _RTC_DefaultErrorFuncW(int errType, const wchar_t *file, int line, const wchar_t *module, const wchar_t *format,...)
Definition: rtcapi.c:36
struct task_struct * current
Definition: linux.c:32
_RTC_error_fnW __cdecl _CRT_RTC_INITW(void *_Res0, void **_Res1, int _Res2, int _Res3, int _Res4)
Definition: mscmain.c:65
Definition: fci.c:126