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.
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 #if defined(ADDRESS_SANITIZER)
25 // EXPECT_DEATH is not supported on IOS.
26 #define HARMFUL_ACCESS(action,error_regexp) do { action; } while (0)
28 #define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp)
31 #define HARMFUL_ACCESS(action,error_regexp) \
32 do { if (RunningOnValgrind()) { action; } } while (0)
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'`.
46 void ReadValueOutOfArrayBoundsLeft(char *ptr
) {
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");
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
96 #define MAYBE_AccessesToNewMemory AccessesToNewMemory
97 #define MAYBE_AccessesToMallocMemory AccessesToMallocMemory
98 #define MAYBE_ArrayDeletedWithoutBraces ArrayDeletedWithoutBraces
99 #define MAYBE_SingleElementDeletedWithBraces SingleElementDeletedWithBraces
101 TEST(ToolsSanityTest
, MAYBE_AccessesToNewMemory
) {
102 char *foo
= new char[10];
103 MakeSomeErrors(foo
, 10);
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);
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
121 if (!RunningOnValgrind())
125 // Without the |volatile|, clang optimizes away the next two lines.
126 int* volatile foo
= new int[10];
130 TEST(ToolsSanityTest
, MAYBE_SingleElementDeletedWithBraces
) {
131 #if !defined(ADDRESS_SANITIZER)
132 // This test may corrupt memory if not run under Valgrind or compiled with
134 if (!RunningOnValgrind())
138 // Without the |volatile|, clang optimizes away the next two lines.
139 int* volatile foo
= new int;
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
;
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.
157 // Work around the OOB warning reported by Clang.
158 int* volatile access
= &array
[5];
163 int g_asan_test_global_array
[10];
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;
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
{
184 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value
) : value_(value
) {}
185 virtual ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
186 virtual void ThreadMain() OVERRIDE
{
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));
198 class ReleaseStoreThread
: public PlatformThread::Delegate
{
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));
211 base::subtle::Atomic32
*value_
;
214 class AcquireLoadThread
: public PlatformThread::Delegate
{
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_
);
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
);
238 // A data race detector should report an error in this test.
239 TEST(ToolsSanityTest
, DataRace
) {
241 TOOLS_SANITY_TEST_CONCURRENT_THREAD
thread1(&shared
), thread2(&shared
);
242 RunInParallel(&thread1
, &thread2
);
246 TEST(ToolsSanityTest
, AnnotateBenignRace
) {
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
);
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
);