From 52e49e763385e02a8a654e770039e65d7b45844d Mon Sep 17 00:00:00 2001 From: trcrsired Date: Thu, 20 Jun 2024 21:19:38 -0400 Subject: [PATCH] Using c_malloc instead of win32_heapalloc when asan is available. This allows asan to detect heap corruption --- .../fast_io_core_impl/allocation/asan_util.h | 65 ++++++++ include/fast_io_core_impl/allocation/impl.h | 153 +++++++++--------- 2 files changed, 142 insertions(+), 76 deletions(-) create mode 100644 include/fast_io_core_impl/allocation/asan_util.h diff --git a/include/fast_io_core_impl/allocation/asan_util.h b/include/fast_io_core_impl/allocation/asan_util.h new file mode 100644 index 000000000..c3447d217 --- /dev/null +++ b/include/fast_io_core_impl/allocation/asan_util.h @@ -0,0 +1,65 @@ +#pragma once + +/* +Referenced from +https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning +*/ + +namespace fast_io::asan +{ + +enum class asan_status +{ + none, + activate, +#if defined(__SANITIZE_ADDRESS__) + current = activate +#elif defined(__has_feature) +#if __has_feature(address_sanitizer) + current = activate +#else + current = none +#endif +#else + current = none +#endif +}; + +#if 0 +#if defined(_MSC_VER) && !defined(__clang__) +__declspec(dllimport) +#elif (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__)) +[[__gnu__::__dllimport__]] +#endif +#if (__has_cpp_attribute(__gnu__::__cdecl__) && !defined(__WINE__)) +[[__gnu__::__cdecl__]] +#endif +extern void +#if (!__has_cpp_attribute(__gnu__::__cdecl__) && !defined(__WINE__)) && defined(_MSC_VER) + __cdecl +#endif + __asan_poison_memory_region(void const volatile *, ::std::size_t) noexcept +#if defined(__clang__) || defined(__GNUC__) + __asm__("__asan_poison_memory_region") +#endif + ; + +#if defined(_MSC_VER) && !defined(__clang__) +__declspec(dllimport) +#elif (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__)) +[[__gnu__::__dllimport__]] +#endif +#if (__has_cpp_attribute(__gnu__::__cdecl__) && !defined(__WINE__)) +[[__gnu__::__cdecl__]] +#endif +extern void +#if (!__has_cpp_attribute(__gnu__::__cdecl__) && !defined(__WINE__)) && defined(_MSC_VER) + __cdecl +#endif + __asan_unpoison_memory_region(void const volatile *, ::std::size_t) noexcept +#if defined(__clang__) || defined(__GNUC__) + __asm__("__asan_unpoison_memory_region") +#endif + ; +#endif +} // namespace fast_io::asan diff --git a/include/fast_io_core_impl/allocation/impl.h b/include/fast_io_core_impl/allocation/impl.h index fd77a45e2..f189d2458 100644 --- a/include/fast_io_core_impl/allocation/impl.h +++ b/include/fast_io_core_impl/allocation/impl.h @@ -1,76 +1,77 @@ -#pragma once - -#include "common.h" -#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(__WINE__) -#include "win32_heapalloc.h" -#include "nt_rtlheapalloc.h" -#if defined(_MSC_VER) && !defined(__clang__) -#include "msvc/impl.h" -#endif -#endif -#if ((__STDC_HOSTED__ == 1 && (!defined(_GLIBCXX_HOSTED) || _GLIBCXX_HOSTED == 1) && \ - !defined(_LIBCPP_FREESTANDING)) || \ - defined(FAST_IO_ENABLE_HOSTED_FEATURES)) -#include "c_malloc.h" -#if defined(_DEBUG) && defined(_MSC_VER) -#include "wincrt_malloc_dbg.h" -#endif -#endif - -#if (defined(__linux__) && defined(__KERNEL__)) || defined(FAST_IO_USE_LINUX_KERNEL_ALLOCATOR) -#include "linux_kernel.h" -#endif - -#if (defined(FAST_IO_ENABLE_MIMALLOC) || defined(FAST_IO_USE_MIMALLOC)) && (!defined(_MSC_VER) || defined(__clang__)) -#include "mimalloc_driver.h" -#endif - -#include "custom.h" -#include "adapters.h" - -namespace fast_io -{ - -using native_global_allocator = generic_allocator_adapter< -#if defined(FAST_IO_USE_CUSTOM_GLOBAL_ALLOCATOR) - custom_global_allocator -#elif defined(FAST_IO_USE_MIMALLOC) && (!defined(_MSC_VER) || defined(__clang__)) - mimalloc_allocator -#elif (defined(__linux__) && defined(__KERNEL__)) || defined(FAST_IO_USE_LINUX_KERNEL_ALLOCATOR) - linux_kmalloc_allocator -#elif ( \ - (__STDC_HOSTED__ == 1 && (!defined(_GLIBCXX_HOSTED) || _GLIBCXX_HOSTED == 1) && !defined(_LIBCPP_FREESTANDING)) || \ - defined(FAST_IO_ENABLE_HOSTED_FEATURES)) -#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__WINE__) && !defined(FAST_IO_USE_C_MALLOC) -#if defined(_DEBUG) && defined(_MSC_VER) - wincrt_malloc_dbg_allocator -#else - win32_heapalloc_allocator -#endif -#else -#if defined(_DEBUG) && defined(_MSC_VER) - wincrt_malloc_dbg_allocator -#else - c_malloc_allocator -#endif -#endif -#else - custom_global_allocator -#endif - >; - -template -using native_typed_global_allocator = typed_generic_allocator_adapter; - -using native_thread_local_allocator = generic_allocator_adapter< -#if defined(FAST_IO_USE_CUSTOM_THREAD_LOCAL_ALLOCATOR) - custom_thread_local_allocator -#else - native_global_allocator -#endif - >; - -template -using native_typed_thread_local_allocator = typed_generic_allocator_adapter; - -} // namespace fast_io +#pragma once + +#include "common.h" +#include "asan_util.h" +#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(__WINE__) +#include "win32_heapalloc.h" +#include "nt_rtlheapalloc.h" +#if defined(_MSC_VER) && !defined(__clang__) +#include "msvc/impl.h" +#endif +#endif +#if ((__STDC_HOSTED__ == 1 && (!defined(_GLIBCXX_HOSTED) || _GLIBCXX_HOSTED == 1) && \ + !defined(_LIBCPP_FREESTANDING)) || \ + defined(FAST_IO_ENABLE_HOSTED_FEATURES)) +#include "c_malloc.h" +#if defined(_DEBUG) && defined(_MSC_VER) +#include "wincrt_malloc_dbg.h" +#endif +#endif + +#if (defined(__linux__) && defined(__KERNEL__)) || defined(FAST_IO_USE_LINUX_KERNEL_ALLOCATOR) +#include "linux_kernel.h" +#endif + +#if (defined(FAST_IO_ENABLE_MIMALLOC) || defined(FAST_IO_USE_MIMALLOC)) && (!defined(_MSC_VER) || defined(__clang__)) +#include "mimalloc_driver.h" +#endif + +#include "custom.h" +#include "adapters.h" + +namespace fast_io +{ + +using native_global_allocator = generic_allocator_adapter< +#if defined(FAST_IO_USE_CUSTOM_GLOBAL_ALLOCATOR) + custom_global_allocator +#elif defined(FAST_IO_USE_MIMALLOC) && (!defined(_MSC_VER) || defined(__clang__)) + mimalloc_allocator +#elif (defined(__linux__) && defined(__KERNEL__)) || defined(FAST_IO_USE_LINUX_KERNEL_ALLOCATOR) + linux_kmalloc_allocator +#elif ( \ + (__STDC_HOSTED__ == 1 && (!defined(_GLIBCXX_HOSTED) || _GLIBCXX_HOSTED == 1) && !defined(_LIBCPP_FREESTANDING)) || \ + defined(FAST_IO_ENABLE_HOSTED_FEATURES)) +#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__WINE__) && !defined(FAST_IO_USE_C_MALLOC) +#if defined(_DEBUG) && defined(_MSC_VER) + wincrt_malloc_dbg_allocator +#else + ::std::conditional_t<::fast_io::asan::asan_status::current == ::fast_io::asan::asan_status::none, win32_heapalloc_allocator, c_malloc_allocator> +#endif +#else +#if defined(_DEBUG) && defined(_MSC_VER) + wincrt_malloc_dbg_allocator +#else + c_malloc_allocator +#endif +#endif +#else + custom_global_allocator +#endif + >; + +template +using native_typed_global_allocator = typed_generic_allocator_adapter; + +using native_thread_local_allocator = generic_allocator_adapter< +#if defined(FAST_IO_USE_CUSTOM_THREAD_LOCAL_ALLOCATOR) + custom_thread_local_allocator +#else + native_global_allocator +#endif + >; + +template +using native_typed_thread_local_allocator = typed_generic_allocator_adapter; + +} // namespace fast_io