This source file includes following definitions.
- DoReadUninitializedValue
- ReadUninitializedValue
- ReadValueOutOfArrayBoundsLeft
- ReadValueOutOfArrayBoundsRight
- WriteValueOutOfArrayBoundsLeft
- WriteValueOutOfArrayBoundsRight
- MakeSomeErrors
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- ThreadMain
- ThreadMain
- ThreadMain
- RunInParallel
- TEST
- TEST
- TEST
#include "base/atomicops.h"
#include "base/message_loop/message_loop.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
const base::subtle::Atomic32 kMagicValue = 42;
#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
#if defined(OS_IOS)
#define HARMFUL_ACCESS(action,error_regexp) do { action; } while (0)
#else
#define HARMFUL_ACCESS(action,error_regexp) EXPECT_DEATH(action,error_regexp)
#endif
#else
#define HARMFUL_ACCESS(action,error_regexp) \
do { if (RunningOnValgrind()) { action; } } while (0)
#endif
void DoReadUninitializedValue(char *ptr) {
if (*ptr == 64) {
VLOG(1) << "Uninit condition is true";
} else {
VLOG(1) << "Uninit condition is false";
}
}
void ReadUninitializedValue(char *ptr) {
#if defined(MEMORY_SANITIZER)
EXPECT_DEATH(DoReadUninitializedValue(ptr),
"use-of-uninitialized-value");
#else
DoReadUninitializedValue(ptr);
#endif
}
void ReadValueOutOfArrayBoundsLeft(char *ptr) {
char c = ptr[-2];
VLOG(1) << "Reading a byte out of bounds: " << c;
}
void ReadValueOutOfArrayBoundsRight(char *ptr, size_t size) {
char c = ptr[size + 1];
VLOG(1) << "Reading a byte out of bounds: " << c;
}
void WriteValueOutOfArrayBoundsLeft(char *ptr) {
ptr[-1] = kMagicValue;
}
void WriteValueOutOfArrayBoundsRight(char *ptr, size_t size) {
ptr[size] = kMagicValue;
}
void MakeSomeErrors(char *ptr, size_t size) {
ReadUninitializedValue(ptr);
HARMFUL_ACCESS(ReadValueOutOfArrayBoundsLeft(ptr),
"2 bytes to the left");
HARMFUL_ACCESS(ReadValueOutOfArrayBoundsRight(ptr, size),
"1 bytes to the right");
HARMFUL_ACCESS(WriteValueOutOfArrayBoundsLeft(ptr),
"1 bytes to the left");
HARMFUL_ACCESS(WriteValueOutOfArrayBoundsRight(ptr, size),
"0 bytes to the right");
}
}
TEST(ToolsSanityTest, MemoryLeak) {
int* volatile leak = new int[256];
leak[4] = 1;
}
#if (defined(ADDRESS_SANITIZER) && defined(OS_IOS)) || defined(SYZYASAN)
#define MAYBE_AccessesToNewMemory DISABLED_AccessesToNewMemory
#define MAYBE_AccessesToMallocMemory DISABLED_AccessesToMallocMemory
#else
#define MAYBE_AccessesToNewMemory AccessesToNewMemory
#define MAYBE_AccessesToMallocMemory AccessesToMallocMemory
#define MAYBE_ArrayDeletedWithoutBraces ArrayDeletedWithoutBraces
#define MAYBE_SingleElementDeletedWithBraces SingleElementDeletedWithBraces
#endif
#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
#define MAYBE_SingleElementDeletedWithBraces \
DISABLED_SingleElementDeletedWithBraces
#define MAYBE_ArrayDeletedWithoutBraces DISABLED_ArrayDeletedWithoutBraces
#endif
TEST(ToolsSanityTest, MAYBE_AccessesToNewMemory) {
char *foo = new char[10];
MakeSomeErrors(foo, 10);
delete [] foo;
HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
}
TEST(ToolsSanityTest, MAYBE_AccessesToMallocMemory) {
char *foo = reinterpret_cast<char*>(malloc(10));
MakeSomeErrors(foo, 10);
free(foo);
HARMFUL_ACCESS(foo[5] = 0, "heap-use-after-free");
}
TEST(ToolsSanityTest, MAYBE_ArrayDeletedWithoutBraces) {
#if !defined(ADDRESS_SANITIZER) && !defined(SYZYASAN)
if (!RunningOnValgrind())
return;
#endif
int* volatile foo = new int[10];
delete foo;
}
TEST(ToolsSanityTest, MAYBE_SingleElementDeletedWithBraces) {
#if !defined(ADDRESS_SANITIZER)
if (!RunningOnValgrind())
return;
#endif
int* volatile foo = new int;
(void) foo;
delete [] foo;
}
#if defined(ADDRESS_SANITIZER) || defined(SYZYASAN)
TEST(ToolsSanityTest, DISABLED_AddressSanitizerNullDerefCrashTest) {
int* volatile zero = NULL;
*zero = 0;
}
TEST(ToolsSanityTest, DISABLED_AddressSanitizerLocalOOBCrashTest) {
int array[5];
int* volatile access = &array[5];
*access = 43;
}
namespace {
int g_asan_test_global_array[10];
}
TEST(ToolsSanityTest, DISABLED_AddressSanitizerGlobalOOBCrashTest) {
int* volatile access = g_asan_test_global_array - 1;
*access = 43;
}
#endif
namespace {
class TOOLS_SANITY_TEST_CONCURRENT_THREAD : public PlatformThread::Delegate {
public:
explicit TOOLS_SANITY_TEST_CONCURRENT_THREAD(bool *value) : value_(value) {}
virtual ~TOOLS_SANITY_TEST_CONCURRENT_THREAD() {}
virtual void ThreadMain() OVERRIDE {
*value_ = true;
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
}
private:
bool *value_;
};
class ReleaseStoreThread : public PlatformThread::Delegate {
public:
explicit ReleaseStoreThread(base::subtle::Atomic32 *value) : value_(value) {}
virtual ~ReleaseStoreThread() {}
virtual void ThreadMain() OVERRIDE {
base::subtle::Release_Store(value_, kMagicValue);
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
}
private:
base::subtle::Atomic32 *value_;
};
class AcquireLoadThread : public PlatformThread::Delegate {
public:
explicit AcquireLoadThread(base::subtle::Atomic32 *value) : value_(value) {}
virtual ~AcquireLoadThread() {}
virtual void ThreadMain() OVERRIDE {
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
base::subtle::Acquire_Load(value_);
}
private:
base::subtle::Atomic32 *value_;
};
void RunInParallel(PlatformThread::Delegate *d1, PlatformThread::Delegate *d2) {
PlatformThreadHandle a;
PlatformThreadHandle b;
PlatformThread::Create(0, d1, &a);
PlatformThread::Create(0, d2, &b);
PlatformThread::Join(a);
PlatformThread::Join(b);
}
}
TEST(ToolsSanityTest, DataRace) {
bool *shared = new bool(false);
TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(shared), thread2(shared);
RunInParallel(&thread1, &thread2);
EXPECT_TRUE(*shared);
delete shared;
}
TEST(ToolsSanityTest, AnnotateBenignRace) {
bool shared = false;
ANNOTATE_BENIGN_RACE(&shared, "Intentional race - make sure doesn't show up");
TOOLS_SANITY_TEST_CONCURRENT_THREAD thread1(&shared), thread2(&shared);
RunInParallel(&thread1, &thread2);
EXPECT_TRUE(shared);
}
TEST(ToolsSanityTest, AtomicsAreIgnored) {
base::subtle::Atomic32 shared = 0;
ReleaseStoreThread thread1(&shared);
AcquireLoadThread thread2(&shared);
RunInParallel(&thread1, &thread2);
EXPECT_EQ(kMagicValue, shared);
}
}