summaryrefslogtreecommitdiff
path: root/test/scudo/interface.cpp
blob: 16523bfe344cc1922dc9d3de0d3c6bdf85d57a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// RUN: %clangxx_scudo %s -lstdc++ -o %t
// RUN: %run %t ownership          2>&1
// RUN: %run %t ownership-and-size 2>&1
// RUN: %run %t heap-size          2>&1

// Tests that the sanitizer interface functions behave appropriately.

#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <vector>

#include <sanitizer/allocator_interface.h>

int main(int argc, char **argv)
{
  assert(argc == 2);

  if (!strcmp(argv[1], "ownership")) {
    // Ensures that __sanitizer_get_ownership can be called before any other
    // allocator function, and that it behaves properly on a pointer not owned
    // by us.
    assert(!__sanitizer_get_ownership(argv));
  }
  if (!strcmp(argv[1], "ownership-and-size")) {
    // Tests that __sanitizer_get_ownership and __sanitizer_get_allocated_size
    // behave properly on chunks allocated by the Primary and Secondary.
    void *p;
    std::vector<ssize_t> sizes{1, 8, 16, 32, 1024, 32768,
      1 << 16, 1 << 17, 1 << 20, 1 << 24};
    for (size_t size : sizes) {
      p = malloc(size);
      assert(p);
      assert(__sanitizer_get_ownership(p));
      assert(__sanitizer_get_allocated_size(p) >= size);
      free(p);
    }
  }
  if (!strcmp(argv[1], "heap-size")) {
    // Ensures that __sanitizer_get_heap_size can be called before any other
    // allocator function.
    assert(__sanitizer_get_heap_size() >= 0);
  }

  return 0;
}