1 // RUN: %clang_scudo %s -lstdc++ -o %t
2 // RUN: %run %t ownership          2>&1
3 // RUN: %run %t ownership-and-size 2>&1
4 // RUN: %run %t heap-size          2>&1
5 
6 // Tests that the sanitizer interface functions behave appropriately.
7 
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <string.h>
11 
12 #include <vector>
13 
14 #include <sanitizer/allocator_interface.h>
15 
16 int main(int argc, char **argv)
17 {
18   assert(argc == 2);
19 
20   if (!strcmp(argv[1], "ownership")) {
21     // Ensures that __sanitizer_get_ownership can be called before any other
22     // allocator function, and that it behaves properly on a pointer not owned
23     // by us.
24     assert(!__sanitizer_get_ownership(argv));
25   }
26   if (!strcmp(argv[1], "ownership-and-size")) {
27     // Tests that __sanitizer_get_ownership and __sanitizer_get_allocated_size
28     // behave properly on chunks allocated by the Primary and Secondary.
29     void *p;
30     std::vector<ssize_t> sizes{1, 8, 16, 32, 1024, 32768,
31       1 << 16, 1 << 17, 1 << 20, 1 << 24};
32     for (size_t size : sizes) {
33       p = malloc(size);
34       assert(p);
35       assert(__sanitizer_get_ownership(p));
36       assert(__sanitizer_get_allocated_size(p) >= size);
37       free(p);
38     }
39   }
40   if (!strcmp(argv[1], "heap-size")) {
41     // Ensures that __sanitizer_get_heap_size can be called before any other
42     // allocator function.
43     assert(__sanitizer_get_heap_size() >= 0);
44   }
45 
46   return 0;
47 }
48