00001
00002
00003
00004 #include "cddefines.h"
00005 #include "parser.h"
00006
00007 #ifdef _MSC_VER
00008
00009 # pragma warning( disable : 4700 )
00010
00011 # pragma warning( disable : 4756 )
00012
00013 # pragma warning( disable : 4127 )
00014 #endif
00015
00016 #ifdef __INTEL_COMPILER
00017 # pragma warning( disable : 592 )
00018 #endif
00019
00020 #ifdef __clang__
00021 # pragma clang diagnostic ignored "-Wuninitialized"
00022 #endif
00023
00024 #ifdef __GNUC_EXCL__
00025 # pragma GCC diagnostic ignored "-Wuninitialized"
00026 #endif
00027
00028
00029 const int ARR_SIZE = 10;
00030
00031
00032 static double ar2[ARR_SIZE];
00033
00034
00035
00036 #if defined(_MSC_VER) || defined(__ICC)
00037 #pragma optimize("", off)
00038 #elif defined(__PGI)
00039 #pragma global opt=0
00040 #elif defined(__HP_aCC)
00041 #pragma OPT_LEVEL 0
00042 #endif
00043
00044
00045 void ParseCrashDo(Parser &p)
00046 {
00047 double ar1, br1;
00048 bool lgCrash = false;
00049
00050 DEBUG_ENTRY( "ParseCrashDo()" );
00051
00052
00053 if( p.nMatch("ZERO") )
00054 {
00055 fprintf(ioQQQ," I will now div by 0 to get crash. Hold on.\n");
00056 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00057 fflush(ioQQQ);
00058 ar1 = 1. / ZeroNum;
00059 fprintf(ioQQQ," I am still alive - something is wrong, result is %e\n",
00060 ar1);
00061 lgCrash = true;
00062 }
00063
00064
00065 else if( p.nMatch("UNDE") )
00066 {
00067 if( p.nMatch("STAT") )
00068 {
00069 fprintf(ioQQQ," Now I will now use an undefined static variable. Hold on.\n");
00070 fprintf(ioQQQ," This should never fail since the compiler should have automatically initialized it to zero.\n");
00071 fflush(ioQQQ);
00072
00073 ar2[0] *= 1e-10;
00074
00075
00076 fprintf(ioQQQ," I am still alive, this is the expected result. The "
00077 "result of the multiplication of undefined by 1e-10 is "
00078 "%e\n", ar2[0] );
00079 fflush(ioQQQ);
00080 }
00081 else if( p.nMatch("STAC") || p.nMatch("AUTO") )
00082 {
00083 double A_variable_which_SHOULD_be_used_uninitialized;
00084 fprintf(ioQQQ," Now I will now use an undefined variable off the stack. Hold on.\n");
00085 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00086 fflush(ioQQQ);
00087
00088 A_variable_which_SHOULD_be_used_uninitialized *= 1e-10f;
00089
00090
00091 fprintf(ioQQQ," I am still alive - something is wrong, the result of the multiplication of undefined by 1e-10 is %e\n", A_variable_which_SHOULD_be_used_uninitialized );
00092 fflush(ioQQQ);
00093 }
00094 else
00095 {
00096 double *aa = (double*)MALLOC(3*sizeof(double));
00097 fprintf(ioQQQ," I will now use an undefined variable off the heap obtained with malloc. Hold on.\n");
00098
00099 if( MyIsnan( aa[1] ) )
00100 fprintf(ioQQQ," The malloc'ed memory was set to NaN.\n" );
00101 else
00102 fprintf(ioQQQ," The malloc'ed memory was NOT initialized by MyMalloc.\n" );
00103 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00104 fflush(ioQQQ);
00105
00106 aa[1] *= 1e-10;
00107
00108 fprintf(ioQQQ," I am still alive - something is wrong, the result of the multiplication of undefined by 1e-10 is %e\n", aa[1] );
00109 fflush(ioQQQ);
00110 free( aa );
00111 }
00112 lgCrash = true;
00113 }
00114
00115
00116 else if( p.nMatch("OVER") && p.nMatch("LONG") )
00117 {
00118 long lng;
00119 fprintf(ioQQQ," I will now make long overflow to get crash. Hold on.\n");
00120 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00121 fflush(ioQQQ);
00122 lng = (long)(LONG_MAX*sqrt(1e6));
00123 fprintf(ioQQQ," I am still alive - something is wrong, the result was %li\n",
00124 lng);
00125 lgCrash = true;
00126 }
00127
00128
00129 else if( p.nMatch("OVER") )
00130 {
00131 ar1 = 1e-20;
00132 fprintf(ioQQQ," I will now make floating point overflow to get crash. Hold on.\n");
00133 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00134 fflush(ioQQQ);
00135 br1 = DBL_MAX / ar1;
00136 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00137 br1);
00138 lgCrash = true;
00139 }
00140
00141
00142 else if( p.nMatch("ASSE") )
00143 {
00144 fprintf(ioQQQ," I will now assert that a false statement is true to get a crash.\n\n");
00145 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00146 fprintf(ioQQQ," If the next line says \"I am still alive - the assert macro is not working ....\" then there are problems.\n\n");
00147 fflush(ioQQQ);
00148 ASSERT( DBL_MAX < ZeroNum );
00149 fprintf(ioQQQ," I am still alive - the assert macro is not working in this executable.\n");
00150 lgCrash = true;
00151 }
00152
00153
00154 else if( p.nMatch(" NAN") )
00155 {
00156 ar1 = 0.;
00157 fprintf(ioQQQ," I will now make invalid operation (div 0 by 0) to get crash. Hold on.\n");
00158 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00159 fflush(ioQQQ);
00160 br1 = ar1 / ZeroNum;
00161 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00162 br1);
00163 lgCrash = true;
00164 }
00165
00166
00167 else if( p.nMatch("SETN") && p.nMatch("FLOA") )
00168 {
00169 sys_float f;
00170 fprintf(ioQQQ," I will now initialize a float to a signaling NaN. This should never crash!\n");
00171 set_NaN(f);
00172 fprintf(ioQQQ," Initialization finished. I will now perform an operation on this variable. Hold on.\n");
00173 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00174 fflush(ioQQQ);
00175 f *= 2.f;
00176 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00177 f);
00178 lgCrash = true;
00179 }
00180
00181
00182 else if( p.nMatch("SETN") )
00183 {
00184 double d;
00185 fprintf(ioQQQ," I will now initialize a double to a signaling NaN. This should never crash!\n");
00186 set_NaN(d);
00187 fprintf(ioQQQ," Initialization finished. I will now perform an operation on this variable. Hold on.\n");
00188 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00189 fflush(ioQQQ);
00190 d *= 2.;
00191 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00192 d);
00193 lgCrash = true;
00194 }
00195
00196
00197
00198 else if( p.nMatch("BOUN") )
00199 {
00200 double x;
00201
00202
00203 x = p.FFmtRead();
00204 if( p.lgEOL() && p.nMatch(" LOW" ) )
00205 x = -2.;
00206 if( p.lgEOL() && p.nMatch("HIGH" ) )
00207 x = 2.;
00208
00209
00210
00211 long int i = ( x >= 0. ) ? (long)(x+0.5) + ARR_SIZE : (long)(x-0.5);
00212
00213
00214 if( p.nMatch("STAT") )
00215 {
00216 fprintf(ioQQQ," I will now access static array element ar2[%ld]. Hold on.\n", i );
00217 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00218 fflush(ioQQQ);
00219 ar2[i] = 1e-10;
00220
00221 fprintf(ioQQQ," I am still alive - something is wrong\n" );
00222 fflush(ioQQQ);
00223 }
00224 else if( p.nMatch("STAC") || p.nMatch("AUTO") )
00225 {
00226 double a[ARR_SIZE];
00227 fprintf(ioQQQ," I will now access automatic array element a[%ld]. Hold on.\n", i );
00228 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00229 fflush(ioQQQ);
00230 a[i] = 1e-10;
00231
00232 fprintf(ioQQQ," I am still alive - something is wrong, return value was %.2e\n", a[i] );
00233 fflush(ioQQQ);
00234 }
00235 else if( p.nMatch("HEAP") )
00236 {
00237 int *ibound;
00238 ibound = ((int *)MALLOC( ARR_SIZE*sizeof(int) ));
00239 fprintf(ioQQQ," I will now access malloced heap array element ibound[%ld]. Hold on.\n", i );
00240 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00241 fflush(ioQQQ);
00242 ibound[i] = 1;
00243 fprintf(ioQQQ," I am still alive - something is wrong, return value is %i\n" , ibound[i] );
00244 fflush(ioQQQ);
00245 free(ibound);
00246 }
00247 else if( p.nMatch("MULT") )
00248 {
00249
00250
00251 multi_arr<double,2> b;
00252 b.reserve(3);
00253 for( int j=0; j < 3; j++ )
00254 b.reserve(j,ARR_SIZE+j);
00255 b.alloc();
00256 if( p.nMatch("ITER") )
00257 {
00258 fprintf(ioQQQ," I will now access multi_arr array element *b.ptr(0,%ld). Hold on.\n", i );
00259 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n\n");
00260 fflush(ioQQQ);
00261 md2i p = b.ptr(0,i);
00262 *p = 2.;
00263 fprintf(ioQQQ," I am still alive - something is wrong, return value is %g\n", *p );
00264 fflush(ioQQQ);
00265 }
00266 else
00267 {
00268 fprintf(ioQQQ," I will now access multi_arr array element b[0][%ld]. Hold on.\n", i );
00269 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n\n");
00270 fflush(ioQQQ);
00271 b[0][i] = 2.;
00272 fprintf(ioQQQ," I am still alive - something is wrong, return value is %g\n" , b[0][i] );
00273 fflush(ioQQQ);
00274 }
00275 b.clear();
00276 }
00277 else
00278 {
00279 fprintf(ioQQQ," The CRASH BOUNDS command has four different tests. One must be specified\n" );
00280 fprintf(ioQQQ," The HEAP option tests a malloc/'d array - this tests valgrind or purify.\n");
00281 fprintf(ioQQQ," The STATIC option tests a static declared array, and the STACK or AUTO option tests an automatic array - these test pgcc.\n");
00282 fprintf(ioQQQ," The MULTI option tests if bounds checking is enabled in the multi_arr class (i.e., if the preprocessor macro BOUNDS_CHECK has been set).\n" );
00283 fprintf(ioQQQ," All have a number as an optional argument, the array element to be accessed.\n");
00284 fflush(ioQQQ);
00285 }
00286 lgCrash = true;
00287 }
00288
00289
00290 else if( p.nMatch("ISNA") )
00291 {
00292 if( p.nMatch("FLOA") )
00293 {
00294 sys_float ff;
00295 fprintf(ioQQQ," I will now set a float to SNaN. This should never crash!\n" );
00296 set_NaN( ff );
00297 fprintf(ioQQQ," I will now test this variable with the isnan function\n" );
00298 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00299 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00300 ASSERT( !isnan( ff ) );
00301 fprintf(ioQQQ," I am still alive - something is wrong, value is %e\n", ff );
00302 }
00303 else
00304 {
00305 double dd;
00306 fprintf(ioQQQ," I will now set a double to SNaN. This should never crash!\n" );
00307 set_NaN( dd );
00308 fprintf(ioQQQ," I will now test this variable with the isnan function\n" );
00309 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00310 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00311 ASSERT( !isnan( dd ) );
00312 fprintf(ioQQQ," I am still alive - something is wrong, value is %e\n", dd );
00313 }
00314 }
00315
00316
00317 else if( p.nMatch("EXCE") )
00318 {
00319 fprintf(ioQQQ," I will now throw a C++ exception of type out_of_range()\n" );
00320 fprintf(ioQQQ," The correct behavior is for the statement \"DISASTER - An out_of_range exception was caught, what() = Cloudy Test. Bailing out...\" to be printed.\n\n");
00321 fprintf(ioQQQ," If you get any other message, the exception was not caught correctly.\n\n");
00322 throw out_of_range( "Cloudy Test" );
00323 fprintf(ioQQQ," If you see this statement, the exception did not terminate the program.\n" );
00324 }
00325
00326 else
00327 {
00328 fprintf(ioQQQ,
00329 "Crash option not found - valid options are ZERO, UNDEfined,"
00330 " OVERflow, ASSErt, _NAN, SETNan, BOUNds, ISNAn, and EXCEption.\nSorry.\n");
00331 lgCrash = true;
00332 }
00333
00334 if( lgCrash )
00335 {
00336 cdEXIT(EXIT_FAILURE);
00337 }
00338 }