This source file includes following definitions.
- HideValueFromCompiler
- IsTcMallocBypassed
- CallocDiesOnOOM
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- OverflowTestsSoftExpectTrue
- TEST
- CallocReturnsNull
- TEST
- ArePointersToSameArea
- TEST
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <limits>
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include <sys/mman.h>
#include <unistd.h>
#endif
using std::nothrow;
using std::numeric_limits;
namespace {
template <typename Type>
Type HideValueFromCompiler(volatile Type value) {
#if defined(__GNUC__)
__asm__ volatile ("" : "+r" (value));
#endif
return value;
}
#if !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER) && \
!defined(OS_IOS) && !defined(OS_MACOSX) && !defined(SYZYASAN)
#define TCMALLOC_TEST(function) function
#else
#define TCMALLOC_TEST(function) DISABLED_##function
#endif
const size_t kTooBigAllocSize = INT_MAX;
bool IsTcMallocBypassed() {
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
char* g_slice = getenv("G_SLICE");
if (g_slice && !strcmp(g_slice, "always-malloc"))
return true;
#elif defined(OS_WIN)
char* allocator = getenv("CHROME_ALLOCATOR");
if (allocator && strcmp(allocator, "tcmalloc"))
return true;
#endif
return false;
}
bool CallocDiesOnOOM() {
#if !defined(OS_WIN) && (defined(ADDRESS_SANITIZER) || \
defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
(defined(OS_LINUX) && defined(NO_TCMALLOC)))
return true;
#else
return false;
#endif
}
TEST(SecurityTest, TCMALLOC_TEST(IsTCMallocDynamicallyBypassed)) {
printf("Malloc is dynamically bypassed: %s\n",
IsTcMallocBypassed() ? "yes." : "no.");
}
TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsMalloc)) {
if (!IsTcMallocBypassed()) {
scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
HideValueFromCompiler(malloc(kTooBigAllocSize))));
ASSERT_TRUE(!ptr);
}
}
TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsCalloc)) {
if (!IsTcMallocBypassed()) {
scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
HideValueFromCompiler(calloc(kTooBigAllocSize, 1))));
ASSERT_TRUE(!ptr);
}
}
TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsRealloc)) {
if (!IsTcMallocBypassed()) {
char* orig_ptr = static_cast<char*>(malloc(1));
ASSERT_TRUE(orig_ptr);
scoped_ptr<char, base::FreeDeleter> ptr(static_cast<char*>(
HideValueFromCompiler(realloc(orig_ptr, kTooBigAllocSize))));
ASSERT_TRUE(!ptr);
free(orig_ptr);
}
}
typedef struct {
char large_array[kTooBigAllocSize];
} VeryLargeStruct;
TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNew)) {
if (!IsTcMallocBypassed()) {
scoped_ptr<VeryLargeStruct> ptr(
HideValueFromCompiler(new (nothrow) VeryLargeStruct));
ASSERT_TRUE(!ptr);
}
}
TEST(SecurityTest, TCMALLOC_TEST(MemoryAllocationRestrictionsNewArray)) {
if (!IsTcMallocBypassed()) {
scoped_ptr<char[]> ptr(
HideValueFromCompiler(new (nothrow) char[kTooBigAllocSize]));
ASSERT_TRUE(!ptr);
}
}
#if defined(OS_IOS) || defined(OS_WIN) || defined(THREAD_SANITIZER)
#define DISABLE_ON_IOS_AND_WIN_AND_TSAN(function) DISABLED_##function
#else
#define DISABLE_ON_IOS_AND_WIN_AND_TSAN(function) function
#endif
void OverflowTestsSoftExpectTrue(bool overflow_detected) {
if (!overflow_detected) {
#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_MACOSX)
printf("Platform has overflow: %s\n",
!overflow_detected ? "yes." : "no.");
#else
EXPECT_TRUE(overflow_detected);
#endif
}
}
TEST(SecurityTest, DISABLE_ON_IOS_AND_WIN_AND_TSAN(NewOverflow)) {
const size_t kArraySize = 4096;
const size_t kDynamicArraySize = HideValueFromCompiler(kArraySize);
const size_t kMaxSizeT = ~static_cast<size_t>(0);
ASSERT_EQ(numeric_limits<size_t>::max(), kMaxSizeT);
const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
const size_t kDynamicArraySize2 = HideValueFromCompiler(kArraySize2);
{
scoped_ptr<char[][kArraySize]> array_pointer(new (nothrow)
char[kDynamicArraySize2][kArraySize]);
OverflowTestsSoftExpectTrue(!array_pointer);
}
#if !defined(OS_WIN) || !defined(ARCH_CPU_64_BITS)
{
scoped_ptr<char[][kArraySize2]> array_pointer(new (nothrow)
char[kDynamicArraySize][kArraySize2]);
OverflowTestsSoftExpectTrue(!array_pointer);
}
#endif
}
bool CallocReturnsNull(size_t nmemb, size_t size) {
scoped_ptr<char, base::FreeDeleter> array_pointer(
static_cast<char*>(calloc(nmemb, size)));
return HideValueFromCompiler(array_pointer.get()) == NULL;
}
TEST(SecurityTest, CallocOverflow) {
const size_t kArraySize = 4096;
const size_t kMaxSizeT = numeric_limits<size_t>::max();
const size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
if (!CallocDiesOnOOM()) {
EXPECT_TRUE(CallocReturnsNull(kArraySize, kArraySize2));
EXPECT_TRUE(CallocReturnsNull(kArraySize2, kArraySize));
} else {
#if defined(GTEST_HAS_DEATH_TEST)
EXPECT_DEATH(CallocReturnsNull(kArraySize, kArraySize2), "");
EXPECT_DEATH(CallocReturnsNull(kArraySize2, kArraySize), "");
#endif
}
}
#if (defined(OS_LINUX) || defined(OS_CHROMEOS)) && defined(__x86_64__)
bool ArePointersToSameArea(void* ptr1, void* ptr2, size_t size) {
ptrdiff_t ptr_diff = reinterpret_cast<char*>(std::max(ptr1, ptr2)) -
reinterpret_cast<char*>(std::min(ptr1, ptr2));
return static_cast<size_t>(ptr_diff) <= size;
}
TEST(SecurityTest, TCMALLOC_TEST(RandomMemoryAllocations)) {
if (IsTcMallocBypassed())
return;
size_t kPageSize = 4096;
void* default_mmap_heap_address =
mmap(0, kPageSize, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
ASSERT_NE(default_mmap_heap_address,
static_cast<void*>(MAP_FAILED));
ASSERT_EQ(munmap(default_mmap_heap_address, kPageSize), 0);
void* brk_heap_address = sbrk(0);
ASSERT_NE(brk_heap_address, reinterpret_cast<void*>(-1));
ASSERT_TRUE(brk_heap_address != NULL);
size_t kAllocSize = 1<<20;
scoped_ptr<char, base::FreeDeleter> ptr(
static_cast<char*>(malloc(kAllocSize)));
ASSERT_TRUE(ptr != NULL);
const size_t kAreaRadius = 1<<29;
bool in_default_mmap_heap = ArePointersToSameArea(
ptr.get(), default_mmap_heap_address, kAreaRadius);
EXPECT_FALSE(in_default_mmap_heap);
bool in_default_brk_heap = ArePointersToSameArea(
ptr.get(), brk_heap_address, kAreaRadius);
EXPECT_FALSE(in_default_brk_heap);
const uintptr_t kRandomMask = 0x3fffffffffffULL;
bool impossible_random_address =
reinterpret_cast<uintptr_t>(ptr.get()) & ~kRandomMask;
EXPECT_FALSE(impossible_random_address);
}
#endif
}