1//===-- asan_benchmarks_test.cc ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Some benchmarks for the instrumented code.
13//===----------------------------------------------------------------------===//
14
15#include "asan_test_config.h"
16#include "asan_test_utils.h"
17
18template<class T>
19__attribute__((noinline))
20static void ManyAccessFunc(T *x, size_t n_elements, size_t n_iter) {
21  for (size_t iter = 0; iter < n_iter; iter++) {
22    break_optimization(0);
23    // hand unroll the loop to stress the reg alloc.
24    for (size_t i = 0; i <= n_elements - 16; i += 16) {
25      x[i + 0] = i;
26      x[i + 1] = i;
27      x[i + 2] = i;
28      x[i + 3] = i;
29      x[i + 4] = i;
30      x[i + 5] = i;
31      x[i + 6] = i;
32      x[i + 7] = i;
33      x[i + 8] = i;
34      x[i + 9] = i;
35      x[i + 10] = i;
36      x[i + 11] = i;
37      x[i + 12] = i;
38      x[i + 13] = i;
39      x[i + 14] = i;
40      x[i + 15] = i;
41    }
42  }
43}
44
45TEST(AddressSanitizer, ManyAccessBenchmark) {
46  size_t kLen = 1024;
47  int *int_array = new int[kLen];
48  ManyAccessFunc(int_array, kLen, 1 << 24);
49  delete [] int_array;
50}
51
52// access 7 char elements in a 7 byte array (i.e. on the border).
53__attribute__((noinline))
54static void BorderAccessFunc(char *x, size_t n_iter) {
55  for (size_t iter = 0; iter < n_iter; iter++) {
56    break_optimization(x);
57    x[0] = 0;
58    x[1] = 0;
59    x[2] = 0;
60    x[3] = 0;
61    x[4] = 0;
62    x[5] = 0;
63    x[6] = 0;
64  }
65}
66
67TEST(AddressSanitizer, BorderAccessBenchmark) {
68  char *char_7_array = new char[7];
69  BorderAccessFunc(char_7_array, 1 << 30);
70  delete [] char_7_array;
71}
72
73static void FunctionWithLargeStack() {
74  int stack[1000];
75  Ident(stack);
76}
77
78TEST(AddressSanitizer, FakeStackBenchmark) {
79  for (int i = 0; i < 10000000; i++)
80    Ident(&FunctionWithLargeStack)();
81}
82
83int main(int argc, char **argv) {
84  testing::InitGoogleTest(&argc, argv);
85  return RUN_ALL_TESTS();
86}
87