2012-12-11 20:27:27 +08:00
|
|
|
//===-- msan_new_delete.cc ------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-11 20:27:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of MemorySanitizer.
|
|
|
|
//
|
|
|
|
// Interceptors for operators new and delete.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "msan.h"
|
2014-11-18 18:33:15 +08:00
|
|
|
#include "interception/interception.h"
|
2017-06-29 05:58:57 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
2018-06-09 07:31:42 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_report.h"
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-04-15 20:41:52 +08:00
|
|
|
#if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
|
|
|
|
|
2012-12-11 20:27:27 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
using namespace __msan; // NOLINT
|
|
|
|
|
2017-12-23 01:02:17 +08:00
|
|
|
// Fake std::nothrow_t and std::align_val_t to avoid including <new>.
|
2012-12-11 20:27:27 +08:00
|
|
|
namespace std {
|
|
|
|
struct nothrow_t {};
|
2017-12-23 01:02:17 +08:00
|
|
|
enum class align_val_t: size_t {};
|
2012-12-11 20:27:27 +08:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
|
2017-06-29 05:58:57 +08:00
|
|
|
// TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
|
|
|
|
#define OPERATOR_NEW_BODY(nothrow) \
|
2012-12-11 20:27:27 +08:00
|
|
|
GET_MALLOC_STACK_TRACE; \
|
2017-07-19 03:11:04 +08:00
|
|
|
void *res = msan_malloc(size, &stack);\
|
2018-06-09 07:31:42 +08:00
|
|
|
if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
|
2017-06-29 05:58:57 +08:00
|
|
|
return res
|
2017-12-23 01:02:17 +08:00
|
|
|
#define OPERATOR_NEW_BODY_ALIGN(nothrow) \
|
|
|
|
GET_MALLOC_STACK_TRACE;\
|
|
|
|
void *res = msan_memalign((uptr)align, size, &stack);\
|
2018-06-09 07:31:42 +08:00
|
|
|
if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
|
2017-12-23 01:02:17 +08:00
|
|
|
return res;
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2017-06-29 05:58:57 +08:00
|
|
|
void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2017-06-29 05:58:57 +08:00
|
|
|
void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2017-06-29 05:58:57 +08:00
|
|
|
void *operator new(size_t size, std::nothrow_t const&) {
|
|
|
|
OPERATOR_NEW_BODY(true /*nothrow*/);
|
|
|
|
}
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2017-06-29 05:58:57 +08:00
|
|
|
void *operator new[](size_t size, std::nothrow_t const&) {
|
|
|
|
OPERATOR_NEW_BODY(true /*nothrow*/);
|
|
|
|
}
|
2017-12-23 01:02:17 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void *operator new(size_t size, std::align_val_t align)
|
|
|
|
{ OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void *operator new[](size_t size, std::align_val_t align)
|
|
|
|
{ OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
|
|
|
|
{ OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
|
|
|
|
{ OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
|
2012-12-11 20:27:27 +08:00
|
|
|
|
|
|
|
#define OPERATOR_DELETE_BODY \
|
2013-09-16 19:03:31 +08:00
|
|
|
GET_MALLOC_STACK_TRACE; \
|
|
|
|
if (ptr) MsanDeallocate(&stack, ptr)
|
2012-12-11 20:27:27 +08:00
|
|
|
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2015-08-10 23:24:22 +08:00
|
|
|
void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2015-08-10 23:24:22 +08:00
|
|
|
void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-12-11 20:27:27 +08:00
|
|
|
void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
|
2013-12-20 21:18:07 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
2012-12-11 20:27:27 +08:00
|
|
|
void operator delete[](void *ptr, std::nothrow_t const&) {
|
|
|
|
OPERATOR_DELETE_BODY;
|
|
|
|
}
|
2017-12-23 01:02:17 +08:00
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete(void *ptr, size_t size) NOEXCEPT { OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete[](void *ptr, size_t size) NOEXCEPT
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete(void *ptr, std::align_val_t align) NOEXCEPT
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const&)
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const&)
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete(void *ptr, size_t size, std::align_val_t align) NOEXCEPT
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
INTERCEPTOR_ATTRIBUTE
|
|
|
|
void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT
|
|
|
|
{ OPERATOR_DELETE_BODY; }
|
|
|
|
|
2013-04-15 20:41:52 +08:00
|
|
|
|
|
|
|
#endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
|