From 19de33f9ca52010094a1c9403bbf56e2ce1a8dea Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 22 Jul 2021 18:16:24 -0700 Subject: [PATCH 1/2] Adds an option to disable jemalloc from cmake While not recommended. It is necessary to allow disabling jemalloc if you want to run asan or tsan --- CMakeLists.txt | 11 ++++++++ External/FEXCore/Source/Utils/Allocator.cpp | 29 ++++++++++----------- Source/Tests/CMakeLists.txt | 6 ++++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fbdf33a10..0fb9eeb87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(ENABLE_VISUAL_DEBUGGER "Enables the visual debugger for compiling" FALSE) option(ENABLE_STRICT_WERROR "Enables stricter -Werror for CI" FALSE) option(ENABLE_WERROR "Enables -Werror" FALSE) option(ENABLE_STATIC_PIE "Enables static-pie build" FALSE) +option(ENABLE_JEMALLOC "Enables jemalloc allocator" TRUE) set (X86_C_COMPILER "x86_64-linux-gnu-gcc" CACHE STRING "c compiler for compiling x86 guest libs") set (X86_CXX_COMPILER "x86_64-linux-gnu-g++" CACHE STRING "c++ compiler for compiling x86 guest libs") @@ -148,6 +149,16 @@ if (ENABLE_TSAN) link_libraries(-fno-omit-frame-pointer -fsanitize=thread) endif() +if (ENABLE_JEMALLOC) + add_definitions(-DENABLE_JEMALLOC=1) +else() + message (STATUS + " jemalloc disabled!\n" + " This is not a recommended configuration!\n" + " This will very explicitly break 32-bit application execution!\n" + " Use at your own risk!") +endif() + set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fno-omit-frame-pointer") set (CMAKE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_LINKER_FLAGS_RELWITHDEBINFO} -fno-omit-frame-pointer") diff --git a/External/FEXCore/Source/Utils/Allocator.cpp b/External/FEXCore/Source/Utils/Allocator.cpp index ff8bf839f..47e345ee9 100644 --- a/External/FEXCore/Source/Utils/Allocator.cpp +++ b/External/FEXCore/Source/Utils/Allocator.cpp @@ -1,7 +1,9 @@ #include "Utils/Allocator/HostAllocator.h" #include #include +#ifdef ENABLE_JEMALLOC #include +#endif #include #include @@ -11,16 +13,24 @@ extern "C" { int fd, off_t offset); typedef int (*munmap_hook_type)(void *addr, size_t length); +#ifdef ENABLE_JEMALLOC extern mmap_hook_type __mmap_hook; extern munmap_hook_type __munmap_hook; +#endif } namespace FEXCore::Allocator { MMAP_Hook mmap {::mmap}; MUNMAP_Hook munmap {::munmap}; +#ifdef ENABLE_JEMALLOC MALLOC_Hook malloc {::je_malloc}; REALLOC_Hook realloc {::je_realloc}; FREE_Hook free {::je_free}; +#else + MALLOC_Hook malloc {::malloc}; + REALLOC_Hook realloc {::realloc}; + FREE_Hook free {::free}; +#endif using GLIBC_MALLOC_Hook = void*(*)(size_t, const void *caller); using GLIBC_REALLOC_Hook = void*(*)(void*, size_t, const void *caller); @@ -46,37 +56,26 @@ namespace FEXCore::Allocator { return Result; } - void *FEX_malloc_hook(size_t size, const void *caller) { - return ::je_malloc(size); - } - - void *FEX_realloc_hook(void *ptr, size_t size, const void *caller) { - return ::je_realloc(ptr, size); - } - - void FEX_free_hook(void *ptr, const void *caller) { - return ::je_free(ptr); - } - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" void SetupHooks() { Alloc64 = Alloc::OSAllocator::Create64BitAllocator(); +#ifdef ENABLE_JEMALLOC __mmap_hook = FEX_mmap; __munmap_hook = FEX_munmap; +#endif FEXCore::Allocator::mmap = FEX_mmap; FEXCore::Allocator::munmap = FEX_munmap; } void ClearHooks() { +#ifdef ENABLE_JEMALLOC __mmap_hook = ::mmap; __munmap_hook = ::munmap; +#endif FEXCore::Allocator::mmap = ::mmap; FEXCore::Allocator::munmap = ::munmap; } #pragma GCC diagnostic pop } - -extern "C" { -} diff --git a/Source/Tests/CMakeLists.txt b/Source/Tests/CMakeLists.txt index 8f1c9b876..6d6c7ce61 100644 --- a/Source/Tests/CMakeLists.txt +++ b/Source/Tests/CMakeLists.txt @@ -1,6 +1,10 @@ add_subdirectory(LinuxSyscalls) -set(LIBS FEX_jemalloc FEXCore Common CommonCore) +if (ENABLE_JEMALLOC) + set (LIBS FEX_jemalloc) +endif() + +list(APPEND LIBS FEXCore Common CommonCore) add_executable(FEXLoader FEXLoader.cpp) target_include_directories(FEXLoader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Source/) From ddcc18277b0579d5679edbb58fcfcddee0ad852c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 22 Jul 2021 18:17:16 -0700 Subject: [PATCH 2/2] Check for valgrind for memory region check If running under valgrind then ignore memory region check. Otherwise you'd need to modify source to get past this check --- Source/Tests/FEXLoader.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Tests/FEXLoader.cpp b/Source/Tests/FEXLoader.cpp index a4227a4c7..85c9ceeb4 100644 --- a/Source/Tests/FEXLoader.cpp +++ b/Source/Tests/FEXLoader.cpp @@ -322,7 +322,9 @@ int main(int argc, char **argv, char **const envp) { #if !(defined(ENABLE_ASAN) && ENABLE_ASAN) // LLVM ASAN maps things to the lower 32bits - if (!CheckMemMapping()) { + // Valgrind also places us in the lower 32-bits + if (!getenv("VALGRIND_LAUNCHER") && + !CheckMemMapping()) { LogMan::Msg::E("[Unsupported] FEX mapped to lower 32bits! Exiting!"); return -1; }