[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 11 | #include "base/atomicops.h" |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 12 | #include "base/debug/asan_invalid_access.h" |
| 13 | #include "base/debug/profiler.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 15 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 16 | #include "base/threading/thread.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 22 | namespace { |
| 23 | |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 24 | const base::subtle::Atomic32 kMagicValue = 42; |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 25 | |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 26 | // Helper for memory accesses that can potentially corrupt memory or cause a |
| 27 | // crash during a native run. |
[email protected] | aee2f33 | 2014-03-27 15:08:04 | [diff] [blame] | 28 | #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 29 | #if defined(OS_IOS) |
| 30 | // EXPECT_DEATH is not supported on IOS. |
| 31 | #define HARMFUL_ACCESS(action,error_regexp) do { action; } while (0) |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 32 | #elif defined(SYZYASAN) |
| 33 | // We won't get a meaningful error message because we're not running under the |
| 34 | // SyzyASan logger, but we can at least make sure that the error has been |
| 35 | // generated in the SyzyASan runtime. |
| 36 | #define HARMFUL_ACCESS(action,unused) \ |
| 37 | if (debug::IsBinaryInstrumented()) { EXPECT_DEATH(action, \ |
| 38 | "AsanRuntime::OnError"); } |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 39 | #else |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 40 | #define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp) |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 41 | #endif // !OS_IOS && !SYZYASAN |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 42 | #else |
| 43 | #define HARMFUL_ACCESS(action,error_regexp) \ |
| 44 | do { if (RunningOnValgrind()) { action; } } while (0) |
| 45 | #endif |
| 46 | |
[email protected] | 1e51a96 | 2014-01-16 05:31:41 | [diff] [blame] | 47 | void DoReadUninitializedValue(char *ptr) { |
[email protected] | 43dea19 | 2012-05-16 08:42:08 | [diff] [blame] | 48 | // Comparison with 64 is to prevent clang from optimizing away the |
[email protected] | 83a13b2 | 2011-09-18 16:39:12 | [diff] [blame] | 49 | // jump -- valgrind only catches jumps and conditional moves, but clang uses |
| 50 | // the borrow flag if the condition is just `*ptr == '\0'`. |
[email protected] | 43dea19 | 2012-05-16 08:42:08 | [diff] [blame] | 51 | if (*ptr == 64) { |
[email protected] | 1e51a96 | 2014-01-16 05:31:41 | [diff] [blame] | 52 | VLOG(1) << "Uninit condition is true"; |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 53 | } else { |
[email protected] | 1e51a96 | 2014-01-16 05:31:41 | [diff] [blame] | 54 | VLOG(1) << "Uninit condition is false"; |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
[email protected] | 1e51a96 | 2014-01-16 05:31:41 | [diff] [blame] | 58 | void ReadUninitializedValue(char *ptr) { |
| 59 | #if defined(MEMORY_SANITIZER) |
| 60 | EXPECT_DEATH(DoReadUninitializedValue(ptr), |
| 61 | "use-of-uninitialized-value"); |
| 62 | #else |
| 63 | DoReadUninitializedValue(ptr); |
| 64 | #endif |
| 65 | } |
| 66 | |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 67 | void ReadValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 68 | char c = ptr[-2]; |
| 69 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | 45b9eba | 2010-10-18 23:57:49 | [diff] [blame] | 73 | char c = ptr[size + 1]; |
| 74 | VLOG(1) << "Reading a byte out of bounds: " << c; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 78 | void WriteValueOutOfArrayBoundsLeft(char *ptr) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 79 | ptr[-1] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // This is harmless if you run it under Valgrind thanks to redzones. |
| 83 | void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 84 | ptr[size] = kMagicValue; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void MakeSomeErrors(char *ptr, size_t size) { |
[email protected] | adf7d80 | 2010-09-23 09:12:37 | [diff] [blame] | 88 | ReadUninitializedValue(ptr); |
[email protected] | e260ebc | 2013-12-18 05:31:33 | [diff] [blame] | 89 | |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 90 | HARMFUL_ACCESS(ReadValueOutOfArrayBoundsLeft(ptr), |
[email protected] | e260ebc | 2013-12-18 05:31:33 | [diff] [blame] | 91 | "2 bytes to the left"); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 92 | HARMFUL_ACCESS(ReadValueOutOfArrayBoundsRight(ptr, size), |
[email protected] | e260ebc | 2013-12-18 05:31:33 | [diff] [blame] | 93 | "1 bytes to the right"); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 94 | HARMFUL_ACCESS(WriteValueOutOfArrayBoundsLeft(ptr), |
[email protected] | e260ebc | 2013-12-18 05:31:33 | [diff] [blame] | 95 | "1 bytes to the left"); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 96 | HARMFUL_ACCESS(WriteValueOutOfArrayBoundsRight(ptr, size), |
[email protected] | e260ebc | 2013-12-18 05:31:33 | [diff] [blame] | 97 | "0 bytes to the right"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 66d051bb | 2010-10-14 08:25:54 | [diff] [blame] | 100 | } // namespace |
| 101 | |
| 102 | // A memory leak detector should report an error in this test. |
| 103 | TEST(ToolsSanityTest, MemoryLeak) { |
[email protected] | 6bfad0e | 2011-11-01 11:08:09 | [diff] [blame] | 104 | // Without the |volatile|, clang optimizes away the next two lines. |
| 105 | int* volatile leak = new int[256]; // Leak some memory intentionally. |
[email protected] | 66d051bb | 2010-10-14 08:25:54 | [diff] [blame] | 106 | leak[4] = 1; // Make sure the allocated memory is used. |
| 107 | } |
| 108 | |
[email protected] | 3da8c16 | 2014-03-28 17:35:10 | [diff] [blame] | 109 | #if (defined(ADDRESS_SANITIZER) && defined(OS_IOS)) || defined(SYZYASAN) |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 110 | // Because iOS doesn't support death tests, each of the following tests will |
[email protected] | 3da8c16 | 2014-03-28 17:35:10 | [diff] [blame] | 111 | // crash the whole program under Asan. On Windows Asan is based on SyzyAsan; the |
| 112 | // error report mechanism is different than with Asan so these tests will fail. |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 113 | #define MAYBE_AccessesToNewMemory DISABLED_AccessesToNewMemory |
| 114 | #define MAYBE_AccessesToMallocMemory DISABLED_AccessesToMallocMemory |
[email protected] | 2d4e013 | 2014-05-14 11:50:37 | [diff] [blame] | 115 | #else |
| 116 | #define MAYBE_AccessesToNewMemory AccessesToNewMemory |
| 117 | #define MAYBE_AccessesToMallocMemory AccessesToMallocMemory |
[email protected] | de0c7ed5 | 2014-07-18 02:40:40 | [diff] [blame] | 118 | #endif // (defined(ADDRESS_SANITIZER) && defined(OS_IOS)) || defined(SYZYASAN) |
[email protected] | f516280 | 2014-05-14 16:04:30 | [diff] [blame] | 119 | |
| 120 | // The following tests pass with Clang r170392, but not r172454, which |
| 121 | // makes AddressSanitizer detect errors in them. We disable these tests under |
| 122 | // AddressSanitizer until we fully switch to Clang r172454. After that the |
| 123 | // tests should be put back under the (defined(OS_IOS) || defined(OS_WIN)) |
| 124 | // clause above. |
| 125 | // See also http://crbug.com/172614. |
| 126 | #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
[email protected] | 2d4e013 | 2014-05-14 11:50:37 | [diff] [blame] | 127 | #define MAYBE_SingleElementDeletedWithBraces \ |
| 128 | DISABLED_SingleElementDeletedWithBraces |
[email protected] | f516280 | 2014-05-14 16:04:30 | [diff] [blame] | 129 | #define MAYBE_ArrayDeletedWithoutBraces DISABLED_ArrayDeletedWithoutBraces |
[email protected] | de0c7ed5 | 2014-07-18 02:40:40 | [diff] [blame] | 130 | #else |
| 131 | #define MAYBE_ArrayDeletedWithoutBraces ArrayDeletedWithoutBraces |
| 132 | #define MAYBE_SingleElementDeletedWithBraces SingleElementDeletedWithBraces |
| 133 | #endif // defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
| 134 | |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 135 | TEST(ToolsSanityTest, MAYBE_AccessesToNewMemory) { |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 136 | char *foo = new char[10]; |
| 137 | MakeSomeErrors(foo, 10); |
| 138 | delete [] foo; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 139 | // Use after delete. |
| 140 | HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 143 | TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) { |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 144 | char *foo = reinterpret_cast<char*>(malloc(10)); |
| 145 | MakeSomeErrors(foo, 10); |
| 146 | free(foo); |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 147 | // Use after free. |
| 148 | HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 149 | } |
| 150 | |
hans | cd4cce3 | 2015-05-19 17:03:14 | [diff] [blame] | 151 | static int* allocateArray() { |
| 152 | // Clang warns about the mismatched new[]/delete if they occur in the same |
| 153 | // function. |
| 154 | return new int[10]; |
| 155 | } |
| 156 | |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 157 | TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) { |
[email protected] | aee2f33 | 2014-03-27 15:08:04 | [diff] [blame] | 158 | #if !defined(ADDRESS_SANITIZER) && !defined(SYZYASAN) |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 159 | // This test may corrupt memory if not run under Valgrind or compiled with |
| 160 | // AddressSanitizer. |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 161 | if (!RunningOnValgrind()) |
| 162 | return; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 163 | #endif |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 164 | |
[email protected] | 014b0b06 | 2011-09-18 16:30:12 | [diff] [blame] | 165 | // Without the |volatile|, clang optimizes away the next two lines. |
hans | cd4cce3 | 2015-05-19 17:03:14 | [diff] [blame] | 166 | int* volatile foo = allocateArray(); |
[email protected] | f516280 | 2014-05-14 16:04:30 | [diff] [blame] | 167 | delete foo; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 168 | } |
| 169 | |
hans | cd4cce3 | 2015-05-19 17:03:14 | [diff] [blame] | 170 | static int* allocateScalar() { |
| 171 | // Clang warns about the mismatched new/delete[] if they occur in the same |
| 172 | // function. |
| 173 | return new int; |
| 174 | } |
| 175 | |
[email protected] | 9a4f618 | 2012-10-24 14:11:02 | [diff] [blame] | 176 | TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) { |
| 177 | #if !defined(ADDRESS_SANITIZER) |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 178 | // This test may corrupt memory if not run under Valgrind or compiled with |
| 179 | // AddressSanitizer. |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 180 | if (!RunningOnValgrind()) |
| 181 | return; |
[email protected] | c2a1849 | 2011-10-05 13:22:50 | [diff] [blame] | 182 | #endif |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 183 | |
[email protected] | 014b0b06 | 2011-09-18 16:30:12 | [diff] [blame] | 184 | // Without the |volatile|, clang optimizes away the next two lines. |
hans | cd4cce3 | 2015-05-19 17:03:14 | [diff] [blame] | 185 | int* volatile foo = allocateScalar(); |
[email protected] | 928ca1d | 2011-09-30 18:52:04 | [diff] [blame] | 186 | (void) foo; |
[email protected] | f516280 | 2014-05-14 16:04:30 | [diff] [blame] | 187 | delete [] foo; |
[email protected] | 9ded991 | 2010-03-26 12:54:44 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | aee2f33 | 2014-03-27 15:08:04 | [diff] [blame] | 190 | #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 191 | |
[email protected] | 9b147ae | 2011-11-08 09:28:49 | [diff] [blame] | 192 | TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) { |
[email protected] | 6fcbc62f | 2011-10-12 17:18:24 | [diff] [blame] | 193 | // Intentionally crash to make sure AddressSanitizer is running. |
| 194 | // This test should not be ran on bots. |
| 195 | int* volatile zero = NULL; |
| 196 | *zero = 0; |
| 197 | } |
[email protected] | 9b147ae | 2011-11-08 09:28:49 | [diff] [blame] | 198 | |
| 199 | TEST(ToolsSanityTest, DISABLED_AddressSanitizerLocalOOBCrashTest) { |
| 200 | // Intentionally crash to make sure AddressSanitizer is instrumenting |
| 201 | // the local variables. |
| 202 | // This test should not be ran on bots. |
| 203 | int array[5]; |
| 204 | // Work around the OOB warning reported by Clang. |
| 205 | int* volatile access = &array[5]; |
| 206 | *access = 43; |
| 207 | } |
| 208 | |
| 209 | namespace { |
| 210 | int g_asan_test_global_array[10]; |
| 211 | } // namespace |
| 212 | |
| 213 | TEST(ToolsSanityTest, DISABLED_AddressSanitizerGlobalOOBCrashTest) { |
| 214 | // Intentionally crash to make sure AddressSanitizer is instrumenting |
| 215 | // the global variables. |
| 216 | // This test should not be ran on bots. |
| 217 | |
| 218 | // Work around the OOB warning reported by Clang. |
| 219 | int* volatile access = g_asan_test_global_array - 1; |
| 220 | *access = 43; |
| 221 | } |
| 222 | |
[email protected] | b4b3479 | 2014-06-14 08:29:37 | [diff] [blame] | 223 | TEST(ToolsSanityTest, AsanHeapOverflow) { |
| 224 | HARMFUL_ACCESS(debug::AsanHeapOverflow() ,"to the right"); |
| 225 | } |
| 226 | |
| 227 | TEST(ToolsSanityTest, AsanHeapUnderflow) { |
| 228 | HARMFUL_ACCESS(debug::AsanHeapUnderflow(), "to the left"); |
| 229 | } |
| 230 | |
| 231 | TEST(ToolsSanityTest, AsanHeapUseAfterFree) { |
| 232 | HARMFUL_ACCESS(debug::AsanHeapUseAfterFree(), "heap-use-after-free"); |
| 233 | } |
| 234 | |
| 235 | #if defined(SYZYASAN) |
| 236 | TEST(ToolsSanityTest, AsanCorruptHeapBlock) { |
| 237 | HARMFUL_ACCESS(debug::AsanCorruptHeapBlock(), ""); |
| 238 | } |
| 239 | |
| 240 | TEST(ToolsSanityTest, AsanCorruptHeap) { |
| 241 | // This test will kill the process by raising an exception, there's no |
| 242 | // particular string to look for in the stack trace. |
| 243 | EXPECT_DEATH(debug::AsanCorruptHeap(), ""); |
| 244 | } |
| 245 | #endif // SYZYASAN |
| 246 | |
| 247 | #endif // ADDRESS_SANITIZER || SYZYASAN |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 248 | |
| 249 | namespace { |
| 250 | |
| 251 | // We use caps here just to ensure that the method name doesn't interfere with |
| 252 | // the wildcarded suppressions. |
| 253 | class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { |
| 254 | public: |
| 255 | explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 256 | ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() override {} |
| 257 | void ThreadMain() override { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 258 | *value_ = true; |
| 259 | |
| 260 | // Sleep for a few milliseconds so the two threads are more likely to live |
| 261 | // simultaneously. Otherwise we may miss the report due to mutex |
| 262 | // lock/unlock's inside thread creation code in pure-happens-before mode... |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 263 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 264 | } |
| 265 | private: |
| 266 | bool *value_; |
| 267 | }; |
| 268 | |
| 269 | class ReleaseStoreThread : public PlatformThread::Delegate { |
| 270 | public: |
| 271 | explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 272 | ~ReleaseStoreThread() override {} |
| 273 | void ThreadMain() override { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 274 | base::subtle::Release_Store(value_, kMagicValue); |
| 275 | |
| 276 | // Sleep for a few milliseconds so the two threads are more likely to live |
| 277 | // simultaneously. Otherwise we may miss the report due to mutex |
| 278 | // lock/unlock's inside thread creation code in pure-happens-before mode... |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 279 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 280 | } |
| 281 | private: |
| 282 | base::subtle::Atomic32 *value_; |
| 283 | }; |
| 284 | |
| 285 | class AcquireLoadThread : public PlatformThread::Delegate { |
| 286 | public: |
| 287 | explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 288 | ~AcquireLoadThread() override {} |
| 289 | void ThreadMain() override { |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 290 | // Wait for the other thread to make Release_Store |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 291 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 292 | base::subtle::Acquire_Load(value_); |
| 293 | } |
| 294 | private: |
| 295 | base::subtle::Atomic32 *value_; |
| 296 | }; |
| 297 | |
| 298 | void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) { |
| 299 | PlatformThreadHandle a; |
| 300 | PlatformThreadHandle b; |
| 301 | PlatformThread::Create(0, d1, &a); |
| 302 | PlatformThread::Create(0, d2, &b); |
| 303 | PlatformThread::Join(a); |
| 304 | PlatformThread::Join(b); |
| 305 | } |
| 306 | |
[email protected] | b11d38d0 | 2014-06-26 13:10:50 | [diff] [blame] | 307 | #if defined(THREAD_SANITIZER) |
| 308 | void DataRace() { |
[email protected] | a405798 | 2013-03-23 11:28:17 | [diff] [blame] | 309 | bool *shared = new bool(false); |
| 310 | TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(shared), thread2(shared); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 311 | RunInParallel(&thread1, &thread2); |
[email protected] | a405798 | 2013-03-23 11:28:17 | [diff] [blame] | 312 | EXPECT_TRUE(*shared); |
| 313 | delete shared; |
[email protected] | b11d38d0 | 2014-06-26 13:10:50 | [diff] [blame] | 314 | // We're in a death test - crash. |
| 315 | CHECK(0); |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 316 | } |
[email protected] | b11d38d0 | 2014-06-26 13:10:50 | [diff] [blame] | 317 | #endif |
| 318 | |
| 319 | } // namespace |
| 320 | |
| 321 | #if defined(THREAD_SANITIZER) |
| 322 | // A data race detector should report an error in this test. |
| 323 | TEST(ToolsSanityTest, DataRace) { |
| 324 | // The suppression regexp must match that in base/debug/tsan_suppressions.cc. |
| 325 | EXPECT_DEATH(DataRace(), "1 race:base/tools_sanity_unittest.cc"); |
| 326 | } |
| 327 | #endif |
[email protected] | f49ecd8f | 2011-05-10 18:03:34 | [diff] [blame] | 328 | |
| 329 | TEST(ToolsSanityTest, AnnotateBenignRace) { |
| 330 | bool shared = false; |
| 331 | ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up"); |
| 332 | TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); |
| 333 | RunInParallel(&thread1, &thread2); |
| 334 | EXPECT_TRUE(shared); |
| 335 | } |
| 336 | |
| 337 | TEST(ToolsSanityTest, AtomicsAreIgnored) { |
| 338 | base::subtle::Atomic32 shared = 0; |
| 339 | ReleaseStoreThread thread1(&shared); |
| 340 | AcquireLoadThread thread2(&shared); |
| 341 | RunInParallel(&thread1, &thread2); |
| 342 | EXPECT_EQ(kMagicValue, shared); |
[email protected] | 0716cba | 2009-12-17 12:37:58 | [diff] [blame] | 343 | } |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 344 | |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 345 | #if defined(CFI_ENFORCEMENT_TRAP) |
Peter Collingbourne | 355546f | 2017-10-12 19:51:27 | [diff] [blame^] | 346 | #if defined(OS_WIN) |
| 347 | #define CFI_ERROR_MSG "EXCEPTION_ILLEGAL_INSTRUCTION" |
| 348 | #elif defined(OS_ANDROID) |
| 349 | // TODO(pcc): Produce proper stack dumps on Android and test for the correct |
| 350 | // si_code here. |
| 351 | #define CFI_ERROR_MSG "^$" |
| 352 | #else |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 353 | #define CFI_ERROR_MSG "ILL_ILLOPN" |
Peter Collingbourne | 355546f | 2017-10-12 19:51:27 | [diff] [blame^] | 354 | #endif |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 355 | #elif defined(CFI_ENFORCEMENT_DIAGNOSTIC) |
| 356 | #define CFI_ERROR_MSG "runtime error: control flow integrity check" |
| 357 | #endif // CFI_ENFORCEMENT_TRAP || CFI_ENFORCEMENT_DIAGNOSTIC |
pcc | 0736491 | 2015-07-31 00:19:06 | [diff] [blame] | 358 | |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 359 | #if defined(CFI_ERROR_MSG) |
krasin | 59d3718 | 2016-07-13 03:24:34 | [diff] [blame] | 360 | class A { |
| 361 | public: |
| 362 | A(): n_(0) {} |
| 363 | virtual void f() { n_++; } |
| 364 | protected: |
| 365 | int n_; |
| 366 | }; |
| 367 | |
| 368 | class B: public A { |
| 369 | public: |
| 370 | void f() override { n_--; } |
| 371 | }; |
| 372 | |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 373 | class C: public B { |
| 374 | public: |
| 375 | void f() override { n_ += 2; } |
| 376 | }; |
| 377 | |
krasin | 59d3718 | 2016-07-13 03:24:34 | [diff] [blame] | 378 | NOINLINE void KillVptrAndCall(A *obj) { |
| 379 | *reinterpret_cast<void **>(obj) = 0; |
| 380 | obj->f(); |
| 381 | } |
| 382 | |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 383 | TEST(ToolsSanityTest, BadVirtualCallNull) { |
krasin | 59d3718 | 2016-07-13 03:24:34 | [diff] [blame] | 384 | A a; |
| 385 | B b; |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 386 | EXPECT_DEATH({ KillVptrAndCall(&a); KillVptrAndCall(&b); }, CFI_ERROR_MSG); |
krasin | 59d3718 | 2016-07-13 03:24:34 | [diff] [blame] | 387 | } |
| 388 | |
Vlad Tsyrklevich | 37f1e35 | 2017-08-15 23:45:10 | [diff] [blame] | 389 | NOINLINE void OverwriteVptrAndCall(B *obj, A *vptr) { |
| 390 | *reinterpret_cast<void **>(obj) = *reinterpret_cast<void **>(vptr); |
| 391 | obj->f(); |
| 392 | } |
| 393 | |
| 394 | TEST(ToolsSanityTest, BadVirtualCallWrongType) { |
| 395 | A a; |
| 396 | B b; |
| 397 | C c; |
| 398 | EXPECT_DEATH({ OverwriteVptrAndCall(&b, &a); OverwriteVptrAndCall(&b, &c); }, |
| 399 | CFI_ERROR_MSG); |
| 400 | } |
| 401 | |
| 402 | // TODO(pcc): remove CFI_CAST_CHECK, see https://crbug.com/626794. |
| 403 | #if defined(CFI_CAST_CHECK) |
| 404 | TEST(ToolsSanityTest, BadDerivedCast) { |
| 405 | A a; |
| 406 | EXPECT_DEATH((void)(B*)&a, CFI_ERROR_MSG); |
| 407 | } |
| 408 | |
| 409 | TEST(ToolsSanityTest, BadUnrelatedCast) { |
| 410 | class A { |
| 411 | virtual void f() {} |
| 412 | }; |
| 413 | |
| 414 | class B { |
| 415 | virtual void f() {} |
| 416 | }; |
| 417 | |
| 418 | A a; |
| 419 | EXPECT_DEATH((void)(B*)&a, CFI_ERROR_MSG); |
| 420 | } |
| 421 | #endif // CFI_CAST_CHECK |
| 422 | |
| 423 | #endif // CFI_ERROR_MSG |
pcc | 0736491 | 2015-07-31 00:19:06 | [diff] [blame] | 424 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 425 | } // namespace base |