diff options
author | timurrrr@chromium.org <timurrrr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 12:54:44 +0000 |
---|---|---|
committer | timurrrr@chromium.org <timurrrr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 12:54:44 +0000 |
commit | 9ded99137e5ada1d9fbeeee8632f3a825f3c4498 (patch) | |
tree | 8a2645eabef077ec85894b281c3e96b1c81c2fb8 /base | |
parent | a10d16e53eac4afba30c05ff56911576be2c569e (diff) | |
download | chromium_src-9ded99137e5ada1d9fbeeee8632f3a825f3c4498.zip chromium_src-9ded99137e5ada1d9fbeeee8632f3a825f3c4498.tar.gz chromium_src-9ded99137e5ada1d9fbeeee8632f3a825f3c4498.tar.bz2 |
Implement more sanity tests for Memcheck/Valgrind
Review URL: http://codereview.chromium.org/1242008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42751 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/tools_sanity_unittest.cc | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/base/tools_sanity_unittest.cc b/base/tools_sanity_unittest.cc index 0a18031..f4d4df9 100644 --- a/base/tools_sanity_unittest.cc +++ b/base/tools_sanity_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/dynamic_annotations.h" #include "base/message_loop.h" #include "base/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -23,7 +24,7 @@ class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate { PlatformThread::Sleep(100); } private: - bool* value_; + bool *value_; }; } @@ -34,6 +35,71 @@ TEST(ToolsSanityTest, MemoryLeak) { leak[4] = 1; // Make sure the allocated memory is used. } +void ReadValueOutOfArrayBoundsLeft(char *ptr) { + LOG(INFO) << "Reading a byte out of bounds: " << ptr[-2]; +} + +void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) { + LOG(INFO) << "Reading a byte out of bounds: " << ptr[size + 1]; +} + +// This is harmless if you run it under Valgrind thanks to redzones. +void WriteValueOutOfArrayBoundsLeft(char *ptr) { + ptr[-1] = 42; +} + +// This is harmless if you run it under Valgrind thanks to redzones. +void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) { + ptr[size] = 42; +} + +void MakeSomeErrors(char *ptr, size_t size) { + ReadValueOutOfArrayBoundsLeft(ptr); + ReadValueOutOfArrayBoundsRight(ptr, size); + WriteValueOutOfArrayBoundsLeft(ptr); + WriteValueOutOfArrayBoundsRight(ptr, size); +} + +TEST(ToolsSanityTest, AccessesToNewMemory) { + // This test may corrupt memory if not run under Valgrind. + if (!RunningOnValgrind()) + return; + + char *foo = new char[10]; + MakeSomeErrors(foo, 10); + delete [] foo; + foo[5] = 0; // Use after delete. This won't break anything under Valgrind. +} + +TEST(ToolsSanityTest, AccessesToMallocMemory) { + // This test may corrupt memory if not run under Valgrind. + if (!RunningOnValgrind()) + return; + + char *foo = reinterpret_cast<char*>(malloc(10)); + MakeSomeErrors(foo, 10); + free(foo); + foo[5] = 0; // Use after free. This won't break anything under Valgrind. +} + +TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) { + // This test may corrupt memory if not run under Valgrind. + if (!RunningOnValgrind()) + return; + + int *foo = new int[10]; + delete foo; +} + +TEST(ToolsSanityTest, SingleElementDeletedWithBraces) { + // This test may corrupt memory if not run under Valgrind. + if (!RunningOnValgrind()) + return; + + int *foo = new int; + delete [] foo; +} + // A data race detector should report an error in this test. TEST(ToolsSanityTest, DataRace) { bool shared = false; |