blob: 249ec71946a568bc3f20f289c4135008d1b7ca11 [file] [log] [blame]
[email protected]f49ecd8f2011-05-10 18:03:341// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]0716cba2009-12-17 12:37:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]c2a18492011-10-05 13:22:504//
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.
[email protected]0716cba2009-12-17 12:37:588
[email protected]f49ecd8f2011-05-10 18:03:349#include "base/atomicops.h"
[email protected]0716cba2009-12-17 12:37:5810#include "base/message_loop.h"
[email protected]ee857512010-05-14 08:24:4211#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread.h"
[email protected]0716cba2009-12-17 12:37:5813#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]ce072a72010-12-31 20:02:1615namespace base {
16
[email protected]0716cba2009-12-17 12:37:5817namespace {
18
[email protected]f49ecd8f2011-05-10 18:03:3419const base::subtle::Atomic32 kMagicValue = 42;
[email protected]0716cba2009-12-17 12:37:5820
[email protected]c2a18492011-10-05 13:22:5021// 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)
25#else
26#define HARMFUL_ACCESS(action,error_regexp) \
27do { if (RunningOnValgrind()) { action; } } while (0)
28#endif
29
[email protected]adf7d802010-09-23 09:12:3730void ReadUninitializedValue(char *ptr) {
[email protected]83a13b22011-09-18 16:39:1231 // 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) {
[email protected]adf7d802010-09-23 09:12:3735 (*ptr)++;
36 } else {
37 (*ptr)--;
38 }
39}
40
[email protected]9ded9912010-03-26 12:54:4441void ReadValueOutOfArrayBoundsLeft(char *ptr) {
[email protected]45b9eba2010-10-18 23:57:4942 char c = ptr[-2];
43 VLOG(1) << "Reading a byte out of bounds: " << c;
[email protected]9ded9912010-03-26 12:54:4444}
45
46void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) {
[email protected]45b9eba2010-10-18 23:57:4947 char c = ptr[size + 1];
48 VLOG(1) << "Reading a byte out of bounds: " << c;
[email protected]9ded9912010-03-26 12:54:4449}
50
51// This is harmless if you run it under Valgrind thanks to redzones.
52void WriteValueOutOfArrayBoundsLeft(char *ptr) {
[email protected]f49ecd8f2011-05-10 18:03:3453 ptr[-1] = kMagicValue;
[email protected]9ded9912010-03-26 12:54:4454}
55
56// This is harmless if you run it under Valgrind thanks to redzones.
57void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) {
[email protected]f49ecd8f2011-05-10 18:03:3458 ptr[size] = kMagicValue;
[email protected]9ded9912010-03-26 12:54:4459}
60
61void MakeSomeErrors(char *ptr, size_t size) {
[email protected]adf7d802010-09-23 09:12:3762 ReadUninitializedValue(ptr);
[email protected]c2a18492011-10-05 13:22:5063 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");
[email protected]9ded9912010-03-26 12:54:4471}
72
[email protected]66d051bb2010-10-14 08:25:5473} // namespace
74
75// A memory leak detector should report an error in this test.
76TEST(ToolsSanityTest, MemoryLeak) {
77 int *leak = new int[256]; // Leak some memory intentionally.
78 leak[4] = 1; // Make sure the allocated memory is used.
79}
80
[email protected]9ded9912010-03-26 12:54:4481TEST(ToolsSanityTest, AccessesToNewMemory) {
[email protected]9ded9912010-03-26 12:54:4482 char *foo = new char[10];
83 MakeSomeErrors(foo, 10);
84 delete [] foo;
[email protected]c2a18492011-10-05 13:22:5085 // Use after delete.
86 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
[email protected]9ded9912010-03-26 12:54:4487}
88
89TEST(ToolsSanityTest, AccessesToMallocMemory) {
[email protected]9ded9912010-03-26 12:54:4490 char *foo = reinterpret_cast<char*>(malloc(10));
91 MakeSomeErrors(foo, 10);
92 free(foo);
[email protected]c2a18492011-10-05 13:22:5093 // Use after free.
94 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
[email protected]9ded9912010-03-26 12:54:4495}
96
97TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) {
[email protected]c2a18492011-10-05 13:22:5098#ifndef ADDRESS_SANITIZER
99 // This test may corrupt memory if not run under Valgrind or compiled with
100 // AddressSanitizer.
[email protected]9ded9912010-03-26 12:54:44101 if (!RunningOnValgrind())
102 return;
[email protected]c2a18492011-10-05 13:22:50103#endif
[email protected]9ded9912010-03-26 12:54:44104
[email protected]014b0b062011-09-18 16:30:12105 // Without the |volatile|, clang optimizes away the next two lines.
106 int* volatile foo = new int[10];
[email protected]9ded9912010-03-26 12:54:44107 delete foo;
108}
109
110TEST(ToolsSanityTest, SingleElementDeletedWithBraces) {
[email protected]c2a18492011-10-05 13:22:50111#ifndef ADDRESS_SANITIZER
112 // This test may corrupt memory if not run under Valgrind or compiled with
113 // AddressSanitizer.
[email protected]9ded9912010-03-26 12:54:44114 if (!RunningOnValgrind())
115 return;
[email protected]c2a18492011-10-05 13:22:50116#endif
[email protected]9ded9912010-03-26 12:54:44117
[email protected]014b0b062011-09-18 16:30:12118 // Without the |volatile|, clang optimizes away the next two lines.
119 int* volatile foo = new int;
[email protected]928ca1d2011-09-30 18:52:04120 (void) foo;
[email protected]9ded9912010-03-26 12:54:44121 delete [] foo;
122}
123
[email protected]6fcbc62f2011-10-12 17:18:24124#ifdef ADDRESS_SANITIZER
125TEST(ToolsSanityTest, DISABLED_AddressSanitizerCrashTest) {
126 // Intentionally crash to make sure AddressSanitizer is running.
127 // This test should not be ran on bots.
128 int* volatile zero = NULL;
129 *zero = 0;
130}
131#endif
[email protected]f49ecd8f2011-05-10 18:03:34132
133namespace {
134
135// We use caps here just to ensure that the method name doesn't interfere with
136// the wildcarded suppressions.
137class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
138 public:
139 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
140 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
141 void ThreadMain() {
142 *value_ = true;
143
144 // Sleep for a few milliseconds so the two threads are more likely to live
145 // simultaneously. Otherwise we may miss the report due to mutex
146 // lock/unlock's inside thread creation code in pure-happens-before mode...
147 PlatformThread::Sleep(100);
148 }
149 private:
150 bool *value_;
151};
152
153class ReleaseStoreThread : public PlatformThread::Delegate {
154 public:
155 explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {}
156 ~ReleaseStoreThread() {}
157 void ThreadMain() {
158 base::subtle::Release_Store(value_, kMagicValue);
159
160 // Sleep for a few milliseconds so the two threads are more likely to live
161 // simultaneously. Otherwise we may miss the report due to mutex
162 // lock/unlock's inside thread creation code in pure-happens-before mode...
163 PlatformThread::Sleep(100);
164 }
165 private:
166 base::subtle::Atomic32 *value_;
167};
168
169class AcquireLoadThread : public PlatformThread::Delegate {
170 public:
171 explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {}
172 ~AcquireLoadThread() {}
173 void ThreadMain() {
174 // Wait for the other thread to make Release_Store
175 PlatformThread::Sleep(100);
176 base::subtle::Acquire_Load(value_);
177 }
178 private:
179 base::subtle::Atomic32 *value_;
180};
181
182void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) {
183 PlatformThreadHandle a;
184 PlatformThreadHandle b;
185 PlatformThread::Create(0, d1, &a);
186 PlatformThread::Create(0, d2, &b);
187 PlatformThread::Join(a);
188 PlatformThread::Join(b);
189}
190
191} // namespace
192
[email protected]0716cba2009-12-17 12:37:58193// A data race detector should report an error in this test.
[email protected]456a3c5e2010-06-11 12:50:22194TEST(ToolsSanityTest, DataRace) {
[email protected]0716cba2009-12-17 12:37:58195 bool shared = false;
[email protected]f49ecd8f2011-05-10 18:03:34196 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
197 RunInParallel(&thread1, &thread2);
[email protected]0716cba2009-12-17 12:37:58198 EXPECT_TRUE(shared);
[email protected]f49ecd8f2011-05-10 18:03:34199}
200
201TEST(ToolsSanityTest, AnnotateBenignRace) {
202 bool shared = false;
203 ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up");
204 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
205 RunInParallel(&thread1, &thread2);
206 EXPECT_TRUE(shared);
207}
208
209TEST(ToolsSanityTest, AtomicsAreIgnored) {
210 base::subtle::Atomic32 shared = 0;
211 ReleaseStoreThread thread1(&shared);
212 AcquireLoadThread thread2(&shared);
213 RunInParallel(&thread1, &thread2);
214 EXPECT_EQ(kMagicValue, shared);
[email protected]0716cba2009-12-17 12:37:58215}
[email protected]ce072a72010-12-31 20:02:16216
217} // namespace base