Files
DroidDeck/tools/tests/test_native_hook_rttime_limit.py
T
JRAlmonte 86b8ae3c99 fix(nativeguest): do not apply a realtime limit of zero (#3704)
Flatpak emulators that use SDL (Flycast, Rosalie's Mupen GUI, xemu) were
killed with status 137 a second or two after a game started. Without a
RealtimeKit, xdg-desktop-portal reports RTTimeUSecMax 0, and SDL sets that
as RLIMIT_RTTIME before asking for a realtime thread. The kernel checks the
limit on a thread that is realtime or has a CPU-time timer armed, and a
limit of 0 is reached at once: SIGKILL at the hard limit.

Fixes #3672
2026-12-04 13:02:07 -05:00

82 lines
2.9 KiB
Python

import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from tools.tests.test_usr2_preload import runner
HOOK_BUILD = Path(__file__).resolve().parents[1] / "nativeguest/build.sh"
PROBE = r"""
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
static void show(const char *name, int ret) {
struct rlimit now;
if (getrlimit(RLIMIT_RTTIME, &now) != 0) exit(2);
printf("%s\t%d\t%lld\t%lld\n", name, ret, (long long)now.rlim_cur, (long long)now.rlim_max);
}
int main(void) {
struct rlimit zero = {0, 0}, soft_zero = {0, 200000}, usable = {100000, 200000};
show("start", 0);
show("zero", setrlimit(RLIMIT_RTTIME, &zero));
show("soft_zero", setrlimit(RLIMIT_RTTIME, &soft_zero));
show("usable", setrlimit(RLIMIT_RTTIME, &usable));
return 0;
}
"""
class NativeHookRttimeLimitTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cc, cls.prefix = runner()
if cls.cc is None:
raise unittest.SkipTest("no aarch64 compiler and runner")
cls.tmp = tempfile.TemporaryDirectory(dir="/tmp")
tmp = Path(cls.tmp.name)
cls.hook = tmp / "hook.so"
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
(tmp / "probe.c").write_text(PROBE)
cls.probe = tmp / "probe"
subprocess.run([cls.cc, "-O1", "-o", str(cls.probe), str(tmp / "probe.c")], check=True)
cls.root = tmp / "root"
cls.root.mkdir()
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def limits(self):
hook_env = {"DDNATV_ROOT": str(self.root), "LD_PRELOAD": str(self.hook)}
env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "DDNATV_ROOT")}
if self.prefix:
cmd = list(self.prefix) + [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
else:
cmd = []
env.update(hook_env)
out = subprocess.run(cmd + [str(self.probe)], env=env, capture_output=True, text=True, timeout=120)
self.assertEqual(0, out.returncode, out.stdout + out.stderr)
return {name: (int(ret), int(cur), int(hard))
for name, ret, cur, hard in (line.split("\t") for line in out.stdout.splitlines())}
def test_a_realtime_limit_of_zero_is_reported_set_and_not_applied(self):
limits = self.limits()
self.assertEqual(0, limits["zero"][0])
self.assertEqual(limits["start"][1:], limits["zero"][1:])
def test_a_soft_realtime_limit_of_zero_is_not_applied(self):
limits = self.limits()
self.assertEqual(0, limits["soft_zero"][0])
self.assertEqual(limits["start"][1:], limits["soft_zero"][1:])
def test_a_usable_realtime_limit_is_applied(self):
self.assertEqual((0, 100000, 200000), self.limits()["usable"])
if __name__ == "__main__":
unittest.main()