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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 6 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
7 #include "base/thread.h" | 7 #include "base/thread.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 // 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 |
13 // the wildcarded suppressions. | 13 // the wildcarded suppressions. |
14 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { | 14 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { |
15 public: | 15 public: |
16 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} | 16 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {} |
17 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {} | 17 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {} |
18 void ThreadMain() { | 18 void ThreadMain() { |
19 *value_ = true; | 19 *value_ = true; |
20 | 20 |
21 // 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 |
22 // simultaneously. Otherwise we may miss the report due to mutex | 22 // simultaneously. Otherwise we may miss the report due to mutex |
23 // 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... |
24 PlatformThread::Sleep(100); | 24 PlatformThread::Sleep(100); |
25 } | 25 } |
26 private: | 26 private: |
27 bool *value_; | 27 bool *value_; |
28 }; | 28 }; |
29 | 29 |
30 } | |
31 | |
32 // A memory leak detector should report an error in this test. | |
33 TEST(ToolsSanityTest, MemoryLeak) { | |
34 int *leak = new int[256]; // Leak some memory intentionally. | |
35 leak[4] = 1; // Make sure the allocated memory is used. | |
36 } | |
37 | |
38 void ReadUninitializedValue(char *ptr) { | 30 void ReadUninitializedValue(char *ptr) { |
39 if (*ptr == '\0') { | 31 if (*ptr == '\0') { |
40 (*ptr)++; | 32 (*ptr)++; |
41 } else { | 33 } else { |
42 (*ptr)--; | 34 (*ptr)--; |
43 } | 35 } |
44 } | 36 } |
45 | 37 |
46 void ReadValueOutOfArrayBoundsLeft(char *ptr) { | 38 void ReadValueOutOfArrayBoundsLeft(char *ptr) { |
47 LOG(INFO) << "Reading a byte out of bounds: " << ptr[-2]; | 39 LOG(INFO) << "Reading a byte out of bounds: " << ptr[-2]; |
(...skipping 22 matching lines...) Expand all Loading... |
70 // Valgrind on Linux and Mac, Dr. Memory on Windows. | 62 // Valgrind on Linux and Mac, Dr. Memory on Windows. |
71 // Currently writing values out-of-bounds makes Dr. Memory a bit crazy when | 63 // Currently writing values out-of-bounds makes Dr. Memory a bit crazy when |
72 // this code is linked with /MTd, so skip these writes on Windows. | 64 // this code is linked with /MTd, so skip these writes on Windows. |
73 // See http://code.google.com/p/drmemory/issues/detail?id=51 | 65 // See http://code.google.com/p/drmemory/issues/detail?id=51 |
74 #if !defined(OS_WIN) | 66 #if !defined(OS_WIN) |
75 WriteValueOutOfArrayBoundsLeft(ptr); | 67 WriteValueOutOfArrayBoundsLeft(ptr); |
76 WriteValueOutOfArrayBoundsRight(ptr, size); | 68 WriteValueOutOfArrayBoundsRight(ptr, size); |
77 #endif | 69 #endif |
78 } | 70 } |
79 | 71 |
| 72 } // namespace |
| 73 |
| 74 // A memory leak detector should report an error in this test. |
| 75 TEST(ToolsSanityTest, MemoryLeak) { |
| 76 int *leak = new int[256]; // Leak some memory intentionally. |
| 77 leak[4] = 1; // Make sure the allocated memory is used. |
| 78 } |
| 79 |
80 TEST(ToolsSanityTest, AccessesToNewMemory) { | 80 TEST(ToolsSanityTest, AccessesToNewMemory) { |
81 // This test may corrupt memory if not run under Valgrind. | 81 // This test may corrupt memory if not run under Valgrind. |
82 if (!RunningOnValgrind()) | 82 if (!RunningOnValgrind()) |
83 return; | 83 return; |
84 | 84 |
85 char *foo = new char[10]; | 85 char *foo = new char[10]; |
86 MakeSomeErrors(foo, 10); | 86 MakeSomeErrors(foo, 10); |
87 delete [] foo; | 87 delete [] foo; |
88 foo[5] = 0; // Use after delete. This won't break anything under Valgrind. | 88 foo[5] = 0; // Use after delete. This won't break anything under Valgrind. |
89 } | 89 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); | 127 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared); |
128 | 128 |
129 PlatformThread::Create(0, thread1, &a); | 129 PlatformThread::Create(0, thread1, &a); |
130 PlatformThread::Create(0, thread2, &b); | 130 PlatformThread::Create(0, thread2, &b); |
131 PlatformThread::Join(a); | 131 PlatformThread::Join(a); |
132 PlatformThread::Join(b); | 132 PlatformThread::Join(b); |
133 EXPECT_TRUE(shared); | 133 EXPECT_TRUE(shared); |
134 delete thread1; | 134 delete thread1; |
135 delete thread2; | 135 delete thread2; |
136 } | 136 } |
OLD | NEW |