smash.c raw

   1  /* Test that overwrite error detection works reasonably. */
   2  
   3  #ifndef GC_DEBUG
   4  #  define GC_DEBUG
   5  #endif
   6  
   7  #include "gc.h"
   8  
   9  #include <stdio.h>
  10  #include <stdlib.h>
  11  
  12  #define COUNT 7000
  13  #define SIZE 40
  14  
  15  #define CHECK_OUT_OF_MEMORY(p)            \
  16    do {                                    \
  17      if (NULL == (p)) {                    \
  18        fprintf(stderr, "Out of memory\n"); \
  19        exit(69);                           \
  20      }                                     \
  21    } while (0)
  22  
  23  char *A[COUNT];
  24  
  25  char *volatile q;
  26  
  27  int
  28  main(void)
  29  {
  30    int i;
  31    char *p;
  32  
  33    GC_INIT();
  34    for (i = 0; i < COUNT; ++i) {
  35      A[i] = p = (char *)GC_MALLOC(SIZE);
  36      CHECK_OUT_OF_MEMORY(p);
  37      if (i % 3000 == 0) {
  38        q = NULL;
  39        GC_gcollect();
  40      } else if (i % 5678 == 0) {
  41        /*
  42         * Write a byte past the end of the allocated object but not beyond
  43         * the last word of the object's memory.  A `volatile` intermediate
  44         * pointer variable is used to avoid a compiler complain of
  45         * out-of-bounds access.
  46         */
  47        q = &p[(SIZE + i / 2000) /* 42 */];
  48        *q = 42;
  49      }
  50    }
  51    printf("SUCCEEDED\n");
  52    return 0;
  53  }
  54