FEX: describe the container's procfs in emulated mount tables

This commit is contained in:
maxjivi05 committed 2026-12-03 13:01:38 -05:00
1 parent 7648b9ae8f
commit 20e3e9a802
2 files changed
+364

No files matched your search

@@ -0,0 +1,54 @@
From 18e2978254eaed75e7742859f501632313da968f Mon Sep 17 00:00:00 2001
From: maxjivi05 <maxjividen@gmail.com>
Date: Thu, 3 Dec 2026 06:51:24 -0500
Subject: [PATCH] Keep the guest stack clear of the host stack growth reserve
---
Source/Tools/FEXInterpreter/ELFCodeLoader.h | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/Source/Tools/FEXInterpreter/ELFCodeLoader.h b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
index f21d755..6d21573 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,310 @@
From fc926d0e9c63a3d5290a5d816029513349de78f6 Mon Sep 17 00:00:00 2001
From: maxjivi05 <maxjividen@gmail.com>
Date: Thu, 3 Dec 2026 08:03:20 -0500
Subject: [PATCH] Describe the container's procfs in emulated mount tables
Android mounts /proc with hidepid=invisible,gid=3009 so an app cannot see the
processes of other users. Every process of the container runs as the app's
user, so the container sees all of its own processes, as it would through a
procfs of its own in a PID namespace. The emulated /proc/<pid>/mounts and
mountinfo now list the proc mount without the host instance's access options
(hidepid, gid, subset). Every other mount is passed through unchanged.
---
.../EmulatedFiles/EmulatedFiles.cpp | 230 +++++++++++++-----
.../EmulatedFiles/EmulatedFiles.h | 1 +
2 files changed, 170 insertions(+), 61 deletions(-)
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
index 49adaaa..a7a4f1d 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
@@ -37,6 +37,7 @@ $end_info$
#include <elf.h>
#include <mutex>
#include <shared_mutex>
+#include <string_view>
#include <fcntl.h>
#include <filesystem>
#include <ostream>
@@ -648,6 +649,149 @@ static bool IsHostArchModule(const char* Path, fextl::unordered_map<fextl::strin
return Host;
}
+static bool ReadHostFile(const char* Path, fextl::string& Out) {
+ int HostFD = ::open(Path, O_RDONLY | O_CLOEXEC);
+ if (HostFD < 0) {
+ return false;
+ }
+ char Buf[65536];
+ for (;;) {
+ const ssize_t Len = ::read(HostFD, Buf, sizeof(Buf));
+ if (Len > 0) {
+ Out.append(Buf, Len);
+ } else if (Len == 0) {
+ break;
+ } else if (errno != EINTR) {
+ LogMan::Msg::EFmt("Emulated /proc file: reading {} failed: {}", Path, errno);
+ ::close(HostFD);
+ return false;
+ }
+ }
+ ::close(HostFD);
+ return true;
+}
+
+static int32_t SealedFileOf(const char* pathname, int32_t flags, const fextl::string& Data) {
+ int NewFD = GenTmpFD(pathname, flags);
+ if (NewFD < 0) {
+ LogMan::Msg::EFmt("Emulated /proc file: memfd_create failed: {}", errno);
+ return -1;
+ }
+ for (size_t Done = 0; Done < Data.size();) {
+ const ssize_t Wrote = ::write(NewFD, Data.data() + Done, Data.size() - Done);
+ if (Wrote > 0) {
+ Done += Wrote;
+ } else if (Wrote < 0 && errno == EINTR) {
+ continue;
+ } else {
+ LogMan::Msg::EFmt("Emulated /proc file: writing the synthesized file failed: {}", errno);
+ ::close(NewFD);
+ return -1;
+ }
+ }
+ if (::lseek(NewFD, 0, SEEK_SET) != 0) {
+ LogMan::Msg::EFmt("Emulated /proc file: lseek failed: {}", errno);
+ ::close(NewFD);
+ return -1;
+ }
+ SealTmpFD(NewFD);
+ return NewFD;
+}
+
+static const char* ProcFileOfPid(const char* Path) {
+ if (strncmp(Path, "/proc/", 6) != 0) {
+ return nullptr;
+ }
+ const char* P = Path + 6;
+ if (strncmp(P, "self/", 5) == 0) {
+ return P + 5;
+ }
+ if (strncmp(P, "thread-self/", 12) == 0) {
+ return P + 12;
+ }
+ if (!isdigit(*P)) {
+ return P;
+ }
+ while (isdigit(*P)) {
+ ++P;
+ }
+ if (*P++ != '/') {
+ return nullptr;
+ }
+ if (strncmp(P, "task/", 5) == 0) {
+ P += 5;
+ if (!isdigit(*P)) {
+ return nullptr;
+ }
+ while (isdigit(*P)) {
+ ++P;
+ }
+ if (*P++ != '/') {
+ return nullptr;
+ }
+ }
+ return P;
+}
+
+static bool IsProcMountTablePath(const char* Path) {
+ const char* File = ProcFileOfPid(Path);
+ return File && (strcmp(File, "mounts") == 0 || strcmp(File, "mountinfo") == 0);
+}
+
+static fextl::string ContainerProcOptions(std::string_view Options) {
+ fextl::string Out;
+ size_t Pos = 0;
+ while (Pos <= Options.size()) {
+ size_t Comma = Options.find(',', Pos);
+ if (Comma == std::string_view::npos) {
+ Comma = Options.size();
+ }
+ const std::string_view Option = Options.substr(Pos, Comma - Pos);
+ if (!Option.empty() && !Option.starts_with("hidepid=") && !Option.starts_with("gid=") && !Option.starts_with("subset=")) {
+ if (!Out.empty()) {
+ Out += ',';
+ }
+ Out.append(Option);
+ }
+ Pos = Comma + 1;
+ }
+ return Out;
+}
+
+static fextl::string ContainerMountLine(std::string_view Line, bool MountInfo) {
+ fextl::vector<std::string_view> Fields;
+ size_t Pos = 0;
+ while (Pos < Line.size()) {
+ size_t Space = Line.find(' ', Pos);
+ if (Space == std::string_view::npos) {
+ Space = Line.size();
+ }
+ Fields.push_back(Line.substr(Pos, Space - Pos));
+ Pos = Space + 1;
+ }
+ size_t Type = 2;
+ if (MountInfo) {
+ auto Dash = std::find(Fields.begin(), Fields.end(), std::string_view("-"));
+ Type = static_cast<size_t>(Dash - Fields.begin()) + 1;
+ }
+ const size_t Options = MountInfo ? Type + 2 : Type + 1;
+ if (Options >= Fields.size() || Fields[Type] != "proc") {
+ return fextl::string(Line);
+ }
+ fextl::string Out;
+ for (size_t i = 0; i < Fields.size(); ++i) {
+ if (i) {
+ Out += ' ';
+ }
+ if (i == Options) {
+ Out += ContainerProcOptions(Fields[i]);
+ } else {
+ Out.append(Fields[i]);
+ }
+ }
+ return Out;
+}
+
static bool IsProcMapsPath(const char* Path) {
if (strncmp(Path, "/proc/", 6) != 0) {
return false;
@@ -672,6 +816,9 @@ int32_t EmulatedFDManager::Open(const char* pathname, int flags, uint32_t mode)
if (pathname && IsProcMapsPath(pathname)) {
return ProcMaps(CTX, AT_FDCWD, pathname, flags, mode);
}
+ if (pathname && IsProcMountTablePath(pathname)) {
+ return ProcMounts(CTX, AT_FDCWD, pathname, flags, mode);
+ }
return -1;
}
@@ -679,25 +826,10 @@ int32_t EmulatedFDManager::Open(const char* pathname, int flags, uint32_t mode)
}
int32_t EmulatedFDManager::ProcMaps(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) {
- int HostFD = ::open(pathname, O_RDONLY | O_CLOEXEC);
- if (HostFD < 0) {
- return -1;
- }
fextl::string In;
- char Buf[65536];
- for (;;) {
- const ssize_t Len = ::read(HostFD, Buf, sizeof(Buf));
- if (Len > 0) {
- In.append(Buf, Len);
- } else if (Len == 0) {
- break;
- } else if (errno != EINTR) {
- LogMan::Msg::EFmt("ProcMaps: reading {} failed: {}", pathname, errno);
- ::close(HostFD);
- return -1;
- }
+ if (!ReadHostFile(pathname, In)) {
+ return -1;
}
- ::close(HostFD);
constexpr uint64_t X86_64TaskSizeMax = (1ULL << 47) - 4096;
const bool HostSharesGuestRange = FEXCore::Allocator::GetHostVABits() < 47;
@@ -755,30 +887,29 @@ int32_t EmulatedFDManager::ProcMaps(FEXCore::Context::Context* ctx, int32_t fd,
Pos = End;
}
- int NewFD = GenTmpFD(pathname, flags);
- if (NewFD < 0) {
- LogMan::Msg::EFmt("ProcMaps: memfd_create failed: {}", errno);
+ return SealedFileOf(pathname, flags, Out);
+}
+
+int32_t EmulatedFDManager::ProcMounts(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) {
+ fextl::string In;
+ if (!ReadHostFile(pathname, In)) {
return -1;
}
- for (size_t Done = 0; Done < Out.size();) {
- const ssize_t Wrote = ::write(NewFD, Out.data() + Done, Out.size() - Done);
- if (Wrote > 0) {
- Done += Wrote;
- } else if (Wrote < 0 && errno == EINTR) {
- continue;
- } else {
- LogMan::Msg::EFmt("ProcMaps: writing the synthesized file failed: {}", errno);
- ::close(NewFD);
- return -1;
+ const char* File = ProcFileOfPid(pathname);
+ const bool MountInfo = File && strcmp(File, "mountinfo") == 0;
+ fextl::string Out;
+ Out.reserve(In.size());
+ size_t Pos = 0;
+ while (Pos < In.size()) {
+ size_t Eol = In.find('\n', Pos);
+ const size_t LineEnd = Eol == fextl::string::npos ? In.size() : Eol;
+ Out += ContainerMountLine(std::string_view(In).substr(Pos, LineEnd - Pos), MountInfo);
+ if (Eol != fextl::string::npos) {
+ Out += '\n';
}
+ Pos = LineEnd + 1;
}
- if (::lseek(NewFD, 0, SEEK_SET) != 0) {
- LogMan::Msg::EFmt("ProcMaps: lseek failed: {}", errno);
- ::close(NewFD);
- return -1;
- }
- SealTmpFD(NewFD);
- return NewFD;
+ return SealedFileOf(pathname, flags, Out);
}
int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) {
@@ -831,30 +962,7 @@ int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd
Cursor = Eol + 1;
}
- int NewFD = GenTmpFD(pathname, flags);
- if (NewFD < 0) {
- LogMan::Msg::EFmt("ProcMaps: memfd_create failed: {}", errno);
- return -1;
- }
- for (size_t Done = 0; Done < Out.size();) {
- const ssize_t Wrote = ::write(NewFD, Out.data() + Done, Out.size() - Done);
- if (Wrote > 0) {
- Done += Wrote;
- } else if (Wrote < 0 && errno == EINTR) {
- continue;
- } else {
- LogMan::Msg::EFmt("ProcMaps: writing the synthesized file failed: {}", errno);
- ::close(NewFD);
- return -1;
- }
- }
- if (::lseek(NewFD, 0, SEEK_SET) != 0) {
- LogMan::Msg::EFmt("ProcMaps: lseek failed: {}", errno);
- ::close(NewFD);
- return -1;
- }
- SealTmpFD(NewFD);
- return NewFD;
+ return SealedFileOf(pathname, flags, Out);
}
int32_t EmulatedFDManager::ProcAuxv(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) {
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.h
index b98ef4b..b1ec241 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.h
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.h
@@ -35,6 +35,7 @@ private:
static int32_t ProcAuxv(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode);
static int32_t ProcMaps(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode);
+ static int32_t ProcMounts(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode);
static int32_t ProcStatus(FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode);
const uint32_t ThreadsConfig;
};