home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3599 / crashme.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-11  |  6.7 KB  |  215 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.2 25-JUN-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. Comments may be addressed to the author at GJC@MITECH.COM
  36.  
  37. Version Date         Description
  38. ----------------------------------------------------------------------
  39.  1.0    early 1990   initial hack.
  40.  1.1    19-SEP-1990  added more signals and an alarm to abort looping.
  41.  1.2    25-JUN-1991  added [nsub] to vfork multiple subprocesses of self.
  42.  
  43.  
  44.                         CRASH REPORTS
  45.  
  46. Date,               Machine              Crashme      Reported 
  47. Crashme Ver   Make    Model OS Version   Arguments    by:        
  48. ------------------------------------------------------------------------------
  49. 10-JUL-90 1.0 SUN     4/110 4.1          1000 20 200    GJC
  50. 10-JUL-90 1.0 SUN     4/280 4.0.3        1000 20 200    GJC
  51. 31-JUL-90 1.0 DIGITAL DECstation 3100    100 10 10000   GAVRON@ARIZONA.EDU
  52. 31-JUL-90 1.0 IBM     RT                 100 10 10000   GAVRON@ARIZONA.EDU
  53.  3-AUG-90 1.0 Alliant FX/2800                           SJA@SIRIUS.HUT.FI
  54. 27-JUN-91 1.2 SUN     4/110 4.1.1        10 1000 10     LPH@PARADIGM.COM   
  55. 27-JUN-91 1.2 SUN     4/110 4.1.1        1000 20 200 10 LPH@PARADIGM.COM   
  56.  
  57.  
  58. Notes: Crashme V1.0 {1000 20 200} used to down the SUN 4/110. V1.2 does *not*
  59. crash SUNOS 4.1.1 on the same arguments. Although using the extra argument
  60. for subprocesses it will crash, with the console reporting:
  61. "Bad Trap, Bad Kernel Read Fault, Bus error. Reboot"
  62.  
  63. */
  64.  
  65.  
  66. #include <stdio.h>
  67. #include <signal.h>
  68. #include <setjmp.h>
  69.  
  70. long nbytes,nseed,ntrys;
  71. unsigned char *the_data;
  72. char *note_buffer;
  73. char *notes;
  74.  
  75. note()
  76. {strcat(note_buffer,"\n");
  77.  fputs(note_buffer,stdout);}
  78.  
  79. jmp_buf again_buff;
  80.  
  81. void (*badboy)();
  82.  
  83. void again_handler(sig, code, scp, addr)
  84.      int sig, code;
  85.      struct sigcontext *scp;
  86.      char *addr;
  87. {char *ss;
  88.  switch(sig)
  89.    {case SIGILL: ss =   " illegal instruction"; break;
  90.     case SIGTRAP: ss =   " trace trap"; break;
  91.     case SIGFPE: ss =   " arithmetic exception"; break;
  92.     case SIGBUS: ss =  " bus error"; break;
  93.     case SIGSEGV: ss =  " segmentation violation"; break;
  94.     case SIGIOT: ss = " IOT instruction"; break;
  95.     case SIGEMT: ss = " EMT instruction"; break;
  96.     case SIGALRM: ss = " alarm clock"; break;
  97.     default: ss = "";}
  98.  sprintf(notes,"Got signal %d%s",sig,ss);
  99.  note();
  100.  longjmp(again_buff,3);}
  101.  
  102. set_up_signals()
  103. {signal(SIGILL,again_handler);
  104.  signal(SIGTRAP,again_handler);
  105.  signal(SIGFPE,again_handler);
  106.  signal(SIGBUS,again_handler);
  107.  signal(SIGSEGV,again_handler);
  108.  signal(SIGIOT,again_handler);
  109.  signal(SIGEMT,again_handler);
  110.  signal(SIGALRM,again_handler);}
  111.  
  112. compute_badboy()
  113. {long j,n;
  114.  n = (nbytes < 0) ? - nbytes : nbytes;
  115.  for(j=0;j<n;++j) the_data[j] = (rand() >> 7) & 0xFF;
  116.  if (nbytes < 0)
  117.    {sprintf(notes,"Dump of %ld bytes of data",n);
  118.     note();
  119.     for(j=0;j<n;++j)
  120.       {fprintf(stdout,"%3d",the_data[j]);
  121.        if ((j % 20) == 19) putc('\n',stdout); else putc(' ',stdout);}
  122.     putc('\n',stdout);}}
  123.  
  124. try_one_crash()
  125. {compute_badboy();
  126.  if (nbytes > 0)
  127.    (*badboy)();
  128.  else if (nbytes == 0)
  129.    while(1);}
  130.  
  131. char *subprocess_ind = "subprocess";
  132.  
  133. main(argc,argv)
  134.      int argc; char **argv;
  135. {long nsubs;
  136.  note_buffer = (char *) malloc(512);
  137.  notes = note_buffer;
  138.  if ((argc == 6) &&
  139.      (strcmp(argv[5],subprocess_ind) == 0))
  140.    {sprintf(note_buffer,"Subprocess %s: ",argv[4]);
  141.     notes = note_buffer + strlen(note_buffer);
  142.     sprintf(notes,"starting");
  143.     note();
  144.     old_main(4,argv);}
  145.  else if (argc == 4)
  146.    old_main(4,argv);
  147.  else if (argc == 5)
  148.    {nsubs = atol(argv[4]);
  149.     sprintf(notes,"Creating %d crashme subprocesses",nsubs);
  150.     note();
  151.     vfork_main(nsubs,argv[0],atol(argv[1]),atol(argv[2]),atol(argv[3]));}
  152.  else
  153.    {sprintf(notes,"crashme <nbytes> <srand> <ntrys> [nsub]");
  154.     note();}}
  155.  
  156. old_main(argc,argv)
  157.      int argc;
  158.      char **argv;
  159. {sprintf(notes,"Crashme: (c) Copyright 1990, 1991 George J. Carrette");
  160.  note();
  161.  sprintf(notes,"Version: %s",crashme_version);
  162.  note();
  163.  nbytes = atol(argv[1]);
  164.  nseed = atol(argv[2]);
  165.  ntrys = atol(argv[3]);
  166.  sprintf(notes,"crashme %ld %ld %ld",nbytes,nseed,ntrys);
  167.  note();
  168.  the_data = (unsigned char *) malloc((nbytes < 0) ? -nbytes : nbytes);
  169.  badboy = (void (*)()) the_data;
  170.  sprintf(notes,"Badboy at %d. 0x%X",badboy,badboy);
  171.  note();
  172.  srand(nseed);
  173.  badboy_loop();}
  174.  
  175. badboy_loop()
  176. {int i;
  177.  for(i=0;i<ntrys;++i)
  178.    {sprintf(notes,"try %ld",i);
  179.     note();
  180.     if (setjmp(again_buff) == 3)
  181.       {sprintf(notes,"Barfed");
  182.        note();}
  183.     else
  184.       {set_up_signals();
  185.        alarm(10);
  186.        try_one_crash();
  187.        sprintf(notes,"didn't barf!");
  188.        note();}}}
  189.  
  190. vfork_main(nsubs,cmd,nb,sr,nt)
  191.      long nsubs,nb,sr,nt;
  192.      char *cmd;
  193. {long j,status,pid;
  194.  char arg1[20],arg2[20],arg3[20],arg4[20];
  195.  for(j=0;j<nsubs;++j)
  196.    {sprintf(arg1,"%d",nb);
  197.     sprintf(arg2,"%d",sr+j);
  198.     sprintf(arg3,"%d",nt);
  199.     sprintf(arg4,"%d",j+1);
  200.     status = vfork();
  201.     if (status == 0)
  202.       {status = execl(cmd,cmd,arg1,arg2,arg3,arg4,subprocess_ind,0);
  203.        if (status == -1)
  204.      {perror(cmd);
  205.       exit(1);}}
  206.     else if (status < 0)
  207.       perror(cmd);
  208.     else
  209.       {sprintf(notes,"pid = %d",status);
  210.        note();}}
  211.  while((pid = wait(&status)) > 0)
  212.    {sprintf(notes,"pid %d exited with status %d",pid,status);
  213.     note();}}
  214.  
  215.