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"
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
{
16 explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value
) : value_(value
) {}
17 ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
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);
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
) {
51 // This is harmless if you run it under Valgrind thanks to redzones.
52 void WriteValueOutOfArrayBoundsRight(char *ptr
, size_t size
) {
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())
68 char *foo
= new char[10];
69 MakeSomeErrors(foo
, 10);
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())
79 char *foo
= reinterpret_cast<char*>(malloc(10));
80 MakeSomeErrors(foo
, 10);
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())
90 int *foo
= new int[10];
94 TEST(ToolsSanityTest
, SingleElementDeletedWithBraces
) {
95 // This test may corrupt memory if not run under Valgrind.
96 if (!RunningOnValgrind())
103 // A data race detector should report an error in this test.
104 TEST(ToolsSanityTest
, DataRace
) {
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
);