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