美文网首页Unreal 技术笔记
unreal 项目,IOS下排查错误: Memory mappe

unreal 项目,IOS下排查错误: Memory mappe

作者: 子夜书案 | 来源:发表于2023-09-14 10:30 被阅读0次

现象:

  • 前阵子做了个 repack 安装包的功能(拿可正常运行的包解开,修改某项内容后再封装成新包),一直运行正常,最近 repack 的包在 ios 平台闪退,其它平台(Android,Windows)正常
  • 闪退没有崩溃信息,连接到 xcode 抓报错信息,最后一行错误为:Engine\Source\Runtime\CoreUObject\Private\Serialization\BulkData2.cpp line:1681 Memory mapped file has the wrong alignment!
  • 这错误只在对 develop 模式打出的包 repack 之后出现

调查:

1,错误定位:

报错位置在 Engine\Source\Runtime\CoreUObject\Private\Serialization\BulkData2.cpp line:1681
很明显是个 check 失败,内容如下:

checkf(IsAligned(MappedRegion->GetMappedPtr(), FPlatformProperties::GetMemoryMappingAlignment()), TEXT("Memory mapped file has the wrong alignment!"));

ISAligned 从字面含义来看就是内存是否对齐,具体代码内容如下:

/**
 * Checks if a pointer is aligned to the specified alignment.
 *
 * @param  Val        The value to align.
 * @param  Alignment  The alignment value, must be a power of two.
 *
 * @return true if the pointer is aligned to the specified alignment, false otherwise.
 */
template <typename T>
FORCEINLINE constexpr bool IsAligned(T Val, uint64 Alignment)
{
    static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "IsAligned expects an integer or pointer type");

    return !((uint64)Val & (Alignment - 1));
}

那这个错误基本就是内存没对齐造成的问题,现在只要搞清楚为什么只在 IOS 会报这个错误。

2,错误排查:

通过阅读上面代码,可以得知,对齐方式是通过 FPlatformProperties::GetMemoryMappingAlignment() 来确定的。在 unreal 中,一看到 FPlatform 下意识就会想到,一开始的入口并不是最终的调用入口,下面肯定隐藏着各平台的实现。果不然,在项目中分别找到了各个实现:

  • Engine\Source\Runtime\Core\Public\GenericPlatform\GenericPlatformProperties.h
  • Engine\Source\Runtime\Core\Public\IOS\IOSPlatformProperties.h
  • Engine\Source\Runtime\Core\Public\Android\AndroidPlatformProperties.h
  • Engine\Source\Runtime\Core\Public\Windows\WindowsPlatformProperties.h
    在除 GenericPlatformProperties.h 外的各个实现里,都有类似的如下定义:
#ifdef PROPERTY_HEADER_SHOULD_DEFINE_TYPE
typedef FIOSPlatformProperties FPlatformProperties;
#endif

PROPERTY_HEADER_SHOULD_DEFINE_TYPE 定义在 Engine\Source\Runtime\Core\Public\HAL\PlatformProperties.h

// note that this is not defined to 1 like normal, because we don't want to have to define it to 0 whenever
// the Properties.h files are included in all other places, so just use #ifdef not #if in this special case
#define PROPERTY_HEADER_SHOULD_DEFINE_TYPE

完整结合起来的含义就是:

  • typedef FIOSPlatformProperties FPlatformProperties;:这行代码在满足条件(即已经定义了PROPERTY_HEADER_SHOULD_DEFINE_TYPE)时执行。它定义了一个类型别名:将FIOSPlatformProperties重命名为FPlatformProperties,使得在后续的代码中可以用FPlatformProperties来代替FIOSPlatformProperties。

那么 IOS 平台下,GetMemoryMappingAlignment 是怎么定义的?代码在 Engine\Source\Runtime\Core\Public\IOS\IOSPlatformProperties.h line:83:

static FORCEINLINE int64 GetMemoryMappingAlignment()
{
    return 16384;
}

对,你没看出,就是写死在代码中,固定为 16384,16进制表示则为:0X4000
其它平台没有单独实现,调用的是默认值 0 。

3,unreal pak 流程中是怎么处理这个问题的?

问题找到这就很好奇,unreal 的 pak 流程中是怎么处理这个对齐问题的,于是翻开项目,找到了 Engine\Source\Programs\AutomationTool\Scripts\CopyBuildToStagingDirectory.Automation.cs文件,在其中搜索 MemoryMappedFiles便能找到这么段代码:

string BulkOption = "";
{
    ConfigHierarchy PlatformEngineConfig;
    if (Params.EngineConfigs.TryGetValue(SC.StageTargetPlatform.PlatformType, out PlatformEngineConfig))
    {
        bool bMasterEnable = false;
        PlatformEngineConfig.GetBool("MemoryMappedFiles", "MasterEnable", out bMasterEnable);
        if (bMasterEnable)
        {
            int Value = 0;
            PlatformEngineConfig.GetInt32("MemoryMappedFiles", "Alignment", out Value);
            if (Value > 0)
            {
                BulkOption = String.Format(" -AlignForMemoryMapping={0}", Value);
            }
        }
    }
}

然后在Engine\Config\IOS\IOSEngine.ini中能找到这样的定义:

[MemoryMappedFiles]
MasterEnable=true
Alignment=16384

ini 中的 16384 和 IOSPlatformProperties.h 中的一致,也就意味着在 unreal pak 过程中传入了 AlignForMemoryMapping 参数。

然后再来看看 Engine\Source\Developer\PakFileUtilities\Private\PakFileUtilities.cpp 中是怎么处理这个参数:

bool bIsMappedBulk = Input.Source.EndsWith(TEXT(".m.ubulk"));
// Align bulk data
if (bIsMappedBulk && CmdLineParameters.AlignForMemoryMapping > 0 && OriginalFileSize != INDEX_NONE && !bDeleted)
{
    if (!IsAligned(NewEntryOffset + NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest), CmdLineParameters.AlignForMemoryMapping))
    {
        int64 OldOffset = NewEntryOffset;
        NewEntryOffset = AlignArbitrary(NewEntryOffset + NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest), CmdLineParameters.AlignForMemoryMapping) - NewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest);
        int64 PaddingRequired = NewEntryOffset - OldOffset;

        check(PaddingRequired > 0);
        check(PaddingBuffer && PaddingBufferSize >= PaddingRequired);

        {
            UE_LOG(LogPakFile, Verbose, TEXT("%14llu - %14llu : %14llu bulk padding."), PakFileHandle->Tell(), PakFileHandle->Tell() + PaddingRequired, PaddingRequired);
            PakFileHandle->Serialize(PaddingBuffer, PaddingRequired);
            check(PakFileHandle->Tell() == NewEntryOffset);
        }
    }
}

4, 综合上述线索,我们可以得出结论:

  • ios 平台做了特殊的内存对齐,而且是写死在代码中的 16384
  • unreal 在 pak 过程中会检索平台配置参数,获取对齐信息
  • unreal 对 AlignForMemoryMapping 进行响应,在 pak 时,对文件做内存对齐

解决方法:

  • pak时,针对 IOS 平台,加入 -AlignForMemoryMapping=16384 参数,这个数字可以通过读配置来获得,也可以写死在脚本中,毕竟 unreal engine 是写死在代码中的

相关文章

网友评论

    本文标题:unreal 项目,IOS下排查错误: Memory mappe

    本文链接:https://www.haomeiwen.com/subject/hadmvdtx.html