2012-06-04 21:50:10 +08:00
|
|
|
//===-- asan_malloc_win.cc ------------------------------------------------===//
|
2012-02-07 01:56:38 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Windows-specific malloc interception.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2013-03-19 22:54:17 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2012-02-07 01:56:38 +08:00
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_stack.h"
|
2013-12-20 21:17:31 +08:00
|
|
|
#include "sanitizer_common/sanitizer_interception.h"
|
2012-02-07 01:56:38 +08:00
|
|
|
|
2012-09-24 19:43:40 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2012-02-07 01:56:38 +08:00
|
|
|
// ---------------------- Replacement functions ---------------- {{{1
|
|
|
|
using namespace __asan; // NOLINT
|
|
|
|
|
|
|
|
// FIXME: Simply defining functions with the same signature in *.obj
|
|
|
|
// files overrides the standard functions in *.lib
|
|
|
|
// This works well for simple helloworld-like tests but might need to be
|
|
|
|
// revisited in the future.
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
// The function attributes will be different for -MD CRT.
|
|
|
|
// Just introduce an extra macro for now, it will get a different value under
|
|
|
|
// ASAN_DYNAMIC soon.
|
|
|
|
#define ALLOCATION_FUNCTION_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
|
2012-02-07 01:56:38 +08:00
|
|
|
extern "C" {
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-02-07 01:56:38 +08:00
|
|
|
void free(void *ptr) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_FREE;
|
2012-12-21 16:53:59 +08:00
|
|
|
return asan_free(ptr, &stack, FROM_MALLOC);
|
2012-02-07 01:56:38 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
|
|
|
void _free_dbg(void *ptr, int) {
|
2012-03-23 19:33:02 +08:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
|
|
|
void cfree(void *) {
|
|
|
|
CHECK(!"cfree() should not be used on Windows");
|
2012-03-23 19:33:02 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-02-07 01:56:38 +08:00
|
|
|
void *malloc(size_t size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-02-07 01:56:38 +08:00
|
|
|
return asan_malloc(size, &stack);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
|
|
|
void *_malloc_dbg(size_t size, int, const char *, int) {
|
2012-03-23 19:33:02 +08:00
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-02-07 01:56:38 +08:00
|
|
|
void *calloc(size_t nmemb, size_t size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-02-07 01:56:38 +08:00
|
|
|
return asan_calloc(nmemb, size, &stack);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
|
|
|
void *_calloc_dbg(size_t nmemb, size_t size, int, const char *, int) {
|
|
|
|
return calloc(nmemb, size);
|
2012-03-23 19:33:02 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-03-12 19:45:09 +08:00
|
|
|
void *_calloc_impl(size_t nmemb, size_t size, int *errno_tmp) {
|
2012-03-23 19:33:02 +08:00
|
|
|
return calloc(nmemb, size);
|
2012-03-12 19:45:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-02-07 01:56:38 +08:00
|
|
|
void *realloc(void *ptr, size_t size) {
|
2012-12-13 17:34:23 +08:00
|
|
|
GET_STACK_TRACE_MALLOC;
|
2012-02-07 01:56:38 +08:00
|
|
|
return asan_realloc(ptr, size, &stack);
|
|
|
|
}
|
2012-03-07 19:19:26 +08:00
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-03-23 19:33:02 +08:00
|
|
|
void *_realloc_dbg(void *ptr, size_t size, int) {
|
|
|
|
CHECK(!"_realloc_dbg should not exist!");
|
2012-05-31 22:35:53 +08:00
|
|
|
return 0;
|
2012-03-23 19:33:02 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
|
|
|
void *_recalloc(void *p, size_t n, size_t elem_size) {
|
2012-03-23 19:33:02 +08:00
|
|
|
if (!p)
|
|
|
|
return calloc(n, elem_size);
|
|
|
|
const size_t size = n * elem_size;
|
|
|
|
if (elem_size != 0 && size / elem_size != n)
|
2012-05-31 22:35:53 +08:00
|
|
|
return 0;
|
2012-03-23 19:33:02 +08:00
|
|
|
return realloc(p, size);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2012-03-07 19:19:26 +08:00
|
|
|
size_t _msize(void *ptr) {
|
2013-11-13 22:46:58 +08:00
|
|
|
GET_CURRENT_PC_BP_SP;
|
|
|
|
(void)sp;
|
|
|
|
return asan_malloc_usable_size(ptr, pc, bp);
|
2012-03-07 19:19:26 +08:00
|
|
|
}
|
2012-03-23 19:33:02 +08:00
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2014-03-27 22:01:11 +08:00
|
|
|
void *_expand(void *memblock, size_t size) {
|
|
|
|
// _expand is used in realloc-like functions to resize the buffer if possible.
|
|
|
|
// We don't want memory to stand still while resizing buffers, so return 0.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:08:53 +08:00
|
|
|
ALLOCATION_FUNCTION_ATTRIBUTE
|
2014-03-27 22:01:11 +08:00
|
|
|
void *_expand_dbg(void *memblock, size_t size) {
|
2014-08-16 00:08:53 +08:00
|
|
|
return _expand(memblock, size);
|
2014-03-27 22:01:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(timurrrr): Might want to add support for _aligned_* allocation
|
|
|
|
// functions to detect a bit more bugs. Those functions seem to wrap malloc().
|
|
|
|
|
2012-03-23 19:33:02 +08:00
|
|
|
int _CrtDbgReport(int, const char*, int,
|
|
|
|
const char*, const char*, ...) {
|
|
|
|
ShowStatsAndAbort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int _CrtDbgReportW(int reportType, const wchar_t*, int,
|
|
|
|
const wchar_t*, const wchar_t*, ...) {
|
|
|
|
ShowStatsAndAbort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int _CrtSetReportMode(int, int) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-07 01:56:38 +08:00
|
|
|
} // extern "C"
|
|
|
|
|
2012-02-29 19:43:03 +08:00
|
|
|
namespace __asan {
|
|
|
|
void ReplaceSystemMalloc() {
|
2012-03-12 19:45:09 +08:00
|
|
|
#if defined(_DLL)
|
2014-08-15 20:56:52 +08:00
|
|
|
# error MD CRT is not yet supported, see PR20214.
|
2012-03-12 19:45:09 +08:00
|
|
|
#endif
|
2012-02-29 19:43:03 +08:00
|
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
|
2012-02-07 01:56:38 +08:00
|
|
|
#endif // _WIN32
|