blob: 00eecb420cd05ed6aa1b13db97637a77bd360472 [file] [log] [blame]
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
glider@chromium.org0f9756f2009-12-17 21:37:58 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
glider@chromium.org71a6e932011-10-05 22:22:50 +09004//
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.
glider@chromium.org0f9756f2009-12-17 21:37:58 +09008
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +09009#include "base/atomicops.h"
sebmarchand@chromium.orgd117d7a2014-06-14 17:29:37 +090010#include "base/debug/asan_invalid_access.h"
11#include "base/debug/profiler.h"
avi@chromium.orga043a862013-07-18 17:12:40 +090012#include "base/message_loop/message_loop.h"
timurrrr@chromium.orgf39c3ff2010-05-14 17:24:42 +090013#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090014#include "base/threading/thread.h"
glider@chromium.org0f9756f2009-12-17 21:37:58 +090015#include "testing/gtest/include/gtest/gtest.h"
16
brettw@chromium.org61391822011-01-01 05:02:16 +090017namespace base {
18
glider@chromium.org0f9756f2009-12-17 21:37:58 +090019namespace {
20
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +090021const base::subtle::Atomic32 kMagicValue = 42;
glider@chromium.org0f9756f2009-12-17 21:37:58 +090022
glider@chromium.org71a6e932011-10-05 22:22:50 +090023// Helper for memory accesses that can potentially corrupt memory or cause a
24// crash during a native run.
chrisha@google.com87839ab2014-03-28 00:08:04 +090025#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
glider@chromium.orga3b3c432012-10-24 23:11:02 +090026#if defined(OS_IOS)
27// EXPECT_DEATH is not supported on IOS.
28#define HARMFUL_ACCESS(action,error_regexp) do { action; } while (0)
sebmarchand@chromium.orgd117d7a2014-06-14 17:29:37 +090029#elif defined(SYZYASAN)
30// We won't get a meaningful error message because we're not running under the
31// SyzyASan logger, but we can at least make sure that the error has been
32// generated in the SyzyASan runtime.
33#define HARMFUL_ACCESS(action,unused) \
34if (debug::IsBinaryInstrumented()) { EXPECT_DEATH(action, \
35 "AsanRuntime::OnError"); }
glider@chromium.orga3b3c432012-10-24 23:11:02 +090036#else
glider@chromium.org71a6e932011-10-05 22:22:50 +090037#define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp)
sebmarchand@chromium.orgd117d7a2014-06-14 17:29:37 +090038#endif // !OS_IOS && !SYZYASAN
glider@chromium.org71a6e932011-10-05 22:22:50 +090039#else
40#define HARMFUL_ACCESS(action,error_regexp) \
41do { if (RunningOnValgrind()) { action; } } while (0)
42#endif
43
earthdok@chromium.org5a5e4652014-01-16 14:31:41 +090044void DoReadUninitializedValue(char *ptr) {
eugenis@google.com7634fc52012-05-16 17:42:08 +090045 // Comparison with 64 is to prevent clang from optimizing away the
thakis@chromium.org987f8332011-09-19 01:39:12 +090046 // jump -- valgrind only catches jumps and conditional moves, but clang uses
47 // the borrow flag if the condition is just `*ptr == '\0'`.
eugenis@google.com7634fc52012-05-16 17:42:08 +090048 if (*ptr == 64) {
earthdok@chromium.org5a5e4652014-01-16 14:31:41 +090049 VLOG(1) << "Uninit condition is true";
timurrrr@chromium.org46315442010-09-23 18:12:37 +090050 } else {
earthdok@chromium.org5a5e4652014-01-16 14:31:41 +090051 VLOG(1) << "Uninit condition is false";
timurrrr@chromium.org46315442010-09-23 18:12:37 +090052 }
53}
54
earthdok@chromium.org5a5e4652014-01-16 14:31:41 +090055void ReadUninitializedValue(char *ptr) {
56#if defined(MEMORY_SANITIZER)
57 EXPECT_DEATH(DoReadUninitializedValue(ptr),
58 "use-of-uninitialized-value");
59#else
60 DoReadUninitializedValue(ptr);
61#endif
62}
63
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090064void ReadValueOutOfArrayBoundsLeft(char *ptr) {
pkasting@chromium.org4baea272010-10-19 08:57:49 +090065 char c = ptr[-2];
66 VLOG(1) << "Reading a byte out of bounds: " << c;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090067}
68
69void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) {
pkasting@chromium.org4baea272010-10-19 08:57:49 +090070 char c = ptr[size + 1];
71 VLOG(1) << "Reading a byte out of bounds: " << c;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090072}
73
74// This is harmless if you run it under Valgrind thanks to redzones.
75void WriteValueOutOfArrayBoundsLeft(char *ptr) {
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +090076 ptr[-1] = kMagicValue;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090077}
78
79// This is harmless if you run it under Valgrind thanks to redzones.
80void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) {
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +090081 ptr[size] = kMagicValue;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090082}
83
84void MakeSomeErrors(char *ptr, size_t size) {
timurrrr@chromium.org46315442010-09-23 18:12:37 +090085 ReadUninitializedValue(ptr);
mithro@mithis.com62c72272013-12-18 14:31:33 +090086
glider@chromium.org71a6e932011-10-05 22:22:50 +090087 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsLeft(ptr),
mithro@mithis.com62c72272013-12-18 14:31:33 +090088 "2 bytes to the left");
glider@chromium.org71a6e932011-10-05 22:22:50 +090089 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsRight(ptr, size),
mithro@mithis.com62c72272013-12-18 14:31:33 +090090 "1 bytes to the right");
glider@chromium.org71a6e932011-10-05 22:22:50 +090091 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsLeft(ptr),
mithro@mithis.com62c72272013-12-18 14:31:33 +090092 "1 bytes to the left");
glider@chromium.org71a6e932011-10-05 22:22:50 +090093 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsRight(ptr, size),
mithro@mithis.com62c72272013-12-18 14:31:33 +090094 "0 bytes to the right");
timurrrr@chromium.org4597f342010-03-26 21:54:44 +090095}
96
glider@chromium.orgb311fbc2010-10-14 17:25:54 +090097} // namespace
98
99// A memory leak detector should report an error in this test.
100TEST(ToolsSanityTest, MemoryLeak) {
thakis@chromium.org1a3c5d12011-11-01 20:08:09 +0900101 // Without the |volatile|, clang optimizes away the next two lines.
102 int* volatile leak = new int[256]; // Leak some memory intentionally.
glider@chromium.orgb311fbc2010-10-14 17:25:54 +0900103 leak[4] = 1; // Make sure the allocated memory is used.
104}
105
chrisha@chromium.org3f23a1a2014-03-29 02:35:10 +0900106#if (defined(ADDRESS_SANITIZER) && defined(OS_IOS)) || defined(SYZYASAN)
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900107// Because iOS doesn't support death tests, each of the following tests will
chrisha@chromium.org3f23a1a2014-03-29 02:35:10 +0900108// crash the whole program under Asan. On Windows Asan is based on SyzyAsan; the
109// error report mechanism is different than with Asan so these tests will fail.
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900110#define MAYBE_AccessesToNewMemory DISABLED_AccessesToNewMemory
111#define MAYBE_AccessesToMallocMemory DISABLED_AccessesToMallocMemory
glider@chromium.org738e89d2014-05-14 20:50:37 +0900112#else
113#define MAYBE_AccessesToNewMemory AccessesToNewMemory
114#define MAYBE_AccessesToMallocMemory AccessesToMallocMemory
tzik@chromium.orgf65e67e2014-07-18 11:40:40 +0900115#endif // (defined(ADDRESS_SANITIZER) && defined(OS_IOS)) || defined(SYZYASAN)
glider@chromium.orge43d0062014-05-15 01:04:30 +0900116
117// The following tests pass with Clang r170392, but not r172454, which
118// makes AddressSanitizer detect errors in them. We disable these tests under
119// AddressSanitizer until we fully switch to Clang r172454. After that the
120// tests should be put back under the (defined(OS_IOS) || defined(OS_WIN))
121// clause above.
122// See also http://crbug.com/172614.
123#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
glider@chromium.org738e89d2014-05-14 20:50:37 +0900124#define MAYBE_SingleElementDeletedWithBraces \
125 DISABLED_SingleElementDeletedWithBraces
glider@chromium.orge43d0062014-05-15 01:04:30 +0900126#define MAYBE_ArrayDeletedWithoutBraces DISABLED_ArrayDeletedWithoutBraces
tzik@chromium.orgf65e67e2014-07-18 11:40:40 +0900127#else
128#define MAYBE_ArrayDeletedWithoutBraces ArrayDeletedWithoutBraces
129#define MAYBE_SingleElementDeletedWithBraces SingleElementDeletedWithBraces
130#endif // defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
131
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900132TEST(ToolsSanityTest, MAYBE_AccessesToNewMemory) {
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900133 char *foo = new char[10];
134 MakeSomeErrors(foo, 10);
135 delete [] foo;
glider@chromium.org71a6e932011-10-05 22:22:50 +0900136 // Use after delete.
137 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900138}
139
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900140TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) {
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900141 char *foo = reinterpret_cast<char*>(malloc(10));
142 MakeSomeErrors(foo, 10);
143 free(foo);
glider@chromium.org71a6e932011-10-05 22:22:50 +0900144 // Use after free.
145 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900146}
147
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900148TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) {
chrisha@google.com87839ab2014-03-28 00:08:04 +0900149#if !defined(ADDRESS_SANITIZER) && !defined(SYZYASAN)
glider@chromium.org71a6e932011-10-05 22:22:50 +0900150 // This test may corrupt memory if not run under Valgrind or compiled with
151 // AddressSanitizer.
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900152 if (!RunningOnValgrind())
153 return;
glider@chromium.org71a6e932011-10-05 22:22:50 +0900154#endif
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900155
thakis@chromium.orgc3846f82011-09-19 01:30:12 +0900156 // Without the |volatile|, clang optimizes away the next two lines.
157 int* volatile foo = new int[10];
glider@chromium.orge43d0062014-05-15 01:04:30 +0900158 delete foo;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900159}
160
glider@chromium.orga3b3c432012-10-24 23:11:02 +0900161TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) {
162#if !defined(ADDRESS_SANITIZER)
glider@chromium.org71a6e932011-10-05 22:22:50 +0900163 // This test may corrupt memory if not run under Valgrind or compiled with
164 // AddressSanitizer.
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900165 if (!RunningOnValgrind())
166 return;
glider@chromium.org71a6e932011-10-05 22:22:50 +0900167#endif
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900168
thakis@chromium.orgc3846f82011-09-19 01:30:12 +0900169 // Without the |volatile|, clang optimizes away the next two lines.
170 int* volatile foo = new int;
pph34r@gmail.com80271292011-10-01 03:52:04 +0900171 (void) foo;
glider@chromium.orge43d0062014-05-15 01:04:30 +0900172 delete [] foo;
timurrrr@chromium.org4597f342010-03-26 21:54:44 +0900173}
174
chrisha@google.com87839ab2014-03-28 00:08:04 +0900175#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
sebmarchand@chromium.orgd117d7a2014-06-14 17:29:37 +0900176
glider@chromium.org4b5cd882011-11-08 18:28:49 +0900177TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) {
glider@chromium.org2fa52d02011-10-13 02:18:24 +0900178 // Intentionally crash to make sure AddressSanitizer is running.
179 // This test should not be ran on bots.
180 int* volatile zero = NULL;
181 *zero = 0;
182}
glider@chromium.org4b5cd882011-11-08 18:28:49 +0900183
184TEST(ToolsSanityTest, DISABLED_AddressSanitizerLocalOOBCrashTest) {
185 // Intentionally crash to make sure AddressSanitizer is instrumenting
186 // the local variables.
187 // This test should not be ran on bots.
188 int array[5];
189 // Work around the OOB warning reported by Clang.
190 int* volatile access = &array[5];
191 *access = 43;
192}
193
194namespace {
195int g_asan_test_global_array[10];
196} // namespace
197
198TEST(ToolsSanityTest, DISABLED_AddressSanitizerGlobalOOBCrashTest) {
199 // Intentionally crash to make sure AddressSanitizer is instrumenting
200 // the global variables.
201 // This test should not be ran on bots.
202
203 // Work around the OOB warning reported by Clang.
204 int* volatile access = g_asan_test_global_array - 1;
205 *access = 43;
206}
207
sebmarchand@chromium.orgd117d7a2014-06-14 17:29:37 +0900208TEST(ToolsSanityTest, AsanHeapOverflow) {
209 HARMFUL_ACCESS(debug::AsanHeapOverflow() ,"to the right");
210}
211
212TEST(ToolsSanityTest, AsanHeapUnderflow) {
213 HARMFUL_ACCESS(debug::AsanHeapUnderflow(), "to the left");
214}
215
216TEST(ToolsSanityTest, AsanHeapUseAfterFree) {
217 HARMFUL_ACCESS(debug::AsanHeapUseAfterFree(), "heap-use-after-free");
218}
219
220#if defined(SYZYASAN)
221TEST(ToolsSanityTest, AsanCorruptHeapBlock) {
222 HARMFUL_ACCESS(debug::AsanCorruptHeapBlock(), "");
223}
224
225TEST(ToolsSanityTest, AsanCorruptHeap) {
226 // This test will kill the process by raising an exception, there's no
227 // particular string to look for in the stack trace.
228 EXPECT_DEATH(debug::AsanCorruptHeap(), "");
229}
230#endif // SYZYASAN
231
232#endif // ADDRESS_SANITIZER || SYZYASAN
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900233
234namespace {
235
236// We use caps here just to ensure that the method name doesn't interfere with
237// the wildcarded suppressions.
238class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
239 public:
240 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +0900241 virtual ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
242 virtual void ThreadMain() OVERRIDE {
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900243 *value_ = true;
244
245 // Sleep for a few milliseconds so the two threads are more likely to live
246 // simultaneously. Otherwise we may miss the report due to mutex
247 // lock/unlock's inside thread creation code in pure-happens-before mode...
tedvessenes@gmail.comaaa63032012-01-01 07:53:51 +0900248 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900249 }
250 private:
251 bool *value_;
252};
253
254class ReleaseStoreThread : public PlatformThread::Delegate {
255 public:
256 explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {}
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +0900257 virtual ~ReleaseStoreThread() {}
258 virtual void ThreadMain() OVERRIDE {
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900259 base::subtle::Release_Store(value_, kMagicValue);
260
261 // Sleep for a few milliseconds so the two threads are more likely to live
262 // simultaneously. Otherwise we may miss the report due to mutex
263 // lock/unlock's inside thread creation code in pure-happens-before mode...
tedvessenes@gmail.comaaa63032012-01-01 07:53:51 +0900264 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900265 }
266 private:
267 base::subtle::Atomic32 *value_;
268};
269
270class AcquireLoadThread : public PlatformThread::Delegate {
271 public:
272 explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {}
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +0900273 virtual ~AcquireLoadThread() {}
274 virtual void ThreadMain() OVERRIDE {
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900275 // Wait for the other thread to make Release_Store
tedvessenes@gmail.comaaa63032012-01-01 07:53:51 +0900276 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900277 base::subtle::Acquire_Load(value_);
278 }
279 private:
280 base::subtle::Atomic32 *value_;
281};
282
283void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) {
284 PlatformThreadHandle a;
285 PlatformThreadHandle b;
286 PlatformThread::Create(0, d1, &a);
287 PlatformThread::Create(0, d2, &b);
288 PlatformThread::Join(a);
289 PlatformThread::Join(b);
290}
291
glider@chromium.orgc75e1372014-06-26 22:10:50 +0900292#if defined(THREAD_SANITIZER)
293void DataRace() {
glider@chromium.org7bb5ece2013-03-23 20:28:17 +0900294 bool *shared = new bool(false);
295 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(shared), thread2(shared);
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900296 RunInParallel(&thread1, &thread2);
glider@chromium.org7bb5ece2013-03-23 20:28:17 +0900297 EXPECT_TRUE(*shared);
298 delete shared;
glider@chromium.orgc75e1372014-06-26 22:10:50 +0900299 // We're in a death test - crash.
300 CHECK(0);
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900301}
glider@chromium.orgc75e1372014-06-26 22:10:50 +0900302#endif
303
304} // namespace
305
306#if defined(THREAD_SANITIZER)
307// A data race detector should report an error in this test.
308TEST(ToolsSanityTest, DataRace) {
309 // The suppression regexp must match that in base/debug/tsan_suppressions.cc.
310 EXPECT_DEATH(DataRace(), "1 race:base/tools_sanity_unittest.cc");
311}
312#endif
timurrrr@chromium.orgb1d5c022011-05-11 03:03:34 +0900313
314TEST(ToolsSanityTest, AnnotateBenignRace) {
315 bool shared = false;
316 ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up");
317 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
318 RunInParallel(&thread1, &thread2);
319 EXPECT_TRUE(shared);
320}
321
322TEST(ToolsSanityTest, AtomicsAreIgnored) {
323 base::subtle::Atomic32 shared = 0;
324 ReleaseStoreThread thread1(&shared);
325 AcquireLoadThread thread2(&shared);
326 RunInParallel(&thread1, &thread2);
327 EXPECT_EQ(kMagicValue, shared);
glider@chromium.org0f9756f2009-12-17 21:37:58 +0900328}
brettw@chromium.org61391822011-01-01 05:02:16 +0900329
330} // namespace base