Create a new-installs-only uniformity trial.
[chromium-blink-merge.git] / base / tools_sanity_unittest.cc
blobd0c57c86363f8da02bcb94faff0c8171659f939f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains intentional memory errors, some of which may lead to
6 // crashes if the test is ran without special memory testing tools. We use these
7 // errors to verify the sanity of the tools.
9 #include "base/atomicops.h"
10 #include "base/message_loop.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 #include "base/threading/thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
17 namespace {
19 const base::subtle::Atomic32 kMagicValue = 42;
21 // Helper for memory accesses that can potentially corrupt memory or cause a
22 // crash during a native run.
23 #if defined(ADDRESS_SANITIZER)
24 #if defined(OS_IOS)
25 // EXPECT_DEATH is not supported on IOS.
26 #define HARMFUL_ACCESS(action,error_regexp) do { action; } while (0)
27 #else
28 #define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp)
29 #endif // !OS_IOS
30 #else
31 #define HARMFUL_ACCESS(action,error_regexp) \
32 do { if (RunningOnValgrind()) { action; } } while (0)
33 #endif
35 void ReadUninitializedValue(char *ptr) {
36 // Comparison with 64 is to prevent clang from optimizing away the
37 // jump -- valgrind only catches jumps and conditional moves, but clang uses
38 // the borrow flag if the condition is just `*ptr == '\0'`.
39 if (*ptr == 64) {
40 (*ptr)++;
41 } else {
42 (*ptr)--;
46 void ReadValueOutOfArrayBoundsLeft(char *ptr) {
47 char c = ptr[-2];
48 VLOG(1) << "Reading a byte out of bounds: " << c;
51 void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) {
52 char c = ptr[size + 1];
53 VLOG(1) << "Reading a byte out of bounds: " << c;
56 // This is harmless if you run it under Valgrind thanks to redzones.
57 void WriteValueOutOfArrayBoundsLeft(char *ptr) {
58 ptr[-1] = kMagicValue;
61 // This is harmless if you run it under Valgrind thanks to redzones.
62 void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) {
63 ptr[size] = kMagicValue;
66 void MakeSomeErrors(char *ptr, size_t size) {
67 ReadUninitializedValue(ptr);
68 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsLeft(ptr),
69 "heap-buffer-overflow.*2 bytes to the left");
70 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsRight(ptr, size),
71 "heap-buffer-overflow.*1 bytes to the right");
72 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsLeft(ptr),
73 "heap-buffer-overflow.*1 bytes to the left");
74 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsRight(ptr, size),
75 "heap-buffer-overflow.*0 bytes to the right");
78 } // namespace
80 // A memory leak detector should report an error in this test.
81 TEST(ToolsSanityTest, MemoryLeak) {
82 // Without the |volatile|, clang optimizes away the next two lines.
83 int* volatile leak = new int[256]; // Leak some memory intentionally.
84 leak[4] = 1; // Make sure the allocated memory is used.
87 #if defined(ADDRESS_SANITIZER) && defined(OS_IOS)
88 // Because iOS doesn't support death tests, each of the following tests will
89 // crash the whole program under ASan.
90 #define MAYBE_AccessesToNewMemory DISABLED_AccessesToNewMemory
91 #define MAYBE_AccessesToMallocMemory DISABLED_AccessesToMallocMemory
92 #define MAYBE_ArrayDeletedWithoutBraces DISABLED_ArrayDeletedWithoutBraces
93 #define MAYBE_SingleElementDeletedWithBraces \
94 DISABLED_SingleElementDeletedWithBraces
95 #else
96 #define MAYBE_AccessesToNewMemory AccessesToNewMemory
97 #define MAYBE_AccessesToMallocMemory AccessesToMallocMemory
98 #define MAYBE_ArrayDeletedWithoutBraces ArrayDeletedWithoutBraces
99 #define MAYBE_SingleElementDeletedWithBraces SingleElementDeletedWithBraces
100 #endif
101 TEST(ToolsSanityTest, MAYBE_AccessesToNewMemory) {
102 char *foo = new char[10];
103 MakeSomeErrors(foo, 10);
104 delete [] foo;
105 // Use after delete.
106 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
109 TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) {
110 char *foo = reinterpret_cast<char*>(malloc(10));
111 MakeSomeErrors(foo, 10);
112 free(foo);
113 // Use after free.
114 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
117 TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) {
118 #if !defined(ADDRESS_SANITIZER)
119 // This test may corrupt memory if not run under Valgrind or compiled with
120 // AddressSanitizer.
121 if (!RunningOnValgrind())
122 return;
123 #endif
125 // Without the |volatile|, clang optimizes away the next two lines.
126 int* volatile foo = new int[10];
127 delete foo;
130 TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) {
131 #if !defined(ADDRESS_SANITIZER)
132 // This test may corrupt memory if not run under Valgrind or compiled with
133 // AddressSanitizer.
134 if (!RunningOnValgrind())
135 return;
136 #endif
138 // Without the |volatile|, clang optimizes away the next two lines.
139 int* volatile foo = new int;
140 (void) foo;
141 delete [] foo;
144 #if defined(ADDRESS_SANITIZER)
145 TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) {
146 // Intentionally crash to make sure AddressSanitizer is running.
147 // This test should not be ran on bots.
148 int* volatile zero = NULL;
149 *zero = 0;
152 TEST(ToolsSanityTest, DISABLED_AddressSanitizerLocalOOBCrashTest) {
153 // Intentionally crash to make sure AddressSanitizer is instrumenting
154 // the local variables.
155 // This test should not be ran on bots.
156 int array[5];
157 // Work around the OOB warning reported by Clang.
158 int* volatile access = &array[5];
159 *access = 43;
162 namespace {
163 int g_asan_test_global_array[10];
164 } // namespace
166 TEST(ToolsSanityTest, DISABLED_AddressSanitizerGlobalOOBCrashTest) {
167 // Intentionally crash to make sure AddressSanitizer is instrumenting
168 // the global variables.
169 // This test should not be ran on bots.
171 // Work around the OOB warning reported by Clang.
172 int* volatile access = g_asan_test_global_array - 1;
173 *access = 43;
176 #endif
178 namespace {
180 // We use caps here just to ensure that the method name doesn't interfere with
181 // the wildcarded suppressions.
182 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
183 public:
184 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
185 virtual ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
186 virtual void ThreadMain() OVERRIDE {
187 *value_ = true;
189 // Sleep for a few milliseconds so the two threads are more likely to live
190 // simultaneously. Otherwise we may miss the report due to mutex
191 // lock/unlock's inside thread creation code in pure-happens-before mode...
192 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
194 private:
195 bool *value_;
198 class ReleaseStoreThread : public PlatformThread::Delegate {
199 public:
200 explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {}
201 virtual ~ReleaseStoreThread() {}
202 virtual void ThreadMain() OVERRIDE {
203 base::subtle::Release_Store(value_, kMagicValue);
205 // Sleep for a few milliseconds so the two threads are more likely to live
206 // simultaneously. Otherwise we may miss the report due to mutex
207 // lock/unlock's inside thread creation code in pure-happens-before mode...
208 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
210 private:
211 base::subtle::Atomic32 *value_;
214 class AcquireLoadThread : public PlatformThread::Delegate {
215 public:
216 explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {}
217 virtual ~AcquireLoadThread() {}
218 virtual void ThreadMain() OVERRIDE {
219 // Wait for the other thread to make Release_Store
220 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
221 base::subtle::Acquire_Load(value_);
223 private:
224 base::subtle::Atomic32 *value_;
227 void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) {
228 PlatformThreadHandle a;
229 PlatformThreadHandle b;
230 PlatformThread::Create(0, d1, &a);
231 PlatformThread::Create(0, d2, &b);
232 PlatformThread::Join(a);
233 PlatformThread::Join(b);
236 } // namespace
238 // A data race detector should report an error in this test.
239 TEST(ToolsSanityTest, DataRace) {
240 bool shared = false;
241 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
242 RunInParallel(&thread1, &thread2);
243 EXPECT_TRUE(shared);
246 TEST(ToolsSanityTest, AnnotateBenignRace) {
247 bool shared = false;
248 ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up");
249 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
250 RunInParallel(&thread1, &thread2);
251 EXPECT_TRUE(shared);
254 TEST(ToolsSanityTest, AtomicsAreIgnored) {
255 base::subtle::Atomic32 shared = 0;
256 ReleaseStoreThread thread1(&shared);
257 AcquireLoadThread thread2(&shared);
258 RunInParallel(&thread1, &thread2);
259 EXPECT_EQ(kMagicValue, shared);
262 } // namespace base