mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-02 02:55:42 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3e59c1b7c |
No files matched your search
@@ -81,6 +81,10 @@ STEAM_VERBS = ("run", "waitforexitandrun")
|
||||
ESCAPE = re.compile(rb"\\([0-7]{3})")
|
||||
DEVICE_FONTS = "/usr/local/share/fonts/android"
|
||||
FONT_RULES = "/etc/fonts/conf.d/66-droiddeck-device-fonts.conf"
|
||||
PROC_MAPS = "/proc/self/maps"
|
||||
JVM_DEPTH = 6
|
||||
X86_ZGC_ADDRESS_BITS = 45
|
||||
WITHOUT_ZGC = "-XX:+UnlockExperimentalVMOptions -XX:-UseZGC"
|
||||
|
||||
|
||||
def say(message):
|
||||
@@ -749,6 +753,47 @@ def vulkan_thunk_loads(fex, folder):
|
||||
return True
|
||||
|
||||
|
||||
def host_address_bits():
|
||||
try:
|
||||
with open(PROC_MAPS) as maps:
|
||||
ends = [int(line.split("-", 1)[1].split(None, 1)[0], 16) for line in maps if "-" in line]
|
||||
except (OSError, ValueError, IndexError):
|
||||
return None
|
||||
return max(ends).bit_length() if ends else None
|
||||
|
||||
|
||||
def bundled_zgc_jvm(folder):
|
||||
seen = 0
|
||||
base = folder.rstrip("/").count("/")
|
||||
for directory, dirs, files in os.walk(folder):
|
||||
seen += 1
|
||||
if seen > SCAN_LIMIT:
|
||||
return None
|
||||
if directory.count("/") - base >= JVM_DEPTH:
|
||||
dirs[:] = []
|
||||
jvm = os.path.join(directory, "libjvm.so")
|
||||
if "libjvm.so" in files and elf_arch(jvm) == "x86_64":
|
||||
try:
|
||||
with open(jvm, "rb") as f:
|
||||
if b"UseZGC" in f.read():
|
||||
return jvm
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def without_zgc(guest, folder):
|
||||
bits = host_address_bits()
|
||||
if bits is None or bits >= X86_ZGC_ADDRESS_BITS:
|
||||
return
|
||||
jvm = bundled_zgc_jvm(folder)
|
||||
if not jvm:
|
||||
return
|
||||
guest["_JAVA_OPTIONS"] = " ".join(part for part in (guest.get("_JAVA_OPTIONS"), WITHOUT_ZGC) if part)
|
||||
say("%s: ZGC cannot reserve its x86 heap in this device's %d-bit address space, so Java uses its default collector"
|
||||
% (jvm, bits))
|
||||
|
||||
|
||||
def required_runtime(program):
|
||||
text = read_text(os.path.join(os.path.dirname(program), "toolmanifest.vdf")) or ""
|
||||
match = re.search(r'"require_tool_appid"\s+"(\d+)"', text)
|
||||
@@ -777,6 +822,7 @@ def launch(command, env, mode, decide=None):
|
||||
and vulkan_thunk_loads(fex, folder):
|
||||
guest["LD_LIBRARY_PATH"] = ":".join(["/usr/" + VULKAN_PIN_DIR] + split_paths(guest.get("LD_LIBRARY_PATH")))
|
||||
thunks = "on, its own Vulkan loader too"
|
||||
without_zgc(guest, folder)
|
||||
say("running %s with %s (runtime %s, thunks %s)" % (program, fex["bin"], guest.get("FEX_ROOTFS", "none"), thunks))
|
||||
return os.execve(fex["bin"], [fex["bin"], program] + command[1:], guest)
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#define _GNU_SOURCE
|
||||
#if defined(__x86_64__) || defined(__i386__)
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "named_semaphores.h"
|
||||
#endif
|
||||
@@ -1,127 +0,0 @@
|
||||
static int posix_shm_path(const char *name, const char *prefix, char *path, size_t size) {
|
||||
if (!name) { errno = EINVAL; return -1; }
|
||||
while (*name == '/') name++;
|
||||
size_t len = strlen(name) + strlen(prefix);
|
||||
if (len == strlen(prefix) || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
|
||||
errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (snprintf(path, size, "/dev/shm/%s%s", prefix, name) >= (int)size) { errno = ENAMETOOLONG; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct posix_sem {
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
sem_t *addr;
|
||||
unsigned int refs;
|
||||
struct posix_sem *next;
|
||||
};
|
||||
static struct posix_sem *g_posix_sems;
|
||||
static pthread_mutex_t g_posix_sems_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void posix_sems_unlock(void) { pthread_mutex_unlock(&g_posix_sems_lock); }
|
||||
|
||||
static void posix_sems_hold(void) { pthread_mutex_lock(&g_posix_sems_lock); }
|
||||
|
||||
__attribute__((constructor)) static void posix_sems_across_fork(void) {
|
||||
pthread_atfork(posix_sems_hold, posix_sems_unlock, posix_sems_unlock);
|
||||
}
|
||||
|
||||
static void close_keeping_errno(int fd) {
|
||||
int saved = errno;
|
||||
close(fd);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static sem_t *posix_sem_map(int fd) {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) return SEM_FAILED;
|
||||
sem_t *sem = SEM_FAILED;
|
||||
posix_sems_hold();
|
||||
struct posix_sem *e = g_posix_sems;
|
||||
while (e && (e->dev != st.st_dev || e->ino != st.st_ino)) e = e->next;
|
||||
if (e) {
|
||||
e->refs++;
|
||||
sem = e->addr;
|
||||
} else {
|
||||
void *addr = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
e = addr == MAP_FAILED ? NULL : malloc(sizeof(*e));
|
||||
if (e) {
|
||||
*e = (struct posix_sem){st.st_dev, st.st_ino, addr, 1, g_posix_sems};
|
||||
g_posix_sems = e;
|
||||
sem = addr;
|
||||
} else if (addr != MAP_FAILED) {
|
||||
munmap(addr, sizeof(sem_t));
|
||||
errno = ENOMEM;
|
||||
}
|
||||
}
|
||||
posix_sems_unlock();
|
||||
return sem;
|
||||
}
|
||||
|
||||
static sem_t *posix_sem_open_fd(int fd) {
|
||||
if (fd < 0) return SEM_FAILED;
|
||||
sem_t *sem = posix_sem_map(fd);
|
||||
close_keeping_errno(fd);
|
||||
return sem;
|
||||
}
|
||||
|
||||
sem_t *sem_open(const char *name, int oflag, ...) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, "sem.", path, sizeof(path)) < 0) return SEM_FAILED;
|
||||
if (!(oflag & O_CREAT)) return posix_sem_open_fd(open(path, O_RDWR | O_NOFOLLOW | O_CLOEXEC));
|
||||
va_list args;
|
||||
va_start(args, oflag);
|
||||
mode_t mode = va_arg(args, mode_t);
|
||||
unsigned int value = va_arg(args, unsigned int);
|
||||
va_end(args);
|
||||
static unsigned int serial;
|
||||
for (;;) {
|
||||
if (!(oflag & O_EXCL)) {
|
||||
int fd = open(path, O_RDWR | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd >= 0 || errno != ENOENT) return posix_sem_open_fd(fd);
|
||||
}
|
||||
if (value > SEM_VALUE_MAX) { errno = EINVAL; return SEM_FAILED; }
|
||||
sem_t initial;
|
||||
memset(&initial, 0, sizeof(initial));
|
||||
sem_init(&initial, 1, value);
|
||||
char temp[PATH_MAX];
|
||||
int fd;
|
||||
do {
|
||||
snprintf(temp, sizeof(temp), "/dev/shm/.sem-%d-%u", (int)getpid(), __atomic_fetch_add(&serial, 1, __ATOMIC_RELAXED));
|
||||
fd = open(temp, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, mode);
|
||||
} while (fd < 0 && errno == EEXIST);
|
||||
if (fd < 0) return SEM_FAILED;
|
||||
errno = EIO;
|
||||
sem_t *sem = write(fd, &initial, sizeof(initial)) == (ssize_t)sizeof(initial) ? posix_sem_map(fd) : SEM_FAILED;
|
||||
close_keeping_errno(fd);
|
||||
if (sem != SEM_FAILED && renameat2(AT_FDCWD, temp, AT_FDCWD, path, RENAME_NOREPLACE) == 0) return sem;
|
||||
int saved = errno;
|
||||
if (sem != SEM_FAILED) sem_close(sem);
|
||||
unlink(temp);
|
||||
errno = saved;
|
||||
if (errno != EEXIST || (oflag & O_EXCL)) return SEM_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
int sem_close(sem_t *sem) {
|
||||
posix_sems_hold();
|
||||
struct posix_sem **link = &g_posix_sems;
|
||||
while (*link && (*link)->addr != sem) link = &(*link)->next;
|
||||
struct posix_sem *e = *link;
|
||||
if (e && --e->refs == 0) {
|
||||
*link = e->next;
|
||||
munmap(e->addr, sizeof(sem_t));
|
||||
free(e);
|
||||
}
|
||||
posix_sems_unlock();
|
||||
if (!e) { errno = EINVAL; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_unlink(const char *name) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, "sem.", path, sizeof(path)) < 0) return -1;
|
||||
return unlink(path);
|
||||
}
|
||||
@@ -6,7 +6,17 @@
|
||||
|
||||
#ifndef DDNATV_HOST_TEST
|
||||
|
||||
#include "../../linuxfs/preload/named_semaphores.h"
|
||||
static int posix_shm_path(const char *name, const char *prefix, char *path, size_t size) {
|
||||
if (!name) { errno = EINVAL; return -1; }
|
||||
while (*name == '/') name++;
|
||||
size_t len = strlen(name) + strlen(prefix);
|
||||
if (len == strlen(prefix) || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
|
||||
errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (snprintf(path, size, "/dev/shm/%s%s", prefix, name) >= (int)size) { errno = ENAMETOOLONG; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int shm_open(const char *name, int oflag, mode_t mode) {
|
||||
char path[PATH_MAX];
|
||||
@@ -20,6 +30,121 @@ int shm_unlink(const char *name) {
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
struct posix_sem {
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
sem_t *addr;
|
||||
unsigned int refs;
|
||||
struct posix_sem *next;
|
||||
};
|
||||
static struct posix_sem *g_posix_sems;
|
||||
static pthread_mutex_t g_posix_sems_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void posix_sems_unlock(void) { pthread_mutex_unlock(&g_posix_sems_lock); }
|
||||
|
||||
static void posix_sems_hold(void) { pthread_mutex_lock(&g_posix_sems_lock); }
|
||||
|
||||
__attribute__((constructor)) static void posix_sems_across_fork(void) {
|
||||
pthread_atfork(posix_sems_hold, posix_sems_unlock, posix_sems_unlock);
|
||||
}
|
||||
|
||||
static void close_keeping_errno(int fd) {
|
||||
int saved = errno;
|
||||
close(fd);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static sem_t *posix_sem_map(int fd) {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) return SEM_FAILED;
|
||||
sem_t *sem = SEM_FAILED;
|
||||
posix_sems_hold();
|
||||
struct posix_sem *e = g_posix_sems;
|
||||
while (e && (e->dev != st.st_dev || e->ino != st.st_ino)) e = e->next;
|
||||
if (e) {
|
||||
e->refs++;
|
||||
sem = e->addr;
|
||||
} else {
|
||||
void *addr = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
e = addr == MAP_FAILED ? NULL : malloc(sizeof(*e));
|
||||
if (e) {
|
||||
*e = (struct posix_sem){st.st_dev, st.st_ino, addr, 1, g_posix_sems};
|
||||
g_posix_sems = e;
|
||||
sem = addr;
|
||||
} else if (addr != MAP_FAILED) {
|
||||
munmap(addr, sizeof(sem_t));
|
||||
errno = ENOMEM;
|
||||
}
|
||||
}
|
||||
posix_sems_unlock();
|
||||
return sem;
|
||||
}
|
||||
|
||||
static sem_t *posix_sem_open_fd(int fd) {
|
||||
if (fd < 0) return SEM_FAILED;
|
||||
sem_t *sem = posix_sem_map(fd);
|
||||
close_keeping_errno(fd);
|
||||
return sem;
|
||||
}
|
||||
|
||||
sem_t *sem_open(const char *name, int oflag, ...) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, "sem.", path, sizeof(path)) < 0) return SEM_FAILED;
|
||||
if (!(oflag & O_CREAT)) return posix_sem_open_fd(open(path, O_RDWR | O_NOFOLLOW | O_CLOEXEC));
|
||||
va_list args;
|
||||
va_start(args, oflag);
|
||||
mode_t mode = va_arg(args, mode_t);
|
||||
unsigned int value = va_arg(args, unsigned int);
|
||||
va_end(args);
|
||||
static unsigned int serial;
|
||||
for (;;) {
|
||||
if (!(oflag & O_EXCL)) {
|
||||
int fd = open(path, O_RDWR | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd >= 0 || errno != ENOENT) return posix_sem_open_fd(fd);
|
||||
}
|
||||
if (value > SEM_VALUE_MAX) { errno = EINVAL; return SEM_FAILED; }
|
||||
sem_t initial;
|
||||
memset(&initial, 0, sizeof(initial));
|
||||
sem_init(&initial, 1, value);
|
||||
char temp[PATH_MAX];
|
||||
int fd;
|
||||
do {
|
||||
snprintf(temp, sizeof(temp), "/dev/shm/.sem-%d-%u", (int)getpid(), __atomic_fetch_add(&serial, 1, __ATOMIC_RELAXED));
|
||||
fd = open(temp, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, mode);
|
||||
} while (fd < 0 && errno == EEXIST);
|
||||
if (fd < 0) return SEM_FAILED;
|
||||
errno = EIO;
|
||||
sem_t *sem = write(fd, &initial, sizeof(initial)) == (ssize_t)sizeof(initial) ? posix_sem_map(fd) : SEM_FAILED;
|
||||
close_keeping_errno(fd);
|
||||
if (sem != SEM_FAILED && renameat2(AT_FDCWD, temp, AT_FDCWD, path, RENAME_NOREPLACE) == 0) return sem;
|
||||
int saved = errno;
|
||||
if (sem != SEM_FAILED) sem_close(sem);
|
||||
unlink(temp);
|
||||
errno = saved;
|
||||
if (errno != EEXIST || (oflag & O_EXCL)) return SEM_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
int sem_close(sem_t *sem) {
|
||||
posix_sems_hold();
|
||||
struct posix_sem **link = &g_posix_sems;
|
||||
while (*link && (*link)->addr != sem) link = &(*link)->next;
|
||||
struct posix_sem *e = *link;
|
||||
if (e && --e->refs == 0) {
|
||||
*link = e->next;
|
||||
munmap(e->addr, sizeof(sem_t));
|
||||
free(e);
|
||||
}
|
||||
posix_sems_unlock();
|
||||
if (!e) { errno = EINVAL; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sem_unlink(const char *name) {
|
||||
char path[PATH_MAX];
|
||||
if (posix_shm_path(name, "sem.", path, sizeof(path)) < 0) return -1;
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
#endif /* !DDNATV_HOST_TEST */
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ class FexTestCase(unittest.TestCase):
|
||||
"FONT_RULES": str(self.tmp / "66-droiddeck-device-fonts.conf"),
|
||||
"OWN_FEX": str(self.tmp / "own"),
|
||||
"OWN_FEX_TAG": str(self.tmp / "own/share/fex-emu/.droiddeck-tag"),
|
||||
"PROC_MAPS": str(self.tmp / "maps"),
|
||||
}
|
||||
patcher = mock.patch.dict(G, patches)
|
||||
patcher.start()
|
||||
@@ -1269,6 +1270,48 @@ class LaunchTest(FexTestCase):
|
||||
native.assert_called_once()
|
||||
emulated.assert_not_called()
|
||||
|
||||
def test_a_bundled_java_runtime_leaves_zgc_when_its_x86_heap_cannot_fit(self):
|
||||
self.fex_tool()
|
||||
self.runtime()
|
||||
maps = self.tmp / "maps"
|
||||
|
||||
def host(top):
|
||||
maps.write_text("5580000000-5580001000 r-xp 00000000 fe:00 1 /usr/bin/python3\n"
|
||||
"%x-%x rw-p 00000000 00:00 0 [stack]\n" % (top - 0x21000, top))
|
||||
|
||||
def app(name, jvm=elf(62) + b"\0UseZGC\0UseG1GC\0", where="usr/lib/game/runtime/lib/server"):
|
||||
run = self.write(self.tmp / name / "AppRun", "#!/bin/sh\n", 0o755)
|
||||
if jvm is not None:
|
||||
self.write(self.tmp / name / where / "libjvm.so", jvm)
|
||||
return run
|
||||
|
||||
def started(program, env=None):
|
||||
with mock.patch("os.execve") as emulated:
|
||||
_, said = self.quiet(FEX["launch"], [str(program)], dict(env or {}), "on")
|
||||
return emulated.call_args[0][2], said
|
||||
|
||||
host(0x7FFFFFF000)
|
||||
env, said = started(app("zgc"))
|
||||
self.assertEqual(env["_JAVA_OPTIONS"], "-XX:+UnlockExperimentalVMOptions -XX:-UseZGC")
|
||||
self.assertIn("39-bit address space", said)
|
||||
env, _ = started(app("zgc"), {"_JAVA_OPTIONS": "-Xmx2g"})
|
||||
self.assertEqual(env["_JAVA_OPTIONS"], "-Xmx2g -XX:+UnlockExperimentalVMOptions -XX:-UseZGC")
|
||||
env, _ = started(app("jdk8", where="jre/lib/amd64/server", jvm=elf(62) + b"\0UseG1GC\0"))
|
||||
self.assertNotIn("_JAVA_OPTIONS", env)
|
||||
env, _ = started(app("arm-jvm", jvm=elf(183) + b"\0UseZGC\0"))
|
||||
self.assertNotIn("_JAVA_OPTIONS", env)
|
||||
env, said = started(app("no-java", jvm=None), {"_JAVA_OPTIONS": "-Xmx2g"})
|
||||
self.assertEqual(env["_JAVA_OPTIONS"], "-Xmx2g")
|
||||
self.assertNotIn("ZGC", said)
|
||||
env, _ = started(app("too-deep", where="a/b/c/d/e/f/g/server"))
|
||||
self.assertNotIn("_JAVA_OPTIONS", env)
|
||||
host(0xFFFFFFFFF000)
|
||||
env, _ = started(app("zgc"))
|
||||
self.assertNotIn("_JAVA_OPTIONS", env)
|
||||
maps.unlink()
|
||||
env, _ = started(app("zgc"))
|
||||
self.assertNotIn("_JAVA_OPTIONS", env)
|
||||
|
||||
def test_programs_that_bring_a_new_enough_runtime_get_the_vulkan_thunk(self):
|
||||
tool = self.fex_tool()
|
||||
self.runtime()
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.tests.test_native_hook_named_semaphores import CALLS, KEEP, PROBE
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
DENY_LINKS = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <linux/filter.h>
|
||||
#include <linux/seccomp.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
struct sock_filter filter[] = {
|
||||
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
|
||||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_link, 1, 0),
|
||||
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_linkat, 0, 1),
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM),
|
||||
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
|
||||
};
|
||||
struct sock_fprog prog = {sizeof(filter) / sizeof(filter[0]), filter};
|
||||
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) return 125;
|
||||
execvp(argv[1], argv + 1);
|
||||
return 126;
|
||||
}
|
||||
"""
|
||||
|
||||
OPEN_EXISTING = r"""
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char **argv) {
|
||||
sem_t *sem = sem_open(argv[1], 0);
|
||||
int value;
|
||||
if (sem == SEM_FAILED || sem_getvalue(sem, &value) || value != atoi(argv[2])) return 1;
|
||||
return sem_trywait(sem) || sem_close(sem);
|
||||
}
|
||||
"""
|
||||
|
||||
PYTHON_PROBE = '''
|
||||
import multiprocessing as mp
|
||||
import _multiprocessing
|
||||
import os
|
||||
import sys
|
||||
|
||||
def square(n):
|
||||
return n * n
|
||||
|
||||
if __name__ == "__main__":
|
||||
name = "/dd-py-sem-" + str(os.getpid())
|
||||
sem = _multiprocessing.SemLock(1, 1, 1, name, True)
|
||||
assert sem.acquire(False)
|
||||
assert not sem.acquire(False)
|
||||
sem.release()
|
||||
for method in mp.get_all_start_methods():
|
||||
ctx = mp.get_context(method)
|
||||
lock = ctx.Lock()
|
||||
with lock:
|
||||
assert not lock.acquire(False)
|
||||
queue = ctx.Queue()
|
||||
queue.put(7)
|
||||
assert queue.get(timeout=10) == 7
|
||||
queue.close()
|
||||
queue.join_thread()
|
||||
with ctx.Pool(2) as pool:
|
||||
assert pool.map(square, [1, 2, 3]) == [1, 4, 9]
|
||||
print(method, flush=True)
|
||||
'''
|
||||
|
||||
|
||||
@unittest.skipUnless(platform.machine() == "x86_64" and shutil.which("cc"), "needs an x86-64 compiler and host")
|
||||
class NamedSemaphoresPreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmp = tempfile.TemporaryDirectory()
|
||||
cls.addClassCleanup(cls.tmp.cleanup)
|
||||
cls.root = Path(cls.tmp.name)
|
||||
cls.lib = cls.root / "semaphores.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-Werror", "-pthread", "-o", str(cls.lib),
|
||||
str(PRELOAD / "named_semaphores.c")], check=True)
|
||||
for name, source in (("deny", DENY_LINKS), ("probe", PROBE), ("keep", KEEP), ("existing", OPEN_EXISTING)):
|
||||
path = cls.root / (name + ".c")
|
||||
path.write_text(source)
|
||||
subprocess.run(["cc", "-pthread", "-o", str(cls.root / name), str(path)], check=True)
|
||||
cls.script = cls.root / "pool.py"
|
||||
cls.script.write_text(PYTHON_PROBE)
|
||||
|
||||
def run_program(self, args, hooked=True, denied=True):
|
||||
env = dict(os.environ)
|
||||
env.pop("LD_PRELOAD", None)
|
||||
if hooked:
|
||||
env["LD_PRELOAD"] = str(self.lib)
|
||||
command = ([str(self.root / "deny")] if denied else []) + list(map(str, args))
|
||||
return subprocess.run(command, env=env, text=True, capture_output=True, timeout=60)
|
||||
|
||||
def test_glibc_semlock_fails_when_hard_links_are_denied(self):
|
||||
result = self.run_program([sys.executable, "-c",
|
||||
"import multiprocessing; multiprocessing.Lock()"], hooked=False)
|
||||
self.assertNotEqual(0, result.returncode)
|
||||
self.assertIn("PermissionError: [Errno 1]", result.stderr)
|
||||
|
||||
def test_named_semaphore_contract_with_and_without_hard_links(self):
|
||||
for denied in (True, False):
|
||||
with self.subTest(denied=denied):
|
||||
result = self.run_program([self.root / "probe", f"/dd-preload-{os.getpid()}-{int(denied)}"], denied=denied)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual({call: "1" for call in CALLS}, dict(line.split("\t") for line in result.stdout.splitlines()))
|
||||
|
||||
def test_semaphores_remain_compatible_with_glibc_and_keep_the_requested_mode(self):
|
||||
for hooked in (True, False):
|
||||
with self.subTest(creator_hooked=hooked):
|
||||
name = f"/dd-preload-existing-{os.getpid()}-{int(hooked)}"
|
||||
path = Path("/dev/shm/sem." + name[1:])
|
||||
self.addCleanup(path.unlink, missing_ok=True)
|
||||
result = self.run_program([self.root / "keep", name], hooked=hooked, denied=hooked)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
umask = os.umask(0)
|
||||
os.umask(umask)
|
||||
self.assertEqual(0o640 & ~umask, path.stat().st_mode & 0o777)
|
||||
for reader, value in ((not hooked, 3), (hooked, 2)):
|
||||
result = self.run_program([self.root / "existing", name, value], hooked=reader, denied=reader)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
|
||||
def test_python_semlock_queues_and_explicit_pool_contexts(self):
|
||||
for denied in (True, False):
|
||||
with self.subTest(denied=denied):
|
||||
result = self.run_program([sys.executable, self.script], denied=denied)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual({"fork", "forkserver", "spawn"}, set(result.stdout.splitlines()))
|
||||
Reference in new issue
Block a user