mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 07:16:04 +00:00
fix(desktop): video wallpapers no longer race ahead after DroidDeck returns from the background (#1833)
* fix(desktop): video wallpapers no longer race ahead after DroidDeck returns from the background * test: give the vrclient loader harness the clock syscall hook
This commit is contained in:
1 parent
8f7cdb7106
commit
83980035c6
11 files changed
+535
-3
No files matched your search
@@ -99,6 +99,8 @@ clear_stale_tmp() {
|
||||
[ -z "${BL_INSIDE:-}" ] && clear_stale_tmp
|
||||
if [ -z "${BL_INSIDE:-}" ]; then
|
||||
if [ "${BL_ZRAM:-0}" = 1 ]; then printf 0 > /tmp/.bl-zram; else rm -f /tmp/.bl-zram; fi
|
||||
rm -f /tmp/.bl-clock
|
||||
head -c 8 /dev/zero > /tmp/.bl-clock
|
||||
fi
|
||||
|
||||
steam_devtools() {
|
||||
|
||||
@@ -0,0 +1,314 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(__aarch64__) || defined(BL_CLOCK_ANY_ARCH)
|
||||
|
||||
#ifndef CP_CTL
|
||||
#define CP_CTL "/tmp/.bl-clock"
|
||||
#endif
|
||||
#ifndef FUTEX_LOCK_PI2
|
||||
#define FUTEX_LOCK_PI2 13
|
||||
#endif
|
||||
#define CP_FDS 65536
|
||||
|
||||
static const int64_t *cp_shift;
|
||||
static int cp_state;
|
||||
static size_t cp_cond_byte;
|
||||
static unsigned char cp_cond_mask;
|
||||
static unsigned char cp_fds[CP_FDS / 8];
|
||||
|
||||
static int (*cp_gettime)(clockid_t, struct timespec *);
|
||||
static int (*cp_nanosleep)(clockid_t, int, const struct timespec *, struct timespec *);
|
||||
static int (*cp_cond_timedwait)(pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
|
||||
static int (*cp_cond_clockwait)(pthread_cond_t *, pthread_mutex_t *, clockid_t, const struct timespec *);
|
||||
static int (*cp_sem_clockwait)(sem_t *, clockid_t, const struct timespec *);
|
||||
static int (*cp_mutex_clocklock)(pthread_mutex_t *, clockid_t, const struct timespec *);
|
||||
static int (*cp_rdlock)(pthread_rwlock_t *, clockid_t, const struct timespec *);
|
||||
static int (*cp_wrlock)(pthread_rwlock_t *, clockid_t, const struct timespec *);
|
||||
static int (*cp_join)(pthread_t, void **, clockid_t, const struct timespec *);
|
||||
static int (*cp_tfd_create)(int, int);
|
||||
static int (*cp_tfd_settime)(int, int, const struct itimerspec *, struct itimerspec *);
|
||||
|
||||
static void *cp_real(const char *name) {
|
||||
return dlsym(RTLD_NEXT, name);
|
||||
}
|
||||
|
||||
#define CP_REAL(var, name) (var ? var : (var = cp_real(name)))
|
||||
|
||||
static int cp_find_cond_bit(void) {
|
||||
pthread_condattr_t attr;
|
||||
pthread_cond_t mono, real;
|
||||
const unsigned char *a = (const unsigned char *)&mono, *b = (const unsigned char *)ℜ
|
||||
int found = 0;
|
||||
memset(&mono, 0, sizeof(mono));
|
||||
memset(&real, 0, sizeof(real));
|
||||
if (pthread_condattr_init(&attr) != 0) return 0;
|
||||
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0 && pthread_cond_init(&mono, &attr) == 0) {
|
||||
if (pthread_cond_init(&real, NULL) == 0) {
|
||||
for (size_t i = 0; i < sizeof(pthread_cond_t); i++) {
|
||||
unsigned char diff = a[i] ^ b[i];
|
||||
if (!diff) continue;
|
||||
if (found || (diff & (diff - 1))) {
|
||||
found = -1;
|
||||
break;
|
||||
}
|
||||
cp_cond_byte = i;
|
||||
cp_cond_mask = diff;
|
||||
found = 1;
|
||||
}
|
||||
pthread_cond_destroy(&real);
|
||||
}
|
||||
pthread_cond_destroy(&mono);
|
||||
}
|
||||
pthread_condattr_destroy(&attr);
|
||||
return found == 1;
|
||||
}
|
||||
|
||||
static void cp_init(void) {
|
||||
struct stat st;
|
||||
void *map;
|
||||
int fd;
|
||||
if (strcmp(program_invocation_short_name, "plasmashell") != 0) return;
|
||||
if (!cp_find_cond_bit()) return;
|
||||
fd = open(CP_CTL, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return;
|
||||
if (fstat(fd, &st) != 0 || st.st_size < (off_t)sizeof(int64_t)) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
map = mmap(NULL, sizeof(int64_t), PROT_READ, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
if (map != MAP_FAILED) __atomic_store_n(&cp_shift, (const int64_t *)map, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
static int64_t cp_offset(void) {
|
||||
const int64_t *shift;
|
||||
int state = __atomic_load_n(&cp_state, __ATOMIC_ACQUIRE);
|
||||
if (state == 0) {
|
||||
int expected = 0;
|
||||
if (__atomic_compare_exchange_n(&cp_state, &expected, 1, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) {
|
||||
cp_init();
|
||||
__atomic_store_n(&cp_state, 2, __ATOMIC_RELEASE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (state != 2) return 0;
|
||||
shift = __atomic_load_n(&cp_shift, __ATOMIC_ACQUIRE);
|
||||
if (!shift) return 0;
|
||||
int64_t value = __atomic_load_n(shift, __ATOMIC_RELAXED);
|
||||
return value > 0 ? value : 0;
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void cp_start(void) {
|
||||
cp_offset();
|
||||
}
|
||||
|
||||
static int cp_mono(clockid_t clock) {
|
||||
return clock == CLOCK_MONOTONIC || clock == CLOCK_MONOTONIC_RAW || clock == CLOCK_MONOTONIC_COARSE;
|
||||
}
|
||||
|
||||
static void cp_move(struct timespec *ts, int64_t ns) {
|
||||
int64_t total = (int64_t)ts->tv_nsec + ns % 1000000000LL;
|
||||
ts->tv_sec += (time_t)(ns / 1000000000LL);
|
||||
while (total < 0) {
|
||||
total += 1000000000LL;
|
||||
ts->tv_sec--;
|
||||
}
|
||||
while (total >= 1000000000LL) {
|
||||
total -= 1000000000LL;
|
||||
ts->tv_sec++;
|
||||
}
|
||||
ts->tv_nsec = (long)total;
|
||||
}
|
||||
|
||||
static const struct timespec *cp_later(const struct timespec *ts, struct timespec *out, int64_t shift) {
|
||||
if (!ts || !shift) return ts;
|
||||
*out = *ts;
|
||||
cp_move(out, shift);
|
||||
return out;
|
||||
}
|
||||
|
||||
static void cp_mark(int fd, int mono) {
|
||||
if (fd < 0 || fd >= CP_FDS) return;
|
||||
if (mono) __atomic_or_fetch(&cp_fds[fd / 8], (unsigned char)(1u << (fd % 8)), __ATOMIC_RELAXED);
|
||||
else __atomic_and_fetch(&cp_fds[fd / 8], (unsigned char)~(1u << (fd % 8)), __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static int cp_marked(int fd) {
|
||||
return fd >= 0 && fd < CP_FDS && (__atomic_load_n(&cp_fds[fd / 8], __ATOMIC_RELAXED) & (1u << (fd % 8)));
|
||||
}
|
||||
|
||||
static int cp_cond_mono(const pthread_cond_t *cond) {
|
||||
return (((const volatile unsigned char *)cond)[cp_cond_byte] & cp_cond_mask) != 0;
|
||||
}
|
||||
|
||||
int clock_gettime(clockid_t clock, struct timespec *ts) {
|
||||
if (!CP_REAL(cp_gettime, "clock_gettime")) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
int rc = cp_gettime(clock, ts);
|
||||
if (rc == 0 && cp_mono(clock)) {
|
||||
int64_t shift = cp_offset();
|
||||
if (shift) cp_move(ts, -shift);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int clock_nanosleep(clockid_t clock, int flags, const struct timespec *req, struct timespec *rem) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_nanosleep, "clock_nanosleep")) return ENOSYS;
|
||||
if ((flags & TIMER_ABSTIME) && cp_mono(clock)) req = cp_later(req, &moved, cp_offset());
|
||||
return cp_nanosleep(clock, flags, req, rem);
|
||||
}
|
||||
|
||||
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_cond_timedwait, "pthread_cond_timedwait")) return ENOSYS;
|
||||
int64_t shift = cp_offset();
|
||||
if (shift && cp_cond_mono(cond)) abstime = cp_later(abstime, &moved, shift);
|
||||
return cp_cond_timedwait(cond, mutex, abstime);
|
||||
}
|
||||
|
||||
int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_cond_clockwait, "pthread_cond_clockwait")) return ENOSYS;
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_cond_clockwait(cond, mutex, clock, abstime);
|
||||
}
|
||||
|
||||
int sem_clockwait(sem_t *sem, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_sem_clockwait, "sem_clockwait")) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_sem_clockwait(sem, clock, abstime);
|
||||
}
|
||||
|
||||
int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_mutex_clocklock, "pthread_mutex_clocklock")) return ENOSYS;
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_mutex_clocklock(mutex, clock, abstime);
|
||||
}
|
||||
|
||||
int pthread_rwlock_clockrdlock(pthread_rwlock_t *lock, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_rdlock, "pthread_rwlock_clockrdlock")) return ENOSYS;
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_rdlock(lock, clock, abstime);
|
||||
}
|
||||
|
||||
int pthread_rwlock_clockwrlock(pthread_rwlock_t *lock, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_wrlock, "pthread_rwlock_clockwrlock")) return ENOSYS;
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_wrlock(lock, clock, abstime);
|
||||
}
|
||||
|
||||
int pthread_clockjoin_np(pthread_t thread, void **result, clockid_t clock, const struct timespec *abstime) {
|
||||
struct timespec moved;
|
||||
if (!CP_REAL(cp_join, "pthread_clockjoin_np")) return ENOSYS;
|
||||
if (cp_mono(clock)) abstime = cp_later(abstime, &moved, cp_offset());
|
||||
return cp_join(thread, result, clock, abstime);
|
||||
}
|
||||
|
||||
int timerfd_create(int clock, int flags) {
|
||||
if (!CP_REAL(cp_tfd_create, "timerfd_create")) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
int fd = cp_tfd_create(clock, flags);
|
||||
cp_mark(fd, cp_mono(clock));
|
||||
return fd;
|
||||
}
|
||||
|
||||
static const struct itimerspec *cp_timer(int fd, int flags, const struct itimerspec *value, struct itimerspec *out) {
|
||||
int64_t shift;
|
||||
if (!value || !(flags & TFD_TIMER_ABSTIME) || !cp_marked(fd)) return value;
|
||||
if ((value->it_value.tv_sec == 0 && value->it_value.tv_nsec == 0) || !(shift = cp_offset())) return value;
|
||||
*out = *value;
|
||||
cp_move(&out->it_value, shift);
|
||||
return out;
|
||||
}
|
||||
|
||||
int timerfd_settime(int fd, int flags, const struct itimerspec *value, struct itimerspec *old) {
|
||||
struct itimerspec moved;
|
||||
if (!CP_REAL(cp_tfd_settime, "timerfd_settime")) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return cp_tfd_settime(fd, flags, cp_timer(fd, flags, value, &moved), old);
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_clock_syscall(long number, const long *args, long (*real)(long, ...), long *ret) {
|
||||
struct timespec moved;
|
||||
struct itimerspec timer;
|
||||
int64_t shift;
|
||||
if (number == SYS_clock_gettime) {
|
||||
*ret = real(number, args[0], args[1]);
|
||||
if (*ret == 0 && args[1] && cp_mono((clockid_t)args[0]) && (shift = cp_offset()))
|
||||
cp_move((struct timespec *)args[1], -shift);
|
||||
return 1;
|
||||
}
|
||||
if (number == SYS_timerfd_create) {
|
||||
*ret = real(number, args[0], args[1]);
|
||||
cp_mark((int)*ret, cp_mono((clockid_t)args[0]));
|
||||
return 1;
|
||||
}
|
||||
if (number == SYS_timerfd_settime) {
|
||||
const struct itimerspec *value = cp_timer((int)args[0], (int)args[1], (const struct itimerspec *)args[2], &timer);
|
||||
if (value == (const struct itimerspec *)args[2]) return 0;
|
||||
*ret = real(number, args[0], args[1], (long)value, args[3]);
|
||||
return 1;
|
||||
}
|
||||
if (number == SYS_clock_nanosleep) {
|
||||
if (!((int)args[1] & TIMER_ABSTIME) || !cp_mono((clockid_t)args[0]) || !args[2] || !(shift = cp_offset())) return 0;
|
||||
*ret = real(number, args[0], args[1], (long)cp_later((const struct timespec *)args[2], &moved, shift), args[3]);
|
||||
return 1;
|
||||
}
|
||||
if (number == SYS_futex) {
|
||||
int op = (int)args[1];
|
||||
int cmd = op & FUTEX_CMD_MASK;
|
||||
if (op & FUTEX_CLOCK_REALTIME) return 0;
|
||||
if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI && cmd != FUTEX_LOCK_PI2) return 0;
|
||||
if (!args[3] || !(shift = cp_offset())) return 0;
|
||||
*ret = real(number, args[0], args[1], args[2], (long)cp_later((const struct timespec *)args[3], &moved, shift), args[4], args[5]);
|
||||
return 1;
|
||||
}
|
||||
#ifdef SYS_futex_waitv
|
||||
if (number == SYS_futex_waitv) {
|
||||
if (!args[3] || (clockid_t)args[4] != CLOCK_MONOTONIC || !(shift = cp_offset())) return 0;
|
||||
*ret = real(number, args[0], args[1], args[2], (long)cp_later((const struct timespec *)args[3], &moved, shift), args[4]);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_clock_syscall(long number, const long *args, long (*real)(long, ...), long *ret) {
|
||||
(void)number;
|
||||
(void)args;
|
||||
(void)real;
|
||||
(void)ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -359,6 +359,7 @@ int bl_openat2(int dirfd, const char *path, void *how, size_t size) {
|
||||
}
|
||||
|
||||
int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) __attribute__((visibility("hidden")));
|
||||
int bl_clock_syscall(long number, const long *args, long (*real)(long, ...), long *ret) __attribute__((visibility("hidden")));
|
||||
|
||||
long syscall(long number, ...) {
|
||||
va_list ap;
|
||||
@@ -385,6 +386,9 @@ long syscall(long number, ...) {
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
if (bl_clock_syscall(number, args, real_syscall, &handled)) {
|
||||
return handled;
|
||||
}
|
||||
if (bl_writewatch_syscall(number, args, real_syscall, &handled)) {
|
||||
return handled;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int64_t ns(clockid_t clock) {
|
||||
struct timespec ts;
|
||||
clock_gettime(clock, &ts);
|
||||
return (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
|
||||
}
|
||||
|
||||
static struct timespec in(clockid_t clock, int64_t delay) {
|
||||
int64_t t = ns(clock) + delay;
|
||||
struct timespec ts = {(time_t)(t / 1000000000LL), (long)(t % 1000000000LL)};
|
||||
return ts;
|
||||
}
|
||||
|
||||
static int64_t start;
|
||||
static void begin(void) { start = ns(CLOCK_BOOTTIME); }
|
||||
static void end(const char *name) { printf("%s %lld\n", name, (long long)((ns(CLOCK_BOOTTIME) - start) / 1000000LL)); }
|
||||
|
||||
int main(void) {
|
||||
const int64_t wait = 60000000LL;
|
||||
printf("gap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ns(CLOCK_MONOTONIC)) / 1000000LL));
|
||||
struct timespec raw;
|
||||
syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &raw);
|
||||
printf("rawgap %lld\n", (long long)((ns(CLOCK_BOOTTIME) - ((int64_t)raw.tv_sec * 1000000000LL + raw.tv_nsec)) / 1000000LL));
|
||||
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_condattr_t attr;
|
||||
pthread_cond_t mono, real;
|
||||
pthread_condattr_init(&attr);
|
||||
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
|
||||
pthread_cond_init(&mono, &attr);
|
||||
pthread_cond_init(&real, NULL);
|
||||
struct timespec ts;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
ts = in(CLOCK_MONOTONIC, wait); begin(); pthread_cond_timedwait(&mono, &mutex, &ts); end("cond_mono");
|
||||
ts = in(CLOCK_REALTIME, wait); begin(); pthread_cond_timedwait(&real, &mutex, &ts); end("cond_real");
|
||||
ts = in(CLOCK_MONOTONIC, wait); begin(); pthread_cond_clockwait(&real, &mutex, CLOCK_MONOTONIC, &ts); end("clockwait");
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
sem_t sem;
|
||||
sem_init(&sem, 0, 0);
|
||||
ts = in(CLOCK_MONOTONIC, wait); begin(); sem_clockwait(&sem, CLOCK_MONOTONIC, &ts); end("sem");
|
||||
|
||||
ts = in(CLOCK_MONOTONIC, wait); begin(); clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL); end("nanosleep");
|
||||
|
||||
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
||||
struct itimerspec it = {{0, 0}, in(CLOCK_MONOTONIC, wait)};
|
||||
uint64_t ticks;
|
||||
timerfd_settime(fd, TFD_TIMER_ABSTIME, &it, NULL);
|
||||
begin(); if (read(fd, &ticks, sizeof(ticks)) < 0) ticks = 0; end("timerfd");
|
||||
|
||||
static uint32_t word;
|
||||
ts = in(CLOCK_MONOTONIC, wait); begin();
|
||||
syscall(SYS_futex, &word, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG, 0, &ts, NULL, FUTEX_BITSET_MATCH_ANY);
|
||||
end("futex");
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
SHIFT_MS = 10000
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
|
||||
class ClockPausePreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.temp = tempfile.TemporaryDirectory()
|
||||
cls.dir = Path(cls.temp.name)
|
||||
cls.control = cls.dir / "clock"
|
||||
build = cls.dir / "build"
|
||||
build.mkdir()
|
||||
(build / "program.c").write_text(PROGRAM)
|
||||
cls.preload = build / "libblsession.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-DBL_CLOCK_ANY_ARCH",
|
||||
'-DCP_CTL="%s"' % cls.control, "-o", str(cls.preload),
|
||||
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
|
||||
cls.shell = build / "plasmashell"
|
||||
subprocess.run(["cc", "-O2", "-pthread", "-o", str(cls.shell), str(build / "program.c")], check=True)
|
||||
cls.other = build / "kwin_wayland"
|
||||
shutil.copy(cls.shell, cls.other)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.temp.cleanup()
|
||||
|
||||
def run_program(self, program, shift_ms, preload=True):
|
||||
self.control.write_bytes(struct.pack("<q", shift_ms * 1000000))
|
||||
env = dict(os.environ)
|
||||
if preload:
|
||||
env["LD_PRELOAD"] = str(self.preload)
|
||||
result = subprocess.run([str(program)], env=env, capture_output=True, text=True, check=True, timeout=30)
|
||||
return {name: int(value) for name, value in (line.split() for line in result.stdout.splitlines())}
|
||||
|
||||
def test_the_shell_sees_monotonic_time_stopped_for_the_pause(self):
|
||||
plain = self.run_program(self.shell, 0)
|
||||
paused = self.run_program(self.shell, SHIFT_MS)
|
||||
self.assertAlmostEqual(paused["gap"] - plain["gap"], SHIFT_MS, delta=50)
|
||||
self.assertAlmostEqual(paused["rawgap"] - plain["rawgap"], SHIFT_MS, delta=50)
|
||||
|
||||
def test_timed_waits_last_as_long_as_asked_after_a_pause(self):
|
||||
paused = self.run_program(self.shell, SHIFT_MS)
|
||||
for name in ("cond_mono", "cond_real", "clockwait", "sem", "nanosleep", "timerfd", "futex"):
|
||||
self.assertGreaterEqual(paused[name], 50, name)
|
||||
self.assertLess(paused[name], 1000, name)
|
||||
|
||||
def test_other_programs_keep_the_real_clock(self):
|
||||
plain = self.run_program(self.other, 0, preload=False)
|
||||
paused = self.run_program(self.other, SHIFT_MS)
|
||||
self.assertAlmostEqual(paused["gap"], plain["gap"], delta=50)
|
||||
for name in ("cond_mono", "clockwait", "sem", "nanosleep", "timerfd", "futex"):
|
||||
self.assertGreaterEqual(paused[name], 50, name)
|
||||
self.assertLess(paused[name], 1000, name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -319,7 +319,7 @@ class FsyncPreloadTest(unittest.TestCase):
|
||||
source = base / "fsync-test.c"
|
||||
source.write_text(PROGRAM)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
|
||||
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-pthread", "-o", str(cls.exe), str(source)], check=True)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -43,7 +43,7 @@ class NoexecPreloadTest(unittest.TestCase):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.lib = cls.tmp / "libnoexec.so"
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib),
|
||||
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
|
||||
source = cls.tmp / "program.c"
|
||||
source.write_text(PROGRAM)
|
||||
for name in ("FEX", "game"):
|
||||
|
||||
@@ -312,7 +312,7 @@ class Openat2PreloadTest(unittest.TestCase):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.lib = cls.tmp / "libopenat2.so"
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib),
|
||||
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True)
|
||||
str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), str(PRELOAD / "clockpause.c"), "-ldl"], check=True)
|
||||
source = cls.tmp / "openat2.c"
|
||||
source.write_text(PROGRAM)
|
||||
subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "openat2"), str(source), "-ldl"], check=True)
|
||||
|
||||
@@ -33,6 +33,7 @@ int probe(void) {
|
||||
#include <dlfcn.h>
|
||||
int bl_fsync_syscall(long n, const long *a, long *r) { (void)n; (void)a; (void)r; return 0; }
|
||||
int bl_writewatch_syscall(long n, const long *a, long (*real)(long, ...), long *r) { (void)n; (void)a; (void)real; (void)r; return 0; }
|
||||
int bl_clock_syscall(long n, const long *a, long (*real)(long, ...), long *r) { (void)n; (void)a; (void)real; (void)r; return 0; }
|
||||
#include """ + '"' + robust.as_posix() + '"' + r"""
|
||||
int main(int argc, char **argv) {
|
||||
assert(argc == 3);
|
||||
|
||||
Reference in new issue
Block a user