home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_03 / v7n3069a.txt < prev    next >
Text File  |  1989-03-05  |  6KB  |  336 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define NBR_OF_BYTES 52
  5.  
  6. #define PRTSTK(fp) {\
  7.   printf("%04.4lx ", (long)fp);\
  8.   printf("%02x ", (unsigned char)*(fp));\
  9.   printf("%c\n", isprint((unsigned char)*(fp))\
  10.     ? (unsigned char)*(fp) : ' ');\
  11. }
  12.  
  13. fcn1b()
  14. {
  15.     unsigned char stk_top;
  16.     static unsigned char *s;
  17.     stk_top = 0xff;
  18.     for (s = &stk_top + NBR_OF_BYTES; s >= &stk_top; --s)
  19.         PRTSTK(s);
  20. }
  21.  
  22. fcn1(a, b, c)
  23. int a, b, c;
  24. {
  25.     int j;
  26.     char s[7];
  27.     j = 5;
  28.     strcpy(s, "123456");
  29.     fcn1b();
  30. }
  31.  
  32. main()
  33. {
  34.     int i;
  35.     i = 1;
  36.     fcn1(2, 3, 4);
  37. }
  38. 7ffffeef 00  
  39. 7ffffeee 00  
  40. 7ffffeed 00  
  41. 7ffffeec f6  
  42. 7ffffeeb 7f  
  43. 7ffffeea ff  
  44. 7ffffee9 fe  
  45. 7ffffee8 fc  
  46. 7ffffee7 00  
  47. 7ffffee6 00  
  48. 7ffffee5 00  
  49. 7ffffee4 01  
  50. 7ffffee3 00  
  51. 7ffffee2 00  
  52. 7ffffee1 00  
  53. 7ffffee0 04  
  54. 7ffffedf 00  
  55. 7ffffede 00  
  56. 7ffffedd 00  
  57. 7ffffedc 03  
  58. 7ffffedb 00  
  59. 7ffffeda 00  
  60. 7ffffed9 00  
  61. 7ffffed8 02  
  62. 7ffffed7 00  
  63. 7ffffed6 00  
  64. 7ffffed5 01  
  65. 7ffffed4 f3  
  66. 7ffffed3 7f  
  67. 7ffffed2 ff  
  68. 7ffffed1 fe  
  69. 7ffffed0 e8  
  70. 7ffffecf 00  
  71. 7ffffece 00  
  72. 7ffffecd 00  
  73. 7ffffecc 05  
  74. 7ffffecb 00  
  75. 7ffffeca 36 6
  76. 7ffffec9 35 5
  77. 7ffffec8 34 4
  78. 7ffffec7 33 3
  79. 7ffffec6 32 2
  80. 7ffffec5 31 1
  81. 7ffffec4 00  
  82. 7ffffec3 00  
  83. 7ffffec2 00  
  84. 7ffffec1 01  
  85. 7ffffec0 ce  
  86. 7ffffebf 7f      
  87. 7ffffebe ff  
  88. 7ffffebd fe  
  89. 7ffffebc d0  
  90. 7ffffebb ff     
  91. /* badstk.c - writes data incorrectly into an automatic
  92.    variable */
  93.  
  94. fcn2()
  95. {
  96.     char s[3];
  97.     /* about to write over frame pointer and return address */
  98.     strcpy(s, "1234567");
  99. }
  100.  
  101. fcn()
  102. {
  103.     int x;
  104.     x = 1;
  105.     fcn2();
  106. }
  107.  
  108. main()
  109. {
  110.     char a;
  111.     a = '~';
  112.     fcn();
  113.     exit(0);
  114. }
  115. /* badstk.c - writes data incorrectly into an automatic
  116.    variable */
  117. #include "chkstk.h"
  118.  
  119. fcn2()
  120. {
  121.     char s[3];
  122.     CHKSTK;
  123.     /* about to write over frame pointer and return address */
  124.     strcpy(s, "1234567");
  125.     CHKSTK;
  126. }
  127.  
  128. fcn()
  129. {
  130.     int x;
  131.     CHKSTK;
  132.     x = 1;
  133.     fcn2();
  134.     CHKSTK;
  135. }
  136.  
  137. main()
  138. {
  139.     char a;
  140.     CHKSTKINIT;
  141.     a = '~';
  142.     fcn();
  143.     CHKSTK;
  144.     exit(0);
  145. }
  146. Error: Stack is corrupted (badstk.c, 11)
  147. 7ffffecf 37 7 
  148. 7ffffece 36 6 
  149. 7ffffecd 35 5 
  150. 7ffffecc 34 4 <= Corrupted frame pointer
  151. 7ffffecb 33 3 
  152. 7ffffeca 32 2 
  153. 7ffffec9 31 1 
  154. 7ffffec8 f8   
  155. 7ffffec7 00   
  156. 7ffffec6 00   
  157. 7ffffec5 00   
  158. 7ffffec4 0b   
  159. 7ffffec3 00   
  160. 7ffffec2 40 @ 
  161. 7ffffec1 03   
  162. 7ffffec0 a5   
  163. 7ffffebf 00   
  164. 7ffffebe 00   
  165. 7ffffebd 01   
  166. 7ffffebc 41 A 
  167. 7ffffebb 7f   
  168. 7ffffeba ff   
  169. 7ffffeb9 fe   
  170. 7ffffeb8 cc   
  171. 7ffffeb7 ff   
  172. 7ffffeb6 ff   
  173. 7ffffeb5 fe   
  174. 7ffffeb4 c9   
  175. 7ffffeb3 7f   
  176. 7ffffeb2 ff   
  177. 7ffffeb1 fe   
  178. 7ffffeb0 cc   
  179. 7ffffeaf 00   
  180. 7ffffeae 00   
  181. 7ffffead 02   
  182. 7ffffeac ec   
  183. 7ffffeab 7f   
  184. 7ffffeaa ff   
  185. 7ffffea9 fe   
  186. 7ffffea8 b8   
  187. 7ffffea7 ff   <= stack top
  188. /* interface to the chkstk module */
  189.  
  190. /*
  191. Instructions:
  192. -   Put CHKSTKINIT immediatly after declaring local variables
  193.     in main()
  194. -   Put CHKSTK anywhere you wish to have the integrity of
  195.     the stack checked
  196. */
  197.  
  198. #define CHKSTKINIT \
  199.     unsigned char no_conflict; \
  200.     no_conflict = 0xaa; \
  201.     chkstkinit();
  202. /*
  203.     For these functions to work with Microsoft compilers,
  204.     main() must have at least one automatic variable,
  205.     so we need the variable no_conflict in the macro 
  206.     definition.
  207. */
  208. #define CHKSTK chkstk(__FILE__, __LINE__);
  209.  
  210. void chkstkinit();
  211. void chkstk();
  212. #include <stdio.h>
  213. #include <ctype.h>
  214. #include "chkstk.h"
  215.  
  216. /* NCR UNIX on NCR tower */
  217. #if defined(tower)
  218. #define OFFSET 4
  219. #define NEXTFP(nextfp, fp) \
  220.   nextfp = *fp * 0x1000000 + *(fp + 1) * 0x10000 + \
  221.   *(fp + 2) * 0x100 + *(fp + 3);
  222. #endif
  223.  
  224. /* XENIX and UNIX 80386 */
  225. #if defined(M_I386) | defined(i386)
  226. #define OFFSET 1
  227. #define NEXTFP(nextfp, fp) \
  228.   nextfp = *fp + *(fp + 1) * 0x100 + \
  229.   *(fp + 2) * 0x10000 + *(fp + 3) * 0x1000000;
  230. #endif
  231.  
  232. /* Microsoft C 5.10, Quick C, SCO Xenix */
  233. /* Huge, Large or Code models */
  234. #if defined(M_I86LM) | defined(M_I86HM) | defined(M_I86CM)
  235. #define OFFSET 2
  236. #define NEXTFP(nextfp, fp) \
  237.   nextfp = (long)fp & 0xffff0000;\
  238.   nextfp = nextfp + *fp + *(fp + 1) * 0x100;
  239. #endif
  240.  
  241. /* Microsoft C 5.10, Quick C, SCO Xenix */
  242. /* Middle or Small models */
  243. #if defined(M_I86MM) | defined(M_I86SM)
  244. #define OFFSET 2
  245. #define NEXTFP(nextfp, fp) \
  246.   nextfp = *fp + *(fp + 1) * 0x100;
  247. #endif
  248.  
  249. #define PRTSTK(fp, q) {\
  250.   trace("%04.4lx ", (long)fp);\
  251.   trace("%02x ", (unsigned char)*(fp));\
  252.   trace("%c", isprint((unsigned char)*(fp))\
  253.      ? (unsigned char)*(fp) : ' ');\
  254.   trace(" %s\n", q);\
  255. }
  256.  
  257. static unsigned char *stk_bottom;
  258. static long first_fp;
  259. static void dumpstk();
  260. static void trace();
  261.  
  262. void chkstkinit()
  263. {
  264.     unsigned char stk_top;
  265.     static long nextfp;
  266.     static unsigned char *fp;
  267.     stk_top = 0xaa;
  268.     fp = &stk_top + OFFSET;
  269.     NEXTFP(nextfp, fp);
  270.     stk_bottom = (unsigned char *)nextfp;
  271.     first_fp = *stk_bottom + *(stk_bottom + 1) * 0x100;
  272. }
  273.  
  274. void chkstk(file, line)
  275. char *file;
  276. int line;
  277. {
  278.     unsigned char stk_top;
  279.     static unsigned char *fp;
  280.     static long nextfp;
  281.     stk_top = 0xff;
  282.     fp = &stk_top + OFFSET;
  283.     while (fp != stk_bottom) {
  284.         NEXTFP(nextfp, fp);
  285.         if ((unsigned char *)nextfp < fp ||
  286.              (unsigned char *)nextfp > stk_bottom) {
  287.             trace("Error: Stack is corrupted (%s, %d)\n",
  288.               file, line);
  289.             fprintf(stderr, "Error: Stack is corrupted (%s, %d)\n",
  290.               file, line);
  291.             dumpstk(fp);
  292.             exit(1);
  293.         }
  294.         fp = (unsigned char *)nextfp;
  295.     }
  296.     if ((*fp + *(fp + 1) * 0x100) != first_fp) {
  297.         trace("Error: stack corrupted (%s, %d)\n",
  298.           file, line);
  299.         fprintf(stderr, "Error: Stack is corrupted (%s, %d)\n",
  300.           file, line);
  301.         dumpstk(fp);
  302.         exit(1);
  303.     }
  304. }
  305.  
  306. static void dumpstk(bfp)
  307. unsigned char *bfp;
  308. {
  309.     unsigned char stk_top;
  310.     static unsigned char *fp, *x;
  311.     static long nextfp;
  312.     stk_top = 0xff;
  313.     PRTSTK(bfp + 3, "");
  314.     PRTSTK(bfp + 2, "");
  315.     PRTSTK(bfp + 1, "");
  316.     PRTSTK(bfp, "<= Corrupted frame pointer");
  317.     fp = bfp - 1;
  318.     while (fp != &stk_top) {
  319.         PRTSTK(fp, "");
  320.         --fp;
  321.     }
  322.     PRTSTK(fp, "<= stack top");
  323. }
  324.  
  325. static void trace(f, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  326. char *f, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8, *a9;
  327. {
  328.     static FILE *out = NULL;
  329.     if (out == NULL) {
  330.         unlink("chkstk");
  331.         out = fopen("chkstk", "w");
  332.         setbuf(out, NULL);
  333.     }
  334.     fprintf(out, f, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  335. }
  336.