1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Smoke tests to verify that clang sanitizers are actually working. 18 19 #include <pthread.h> 20 #include <stdint.h> 21 22 #include <memory> 23 24 #include "gtest/gtest.h" 25 26 namespace perfetto { 27 namespace { 28 29 #if defined(ADDRESS_SANITIZER) 30 TEST(SanitizerTests, ASAN_UserAfterFree) { 31 EXPECT_DEATH( 32 { 33 void* alloc = malloc(16); 34 volatile char* mem = reinterpret_cast<volatile char*>(alloc); 35 mem[0] = 1; 36 mem[15] = 1; 37 free(alloc); 38 mem[0] = 2; 39 abort(); 40 }, 41 "AddressSanitizer:.*heap-use-after-free"); 42 } 43 #endif // ADDRESS_SANITIZER 44 45 #if defined(THREAD_SANITIZER) 46 TEST(SanitizerTests, TSAN_ThreadDataRace) { 47 EXPECT_DEATH( 48 { 49 pthread_t thread; 50 volatile int race_var = 0; 51 auto thread_main = [](void* race_var_ptr) -> void* { 52 (*reinterpret_cast<volatile int*>(race_var_ptr))++; 53 return nullptr; 54 }; 55 void* arg = 56 const_cast<void*>(reinterpret_cast<volatile void*>(&race_var)); 57 ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_main, arg)); 58 race_var--; 59 ASSERT_EQ(0, pthread_join(thread, nullptr)); 60 abort(); 61 }, 62 "ThreadSanitizer:.*data race"); 63 } 64 #endif // THREAD_SANITIZER 65 66 #if defined(MEMORY_SANITIZER) 67 TEST(SanitizerTests, MSAN_UninitializedMemory) { 68 EXPECT_DEATH( 69 { 70 std::unique_ptr<int> mem(new int[10]); 71 volatile int* x = reinterpret_cast<volatile int*>(mem.get()); 72 if (x[rand() % 10] == 42) 73 printf("\n"); 74 abort(); 75 }, 76 "MemorySanitizer:.*use-of-uninitialized-value"); 77 } 78 #endif 79 80 #if defined(LEAK_SANITIZER) 81 TEST(SanitizerTests, LSAN_LeakMalloc) { 82 EXPECT_DEATH( 83 { 84 void* alloc = malloc(16); 85 reinterpret_cast<volatile char*>(alloc)[0] = 1; 86 alloc = malloc(16); 87 reinterpret_cast<volatile char*>(alloc)[0] = 2; 88 free(alloc); 89 exit(0); // LSan runs on the atexit handler. 90 }, 91 "LeakSanitizer:.*detected memory leaks"); 92 } 93 94 TEST(SanitizerTests, LSAN_LeakCppNew) { 95 EXPECT_DEATH( 96 { 97 std::unique_ptr<int> alloc(new int(1)); 98 *reinterpret_cast<volatile char*>(alloc.get()) = 1; 99 alloc.release(); 100 alloc.reset(new int(2)); 101 *reinterpret_cast<volatile char*>(alloc.get()) = 2; 102 exit(0); // LSan runs on the atexit handler. 103 }, 104 "LeakSanitizer:.*detected memory leaks"); 105 } 106 #endif // LEAK_SANITIZER 107 108 #if defined(UNDEFINED_SANITIZER) 109 TEST(SanitizerTests, UBSAN_DivisionByZero) { 110 EXPECT_DEATH( 111 { 112 volatile float div = 1; 113 float res = 3 / (div - 1); 114 ASSERT_GT(res, -1.0f); // just use |res| to make the compiler happy. 115 abort(); 116 }, 117 "error:.*division by zero"); 118 } 119 120 TEST(SanitizerTests, UBSAN_ShiftExponent) { 121 EXPECT_DEATH( 122 { 123 volatile uint32_t n = 32; 124 volatile uint32_t shift = 31; 125 uint64_t res = n << (shift + 3); 126 ASSERT_NE(1u, res); // just use |res| to make the compiler happy. 127 abort(); 128 }, 129 "error:.*shift exponent"); 130 } 131 #endif // UNDEFINED_SANITIZER 132 133 #if !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) && \ 134 !defined(MEMORY_SANITIZER) && !defined(LEAK_SANITIZER) && \ 135 !defined(UNDEFINED_SANITIZER) 136 TEST(SanitizerTests, NoSanitizersConfigured) { 137 printf("No sanitizers configured!\n"); 138 } 139 #endif 140 141 } // namespace 142 } // namespace perfetto 143