Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_malloc_win_thunk.cpp
213766 views
//===-- asan_malloc_win_thunk.cpp1//-----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of AddressSanitizer, an address sanity checker.10//11// Windows-specific malloc interception.12// This is included statically for projects statically linking13// with the C Runtime (/MT, /MTd) in order to provide ASAN-aware14// versions of the C allocation functions.15//===----------------------------------------------------------------------===//1617#ifdef SANITIZER_STATIC_RUNTIME_THUNK18# include "..\sanitizer_common\sanitizer_allocator_interface.h"19// #include "asan_win_thunk_common.h"2021// Preserve stack traces with noinline.22# define STATIC_MALLOC_INTERFACE __declspec(noinline)2324extern "C" {25__declspec(dllimport) size_t __cdecl __asan_msize(void *ptr);26__declspec(dllimport) void __cdecl __asan_free(void *const ptr);27__declspec(dllimport) void *__cdecl __asan_malloc(const size_t size);28__declspec(dllimport) void *__cdecl __asan_calloc(const size_t nmemb,29const size_t size);30__declspec(dllimport) void *__cdecl __asan_realloc(void *const ptr,31const size_t size);32__declspec(dllimport) void *__cdecl __asan_recalloc(void *const ptr,33const size_t nmemb,34const size_t size);3536// Avoid tailcall optimization to preserve stack frames.37# pragma optimize("", off)3839// _msize40STATIC_MALLOC_INTERFACE size_t _msize(void *ptr) { return __asan_msize(ptr); }4142STATIC_MALLOC_INTERFACE size_t _msize_base(void *ptr) {43return __asan_msize(ptr);44}4546STATIC_MALLOC_INTERFACE size_t _msize_dbg(void *ptr) {47return __asan_msize(ptr);48}4950// free51STATIC_MALLOC_INTERFACE void free(void *const ptr) { return __asan_free(ptr); }5253STATIC_MALLOC_INTERFACE void _free_base(void *const ptr) {54return __asan_free(ptr);55}5657STATIC_MALLOC_INTERFACE void _free_dbg(void *const ptr) {58return __asan_free(ptr);59}6061// malloc62STATIC_MALLOC_INTERFACE void *malloc(const size_t size) {63return __asan_malloc(size);64}6566STATIC_MALLOC_INTERFACE void *_malloc_base(const size_t size) {67return __asan_malloc(size);68}6970STATIC_MALLOC_INTERFACE void *_malloc_dbg(const size_t size) {71return __asan_malloc(size);72}7374// calloc75STATIC_MALLOC_INTERFACE void *calloc(const size_t nmemb, const size_t size) {76return __asan_calloc(nmemb, size);77}7879STATIC_MALLOC_INTERFACE void *_calloc_base(const size_t nmemb,80const size_t size) {81return __asan_calloc(nmemb, size);82}8384STATIC_MALLOC_INTERFACE void *_calloc_impl(const size_t nmemb,85const size_t size,86int *const errno_tmp) {87// Provided by legacy msvcrt.88(void)errno_tmp;8990return __asan_calloc(nmemb, size);91}9293STATIC_MALLOC_INTERFACE void *_calloc_dbg(const size_t nmemb, const size_t size,94int, const char *, int) {95return __asan_calloc(nmemb, size);96}9798// realloc99STATIC_MALLOC_INTERFACE void *realloc(void *const ptr, const size_t size) {100return __asan_realloc(ptr, size);101}102103STATIC_MALLOC_INTERFACE void *_realloc_base(void *const ptr,104const size_t size) {105return __asan_realloc(ptr, size);106}107108STATIC_MALLOC_INTERFACE void *_realloc_dbg(void *const ptr, const size_t size,109int, const char *, int) {110return __asan_realloc(ptr, size);111}112113// recalloc114STATIC_MALLOC_INTERFACE void *_recalloc(void *const ptr, const size_t nmemb,115const size_t size) {116return __asan_recalloc(ptr, nmemb, size);117}118119STATIC_MALLOC_INTERFACE void *_recalloc_base(void *const ptr,120const size_t nmemb,121const size_t size) {122return __asan_recalloc(ptr, nmemb, size);123}124125STATIC_MALLOC_INTERFACE void *_recalloc_dbg(void *const ptr, const size_t nmemb,126const size_t size, int,127const char *, int) {128return __asan_recalloc(ptr, nmemb, size);129}130131// expand132STATIC_MALLOC_INTERFACE void *_expand(void *, size_t) {133// _expand is used in realloc-like functions to resize the buffer if possible.134// We don't want memory to stand still while resizing buffers, so return 0.135return nullptr;136}137138STATIC_MALLOC_INTERFACE void *_expand_dbg(void *, size_t, int, const char *,139int) {140return nullptr;141}142143// We need to provide symbols for all the debug CRT functions if we decide to144// provide any. Most of these functions make no sense under ASan and so we145// make them no-ops.146long _CrtSetBreakAlloc(long const) { return ~0; }147148void _CrtSetDbgBlockType(void *const, int const) { return; }149150typedef int(__cdecl *CRT_ALLOC_HOOK)(int, void *, size_t, int, long,151const unsigned char *, int);152153CRT_ALLOC_HOOK _CrtGetAllocHook() { return nullptr; }154155CRT_ALLOC_HOOK _CrtSetAllocHook(CRT_ALLOC_HOOK const hook) { return hook; }156157int _CrtCheckMemory() { return 1; }158159int _CrtSetDbgFlag(int const new_bits) { return new_bits; }160161typedef void (*CrtDoForAllClientObjectsCallback)(void *, void *);162163void _CrtDoForAllClientObjects(CrtDoForAllClientObjectsCallback const,164void *const) {165return;166}167168int _CrtIsValidPointer(void const *const p, unsigned int const, int const) {169return p != nullptr;170}171172int _CrtIsValidHeapPointer(void const *const block) {173if (!block) {174return 0;175}176177return __sanitizer_get_ownership(block);178}179180int _CrtIsMemoryBlock(void const *const, unsigned const, long *const,181char **const, int *const) {182return 0;183}184185int _CrtReportBlockType(void const *const) { return -1; }186187typedef void(__cdecl *CRT_DUMP_CLIENT)(void *, size_t);188189CRT_DUMP_CLIENT _CrtGetDumpClient() { return nullptr; }190191CRT_DUMP_CLIENT _CrtSetDumpClient(CRT_DUMP_CLIENT new_client) {192return new_client;193}194195void _CrtMemCheckpoint(void *const) { return; }196197int _CrtMemDifference(void *const, void const *const, void const *const) {198return 0;199}200201void _CrtMemDumpAllObjectsSince(void const *const) { return; }202203int _CrtDumpMemoryLeaks() { return 0; }204205void _CrtMemDumpStatistics(void const *const) { return; }206207int _crtDbgFlag{0};208long _crtBreakAlloc{-1};209CRT_DUMP_CLIENT _pfnDumpClient{nullptr};210211int *__p__crtDbgFlag() { return &_crtDbgFlag; }212213long *__p__crtBreakAlloc() { return &_crtBreakAlloc; }214215// TODO: These were added upstream but conflict with definitions in ucrtbased.216// int _CrtDbgReport(int, const char *, int, const char *, const char *, ...) {217// ShowStatsAndAbort();218// }219//220// int _CrtDbgReportW(int reportType, const wchar_t *, int, const wchar_t *,221// const wchar_t *, ...) {222// ShowStatsAndAbort();223// }224//225// int _CrtSetReportMode(int, int) { return 0; }226227} // extern "C"228#endif // SANITIZER_STATIC_RUNTIME_THUNK229230231