brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 8c6cdf6 Raw
103 lines · c
1// RUN: %clang_scudo %s -o %t2// RUN:                                                 %run %t valid       2>&13// RUN:                                             not %run %t invalid     2>&1 | FileCheck --check-prefix=CHECK-align %s4// RUN: %env_scudo_opts=allocator_may_return_null=1     %run %t invalid     2>&15// RUN:                                             not %run %t double-free 2>&1 | FileCheck --check-prefix=CHECK-double-free %s6// RUN: %env_scudo_opts=DeallocationTypeMismatch=1  not %run %t realloc     2>&1 | FileCheck --check-prefix=CHECK-realloc %s7// RUN: %env_scudo_opts=DeallocationTypeMismatch=0      %run %t realloc     2>&18 9// Tests that the various aligned allocation functions work as intended. Also10// tests for the condition where the alignment is not a power of 2.11 12#include <assert.h>13#include <errno.h>14#include <malloc.h>15#include <stdint.h>16#include <stdlib.h>17#include <string.h>18#include <unistd.h>19 20// Sometimes the headers may not have this...21void *aligned_alloc(size_t alignment, size_t size);22 23int main(int argc, char **argv) {24  void *p = NULL;25  size_t alignment = 1U << 12;26  size_t size = 1U << 12;27  int err;28 29  assert(argc == 2);30 31  if (!strcmp(argv[1], "valid")) {32    posix_memalign(&p, alignment, size);33    assert(p);34    assert(((uintptr_t)p & (alignment - 1)) == 0);35    free(p);36    p = aligned_alloc(alignment, size);37    assert(p);38    assert(((uintptr_t)p & (alignment - 1)) == 0);39    free(p);40    // Tests various combinations of alignment and sizes41    for (int i = (sizeof(void *) == 4) ? 3 : 4; i < 19; i++) {42      alignment = 1U << i;43      for (int j = 1; j < 33; j++) {44        size = 0x800 * j;45        for (int k = 0; k < 3; k++) {46          p = memalign(alignment, size - (2 * sizeof(void *) * k));47          assert(p);48          assert(((uintptr_t)p & (alignment - 1)) == 0);49          free(p);50        }51      }52    }53    // For larger alignment, reduce the number of allocations to avoid running54    // out of potential addresses (on 32-bit).55    for (int i = 19; i <= 24; i++) {56      alignment = 1U << i;57      for (int k = 0; k < 3; k++) {58        p = memalign(alignment, 0x1000 - (2 * sizeof(void *) * k));59        assert(p);60        assert(((uintptr_t)p & (alignment - 1)) == 0);61        free(p);62      }63    }64  }65  if (!strcmp(argv[1], "invalid")) {66    // Alignment is not a power of 2.67    p = memalign(alignment - 1, size);68    // CHECK-align: Scudo ERROR: invalid allocation alignment69    assert(!p);70    // Size is not a multiple of alignment.71    p = aligned_alloc(alignment, size >> 1);72    assert(!p);73    void *p_unchanged = (void *)0x42UL;74    p = p_unchanged;75    // Alignment is not a power of 2.76    err = posix_memalign(&p, 3, size);77    assert(p == p_unchanged);78    assert(err == EINVAL);79    // Alignment is a power of 2, but not a multiple of size(void *).80    err = posix_memalign(&p, 2, size);81    assert(p == p_unchanged);82    assert(err == EINVAL);83  }84  if (!strcmp(argv[1], "double-free")) {85    void *p = NULL;86    posix_memalign(&p, 0x100, sizeof(int));87    assert(p);88    free(p);89    free(p);90  }91  if (!strcmp(argv[1], "realloc")) {92    // We cannot reallocate a memalign'd chunk.93    void *p = memalign(16, 16);94    assert(p);95    p = realloc(p, 32);96    free(p);97  }98  return 0;99}100 101// CHECK-double-free: ERROR: invalid chunk state102// CHECK-realloc: ERROR: allocation type mismatch when reallocating address103