• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_
16 #define PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_
17 
18 #include "libpandabase/mem/mem.h"
19 #include "libpandabase/macros.h"
20 
21 namespace ark::os::unix::mem_hooks {
22 
23 class PandaHooks {
24 public:
25     PANDA_PUBLIC_API static void Initialize();
26     PANDA_PUBLIC_API static void Enable();
27 
28     PANDA_PUBLIC_API static void Disable();
29 
GetAllocViaStandard()30     static size_t GetAllocViaStandard() noexcept
31     {
32         return allocViaStandard_;
33     }
34 
IsActive()35     static bool IsActive() noexcept
36     {
37         return isActive_;
38     }
39 
40     static void *MallocHook(size_t size, const void *caller);
41     static void *MemalignHook(size_t alignment, size_t size, const void *caller);
42     static void FreeHook(void *ptr, const void *caller);
43 
44     static void SaveRealFunctions();
45     static void *(*realMalloc_)(size_t);
46     static void *(*realMemalign_)(size_t, size_t);
47     static void (*realFree_)(void *);
48 
49     class AddrRange {
50     public:
51         AddrRange() = default;
52         ~AddrRange() = default;
53         DEFAULT_COPY_SEMANTIC(AddrRange);
54         DEFAULT_MOVE_SEMANTIC(AddrRange);
55 
AddrRange(uintptr_t base,size_t size)56         AddrRange(uintptr_t base, size_t size) : base_(base), size_(size) {}
57 
Contains(uintptr_t addr)58         bool Contains(uintptr_t addr) const
59         {
60             return base_ <= addr && addr < base_ + size_;
61         }
62 
63     private:
64         uintptr_t base_ {0};
65         size_t size_ {0};
66     };
67 
68 private:
69     static constexpr size_t MAX_ALLOC_VIA_STANDARD = 4_MB;
70 
71     static bool ShouldCountAllocation(const void *caller);
72 
73     static size_t allocViaStandard_;
74     static AddrRange ignoreCodeRange_;
75     static bool isActive_;
76 };
77 
78 }  // namespace ark::os::unix::mem_hooks
79 
80 namespace ark::os::mem_hooks {
81 using PandaHooks = ark::os::unix::mem_hooks::PandaHooks;
82 }  // namespace ark::os::mem_hooks
83 
84 // Don't use for musl and mobile
85 // Sanitizers hook malloc functions, so we don't use memory hooks
86 #if !defined(__MUSL__) && !defined(PANDA_TARGET_ARM64) && !defined(PANDA_TARGET_ARM32) && \
87     !defined(USE_ADDRESS_SANITIZER) && !defined(USE_THREAD_SANITIZER)
88 #define PANDA_USE_MEMORY_HOOKS
89 #endif
90 
91 #ifdef PANDA_USE_MEMORY_HOOKS
92 
93 void *malloc(size_t size) noexcept;                      // NOLINT(readability-redundant-declaration)
94 void *Memalign(size_t alignment, size_t size) noexcept;  // NOLINT(readability-redundant-declaration)
95 void free(void *ptr) noexcept;                           // NOLINT(readability-redundant-declaration)
96 
97 #endif  // PANDA_USE_MEMORY_HOOKS
98 
99 #endif  // PANDA_LIBPANDABASE_OS_UNIX_MEM_HOOKS_H_
100