[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. |
| 4 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 5 | #include "base/atomicops.h" |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 6 | #include "base/message_loop.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 7 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 8 | #include "base/threading/thread.h" |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 11 | namespace base { |
| 12 | |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 13 | namespace { |
| 14 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 15 | const base::subtle::Atomic32 kMagicValue = 42; |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 16 | |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 17 | void ReadUninitializedValue(char *ptr) { |
[email protected] | d0d0152e | 2010-09-23 09:50:47 | [diff] [blame] | 18 | if (*ptr == '\0') { |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 19 | (*ptr)++; |
| 20 | } else { |
| 21 | (*ptr)--; |
| 22 | } |
| 23 | } |
| 24 | |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 25 | void ReadValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 26 | char c = ptr[-2]; |
| 27 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 31 | char c = ptr[size + 1]; |
| 32 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 36 | void WriteValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 37 | ptr[-1] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 41 | void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 42 | ptr[size] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void MakeSomeErrors(char *ptr, size_t size) { |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 46 | ReadUninitializedValue(ptr); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 47 | ReadValueOutOfArrayBoundsLeft(ptr); |
| 48 | ReadValueOutOfArrayBoundsRight(ptr, size); |
| 49 | WriteValueOutOfArrayBoundsLeft(ptr); |
| 50 | WriteValueOutOfArrayBoundsRight(ptr, size); |
| 51 | } |
| 52 | |
[email protected] | 66d051bb | 2010-10-14 08:25:54 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | // A memory leak detector should report an error in this test. |
| 56 | TEST(ToolsSanityTest, MemoryLeak) { |
| 57 | int *leak = new int[256]; // Leak some memory intentionally. |
| 58 | leak[4] = 1; // Make sure the allocated memory is used. |
| 59 | } |
| 60 | |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 61 | TEST(ToolsSanityTest, AccessesToNewMemory) { |
| 62 | // This test may corrupt memory if not run under Valgrind. |
| 63 | if (!RunningOnValgrind()) |
| 64 | return; |
| 65 | |
| 66 | char *foo = new char[10]; |
| 67 | MakeSomeErrors(foo, 10); |
| 68 | delete [] foo; |
| 69 | foo[5] = 0; // Use after delete. This won't break anything under Valgrind. |
| 70 | } |
| 71 | |
| 72 | TEST(ToolsSanityTest, AccessesToMallocMemory) { |
| 73 | // This test may corrupt memory if not run under Valgrind. |
| 74 | if (!RunningOnValgrind()) |
| 75 | return; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 76 | char *foo = reinterpret_cast<char*>(malloc(10)); |
| 77 | MakeSomeErrors(foo, 10); |
| 78 | free(foo); |
| 79 | foo[5] = 0; // Use after free. This won't break anything under Valgrind. |
| 80 | } |
| 81 | |
| 82 | TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) { |
| 83 | // This test may corrupt memory if not run under Valgrind. |
| 84 | if (!RunningOnValgrind()) |
| 85 | return; |
| 86 | |
| 87 | int *foo = new int[10]; |
| 88 | delete foo; |
| 89 | } |
| 90 | |
| 91 | TEST(ToolsSanityTest, SingleElementDeletedWithBraces) { |
| 92 | // This test may corrupt memory if not run under Valgrind. |
| 93 | if (!RunningOnValgrind()) |
| 94 | return; |
| 95 | |
| 96 | int *foo = new int; |
| 97 | delete [] foo; |
| 98 | } |
| 99 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 100 | |
| 101 | namespace { |
| 102 | |
| 103 | // We use caps here just to ensure that the method name doesn't interfere with |
| 104 | // the wildcarded suppressions. |
| 105 | class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { |
| 106 | public: |
| 107 | explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} |
| 108 | ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {} |
| 109 | void ThreadMain() { |
| 110 | *value_ = true; |
| 111 | |
| 112 | // Sleep for a few milliseconds so the two threads are more likely to live |
| 113 | // simultaneously. Otherwise we may miss the report due to mutex |
| 114 | // lock/unlock's inside thread creation code in pure-happens-before mode... |
| 115 | PlatformThread::Sleep(100); |
| 116 | } |
| 117 | private: |
| 118 | bool *value_; |
| 119 | }; |
| 120 | |
| 121 | class ReleaseStoreThread : public PlatformThread::Delegate { |
| 122 | public: |
| 123 | explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {} |
| 124 | ~ReleaseStoreThread() {} |
| 125 | void ThreadMain() { |
| 126 | base::subtle::Release_Store(value_, kMagicValue); |
| 127 | |
| 128 | // Sleep for a few milliseconds so the two threads are more likely to live |
| 129 | // simultaneously. Otherwise we may miss the report due to mutex |
| 130 | // lock/unlock's inside thread creation code in pure-happens-before mode... |
| 131 | PlatformThread::Sleep(100); |
| 132 | } |
| 133 | private: |
| 134 | base::subtle::Atomic32 *value_; |
| 135 | }; |
| 136 | |
| 137 | class AcquireLoadThread : public PlatformThread::Delegate { |
| 138 | public: |
| 139 | explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {} |
| 140 | ~AcquireLoadThread() {} |
| 141 | void ThreadMain() { |
| 142 | // Wait for the other thread to make Release_Store |
| 143 | PlatformThread::Sleep(100); |
| 144 | base::subtle::Acquire_Load(value_); |
| 145 | } |
| 146 | private: |
| 147 | base::subtle::Atomic32 *value_; |
| 148 | }; |
| 149 | |
| 150 | void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) { |
| 151 | PlatformThreadHandle a; |
| 152 | PlatformThreadHandle b; |
| 153 | PlatformThread::Create(0, d1, &a); |
| 154 | PlatformThread::Create(0, d2, &b); |
| 155 | PlatformThread::Join(a); |
| 156 | PlatformThread::Join(b); |
| 157 | } |
| 158 | |
| 159 | } // namespace |
| 160 | |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 161 | // A data race detector should report an error in this test. |
[email protected] | 456a3c5e | 2010-06-11 12:50:22 | [diff] [blame] | 162 | TEST(ToolsSanityTest, DataRace) { |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 163 | bool shared = false; |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 164 | TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); |
| 165 | RunInParallel(&thread1, &thread2); |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 166 | EXPECT_TRUE(shared); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TEST(ToolsSanityTest, AnnotateBenignRace) { |
| 170 | bool shared = false; |
| 171 | ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up"); |
| 172 | TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); |
| 173 | RunInParallel(&thread1, &thread2); |
| 174 | EXPECT_TRUE(shared); |
| 175 | } |
| 176 | |
| 177 | TEST(ToolsSanityTest, AtomicsAreIgnored) { |
| 178 | base::subtle::Atomic32 shared = 0; |
| 179 | ReleaseStoreThread thread1(&shared); |
| 180 | AcquireLoadThread thread2(&shared); |
| 181 | RunInParallel(&thread1, &thread2); |
| 182 | EXPECT_EQ(kMagicValue, shared); |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 183 | } |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 184 | |
| 185 | } // namespace base |