Home | History | Annotate | Download | only in tests
      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_utils.h"
     16 
     17 template<class T>
     18 __attribute__((noinline))
     19 static void ManyAccessFunc(T *x, size_t n_elements, size_t n_iter) {
     20   for (size_t iter = 0; iter < n_iter; iter++) {
     21     break_optimization(0);
     22     // hand unroll the loop to stress the reg alloc.
     23     for (size_t i = 0; i <= n_elements - 16; i += 16) {
     24       x[i + 0] = i;
     25       x[i + 1] = i;
     26       x[i + 2] = i;
     27       x[i + 3] = i;
     28       x[i + 4] = i;
     29       x[i + 5] = i;
     30       x[i + 6] = i;
     31       x[i + 7] = i;
     32       x[i + 8] = i;
     33       x[i + 9] = i;
     34       x[i + 10] = i;
     35       x[i + 11] = i;
     36       x[i + 12] = i;
     37       x[i + 13] = i;
     38       x[i + 14] = i;
     39       x[i + 15] = i;
     40     }
     41   }
     42 }
     43 
     44 TEST(AddressSanitizer, ManyAccessBenchmark) {
     45   size_t kLen = 1024;
     46   int *int_array = new int[kLen];
     47   ManyAccessFunc(int_array, kLen, 1 << 24);
     48   delete [] int_array;
     49 }
     50 
     51 // access 7 char elements in a 7 byte array (i.e. on the border).
     52 __attribute__((noinline))
     53 static void BorderAccessFunc(char *x, size_t n_iter) {
     54   for (size_t iter = 0; iter < n_iter; iter++) {
     55     break_optimization(x);
     56     x[0] = 0;
     57     x[1] = 0;
     58     x[2] = 0;
     59     x[3] = 0;
     60     x[4] = 0;
     61     x[5] = 0;
     62     x[6] = 0;
     63   }
     64 }
     65 
     66 TEST(AddressSanitizer, BorderAccessBenchmark) {
     67   char *char_7_array = new char[7];
     68   BorderAccessFunc(char_7_array, 1 << 30);
     69   delete [] char_7_array;
     70 }
     71 
     72 static void FunctionWithLargeStack() {
     73   int stack[1000];
     74   Ident(stack);
     75 }
     76 
     77 TEST(AddressSanitizer, FakeStackBenchmark) {
     78   for (int i = 0; i < 10000000; i++)
     79     Ident(&FunctionWithLargeStack)();
     80 }
     81 
     82 int main(int argc, char **argv) {
     83   testing::InitGoogleTest(&argc, argv);
     84   return RUN_ALL_TESTS();
     85 }
     86