| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/dynamic_annotations.h" |
| 5 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 6 #include "base/thread.h" | 7 #include "base/thread.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 namespace { | 10 namespace { |
| 10 | 11 |
| 11 // We use caps here just to ensure that the method name doesn't interfere with | 12 // We use caps here just to ensure that the method name doesn't interfere with |
| 12 // the wildcarded suppressions. | 13 // the wildcarded suppressions. |
| 13 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { | 14 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { |
| 14 public: | 15 public: |
| 15 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} | 16 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} |
| 16 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {} | 17 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {} |
| 17 void ThreadMain() { | 18 void ThreadMain() { |
| 18 *value_ = true; | 19 *value_ = true; |
| 19 | 20 |
| 20 // Sleep for a few milliseconds so the two threads are more likely to live | 21 // Sleep for a few milliseconds so the two threads are more likely to live |
| 21 // simultaneously. Otherwise we may miss the report due to mutex | 22 // simultaneously. Otherwise we may miss the report due to mutex |
| 22 // lock/unlock's inside thread creation code in pure-happens-before mode... | 23 // lock/unlock's inside thread creation code in pure-happens-before mode... |
| 23 PlatformThread::Sleep(100); | 24 PlatformThread::Sleep(100); |
| 24 } | 25 } |
| 25 private: | 26 private: |
| 26 bool* value_; | 27 bool *value_; |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 } | 30 } |
| 30 | 31 |
| 31 // A memory leak detector should report an error in this test. | 32 // A memory leak detector should report an error in this test. |
| 32 TEST(ToolsSanityTest, MemoryLeak) { | 33 TEST(ToolsSanityTest, MemoryLeak) { |
| 33 int *leak = new int[256]; // Leak some memory intentionally. | 34 int *leak = new int[256]; // Leak some memory intentionally. |
| 34 leak[4] = 1; // Make sure the allocated memory is used. | 35 leak[4] = 1; // Make sure the allocated memory is used. |
| 35 } | 36 } |
| 36 | 37 |
| 38 void ReadValueOutOfArrayBoundsLeft(char *ptr) { |
| 39 LOG(INFO) << "Reading a byte out of bounds: " << ptr[-2]; |
| 40 } |
| 41 |
| 42 void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
| 43 LOG(INFO) << "Reading a byte out of bounds: " << ptr[size + 1]; |
| 44 } |
| 45 |
| 46 // This is harmless if you run it under Valgrind thanks to redzones. |
| 47 void WriteValueOutOfArrayBoundsLeft(char *ptr) { |
| 48 ptr[-1] = 42; |
| 49 } |
| 50 |
| 51 // This is harmless if you run it under Valgrind thanks to redzones. |
| 52 void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) { |
| 53 ptr[size] = 42; |
| 54 } |
| 55 |
| 56 void MakeSomeErrors(char *ptr, size_t size) { |
| 57 ReadValueOutOfArrayBoundsLeft(ptr); |
| 58 ReadValueOutOfArrayBoundsRight(ptr, size); |
| 59 WriteValueOutOfArrayBoundsLeft(ptr); |
| 60 WriteValueOutOfArrayBoundsRight(ptr, size); |
| 61 } |
| 62 |
| 63 TEST(ToolsSanityTest, AccessesToNewMemory) { |
| 64 // This test may corrupt memory if not run under Valgrind. |
| 65 if (!RunningOnValgrind()) |
| 66 return; |
| 67 |
| 68 char *foo = new char[10]; |
| 69 MakeSomeErrors(foo, 10); |
| 70 delete [] foo; |
| 71 foo[5] = 0; // Use after delete. This won't break anything under Valgrind. |
| 72 } |
| 73 |
| 74 TEST(ToolsSanityTest, AccessesToMallocMemory) { |
| 75 // This test may corrupt memory if not run under Valgrind. |
| 76 if (!RunningOnValgrind()) |
| 77 return; |
| 78 |
| 79 char *foo = reinterpret_cast<char*>(malloc(10)); |
| 80 MakeSomeErrors(foo, 10); |
| 81 free(foo); |
| 82 foo[5] = 0; // Use after free. This won't break anything under Valgrind. |
| 83 } |
| 84 |
| 85 TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) { |
| 86 // This test may corrupt memory if not run under Valgrind. |
| 87 if (!RunningOnValgrind()) |
| 88 return; |
| 89 |
| 90 int *foo = new int[10]; |
| 91 delete foo; |
| 92 } |
| 93 |
| 94 TEST(ToolsSanityTest, SingleElementDeletedWithBraces) { |
| 95 // This test may corrupt memory if not run under Valgrind. |
| 96 if (!RunningOnValgrind()) |
| 97 return; |
| 98 |
| 99 int *foo = new int; |
| 100 delete [] foo; |
| 101 } |
| 102 |
| 37 // A data race detector should report an error in this test. | 103 // A data race detector should report an error in this test. |
| 38 TEST(ToolsSanityTest, DataRace) { | 104 TEST(ToolsSanityTest, DataRace) { |
| 39 bool shared = false; | 105 bool shared = false; |
| 40 PlatformThreadHandle a; | 106 PlatformThreadHandle a; |
| 41 PlatformThreadHandle b; | 107 PlatformThreadHandle b; |
| 42 PlatformThread::Delegate *thread1 = | 108 PlatformThread::Delegate *thread1 = |
| 43 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); | 109 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); |
| 44 PlatformThread::Delegate *thread2 = | 110 PlatformThread::Delegate *thread2 = |
| 45 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); | 111 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); |
| 46 | 112 |
| 47 PlatformThread::Create(0, thread1, &a); | 113 PlatformThread::Create(0, thread1, &a); |
| 48 PlatformThread::Create(0, thread2, &b); | 114 PlatformThread::Create(0, thread2, &b); |
| 49 PlatformThread::Join(a); | 115 PlatformThread::Join(a); |
| 50 PlatformThread::Join(b); | 116 PlatformThread::Join(b); |
| 51 EXPECT_TRUE(shared); | 117 EXPECT_TRUE(shared); |
| 52 delete thread1; | 118 delete thread1; |
| 53 delete thread2; | 119 delete thread2; |
| 54 } | 120 } |
| OLD | NEW |