• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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...
21 void *aligned_alloc(size_t alignment, size_t size);
22 
main(int argc,char ** argv)23 int main(int argc, char **argv)
24 {
25   void *p = NULL;
26   size_t alignment = 1U << 12;
27   size_t size = 1U << 12;
28   int err;
29 
30   assert(argc == 2);
31 
32   if (!strcmp(argv[1], "valid")) {
33     posix_memalign(&p, alignment, size);
34     assert(p);
35     assert(((uintptr_t)p & (alignment - 1)) == 0);
36     free(p);
37     p = aligned_alloc(alignment, size);
38     assert(p);
39     assert(((uintptr_t)p & (alignment - 1)) == 0);
40     free(p);
41     // Tests various combinations of alignment and sizes
42     for (int i = (sizeof(void *) == 4) ? 3 : 4; i < 19; i++) {
43       alignment = 1U << i;
44       for (int j = 1; j < 33; j++) {
45         size = 0x800 * j;
46         for (int k = 0; k < 3; k++) {
47           p = memalign(alignment, size - (2 * sizeof(void *) * k));
48           assert(p);
49           assert(((uintptr_t)p & (alignment - 1)) == 0);
50           free(p);
51         }
52       }
53     }
54     // For larger alignment, reduce the number of allocations to avoid running
55     // out of potential addresses (on 32-bit).
56     for (int i = 19; i <= 24; i++) {
57       alignment = 1U << i;
58       for (int k = 0; k < 3; k++) {
59         p = memalign(alignment, 0x1000 - (2 * sizeof(void *) * k));
60         assert(p);
61         assert(((uintptr_t)p & (alignment - 1)) == 0);
62         free(p);
63       }
64     }
65   }
66   if (!strcmp(argv[1], "invalid")) {
67     // Alignment is not a power of 2.
68     p = memalign(alignment - 1, size);
69     // CHECK-align: Scudo ERROR: invalid allocation alignment
70     assert(!p);
71     // Size is not a multiple of alignment.
72     p = aligned_alloc(alignment, size >> 1);
73     assert(!p);
74     void *p_unchanged = (void *)0x42UL;
75     p = p_unchanged;
76     // Alignment is not a power of 2.
77     err = posix_memalign(&p, 3, size);
78     assert(p == p_unchanged);
79     assert(err == EINVAL);
80     // Alignment is a power of 2, but not a multiple of size(void *).
81     err = posix_memalign(&p, 2, size);
82     assert(p == p_unchanged);
83     assert(err == EINVAL);
84   }
85   if (!strcmp(argv[1], "double-free")) {
86     void *p = NULL;
87     posix_memalign(&p, 0x100, sizeof(int));
88     assert(p);
89     free(p);
90     free(p);
91   }
92   if (!strcmp(argv[1], "realloc")) {
93     // We cannot reallocate a memalign'd chunk.
94     void *p = memalign(16, 16);
95     assert(p);
96     p = realloc(p, 32);
97     free(p);
98   }
99   return 0;
100 }
101 
102 // CHECK-double-free: ERROR: invalid chunk state
103 // CHECK-realloc: ERROR: allocation type mismatch when reallocating address
104