Randomization in lfalloc

This commit is contained in:
Danila Kutenin 2019-04-13 17:45:01 +03:00
parent 6cfe80ba90
commit 18a6b3123f
4 changed files with 12 additions and 2 deletions

View File

@ -3,6 +3,7 @@ if (NOT SANITIZE AND NOT ARCH_ARM AND NOT ARCH_32 AND NOT ARCH_PPC64LE)
message (FATAL_ERROR "submodule contrib/lfalloc is missing. to fix try run: \n git submodule update --init --recursive") message (FATAL_ERROR "submodule contrib/lfalloc is missing. to fix try run: \n git submodule update --init --recursive")
endif() endif()
set (USE_LFALLOC 1) set (USE_LFALLOC 1)
set (USE_LFALLOC_RANDOM_HINT 1)
set (LFALLOC_INCLUDE_DIR ${ClickHouse_SOURCE_DIR}/contrib/lfalloc/src) set (LFALLOC_INCLUDE_DIR ${ClickHouse_SOURCE_DIR}/contrib/lfalloc/src)
message (STATUS "Using lfalloc=${USE_LFALLOC}: ${LFALLOC_INCLUDE_DIR}") message (STATUS "Using lfalloc=${USE_LFALLOC}: ${LFALLOC_INCLUDE_DIR}")
endif () endif ()

View File

@ -8,6 +8,7 @@
#include "util/system/compiler.h" #include "util/system/compiler.h"
#include "util/system/types.h" #include "util/system/types.h"
#include <random>
#ifdef _MSC_VER #ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
@ -137,7 +138,7 @@ inline T* DoCas(T* volatile* target, T* exchange, T* compare) {
} }
#ifdef _64_ #ifdef _64_
const uintptr_t N_MAX_WORKSET_SIZE = 0x100000000ll * 200; const uintptr_t N_MAX_WORKSET_SIZE = 0x700000000000ll;
const uintptr_t N_HUGE_AREA_FINISH = 0x700000000000ll; const uintptr_t N_HUGE_AREA_FINISH = 0x700000000000ll;
#ifndef _freebsd_ #ifndef _freebsd_
const uintptr_t LINUX_MMAP_AREA_START = 0x100000000ll; const uintptr_t LINUX_MMAP_AREA_START = 0x100000000ll;
@ -345,8 +346,14 @@ static char* AllocWithMMap(uintptr_t sz, EMMapMode mode) {
if (Y_UNLIKELY(uintptr_t(((char*)largeBlock - ALLOC_START) + sz) >= N_MAX_WORKSET_SIZE)) if (Y_UNLIKELY(uintptr_t(((char*)largeBlock - ALLOC_START) + sz) >= N_MAX_WORKSET_SIZE))
NMalloc::AbortFromCorruptedAllocator(); // out of working set, something has broken NMalloc::AbortFromCorruptedAllocator(); // out of working set, something has broken
#else #else
#if defined(_freebsd_) || !defined(_64_) #if defined(_freebsd_) || !defined(_64_) || defined(USE_LFALLOC_RANDOM_HINT)
#if defined(USE_LFALLOC_RANDOM_HINT)
static thread_local std::mt19937_64 generator(std::random_device{}());
std::uniform_int_distribution<intptr_t> distr(0x100000000000UL, N_MAX_WORKSET_SIZE / 2);
char* largeBlock = (char*)mmap(reinterpret_cast<void*>(distr(generator)), sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
#else
char* largeBlock = (char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); char* largeBlock = (char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
#endif
VerifyMmapResult(largeBlock); VerifyMmapResult(largeBlock);
if (Y_UNLIKELY(uintptr_t(((char*)largeBlock - ALLOC_START) + sz) >= N_MAX_WORKSET_SIZE)) if (Y_UNLIKELY(uintptr_t(((char*)largeBlock - ALLOC_START) + sz) >= N_MAX_WORKSET_SIZE))
NMalloc::AbortFromCorruptedAllocator(); // out of working set, something has broken NMalloc::AbortFromCorruptedAllocator(); // out of working set, something has broken

View File

@ -26,6 +26,7 @@
#cmakedefine01 USE_SSL #cmakedefine01 USE_SSL
#cmakedefine01 USE_HYPERSCAN #cmakedefine01 USE_HYPERSCAN
#cmakedefine01 USE_LFALLOC #cmakedefine01 USE_LFALLOC
#cmakedefine01 USE_LFALLOC_RANDOM_HINT
#cmakedefine01 CLICKHOUSE_SPLIT_BINARY #cmakedefine01 CLICKHOUSE_SPLIT_BINARY
#cmakedefine01 LLVM_HAS_RTTI #cmakedefine01 LLVM_HAS_RTTI

View File

@ -58,6 +58,7 @@ const char * auto_config_build[]
"USE_SSL", "@USE_SSL@", "USE_SSL", "@USE_SSL@",
"USE_HYPERSCAN", "@USE_HYPERSCAN@", "USE_HYPERSCAN", "@USE_HYPERSCAN@",
"USE_LFALLOC", "@USE_LFALLOC@", "USE_LFALLOC", "@USE_LFALLOC@",
"USE_LFALLOC_RANDOM_HINT", "@USE_LFALLOC_RANDOM_HINT@",
nullptr, nullptr nullptr, nullptr
}; };