Skip to content

Commit

Permalink
Using c_malloc instead of win32_heapalloc when asan is available.
Browse files Browse the repository at this point in the history
This allows asan to detect heap corruption
  • Loading branch information
trcrsired committed Jun 21, 2024
1 parent b61f827 commit 52e49e7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 76 deletions.
65 changes: 65 additions & 0 deletions include/fast_io_core_impl/allocation/asan_util.h
Original file line number Diff line number Diff line change
@@ -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
153 changes: 77 additions & 76 deletions include/fast_io_core_impl/allocation/impl.h
Original file line number Diff line number Diff line change
@@ -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 <typename T>
using native_typed_global_allocator = typed_generic_allocator_adapter<native_global_allocator, T>;

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 <typename T>
using native_typed_thread_local_allocator = typed_generic_allocator_adapter<native_thread_local_allocator, T>;

} // 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 <typename T>
using native_typed_global_allocator = typed_generic_allocator_adapter<native_global_allocator, T>;

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 <typename T>
using native_typed_thread_local_allocator = typed_generic_allocator_adapter<native_thread_local_allocator, T>;

} // namespace fast_io

0 comments on commit 52e49e7

Please sign in to comment.