huge.c raw

   1  
   2  #include <limits.h>
   3  #include <stdio.h>
   4  #include <stdlib.h>
   5  
   6  #ifndef GC_IGNORE_WARN
   7  /*
   8   * Ignore misleading "Out of Memory!" warning (which is printed on every
   9   * `GC_MALLOC` call below) by defining this macro before include `gc.h` file.
  10   */
  11  #  define GC_IGNORE_WARN
  12  #endif
  13  
  14  #ifndef GC_MAXIMUM_HEAP_SIZE
  15  #  define GC_MAXIMUM_HEAP_SIZE (100 * 1024 * 1024)
  16  #  define GC_INITIAL_HEAP_SIZE (GC_MAXIMUM_HEAP_SIZE / 20)
  17  /*
  18   * Otherwise heap expansion aborts when deallocating large block.
  19   * That is OK.  We test this corner case mostly to make sure that
  20   * it fails predictably.
  21   */
  22  #endif
  23  
  24  #ifndef GC_ATTR_ALLOC_SIZE
  25  /*
  26   * Omit `alloc_size` attribute to avoid compiler warnings about exceeding
  27   * maximum object size when values close to `GC_SWORD_MAX` are passed
  28   * to `GC_MALLOC()`.
  29   */
  30  #  define GC_ATTR_ALLOC_SIZE(argnum) /*< empty */
  31  #endif
  32  
  33  #include "gc.h"
  34  
  35  /*
  36   * Check that very large allocation requests fail.  "Success" would usually
  37   * indicate that the size was somehow converted to a negative number.
  38   * Clients should not do this, but we should fail in the expected manner.
  39   */
  40  #define CHECK_ALLOC_FAILED(r, sz_str)                                         \
  41    do {                                                                        \
  42      if (NULL != (r)) {                                                        \
  43        fprintf(stderr, "Size " sz_str " allocation unexpectedly succeeded\n"); \
  44        exit(1);                                                                \
  45      }                                                                         \
  46    } while (0)
  47  
  48  #undef SIZE_MAX
  49  #define SIZE_MAX (~(size_t)0)
  50  
  51  #define U_SSIZE_MAX (SIZE_MAX >> 1) /*< unsigned */
  52  
  53  int
  54  main(void)
  55  {
  56    GC_INIT();
  57  
  58    CHECK_ALLOC_FAILED(GC_MALLOC(U_SSIZE_MAX - 4096), "SSIZE_MAX-4096");
  59    /* Skip other checks to avoid "exceeds maximum object size" gcc warning. */
  60  #if !(defined(_FORTIFY_SOURCE) && defined(__x86_64__) && defined(__ILP32__))
  61    CHECK_ALLOC_FAILED(GC_MALLOC(U_SSIZE_MAX - 1024), "SSIZE_MAX-1024");
  62    CHECK_ALLOC_FAILED(GC_MALLOC(U_SSIZE_MAX), "SSIZE_MAX");
  63  #endif
  64  #if !defined(_FORTIFY_SOURCE)
  65    CHECK_ALLOC_FAILED(GC_MALLOC(U_SSIZE_MAX + 1), "SSIZE_MAX+1");
  66    CHECK_ALLOC_FAILED(GC_MALLOC(U_SSIZE_MAX + 1024), "SSIZE_MAX+1024");
  67    CHECK_ALLOC_FAILED(GC_MALLOC(SIZE_MAX - 1024), "SIZE_MAX-1024");
  68    CHECK_ALLOC_FAILED(GC_MALLOC(SIZE_MAX - 16), "SIZE_MAX-16");
  69    CHECK_ALLOC_FAILED(GC_MALLOC(SIZE_MAX - 8), "SIZE_MAX-8");
  70    CHECK_ALLOC_FAILED(GC_MALLOC(SIZE_MAX - 4), "SIZE_MAX-4");
  71    CHECK_ALLOC_FAILED(GC_MALLOC(SIZE_MAX), "SIZE_MAX");
  72  #endif
  73    printf("SUCCEEDED\n");
  74    return 0;
  75  }
  76