Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MallocPoisonProxy.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Misc/AssertionMacros.h"
7#include "HAL/MemoryBase.h"
8#include "HAL/UnrealMemory.h"
9
10/** Governs when malloc that poisons the allocations is enabled. */
11#if !defined(UE_USE_MALLOC_FILL_BYTES)
13#endif // !defined(UE_USE_MALLOC_FILL_BYTES)
14
15/** Value that a freed memory block will be filled with when UE_USE_MALLOC_FILL_BYTES is defined. */
16#define UE_DEBUG_FILL_FREED (0xdd)
17
18/** Value that a new memory block will be filled with when UE_USE_MALLOC_FILL_BYTES is defined. */
19#define UE_DEBUG_FILL_NEW (0xcd)
20
21/**
22 * FMalloc proxy that poisons new and freed allocations, helping to catch code that relies on uninitialized or freed memory.
23 */
25{
26private:
27 /** Malloc we're based on, aka using under the hood */
29
30public:
31 // FMalloc interface begin
32 explicit FMallocPoisonProxy(FMalloc* InMalloc)
33 : UsedMalloc(InMalloc)
34 {
35 checkf(UsedMalloc, TEXT("FMallocPoisonProxy is used without a valid malloc!"));
36 }
37
38 virtual void InitializeStatsMetadata() override
39 {
41 }
42
43 virtual void* Malloc(SIZE_T Size, uint32 Alignment) override
44 {
45 void* Result=UsedMalloc->Malloc(Size, Alignment);
46 if (LIKELY(Result != nullptr && Size > 0))
47 {
49 }
50 return Result;
51 }
52
53 virtual void* Realloc(void* Ptr, SIZE_T NewSize, uint32 Alignment) override
54 {
55 // NOTE: case when Realloc returns completely new pointer is not handled properly (we would like to have previous memory poisoned completely).
56 // This can be done by avoiding calling Realloc() of the nested allocator and Malloc()/Free()'ing directly, but this is deemed unacceptable
57 // from a performance/fragmentation standpoint.
58 SIZE_T OldSize = 0;
59 if (Ptr != nullptr && GetAllocationSize(Ptr, OldSize) && OldSize > 0 && OldSize > NewSize)
60 {
61 FMemory::Memset(static_cast<uint8*>(Ptr) + NewSize, UE_DEBUG_FILL_FREED, OldSize - NewSize);
62 }
63
64 void* Result = UsedMalloc->Realloc(Ptr, NewSize, Alignment);
65
66 if (Result != nullptr && OldSize > 0 && OldSize < NewSize)
67 {
68 FMemory::Memset(static_cast<uint8*>(Result) + OldSize, UE_DEBUG_FILL_NEW, NewSize - OldSize);
69 }
70
71 return Result;
72 }
73
74 virtual void Free(void* Ptr) override
75 {
76 if (LIKELY(Ptr))
77 {
78 SIZE_T AllocSize;
79 if (LIKELY(GetAllocationSize(Ptr, AllocSize) && AllocSize > 0))
80 {
82 }
84 }
85 }
86
87 virtual SIZE_T QuantizeSize(SIZE_T Count, uint32 Alignment) override
88 {
89 return UsedMalloc->QuantizeSize(Count, Alignment);
90 }
91
92 virtual void UpdateStats() override
93 {
95 }
96
97 virtual void GetAllocatorStats( FGenericMemoryStats& out_Stats ) override
98 {
100 }
101
102 virtual void DumpAllocatorStats( class FOutputDevice& Ar ) override
103 {
105 }
106
107 virtual bool IsInternallyThreadSafe() const override
108 {
110 }
111
112 virtual bool ValidateHeap() override
113 {
115 }
116
117 virtual bool Exec( UWorld* InWorld, const TCHAR* Cmd, FOutputDevice& Ar ) override
118 {
119 return UsedMalloc->Exec(InWorld, Cmd, Ar);
120 }
121
122 virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut) override
123 {
124 return UsedMalloc->GetAllocationSize(Original,SizeOut);
125 }
126
127 virtual const TCHAR* GetDescriptiveName() override
128 {
130 }
131
132 virtual void Trim(bool bTrimThreadCaches) override
133 {
134 UsedMalloc->Trim(bTrimThreadCaches);
135 }
136
137 virtual void SetupTLSCachesOnCurrentThread() override
138 {
140 }
141
143 {
145 }
146
147 virtual void OnMallocInitialized() override
148 {
150 }
151
152 virtual void OnPreFork() override
153 {
155 }
156
157
158 virtual void OnPostFork() override
159 {
161 }
162
163 // FMalloc interface end
164};
#define checkf(expr, format,...)
#define UE_BUILD_DEVELOPMENT
Definition Build.h:20
#define UE_BUILD_DEBUG
Definition Build.h:17
#define WITH_EDITORONLY_DATA
#define UE_DEBUG_FILL_FREED
#define UE_DEBUG_FILL_NEW
#define USING_ADDRESS_SANITISER
Definition Platform.h:117
#define PLATFORM_USES_FIXED_GMalloc_CLASS
Definition Platform.h:413
#define LIKELY(x)
Definition Platform.h:725
virtual const TCHAR * GetDescriptiveName()
Definition MemoryBase.h:208
virtual void Free(void *Original)=0
virtual void UpdateStats()
virtual void OnMallocInitialized()
Definition MemoryBase.h:216
virtual void OnPreFork()
Definition MemoryBase.h:221
virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut)
Definition MemoryBase.h:133
virtual void * Malloc(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)=0
virtual bool ValidateHeap()
Definition MemoryBase.h:198
virtual void SetupTLSCachesOnCurrentThread()
Definition MemoryBase.h:148
virtual bool IsInternallyThreadSafe() const
Definition MemoryBase.h:190
virtual void GetAllocatorStats(FGenericMemoryStats &out_Stats)
virtual void DumpAllocatorStats(class FOutputDevice &Ar)
Definition MemoryBase.h:181
virtual void Trim(bool bTrimThreadCaches)
Definition MemoryBase.h:141
virtual void * Realloc(void *Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)=0
virtual void OnPostFork()
Definition MemoryBase.h:226
virtual void InitializeStatsMetadata()
virtual void ClearAndDisableTLSCachesOnCurrentThread()
Definition MemoryBase.h:155
virtual SIZE_T QuantizeSize(SIZE_T Count, uint32 Alignment)
Definition MemoryBase.h:121
virtual void DumpAllocatorStats(class FOutputDevice &Ar) override
virtual SIZE_T QuantizeSize(SIZE_T Count, uint32 Alignment) override
virtual const TCHAR * GetDescriptiveName() override
virtual void OnPreFork() override
virtual void InitializeStatsMetadata() override
virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut) override
virtual void GetAllocatorStats(FGenericMemoryStats &out_Stats) override
virtual void * Malloc(SIZE_T Size, uint32 Alignment) override
virtual void SetupTLSCachesOnCurrentThread() override
virtual void OnMallocInitialized() override
virtual void Free(void *Ptr) override
virtual void * Realloc(void *Ptr, SIZE_T NewSize, uint32 Alignment) override
virtual void ClearAndDisableTLSCachesOnCurrentThread() override
FMallocPoisonProxy(FMalloc *InMalloc)
virtual bool Exec(UWorld *InWorld, const TCHAR *Cmd, FOutputDevice &Ar) override
virtual void Trim(bool bTrimThreadCaches) override
virtual bool IsInternallyThreadSafe() const override
virtual void UpdateStats() override
virtual void OnPostFork() override
virtual bool ValidateHeap() override
static FORCEINLINE void * Memset(void *Dest, uint8 Char, SIZE_T Count)