home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13454 < prev    next >
Encoding:
Text File  |  1992-09-10  |  1.9 KB  |  79 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!darwin.sura.net!Sirius.dfn.de!chx400!bernina!neptune!templ
  3. From: templ@inf.ethz.ch (Josef Templ)
  4. Subject: problem with signal handling
  5. Message-ID: <1992Sep10.111952.2596@neptune.inf.ethz.ch>
  6. Sender: news@neptune.inf.ethz.ch (Mr News)
  7. Nntp-Posting-Host: lillian-gw
  8. Organization: Dept. Informatik, Swiss Federal Institute of Technology (ETH)
  9. Date: Thu, 10 Sep 1992 11:19:52 GMT
  10. Lines: 67
  11.  
  12. Does anybody know why the following program does not work
  13. as expected on Sun computers?
  14. It works fine e.g. on DECstation running Ultrix.
  15.  
  16. The program is intended to generate a stack overflow,
  17. to enter a signal handler (catch the overflow), and
  18. to recover from the signal by means of a longjmp call.
  19. Surprisingly, it does work with 
  20. limit stacksize 1000,
  21. but it does not work with 
  22. limit stacksize 2000.
  23. Is there anything wrong with the program or with Sun's
  24. C compiler or with SunOS?
  25.  
  26. /* stack.c    J. Templ, 4.09.92,
  27. test program to find out how to recover from a stack overflow */
  28.  
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <setjmp.h>
  32. #include <malloc.h>
  33.  
  34. jmp_buf env;
  35.  
  36. void SignalHandler(sig, code, scp, addr) 
  37.     int sig, code;
  38.     struct sigcontext *scp;
  39.     char *addr;
  40. {
  41. /*    printf("in SignalHandler %d\n", sig); */
  42.     longjmp(env, 1);
  43. }
  44.  
  45. void GenStackOvfl() {
  46.     GenStackOvfl();
  47. }
  48.  
  49. void SetSignalStack() {
  50.     struct sigstack ss;
  51.     ss.ss_sp = (char*) (((int) malloc(100000)) + 100000 - 256);
  52.     ss.ss_onstack = 0;
  53.     sigstack(&ss, 0);
  54. }
  55.  
  56. void InstallSignalHandler() {
  57.     struct sigvec vec;
  58.     vec.sv_handler = SignalHandler;
  59.     vec.sv_mask = 0;
  60.     vec.sv_flags = SV_ONSTACK;
  61.     sigvec(SIGINT, &vec, 0);
  62.     sigvec(SIGILL, &vec, 0);
  63.     sigvec(SIGFPE, &vec, 0);
  64.     sigvec(SIGBUS, &vec, 0);
  65.     sigvec(SIGSEGV, &vec, 0);
  66. }
  67.  
  68. main() {
  69.     SetSignalStack(); InstallSignalHandler();
  70.     if (setjmp(env) == 0) {printf("before signal\n"); GenStackOvfl();}
  71.     else {printf("recovered from signal\n");}
  72. }
  73.  
  74.  
  75. Any comments are deeply appreciated.
  76. Josef Templ
  77. templ@inf.ethz.ch
  78.  
  79.