mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(fex): keep the guest stack clear of the host stack's growth reserve (#3623)
This commit is contained in:
1 parent
8cf1e60cc2
commit
affb0da37f
3 files changed
+128
No files matched your search
@@ -303,6 +303,16 @@ alone would then read as an installed client.
|
||||
hook records the guest handler as it does for `sigaction()` and keeps its own installed, so a
|
||||
program that sets a crash handler is no longer killed by the next blocked system call
|
||||
(`tools/tests/test_native_hook_sigsys_mask.py`).
|
||||
- `0020-guest-stack-clear-of-host-stack-reserve`: FEX places the guest's 128 MB stack range
|
||||
with `MAP_FIXED_NOREPLACE`, scanning down from the top of the address space. With a 39-bit address
|
||||
space the host stack sits in that top range in about one start in eight, and the guest stack then
|
||||
landed directly below it, inside the space the kernel keeps free for the host stack to grow
|
||||
(`RLIMIT_STACK`, at least 128 MB). A deep host call chain (the native userland's `execve` hook
|
||||
and libc `syscall()` wrappers) then grew the host stack into the top of the guest stack, where
|
||||
the guest's argument and environment strings live, and the next program started with a corrupted
|
||||
environment (Proton failing with `KeyError: 'PWD'` or "No compat data path"). The guest stack is
|
||||
now placed below that reserve. On a Retroid Pocket Duo, 100 nested Proton starts ran clean, 10 of
|
||||
them with the host stack at the top; without the patch 3 of 25 were corrupted.
|
||||
- **Building and publishing.** Droid-Deck/DroidDeck-Components' `build-fex.yml` builds `tools/fex` from
|
||||
this repository at a chosen ref (default `main`) with host thunks, in the same Arch Linux ARM container
|
||||
with a read-only token, then a separate job replaces `fex.tzst` on Components' single `fex` release,
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
diff --git a/Source/Tools/FEXInterpreter/ELFCodeLoader.h b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
index cebf1f5..b1a72d2 100644
|
||||
--- a/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
+++ b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <sys/personality.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/random.h>
|
||||
+#include <sys/resource.h>
|
||||
|
||||
#define PAGE_START(x) ((x) & ~(uintptr_t)(4095))
|
||||
#define PAGE_OFFSET(x) ((x) & 4095)
|
||||
@@ -505,7 +506,12 @@ public:
|
||||
auto PageSize = sysconf(_SC_PAGESIZE);
|
||||
PageSize = PageSize > 0 ? PageSize : FEXCore::Utils::FEX_PAGE_SIZE;
|
||||
|
||||
+ const auto [HostStackLow, HostStackHigh] = HostStackReservation(PageSize);
|
||||
+
|
||||
do {
|
||||
+ if (StackHint < HostStackHigh && StackHint + FULL_STACK_SIZE > HostStackLow && HostStackLow > FULL_STACK_SIZE) {
|
||||
+ StackHint = (HostStackLow - FULL_STACK_SIZE) & ~static_cast<uint64_t>(PageSize - 1);
|
||||
+ }
|
||||
// Allocate the base of the full 128MB stack range.
|
||||
StackPointerBase = Handler->GuestMmap(Thread, reinterpret_cast<void*>(StackHint), FULL_STACK_SIZE, PROT_NONE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK | MAP_GROWSDOWN | MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
|
||||
@@ -793,6 +799,19 @@ public:
|
||||
*AuxTabSize = sizeof(AuxType) * AuxVariables.size();
|
||||
}
|
||||
|
||||
+ static std::pair<uint64_t, uint64_t> HostStackReservation(uint64_t PageSize) {
|
||||
+ struct prctl_mm_map map {};
|
||||
+ uint64_t Top = GetCurrentMap(map) && map.env_end ? map.env_end : reinterpret_cast<uint64_t>(__builtin_frame_address(0));
|
||||
+ Top = (Top + PageSize - 1) & ~(PageSize - 1);
|
||||
+
|
||||
+ constexpr uint64_t MinGap = 128ULL * 1024 * 1024;
|
||||
+ const uint64_t MaxGap = std::max(Top / 6 * 5, MinGap);
|
||||
+ struct rlimit Limit {};
|
||||
+ uint64_t Gap = getrlimit(RLIMIT_STACK, &Limit) == 0 ? static_cast<uint64_t>(Limit.rlim_cur) : MinGap;
|
||||
+ Gap = std::clamp(Gap, MinGap, MaxGap) + 256 * PageSize;
|
||||
+ return {Top > Gap ? Top - Gap : 0, Top};
|
||||
+ }
|
||||
+
|
||||
// Get the current memory map from /proc/self/stat
|
||||
static bool GetCurrentMap(struct prctl_mm_map& map) {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
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()
|
||||
Reference in new issue
Block a user