blob: 499ff88bedb0761d43d667a040519e0b8d410cfb [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 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) {
[email protected]6bfad0e2011-11-01 11:08:0977 // Without the |volatile|, clang optimizes away the next two lines.
78 int* volatile leak = new int[256]; // Leak some memory intentionally.
[email protected]66d051bb2010-10-14 08:25:5479 leak[4] = 1; // Make sure the allocated memory is used.
80}
81
[email protected]9ded9912010-03-26 12:54:4482TEST(ToolsSanityTest, AccessesToNewMemory) {
[email protected]9ded9912010-03-26 12:54:4483 char *foo = new char[10];
84 MakeSomeErrors(foo, 10);
85 delete [] foo;
[email protected]c2a18492011-10-05 13:22:5086 // Use after delete.
87 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
[email protected]9ded9912010-03-26 12:54:4488}
89
90TEST(ToolsSanityTest, AccessesToMallocMemory) {
[email protected]9ded9912010-03-26 12:54:4491 char *foo = reinterpret_cast<char*>(malloc(10));
92 MakeSomeErrors(foo, 10);
93 free(foo);
[email protected]c2a18492011-10-05 13:22:5094 // Use after free.
95 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
[email protected]9ded9912010-03-26 12:54:4496}
97
98TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) {
[email protected]c2a18492011-10-05 13:22:5099#ifndef ADDRESS_SANITIZER
100 // This test may corrupt memory if not run under Valgrind or compiled with
101 // AddressSanitizer.
[email protected]9ded9912010-03-26 12:54:44102 if (!RunningOnValgrind())
103 return;
[email protected]c2a18492011-10-05 13:22:50104#endif
[email protected]9ded9912010-03-26 12:54:44105
[email protected]014b0b062011-09-18 16:30:12106 // Without the |volatile|, clang optimizes away the next two lines.
107 int* volatile foo = new int[10];
[email protected]9ded9912010-03-26 12:54:44108 delete foo;
109}
110
111TEST(ToolsSanityTest, SingleElementDeletedWithBraces) {
[email protected]c2a18492011-10-05 13:22:50112#ifndef ADDRESS_SANITIZER
113 // This test may corrupt memory if not run under Valgrind or compiled with
114 // AddressSanitizer.
[email protected]9ded9912010-03-26 12:54:44115 if (!RunningOnValgrind())
116 return;
[email protected]c2a18492011-10-05 13:22:50117#endif
[email protected]9ded9912010-03-26 12:54:44118
[email protected]014b0b062011-09-18 16:30:12119 // Without the |volatile|, clang optimizes away the next two lines.
120 int* volatile foo = new int;
[email protected]928ca1d2011-09-30 18:52:04121 (void) foo;
[email protected]9ded9912010-03-26 12:54:44122 delete [] foo;
123}
124
[email protected]6fcbc62f2011-10-12 17:18:24125#ifdef ADDRESS_SANITIZER
[email protected]9b147ae2011-11-08 09:28:49126TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) {
[email protected]6fcbc62f2011-10-12 17:18:24127 // Intentionally crash to make sure AddressSanitizer is running.
128 // This test should not be ran on bots.
129 int* volatile zero = NULL;
130 *zero = 0;
131}
[email protected]9b147ae2011-11-08 09:28:49132
133TEST(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.
137 int array[5];
138 // Work around the OOB warning reported by Clang.
139 int* volatile access = &array[5];
140 *access = 43;
141}
142
143namespace {
144int g_asan_test_global_array[10];
145} // namespace
146
147TEST(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.
151
152 // Work around the OOB warning reported by Clang.
153 int* volatile access = g_asan_test_global_array - 1;
154 *access = 43;
155}
156
[email protected]6fcbc62f2011-10-12 17:18:24157#endif
[email protected]f49ecd8f2011-05-10 18:03:34158
159namespace {
160
161// We use caps here just to ensure that the method name doesn't interfere with
162// the wildcarded suppressions.
163class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
164 public:
165 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
[email protected]44106182012-04-06 03:53:02166 virtual ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
167 virtual void ThreadMain() OVERRIDE {
[email protected]f49ecd8f2011-05-10 18:03:34168 *value_ = true;
169
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...
[email protected]a1b75b942011-12-31 22:53:51173 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]f49ecd8f2011-05-10 18:03:34174 }
175 private:
176 bool *value_;
177};
178
179class ReleaseStoreThread : public PlatformThread::Delegate {
180 public:
181 explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {}
[email protected]44106182012-04-06 03:53:02182 virtual ~ReleaseStoreThread() {}
183 virtual void ThreadMain() OVERRIDE {
[email protected]f49ecd8f2011-05-10 18:03:34184 base::subtle::Release_Store(value_, kMagicValue);
185
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...
[email protected]a1b75b942011-12-31 22:53:51189 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]f49ecd8f2011-05-10 18:03:34190 }
191 private:
192 base::subtle::Atomic32 *value_;
193};
194
195class AcquireLoadThread : public PlatformThread::Delegate {
196 public:
197 explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {}
[email protected]44106182012-04-06 03:53:02198 virtual ~AcquireLoadThread() {}
199 virtual void ThreadMain() OVERRIDE {
[email protected]f49ecd8f2011-05-10 18:03:34200 // Wait for the other thread to make Release_Store
[email protected]a1b75b942011-12-31 22:53:51201 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
[email protected]f49ecd8f2011-05-10 18:03:34202 base::subtle::Acquire_Load(value_);
203 }
204 private:
205 base::subtle::Atomic32 *value_;
206};
207
208void 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);
215}
216
217} // namespace
218
[email protected]0716cba2009-12-17 12:37:58219// A data race detector should report an error in this test.
[email protected]456a3c5e2010-06-11 12:50:22220TEST(ToolsSanityTest, DataRace) {
[email protected]0716cba2009-12-17 12:37:58221 bool shared = false;
[email protected]f49ecd8f2011-05-10 18:03:34222 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
223 RunInParallel(&thread1, &thread2);
[email protected]0716cba2009-12-17 12:37:58224 EXPECT_TRUE(shared);
[email protected]f49ecd8f2011-05-10 18:03:34225}
226
227TEST(ToolsSanityTest, AnnotateBenignRace) {
228 bool shared = false;
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);
232 EXPECT_TRUE(shared);
233}
234
235TEST(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);
[email protected]0716cba2009-12-17 12:37:58241}
[email protected]ce072a72010-12-31 20:02:16242
243} // namespace base