Port PluginObject fix downstream. See http://trac.webkit.org/changeset/61415/ for...
[chromium-blink-merge.git] / base / tools_sanity_unittest.cc
blob30230591ab0817d8b8d86a655e5d5bdc66270685
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
3 // found in the LICENSE file.
5 #include "base/message_loop.h"
6 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
7 #include "base/thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace {
12 // We use caps here just to ensure that the method name doesn't interfere with
13 // the wildcarded suppressions.
14 class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
15 public:
16 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
17 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
18 void ThreadMain() {
19 *value_ = true;
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
23 // lock/unlock's inside thread creation code in pure-happens-before mode...
24 PlatformThread::Sleep(100);
26 private:
27 bool *value_;
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.
38 void ReadValueOutOfArrayBoundsLeft(char *ptr) {
39 LOG(INFO) << "Reading a byte out of bounds: " << ptr[-2];
42 void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) {
43 LOG(INFO) << "Reading a byte out of bounds: " << ptr[size + 1];
46 // This is harmless if you run it under Valgrind thanks to redzones.
47 void WriteValueOutOfArrayBoundsLeft(char *ptr) {
48 ptr[-1] = 42;
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;
56 void MakeSomeErrors(char *ptr, size_t size) {
57 ReadValueOutOfArrayBoundsLeft(ptr);
58 ReadValueOutOfArrayBoundsRight(ptr, size);
59 WriteValueOutOfArrayBoundsLeft(ptr);
60 WriteValueOutOfArrayBoundsRight(ptr, size);
63 TEST(ToolsSanityTest, AccessesToNewMemory) {
64 // This test may corrupt memory if not run under Valgrind.
65 if (!RunningOnValgrind())
66 return;
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.
74 TEST(ToolsSanityTest, AccessesToMallocMemory) {
75 // This test may corrupt memory if not run under Valgrind.
76 if (!RunningOnValgrind())
77 return;
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.
85 TEST(ToolsSanityTest, ArrayDeletedWithoutBraces) {
86 // This test may corrupt memory if not run under Valgrind.
87 if (!RunningOnValgrind())
88 return;
90 int *foo = new int[10];
91 delete foo;
94 TEST(ToolsSanityTest, SingleElementDeletedWithBraces) {
95 // This test may corrupt memory if not run under Valgrind.
96 if (!RunningOnValgrind())
97 return;
99 int *foo = new int;
100 delete [] foo;
103 // A data race detector should report an error in this test.
104 TEST(ToolsSanityTest, DataRace) {
105 bool shared = false;
106 PlatformThreadHandle a;
107 PlatformThreadHandle b;
108 PlatformThread::Delegate *thread1 =
109 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);
110 PlatformThread::Delegate *thread2 =
111 new TOOLS_SANITY_TEST_CONCURRENT_THREAD(&shared);
113 PlatformThread::Create(0, thread1, &a);
114 PlatformThread::Create(0, thread2, &b);
115 PlatformThread::Join(a);
116 PlatformThread::Join(b);
117 EXPECT_TRUE(shared);
118 delete thread1;
119 delete thread2;