OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file contains intentional memory errors, some of which may lead to | 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 | 6 // crashes if the test is ran without special memory testing tools. We use these |
7 // errors to verify the sanity of the tools. | 7 // errors to verify the sanity of the tools. |
8 | 8 |
9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/debug/asan_invalid_access.h" | 10 #include "base/debug/asan_invalid_access.h" |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 | 280 |
281 void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) { | 281 void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) { |
282 PlatformThreadHandle a; | 282 PlatformThreadHandle a; |
283 PlatformThreadHandle b; | 283 PlatformThreadHandle b; |
284 PlatformThread::Create(0, d1, &a); | 284 PlatformThread::Create(0, d1, &a); |
285 PlatformThread::Create(0, d2, &b); | 285 PlatformThread::Create(0, d2, &b); |
286 PlatformThread::Join(a); | 286 PlatformThread::Join(a); |
287 PlatformThread::Join(b); | 287 PlatformThread::Join(b); |
288 } | 288 } |
289 | 289 |
290 void DataRace() { | |
291 bool *shared = new bool(false); | |
292 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(shared), thread2(shared); | |
293 RunInParallel(&thread1, &thread2); | |
294 EXPECT_TRUE(*shared); | |
295 delete shared; | |
296 #if defined(THREAD_SANITIZER) | |
297 // We're in a death test - crash. | |
298 CHECK(0); | |
299 #endif | |
300 } | |
301 | |
290 } // namespace | 302 } // namespace |
291 | 303 |
292 // A data race detector should report an error in this test. | 304 // A data race detector should report an error in this test. |
293 TEST(ToolsSanityTest, DataRace) { | 305 TEST(ToolsSanityTest, DataRace) { |
294 bool *shared = new bool(false); | 306 #if defined(THREAD_SANITIZER) |
295 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(shared), thread2(shared); | 307 // The suppression regexp must match that in base/debug/tsan_suppressions.cc. |
296 RunInParallel(&thread1, &thread2); | 308 EXPECT_DEATH(DataRace(), "1 race:base/tools_sanity_unittest.cc"); |
297 EXPECT_TRUE(*shared); | 309 #else |
298 delete shared; | 310 // TODO(glider): remove this path when TSan v1 is retired. |
Nico
2014/06/25 16:10:22
Isn't this done?
| |
311 DataRace(); | |
312 #endif | |
299 } | 313 } |
300 | 314 |
301 TEST(ToolsSanityTest, AnnotateBenignRace) { | 315 TEST(ToolsSanityTest, AnnotateBenignRace) { |
302 bool shared = false; | 316 bool shared = false; |
303 ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up"); | 317 ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up"); |
304 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); | 318 TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared); |
305 RunInParallel(&thread1, &thread2); | 319 RunInParallel(&thread1, &thread2); |
306 EXPECT_TRUE(shared); | 320 EXPECT_TRUE(shared); |
307 } | 321 } |
308 | 322 |
309 TEST(ToolsSanityTest, AtomicsAreIgnored) { | 323 TEST(ToolsSanityTest, AtomicsAreIgnored) { |
310 base::subtle::Atomic32 shared = 0; | 324 base::subtle::Atomic32 shared = 0; |
311 ReleaseStoreThread thread1(&shared); | 325 ReleaseStoreThread thread1(&shared); |
312 AcquireLoadThread thread2(&shared); | 326 AcquireLoadThread thread2(&shared); |
313 RunInParallel(&thread1, &thread2); | 327 RunInParallel(&thread1, &thread2); |
314 EXPECT_EQ(kMagicValue, shared); | 328 EXPECT_EQ(kMagicValue, shared); |
315 } | 329 } |
316 | 330 |
317 } // namespace base | 331 } // namespace base |
OLD | NEW |