[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 4 | // |
| 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] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 8 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 9 | #include "base/atomicops.h" |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 10 | #include "base/message_loop.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 11 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 15 | namespace base { |
| 16 | |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 17 | namespace { |
| 18 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 19 | const base::subtle::Atomic32 kMagicValue = 42; |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 20 | |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 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) |
| 25 | #else |
| 26 | #define HARMFUL_ACCESS(action,error_regexp) \ |
| 27 | do { if (RunningOnValgrind()) { action; } } while (0) |
| 28 | #endif |
| 29 | |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 30 | void ReadUninitializedValue(char *ptr) { |
[email protected] | 83a13b2 | 2011-09-18 16:39:12 | [diff] [blame] | 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) { |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 35 | (*ptr)++; |
| 36 | } else { |
| 37 | (*ptr)--; |
| 38 | } |
| 39 | } |
| 40 | |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 41 | void ReadValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 42 | char c = ptr[-2]; |
| 43 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 47 | char c = ptr[size + 1]; |
| 48 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 52 | void WriteValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 53 | ptr[-1] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 57 | void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 58 | ptr[size] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void MakeSomeErrors(char *ptr, size_t size) { |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 62 | ReadUninitializedValue(ptr); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 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"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | 66d051bb | 2010-10-14 08:25:54 | [diff] [blame] | 73 | } // namespace |
| 74 | |
| 75 | // A memory leak detector should report an error in this test. |
| 76 | TEST(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] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 81 | TEST(ToolsSanityTest, AccessesToNewMemory) { |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 82 | char *foo = new char[10]; |
| 83 | MakeSomeErrors(foo, 10); |
| 84 | delete [] foo; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 85 | // Use after delete. |
| 86 | HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST(ToolsSanityTest, AccessesToMallocMemory) { |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 90 | char *foo = reinterpret_cast<char*>(malloc(10)); |
| 91 | MakeSomeErrors(foo, 10); |
| 92 | free(foo); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 93 | // Use after free. |
| 94 | HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) { |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 98 | #ifndef ADDRESS_SANITIZER |
| 99 | // This test may corrupt memory if not run under Valgrind or compiled with |
| 100 | // AddressSanitizer. |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 101 | if (!RunningOnValgrind()) |
| 102 | return; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 103 | #endif |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 104 | |
[email protected] | 014b0b06 | 2011-09-18 16:30:12 | [diff] [blame] | 105 | // Without the |volatile|, clang optimizes away the next two lines. |
| 106 | int* volatile foo = new int[10]; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 107 | delete foo; |
| 108 | } |
| 109 | |
| 110 | TEST(ToolsSanityTest, SingleElementDeletedWithBraces) { |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 111 | #ifndef ADDRESS_SANITIZER |
| 112 | // This test may corrupt memory if not run under Valgrind or compiled with |
| 113 | // AddressSanitizer. |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 114 | if (!RunningOnValgrind()) |
| 115 | return; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 116 | #endif |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 117 | |
[email protected] | 014b0b06 | 2011-09-18 16:30:12 | [diff] [blame] | 118 | // Without the |volatile|, clang optimizes away the next two lines. |
| 119 | int* volatile foo = new int; |
[email protected] | 928ca1d | 2011-09-30 18:52:04 | [diff] [blame] | 120 | (void) foo; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 121 | delete [] foo; |
| 122 | } |
| 123 | |
[email protected] | 6fcbc62f | 2011-10-12 17:18:24 | [diff] [blame^] | 124 | #ifdef ADDRESS_SANITIZER |
| 125 | TEST(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] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 132 | |
| 133 | namespace { |
| 134 | |
| 135 | // We use caps here just to ensure that the method name doesn't interfere with |
| 136 | // the wildcarded suppressions. |
| 137 | class 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 | |
| 153 | class 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 | |
| 169 | class 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 | |
| 182 | void 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] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 193 | // A data race detector should report an error in this test. |
[email protected] | 456a3c5e | 2010-06-11 12:50:22 | [diff] [blame] | 194 | TEST(ToolsSanityTest, DataRace) { |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 195 | bool shared = false; |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 196 | TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); |
| 197 | RunInParallel(&thread1, &thread2); |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 198 | EXPECT_TRUE(shared); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TEST(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 | |
| 209 | TEST(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] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 215 | } |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 216 | |
| 217 | } // namespace base |