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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 } | 138 } |
139 | 139 |
140 TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) { | 140 TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) { |
141 char *foo = reinterpret_cast<char*>(malloc(10)); | 141 char *foo = reinterpret_cast<char*>(malloc(10)); |
142 MakeSomeErrors(foo, 10); | 142 MakeSomeErrors(foo, 10); |
143 free(foo); | 143 free(foo); |
144 // Use after free. | 144 // Use after free. |
145 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); | 145 HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free"); |
146 } | 146 } |
147 | 147 |
| 148 static int* allocateArray() { |
| 149 // Clang warns about the mismatched new[]/delete if they occur in the same |
| 150 // function. |
| 151 return new int[10]; |
| 152 } |
| 153 |
148 TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) { | 154 TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) { |
149 #if !defined(ADDRESS_SANITIZER) && !defined(SYZYASAN) | 155 #if !defined(ADDRESS_SANITIZER) && !defined(SYZYASAN) |
150 // This test may corrupt memory if not run under Valgrind or compiled with | 156 // This test may corrupt memory if not run under Valgrind or compiled with |
151 // AddressSanitizer. | 157 // AddressSanitizer. |
152 if (!RunningOnValgrind()) | 158 if (!RunningOnValgrind()) |
153 return; | 159 return; |
154 #endif | 160 #endif |
155 | 161 |
156 // Without the |volatile|, clang optimizes away the next two lines. | 162 // Without the |volatile|, clang optimizes away the next two lines. |
157 int* volatile foo = new int[10]; | 163 int* volatile foo = allocateArray(); |
158 delete foo; | 164 delete foo; |
159 } | 165 } |
160 | 166 |
| 167 static int* allocateScalar() { |
| 168 // Clang warns about the mismatched new/delete[] if they occur in the same |
| 169 // function. |
| 170 return new int; |
| 171 } |
| 172 |
161 TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) { | 173 TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) { |
162 #if !defined(ADDRESS_SANITIZER) | 174 #if !defined(ADDRESS_SANITIZER) |
163 // This test may corrupt memory if not run under Valgrind or compiled with | 175 // This test may corrupt memory if not run under Valgrind or compiled with |
164 // AddressSanitizer. | 176 // AddressSanitizer. |
165 if (!RunningOnValgrind()) | 177 if (!RunningOnValgrind()) |
166 return; | 178 return; |
167 #endif | 179 #endif |
168 | 180 |
169 // Without the |volatile|, clang optimizes away the next two lines. | 181 // Without the |volatile|, clang optimizes away the next two lines. |
170 int* volatile foo = new int; | 182 int* volatile foo = allocateScalar(); |
171 (void) foo; | 183 (void) foo; |
172 delete [] foo; | 184 delete [] foo; |
173 } | 185 } |
174 | 186 |
175 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) | 187 #if defined(ADDRESS_SANITIZER) || defined(SYZYASAN) |
176 | 188 |
177 TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) { | 189 TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) { |
178 // Intentionally crash to make sure AddressSanitizer is running. | 190 // Intentionally crash to make sure AddressSanitizer is running. |
179 // This test should not be ran on bots. | 191 // This test should not be ran on bots. |
180 int* volatile zero = NULL; | 192 int* volatile zero = NULL; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 333 |
322 TEST(ToolsSanityTest, AtomicsAreIgnored) { | 334 TEST(ToolsSanityTest, AtomicsAreIgnored) { |
323 base::subtle::Atomic32 shared = 0; | 335 base::subtle::Atomic32 shared = 0; |
324 ReleaseStoreThread thread1(&shared); | 336 ReleaseStoreThread thread1(&shared); |
325 AcquireLoadThread thread2(&shared); | 337 AcquireLoadThread thread2(&shared); |
326 RunInParallel(&thread1, &thread2); | 338 RunInParallel(&thread1, &thread2); |
327 EXPECT_EQ(kMagicValue, shared); | 339 EXPECT_EQ(kMagicValue, shared); |
328 } | 340 } |
329 | 341 |
330 } // namespace base | 342 } // namespace base |
OLD | NEW |