This source file includes following definitions.
- RUN_ALL_TESTS
- Sleep
- TestHookList_Traverse
- TestHookList_Add
- TestHookList_Remove
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- MultithreadedTestThread
- MultithreadedTestThreadRunner
- TEST
- MmapReplacement
- MunmapReplacement
- TEST
- main
#include "config_for_unittests.h"
#include <assert.h>
#include <stdio.h>
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <algorithm>
#include <string>
#include <vector>
#include <gperftools/malloc_hook.h>
#include "malloc_hook-inl.h"
#include "base/logging.h"
#include "base/simple_mutex.h"
#include "base/sysinfo.h"
#include "tests/testutil.h"
#ifndef MAP_ANONYMOUS
# define MAP_ANONYMOUS MAP_ANON
#endif
namespace {
using std::string;
using std::vector;
vector<void (*)()> g_testlist;
#define TEST(a, b) \
struct Test_##a##_##b { \
Test_##a##_##b() { g_testlist.push_back(&Run); } \
static void Run(); \
}; \
static Test_##a##_##b g_test_##a##_##b; \
void Test_##a##_##b::Run()
static int RUN_ALL_TESTS() {
vector<void (*)()>::const_iterator it;
for (it = g_testlist.begin(); it != g_testlist.end(); ++it) {
(*it)();
}
fprintf(stderr, "\nPassed %d tests\n\nPASS\n",
static_cast<int>(g_testlist.size()));
return 0;
}
void Sleep(int seconds) {
#ifdef _MSC_VER
_sleep(seconds * 1000);
#else
sleep(seconds);
#endif
}
using std::min;
using base::internal::kHookListMaxValues;
typedef base::internal::HookList<MallocHook::NewHook> TestHookList;
int TestHookList_Traverse(const TestHookList& list, int* output_array, int n) {
MallocHook::NewHook values_as_hooks[kHookListMaxValues];
int result = list.Traverse(values_as_hooks, min(n, kHookListMaxValues));
for (int i = 0; i < result; ++i) {
output_array[i] = reinterpret_cast<const int&>(values_as_hooks[i]);
}
return result;
}
bool TestHookList_Add(TestHookList* list, int val) {
return list->Add(reinterpret_cast<MallocHook::NewHook>(val));
}
bool TestHookList_Remove(TestHookList* list, int val) {
return list->Remove(reinterpret_cast<MallocHook::NewHook>(val));
}
#define INIT_HOOK_LIST(initial_value) { 1, { initial_value } }
TEST(HookListTest, InitialValueExists) {
TestHookList list = INIT_HOOK_LIST(69);
int values[2] = { 0, 0 };
EXPECT_EQ(1, TestHookList_Traverse(list, values, 2));
EXPECT_EQ(69, values[0]);
EXPECT_EQ(1, list.priv_end);
}
TEST(HookListTest, CanRemoveInitialValue) {
TestHookList list = INIT_HOOK_LIST(69);
ASSERT_TRUE(TestHookList_Remove(&list, 69));
EXPECT_EQ(0, list.priv_end);
int values[2] = { 0, 0 };
EXPECT_EQ(0, TestHookList_Traverse(list, values, 2));
}
TEST(HookListTest, AddAppends) {
TestHookList list = INIT_HOOK_LIST(69);
ASSERT_TRUE(TestHookList_Add(&list, 42));
EXPECT_EQ(2, list.priv_end);
int values[2] = { 0, 0 };
EXPECT_EQ(2, TestHookList_Traverse(list, values, 2));
EXPECT_EQ(69, values[0]);
EXPECT_EQ(42, values[1]);
}
TEST(HookListTest, RemoveWorksAndWillClearSize) {
TestHookList list = INIT_HOOK_LIST(69);
ASSERT_TRUE(TestHookList_Add(&list, 42));
ASSERT_TRUE(TestHookList_Remove(&list, 69));
EXPECT_EQ(2, list.priv_end);
int values[2] = { 0, 0 };
EXPECT_EQ(1, TestHookList_Traverse(list, values, 2));
EXPECT_EQ(42, values[0]);
ASSERT_TRUE(TestHookList_Remove(&list, 42));
EXPECT_EQ(0, list.priv_end);
EXPECT_EQ(0, TestHookList_Traverse(list, values, 2));
}
TEST(HookListTest, AddPrependsAfterRemove) {
TestHookList list = INIT_HOOK_LIST(69);
ASSERT_TRUE(TestHookList_Add(&list, 42));
ASSERT_TRUE(TestHookList_Remove(&list, 69));
EXPECT_EQ(2, list.priv_end);
ASSERT_TRUE(TestHookList_Add(&list, 7));
EXPECT_EQ(2, list.priv_end);
int values[2] = { 0, 0 };
EXPECT_EQ(2, TestHookList_Traverse(list, values, 2));
EXPECT_EQ(7, values[0]);
EXPECT_EQ(42, values[1]);
}
TEST(HookListTest, InvalidAddRejected) {
TestHookList list = INIT_HOOK_LIST(69);
EXPECT_FALSE(TestHookList_Add(&list, 0));
int values[2] = { 0, 0 };
EXPECT_EQ(1, TestHookList_Traverse(list, values, 2));
EXPECT_EQ(69, values[0]);
EXPECT_EQ(1, list.priv_end);
}
TEST(HookListTest, FillUpTheList) {
TestHookList list = INIT_HOOK_LIST(69);
int num_inserts = 0;
while (TestHookList_Add(&list, ++num_inserts))
;
EXPECT_EQ(kHookListMaxValues, num_inserts);
EXPECT_EQ(kHookListMaxValues, list.priv_end);
int values[kHookListMaxValues + 1];
EXPECT_EQ(kHookListMaxValues, TestHookList_Traverse(list, values,
kHookListMaxValues));
EXPECT_EQ(69, values[0]);
for (int i = 1; i < kHookListMaxValues; ++i) {
EXPECT_EQ(i, values[i]);
}
}
void MultithreadedTestThread(TestHookList* list, int shift,
int thread_num) {
string message;
char buf[64];
for (int i = 1; i < 1000; ++i) {
int value = (i << shift) + thread_num;
EXPECT_TRUE(TestHookList_Add(list, value));
sched_yield();
int values[kHookListMaxValues + 1];
int num_values = TestHookList_Traverse(*list, values, kHookListMaxValues);
EXPECT_LT(0, num_values);
int value_index;
for (value_index = 0;
value_index < num_values && values[value_index] != value;
++value_index)
;
EXPECT_LT(value_index, num_values);
snprintf(buf, sizeof(buf), "[%d/%d; ", value_index, num_values);
message += buf;
sched_yield();
EXPECT_TRUE(TestHookList_Remove(list, value));
sched_yield();
num_values = TestHookList_Traverse(*list, values, kHookListMaxValues);
for (value_index = 0;
value_index < num_values && values[value_index] != value;
++value_index)
;
EXPECT_EQ(value_index, num_values);
snprintf(buf, sizeof(buf), "%d]", num_values);
message += buf;
sched_yield();
}
fprintf(stderr, "thread %d: %s\n", thread_num, message.c_str());
}
static volatile int num_threads_remaining;
static TestHookList list = INIT_HOOK_LIST(69);
static Mutex threadcount_lock;
void MultithreadedTestThreadRunner(int thread_num) {
{
MutexLock ml(&threadcount_lock);
assert(num_threads_remaining > 0);
--num_threads_remaining;
while (num_threads_remaining > 0) {
threadcount_lock.Unlock();
Sleep(1);
threadcount_lock.Lock();
}
}
int shift = 0;
for (int i = kHookListMaxValues; i > 0; i >>= 1)
shift += 1;
MultithreadedTestThread(&list, shift, thread_num);
}
TEST(HookListTest, MultithreadedTest) {
ASSERT_TRUE(TestHookList_Remove(&list, 69));
ASSERT_EQ(0, list.priv_end);
num_threads_remaining = kHookListMaxValues;
RunManyThreadsWithId(&MultithreadedTestThreadRunner, num_threads_remaining,
1 << 15);
int values[kHookListMaxValues + 1];
EXPECT_EQ(0, TestHookList_Traverse(list, values, kHookListMaxValues));
EXPECT_EQ(0, list.priv_end);
}
#if defined(HAVE_MMAP) && defined(__linux) && \
(defined(__i386__) || defined(__x86_64__) || defined(__PPC__))
int mmap_calls = 0;
int mmap_matching_calls = 0;
int munmap_calls = 0;
int munmap_matching_calls = 0;
const int kMmapMagicFd = 1;
void* const kMmapMagicPointer = reinterpret_cast<void*>(1);
int MmapReplacement(const void* start,
size_t size,
int protection,
int flags,
int fd,
off_t offset,
void** result) {
++mmap_calls;
if (fd == kMmapMagicFd) {
++mmap_matching_calls;
*result = kMmapMagicPointer;
return true;
}
return false;
}
int MunmapReplacement(const void* ptr, size_t size, int* result) {
++munmap_calls;
if (ptr == kMmapMagicPointer) {
++munmap_matching_calls;
*result = 0;
return true;
}
return false;
}
TEST(MallocMookTest, MmapReplacements) {
mmap_calls = mmap_matching_calls = munmap_calls = munmap_matching_calls = 0;
MallocHook::SetMmapReplacement(&MmapReplacement);
MallocHook::SetMunmapReplacement(&MunmapReplacement);
EXPECT_EQ(kMmapMagicPointer, mmap(NULL, 1, PROT_READ, MAP_PRIVATE,
kMmapMagicFd, 0));
EXPECT_EQ(1, mmap_matching_calls);
char* ptr = reinterpret_cast<char*>(
mmap(NULL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
EXPECT_EQ(2, mmap_calls);
EXPECT_EQ(1, mmap_matching_calls);
ASSERT_NE(MAP_FAILED, ptr);
*ptr = 'a';
EXPECT_EQ(0, munmap(kMmapMagicPointer, 1));
EXPECT_EQ(1, munmap_calls);
EXPECT_EQ(1, munmap_matching_calls);
EXPECT_EQ(0, munmap(ptr, 1));
EXPECT_EQ(2, munmap_calls);
EXPECT_EQ(1, munmap_matching_calls);
}
#endif
}
int main(int argc, char** argv) {
return RUN_ALL_TESTS();
}