import re import shutil import subprocess import tempfile import unittest from pathlib import Path REPO = Path(__file__).resolve().parents[2] PATCH = REPO / "tools/fex/patches/0029-proc-mount-tables-describe-the-container-procfs.patch" EMULATED = "Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles" HOST_MOUNTS = """/dev/block/dm-28 / ext4 ro,seclabel,relatime,discard 0 0 tmpfs /dev tmpfs rw,seclabel,nosuid,relatime,size=5791196k,nr_inodes=1447799,mode=755 0 0 proc /proc proc rw,relatime,gid=3009,hidepid=invisible 0 0 sysfs /sys sysfs rw,seclabel,relatime 0 0 /dev/fuse /mnt/user/0/emulated fuse rw,lazytime,nosuid,nodev,noexec,noatime,user_id=0,group_id=0,allow_other 0 0 """ HOST_MOUNTINFO = """9838 9837 254:28 / / ro,relatime master:1 - ext4 /dev/block/dm-28 ro,seclabel,discard 9853 9838 0:20 / /proc rw,relatime master:4 - proc proc rw,gid=3009,hidepid=invisible 9860 9853 0:20 / /proc/sys rw,relatime shared:7 master:4 - proc proc rw,hidepid=2,subset=pid 9871 9838 0:21 / /sys rw,relatime master:5 - sysfs sysfs rw,seclabel """ HARNESS = r""" #include #include #include #include #include #include #include namespace fextl { using string = std::string; template using vector = std::vector; } HELPERS static std::string Filter(const std::string& In, bool MountInfo) { std::string Out; size_t Pos = 0; while (Pos < In.size()) { size_t Eol = In.find('\n', Pos); const size_t LineEnd = Eol == std::string::npos ? In.size() : Eol; Out += ContainerMountLine(std::string_view(In).substr(Pos, LineEnd - Pos), MountInfo); if (Eol != std::string::npos) { Out += '\n'; } Pos = LineEnd + 1; } return Out; } int main(int argc, char** argv) { if (argv[1][0] == 'p') { for (int i = 2; i < argc; ++i) { std::printf("%s\n", IsProcMountTablePath(argv[i]) ? "yes" : "no"); } return 0; } std::string In; char Buf[4096]; size_t Len; while ((Len = std::fread(Buf, 1, sizeof(Buf), stdin)) > 0) { In.append(Buf, Len); } std::fputs(Filter(In, argv[1][0] == 'i').c_str(), stdout); return 0; } """ def added(): return [line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")] def helpers(): body = added() start = body.index("static const char* ProcFileOfPid(const char* Path) {") end = body.index("static fextl::string ContainerMountLine(std::string_view Line, bool MountInfo) {") end = body.index("}", end) + 1 return "\n".join(body[start:end]) class FexProcMountTablesTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.tmp = tempfile.TemporaryDirectory() cls.binary = None compiler = shutil.which("g++") or shutil.which("clang++") if compiler: source = Path(cls.tmp.name) / "mounts.cpp" source.write_text(HARNESS.replace("HELPERS", helpers())) binary = Path(cls.tmp.name) / "mounts" subprocess.run([compiler, "-std=c++20", "-O1", "-o", str(binary), str(source)], check=True) cls.binary = binary @classmethod def tearDownClass(cls): cls.tmp.cleanup() def run_filter(self, mode, text): if not self.binary: self.skipTest("no C++ compiler") return subprocess.run([str(self.binary), mode], input=text, capture_output=True, text=True, check=True).stdout def matches(self, *paths): if not self.binary: self.skipTest("no C++ compiler") out = subprocess.run([str(self.binary), "p", *paths], capture_output=True, text=True, check=True).stdout return out.split() def test_only_the_emulated_files_change(self): files = re.findall(r"^diff --git a/(\S+) ", PATCH.read_text(), re.M) self.assertEqual({EMULATED + ".cpp", EMULATED + ".h"}, set(files)) def test_mount_tables_are_routed_to_the_filter_on_open(self): body = added() self.assertIn(" if (pathname && IsProcMountTablePath(pathname)) {", body) self.assertIn(" return ProcMounts(CTX, AT_FDCWD, pathname, flags, mode);", body) def test_every_path_form_of_the_mount_tables_matches(self): paths = ["/proc/mounts", "/proc/self/mounts", "/proc/thread-self/mounts", "/proc/self/mountinfo", "/proc/4242/mounts", "/proc/4242/mountinfo", "/proc/4242/task/4243/mountinfo"] self.assertEqual(["yes"] * len(paths), self.matches(*paths)) def test_other_proc_files_do_not_match(self): paths = ["/proc/self/maps", "/proc/self/status", "/proc/self/mountstats", "/proc/4242/task/mounts", "/proc/self/mounts/x", "/etc/mounts", "/proc/4242x/mounts"] self.assertEqual(["no"] * len(paths), self.matches(*paths)) def test_only_the_proc_line_of_mounts_changes(self): out = self.run_filter("m", HOST_MOUNTS).splitlines() expected = HOST_MOUNTS.splitlines() expected[2] = "proc /proc proc rw,relatime 0 0" self.assertEqual(expected, out) def test_only_proc_super_options_of_mountinfo_change(self): out = self.run_filter("i", HOST_MOUNTINFO).splitlines() expected = HOST_MOUNTINFO.splitlines() expected[1] = "9853 9838 0:20 / /proc rw,relatime master:4 - proc proc rw" expected[2] = "9860 9853 0:20 / /proc/sys rw,relatime shared:7 master:4 - proc proc rw" self.assertEqual(expected, out) def test_tables_without_proc_pass_through_unchanged(self): text = "".join(line + "\n" for line in HOST_MOUNTS.splitlines() if " proc " not in line) self.assertEqual(text, self.run_filter("m", text)) def test_malformed_lines_pass_through_unchanged(self): text = "proc /proc proc\nno dash here proc rw\n\nproc\n" self.assertEqual(text, self.run_filter("m", text)) self.assertEqual(text, self.run_filter("i", text)) def test_no_comments_are_added(self): self.assertFalse([line for line in added() if line.strip().startswith(("//", "/*"))]) if __name__ == "__main__": unittest.main()