1 //===-- hwasan_new_delete.cpp ---------------------------------------------===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of HWAddressSanitizer.
10 //
11 // Interceptors for operators new and delete.
12 //===----------------------------------------------------------------------===//
13 
14 #include "hwasan.h"
15 #include "interception/interception.h"
16 #include "sanitizer_common/sanitizer_allocator.h"
17 #include "sanitizer_common/sanitizer_allocator_report.h"
18 
19 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
20 
21 #include <stddef.h>
22 
23 using namespace __hwasan;
24 
25 // Fake std::nothrow_t to avoid including <new>.
26 namespace std {
27   struct nothrow_t {};
28 }  // namespace std
29 
30 
31 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
32 #define OPERATOR_NEW_BODY(nothrow) \
33   GET_MALLOC_STACK_TRACE; \
34   void *res = hwasan_malloc(size, &stack);\
35   if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
36   return res
37 
38 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
39 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
40 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
41 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
42 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
43 void *operator new(size_t size, std::nothrow_t const&) {
44   OPERATOR_NEW_BODY(true /*nothrow*/);
45 }
46 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
47 void *operator new[](size_t size, std::nothrow_t const&) {
48   OPERATOR_NEW_BODY(true /*nothrow*/);
49 }
50 
51 #define OPERATOR_DELETE_BODY \
52   GET_MALLOC_STACK_TRACE; \
53   if (ptr) hwasan_free(ptr, &stack)
54 
55 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
56 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
57 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
58 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
59 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
60 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
61 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
62 void operator delete[](void *ptr, std::nothrow_t const&) {
63   OPERATOR_DELETE_BODY;
64 }
65 
66 #endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
67