mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

mimalloc-redirect for Xbox

Open kerambyte opened this issue 8 months ago • 5 comments

Hello,

Would it be possible to build a custom version of the redirection DLL to be used on Xbox? Right now we are hitting this issue on startup:

014c:0150 @ 00005968 - LdrpNameToOrdinal - WARNING: Procedure "RtlGetNtVersionNumbers" could not be located in DLL at base 0x00000105567B0000.
014c:0150 @ 00005968 - LdrpReportError - ERROR: Locating export "RtlGetNtVersionNumbers" for DLL "G:\mimalloc-redirect.dll" failed with status: 0xc0000139.
Exception thrown at 0x00000105567EE9A8 (ntdll.dll) in XboxClient.exe: 0xC0000139: Entry Point Not Found.

We could patch the import table and fake that function, but ideally we'd want something more robust. Perhaps you can remove this dependency altogether?

Cheers, Dmitry

kerambyte avatar Mar 27 '25 21:03 kerambyte

Can you try with the latest dev2 /dev3 ? I think it should work now :-)

daanx avatar Mar 28 '25 20:03 daanx

Thanks, with this one it did get further, but looks like it still depends on ntdll.dll:

014c:0150 @ 00042046 - LdrpNameToOrdinal - WARNING: Procedure "NtAllocateVirtualMemoryEx" could not be located in DLL at base 0x000001046C1B0000.
014c:0150 @ 00042046 - LdrpReportError - WARNING: Locating export "NtAllocateVirtualMemoryEx" for DLL "Unknown" failed with status: 0xc0000139.
014c:0150 @ 00042109 - LdrpNameToOrdinal - WARNING: Procedure "CreateHardLinkW" could not be located in DLL at base 0x000001046DD30000.
014c:0150 @ 00042109 - LdrpReportError - WARNING: Locating export "CreateHardLinkW" for DLL "Unknown" failed with status: 0xc0000139.
014c:0150 @ 00042125 - LdrpNameToOrdinal - WARNING: Procedure "CreateSymbolicLinkW" could not be located in DLL at base 0x000001046DD30000.
014c:0150 @ 00042125 - LdrpReportError - WARNING: Locating export "CreateSymbolicLinkW" for DLL "Unknown" failed with status: 0xc0000139.

I imagine there is probably no way around this and we'd have to use the native memory override mechanism that Xbox provides. It is a bit unfortunate since it requires to set up callbacks at runtime but maybe we can do something similar to what mimalloc-redirect.dll does to make sure we set them as early as possible. I imagine the CRT and kernel system library are quite different on Xbox for mimalloc to 'just work' out of the box.

kerambyte avatar Mar 28 '25 22:03 kerambyte

Ah, those must be dependencies outside of mimalloc-redirect.dll since it does not use any of the above functions. (you can use dumpbin to see the import table). mimalloc.dll uses of course VirtualAlloc (and therefore NtAllocateVirtualMemoryEx) -- but neither use CreateHardLinkW or CreateSymbolicLinkW so not sure where those come from?

I am not familiar enough with XBox to say for sure how this can be done; is it possible to override statically?

daanx avatar Mar 28 '25 23:03 daanx

You are right, NtAllocateVirtualMemory is coming from mimalloc.dll indeed but the link ones are unrelated (it was boost/filesystem).

Image

I was able to craft a 'custom' mimalloc-redirect.dll of sorts that is running very early and is overriding the allocation callbacks. However, on Xbox the legacy memory allocations are still coming through malloc/RtlAllocateHeap, they don't use Xbox-specific memory management code. So I was hoping I can combine the two approaches and run original mimalloc-redirect.dll from within my custom one. But alas, it is failing to resolve several symbols and ultimately refuses to override malloc. Do you think this is something that can be fixed? Happy to switch to a more private channel to discuss Xbox-specific bits.

mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_expand_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_recalloc_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: warning: unable to resolve "ucrtbase.dll!_msize_base" -- enabling MIMALLOC_PATCH_IMPORTS to prevent allocation errors.
mimalloc-redirect: error: unable to patch _mi_recalloc_ind (0x0000010103EB58A0); unable to get write permission
mimalloc-redirect: warning: redirection patching failed
mimalloc-redirect: warning: standard malloc is _not_ redirected! -- using regular malloc/free. (v1.3.3)

kerambyte avatar Mar 30 '25 17:03 kerambyte

Ah, I am not sure if this can work as I am not familiar enough with the xbox API's. Feel free to send me an email and we can discuss it a bit more.

daanx avatar Mar 30 '25 23:03 daanx