blob: 5f61f2f023a19d266047cb70dde433d275fbb4d7 [file] [log] [blame]
Primiano Tucci0825bc82017-09-28 18:50:23 +01001/*
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
26namespace perfetto {
27namespace {
28
29#if defined(ADDRESS_SANITIZER)
30TEST(SanitizerTests, ASAN_UserAfterFree) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000031 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +010042}
43#endif // ADDRESS_SANITIZER
44
45#if defined(THREAD_SANITIZER)
46TEST(SanitizerTests, TSAN_ThreadDataRace) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000047 EXPECT_DEATH(
48 {
49 pthread_t thread;
Primiano Tucci7278dea2017-10-31 11:50:32 +000050 volatile int race_var = 0;
51 auto thread_main = [](void* race_var_ptr) -> void* {
Primiano Tucci931284e2017-12-11 09:23:53 +000052 (*reinterpret_cast<volatile int*>(race_var_ptr))++;
Primiano Tucci7278dea2017-10-31 11:50:32 +000053 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));
Primiano Tucci931284e2017-12-11 09:23:53 +000058 race_var--;
Primiano Tucci7278dea2017-10-31 11:50:32 +000059 ASSERT_EQ(0, pthread_join(thread, nullptr));
60 abort();
61 },
62 "ThreadSanitizer:.*data race");
Primiano Tucci0825bc82017-09-28 18:50:23 +010063}
64#endif // THREAD_SANITIZER
65
66#if defined(MEMORY_SANITIZER)
67TEST(SanitizerTests, MSAN_UninitializedMemory) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000068 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +010077}
78#endif
79
80#if defined(LEAK_SANITIZER)
81TEST(SanitizerTests, LSAN_LeakMalloc) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000082 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +010092}
93
94TEST(SanitizerTests, LSAN_LeakCppNew) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000095 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +0100105}
106#endif // LEAK_SANITIZER
107
108#if defined(UNDEFINED_SANITIZER)
109TEST(SanitizerTests, UBSAN_DivisionByZero) {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000110 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +0100118}
119
120TEST(SanitizerTests, UBSAN_ShiftExponent) {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000121 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");
Primiano Tucci0825bc82017-09-28 18:50:23 +0100130}
131#endif // UNDEFINED_SANITIZER
132
Primiano Tucci7278dea2017-10-31 11:50:32 +0000133#if !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) && \
134 !defined(MEMORY_SANITIZER) && !defined(LEAK_SANITIZER) && \
135 !defined(UNDEFINED_SANITIZER)
136TEST(SanitizerTests, NoSanitizersConfigured) {
137 printf("No sanitizers configured!\n");
138}
139#endif
140
Primiano Tucci0825bc82017-09-28 18:50:23 +0100141} // namespace
142} // namespace perfetto