[qemu] Out of memory faster with bulletproof JIT
From @saagarjha https://saagarjha.com/blog/2020/02/23/jailed-just-in-time-compilation-on-ios/
Unlike processes with dynamic-codesigning which get access to “jumbo VA spaces”, iOS applications can normally only allocate a limited amount of virtual memory (which is determined using a fairly elaborate calculation based on the size of physical memory).
TCG allocates from 1024KiB to up to 2GiB of JIT cache. By default it uses ram size / 4. Because of the mirror mapping, iOS thinks we use 2x the amount of space we actually do for this JIT cache and therefore kills the app much quicker than before.
We should experiment with lowering the size of the JIT cache by default and/or allow the user to configure it. Make sure that the performance lost is less than the gains from the bulletproof JIT implementation. Otherwise, we should allow the old JIT as an option for those who value larger memory over faster speeds.
Can someone with jailbreak test this? https://github.com/utmapp/UTM/releases/download/v0.2-fakesign/UTM.ipa
I added the dynamic-codesign entitlement to the fake signed IPA. Let me know if that fixes this issue on jailbroken devices.
Was this built off of master?
Yes (at the time).
If so, we should disable the ptrace hacks on these builds. Unless you already took care of that?
I haven't but right now I haven't committed to having a separate "JB only" build yet. Just want to test it out. If there are benefits (such as supporting higher memory limits) then I will create a proper build config. The JB version could also use fork() and support multiple VMs and other features. I kinda want to get everything else working before doing stuff like that though as I would have to obtain a JB device to test with.
Is there anything in particular that is a good test?
If you have VM booting (Windows specifically since I haven't tried much other ones), bump the memory up until you get to a point where it boots but goes out of memory once it gets to the desktop. Then try that same config with the fakesigned version which has the dynamic-codesign entitlement. Even though it doesn't use MAP_JIT, I wonder if the entitlement is enough to bypass the limits.
Turns out my iPhone 6 doesn't have enough storage space to run Windows :(
Can someone with jailbreak test this? https://github.com/utmapp/UTM/releases/download/v0.2-fakesign/UTM.ipa
I added the dynamic-codesign entitlement to the fake signed IPA. Let me know if that fixes this issue on jailbroken devices.
Crashes on startup. Reboot did not solve the issue. On iPad Air 3 iOS 13.3 Jailbroken unc0ver 4.0.3.
Installs correctly but crashes on startup.
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Termination Description: DYLD, Library not loaded: @rpath/libgpg-error.0.dylib | Referenced from: /private/var/containers/Bundle/Application/632C8241-83A3-42D3-9191-7F1D49974C11/UTM.app/UTM | Reason: Incompatible library version: UTM requires version 28.0.0 or later, but libgpg-error.0.dylib provides version 25.0.0
Triggered by Thread: 0
Does jailbreak + jailbreak apps install additional libs? That’s probably the issue for many people.
I’m not sure how to fix this though.
Did not crash for me, fresh jailbreak with the necessary tools on an iPad Pro first gen on iOS 13 jailbroken with checkra1n. However, I cannot test Windows because I do not have an image. I am testing Arch and Ubuntu, will report back.
I found that using an older version of sysroot(download from "Actions" in 22/02/2020) to compile UTM can reduces memory usage by 200+MB. With the new version of sysroot, I set 1GB of memory for the iPad mini 5 and UTM always crashes, but with the old version I can set 1GB of memory.
Uh you do realize that it’s because you’re using the older slower JIT code right. That’s literally what this issue is about. The new jit code is faster but makes iOS think it’s using more memory.
Update: I'm punting this issue until the future. I looked into implementing a QEMU solution where not everything is mirror mapped but no solution can solve all the following problems:
- To link TBs with jumps, you would require the TB to be mirror mapped or turn off direct jumps. Either way there's a performance downgrade.
- TB metadata is stored before the code as an optimization. If we move the metadata we run into the old issue of having to statically allocate space for TB metadata with a best guess. Also cache locality.
- In MTTCG one thread could execute a TB created by another thread. If that other thread decides to unmap the mirror and the first thread decides to modify the jump list or page list or anything, it will crash. Adding locks will degrade performance.
So here's the situation: on iDevices, you are limited to about 50% of the physical RAM per app. If you allocate all of that to the VM, then about 8% of that is guest JIT and 8% is wasted and 34% is host RAM. If you want that 8%, either jailbreak or decrease the JIT cache (decreases performance somewhat).
There is this new sandbox escape that works in AltStore https://twitter.com/rileytestut/status/1256321645930663936 it may be useful for faster performance
The sandbox escape has no relevance here.
I wonder who enforces dynamic-codesigning…
I don't think dynamic-codesigning gives any improved performance? The alias mapping technique seems as good as we can get. Maybe you can find some cache locality improvements but I'm willing to bet those improvements are minimal.
Not only does dynamic-codesigning give you the ability to map RWX, you also get a larger virtual address space.