This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- signed_test_size_
- SetUp
- TearDown
- SetUpInDeathAssert
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- TEST_F
- TEST_F
#define _CRT_SECURE_NO_WARNINGS
#include "base/process/memory.h"
#include <limits>
#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#if defined(OS_POSIX)
#include <errno.h>
#endif
#if defined(OS_MACOSX)
#include <malloc/malloc.h>
#include "base/process/memory_unittest_mac.h"
#endif
#if defined(OS_LINUX)
#include <malloc.h>
#endif
#if defined(OS_WIN)
typedef BOOL (WINAPI* HeapQueryFn) \
(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T);
const int kConstantInModule = 42;
TEST(ProcessMemoryTest, GetModuleFromAddress) {
EXPECT_EQ(::GetModuleHandle(NULL),
base::GetModuleFromAddress(
const_cast<int*>(&kConstantInModule)));
HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll");
HMODULE kernel32_from_address =
base::GetModuleFromAddress(reinterpret_cast<DWORD*>(kernel32) + 1);
EXPECT_EQ(kernel32, kernel32_from_address);
}
TEST(ProcessMemoryTest, EnableLFH) {
ASSERT_TRUE(base::EnableLowFragmentationHeap());
if (IsDebuggerPresent()) {
const char* no_debug_env = getenv("_NO_DEBUG_HEAP");
if (!no_debug_env || strcmp(no_debug_env, "1"))
return;
}
HMODULE kernel32 = GetModuleHandle(L"kernel32.dll");
ASSERT_TRUE(kernel32 != NULL);
HeapQueryFn heap_query = reinterpret_cast<HeapQueryFn>(GetProcAddress(
kernel32,
"HeapQueryInformation"));
if (heap_query == NULL)
return;
HANDLE heaps[1024] = { 0 };
unsigned number_heaps = GetProcessHeaps(1024, heaps);
EXPECT_GT(number_heaps, 0u);
for (unsigned i = 0; i < number_heaps; ++i) {
ULONG flag = 0;
SIZE_T length;
ASSERT_NE(0, heap_query(heaps[i],
HeapCompatibilityInformation,
&flag,
sizeof(flag),
&length));
EXPECT_LE(flag, 2u);
EXPECT_NE(flag, 1u);
}
}
#endif
#if defined(OS_MACOSX)
#if !defined(ADDRESS_SANITIZER)
TEST(ProcessMemoryTest, MacMallocFailureDoesNotTerminate) {
void* buf = NULL;
ASSERT_EXIT(
{
base::EnableTerminationOnOutOfMemory();
buf = malloc(std::numeric_limits<size_t>::max() - (2 * PAGE_SIZE) - 1);
},
testing::KilledBySignal(SIGTRAP),
"\\*\\*\\* error: can't allocate region.*\\n?.*");
base::debug::Alias(buf);
}
#endif
TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) {
char buf[9];
asm("" : "=r" (buf));
#if ARCH_CPU_64_BITS
ASSERT_DEATH(free(buf), "");
#elif defined(ADDRESS_SANITIZER)
ASSERT_DEATH(free(buf), "attempting free on address which "
"was not malloc\\(\\)-ed");
#else
ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*"
"\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*"
"Terminating process due to a potential for future heap corruption");
#endif
}
#endif
#if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
!defined(OS_WIN)
#if defined(USE_TCMALLOC)
extern "C" {
int tc_set_new_mode(int mode);
}
#endif
class OutOfMemoryTest : public testing::Test {
public:
OutOfMemoryTest()
: value_(NULL),
test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024),
signed_test_size_(std::numeric_limits<ssize_t>::max()) {
}
#if defined(USE_TCMALLOC)
virtual void SetUp() OVERRIDE {
tc_set_new_mode(1);
}
virtual void TearDown() OVERRIDE {
tc_set_new_mode(0);
}
#endif
protected:
void* value_;
size_t test_size_;
ssize_t signed_test_size_;
};
class OutOfMemoryDeathTest : public OutOfMemoryTest {
public:
void SetUpInDeathAssert() {
base::EnableTerminationOnOutOfMemory();
}
};
TEST_F(OutOfMemoryDeathTest, New) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = operator new(test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, NewArray) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = new char[test_size_];
}, "");
}
TEST_F(OutOfMemoryDeathTest, Malloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc(test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, Realloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = realloc(NULL, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, Calloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = calloc(1024, test_size_ / 1024L);
}, "");
}
TEST_F(OutOfMemoryDeathTest, Valloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = valloc(test_size_);
}, "");
}
#if defined(OS_LINUX)
#if PVALLOC_AVAILABLE == 1
TEST_F(OutOfMemoryDeathTest, Pvalloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = pvalloc(test_size_);
}, "");
}
#endif
TEST_F(OutOfMemoryDeathTest, Memalign) {
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = memalign(4, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) {
std::string format = base::StringPrintf("%%%zud", test_size_);
char *value = NULL;
ASSERT_DEATH({
SetUpInDeathAssert();
EXPECT_EQ(-1, asprintf(&value, format.c_str(), 0));
}, "");
}
#endif
#if defined(OS_POSIX) && !defined(OS_ANDROID)
TEST_F(OutOfMemoryDeathTest, Posix_memalign) {
ASSERT_DEATH({
SetUpInDeathAssert();
EXPECT_EQ(ENOMEM, posix_memalign(&value_, 8, test_size_));
}, "");
}
#endif
#if defined(OS_MACOSX)
TEST_F(OutOfMemoryDeathTest, MallocPurgeable) {
malloc_zone_t* zone = malloc_default_purgeable_zone();
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc_zone_malloc(zone, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) {
malloc_zone_t* zone = malloc_default_purgeable_zone();
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc_zone_realloc(zone, NULL, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, CallocPurgeable) {
malloc_zone_t* zone = malloc_default_purgeable_zone();
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc_zone_calloc(zone, 1024, test_size_ / 1024L);
}, "");
}
TEST_F(OutOfMemoryDeathTest, VallocPurgeable) {
malloc_zone_t* zone = malloc_default_purgeable_zone();
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc_zone_valloc(zone, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) {
malloc_zone_t* zone = malloc_default_purgeable_zone();
ASSERT_DEATH({
SetUpInDeathAssert();
value_ = malloc_zone_memalign(zone, 8, test_size_);
}, "");
}
TEST_F(OutOfMemoryDeathTest, CFAllocatorSystemDefault) {
ASSERT_DEATH({
SetUpInDeathAssert();
while ((value_ =
base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}
}, "");
}
TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) {
ASSERT_DEATH({
SetUpInDeathAssert();
while ((value_ =
base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {}
}, "");
}
TEST_F(OutOfMemoryDeathTest, CFAllocatorMallocZone) {
ASSERT_DEATH({
SetUpInDeathAssert();
while ((value_ =
base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}
}, "");
}
#if !defined(ARCH_CPU_64_BITS)
TEST_F(OutOfMemoryDeathTest, PsychoticallyBigObjCObject) {
ASSERT_DEATH({
SetUpInDeathAssert();
while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {}
}, "");
}
#endif
#endif
class OutOfMemoryHandledTest : public OutOfMemoryTest {
public:
static const size_t kSafeMallocSize = 512;
static const size_t kSafeCallocSize = 128;
static const size_t kSafeCallocItems = 4;
virtual void SetUp() {
OutOfMemoryTest::SetUp();
base::EnableTerminationOnOutOfMemory();
}
};
#if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) {
EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_));
EXPECT_TRUE(value_ != NULL);
free(value_);
EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_));
EXPECT_TRUE(value_ == NULL);
}
TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) {
EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_));
EXPECT_TRUE(value_ != NULL);
const char* bytes = static_cast<const char*>(value_);
for (size_t i = 0; i < kSafeMallocSize; ++i)
EXPECT_EQ(0, bytes[i]);
free(value_);
EXPECT_TRUE(
base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_));
EXPECT_TRUE(value_ != NULL);
bytes = static_cast<const char*>(value_);
for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i)
EXPECT_EQ(0, bytes[i]);
free(value_);
EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_));
EXPECT_TRUE(value_ == NULL);
}
#endif
#endif