QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_crashtest.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2007 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(ON_CRASH_TEST_STATICS)
17
18#error See instructions in the following comment.
19
20/*
21
22// This code defines a global function call MyGlobalCrashTestFunction
23// that you can call from a release build to test your program's
24// behavior when it crashes.
25#define ON_CRASH_TEST_STATICS
26#include "opennurbs_crashtest.h"
27#undef ON_CRASH_TEST_STATICS
28int MyGlobalCrashTestFunction( int crash_type, ON_TextLog& textlog )
29{
30 //crash_type
31 // 0 = NullPtrDeRef
32 // 1 = BogusPtrDeRef
33 // 2 = CallNullFuncPtr
34 // 3 = CallBogusFuncPtr
35 // 4 = Call CRT _abort
36 // 5 = Call CRT _exit99
37 // 6 = DivideByDoubleZero
38 // 7 = DivideByFloatZero
39 // 8 = DivideByIntZero
40 // 9 = log(negative number)
41 // 10 = double overflow exception
42 // 11 = calling throw(123)
43 return CrashTestHelper(crash_type,textlog); // static function defined in opennurbs_crashtest.h
44}
45
46*/
47
48#endif
49
51//
52// For testing crash handling.
53//
54
56
57static
59{
60 *pp = 0;
61}
62
63static
65{
66 INT_PTR i = 0xCACAF00D;
67 *pp = (int*)i;
68}
69
70static
75
76static
82
83static
84int CrashTestHelper_DerefNullIntPtr( ON_TextLog& textlog, int crash_type, int* stack_ptr )
85{
86 int* ptr;
87 CrashTestHelper_GetNullIntPrt(&ptr); // sets ptr = NULL
88 textlog.Print("NULL ptr = %08x\n",ptr);
89 *stack_ptr = *ptr; // dereferences NULL pointer and crashes
90 textlog.Print("*ptr = %d\n",*ptr);
91 return crash_type;
92}
93
94static
95int CrashTestHelper_DerefBogusIntPtr( ON_TextLog& textlog, int crash_type, int* stack_ptr )
96{
97 int* ptr;
98 CrashTestHelper_GetBogusIntPtr(&ptr); // sets ptr = 0xCACAF00D
99 textlog.Print("Bogus ptr = %08x\n",ptr);
100 *stack_ptr = *ptr; // dereferences bogus pointer and crashes
101 textlog.Print("*ptr = %d\n",*ptr);
102 return crash_type;
103}
104
105static
106int CrashTestHelper_CallNullFuncPtr( ON_TextLog& textlog, int crash_type, int* stack_ptr )
107{
109 CrashTestHelper_GetNullFuncPtr(&fptr); // sets ptr = NULL
110 textlog.Print("NULL function f = %08x\n",fptr);
111 *stack_ptr = fptr(crash_type); // dereferences NULL function pointer and crashes
112 textlog.Print("f(%d) = %d\n",crash_type,*stack_ptr);
113 return crash_type;
114}
115
116static
117int CrashTestHelper_CallBoguslFuncPtr( ON_TextLog& textlog, int crash_type, int* stack_ptr )
118{
120 CrashTestHelper_GetBogusFuncPtr(&fptr); // sets ptr = NULL
121 textlog.Print("Bogus function f = %08x\n",fptr);
122 *stack_ptr = fptr(crash_type); // dereferences bogus function pointer and crashes
123 textlog.Print("f(%d) = %d\n",crash_type,*stack_ptr);
124 return crash_type;
125}
126
127static
128bool CrashTestHelper_DivideByFloatZero( ON_TextLog& textlog, const char* zero )
129{
130 double z;
131 sscanf( zero, "%lg", &z );
132 float fz = (float)z;
133 textlog.Print("float fz = %f\n",fz);
134 float fy = 13.0f/fz;
135 textlog.Print("double 13.0f/fz = %f\n",fy);
136 return (0.123f != fy);
137}
138
139static
140bool CrashTestHelper_DivideByDoubleZero( ON_TextLog& textlog, const char* zero )
141{
142 double z;
143 sscanf( zero, "%lg", &z );
144 textlog.Print("double z = %g\n",z);
145 double y = 13.0/z;
146 textlog.Print("double 13.0/z = %g\n",y);
147 return ON_IsValid(y);
148}
149
150static
151bool CrashTestHelper_DivideByIntZero( ON_TextLog& textlog, const char* zero )
152{
153 int iz = 0, iy = 0;
154 double z;
155 sscanf( zero, "%lg", &z );
156 iz = (int)z;
157 textlog.Print("int iz = %d\n",iz);
158 iy = 17/iz;
159 textlog.Print("17/iz = %d\n",iy);
160 return (123456 != iy);
161}
162
163static
164bool CrashTestHelper_LogNegativeNumber( ON_TextLog& textlog, const char* minus_one )
165{
166 double z;
167 sscanf( minus_one, "%lg", &z );
168 textlog.Print("z = %g\n",z);
169 double y = log(z);
170 textlog.Print("log(z) = %g\n",y);
171 return ON_IsValid(y);
172}
173
174static
175bool CrashTestHelper_DoubleOverflow( ON_TextLog& textlog, const char* sx, const char* sy )
176{
177 double x,y,z;
178 sscanf( sx, "%lg", &x );
179 sscanf( sy, "%lg", &y );
180 textlog.Print("x = %g y = %g\n",x,y);
181 z = x*y;
182 textlog.Print("x*y = z = %g\n",z);
183 return ON_IsValid(z);
184}
185
186static
187bool CrashTestHelper_Throw( ON_TextLog& textlog, const char* si )
188{
189 int i=0;
190 sscanf( si, "%i", &i );
191 textlog.Print("i = %d\n",i);
192 if ( 0==strcmp("123",si) )
193 throw(i);
194 return (123==i);
195}
196
197/*
198Description:
199 Create a condition that should result in a crash.
200 The intended use is for testing application crash handling.
201Parameters:
202 crash_type - [in]
203 0: dereference NULL data pointer
204 1: dereference bogus data pointer (0xCACAF00D)
205 2: dereference NULL function pointer
206 3: dereference bogus function pointer (0xCACAF00D)
207 4: call abort()
208 5: call exit(99);
209 6: double divide by 0.0 - FPU exception
210 well designed apps will mask the exception but detect the error
211 7: float divide by 0.0 - FPU exception
212 well designed apps will mask the exception but detect the error
213 8: int divide by 0 - will crash
214 9: log(-1.0) - should call math error handler
215 10: double overflow - FPU exception
216 well designed apps will mask the exception but detect the error
217 11: throw(123) - well designed apps with catch the exception
218 before crashing
219Returns:
220 Value of crash_type parameter.
221*/
222#if defined(ON_COMPILER_MSC)
223// Disable the MSC /W4 unreachable code warning around the exit(99) call
224#pragma warning( push )
225#pragma warning( disable : 4702 )
226#endif
227
228static
229int CrashTestHelper( int crash_type, ON_TextLog& textlog )
230{
231 // Note: The code and calls are intentionally obtuse
232 // so that it is difficult for an optimizer to
233 // not perform the calculation.
234 int stack_int = 0;
235 int rc = crash_type;
236
237 switch( crash_type )
238 {
239 case 0: // dereference NULL pointer
240 rc = CrashTestHelper_DerefNullIntPtr( textlog, crash_type, &stack_int );
241 break;
242
243 case 1: // dereference bogus pointer
244 rc = CrashTestHelper_DerefBogusIntPtr( textlog, crash_type, &stack_int );
245 break;
246
247 case 2: // call NULL function pointer
248 rc = CrashTestHelper_CallNullFuncPtr( textlog, crash_type, &stack_int );
249 break;
250
251 case 3: // divide by zero
252 rc = CrashTestHelper_CallBoguslFuncPtr( textlog, crash_type, &stack_int );
253 break;
254
255 case 4:
256 abort();
257 textlog.Print("abort() didn't crash.\n");
258 break;
259
260 case 5:
261 exit(99);
262 textlog.Print("exit(99) didn't crash.\n");
263 break;
264
265 case 6:
266 if ( CrashTestHelper_DivideByDoubleZero( textlog, "0.0" ) )
267 {
268 textlog.Print("Divide by double 0.0 didn't crash - exception must have been handled or ignored.\n");
269 }
270 break;
271
272 case 7:
273 if ( CrashTestHelper_DivideByFloatZero( textlog, "0.0" ) )
274 {
275 textlog.Print("Divide by float 0.0f didn't crash - exception must have been handled or ignored.\n");
276 }
277 break;
278
279 case 8:
280 if ( CrashTestHelper_DivideByIntZero( textlog, "0.0" ) )
281 {
282 textlog.Print("Divide by int 0 didn't crash - exception must have been handled or ignored.\n");
283 }
284 break;
285
286 case 9:
287 if ( CrashTestHelper_LogNegativeNumber( textlog, "-1.0" ) )
288 {
289 textlog.Print("log(negative number) didn't crash - exception must have been handled or ignored.\n");
290 }
291 break;
292
293 case 10:
294 if ( CrashTestHelper_DoubleOverflow( textlog, "1.0e200", "2.0e222" ) )
295 {
296 textlog.Print("1.0e200*2.0e222 didn't crash - exception must have been handled or ignored.\n");
297 }
298 break;
299
300 case 11:
301 if ( CrashTestHelper_Throw( textlog, "123" ) )
302 {
303 textlog.Print("throw(123) crash - exception must have been handled or ignored.\n");
304 }
305 break;
306
307 default:
308 break;
309 }
310
311 return rc;
312}
313
314#if defined(ON_COMPILER_MSC)
315#pragma warning( pop )
316#endif
int i
Copyright (c) 2011-2018 by Andrew Mustun.
Definition autostart.js:32
Definition opennurbs_textlog.h:20
void Print(const char *format,...)
Definition opennurbs_textlog.cpp:105
static int CrashTestHelper_CallBoguslFuncPtr(ON_TextLog &textlog, int crash_type, int *stack_ptr)
Definition opennurbs_crashtest.h:117
static bool CrashTestHelper_LogNegativeNumber(ON_TextLog &textlog, const char *minus_one)
Definition opennurbs_crashtest.h:164
static bool CrashTestHelper_DoubleOverflow(ON_TextLog &textlog, const char *sx, const char *sy)
Definition opennurbs_crashtest.h:175
static bool CrashTestHelper_DivideByDoubleZero(ON_TextLog &textlog, const char *zero)
Definition opennurbs_crashtest.h:140
static void CrashTestHelper_GetNullIntPrt(int **pp)
Definition opennurbs_crashtest.h:58
static bool CrashTestHelper_DivideByFloatZero(ON_TextLog &textlog, const char *zero)
Definition opennurbs_crashtest.h:128
static void CrashTestHelper_GetBogusIntPtr(int **pp)
Definition opennurbs_crashtest.h:64
static int CrashTestHelper(int crash_type, ON_TextLog &textlog)
Definition opennurbs_crashtest.h:229
static void CrashTestHelper_GetNullFuncPtr(CRASHTEST__FUNCTION__POINTER__ *pp)
Definition opennurbs_crashtest.h:71
static bool CrashTestHelper_DivideByIntZero(ON_TextLog &textlog, const char *zero)
Definition opennurbs_crashtest.h:151
static int CrashTestHelper_DerefNullIntPtr(ON_TextLog &textlog, int crash_type, int *stack_ptr)
Definition opennurbs_crashtest.h:84
static int CrashTestHelper_CallNullFuncPtr(ON_TextLog &textlog, int crash_type, int *stack_ptr)
Definition opennurbs_crashtest.h:106
static void CrashTestHelper_GetBogusFuncPtr(CRASHTEST__FUNCTION__POINTER__ *pp)
Definition opennurbs_crashtest.h:77
int(* CRASHTEST__FUNCTION__POINTER__)(int)
Definition opennurbs_crashtest.h:55
static int CrashTestHelper_DerefBogusIntPtr(ON_TextLog &textlog, int crash_type, int *stack_ptr)
Definition opennurbs_crashtest.h:95
static bool CrashTestHelper_Throw(ON_TextLog &textlog, const char *si)
Definition opennurbs_crashtest.h:187
ON_DECL bool ON_IsValid(double x)
Definition opennurbs_point.cpp:18