mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
74 lines
3.0 KiB
Python
74 lines
3.0 KiB
Python
import re
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
PATCH = REPO / "tools/fex/patches/0020-guest-stack-clear-of-host-stack-reserve.patch"
|
|
LOADER = "Source/Tools/FEXInterpreter/ELFCodeLoader.h"
|
|
MIN_GAP = 128 * 1024 * 1024
|
|
STACK = 128 * 1024 * 1024
|
|
PAGE = 4096
|
|
|
|
|
|
def lines(sign):
|
|
return [line[1:].strip() for line in PATCH.read_text().splitlines() if line.startswith(sign) and not line.startswith(sign * 3)]
|
|
|
|
|
|
def reservation(top, rlimit):
|
|
top = (top + PAGE - 1) & ~(PAGE - 1)
|
|
gap = min(max(rlimit, MIN_GAP), max(top // 6 * 5, MIN_GAP)) + 256 * PAGE
|
|
return (top - gap if top > gap else 0, top)
|
|
|
|
|
|
def place(hint, low, high):
|
|
if hint < high and hint + STACK > low and low > STACK:
|
|
hint = (low - STACK) & ~(PAGE - 1)
|
|
return hint
|
|
|
|
|
|
class FexGuestStackReserveTest(unittest.TestCase):
|
|
def test_only_the_elf_loader_changes(self):
|
|
self.assertEqual([LOADER], re.findall(r"^diff --git a/(\S+) ", PATCH.read_text(), re.M))
|
|
|
|
def test_nothing_is_removed(self):
|
|
self.assertEqual([], lines("-"))
|
|
|
|
def test_the_reserve_starts_at_the_end_of_the_host_environment(self):
|
|
added = lines("+")
|
|
self.assertIn("uint64_t Top = GetCurrentMap(map) && map.env_end ? map.env_end : reinterpret_cast<uint64_t>(__builtin_frame_address(0));", added)
|
|
self.assertIn("uint64_t Gap = getrlimit(RLIMIT_STACK, &Limit) == 0 ? static_cast<uint64_t>(Limit.rlim_cur) : MinGap;", added)
|
|
self.assertIn("Gap = std::clamp(Gap, MinGap, MaxGap) + 256 * PageSize;", added)
|
|
|
|
def test_every_stack_hint_is_checked_against_the_reserve(self):
|
|
text = PATCH.read_text()
|
|
self.assertLess(text.index("HostStackReservation(PageSize);"), text.index("do {"))
|
|
self.assertLess(text.index("do {"), text.index("StackHint = (HostStackLow - FULL_STACK_SIZE)"))
|
|
self.assertLess(text.index("StackHint = (HostStackLow - FULL_STACK_SIZE)"), text.index("// Allocate the base of the full 128MB stack range."))
|
|
|
|
def test_a_guest_stack_overlapping_the_reserve_moves_below_it(self):
|
|
host_env_end = 0x7FFC391000
|
|
low, high = reservation(host_env_end, 8 * 1024 * 1024)
|
|
hint = place((1 << 39) - STACK, low, high)
|
|
self.assertLessEqual(hint + STACK, low)
|
|
self.assertGreaterEqual(host_env_end - (hint + STACK), MIN_GAP)
|
|
|
|
def test_a_guest_stack_clear_of_the_reserve_stays_put(self):
|
|
low, high = reservation(0x7FC491C000, 8 * 1024 * 1024)
|
|
hint = (1 << 39) - STACK
|
|
self.assertEqual(hint, place(hint, low, high))
|
|
|
|
def test_a_larger_stack_limit_widens_the_reserve(self):
|
|
low, _ = reservation(0x7FFC391000, 512 * 1024 * 1024)
|
|
self.assertEqual(0x7FFC391000 - 512 * 1024 * 1024 - 256 * PAGE, low)
|
|
|
|
def test_an_unlimited_stack_limit_keeps_room_for_the_guest(self):
|
|
low, _ = reservation(0x7FFC391000, (1 << 64) - 1)
|
|
self.assertGreater(low, STACK)
|
|
|
|
def test_no_comments_are_added(self):
|
|
self.assertFalse([line for line in lines("+") if line.startswith(("//", "/*"))])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|