1// RUN: %clang_scudo %s -o %t
2// RUN: %run %t valid 2>&1
3// RUN: not %run %t invalid 2>&1 | FileCheck --check-prefix=CHECK-align %s
4// RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
5// RUN: not %run %t double-free 2>&1 | FileCheck --check-prefix=CHECK-double-free %s
6// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t realloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s
7// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t realloc 2>&1
8
9// Tests that the various aligned allocation functions work as intended. Also
10// 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(s1: argv[1], s2: "valid")) {
32 posix_memalign(memptr: &p, alignment: alignment, size: size);
33 assert(p);
34 assert(((uintptr_t)p & (alignment - 1)) == 0);
35 free(ptr: p);
36 p = aligned_alloc(alignment, size);
37 assert(p);
38 assert(((uintptr_t)p & (alignment - 1)) == 0);
39 free(ptr: p);
40 // Tests various combinations of alignment and sizes
41 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: alignment, size: size - (2 * sizeof(void *) * k));
47 assert(p);
48 assert(((uintptr_t)p & (alignment - 1)) == 0);
49 free(ptr: p);
50 }
51 }
52 }
53 // For larger alignment, reduce the number of allocations to avoid running
54 // 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: alignment, size: 0x1000 - (2 * sizeof(void *) * k));
59 assert(p);
60 assert(((uintptr_t)p & (alignment - 1)) == 0);
61 free(ptr: p);
62 }
63 }
64 }
65 if (!strcmp(s1: argv[1], s2: "invalid")) {
66 // Alignment is not a power of 2.
67 p = memalign(alignment: alignment - 1, size: size);
68 // CHECK-align: Scudo ERROR: invalid allocation alignment
69 assert(!p);
70 // Size is not a multiple of alignment.
71 p = aligned_alloc(alignment, size: 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(memptr: &p, alignment: 3, size: 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(memptr: &p, alignment: 2, size: size);
81 assert(p == p_unchanged);
82 assert(err == EINVAL);
83 }
84 if (!strcmp(s1: argv[1], s2: "double-free")) {
85 void *p = NULL;
86 posix_memalign(memptr: &p, alignment: 0x100, size: sizeof(int));
87 assert(p);
88 free(ptr: p);
89 free(ptr: p);
90 }
91 if (!strcmp(s1: argv[1], s2: "realloc")) {
92 // We cannot reallocate a memalign'd chunk.
93 void *p = memalign(alignment: 16, size: 16);
94 assert(p);
95 p = realloc(ptr: p, size: 32);
96 free(ptr: p);
97 }
98 return 0;
99}
100
101// CHECK-double-free: ERROR: invalid chunk state
102// CHECK-realloc: ERROR: allocation type mismatch when reallocating address
103

source code of compiler-rt/test/scudo/memalign.c