Target Android SDK 37 for Play Store review

Integrate current main while preserving the original SDK migration history.
Update the Android toolchain, runtime permissions, foreground service lifecycle, storage access, native execution and platform API compatibility. Complete translations and regression coverage.

Validation: 3,260 JVM tests, 1,620 Python tests (eight skips), 171 Rust tests, six native regression programs and release lint with zero errors. Signed APK upgrade installed on the requested device. Signed app bundle validated with bundletool.

Review candidate: physical before/after feature checks, performance parity and native 16 KiB audio compatibility remain pending. Play Console acceptance has not been verified.
This commit is contained in:
maxjivi05 committed 2026-11-02 08:17:51 -05:00
commit b14587baeb
1553 files changed
+124616 -8790

No files matched your search

+314
View File
@@ -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 *)&real;
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
-4
View File
@@ -178,10 +178,6 @@ static int caller_is_winebus(void *caller) {
return strstr(info.dli_fname, "winebus.so") != NULL;
}
/* Wine's HID haptics give every XInput rumble a 1000 ms cutoff and xinput only writes on a change,
* but XInput motors hold their speed until the next XInputSetState; evdev's length 0 holds until
* stopped. Only this library, preloaded first, sees winebus as the caller. The length is put back
* because winebus compares its copy with the next state to decide whether to upload again. */
__attribute__((visibility("hidden"))) int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd,
unsigned long request, void *arg, void *caller,
int *rc) {
+2
View File
@@ -407,6 +407,7 @@ int bl_kgsl_poll(int (*real)(int, unsigned long, void *), int fd, unsigned long
int bl_ntsync_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
int bl_vdec_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
void *arg, void *caller, int *rc) __attribute__((visibility("hidden")));
int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg,
@@ -423,6 +424,7 @@ int ioctl(int fd, unsigned long request, ...) {
int rc;
if (bl_ntsync_ioctl(fd, request, arg, &rc)) return rc;
if (bl_venc_ioctl(fd, request, arg, &rc)) return rc;
if (bl_vdec_ioctl(fd, request, arg, &rc)) return rc;
if (bl_drm_gem_close(fd, request, arg, &rc)) return rc;
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc;
+17 -7
View File
@@ -43,7 +43,7 @@ __attribute__((constructor)) static void noexec_init(void) {
real_mprotect = (int (*)(void *, size_t, int))dlsym(RTLD_NEXT, "mprotect");
}
static int private_file_mapping(uintptr_t start, uintptr_t end) {
static int private_file_mapping(uintptr_t start, uintptr_t end, int *original_prot) {
int fd = (int)syscall(SYS_openat, AT_FDCWD, "/proc/self/maps", O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;
char buf[4096], line[1024];
@@ -67,6 +67,9 @@ static int private_file_mapping(uintptr_t start, uintptr_t end) {
char *path = strchr(perms, '/');
struct statvfs vfs;
found = path && !(statvfs(path, &vfs) == 0 && (vfs.f_flag & ST_NOEXEC));
if (found) *original_prot = (perms[0] == 'r' ? PROT_READ : 0) |
(perms[1] == 'w' ? PROT_WRITE : 0) |
(perms[2] == 'x' ? PROT_EXEC : 0);
}
}
syscall(SYS_close, fd);
@@ -119,12 +122,11 @@ static int write_all(int fd, const unsigned char *p, size_t len) {
static int rebake_shared(void *addr, size_t len, int prot) {
const char *directory = rx_directory();
size_t directory_len = strlen(directory);
if (!directory_len || directory_len + 40 >= PATH_MAX) return -1;
if (!directory_len || directory_len + 52 >= PATH_MAX) return -1;
char path[PATH_MAX], staged[PATH_MAX];
char *end = path + directory_len;
memcpy(path, directory, directory_len);
if (end[-1] != '/') *end++ = '/';
*end++ = '/';
end = put_hex(end, fnv1a(addr, len));
*end++ = '-';
end = put_hex(end, len);
@@ -155,16 +157,20 @@ static int rebake_shared(void *addr, size_t len, int prot) {
return mapped == (long)addr ? 0 : -1;
}
static int rebake(void *addr, size_t len, int prot) {
static int rebake(void *addr, size_t len, int prot, int original_prot) {
size_t page = getauxval(AT_PAGESZ);
if (page) len = (len + page - 1) & ~(page - 1);
if (real_mprotect(addr, len, PROT_READ)) return -1;
if (!rebake_shared(addr, len, prot)) return 0;
void *copy = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (copy == MAP_FAILED) return -1;
if (copy == MAP_FAILED) {
real_mprotect(addr, len, original_prot);
return -1;
}
memcpy(copy, addr, len);
if (mmap(addr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED) {
munmap(copy, len);
real_mprotect(addr, len, original_prot);
return -1;
}
memcpy(addr, copy, len);
@@ -179,8 +185,12 @@ int mprotect(void *addr, size_t len, int prot) {
if (result && errno == EACCES && (prot & PROT_EXEC)) {
if (in_fex()) {
result = real_mprotect(addr, len, prot & ~PROT_EXEC);
} else if (len && private_file_mapping((uintptr_t)addr, (uintptr_t)addr + len)) {
result = rebake(addr, len, prot);
} else {
int original_prot;
if (len && (uintptr_t)addr <= UINTPTR_MAX - len &&
private_file_mapping((uintptr_t)addr, (uintptr_t)addr + len, &original_prot)) {
result = rebake(addr, len, prot, original_prot);
}
}
if (result) errno = EACCES;
}
+289 -75
View File
@@ -17,6 +17,7 @@
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <time.h>
#include <ucontext.h>
#include <unistd.h>
struct ns_sem_args {
@@ -68,12 +69,13 @@ struct ns_wait_args {
#define NS_PROCS 256u
#define NS_FDS 65536
#define NS_DEVS 8
#define NS_VERSION 1u
#define NS_VERSION 2u
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
#define NS_DEV_MAGIC 0x4e54445600000000ull
#define NS_OBJ_MAGIC 0x27ull
#define NS_WAITERS 0x80000000u
#define NS_SWEEP_NS 2000000000ull
#define NS_SPIN_NS 4000ull
#define NS_DEAD 0xffffffffu
#ifndef CLOSE_RANGE_CLOEXEC
@@ -144,6 +146,7 @@ struct ns_q {
uint32_t waiting;
uint32_t inuse;
uint32_t next_free;
uint32_t sleeping;
struct ns_ent ent[NS_ENTS];
};
@@ -168,6 +171,7 @@ struct ns_tls {
struct ns_dev *d;
uint32_t q;
int waiting;
int sleep;
};
static struct ns_dev ns_devs[NS_DEVS];
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
}
}
static void ns_block(sigset_t *old) {
sigset_t s;
sigfillset(&s);
sigdelset(&s, SIGSEGV);
sigdelset(&s, SIGBUS);
sigdelset(&s, SIGILL);
sigdelset(&s, SIGFPE);
sigdelset(&s, SIGTRAP);
pthread_sigmask(SIG_BLOCK, &s, old);
#define NS_DEFER 4
#define NS_LEVELS 8
struct ns_cs {
int depth;
int count;
int level;
unsigned held_levels;
int sigs[NS_DEFER];
siginfo_t info[NS_DEFER];
sigset_t held[NS_LEVELS];
};
static __thread struct ns_cs ns_cs;
static uintptr_t ns_handlers[_NSIG];
static uint8_t ns_siginfo[_NSIG];
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
static int ns_deferrable(int sig) {
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
}
static void ns_restore(const sigset_t *old) {
pthread_sigmask(SIG_SETMASK, old, NULL);
static int ns_defer(int sig, const siginfo_t *si) {
struct ns_cs *cs = &ns_cs;
if (sig < SIGRTMIN)
for (int i = 0; i < NS_DEFER; i++)
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
if (i >= NS_DEFER) {
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
return 0;
}
if (si) memcpy(&cs->info[i], si, sizeof(*si));
else memset(&cs->info[i], 0, sizeof(*si));
__atomic_signal_fence(__ATOMIC_SEQ_CST);
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
return 1;
}
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
int level = ns_cs.level;
if (level < 0 || level >= NS_LEVELS) return 0;
sigset_t one;
sigemptyset(&one);
sigaddset(&one, sig);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &one, NULL);
pid_t pid = getpid(), tid = (pid_t)ns_self();
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
errno = saved;
if (!ok) return 0;
sigaddset(&uc->uc_sigmask, sig);
unsigned long *words = (unsigned long *)&ns_cs.held[level];
unsigned bits = 8 * sizeof(unsigned long);
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
return 1;
}
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
if (!h) return;
ns_cs.level++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
else ((void (*)(int))h)(sig);
__atomic_signal_fence(__ATOMIC_SEQ_CST);
ns_cs.level--;
}
static void ns_flush(void) {
struct ns_cs *cs = &ns_cs;
sigset_t all, mask;
sigfillset(&all);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &all, &mask);
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
pid_t pid = getpid(), tid = (pid_t)ns_self();
for (int i = 0; i < n; i++)
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
memset(cs->sigs, 0, sizeof(cs->sigs));
cs->count = 0;
int level = cs->level;
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
for (int sig = 1; sig < _NSIG; sig++)
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
sigemptyset(&cs->held[level]);
cs->held_levels &= ~(1u << level);
}
pthread_sigmask(SIG_SETMASK, &mask, NULL);
errno = saved;
}
static void ns_block(void) {
ns_cs.depth++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
}
static void ns_restore(void) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (--ns_cs.depth == 0) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
ns_flush();
}
}
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
static int (*real)(int, const struct sigaction *, struct sigaction *);
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
if (!real) {
errno = ENOSYS;
return -1;
}
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
struct sigaction mine;
const struct sigaction *use = act;
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
bl_from_ntdll(__builtin_return_address(0))) {
mine = *act;
mine.sa_sigaction = ns_trampoline;
mine.sa_flags |= SA_SIGINFO;
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
use = &mine;
}
int r = real(sig, use, old);
if (r != 0 && use == &mine) {
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
}
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
}
return r;
}
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
if (dead) return 0;
}
int32_t want = -1;
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
static void ns_wake(struct ns_q *q) {
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
int saved = errno;
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
errno = saved;
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
static void ns_dev_put(struct ns_dev *d) {
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
uint32_t zero = 0;
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
}
static void ns_forget(int fd) {
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
ns_forget_locked(fd);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
uint64_t e = 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
uint64_t e = 0;
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
if (d) {
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
}
}
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
static void ns_thread_gone(void *p) {
struct ns_tls *t = p;
if (!t || !t->d || !t->q) return;
sigset_t old;
if (!t || !t->d) return;
struct ns_dev *d = t->d;
ns_block(&old);
ns_free_q(d->h, t->q - 1);
ns_block();
if (t->q) ns_free_q(d->h, t->q - 1);
t->q = 0;
t->waiting = 0;
t->d = NULL;
ns_restore(&old);
ns_restore();
ns_dev_put(d);
}
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
pthread_key_create(&ns_key, ns_thread_gone);
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.d == d && ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
if (ns_tq.d && ns_tq.q) {
struct ns_dev *old = ns_tq.d;
ns_free_q(old->h, ns_tq.q - 1);
static int ns_hold(struct ns_dev *d) {
if (ns_tq.d == d) return 1;
if (!ns_dev_get(d)) return 0;
if (ns_tq.sleep) return 2;
struct ns_dev *old = ns_tq.d;
if (old) {
ns_block();
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
ns_tq.q = 0;
ns_tq.waiting = 0;
ns_tq.d = NULL;
ns_restore();
ns_dev_put(old);
}
pthread_once(&ns_once, ns_key_init);
ns_tq.d = d;
pthread_setspecific(ns_key, &ns_tq);
return 1;
}
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
uint32_t i;
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
ns_sweep_dead(h);
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_q *q = ns_q(h, i);
q->proc = (uint32_t)d->proc;
q->waiting = 0;
q->sleeping = 0;
q->inuse = 1;
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
ns_tq.d = d;
ns_tq.q = i + 1;
ns_tq.waiting = 0;
pthread_setspecific(ns_key, &ns_tq);
*qi = i;
return q;
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d != d) return NULL;
if (ns_tq.q && ns_tq.waiting) {
ns_drop_q(h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
struct ns_q *q = ns_new_q(d, qi);
if (!q) return NULL;
ns_tq.q = *qi + 1;
ns_tq.waiting = 0;
return q;
}
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
struct ns_head *h = d->h;
sigset_t old;
int fd = -1, err = 0;
uint32_t s = 0;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!ns_table() || ns_attach(d) < 0) {
err = ENOMEM;
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
if (fd < 0) errno = err;
return fd;
}
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
}
static uint64_t ns_now(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
}
static void ns_spin(struct ns_q *q) {
uint64_t end = ns_now() + NS_SPIN_NS;
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
ns_relax();
if (!(i & 63) && ns_now() >= end) return;
}
}
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
struct timespec ts, *tsp = NULL;
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
tsp = &ts;
}
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
if (tsp && ns_expired(args)) return ETIMEDOUT;
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
if (errno == ETIMEDOUT) return ETIMEDOUT;
if (errno == EINTR) return EINTR;
}
ns_spin(q);
int ret = 0;
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
ret = errno;
}
return 0;
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
return ret;
}
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
for (uint32_t i = 0; i < total; i++) {
struct ns_obj *o = ns_obj(h, slots[i]);
ns_lock(&o->lock);
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
if (!busy && ns_signaled(o, owner)) {
switch (o->type) {
case NS_SEM:
o->a--;
break;
case NS_MUTEX:
*dead = (int)o->ownerdead;
o->ownerdead = 0;
o->a++;
o->b = owner;
break;
case NS_EVENT:
if (!o->a) o->b = 0;
break;
}
ns_unlock(&o->lock);
return (int)i;
}
ns_unlock(&o->lock);
if (busy) return -1;
}
return -2;
}
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
for (uint32_t j = 0; j < i; j++)
if (slots[j] == slots[i]) return EINVAL;
}
sigset_t old;
ns_block(&old);
int nested = ns_tq.sleep > 0;
ns_block();
if (!all && (nested || !ns_tq.waiting)) {
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
if (got >= 0) {
ns_restore();
ua->index = (uint32_t)got;
return dead ? EOWNERDEAD : 0;
}
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
ns_restore();
return ETIMEDOUT;
}
}
uint32_t qi;
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
if (!q) {
ns_restore(&old);
ns_restore();
return ENOMEM;
}
ns_maybe_sweep(h);
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
q->total = total;
q->waiting = 1;
ns_tq.waiting = 1;
if (!nested) ns_tq.waiting = 1;
int bad = 0;
if (!all) {
for (uint32_t i = 0; i < total && !bad; i++) {
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
int ret = 0;
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
sigset_t held;
ns_restore(&old);
ns_tq.sleep++;
ns_restore();
ret = ns_sleep(q, &args);
ns_block(&held);
ns_block();
ns_tq.sleep--;
}
ns_drop_q(h, qi);
ns_tq.waiting = 0;
if (!nested) ns_tq.waiting = 0;
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
int dead = (int)q->ownerdead;
ns_restore(&old);
if (nested) ns_free_q(h, qi);
ns_restore();
if (bad) return EINVAL;
if (got != -1) {
ua->index = (uint32_t)got;
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
return ENOTTY;
}
if (type != (uint32_t)want) return EINVAL;
sigset_t old;
ns_block(&old);
ns_block();
int all = ns_lock_obj(h, o);
if (o->type != (uint32_t)want || o->gen != gen) {
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
return EINVAL;
}
switch (req) {
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
}
}
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
if (ret) return ret;
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
memcpy(arg, &prev, sizeof(prev));
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
uint64_t e = ns_lookup(fd);
if (!e) return 0;
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
if (!ns_dev_get(d)) return 0;
int held = ns_hold(d);
if (!held) return 0;
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
ns_dev_put(d);
if (held == 2) ns_dev_put(d);
if (err) {
errno = err;
*rc = -1;
@@ -1254,13 +1459,22 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
void bl_fsync_fds_closed(unsigned int first, unsigned int last) __attribute__((visibility("hidden")));
void bl_venc_closed(int fd) __attribute__((visibility("hidden")));
void bl_vdec_closed(int fd) __attribute__((visibility("hidden")));
int close(int fd) {
if (fd >= 0) bl_fsync_fds_closed((unsigned int)fd, (unsigned int)fd);
bl_venc_closed(fd);
bl_vdec_closed(fd);
uint64_t *t = __atomic_load_n(&ns_fdt, __ATOMIC_ACQUIRE);
if (t && fd >= 0 && fd < NS_FDS && __atomic_load_n(&t[fd], __ATOMIC_ACQUIRE)) ns_forget(fd);
return ns_close_fd(fd);
int saved = errno;
int mode = fd >= 0 ? fcntl(fd, F_GETFL) : -1;
int r = ns_close_fd(fd);
if (r < 0 && errno == EIO && mode >= 0 && (mode & O_ACCMODE) == O_RDONLY) {
errno = saved;
return 0;
}
return r;
}
int dup2(int oldfd, int newfd) {
+3
View File
@@ -15,6 +15,7 @@ int bl_status_without_tracer(const char *path, int flags, const void *caller) __
int bl_writable_retry(int dirfd, const char *path, int flags) __attribute__((visibility("hidden")));
int bl_ntsync_open(const char *path, int flags) __attribute__((visibility("hidden")));
int bl_venc_open(const char *path, int flags) __attribute__((visibility("hidden")));
int bl_vdec_open(const char *path, int flags) __attribute__((visibility("hidden")));
int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode,
int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden")));
@@ -55,6 +56,7 @@ int open(const char *path, int flags, ...) {
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open");
if (real_open == NULL) {
errno = ENOSYS;
@@ -81,6 +83,7 @@ int openat(int dirfd, const char *path, int flags, ...) {
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat");
if (real_openat == NULL) {
errno = ENOSYS;
+41
View File
@@ -0,0 +1,41 @@
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <unistd.h>
#define FUSE_MAGIC 0x65735546
#define SDCARDFS_MAGIC 0x5dca2df5
static int is_wineserver(void) {
static int cached = -1;
char exe[PATH_MAX];
const char *base;
ssize_t n;
if (cached >= 0) return cached;
n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (n <= 0) {
cached = 0;
return cached;
}
exe[n] = '\0';
base = strrchr(exe, '/');
cached = strcmp(base ? base + 1 : exe, "wineserver") == 0;
return cached;
}
__attribute__((visibility("hidden")))
void bl_storage_owner(int fd, struct stat *st) {
struct statfs fs;
int saved = errno;
uid_t uid;
if (st != NULL && is_wineserver()) {
uid = getuid();
if (st->st_uid != uid && fstatfs(fd, &fs) == 0 && (fs.f_type == FUSE_MAGIC || fs.f_type == SDCARDFS_MAGIC)) st->st_uid = uid;
}
errno = saved;
}
+21 -2
View File
@@ -139,17 +139,31 @@ static int (*next_faccessat(void))(int, const char *, int, int) {
}
__attribute__((visibility("hidden"))) int bl_binfmt_runnable(const char *path, int mode);
__attribute__((visibility("hidden"))) int bl_writable_access(int dirfd, const char *path, int mode, int flags);
static int writable_as_owner(int (*real)(int, const char *, int, int), int dirfd, const char *path, int mode, int flags) {
if (!bl_writable_access(dirfd, path, mode, flags)) return -1;
if ((mode & ~W_OK) == 0 || real(dirfd, path, mode & ~W_OK, flags) == 0) return 0;
return -1;
}
static int check_cwd(const char *path, int mode, int flags) {
int (*real)(int, const char *, int, int) = next_faccessat();
int result;
int err;
if (real == NULL) {
errno = ENOSYS;
return -1;
}
result = real(AT_FDCWD, path, mode, flags);
return result != 0 && errno == EACCES && bl_binfmt_runnable(path, mode) ? 0 : result;
if (result == 0) return 0;
err = errno;
if (err == EACCES && bl_binfmt_runnable(path, mode)) return 0;
errno = err;
if (writable_as_owner(real, AT_FDCWD, path, mode, flags) == 0) return 0;
errno = err;
return result;
}
int access(const char *path, int mode) {
@@ -166,6 +180,7 @@ int eaccess(const char *path, int mode) {
int faccessat(int dirfd, const char *path, int mode, int flags) {
int (*real)(int, const char *, int, int);
int err;
if (dirfd == AT_FDCWD) {
return cached_access(path, mode, flags, check_cwd);
@@ -175,5 +190,9 @@ int faccessat(int dirfd, const char *path, int mode, int flags) {
errno = ENOSYS;
return -1;
}
return real(dirfd, path, mode, flags);
if (real(dirfd, path, mode, flags) == 0) return 0;
err = errno;
if (writable_as_owner(real, dirfd, path, mode, flags) == 0) return 0;
errno = err;
return -1;
}
+42 -1
View File
@@ -3,6 +3,7 @@
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -23,6 +24,34 @@ static int is_steam_client(void) {
return cached;
}
static int wine_name(const char *path) {
const char *base;
size_t n;
if (path == NULL) return 0;
base = strrchr(path, '/');
base = base ? base + 1 : path;
n = strlen(path);
return strncmp(base, "wine", 4) == 0 || strstr(path, "/lib/wine/") != NULL
|| (n > 4 && strcasecmp(path + n - 4, ".exe") == 0);
}
static int is_wine(void) {
static int cached = -1;
char exe[PATH_MAX];
ssize_t n;
if (cached >= 0) return cached;
n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (n > 0) exe[n] = '\0';
cached = (n > 0 && wine_name(exe)) || wine_name(program_invocation_name);
return cached;
}
static int acts_as_root(void) {
return is_steam_client() || !is_wine();
}
static int grant_owner_write(int dirfd, const char *path) {
struct stat st;
@@ -37,7 +66,7 @@ int bl_writable_retry(int dirfd, const char *path, int flags) {
char parent[PATH_MAX];
const char *slash;
if (saved != EACCES || path == NULL || (flags & O_ACCMODE) == O_RDONLY || !is_steam_client()) return 0;
if (saved != EACCES || path == NULL || (flags & O_ACCMODE) == O_RDONLY || !acts_as_root()) return 0;
if (faccessat(dirfd, path, F_OK, 0) == 0) {
granted = grant_owner_write(dirfd, path);
} else if (flags & O_CREAT) {
@@ -53,3 +82,15 @@ int bl_writable_retry(int dirfd, const char *path, int flags) {
errno = saved;
return granted;
}
__attribute__((visibility("hidden")))
int bl_writable_access(int dirfd, const char *path, int mode, int flags) {
int saved = errno;
struct stat st;
int owned;
if (saved != EACCES || path == NULL || !(mode & W_OK) || !acts_as_root()) return 0;
owned = fstatat(dirfd, path, &st, flags & AT_SYMLINK_NOFOLLOW) == 0 && st.st_uid == geteuid();
errno = saved;
return owned;
}
-3
View File
@@ -1,5 +1,3 @@
/* In Deck mode (-steamos3) the x86-64 client's SteamOS audio manager passes libpulse the NULL operation of a
* failed request, and libpulse aborts on it ("Assertion 'o' failed ... pa_operation_get_state()"). */
#if defined(__x86_64__) || defined(__i386__)
#define _GNU_SOURCE
#include <dlfcn.h>
@@ -9,7 +7,6 @@ typedef struct pa_operation pa_operation;
#define PA_OPERATION_CANCELLED 2
/* A libpulse loaded with RTLD_LOCAL (Wine's winepulse.so) is not in RTLD_NEXT's scope. */
static void *libpulse(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (!fn) {
+43 -14
View File
@@ -23,7 +23,6 @@
#define BL_RESOLVE_BENEATH 0x08
#define BL_RESOLVE_IN_ROOT 0x10
// The kernel's MAXSYMLINKS.
#define BL_MAX_SYMLINKS 40
#define BL_PROC_ROOT_INO 1
@@ -49,6 +48,7 @@ static int openat2_missing;
static int (*next_fstatat)(int, const char *, bl_stat *, int);
static int (*next_openat)(int, const char *, int, ...);
static ssize_t (*next_readlinkat)(int, const char *, char *, size_t);
static int (*next_statx)(int, const char *, int, unsigned int, struct statx *);
static int find_fastpath(struct dl_phdr_info *info, size_t size, void *name) {
(void)size;
@@ -68,6 +68,7 @@ __attribute__((constructor)) static void find_fastpath_calls(void) {
next_fstatat = (int (*)(int, const char *, bl_stat *, int))dlsym(fastpath, BL_FSTATAT_NAME);
next_openat = (int (*)(int, const char *, int, ...))dlsym(fastpath, "openat");
next_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(fastpath, "readlinkat");
next_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(fastpath, "statx");
}
}
@@ -92,13 +93,19 @@ static long path_readlink(int dirfd, const char *path, char *buf, size_t size) {
return next_readlinkat(dirfd, path, buf, size);
}
static long path_statx(int dirfd, const char *path, int flags, unsigned int mask, struct statx *out) {
if (!next_statx) {
return real_syscall(SYS_statx, (long)dirfd, (long)path, (long)flags, (long)mask, (long)out);
}
return next_statx(dirfd, path, flags, mask, out);
}
void *dlopen(const char *filename, int flags) {
if (!real_dlopen) {
real_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen");
}
const char *basename = filename ? strrchr(filename, '/') : NULL;
basename = basename ? basename + 1 : filename;
// Steam's deep binding bypasses the robust-list shim under PRoot.
if (basename && strcmp(basename, "vrclient.so") == 0) {
flags &= ~RTLD_DEEPBIND;
}
@@ -139,7 +146,6 @@ static struct robust_list_head *thread_robust_head(void) {
return head;
}
// Magic links are procfs links below a process's directory; procfs's root holds only plain ones (self, thread-self, mounts).
static int is_magic_link(int dirfd, char *link, size_t parent_len) {
int fd = (int)path_open(dirfd, link, O_PATH | O_NOFOLLOW | O_CLOEXEC, 0L);
if (fd < 0) {
@@ -159,7 +165,6 @@ static int is_magic_link(int dirfd, char *link, size_t parent_len) {
return result != 0 || parent.st_ino != BL_PROC_ROOT_INO;
}
// RESOLVE_IN_ROOT walked one component at a time: absolute symlinks restart at dirfd and ".." stops there.
static long openat_in_root(int dirfd, const char *path, int flags, long mode) {
char resolved[PATH_MAX];
char pending[PATH_MAX];
@@ -168,7 +173,6 @@ static long openat_in_root(int dirfd, const char *path, int flags, long mode) {
int have_root = 0;
int root_on_procfs = 0;
dev_t root_dev = 0;
// O_NOFOLLOW and O_CREAT | O_EXCL leave a final symlink alone, unless a trailing slash follows it.
int nofollow = (flags & O_NOFOLLOW) || (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
const char *rest = path;
for (;;) {
@@ -232,7 +236,6 @@ static long openat_in_root(int dirfd, const char *path, int flags, long mode) {
have_root = 1;
}
if ((st.st_dev != root_dev || root_on_procfs) && is_magic_link(dirfd, resolved, resolved_len)) {
// RESOLVE_IN_ROOT refuses magic links.
errno = EXDEV;
return -1;
}
@@ -329,7 +332,28 @@ static int execveat_is_execve(const long *args) {
return path[0] == '/' || (int)args[0] == AT_FDCWD;
}
static long openat2_call(const long *args) {
if (!real_syscall) {
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
}
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
long fd = real_syscall(SYS_openat2, args[0], args[1], args[2], args[3]);
if (fd != -1 || errno != ENOSYS) {
return fd;
}
__atomic_store_n(&openat2_missing, 1, __ATOMIC_RELAXED);
}
return openat2_via_openat(args);
}
int bl_openat2(int dirfd, const char *path, void *how, size_t size) __asm__("openat2");
int bl_openat2(int dirfd, const char *path, void *how, size_t size) {
long args[6] = {dirfd, (long)path, (long)how, (long)size, 0, 0};
return (int)openat2_call(args);
}
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;
@@ -356,22 +380,27 @@ 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;
}
if (number == SYS_openat2) {
if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) {
long fd = real_syscall(number, args[0], args[1], args[2], args[3]);
if (fd != -1 || errno != ENOSYS) {
return fd;
}
__atomic_store_n(&openat2_missing, 1, __ATOMIC_RELAXED);
}
return openat2_via_openat(args);
return openat2_call(args);
}
if (number == SYS_openat) {
return path_open((int)args[0], (const char *)args[1], (int)args[2], args[3]);
}
if (number == BL_SYS_FSTATAT) {
return path_stat((int)args[0], (const char *)args[1], (bl_stat *)args[2], (int)args[3]);
}
if (number == SYS_readlinkat) {
return path_readlink((int)args[0], (const char *)args[1], (char *)args[2], (size_t)args[3]);
}
if (number == SYS_statx) {
return path_statx((int)args[0], (const char *)args[1], (int)args[2], (unsigned int)args[3], (struct statx *)args[4]);
}
if (number == SYS_execveat && execveat_is_execve(args)) {
return real_syscall(SYS_execve, args[1], args[2], args[3]);
}
+9 -5
View File
@@ -1,6 +1,3 @@
/* Steam's Linux Remote Play host has no touch injection: client touches arrive as an XTest mouse (absolute
* motion plus button 1). While a stream holds the encoder, those become touches into gamescope's own touch
* input, as a finger on the device screen would. */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <pthread.h>
@@ -51,9 +48,16 @@ static void rt_send(void *dpy, uint32_t action) {
}
}
static void *rt_xtst(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (fn) return fn;
void *lib = dlopen("libXtst.so.6", RTLD_LAZY | RTLD_NOLOAD);
return lib ? dlsym(lib, name) : NULL;
}
int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay) {
static int (*real)(void *, int, int, int, unsigned long);
if (!real) real = (int (*)(void *, int, int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeMotionEvent");
if (!real) real = (int (*)(void *, int, int, int, unsigned long))rt_xtst("XTestFakeMotionEvent");
if (rt_enabled()) {
pthread_mutex_lock(&rt_lock);
rt_x = x;
@@ -69,7 +73,7 @@ int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long dela
int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay) {
static int (*real)(void *, unsigned int, int, unsigned long);
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeButtonEvent");
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))rt_xtst("XTestFakeButtonEvent");
if (button == 1) {
pthread_mutex_lock(&rt_lock);
int handled = 0;
+7 -1
View File
@@ -13,6 +13,7 @@
#include <unistd.h>
static int (*next_fstat)(int, struct stat *);
void bl_storage_owner(int fd, struct stat *st) __attribute__((visibility("hidden")));
static int (*next_fallocate)(int, int, off_t, off_t);
static int (*next_posix_fallocate)(int, off_t, off_t);
static ssize_t (*next_pwrite)(int, const void *, size_t, off_t);
@@ -285,12 +286,17 @@ int fstat(int fd, struct stat *out) {
if (next_fstat == NULL) { errno = ENOSYS; return -1; }
initialize();
errno = entry_errno;
if (!enabled) return next_fstat(fd, out);
if (!enabled) {
int result = next_fstat(fd, out);
if (result == 0) bl_storage_owner(fd, out);
return result;
}
uint64_t start = now_ns();
errno = entry_errno;
int result = next_fstat(fd, out);
int call_errno = errno;
uint64_t elapsed = now_ns() - start;
if (result == 0) bl_storage_owner(fd, out);
if (result == 0 && on_selected_device(fd, out)) {
add(&stats.fstat_calls, 1);
add(&stats.fstat_elapsed_ns, elapsed);
+2 -2
View File
@@ -195,8 +195,8 @@ static unsigned murmur_hash2(const char *key, size_t len) {
len -= 4;
}
switch (len) {
case 3: h ^= data[2] << 16; /* fallthrough */
case 2: h ^= data[1] << 8; /* fallthrough */
case 3: h ^= data[2] << 16; __attribute__((fallthrough));
case 2: h ^= data[1] << 8; __attribute__((fallthrough));
case 1: h ^= data[0]; h *= m;
}
h ^= h >> 13;
+978
View File
@@ -0,0 +1,978 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <poll.h>
#include <stddef.h>
#if defined(__aarch64__) || defined(BL_VDEC_ANY_ARCH)
#include <fcntl.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "../../../app/src/main/cpp/venc/venc_proto.h"
#define VDEC_DEVICE "/dev/video-dec0"
#define VDEC_INSTANCES 4
#define VDEC_BUFFERS 16
#define VDEC_BITSTREAM_SIZE (4u << 20)
#define VDEC_PADDING 64
#define VDEC_DUMB 64
struct vd_dma_heap_alloc {
uint64_t len;
uint32_t fd;
uint32_t fd_flags;
uint64_t heap_flags;
};
#define VD_DMA_HEAP_IOCTL_ALLOC _IOWR('H', 0x0, struct vd_dma_heap_alloc)
struct vd_dma_buf_sync {
uint64_t flags;
};
#define VD_DMA_BUF_SYNC_WRITE 2u
#define VD_DMA_BUF_SYNC_START 0u
#define VD_DMA_BUF_SYNC_END 4u
#define VD_DMA_BUF_IOCTL_SYNC _IOW('b', 0, struct vd_dma_buf_sync)
struct vd_create_dumb {
uint32_t height, width, bpp, flags, handle, pitch;
uint64_t size;
};
struct vd_destroy_dumb {
uint32_t handle;
};
struct vd_prime_handle {
uint32_t handle, flags;
int32_t fd;
};
#define VD_IOCTL_MODE_CREATE_DUMB _IOWR('d', 0xB2, struct vd_create_dumb)
#define VD_IOCTL_MODE_DESTROY_DUMB _IOWR('d', 0xB4, struct vd_destroy_dumb)
#define VD_IOCTL_PRIME_HANDLE_TO_FD _IOWR('d', 0x2d, struct vd_prime_handle)
struct vd_mode_info {
uint32_t clock;
uint16_t hdisplay, hsync_start, hsync_end, htotal, hskew;
uint16_t vdisplay, vsync_start, vsync_end, vtotal, vscan;
uint32_t vrefresh, flags, type;
char name[32];
};
struct vd_res {
int count_fbs;
uint32_t *fbs;
int count_crtcs;
uint32_t *crtcs;
int count_connectors;
uint32_t *connectors;
int count_encoders;
uint32_t *encoders;
uint32_t min_width, max_width, min_height, max_height;
};
struct vd_connector {
uint32_t connector_id, encoder_id, connector_type, connector_type_id;
int connection;
uint32_t mm_width, mm_height;
int subpixel;
int count_modes;
struct vd_mode_info *modes;
int count_props;
uint32_t *props;
uint64_t *prop_values;
int count_encoders;
uint32_t *encoders;
};
struct vd_encoder {
uint32_t encoder_id, encoder_type, crtc_id, possible_crtcs, possible_clones;
};
struct vd_crtc {
uint32_t crtc_id, buffer_id, x, y, width, height;
int mode_valid;
struct vd_mode_info mode;
int gamma_size;
};
struct vd_device {
char **nodes;
int available_nodes;
int bustype;
void *businfo;
void *deviceinfo;
};
struct vd_capture {
int fd;
ino_t ino;
uint8_t *map;
size_t len;
};
struct vd_done {
uint32_t index;
struct timeval timestamp;
};
struct vdec {
int used, fd;
uint32_t fourcc, width, height;
uint32_t cap_width, cap_height, bpl, cap_size;
uint8_t *bitstream;
uint32_t out_count;
uint32_t out_done[VDEC_BUFFERS], n_out_done;
struct vd_capture cap[VDEC_BUFFERS];
uint32_t cap_count;
uint32_t cap_queue[VDEC_BUFFERS], n_cap_queue;
struct vd_done cap_done[VDEC_BUFFERS];
uint32_t n_cap_done;
int out_on, cap_on;
int sock, hw, hw_failed;
int64_t pts;
void *ctx, *packet, *frame;
uint8_t *packet_data;
size_t packet_capacity;
int last;
uint32_t frames, slow;
double decode_ms;
};
static struct {
void *(*find_decoder_by_name)(const char *);
void *(*alloc_context3)(const void *);
int (*open2)(void *, const void *, void **);
void (*free_context)(void **);
int (*send_packet)(void *, const void *);
int (*receive_frame)(void *, void *);
void (*flush_buffers)(void *);
void *(*packet_alloc)(void);
void (*packet_free)(void **);
void *(*frame_alloc)(void);
void (*frame_free)(void **);
void (*frame_unref)(void *);
int (*dict_set)(void **, const char *, const char *, int);
void (*dict_free)(void **);
const char *(*pix_fmt_name)(int);
} av;
static pthread_mutex_t vd_lock = PTHREAD_MUTEX_INITIALIZER;
static struct vdec vd_all[VDEC_INSTANCES];
static int vd_open_count;
static int vd_state;
static int vd_ffmpeg;
static int vd_heap = -1;
static int vd_dumb_fd[VDEC_DUMB];
static char vd_drm_handle;
static void vd_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
static void vd_log(const char *fmt, ...) {
va_list ap;
char line[256];
va_start(ap, fmt);
int n = vsnprintf(line, sizeof line - 1, fmt, ap);
va_end(ap);
if (n < 0) return;
if (n > (int)sizeof line - 2) n = sizeof line - 2;
line[n++] = '\n';
if (write(2, "droiddeck-vdec: ", 16) < 0 || write(2, line, n) < 0) return;
}
static int vd_load_ffmpeg(void) {
void *codec = dlopen("libavcodec.so.62", RTLD_LAZY | RTLD_LOCAL);
void *util = codec ? dlopen("libavutil.so.60", RTLD_LAZY | RTLD_LOCAL) : NULL;
if (codec == NULL || util == NULL) {
vd_log("FFmpeg 8 is not available (%s), Remote Play decoding stays off", dlerror());
return 0;
}
#define VD_SYM(lib, field, name) \
if ((*(void **)&av.field = dlsym(lib, name)) == NULL) { \
vd_log("FFmpeg has no %s, Remote Play decoding stays off", name); \
return 0; \
}
VD_SYM(codec, find_decoder_by_name, "avcodec_find_decoder_by_name")
VD_SYM(codec, alloc_context3, "avcodec_alloc_context3")
VD_SYM(codec, open2, "avcodec_open2")
VD_SYM(codec, free_context, "avcodec_free_context")
VD_SYM(codec, send_packet, "avcodec_send_packet")
VD_SYM(codec, receive_frame, "avcodec_receive_frame")
VD_SYM(codec, flush_buffers, "avcodec_flush_buffers")
VD_SYM(codec, packet_alloc, "av_packet_alloc")
VD_SYM(codec, packet_free, "av_packet_free")
VD_SYM(util, frame_alloc, "av_frame_alloc")
VD_SYM(util, frame_free, "av_frame_free")
VD_SYM(util, frame_unref, "av_frame_unref")
VD_SYM(util, dict_set, "av_dict_set")
VD_SYM(util, dict_free, "av_dict_free")
VD_SYM(util, pix_fmt_name, "av_get_pix_fmt_name")
#undef VD_SYM
return 1;
}
static int vd_have_ffmpeg(void) {
if (vd_ffmpeg == 0) vd_ffmpeg = vd_load_ffmpeg() ? 1 : -1;
return vd_ffmpeg > 0;
}
__attribute__((visibility("hidden"))) int bl_venc_connect(void);
static int vd_enabled(void) {
pthread_mutex_lock(&vd_lock);
if (vd_state == 0) {
const char *flag = getenv("BL_REMOTE_PLAY_DECODE"), *socket = getenv("BL_VENC_SOCKET");
vd_state = -1;
if (strcmp(program_invocation_short_name, "streaming_client") == 0 && (flag == NULL || strcmp(flag, "0") != 0)) {
vd_heap = open("/dev/dma_heap/system", O_RDONLY | O_CLOEXEC);
if (vd_heap < 0) vd_log("no /dev/dma_heap/system (%s), Remote Play decoding stays off", strerror(errno));
else if ((socket && *socket) || vd_have_ffmpeg()) vd_state = 1;
for (int i = 0; i < VDEC_DUMB; i++) vd_dumb_fd[i] = -1;
}
}
int on = vd_state > 0;
pthread_mutex_unlock(&vd_lock);
return on;
}
static struct vdec *vd_find(int fd) {
if (fd < 0 || __atomic_load_n(&vd_open_count, __ATOMIC_ACQUIRE) == 0) return NULL;
for (int i = 0; i < VDEC_INSTANCES; i++) if (vd_all[i].used && vd_all[i].fd == fd) return &vd_all[i];
return NULL;
}
__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) {
(void)flags;
if (path == NULL || strcmp(path, VDEC_DEVICE) != 0 || !vd_enabled()) return -2;
int fd = memfd_create("droiddeck-vdec", 0);
if (fd < 0) return -1;
pthread_mutex_lock(&vd_lock);
struct vdec *v = NULL;
for (int i = 0; i < VDEC_INSTANCES && v == NULL; i++) if (!vd_all[i].used) v = &vd_all[i];
if (v == NULL) {
pthread_mutex_unlock(&vd_lock);
close(fd);
errno = EBUSY;
return -1;
}
memset(v, 0, sizeof *v);
v->used = 1;
v->fd = fd;
v->fourcc = V4L2_PIX_FMT_H264;
v->last = -1;
v->sock = -1;
for (int i = 0; i < VDEC_BUFFERS; i++) v->cap[i].fd = -1;
__atomic_add_fetch(&vd_open_count, 1, __ATOMIC_RELEASE);
pthread_mutex_unlock(&vd_lock);
return fd;
}
static void vd_close_decoder(struct vdec *v) {
if (v->frames) {
vd_log("%u frames, %.1f ms each on average, %u over 25 ms", v->frames, v->decode_ms / v->frames, v->slow);
}
if (v->sock >= 0) close(v->sock);
v->sock = -1;
v->hw = 0;
if (v->ctx) av.free_context(&v->ctx);
if (v->packet) av.packet_free(&v->packet);
if (v->frame) av.frame_free(&v->frame);
free(v->packet_data);
v->packet_data = NULL;
v->packet_capacity = 0;
v->frames = v->slow = 0;
v->decode_ms = 0;
v->last = -1;
}
static int vd_send(int sock, const void *msg, size_t len, int fd) {
struct iovec iov = { .iov_base = (void *)msg, .iov_len = len };
char control[CMSG_SPACE(sizeof(int))];
struct msghdr m = { .msg_iov = &iov, .msg_iovlen = 1 };
if (fd >= 0) {
memset(control, 0, sizeof control);
m.msg_control = control;
m.msg_controllen = sizeof control;
struct cmsghdr *c = CMSG_FIRSTHDR(&m);
c->cmsg_level = SOL_SOCKET;
c->cmsg_type = SCM_RIGHTS;
c->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(c), &fd, sizeof fd);
}
return sendmsg(sock, &m, MSG_NOSIGNAL) == (ssize_t)len ? 0 : -1;
}
static void vd_send_capture(struct vdec *v, uint32_t index) {
struct vdec_capture c = { .type = VDEC_CAPTURE, .index = index, .size = (uint32_t)v->cap[index].len };
if (v->sock >= 0 && v->cap[index].map && vd_send(v->sock, &c, sizeof c, v->cap[index].fd) != 0)
vd_log("could not hand capture buffer %u to the app: %s", index, strerror(errno));
}
static int vd_open_hardware(struct vdec *v) {
if (v->hw_failed) return -1;
int sock = bl_venc_connect();
if (sock < 0) return -1;
struct vdec_config c = {
.type = VDEC_CONFIG, .codec = v->fourcc == V4L2_PIX_FMT_HEVC ? VENC_HEVC : VENC_H264,
.width = v->width ? v->width : v->cap_width, .height = v->height ? v->height : v->cap_height,
.stride = v->bpl, .rows = v->cap_height, .bitstreams = v->out_count, .bitstream_size = VDEC_BITSTREAM_SIZE,
};
struct vdec_result r;
if (vd_send(sock, &c, sizeof c, v->fd) != 0 || recv(sock, &r, sizeof r, 0) != (ssize_t)sizeof r ||
r.type != VDEC_RESULT || r.status != 0) {
vd_log("the app has no %s decoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height);
close(sock);
v->hw_failed = 1;
return -1;
}
v->sock = sock;
v->hw = 1;
for (uint32_t i = 0; i < v->cap_count; i++) vd_send_capture(v, i);
vd_log("decoding %s %ux%u with the app's MediaCodec into %ux%u NV12 dma-bufs",
c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height, v->cap_width, v->cap_height);
return 0;
}
static int vd_open_decoder(struct vdec *v) {
if (vd_open_hardware(v) == 0) return 0;
if (!vd_have_ffmpeg()) return -1;
const char *name = v->fourcc == V4L2_PIX_FMT_HEVC ? "hevc" : "h264";
const void *codec = av.find_decoder_by_name(name);
if (codec == NULL) {
vd_log("FFmpeg has no %s decoder", name);
return -1;
}
v->ctx = av.alloc_context3(codec);
v->packet = av.packet_alloc();
v->frame = av.frame_alloc();
void *options = NULL;
long cores = sysconf(_SC_NPROCESSORS_ONLN);
char threads[8];
snprintf(threads, sizeof threads, "%ld", cores > 4 ? 4L : cores < 1 ? 1L : cores);
av.dict_set(&options, "threads", threads, 0);
av.dict_set(&options, "thread_type", "slice", 0);
av.dict_set(&options, "flags", "+low_delay", 0);
av.dict_set(&options, "flags2", "+fast", 0);
int r = v->ctx && v->packet && v->frame ? av.open2(v->ctx, codec, &options) : -1;
av.dict_free(&options);
if (r < 0) {
vd_log("could not open the %s decoder (%d)", name, r);
vd_close_decoder(v);
return -1;
}
vd_log("decoding %s %ux%u in software into %ux%u NV12 dma-bufs", v->fourcc == V4L2_PIX_FMT_HEVC ? "HEVC" : "H.264",
v->width, v->height, v->cap_width, v->cap_height);
return 0;
}
static void vd_unmap_capture(struct vdec *v) {
for (uint32_t i = 0; i < VDEC_BUFFERS; i++) {
if (v->cap[i].map) munmap(v->cap[i].map, v->cap[i].len);
v->cap[i] = (struct vd_capture){ .fd = -1 };
}
v->cap_count = v->n_cap_queue = v->n_cap_done = 0;
v->last = -1;
}
static void vd_sync(int fd, uint64_t flags) {
struct vd_dma_buf_sync s = { .flags = flags | VD_DMA_BUF_SYNC_WRITE };
ioctl(fd, VD_DMA_BUF_IOCTL_SYNC, &s);
}
static void vd_blank(struct vdec *v, struct vd_capture *c) {
size_t luma = (size_t)v->bpl * v->cap_height;
if (luma + luma / 2 > c->len) return;
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
memset(c->map, 16, luma);
memset(c->map + luma, 128, luma / 2);
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
}
static int vd_map_capture(struct vdec *v, uint32_t index, int fd) {
struct stat st;
if (fd < 0 || fstat(fd, &st) != 0) return -EINVAL;
struct vd_capture *c = &v->cap[index];
if (c->map && c->ino == st.st_ino) {
c->fd = fd;
return 0;
}
if (c->map) {
munmap(c->map, c->len);
*c = (struct vd_capture){ .fd = -1 };
if (v->last == (int)index) v->last = -1;
}
off_t len = lseek(fd, 0, SEEK_END);
if (len < (off_t)v->cap_size) return -EINVAL;
void *p = mmap(NULL, (size_t)len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
vd_log("could not map capture buffer %u: %s", index, strerror(errno));
*c = (struct vd_capture){ .fd = -1 };
return -EINVAL;
}
*c = (struct vd_capture){ .fd = fd, .ino = st.st_ino, .map = p, .len = (size_t)len };
vd_blank(v, c);
if (v->hw) vd_send_capture(v, index);
return 0;
}
static int vd_write_frame(struct vdec *v, struct vd_capture *c) {
uint8_t **data = (uint8_t **)v->frame;
int *linesize = (int *)((uint8_t *)v->frame + 64);
int width = *(int *)((uint8_t *)v->frame + 104), height = *(int *)((uint8_t *)v->frame + 108);
const char *format = av.pix_fmt_name(*(int *)((uint8_t *)v->frame + 116));
if (format == NULL || width <= 0 || height <= 0) return -1;
int nv12 = strcmp(format, "nv12") == 0;
if (!nv12 && strcmp(format, "yuv420p") != 0 && strcmp(format, "yuvj420p") != 0) {
static int warned;
if (!warned++) vd_log("cannot show %s frames", format);
return -1;
}
uint32_t w = (uint32_t)width < v->bpl ? (uint32_t)width : v->bpl;
uint32_t h = (uint32_t)height < v->cap_height ? (uint32_t)height : v->cap_height;
uint8_t *y = c->map, *uv = c->map + (size_t)v->bpl * v->cap_height;
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
for (uint32_t row = 0; row < h; row++) memcpy(y + (size_t)row * v->bpl, data[0] + (size_t)row * linesize[0], w);
for (uint32_t row = 0; row < (h + 1) / 2; row++) {
uint8_t *dst = uv + (size_t)row * v->bpl;
if (nv12) {
memcpy(dst, data[1] + (size_t)row * linesize[1], w & ~1u);
continue;
}
const uint8_t *u = data[1] + (size_t)row * linesize[1], *vv = data[2] + (size_t)row * linesize[2];
for (uint32_t x = 0; x < w / 2; x++) {
dst[2 * x] = u[x];
dst[2 * x + 1] = vv[x];
}
}
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
return 0;
}
static void vd_copy_last(struct vdec *v, struct vd_capture *c) {
if (v->last < 0 || v->cap[v->last].map == NULL || &v->cap[v->last] == c) return;
size_t size = v->cap_size < c->len ? v->cap_size : c->len;
vd_sync(c->fd, VD_DMA_BUF_SYNC_START);
memcpy(c->map, v->cap[v->last].map, size);
vd_sync(c->fd, VD_DMA_BUF_SYNC_END);
}
static double vd_now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6;
}
static void vd_deliver(struct vdec *v, int written, double start, struct timeval timestamp) {
uint32_t target = v->cap_queue[0];
if (!written) {
if (v->last < 0) return;
vd_copy_last(v, &v->cap[target]);
}
double ms = vd_now_ms() - start;
v->frames++;
v->decode_ms += ms;
if (ms > 25.0) v->slow++;
memmove(v->cap_queue, v->cap_queue + 1, --v->n_cap_queue * sizeof v->cap_queue[0]);
v->cap_done[v->n_cap_done++] = (struct vd_done){ .index = target, .timestamp = timestamp };
v->last = (int)target;
}
static void vd_decode_hardware(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) {
double start = vd_now_ms();
v->pts += 16667;
struct vdec_packet p = { .type = VDEC_PACKET, .input = index, .bytes = bytes, .capture = v->cap_queue[0], .pts_us = v->pts };
struct vdec_result r;
if (vd_send(v->sock, &p, sizeof p, -1) != 0 || recv(v->sock, &r, sizeof r, 0) != (ssize_t)sizeof r ||
r.type != VDEC_RESULT) {
vd_log("lost the app's decoder (%s), switching to software", strerror(errno));
vd_close_decoder(v);
v->hw_failed = 1;
return;
}
vd_deliver(v, r.status == 0 && r.frame, start, timestamp);
}
static void vd_decode(struct vdec *v, uint32_t index, uint32_t bytes, struct timeval timestamp) {
if (!v->cap_on || v->n_cap_queue == 0) return;
if (v->ctx == NULL && !v->hw && vd_open_decoder(v) != 0) return;
if (v->hw) {
vd_decode_hardware(v, index, bytes, timestamp);
return;
}
if (v->packet_capacity < bytes + VDEC_PADDING) {
free(v->packet_data);
v->packet_capacity = bytes + VDEC_PADDING + (64u << 10);
v->packet_data = malloc(v->packet_capacity);
if (v->packet_data == NULL) {
v->packet_capacity = 0;
return;
}
}
memcpy(v->packet_data, v->bitstream + (size_t)index * VDEC_BITSTREAM_SIZE, bytes);
memset(v->packet_data + bytes, 0, VDEC_PADDING);
*(uint8_t **)((uint8_t *)v->packet + 24) = v->packet_data;
*(int *)((uint8_t *)v->packet + 32) = (int)bytes;
double start = vd_now_ms();
uint32_t target = v->cap_queue[0];
struct vd_capture *c = &v->cap[target];
int written = 0;
if (av.send_packet(v->ctx, v->packet) >= 0) {
while (av.receive_frame(v->ctx, v->frame) >= 0) {
if (vd_write_frame(v, c) == 0) written = 1;
av.frame_unref(v->frame);
}
}
*(uint8_t **)((uint8_t *)v->packet + 24) = NULL;
*(int *)((uint8_t *)v->packet + 32) = 0;
vd_deliver(v, written, start, timestamp);
}
static int is_output(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || type == V4L2_BUF_TYPE_VIDEO_OUTPUT; }
static int is_capture(uint32_t type) { return type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || type == V4L2_BUF_TYPE_VIDEO_CAPTURE; }
static void vd_fill_format(struct vdec *v, struct v4l2_format *f) {
struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp;
memset(p, 0, sizeof *p);
p->field = V4L2_FIELD_NONE;
p->num_planes = 1;
p->colorspace = V4L2_COLORSPACE_REC709;
if (is_output(f->type)) {
p->width = v->width;
p->height = v->height;
p->pixelformat = v->fourcc;
p->plane_fmt[0].sizeimage = VDEC_BITSTREAM_SIZE;
} else {
p->width = v->cap_width;
p->height = v->cap_height;
p->pixelformat = V4L2_PIX_FMT_NV12;
p->plane_fmt[0].bytesperline = v->bpl;
p->plane_fmt[0].sizeimage = v->cap_size;
}
}
static int vd_set_format(struct vdec *v, struct v4l2_format *f, int apply) {
struct v4l2_pix_format_mplane *p = &f->fmt.pix_mp;
if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL;
struct vdec t = *v;
if (is_output(f->type)) {
if (p->pixelformat != V4L2_PIX_FMT_H264 && p->pixelformat != V4L2_PIX_FMT_HEVC) return -EINVAL;
t.fourcc = p->pixelformat;
t.width = p->width;
t.height = p->height;
} else {
uint32_t w = p->width > t.width ? p->width : t.width, h = p->height > t.height ? p->height : t.height;
if (w < 16 || h < 16 || w > 8192 || h > 8192) return -EINVAL;
t.cap_width = (w + 15) & ~15u;
t.cap_height = (h + 15) & ~15u;
t.bpl = t.cap_width;
t.cap_size = t.bpl * t.cap_height * 3 / 2;
}
if (apply) {
if (t.fourcc != v->fourcc || (t.hw && (t.bpl != v->bpl || t.cap_height != v->cap_height))) vd_close_decoder(&t);
*v = t;
}
vd_fill_format(&t, f);
return 0;
}
static int vd_reqbufs(struct vdec *v, struct v4l2_requestbuffers *r) {
uint32_t n = r->count > VDEC_BUFFERS ? VDEC_BUFFERS : r->count;
if (r->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && r->memory == V4L2_MEMORY_MMAP) {
if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE);
v->bitstream = NULL;
v->out_count = v->n_out_done = 0;
if (n && ftruncate(v->fd, (off_t)n * VDEC_BITSTREAM_SIZE) != 0) return -ENOMEM;
if (n) {
void *p = mmap(NULL, (size_t)n * VDEC_BITSTREAM_SIZE, PROT_READ, MAP_SHARED, v->fd, 0);
if (p == MAP_FAILED) return -ENOMEM;
v->bitstream = p;
}
v->out_count = n;
} else if (r->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && r->memory == V4L2_MEMORY_DMABUF) {
if (v->hw) vd_close_decoder(v);
vd_unmap_capture(v);
v->cap_count = n;
} else {
return -EINVAL;
}
r->count = n;
r->capabilities = r->memory == V4L2_MEMORY_MMAP ? V4L2_BUF_CAP_SUPPORTS_MMAP : V4L2_BUF_CAP_SUPPORTS_DMABUF;
return 0;
}
static int vd_querybuf(struct vdec *v, struct v4l2_buffer *b) {
if (b->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || b->index >= v->out_count || b->m.planes == NULL || b->length < 1)
return -EINVAL;
b->memory = V4L2_MEMORY_MMAP;
b->flags = V4L2_BUF_FLAG_MAPPED;
b->length = 1;
b->m.planes[0].length = VDEC_BITSTREAM_SIZE;
b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE;
return 0;
}
static int vd_qbuf(struct vdec *v, struct v4l2_buffer *b) {
if (b->m.planes == NULL || b->length < 1) return -EINVAL;
if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
if (b->index >= v->out_count || v->n_out_done >= VDEC_BUFFERS) return -EINVAL;
uint32_t bytes = b->m.planes[0].bytesused;
if (bytes > VDEC_BITSTREAM_SIZE) bytes = VDEC_BITSTREAM_SIZE;
if (v->out_on && bytes) vd_decode(v, b->index, bytes, b->timestamp);
v->out_done[v->n_out_done++] = b->index;
} else if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
if (b->index >= v->cap_count || v->cap_size == 0) return -EINVAL;
int r = vd_map_capture(v, b->index, b->m.planes[0].m.fd);
if (r < 0) return r;
for (uint32_t i = 0; i < v->n_cap_queue; i++) if (v->cap_queue[i] == b->index) return 0;
if (v->n_cap_queue >= VDEC_BUFFERS) return -EINVAL;
v->cap_queue[v->n_cap_queue++] = b->index;
} else {
return -EINVAL;
}
b->flags |= V4L2_BUF_FLAG_QUEUED;
return 0;
}
static int vd_dqbuf(struct vdec *v, struct v4l2_buffer *b) {
if (b->m.planes == NULL || b->length < 1) return -EINVAL;
if (b->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
if (v->n_out_done == 0) return -EAGAIN;
b->index = v->out_done[0];
memmove(v->out_done, v->out_done + 1, --v->n_out_done * sizeof v->out_done[0]);
b->memory = V4L2_MEMORY_MMAP;
b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_MAPPED;
b->length = 1;
b->m.planes[0].bytesused = 0;
b->m.planes[0].length = VDEC_BITSTREAM_SIZE;
b->m.planes[0].m.mem_offset = b->index * VDEC_BITSTREAM_SIZE;
return 0;
}
if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return -EINVAL;
if (v->n_cap_done == 0) return -EAGAIN;
struct vd_done d = v->cap_done[0];
memmove(v->cap_done, v->cap_done + 1, --v->n_cap_done * sizeof d);
b->index = d.index;
b->memory = V4L2_MEMORY_DMABUF;
b->flags = V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_TIMESTAMP_COPY;
b->field = V4L2_FIELD_NONE;
b->timestamp = d.timestamp;
b->length = 1;
b->m.planes[0].bytesused = v->cap_size;
b->m.planes[0].length = v->cap_size;
b->m.planes[0].m.fd = v->cap[d.index].fd;
return 0;
}
static int vd_ioctl(struct vdec *v, unsigned long req, void *arg) {
switch (req) {
case VIDIOC_QUERYCAP: {
struct v4l2_capability *c = arg;
memset(c, 0, sizeof *c);
strcpy((char *)c->driver, "droiddeck_vdec");
strcpy((char *)c->card, "DroidDeck video decoder");
strcpy((char *)c->bus_info, "platform:droiddeck-vdec");
c->version = 0x00060000;
c->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
c->capabilities = c->device_caps | V4L2_CAP_DEVICE_CAPS;
return 0;
}
case VIDIOC_ENUM_FMT: {
struct v4l2_fmtdesc *d = arg;
static const uint32_t coded[] = { V4L2_PIX_FMT_H264, V4L2_PIX_FMT_HEVC };
if (is_capture(d->type) && d->index == 0) {
d->pixelformat = V4L2_PIX_FMT_NV12;
strcpy((char *)d->description, "Y/CbCr 4:2:0");
d->flags = 0;
} else if (is_output(d->type) && d->index < 2) {
d->pixelformat = coded[d->index];
strcpy((char *)d->description, d->index ? "HEVC" : "H.264");
d->flags = V4L2_FMT_FLAG_COMPRESSED;
} else {
return -EINVAL;
}
return 0;
}
case VIDIOC_G_FMT: {
struct v4l2_format *f = arg;
if (!is_output(f->type) && !is_capture(f->type)) return -EINVAL;
vd_fill_format(v, f);
return 0;
}
case VIDIOC_S_FMT: return vd_set_format(v, arg, 1);
case VIDIOC_TRY_FMT: return vd_set_format(v, arg, 0);
case VIDIOC_REQBUFS: return vd_reqbufs(v, arg);
case VIDIOC_QUERYBUF: return vd_querybuf(v, arg);
case VIDIOC_QBUF: return vd_qbuf(v, arg);
case VIDIOC_DQBUF: return vd_dqbuf(v, arg);
case VIDIOC_STREAMON:
case VIDIOC_STREAMOFF: {
uint32_t type = *(uint32_t *)arg;
int on = req == VIDIOC_STREAMON;
if (is_output(type)) {
v->out_on = on;
if (!on) {
v->n_out_done = 0;
if (v->ctx) av.flush_buffers(v->ctx);
}
} else if (is_capture(type)) {
v->cap_on = on;
if (!on) v->n_cap_queue = v->n_cap_done = 0;
} else {
return -EINVAL;
}
if (v->out_on && v->cap_on && v->ctx == NULL && !v->hw && v->out_count && v->cap_size) vd_open_decoder(v);
return 0;
}
case VIDIOC_SUBSCRIBE_EVENT:
case VIDIOC_UNSUBSCRIBE_EVENT:
case VIDIOC_DECODER_CMD:
case VIDIOC_TRY_DECODER_CMD:
return 0;
case VIDIOC_DQEVENT:
return -ENOENT;
default:
vd_log("unhandled ioctl 0x%lx", req);
return -ENOTTY;
}
}
__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) {
struct vdec *v = vd_find(fd);
if (v == NULL) return 0;
pthread_mutex_lock(&vd_lock);
int r = arg ? vd_ioctl(v, req, arg) : -EFAULT;
pthread_mutex_unlock(&vd_lock);
if (r < 0) {
errno = -r;
*rc = -1;
} else {
*rc = 0;
}
return 1;
}
__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) {
struct vdec *v = vd_find(fd);
if (v == NULL) return;
pthread_mutex_lock(&vd_lock);
vd_close_decoder(v);
vd_unmap_capture(v);
if (v->bitstream) munmap(v->bitstream, (size_t)v->out_count * VDEC_BITSTREAM_SIZE);
v->bitstream = NULL;
v->used = 0;
__atomic_sub_fetch(&vd_open_count, 1, __ATOMIC_RELEASE);
pthread_mutex_unlock(&vd_lock);
}
__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
if (n != 1 || vd_find(fds[0].fd) == NULL) return 0;
struct vdec *v = vd_find(fds[0].fd);
pthread_mutex_lock(&vd_lock);
short ready = (v->n_cap_done ? POLLIN | POLLRDNORM : 0) | (v->n_out_done ? POLLOUT | POLLWRNORM : 0);
pthread_mutex_unlock(&vd_lock);
fds[0].revents = ready & fds[0].events;
if (fds[0].revents == 0 && timeout_ms != 0) real(NULL, 0, timeout_ms < 0 || timeout_ms > 1000 ? 1000 : timeout_ms);
*ret = fds[0].revents ? 1 : 0;
return 1;
}
static int drm_get_devices2(uint32_t flags, struct vd_device **devices, int max) {
(void)flags;
static char *nodes[3] = { "/dev/null", NULL, NULL };
static struct vd_device device = { .nodes = nodes, .available_nodes = 1 };
if (devices == NULL || max < 1) return 1;
devices[0] = &device;
return 1;
}
static void drm_free_devices(struct vd_device **devices, int count) { (void)devices; (void)count; }
static struct vd_mode_info drm_mode = {
.clock = 148500, .hdisplay = 1920, .hsync_start = 2008, .hsync_end = 2052, .htotal = 2200,
.vdisplay = 1080, .vsync_start = 1084, .vsync_end = 1089, .vtotal = 1125, .vrefresh = 60, .name = "1920x1080",
};
static uint32_t drm_crtc_ids[1] = { 1 }, drm_connector_ids[1] = { 2 }, drm_encoder_ids[1] = { 3 };
static struct vd_res *drm_get_resources(int fd) {
(void)fd;
static struct vd_res res = {
.count_crtcs = 1, .crtcs = drm_crtc_ids, .count_connectors = 1, .connectors = drm_connector_ids,
.count_encoders = 1, .encoders = drm_encoder_ids, .max_width = 8192, .max_height = 8192,
};
return &res;
}
static struct vd_connector *drm_get_connector(int fd, uint32_t id) {
(void)fd;
static struct vd_connector connector = {
.connector_id = 2, .encoder_id = 3, .connector_type = 14, .connection = 1, .count_modes = 1,
.modes = &drm_mode, .count_encoders = 1, .encoders = drm_encoder_ids,
};
return id == 2 ? &connector : NULL;
}
static struct vd_encoder *drm_get_encoder(int fd, uint32_t id) {
(void)fd;
static struct vd_encoder encoder = { .encoder_id = 3, .encoder_type = 8, .crtc_id = 1, .possible_crtcs = 1 };
return id == 3 ? &encoder : NULL;
}
static struct vd_crtc *drm_get_crtc(int fd, uint32_t id) {
(void)fd;
static struct vd_crtc crtc;
if (id != 1) return NULL;
crtc = (struct vd_crtc){ .crtc_id = 1, .width = 1920, .height = 1080, .mode_valid = 1, .mode = drm_mode };
return &crtc;
}
static void drm_free(void *p) { (void)p; }
static void *drm_null(void) { return NULL; }
static int drm_fail(void) { return -EINVAL; }
static int drm_dumb_slot(uint32_t handle) { return handle >= 1 && handle <= VDEC_DUMB ? (int)handle - 1 : -1; }
static int drm_ioctl(int fd, unsigned long request, void *arg) {
(void)fd;
int r = -EINVAL;
pthread_mutex_lock(&vd_lock);
if (request == VD_IOCTL_MODE_CREATE_DUMB && arg) {
struct vd_create_dumb *c = arg;
uint64_t pitch = ((uint64_t)c->width * c->bpp + 7) / 8;
uint64_t size = (pitch * c->height + 4095) & ~4095ull;
int slot = -1;
for (int i = 0; i < VDEC_DUMB && slot < 0; i++) if (vd_dumb_fd[i] < 0) slot = i;
struct vd_dma_heap_alloc a = { .len = size, .fd_flags = O_RDWR | O_CLOEXEC };
if (slot < 0 || size == 0 || size > (512ull << 20)) {
r = -ENOMEM;
} else if (ioctl(vd_heap, VD_DMA_HEAP_IOCTL_ALLOC, &a) != 0) {
r = -errno;
vd_log("dma-heap allocation of %llu bytes failed: %s", (unsigned long long)size, strerror(errno));
} else {
vd_dumb_fd[slot] = (int)a.fd;
c->handle = (uint32_t)slot + 1;
c->pitch = (uint32_t)pitch;
c->size = size;
r = 0;
}
} else if (request == VD_IOCTL_PRIME_HANDLE_TO_FD && arg) {
struct vd_prime_handle *h = arg;
int slot = drm_dumb_slot(h->handle);
int dup_fd = slot >= 0 && vd_dumb_fd[slot] >= 0 ? fcntl(vd_dumb_fd[slot], F_DUPFD_CLOEXEC, 0) : -1;
if (dup_fd >= 0) {
h->fd = dup_fd;
r = 0;
} else {
r = slot >= 0 ? -errno : -ENOENT;
}
} else if (request == VD_IOCTL_MODE_DESTROY_DUMB && arg) {
int slot = drm_dumb_slot(((struct vd_destroy_dumb *)arg)->handle);
if (slot >= 0 && vd_dumb_fd[slot] >= 0) {
close(vd_dumb_fd[slot]);
vd_dumb_fd[slot] = -1;
r = 0;
} else {
r = -ENOENT;
}
}
pthread_mutex_unlock(&vd_lock);
if (r < 0) {
errno = -r;
return -1;
}
return 0;
}
static const struct {
const char *name;
void *fn;
} drm_table[] = {
{ "drmGetDevices2", (void *)drm_get_devices2 },
{ "drmFreeDevices", (void *)drm_free_devices },
{ "drmSetMaster", (void *)drm_fail },
{ "drmSetClientCap", (void *)drm_fail },
{ "drmModeGetResources", (void *)drm_get_resources },
{ "drmModeFreeResources", (void *)drm_free },
{ "drmModeGetConnector", (void *)drm_get_connector },
{ "drmModeFreeConnector", (void *)drm_free },
{ "drmModeGetEncoder", (void *)drm_get_encoder },
{ "drmModeFreeEncoder", (void *)drm_free },
{ "drmModeGetCrtc", (void *)drm_get_crtc },
{ "drmModeSetCrtc", (void *)drm_fail },
{ "drmModeFreeCrtc", (void *)drm_free },
{ "drmModeGetPlaneResources", (void *)drm_null },
{ "drmModeFreePlaneResources", (void *)drm_free },
{ "drmModeGetPlane", (void *)drm_null },
{ "drmModeFreePlane", (void *)drm_free },
{ "drmModeObjectGetProperties", (void *)drm_null },
{ "drmModeFreeObjectProperties", (void *)drm_free },
{ "drmModeGetProperty", (void *)drm_null },
{ "drmModeFreeProperty", (void *)drm_free },
{ "drmModeAddFB2", (void *)drm_fail },
{ "drmModeAddFB2WithModifiers", (void *)drm_fail },
{ "drmModeRmFB", (void *)drm_fail },
{ "drmModeAtomicAlloc", (void *)drm_null },
{ "drmModeAtomicFree", (void *)drm_free },
{ "drmModeAtomicAddProperty", (void *)drm_fail },
{ "drmModeAtomicCommit", (void *)drm_fail },
{ "drmIoctl", (void *)drm_ioctl },
};
void *SDL_LoadObject(const char *name) {
static void *(*real)(const char *);
if (name && strcmp(name, "libdrm.so.2") == 0 && vd_enabled()) return &vd_drm_handle;
if (!real) real = (void *(*)(const char *))dlsym(RTLD_NEXT, "SDL_LoadObject");
return real ? real(name) : NULL;
}
void *SDL_LoadFunction(void *handle, const char *name) {
static void *(*real)(void *, const char *);
if (handle == &vd_drm_handle) {
for (size_t i = 0; name && i < sizeof drm_table / sizeof drm_table[0]; i++)
if (strcmp(drm_table[i].name, name) == 0) return drm_table[i].fn;
return NULL;
}
if (!real) real = (void *(*)(void *, const char *))dlsym(RTLD_NEXT, "SDL_LoadFunction");
return real ? real(handle, name) : NULL;
}
void SDL_UnloadObject(void *handle) {
static void (*real)(void *);
if (handle == &vd_drm_handle) return;
if (!real) real = (void (*)(void *))dlsym(RTLD_NEXT, "SDL_UnloadObject");
if (real) real(handle);
}
#else
__attribute__((visibility("hidden"))) int bl_vdec_open(const char *path, int flags) {
(void)path;
(void)flags;
return -2;
}
__attribute__((visibility("hidden"))) int bl_vdec_ioctl(int fd, unsigned long req, void *arg, int *rc) {
(void)fd;
(void)req;
(void)arg;
(void)rc;
return 0;
}
__attribute__((visibility("hidden"))) void bl_vdec_closed(int fd) { (void)fd; }
__attribute__((visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
(void)fds;
(void)n;
(void)timeout_ms;
(void)real;
(void)ret;
return 0;
}
#endif
+5 -2
View File
@@ -1,4 +1,3 @@
/* /dev/video-enc0 for Steam's Remote Play V4L2 encoder stage, backed by the app's MediaCodec encoder. */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
@@ -587,7 +586,6 @@ __attribute__((visibility("hidden"))) void bl_venc_closed(int fd) {
vn_free_queue(&v->raw);
vn_free_queue(&v->coded);
v->used = 0;
/* A forked child closing its inherited copy must not end the stream for the parent. */
if (__atomic_sub_fetch(&vn_open_count, 1, __ATOMIC_RELEASE) == 0 && v->owner == getpid()) vn_marker(0);
pthread_mutex_unlock(&vn_lock);
}
@@ -625,9 +623,14 @@ static int vn_poll(struct pollfd *fds, nfds_t n, int timeout_ms, int (*real)(str
return r + ready;
}
__attribute__((weak, visibility("hidden"))) int bl_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
int (*real)(struct pollfd *, nfds_t, int), int *ret);
int poll(struct pollfd *fds, nfds_t n, int timeout_ms) {
static int (*real)(struct pollfd *, nfds_t, int);
if (!real) real = (int (*)(struct pollfd *, nfds_t, int))dlsym(RTLD_NEXT, "poll");
int ret;
if (bl_vdec_poll && bl_vdec_poll(fds, n, timeout_ms, real, &ret)) return ret;
if (__atomic_load_n(&vn_open_count, __ATOMIC_ACQUIRE) == 0) return real(fds, n, timeout_ms);
return vn_poll(fds, n, timeout_ms, real);
}
+24 -1
View File
@@ -1,6 +1,7 @@
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef WEBHELPER_DEFAULT
#define WEBHELPER_DEFAULT "--no-zygote --in-process-gpu -AudioServiceOutOfProcess"
@@ -25,8 +26,30 @@ static char *join_features(const char *have, const char *add) {
return strcat(out, add);
}
static char *const *chromium_argv(const char *path, char *const argv[]) {
const char *slash = path ? strrchr(path, '/') : NULL;
if (!slash || !argv || !argv[0]) return argv;
int argc = 0;
for (; argv[argc]; argc++)
if (argc && (!strncmp(argv[argc], "--type=", 7) || !strcmp(argv[argc], "--no-sandbox"))) return argv;
size_t dir = (size_t)(slash - path) + 1;
char *sandbox = malloc(dir + sizeof "chrome-sandbox");
if (!sandbox) return argv;
memcpy(sandbox, path, dir);
strcpy(sandbox + dir, "chrome-sandbox");
int chromium = access(sandbox, F_OK) == 0;
free(sandbox);
if (!chromium) return argv;
char **out = calloc((size_t)argc + 2, sizeof *out);
if (!out) return argv;
out[0] = argv[0];
out[1] = (char *)"--no-sandbox";
for (int i = 1; i < argc; i++) out[i + 1] = argv[i];
return out;
}
__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]) {
if (!is_webhelper_browser(path, argv)) return argv;
if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv);
const char *spec = getenv("BL_WEBHELPER_ARGS");
if (!spec || !*spec) spec = WEBHELPER_DEFAULT;
if (!strcmp(spec, "stock")) return argv;
-2
View File
@@ -126,8 +126,6 @@ static void zr_walk(int compress) {
zr_sys(SYS_close, maps);
}
// Before it stops a session for suspend, the app waits for <ZR_CTL>.ack.<pid> to appear (the handler
// runs here, not one a program installed over it) and then to hold a byte (the page-out finished).
static void zr_ack(int done) {
static const char suffix[] = ".ack.";
char path[sizeof(ZR_CTL) + sizeof(suffix) + 20];