home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 4 / 4017 / crashme.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  10.9 KB  |  347 lines

  1. /* crashme: Create a string of random bytes and then jump to it.
  2.             crashme <nbytes> <srand> <ntrys> [nsub] */
  3.  
  4. char *crashme_version = "1.6 5-SEP-1991";
  5.  
  6. /*
  7.  *             COPYRIGHT (c) 1990, 1991 BY       *
  8.  *  GEORGE J. CARRETTE, CONCORD, MASSACHUSETTS.  *
  9.  *             ALL RIGHTS RESERVED               *
  10.  
  11. Permission to use, copy, modify, distribute and sell this software
  12. and its documentation for any purpose and without fee is hereby
  13. granted, provided that the above copyright notice appear in all copies
  14. and that both that copyright notice and this permission notice appear
  15. in supporting documentation, and that the name of the author
  16. not be used in advertising or publicity pertaining to distribution
  17. of the software without specific, written prior permission.
  18.  
  19. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  21. HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  23. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  25. SOFTWARE.
  26.  
  27. A signal handler is set up so that in most cases the machine exception
  28. generated by the illegal instructions, bad operands, etc in the procedure
  29. made up of random data are caught; and another round of randomness may
  30. be tried. Eventually a random instruction may corrupt the program or
  31. the machine state in such a way that the program must halt. This is
  32. a test of the robustness of the hardware/software for instruction
  33. fault handling.
  34.  
  35. Note: Running this program just a few times, using total CPU time of
  36. less than a few seconds SHOULD NOT GIVE YOU ANY CONFIDENCE in system
  37. robustness. Having it run for hours, with tens of thousands of cases
  38. would be a different thing.
  39.  
  40. Comments may be addressed to the author at GJC@MITECH.COM
  41.  
  42. See the documentation in crashme.1 or read this code for a description
  43. of command line arguments to this program.
  44.  
  45. Version Date         Description
  46. ----------------------------------------------------------------------
  47.  1.0    early 1990   initial hack.
  48.  1.1    19-SEP-1990  added more signals and an alarm to abort looping.
  49.  1.2    25-JUN-1991  added [nsub] to vfork multiple subprocesses of self.
  50.  1.3    14-AUG-1991  +nbytes malloc option, and -nsub option.
  51.  1.4    29-AUG-1991  fix +nbytes (subproc). Add time-driven nprocs. SIGINT.
  52.  1.5     3-SEP-1991  added alarm subprocess monitor to vfork_main.
  53.  1.6     5-SEP-1991  some systems don't have vfork, just fork.
  54.  
  55. Suggested test: At least let the thing run the length of your lunch break,
  56. in this case 1 hour, 10 minutes, and 30 seconds.
  57.  
  58.   crashme +2000 666 100 1:10:30
  59.  
  60.  
  61.                         CRASH REPORTS
  62.  
  63. Date,               Machine              Crashme        Reported 
  64. Crashme Ver   Make    Model OS Version   Arguments      by:        
  65. ------------------------------------------------------------------------------
  66. 10-JUL-90 1.0 SUN     4/110 4.1          1000 20 200    GJC@paradigm.com
  67. 10-JUL-90 1.0 SUN     4/280 4.0.3        1000 20 200    GJC@paradigm.com
  68. 31-JUL-90 1.0 DIGITAL DECstation 3100    100 10 10000   GAVRON@ARIZONA.EDU
  69. 31-JUL-90 1.0 IBM     RT                 100 10 10000   GAVRON@ARIZONA.EDU
  70.  1-AUG-90 1.0 DIGITAL DECstation 5000    10000 230 1000 hudgens@scri.fsu.edu
  71.  3-AUG-90 1.0 Alliant FX/2800                           SJA@SIRIUS.HUT.FI
  72. 27-JUN-91 1.2 SUN     4/110 4.1.1        10 1000 10     LPH@PARADIGM.COM   
  73. 27-JUN-91 1.2 SUN     4/110 4.1.1        1000 20 200 10 LPH@PARADIGM.COM   
  74. 29-JUN-91 1.2 SUN     4/40C 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
  75. 29-JUN-91 1.2 SUN     4/60 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
  76. 29-JUN-91 1.2 SUN     4/100 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
  77. 29-JUN-91 1.2 SUN     4/65 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
  78. 18-JUL-91 1.2 SGI     Iris4d Unix 3.3.2  1000 $$ 1000 4 tsacas@ilog.ilog.fr
  79. 29-JUL-91 1.1 IBM     RS/6000 AIX 1.3    script         brandis@inf.ethz.ch
  80.  5-SEP-91 1.6 IBM     RS/6000-320 AIX 3.1.5 +2000 666 50 40:00:00
  81.  
  82. Notes: Crashme V1.0 {1000 20 200} used to down the SUN 4/110. V1.2 does *not*
  83. crash SUNOS 4.1.1 on the same arguments. Although using the extra argument
  84. for subprocesses it will crash, with the console reporting:
  85. "Bad Trap, Bad Kernel Read Fault, Bus error. Reboot" 
  86.  
  87. Script means invoking file with many calls to crashme such as this:
  88. #/bin/csh
  89. crashme 1020 234 500 &
  90. crashme 394 38484 5723 &
  91. crashme 3784 474 474 &
  92. crashme 437 4747 38 &
  93. crashme 47848 4745 123 &
  94. crashme 4747 4747 3463 &
  95. crashme 474 46464 262 &
  96. crashme 37 3644 3723 &
  97. crashme 374 46464 22 &
  98. crashme 3747 464 363 &
  99. crashme 347 4747 44 &
  100. crashme 37374 374 66 &
  101. crashme 3737 474 4444 &
  102.  
  103. The 4-argument case of crashme could likely do as well as executing
  104. a script.
  105.  
  106. */
  107.  
  108.  
  109. #include <stdio.h>
  110. #include <signal.h>
  111. #include <setjmp.h>
  112. #include <time.h>
  113.  
  114. long nbytes,nseed,ntrys;
  115. long malloc_flag = 0;
  116. unsigned char *the_data;
  117. char *note_buffer;
  118. char *notes;
  119.  
  120. note()
  121. {strcat(note_buffer,"\n");
  122.  fputs(note_buffer,stdout);}
  123.  
  124. jmp_buf again_buff;
  125.  
  126. void (*badboy)();
  127.  
  128. void again_handler(sig, code, scp, addr)
  129.      int sig, code;
  130.      struct sigcontext *scp;
  131.      char *addr;
  132. {char *ss;
  133.  switch(sig)
  134.    {case SIGILL: ss =   " illegal instruction"; break;
  135.     case SIGTRAP: ss =   " trace trap"; break;
  136.     case SIGFPE: ss =   " arithmetic exception"; break;
  137.     case SIGBUS: ss =  " bus error"; break;
  138.     case SIGSEGV: ss =  " segmentation violation"; break;
  139.     case SIGIOT: ss = " IOT instruction"; break;
  140.     case SIGEMT: ss = " EMT instruction"; break;
  141.     case SIGALRM: ss = " alarm clock"; break;
  142.     case SIGINT:  ss = " interrupt"; break;
  143.     default: ss = "";}
  144.  sprintf(notes,"Got signal %d%s",sig,ss);
  145.  note();
  146.  longjmp(again_buff,3);}
  147.  
  148. set_up_signals()
  149. {signal(SIGILL,again_handler);
  150.  signal(SIGTRAP,again_handler);
  151.  signal(SIGFPE,again_handler);
  152.  signal(SIGBUS,again_handler);
  153.  signal(SIGSEGV,again_handler);
  154.  signal(SIGIOT,again_handler);
  155.  signal(SIGEMT,again_handler);
  156.  signal(SIGALRM,again_handler);
  157.  signal(SIGINT,again_handler);}
  158.  
  159. compute_badboy()
  160. {long j,n;
  161.  n = (nbytes < 0) ? - nbytes : nbytes;
  162.  if (malloc_flag == 1)
  163.    {the_data = (unsigned char *) malloc(n);
  164.     badboy = (void (*)()) the_data;}
  165.  for(j=0;j<n;++j) the_data[j] = (rand() >> 7) & 0xFF;
  166.  if (nbytes < 0)
  167.    {sprintf(notes,"Dump of %ld bytes of data",n);
  168.     note();
  169.     for(j=0;j<n;++j)
  170.       {fprintf(stdout,"%3d",the_data[j]);
  171.        if ((j % 20) == 19) putc('\n',stdout); else putc(' ',stdout);}
  172.     putc('\n',stdout);}}
  173.  
  174. try_one_crash()
  175. {if (nbytes > 0)
  176.    (*badboy)();
  177.  else if (nbytes == 0)
  178.    while(1);}
  179.  
  180. char *subprocess_ind = "subprocess";
  181.  
  182. main(argc,argv)
  183.      int argc; char **argv;
  184. {long nsubs,hrs,mns,scs,tflag;
  185.  note_buffer = (char *) malloc(512);
  186.  notes = note_buffer;
  187.  if ((argc == 6) &&
  188.      (strcmp(argv[5],subprocess_ind) == 0))
  189.    {sprintf(note_buffer,"Subprocess %s: ",argv[4]);
  190.     notes = note_buffer + strlen(note_buffer);
  191.     sprintf(notes,"starting");
  192.     note();
  193.     old_main(4,argv);}
  194.  else if (argc == 4)
  195.    old_main(4,argv);
  196.  else if (argc == 5)
  197.    {if (strchr(argv[4],':'))
  198.       {sscanf(argv[4],"%d:%d:%d",&hrs,&mns,&scs);
  199.        tflag = 1;
  200.        nsubs = (((hrs * 60) + mns) * 60) + scs;
  201.        sprintf(notes,"Subprocess run for %d seconds (%d %02d:%02d:%02d)",
  202.            nsubs, hrs / 24, hrs % 24,mns,scs);}
  203.     else
  204.       {tflag = 0;
  205.        nsubs = atol(argv[4]);
  206.        sprintf(notes,"Creating %d crashme subprocesses",nsubs);}
  207.     note();
  208.     vfork_main(tflag,nsubs,argv[0],argv[1],atol(argv[2]),argv[3]);}
  209.  else
  210.    {sprintf(notes,"crashme <nbytes> <srand> <ntrys> [nsub]");
  211.     note();}}
  212.  
  213. old_main(argc,argv)
  214.      int argc;
  215.      char **argv;
  216. {sprintf(notes,"Crashme: (c) Copyright 1990, 1991 George J. Carrette");
  217.  note();
  218.  sprintf(notes,"Version: %s",crashme_version);
  219.  note();
  220.  nbytes = atol(argv[1]);
  221.  if (argv[1][0] == '+') malloc_flag = 1;
  222.  nseed = atol(argv[2]);
  223.  ntrys = atol(argv[3]);
  224.  sprintf(notes,"crashme %ld %ld %ld",nbytes,nseed,ntrys);
  225.  note();
  226.  if (malloc_flag == 0)
  227.    {the_data = (unsigned char *) malloc((nbytes < 0) ? -nbytes : nbytes);
  228.     badboy = (void (*)()) the_data;
  229.     sprintf(notes,"Badboy at %d. 0x%X",badboy,badboy);
  230.     note();}
  231.  srand(nseed);
  232.  badboy_loop();}
  233.  
  234. badboy_loop()
  235. {int i;
  236.  for(i=0;i<ntrys;++i)
  237.    {compute_badboy();
  238.     if (malloc_flag == 1)
  239.       sprintf(notes,"try %ld, Badboy at %d. 0x%X",i,badboy,badboy);
  240.     else
  241.       sprintf(notes,"try %ld",i);
  242.     note();
  243.     if (setjmp(again_buff) == 3)
  244.       {sprintf(notes,"Barfed");
  245.        note();}
  246.     else
  247.       {set_up_signals();
  248.        alarm(10);
  249.        try_one_crash();
  250.        sprintf(notes,"didn't barf!");
  251.        note();}}}
  252.  
  253. long monitor_pid = 0;
  254. long monitor_period = 10;
  255. long monitor_limit =  18; /* 3 minute limit on a subprocess */
  256. long monitor_count = 0;
  257. long monitor_active = 0;
  258.  
  259. void monitor_fcn(sig, code, scp, addr)
  260.      int sig, code;
  261.      struct sigcontext *scp;
  262.      char *addr;
  263. {long status;
  264.  signal(SIGALRM,monitor_fcn);
  265.  alarm(monitor_period);
  266.  if (monitor_active)
  267.    {++monitor_count;
  268.     if (monitor_count >= monitor_limit)
  269.       {sprintf(notes,"time limit reached on pid %d 0x%X. using kill.",
  270.            monitor_pid,monitor_pid);
  271.        note();
  272.        status = kill(monitor_pid,SIGKILL);
  273.        if (status < 0)
  274.      {sprintf(notes,"failed to kill process");
  275.       note();}
  276.        monitor_active = 0;}}}
  277.  
  278. vfork_main(tflag,nsubs,cmd,nb,sr,nt)
  279.      long tflag,nsubs,sr;
  280.      char *cmd,*nb,*nt;
  281. {long j,status,pid,n,seq,total_time,dys,hrs,mns,scs;
  282.  char arg2[20],arg4[20];
  283.  time_t before_time,after_time;
  284.  if (tflag == 1)
  285.    {seq = 1;
  286.     n = 100000000;}
  287.  else if (nsubs < 0)
  288.    {n = -nsubs;
  289.     seq = 1;}
  290.  else
  291.    {n = nsubs;
  292.     seq = 0;}
  293.  if (seq == 1)
  294.    {signal(SIGALRM,monitor_fcn);
  295.     alarm(monitor_period);}
  296.  time(&before_time);
  297.  for(j=0;j<n;++j)
  298.    {sprintf(arg2,"%d",sr+j);
  299.     sprintf(arg4,"%d",j+1);
  300. #ifdef VMS
  301.     status = vfork();
  302. #else
  303.     status = fork();
  304. #endif
  305.     if (status == 0)
  306.       {status = execl(cmd,cmd,nb,arg2,nt,arg4,subprocess_ind,0);
  307.        if (status == -1)
  308.      {perror(cmd);
  309.       exit(1);}}
  310.     else if (status < 0)
  311.       perror(cmd);
  312.     else
  313.       {sprintf(notes,"pid = %d 0x%X (subprocess %d)",status,status,j+1);
  314.        note();
  315.        if (seq == 1)
  316.      {monitor_pid = status;
  317.       monitor_count = 0;
  318.       monitor_active = 1;
  319.       while((pid = wait(&status)) > 0)
  320.         {monitor_active = 0;
  321.          sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
  322.          note();}}
  323.        if (tflag == 1)
  324.      {time(&after_time);
  325.       total_time = after_time - before_time;
  326.       if (total_time >= nsubs)
  327.         {sprintf(notes,"Time limit reached after run %d",j+1);
  328.          note();
  329.          break;}}}}
  330.  if (seq == 0)
  331.    while((pid = wait(&status)) > 0)
  332.      {sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
  333.       note();}
  334.  time(&after_time);
  335.  total_time = after_time - before_time;
  336.  scs = total_time;
  337.  mns = scs / 60;
  338.  hrs = mns / 60;
  339.  dys = hrs / 24;
  340.  scs = scs % 60;
  341.  mns = mns % 60;
  342.  hrs = hrs % 24;
  343.  sprintf(notes,
  344.      "Test complete, total real time: %d seconds (%d %02d:%02d:%02d)",
  345.      total_time,dys,hrs,mns,scs);
  346.  note();}
  347.