SharedStruct: Ensure FStructSharedMemory struct memory is aligned according to the wrapped struct's alignment

- Fixes crash on types with > 8 alignment e.g: any types using FTransform etc

#rnx
#jira none
#preflight 61ee79548f38611657d71e2f

#ROBOMERGE-AUTHOR: dan.smith
#ROBOMERGE-SOURCE: CL 18706726 in //UE5/Release-5.0/... via CL 18706729 via CL 18706846
#ROBOMERGE-BOT: UE5 (Release-Engine-Test -> Main) (v903-18687472)

[CL 18706859 by dan smith in ue5-main branch]
This commit is contained in:
dan smith
2022-01-24 05:40:23 -05:00
parent eb811e953f
commit 97cfa256f4
@@ -24,19 +24,29 @@ struct STRUCTUTILS_API FStructSharedMemory
ScriptStruct.DestroyStruct(GetMemory());
}
struct FStructSharedMemoryDeleter
{
FORCEINLINE void operator()(FStructSharedMemory* StructSharedMemory) const
{
FMemory::Free(StructSharedMemory);
}
};
static TSharedPtr<FStructSharedMemory> Create(const UScriptStruct& InScriptStruct, const uint8* InStructMemory = nullptr)
{
const int32 RequiredSize = sizeof(FStructSharedMemory) + InScriptStruct.GetStructureSize();
// Align RequiredSize to InScriptStruct's alignment to effectively add padding in between ScriptStruct and
// StructMemory. GetMemory will then round &StructMemory up past this 'padding' to the nearest aligned address.
const int32 RequiredSize = Align(sizeof(FStructSharedMemory), InScriptStruct.GetMinAlignment()) + InScriptStruct.GetStructureSize();
// Code analysis is unable to understand correctly what we are doing here, so disabling the warning C6386: Buffer overrun while writing to...
CA_SUPPRESS( 6386 )
FStructSharedMemory* StructMemory = new(new uint8[RequiredSize]) FStructSharedMemory(InScriptStruct, InStructMemory);
return MakeShareable(StructMemory);
FStructSharedMemory* StructMemory = new(FMemory::Malloc(RequiredSize, InScriptStruct.GetMinAlignment())) FStructSharedMemory(InScriptStruct, InStructMemory);
return MakeShareable(StructMemory, FStructSharedMemoryDeleter());
}
/** Returns pointer to struct memory. */
/** Returns pointer to aligned struct memory. */
uint8* GetMemory() const
{
return (uint8*)StructMemory;
return Align((uint8*)StructMemory, ScriptStruct.GetMinAlignment());
}
/** Returns struct type. */
@@ -49,11 +59,11 @@ private:
FStructSharedMemory(const UScriptStruct& InScriptStruct, const uint8* InStructMemory = nullptr)
: ScriptStruct(InScriptStruct)
{
ScriptStruct.InitializeStruct(StructMemory);
ScriptStruct.InitializeStruct(GetMemory());
if (InStructMemory)
{
ScriptStruct.CopyScriptStruct(StructMemory, InStructMemory);
ScriptStruct.CopyScriptStruct(GetMemory(), InStructMemory);
}
}