home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3941 / crashme.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-30  |  9.8 KB  |  309 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.4 29-AUG-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.  
  53.  
  54. Suggested test: At least let the thing run the length of your lunch break,
  55. in this case 1 hour, 10 minutes, and 30 seconds.
  56.  
  57.   crashme +2000 666 100 1:10:30
  58.  
  59.  
  60.                         CRASH REPORTS
  61.  
  62. Date,               Machine              Crashme        Reported 
  63. Crashme Ver   Make    Model OS Version   Arguments      by:        
  64. ------------------------------------------------------------------------------
  65. 10-JUL-90 1.0 SUN     4/110 4.1          1000 20 200    GJC@paradigm.com
  66. 10-JUL-90 1.0 SUN     4/280 4.0.3        1000 20 200    GJC@paradigm.com
  67. 31-JUL-90 1.0 DIGITAL DECstation 3100    100 10 10000   GAVRON@ARIZONA.EDU
  68. 31-JUL-90 1.0 IBM     RT                 100 10 10000   GAVRON@ARIZONA.EDU
  69.  1-AUG-90 1.0 DIGITAL DECstation 5000    10000 230 1000 hudgens@scri.fsu.edu
  70.  3-AUG-90 1.0 Alliant FX/2800                           SJA@SIRIUS.HUT.FI
  71. 27-JUN-91 1.2 SUN     4/110 4.1.1        10 1000 10     LPH@PARADIGM.COM   
  72. 27-JUN-91 1.2 SUN     4/110 4.1.1        1000 20 200 10 LPH@PARADIGM.COM   
  73. 29-JUN-91 1.2 SUN     4/40C 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
  74. 29-JUN-91 1.2 SUN     4/60 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
  75. 29-JUN-91 1.2 SUN     4/100 4.1.1        9 29748 5877 4 jon@uk.ac.oxford.robots
  76. 29-JUN-91 1.2 SUN     4/65 4.1.1         9 29748 5877 4 jon@uk.ac.oxford.robots
  77. 18-JUL-91 1.2 SGI     Iris4d Unix 3.3.2  1000 $$ 1000 4 tsacas@ilog.ilog.fr
  78. 29-JUL-91 1.1 IBM     RS/6000 AIX 1.3    script         brandis@inf.ethz.ch
  79.  
  80. Notes: Crashme V1.0 {1000 20 200} used to down the SUN 4/110. V1.2 does *not*
  81. crash SUNOS 4.1.1 on the same arguments. Although using the extra argument
  82. for subprocesses it will crash, with the console reporting:
  83. "Bad Trap, Bad Kernel Read Fault, Bus error. Reboot" 
  84.  
  85. Script means invoking file with many calls to crashme such as this:
  86. #/bin/csh
  87. crashme 1020 234 500 &
  88. crashme 394 38484 5723 &
  89. crashme 3784 474 474 &
  90. crashme 437 4747 38 &
  91. crashme 47848 4745 123 &
  92. crashme 4747 4747 3463 &
  93. crashme 474 46464 262 &
  94. crashme 37 3644 3723 &
  95. crashme 374 46464 22 &
  96. crashme 3747 464 363 &
  97. crashme 347 4747 44 &
  98. crashme 37374 374 66 &
  99. crashme 3737 474 4444 &
  100.  
  101. The 4-argument case of crashme could likely do as well as executing
  102. a script.
  103.  
  104. */
  105.  
  106.  
  107. #include <stdio.h>
  108. #include <signal.h>
  109. #include <setjmp.h>
  110. #include <time.h>
  111.  
  112. long nbytes,nseed,ntrys;
  113. long malloc_flag = 0;
  114. unsigned char *the_data;
  115. char *note_buffer;
  116. char *notes;
  117.  
  118. note()
  119. {strcat(note_buffer,"\n");
  120.  fputs(note_buffer,stdout);}
  121.  
  122. jmp_buf again_buff;
  123.  
  124. void (*badboy)();
  125.  
  126. void again_handler(sig, code, scp, addr)
  127.      int sig, code;
  128.      struct sigcontext *scp;
  129.      char *addr;
  130. {char *ss;
  131.  switch(sig)
  132.    {case SIGILL: ss =   " illegal instruction"; break;
  133.     case SIGTRAP: ss =   " trace trap"; break;
  134.     case SIGFPE: ss =   " arithmetic exception"; break;
  135.     case SIGBUS: ss =  " bus error"; break;
  136.     case SIGSEGV: ss =  " segmentation violation"; break;
  137.     case SIGIOT: ss = " IOT instruction"; break;
  138.     case SIGEMT: ss = " EMT instruction"; break;
  139.     case SIGALRM: ss = " alarm clock"; break;
  140.     case SIGINT:  ss = " interrupt"; break;
  141.     default: ss = "";}
  142.  sprintf(notes,"Got signal %d%s",sig,ss);
  143.  note();
  144.  longjmp(again_buff,3);}
  145.  
  146. set_up_signals()
  147. {signal(SIGILL,again_handler);
  148.  signal(SIGTRAP,again_handler);
  149.  signal(SIGFPE,again_handler);
  150.  signal(SIGBUS,again_handler);
  151.  signal(SIGSEGV,again_handler);
  152.  signal(SIGIOT,again_handler);
  153.  signal(SIGEMT,again_handler);
  154.  signal(SIGALRM,again_handler);
  155.  signal(SIGINT,again_handler);}
  156.  
  157. compute_badboy()
  158. {long j,n;
  159.  n = (nbytes < 0) ? - nbytes : nbytes;
  160.  if (malloc_flag == 1)
  161.    {the_data = (unsigned char *) malloc(n);
  162.     badboy = (void (*)()) the_data;}
  163.  for(j=0;j<n;++j) the_data[j] = (rand() >> 7) & 0xFF;
  164.  if (nbytes < 0)
  165.    {sprintf(notes,"Dump of %ld bytes of data",n);
  166.     note();
  167.     for(j=0;j<n;++j)
  168.       {fprintf(stdout,"%3d",the_data[j]);
  169.        if ((j % 20) == 19) putc('\n',stdout); else putc(' ',stdout);}
  170.     putc('\n',stdout);}}
  171.  
  172. try_one_crash()
  173. {if (nbytes > 0)
  174.    (*badboy)();
  175.  else if (nbytes == 0)
  176.    while(1);}
  177.  
  178. char *subprocess_ind = "subprocess";
  179.  
  180. main(argc,argv)
  181.      int argc; char **argv;
  182. {long nsubs,hrs,mns,scs,tflag;
  183.  note_buffer = (char *) malloc(512);
  184.  notes = note_buffer;
  185.  if ((argc == 6) &&
  186.      (strcmp(argv[5],subprocess_ind) == 0))
  187.    {sprintf(note_buffer,"Subprocess %s: ",argv[4]);
  188.     notes = note_buffer + strlen(note_buffer);
  189.     sprintf(notes,"starting");
  190.     note();
  191.     old_main(4,argv);}
  192.  else if (argc == 4)
  193.    old_main(4,argv);
  194.  else if (argc == 5)
  195.    {if (strchr(argv[4],':'))
  196.       {sscanf(argv[4],"%d:%d:%d",&hrs,&mns,&scs);
  197.        tflag = 1;
  198.        nsubs = (((hrs * 60) + mns) * 60) + scs;
  199.        sprintf(notes,"Subprocess run for %d seconds (%d %02d:%02d:%02d)\n",
  200.            nsubs, hrs / 24, hrs % 24,mns,scs);}
  201.     else
  202.       {tflag = 0;
  203.        nsubs = atol(argv[4]);
  204.        sprintf(notes,"Creating %d crashme subprocesses",nsubs);}
  205.     note();
  206.     vfork_main(tflag,nsubs,argv[0],argv[1],atol(argv[2]),argv[3]);}
  207.  else
  208.    {sprintf(notes,"crashme <nbytes> <srand> <ntrys> [nsub]");
  209.     note();}}
  210.  
  211. old_main(argc,argv)
  212.      int argc;
  213.      char **argv;
  214. {sprintf(notes,"Crashme: (c) Copyright 1990, 1991 George J. Carrette");
  215.  note();
  216.  sprintf(notes,"Version: %s",crashme_version);
  217.  note();
  218.  nbytes = atol(argv[1]);
  219.  if (argv[1][0] == '+') malloc_flag = 1;
  220.  nseed = atol(argv[2]);
  221.  ntrys = atol(argv[3]);
  222.  sprintf(notes,"crashme %ld %ld %ld",nbytes,nseed,ntrys);
  223.  note();
  224.  if (malloc_flag == 0)
  225.    {the_data = (unsigned char *) malloc((nbytes < 0) ? -nbytes : nbytes);
  226.     badboy = (void (*)()) the_data;
  227.     sprintf(notes,"Badboy at %d. 0x%X",badboy,badboy);
  228.     note();}
  229.  srand(nseed);
  230.  badboy_loop();}
  231.  
  232. badboy_loop()
  233. {int i;
  234.  for(i=0;i<ntrys;++i)
  235.    {compute_badboy();
  236.     if (malloc_flag == 1)
  237.       sprintf(notes,"try %ld, Badboy at %d. 0x%X",i,badboy,badboy);
  238.     else
  239.       sprintf(notes,"try %ld",i);
  240.     note();
  241.     if (setjmp(again_buff) == 3)
  242.       {sprintf(notes,"Barfed");
  243.        note();}
  244.     else
  245.       {set_up_signals();
  246.        alarm(10);
  247.        try_one_crash();
  248.        sprintf(notes,"didn't barf!");
  249.        note();}}}
  250.  
  251. vfork_main(tflag,nsubs,cmd,nb,sr,nt)
  252.      long tflag,nsubs,sr;
  253.      char *cmd,*nb,*nt;
  254. {long j,status,pid,n,seq,total_time,dys,hrs,mns,scs;
  255.  char arg2[20],arg4[20];
  256.  time_t before_time,after_time;
  257.  if (tflag == 1)
  258.    {seq = 1;
  259.     n = 100000000;}
  260.  else if (nsubs < 0)
  261.    {n = -nsubs;
  262.     seq = 1;}
  263.  else
  264.    {n = nsubs;
  265.     seq = 0;}
  266.  time(&before_time);
  267.  for(j=0;j<n;++j)
  268.    {sprintf(arg2,"%d",sr+j);
  269.     sprintf(arg4,"%d",j+1);
  270.     status = vfork();
  271.     if (status == 0)
  272.       {status = execl(cmd,cmd,nb,arg2,nt,arg4,subprocess_ind,0);
  273.        if (status == -1)
  274.      {perror(cmd);
  275.       exit(1);}}
  276.     else if (status < 0)
  277.       perror(cmd);
  278.     else
  279.       {sprintf(notes,"pid = %d 0x%X",status,status);
  280.        note();
  281.        if (seq == 1)
  282.      while((pid = wait(&status)) > 0)
  283.        {sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
  284.         note();}
  285.        if (tflag == 1)
  286.      {time(&after_time);
  287.       total_time = after_time - before_time;
  288.       if (total_time >= nsubs)
  289.         {sprintf(notes,"Time limit reached after run %d\n",j+1);
  290.          note();
  291.          break;}}}}
  292.  if (seq == 0)
  293.    while((pid = wait(&status)) > 0)
  294.      {sprintf(notes,"pid %d 0x%X exited with status %d",pid,pid,status);
  295.       note();}
  296.  time(&after_time);
  297.  total_time = after_time - before_time;
  298.  scs = total_time;
  299.  mns = scs / 60;
  300.  hrs = mns / 60;
  301.  dys = hrs / 24;
  302.  scs = scs % 60;
  303.  mns = mns % 60;
  304.  hrs = hrs % 24;
  305.  sprintf(notes,
  306.      "Test complete, total real time: %d seconds (%d %02d:%02d:%02d)\n",
  307.      total_time,dys,hrs,mns,scs);
  308.  note();}
  309.