1 // Copyright (c) 2011 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.
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"
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 #ifdef ADDRESS_SANITIZER
24 #define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp)
26 #define HARMFUL_ACCESS(action,error_regexp) \
27 do { if (RunningOnValgrind()) { action; } } while (0)
30 void ReadUninitializedValue(char *ptr
) {
31 // The || in the conditional is to prevent clang from optimizing away the
32 // jump -- valgrind only catches jumps and conditional moves, but clang uses
33 // the borrow flag if the condition is just `*ptr == '\0'`.
34 if (*ptr
== '\0' || *ptr
== 64) {
41 void ReadValueOutOfArrayBoundsLeft(char *ptr
) {
43 VLOG(1) << "Reading a byte out of bounds: " << c
;
46 void ReadValueOutOfArrayBoundsRight(char *ptr
, size_t size
) {
47 char c
= ptr
[size
+ 1];
48 VLOG(1) << "Reading a byte out of bounds: " << c
;
51 // This is harmless if you run it under Valgrind thanks to redzones.
52 void WriteValueOutOfArrayBoundsLeft(char *ptr
) {
53 ptr
[-1] = kMagicValue
;
56 // This is harmless if you run it under Valgrind thanks to redzones.
57 void WriteValueOutOfArrayBoundsRight(char *ptr
, size_t size
) {
58 ptr
[size
] = kMagicValue
;
61 void MakeSomeErrors(char *ptr
, size_t size
) {
62 ReadUninitializedValue(ptr
);
63 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsLeft(ptr
),
64 "heap-buffer-overflow.*2 bytes to the left");
65 HARMFUL_ACCESS(ReadValueOutOfArrayBoundsRight(ptr
, size
),
66 "heap-buffer-overflow.*1 bytes to the right");
67 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsLeft(ptr
),
68 "heap-buffer-overflow.*1 bytes to the left");
69 HARMFUL_ACCESS(WriteValueOutOfArrayBoundsRight(ptr
, size
),
70 "heap-buffer-overflow.*0 bytes to the right");
75 // A memory leak detector should report an error in this test.
76 TEST(ToolsSanityTest
, MemoryLeak
) {
77 // Without the |volatile|, clang optimizes away the next two lines.
78 int* volatile leak
= new int[256]; // Leak some memory intentionally.
79 leak
[4] = 1; // Make sure the allocated memory is used.
82 TEST(ToolsSanityTest
, AccessesToNewMemory
) {
83 char *foo
= new char[10];
84 MakeSomeErrors(foo
, 10);
87 HARMFUL_ACCESS(foo
[5] = 0, "heap-use-after-free");
90 TEST(ToolsSanityTest
, AccessesToMallocMemory
) {
91 char *foo
= reinterpret_cast<char*>(malloc(10));
92 MakeSomeErrors(foo
, 10);
95 HARMFUL_ACCESS(foo
[5] = 0, "heap-use-after-free");
98 TEST(ToolsSanityTest
, ArrayDeletedWithoutBraces
) {
99 #ifndef ADDRESS_SANITIZER
100 // This test may corrupt memory if not run under Valgrind or compiled with
102 if (!RunningOnValgrind())
106 // Without the |volatile|, clang optimizes away the next two lines.
107 int* volatile foo
= new int[10];
111 TEST(ToolsSanityTest
, SingleElementDeletedWithBraces
) {
112 #ifndef ADDRESS_SANITIZER
113 // This test may corrupt memory if not run under Valgrind or compiled with
115 if (!RunningOnValgrind())
119 // Without the |volatile|, clang optimizes away the next two lines.
120 int* volatile foo
= new int;
125 #ifdef ADDRESS_SANITIZER
126 TEST(ToolsSanityTest
, DISABLED_AddressSanitizerNullDerefCrashTest
) {
127 // Intentionally crash to make sure AddressSanitizer is running.
128 // This test should not be ran on bots.
129 int* volatile zero
= NULL
;
133 TEST(ToolsSanityTest
, DISABLED_AddressSanitizerLocalOOBCrashTest
) {
134 // Intentionally crash to make sure AddressSanitizer is instrumenting
135 // the local variables.
136 // This test should not be ran on bots.
138 // Work around the OOB warning reported by Clang.
139 int* volatile access
= &array
[5];
144 int g_asan_test_global_array
[10];
147 TEST(ToolsSanityTest
, DISABLED_AddressSanitizerGlobalOOBCrashTest
) {
148 // Intentionally crash to make sure AddressSanitizer is instrumenting
149 // the global variables.
150 // This test should not be ran on bots.
152 // Work around the OOB warning reported by Clang.
153 int* volatile access
= g_asan_test_global_array
- 1;
161 // We use caps here just to ensure that the method name doesn't interfere with
162 // the wildcarded suppressions.
163 class TOOLS_SANITY_TEST_CONCURRENT_THREAD
: public PlatformThread::Delegate
{
165 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value
) : value_(value
) {}
166 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
170 // Sleep for a few milliseconds so the two threads are more likely to live
171 // simultaneously. Otherwise we may miss the report due to mutex
172 // lock/unlock's inside thread creation code in pure-happens-before mode...
173 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
179 class ReleaseStoreThread
: public PlatformThread::Delegate
{
181 explicit ReleaseStoreThread(base::subtle::Atomic32
*value
) : value_(value
) {}
182 ~ReleaseStoreThread() {}
184 base::subtle::Release_Store(value_
, kMagicValue
);
186 // Sleep for a few milliseconds so the two threads are more likely to live
187 // simultaneously. Otherwise we may miss the report due to mutex
188 // lock/unlock's inside thread creation code in pure-happens-before mode...
189 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
192 base::subtle::Atomic32
*value_
;
195 class AcquireLoadThread
: public PlatformThread::Delegate
{
197 explicit AcquireLoadThread(base::subtle::Atomic32
*value
) : value_(value
) {}
198 ~AcquireLoadThread() {}
200 // Wait for the other thread to make Release_Store
201 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
202 base::subtle::Acquire_Load(value_
);
205 base::subtle::Atomic32
*value_
;
208 void RunInParallel(PlatformThread::Delegate
*d1
, PlatformThread::Delegate
*d2
) {
209 PlatformThreadHandle a
;
210 PlatformThreadHandle b
;
211 PlatformThread::Create(0, d1
, &a
);
212 PlatformThread::Create(0, d2
, &b
);
213 PlatformThread::Join(a
);
214 PlatformThread::Join(b
);
219 // A data race detector should report an error in this test.
220 TEST(ToolsSanityTest
, DataRace
) {
222 TOOLS_SANITY_TEST_CONCURRENT_THREAD
thread1(&shared
), thread2(&shared
);
223 RunInParallel(&thread1
, &thread2
);
227 TEST(ToolsSanityTest
, AnnotateBenignRace
) {
229 ANNOTATE_BENIGN_RACE(&shared
, "Intentional race - make sure doesn't show up");
230 TOOLS_SANITY_TEST_CONCURRENT_THREAD
thread1(&shared
), thread2(&shared
);
231 RunInParallel(&thread1
, &thread2
);
235 TEST(ToolsSanityTest
, AtomicsAreIgnored
) {
236 base::subtle::Atomic32 shared
= 0;
237 ReleaseStoreThread
thread1(&shared
);
238 AcquireLoadThread
thread2(&shared
);
239 RunInParallel(&thread1
, &thread2
);
240 EXPECT_EQ(kMagicValue
, shared
);