blob: 8655dbcdfd146c8d29b96b705d6ff751855e7572 [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
Primiano Tucci919ca1e2019-08-21 20:26:58 +020024#include "test/gtest_and_gmock.h"
Primiano Tucci0825bc82017-09-28 18:50:23 +010025
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
Primiano Tucci0825bc82017-09-28 18:50:23 +010045#if defined(MEMORY_SANITIZER)
46TEST(SanitizerTests, MSAN_UninitializedMemory) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000047 EXPECT_DEATH(
48 {
49 std::unique_ptr<int> mem(new int[10]);
50 volatile int* x = reinterpret_cast<volatile int*>(mem.get());
51 if (x[rand() % 10] == 42)
52 printf("\n");
53 abort();
54 },
55 "MemorySanitizer:.*use-of-uninitialized-value");
Primiano Tucci0825bc82017-09-28 18:50:23 +010056}
57#endif
58
Sami Kyostilaabc57fb2019-09-23 15:55:14 +010059// b/141460117: Leak sanitizer tests don't work in debug builds.
60#if defined(LEAK_SANITIZER) && defined(NDEBUG)
Primiano Tucci0825bc82017-09-28 18:50:23 +010061TEST(SanitizerTests, LSAN_LeakMalloc) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000062 EXPECT_DEATH(
63 {
64 void* alloc = malloc(16);
65 reinterpret_cast<volatile char*>(alloc)[0] = 1;
66 alloc = malloc(16);
67 reinterpret_cast<volatile char*>(alloc)[0] = 2;
68 free(alloc);
69 exit(0); // LSan runs on the atexit handler.
70 },
71 "LeakSanitizer:.*detected memory leaks");
Primiano Tucci0825bc82017-09-28 18:50:23 +010072}
73
74TEST(SanitizerTests, LSAN_LeakCppNew) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000075 EXPECT_DEATH(
76 {
77 std::unique_ptr<int> alloc(new int(1));
78 *reinterpret_cast<volatile char*>(alloc.get()) = 1;
79 alloc.release();
80 alloc.reset(new int(2));
81 *reinterpret_cast<volatile char*>(alloc.get()) = 2;
82 exit(0); // LSan runs on the atexit handler.
83 },
84 "LeakSanitizer:.*detected memory leaks");
Primiano Tucci0825bc82017-09-28 18:50:23 +010085}
Sami Kyostilaabc57fb2019-09-23 15:55:14 +010086#endif // LEAK_SANITIZER && defined(NDEBUG)
Primiano Tucci0825bc82017-09-28 18:50:23 +010087
88#if defined(UNDEFINED_SANITIZER)
89TEST(SanitizerTests, UBSAN_DivisionByZero) {
Primiano Tucci7278dea2017-10-31 11:50:32 +000090 EXPECT_DEATH(
91 {
92 volatile float div = 1;
93 float res = 3 / (div - 1);
94 ASSERT_GT(res, -1.0f); // just use |res| to make the compiler happy.
95 abort();
96 },
97 "error:.*division by zero");
Primiano Tucci0825bc82017-09-28 18:50:23 +010098}
99
100TEST(SanitizerTests, UBSAN_ShiftExponent) {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000101 EXPECT_DEATH(
102 {
103 volatile uint32_t n = 32;
104 volatile uint32_t shift = 31;
105 uint64_t res = n << (shift + 3);
106 ASSERT_NE(1u, res); // just use |res| to make the compiler happy.
107 abort();
108 },
109 "error:.*shift exponent");
Primiano Tucci0825bc82017-09-28 18:50:23 +0100110}
111#endif // UNDEFINED_SANITIZER
112
Primiano Tucci7278dea2017-10-31 11:50:32 +0000113#if !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) && \
114 !defined(MEMORY_SANITIZER) && !defined(LEAK_SANITIZER) && \
115 !defined(UNDEFINED_SANITIZER)
116TEST(SanitizerTests, NoSanitizersConfigured) {
117 printf("No sanitizers configured!\n");
118}
119#endif
120
Primiano Tucci0825bc82017-09-28 18:50:23 +0100121} // namespace
122} // namespace perfetto