fix(bionic): address review findings

Launcher (tools/bionic-runtime/tool/launch.sh)
- The FEX code cache is also cleared when another FEX build wrote to it since this tool last ran the prefix:
  the stamp now holds the FEX ID and a fingerprint of the caches, saved after each run (also after
  runinprefix and when Steam stops the game).
- A game started by a launcher that then exits keeps the tool waiting: the wait goes on while any program
  of the prefix runs, apart from Wine's own services. The game is looked up in this prefix only, by its
  name as fixed text.
- The tool exits with Wine's status instead of 0.

Install scripts (droiddeck-game-env)
- A "run process" entry counts as done only when its hasrunkey value is a DWORD of at least its
  MinimumHasRunValue (1 by default), as Steam's evaluator decides; a 0 or a value of another type no longer
  lets the evaluator be skipped.

Helpers
- execshim: non-regular files (a FIFO) go to libc without a blocking open; libc functions are bound at load
  through atomic slots; relative posix_spawn paths with file actions are left to libc; the argument vector
  has room for its terminator when argv is empty.
- gstag: replies and errors are matched to their request's sequence number per connection; one bounded
  packet reader; destroyed windows leave the candidate and tagged lists; the focus override needs a live
  tagged window and a successful focus read; only gone candidates are pruned.
- steamshm: a new shared object is created and sized under a private name and then put in place in one
  step; both code redirects go in or neither, and unused trampolines are released.
- check-needed.py fails when readelf fails and ignores dangling links.
- The bionic runtime workflow also runs for tools/gpuload changes.

App and tool wrapper
- FEX twin downloads run once per name; a twin that a game's FEX choice still needs on an installed tool
  cannot be deleted.
- With proot on and no DroidDeck-Proton installed, a launch sent to DroidDeck-Bionic stops with a message
  instead of starting the bionic tool.
This commit is contained in:
The412Banner committed 2026-11-22 17:56:19 -05:00
1 parent dca443e3aa
commit 2aae4b2284
11 files changed
+435 -122

No files matched your search

+7 -4
View File
@@ -37,15 +37,18 @@ def elf_files(pack):
def needed(readelf, path):
out = subprocess.run([readelf, "-d", path], capture_output=True, text=True).stdout
return [l.split("[", 1)[1].split("]", 1)[0] for l in out.splitlines() if "(NEEDED)" in l]
run = subprocess.run([readelf, "-d", path], capture_output=True, text=True)
if run.returncode != 0: # a damaged ELF must fail the check, not pass with no dependencies
raise SystemExit("check-needed: %s -d %s failed (%d): %s" % (readelf, path, run.returncode, run.stderr.strip()))
return [l.split("[", 1)[1].split("]", 1)[0] for l in run.stdout.splitlines() if "(NEEDED)" in l]
def names_in(*dirs):
names = set()
for d in dirs:
for _, _, files in os.walk(d):
names.update(files)
for root, _, files in os.walk(d):
# a link whose target is missing provides nothing
names.update(f for f in files if os.path.isfile(os.path.join(root, f)))
return names
+66 -26
View File
@@ -20,6 +20,7 @@ typedef struct __posix_spawn_file_actions *posix_spawn_file_actions_t;
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define LINKER "/system/bin/linker64"
@@ -29,8 +30,14 @@ enum kind { PASS, VIA_LINKER, VIA_INTERP };
// What starting `path` needs; for scripts, the interpreter and its optional argument.
static enum kind classify(const char *path, char *resolved, char *interp, char *iarg) {
if (!path || !realpath(path, resolved) || strncmp(resolved, "/data/", 6) != 0) return PASS;
int fd = open(resolved, O_RDONLY | O_CLOEXEC);
// non-blocking, so a FIFO without a writer cannot hang the start; anything but a regular file goes to libc
int fd = open(resolved, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
if (fd < 0) return PASS;
struct stat st;
if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) {
close(fd);
return PASS;
}
enum kind k = PASS;
unsigned char head[256];
ssize_t n = pread(fd, head, sizeof head, 0);
@@ -81,7 +88,8 @@ static char **rewrite(const char *path, char *const argv[], const char **file, c
enum kind k = classify(path, resolved, interp, iarg);
if (k == PASS) return NULL;
int argc = argc_of(argv);
char **out = calloc((size_t)argc + 4, sizeof *out);
// up to four leading entries, argc - 1 trailing ones and the terminator (argc may be 0)
char **out = calloc((size_t)(argc > 0 ? argc - 1 : 0) + 5, sizeof *out);
if (!out) return NULL;
int j = 0;
if (k == VIA_LINKER) {
@@ -107,11 +115,41 @@ static char **rewrite(const char *path, char *const argv[], const char **file, c
return out;
}
static ssize_t (*real_readlink)(const char *, char *, size_t);
// libc's functions: bound when the library loads (single-threaded), so execve() in a post-fork child finds them bound.
// A call that comes before that (another library's constructor) binds through an atomic slot: concurrent binders
// store the same pointer, and readers never see a torn one.
typedef ssize_t (*readlink_fn)(const char *, char *, size_t);
typedef char *(*realpath_fn)(const char *, char *);
typedef int (*execve_fn)(const char *, char *const[], char *const[]);
typedef int (*spawn_fn)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
static void *slot_readlink, *slot_realpath, *slot_execve, *slot_spawn, *slot_spawnp;
static void *bound(void **slot, const char *name) {
void *p = __atomic_load_n(slot, __ATOMIC_ACQUIRE);
if (!p) {
p = dlsym(RTLD_NEXT, name);
__atomic_store_n(slot, p, __ATOMIC_RELEASE);
}
return p;
}
#define real_readlink ((readlink_fn)bound(&slot_readlink, "readlink"))
#define real_realpath ((realpath_fn)bound(&slot_realpath, "realpath"))
#define real_execve ((execve_fn)bound(&slot_execve, "execve"))
#define real_spawn ((spawn_fn)bound(&slot_spawn, "posix_spawn"))
#define real_spawnp ((spawn_fn)bound(&slot_spawnp, "posix_spawnp"))
__attribute__((constructor)) static void bind_libc(void) {
(void)real_readlink;
(void)real_realpath;
(void)real_execve;
(void)real_spawn;
(void)real_spawnp;
}
// The program linker64 runs, when this process is such a start; 0 otherwise.
static int linker_program(char *out, size_t n) {
if (!real_readlink) real_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
if (!real_readlink) return 0;
char link[PATH_MAX];
ssize_t l = real_readlink("/proc/self/exe", link, sizeof link - 1);
if (l <= 0) return 0;
@@ -132,32 +170,38 @@ static int linker_program(char *out, size_t n) {
}
char *realpath(const char *path, char *resolved) {
static char *(*real)(const char *, char *);
if (!real) real = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath");
if (!real_realpath) {
errno = ENOSYS;
return NULL;
}
char prog[PATH_MAX];
if (path && strcmp(path, "/proc/self/exe") == 0 && linker_program(prog, sizeof prog)) return real(prog, resolved);
return real(path, resolved);
if (path && strcmp(path, "/proc/self/exe") == 0 && linker_program(prog, sizeof prog)) return real_realpath(prog, resolved);
return real_realpath(path, resolved);
}
ssize_t readlink(const char *path, char *buf, size_t size) {
char prog[PATH_MAX], full[PATH_MAX];
if (path && strcmp(path, "/proc/self/exe") == 0 && linker_program(prog, sizeof prog)) {
static char *(*real_rp)(const char *, char *);
if (!real_rp) real_rp = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath");
if (real_rp(prog, full)) {
if (real_realpath && real_realpath(prog, full)) {
size_t l = strlen(full);
if (l > size) l = size;
memcpy(buf, full, l);
return (ssize_t)l;
}
}
if (!real_readlink) real_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink");
if (!real_readlink) {
errno = ENOSYS;
return -1;
}
return real_readlink(path, buf, size);
}
int execve(const char *path, char *const argv[], char *const envp[]) {
static int (*real)(const char *, char *const[], char *const[]);
if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execve");
execve_fn real = real_execve;
if (!real) {
errno = ENOSYS;
return -1;
}
char storage[6 * PATH_MAX]; // stack only: this may run in posix_spawn's vfork-like child (where it passes)
const char *file = path;
char **args = rewrite(path, argv, &file, storage);
@@ -170,14 +214,13 @@ int execve(const char *path, char *const argv[], char *const envp[]) {
int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *actions,
const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) {
static int (*real)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
if (!real)
real = (int (*)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]))dlsym(RTLD_NEXT, "posix_spawn");
spawn_fn real = real_spawn;
if (!real) return ENOSYS;
char storage[6 * PATH_MAX];
const char *file = path;
char **args = rewrite(path, argv, &file, storage);
// file actions can change the child's directory before it starts: a relative path is resolved there by libc, so
// it is left to libc instead of being resolved here against the parent's directory
char **args = (actions && path && path[0] != '/') ? NULL : rewrite(path, argv, &file, storage);
int ret = real(pid, file, actions, attr, args ? args : argv, envp);
free(args);
return ret;
@@ -185,11 +228,8 @@ int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *
int posix_spawnp(pid_t *pid, const char *file, const posix_spawn_file_actions_t *actions,
const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) {
static int (*real)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]);
if (strchr(file, '/')) return posix_spawn(pid, file, actions, attr, argv, envp);
if (!real)
real = (int (*)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *,
char *const[], char *const[]))dlsym(RTLD_NEXT, "posix_spawnp");
if (file && strchr(file, '/')) return posix_spawn(pid, file, actions, attr, argv, envp);
spawn_fn real = real_spawnp;
if (!real) return ENOSYS;
return real(pid, file, actions, attr, argv, envp);
}
+112 -40
View File
@@ -20,7 +20,8 @@
#include <time.h>
#include <unistd.h>
static int fd;
static int fd, tconn = -1, cconn = -1;
static uint16_t tseq, cseq; // sequence number of the last request sent on each connection
static uint8_t rbuf[1 << 16];
static uint32_t pending[256]; static int npending;
static uint32_t tagged[256]; static int ntagged;
@@ -36,27 +37,54 @@ static int rd(void *p, size_t n) {
return 0;
}
static size_t pad4(size_t n) { return (n + 3) & ~(size_t)3; }
// Sends one request on the current connection; its sequence number goes to *seq. 0 when sent.
static int request(const void *p, size_t n, uint16_t *seq) {
if (wr(p, n)) return -1;
uint16_t *last = (fd == cconn && cconn != tconn) ? &cseq : &tseq;
++*last;
if (seq) *seq = *last;
return 0;
}
// Reads one whole packet into rbuf; a reply (or generic event) longer than rbuf ends the connection. 0 when read.
static int packet(void) {
if (rd(rbuf, 32)) return -1;
uint8_t type = rbuf[0] & 0x7f;
if (rbuf[0] == 1 || type == 35) {
uint32_t words = *(uint32_t *)(rbuf + 4);
if (words > (sizeof rbuf - 32) / 4) return -1;
if (words && rd(rbuf + 32, words * 4u)) return -1;
}
return 0;
}
static int known(uint32_t *list, int n, uint32_t w) { for (int i = 0; i < n; i++) if (list[i] == w) return 1; return 0; }
static void forget(uint32_t *list, int *n, uint32_t w) {
for (int i = 0; i < *n; i++)
if (list[i] == w) { list[i] = list[--*n]; return; }
}
static void note_event(const uint8_t *e) {
uint8_t type = e[0] & 0x7f;
uint32_t win = 0;
if (type == 16) win = *(uint32_t *)(e + 8); // CreateNotify
else if (type == 19) win = *(uint32_t *)(e + 8); // MapNotify
uint32_t win = *(uint32_t *)(e + 8);
if (type == 17) { // DestroyNotify: a destroyed window is neither a candidate nor a tagged game window
forget(pending, &npending, win);
forget(tagged, &ntagged, win);
return;
}
if (type != 16 && type != 19) return; // CreateNotify, MapNotify
if (win && !known(tagged, ntagged, win) && !known(pending, npending, win) && npending < 256) pending[npending++] = win;
}
// Reads one packet. Returns 1 reply, 0 error, -1 io; events are noted and skipped.
static int reply(void) {
// Reads until the answer to request `seq`. Returns 1 reply, 0 error, -1 io. Events are noted; errors of requests
// without a reply (a ChangeProperty on a window that is gone) and other stale packets are skipped.
static int reply(uint16_t seq) {
for (;;) {
if (rd(rbuf, 32)) return -1;
if (rbuf[0] == 0) return 0;
if (rbuf[0] == 1) {
uint32_t extra = *(uint32_t *)(rbuf + 4) * 4;
if (extra > sizeof rbuf - 32 || (extra && rd(rbuf + 32, extra))) return -1;
return 1;
}
note_event(rbuf);
if (packet()) return -1;
if (rbuf[0] > 1) { note_event(rbuf); continue; }
if (*(uint16_t *)(rbuf + 2) != seq) continue;
return rbuf[0] == 1;
}
}
@@ -67,8 +95,9 @@ static uint32_t intern(const char *name) {
*(uint16_t *)(req + 2) = (uint16_t)(2 + pad4(n) / 4);
*(uint16_t *)(req + 4) = (uint16_t)n;
memcpy(req + 8, name, n);
if (wr(req, 8 + pad4(n))) return 0;
return reply() == 1 ? *(uint32_t *)(rbuf + 8) : 0;
uint16_t seq;
if (request(req, 8 + pad4(n), &seq)) return 0;
return reply(seq) == 1 ? *(uint32_t *)(rbuf + 8) : 0;
}
// WM_CLASS of a window into out (both strings joined by '|'); 0 if none.
@@ -80,7 +109,8 @@ static int wm_class(uint32_t win, char *out, size_t cap) {
*(uint32_t *)(req + 8) = 67; // WM_CLASS
*(uint32_t *)(req + 12) = 31; // STRING
*(uint32_t *)(req + 20) = 64;
if (wr(req, sizeof req) || reply() != 1) return 0;
uint16_t seq;
if (request(req, sizeof req, &seq) || reply(seq) != 1) return 0;
uint32_t n = *(uint32_t *)(rbuf + 16);
if (!n || n >= cap) return 0;
for (uint32_t i = 0; i < n; i++) out[i] = rbuf[32 + i] ? (char)rbuf[32 + i] : '|';
@@ -99,17 +129,28 @@ static void set_cards(uint32_t win, uint32_t prop, const uint32_t *values, uint3
req[16] = 32;
*(uint32_t *)(req + 20) = n;
memcpy(req + 24, values, 4 * n);
wr(req, 24 + 4 * n);
request(req, 24 + 4 * n, NULL);
}
// First CARDINAL of a root property, or 0 when unset.
static uint32_t first_card(uint32_t root, uint32_t prop) {
// First CARDINAL of a root property into *value (0 when unset). 0 when the property could not be read.
static int first_card(uint32_t root, uint32_t prop, uint32_t *value) {
uint8_t gp[24] = {20, 0, 6, 0};
uint16_t seq;
*(uint32_t *)(gp + 4) = root;
*(uint32_t *)(gp + 8) = prop;
*(uint32_t *)(gp + 20) = 64;
if (wr(gp, sizeof gp) || reply() != 1) return 0;
return *(uint32_t *)(rbuf + 16) ? *(uint32_t *)(rbuf + 32) : 0;
if (request(gp, sizeof gp, &seq) || reply(seq) != 1) return 0;
*value = *(uint32_t *)(rbuf + 16) ? *(uint32_t *)(rbuf + 32) : 0;
return 1;
}
// 1 when the window still exists (GetWindowAttributes answers), 0 when it is gone, -1 on an io failure.
static int alive(uint32_t win) {
uint8_t ga[8] = {3, 0, 2, 0};
uint16_t seq;
*(uint32_t *)(ga + 4) = win;
if (request(ga, sizeof ga, &seq)) return -1;
return reply(seq);
}
static void set_card(uint32_t win, uint32_t prop, uint32_t value) {
@@ -122,7 +163,7 @@ static void set_card(uint32_t win, uint32_t prop, uint32_t value) {
req[16] = 32;
*(uint32_t *)(req + 20) = 1;
*(uint32_t *)(req + 24) = value;
wr(req, sizeof req);
request(req, sizeof req, NULL);
}
// Connects to display :<disp> (abstract socket) and returns the socket, its root window in *root; -1 on failure.
@@ -162,6 +203,8 @@ int main(int argc, char **argv) {
if (c >= 0) cfd = c;
else croot = root;
}
tconn = tfd;
cconn = cfd;
fd = tfd;
uint32_t a_game = intern("STEAM_GAME");
@@ -169,11 +212,12 @@ int main(int argc, char **argv) {
*(uint32_t *)(cwa + 4) = root;
*(uint32_t *)(cwa + 8) = 0x800;
*(uint32_t *)(cwa + 12) = 0x80000;
wr(cwa, sizeof cwa);
request(cwa, sizeof cwa, NULL);
uint8_t qt[8] = {15, 0, 2, 0}; // QueryTree(root): windows that exist already
uint16_t qseq;
*(uint32_t *)(qt + 4) = root;
if (wr(qt, sizeof qt) == 0 && reply() == 1) {
if (request(qt, sizeof qt, &qseq) == 0 && reply(qseq) == 1) {
uint16_t nk = *(uint16_t *)(rbuf + 16);
uint32_t kids[1024];
if (nk > 1024) nk = 1024;
@@ -187,7 +231,7 @@ int main(int argc, char **argv) {
uint32_t a_focus = intern("GAMESCOPE_FOCUSED_APP");
uint32_t a_ctrl = intern("GAMESCOPECTRL_BASELAYER_APPID");
fd = tfd;
time_t tagged_at = 0; int focus_checked = 0;
time_t tagged_at = 0; int focus_checked = 0, focus_reads = 0;
time_t end = time(NULL) + seconds, next_status = time(NULL) + 3;
while (time(NULL) < end) {
if (time(NULL) >= next_status) {
@@ -201,7 +245,8 @@ int main(int argc, char **argv) {
*(uint32_t *)(gp + 4) = croot;
*(uint32_t *)(gp + 8) = props[k];
*(uint32_t *)(gp + 20) = 64;
if (wr(gp, sizeof gp) == 0 && reply() == 1) {
uint16_t gseq;
if (request(gp, sizeof gp, &gseq) == 0 && reply(gseq) == 1) {
uint32_t cnt = *(uint32_t *)(rbuf + 16);
printf(" %s=[", names[k]);
for (uint32_t i = 0; i < cnt && i < 16; i++) printf("%s%u", i ? "," : "", ((uint32_t *)(rbuf + 32))[i]);
@@ -211,26 +256,42 @@ int main(int argc, char **argv) {
fd = tfd;
for (int i = 0; i < ntagged; i++) {
uint8_t ga[8] = {3, 0, 2, 0}; // GetWindowAttributes
uint16_t aseq, gseq;
*(uint32_t *)(ga + 4) = tagged[i];
int map = -1, ovr = -1, w = -1, h = -1;
if (wr(ga, sizeof ga) == 0 && reply() == 1) { map = rbuf[26]; ovr = rbuf[27]; }
if (request(ga, sizeof ga, &aseq) == 0 && reply(aseq) == 1) { map = rbuf[26]; ovr = rbuf[27]; }
uint8_t gg[8] = {14, 0, 2, 0}; // GetGeometry
*(uint32_t *)(gg + 4) = tagged[i];
if (wr(gg, sizeof gg) == 0 && reply() == 1) { w = *(uint16_t *)(rbuf + 16); h = *(uint16_t *)(rbuf + 18); }
if (request(gg, sizeof gg, &gseq) == 0 && reply(gseq) == 1) { w = *(uint16_t *)(rbuf + 16); h = *(uint16_t *)(rbuf + 18); }
printf(" win0x%x=%dx%d map%d ovr%d", tagged[i], w, h, map, ovr);
}
printf("\n");
}
if (tagged_at && !focus_checked && time(NULL) - tagged_at >= 8) {
focus_checked = 1;
// the override is only for a game that still has a window: a tagged window that was destroyed is gone
// from `tagged` (DestroyNotify), and one that is left must still answer
int live = 0;
for (int i = 0; i < ntagged && !live; i++) live = alive(tagged[i]) == 1;
uint32_t f = 0;
fd = cfd;
uint32_t f = first_card(croot, a_focus);
if (f != appid) {
uint32_t ids[2] = {appid, 769};
set_cards(croot, a_ctrl, ids, 2);
printf("gstag: focus was %u, not %u, 8 s after the tag; set GAMESCOPECTRL_BASELAYER_APPID=%u,769\n", f, appid, appid);
} else printf("gstag: %u has focus\n", appid);
int read_ok = live && first_card(croot, a_focus, &f);
fd = tfd;
if (!live) {
focus_checked = 1;
printf("gstag: no tagged window left 8 s after the tag; focus left as it is\n");
} else if (!read_ok) {
// a failed read says nothing about who has focus: try again on the next pass, a few times
if (++focus_reads >= 3) { focus_checked = 1; printf("gstag: focus could not be read; left as it is\n"); }
} else {
focus_checked = 1;
if (f != appid) {
uint32_t ids[2] = {appid, 769};
fd = cfd;
set_cards(croot, a_ctrl, ids, 2);
fd = tfd;
printf("gstag: focus was %u, not %u, 8 s after the tag; set GAMESCOPECTRL_BASELAYER_APPID=%u,769\n", f, appid, appid);
} else printf("gstag: %u has focus\n", appid);
}
}
if (watch && kill(watch, 0) != 0 && errno == ESRCH) { printf("gstag: pid %d gone, stopping\n", watch); break; }
for (int i = 0; i < npending; ) {
@@ -247,11 +308,22 @@ int main(int argc, char **argv) {
fd_set rs; FD_ZERO(&rs); FD_SET(fd, &rs);
struct timeval tv = {0, 500000};
if (select(fd + 1, &rs, NULL, NULL, &tv) > 0) {
if (rd(rbuf, 32)) { printf("gstag: connection closed\n"); break; }
if (rbuf[0] == 1) { uint32_t x = *(uint32_t *)(rbuf + 4) * 4; if (x && rd(rbuf + 32, x)) break; }
else if (rbuf[0] != 0) note_event(rbuf);
if (packet()) { printf("gstag: connection closed or a packet too long\n"); break; }
if (rbuf[0] > 1) note_event(rbuf); // replies and errors here belong to no request waiting
}
if (npending > 200) {
// drop only windows that are gone; live candidates may still get WM_CLASS
for (int i = 0; i < npending; ) {
int a = alive(pending[i]);
if (a < 0) break;
if (a == 0) pending[i] = pending[--npending];
else i++;
}
if (npending > 200) { // still too many: keep the newest
memmove(pending, pending + (npending - 200), 200 * sizeof *pending);
npending = 200;
}
}
if (npending > 200) npending = 0;
}
printf("gstag: done, %d window(s) tagged\n", ntagged);
return 0;
+124 -25
View File
@@ -22,6 +22,7 @@
#include <semaphore.h>
#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#define EXPORT __attribute__((visibility("default")))
@@ -98,18 +99,33 @@ static int hook_open(const char *name, size_t size, bool *created)
return fd;
}
if (errno != ENOENT) break;
fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd >= 0) {
if (ftruncate(fd, (off_t)size) == 0) {
if (created) *created = true;
if (notes++ < 64) say("steamshm: %s created (%zu bytes)\n", path, size);
return fd;
}
// made and sized under a private name, then put in place in one step: no other process can open it while it
// is still empty, and an object another process published first is opened instead
char tmp[PATH_MAX];
int w = snprintf(tmp, sizeof tmp, "%s.%d.%d", path, (int)getpid(), i);
if (w <= 0 || (size_t)w >= sizeof tmp) break;
fd = open(tmp, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd < 0) break;
if (ftruncate(fd, (off_t)size) != 0) {
close(fd);
unlink(path);
unlink(tmp);
break;
}
if (errno != EEXIST) break;
int placed = (int)syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 1 /* RENAME_NOREPLACE */);
if (placed != 0 && (errno == ENOSYS || errno == EINVAL)) { // no renameat2: a hard link fails on EEXIST too
placed = link(tmp, path);
unlink(tmp);
}
if (placed == 0) {
if (created) *created = true;
if (notes++ < 64) say("steamshm: %s created (%zu bytes)\n", path, size);
return fd;
}
int err = errno;
close(fd);
unlink(tmp);
errno = err;
if (err != EEXIST) break;
}
say("steamshm: %s failed (errno %d)\n", path, errno);
return -1;
@@ -286,30 +302,109 @@ static void *trampoline(uint32_t *fn)
if (t == MAP_FAILED) return NULL;
memcpy(t, fn, 16);
jump_code(t + 4, (uintptr_t)(fn + 4));
if (mprotect(t, page, PROT_READ | PROT_EXEC)) return NULL;
if (mprotect(t, page, PROT_READ | PROT_EXEC)) {
munmap(t, page);
return NULL;
}
flush((uintptr_t)t, 32);
return t;
}
// The library's code pages are file mappings: write an anonymous copy and move it over the original in one step.
static bool redirect(uint32_t *fn, uintptr_t to)
static void drop_trampoline(void *t)
{
size_t page = getauxval(AT_PAGESZ);
uintptr_t lo = (uintptr_t)fn & ~(page - 1), hi = ((uintptr_t)fn + 16 + page - 1) & ~(page - 1);
size_t len = hi - lo;
uint8_t *copy = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (copy == MAP_FAILED) return false;
memcpy(copy, (void *)lo, len);
jump_code((uint32_t *)(copy + ((uintptr_t)fn - lo)), to);
if (mprotect(copy, len, PROT_READ | PROT_EXEC) ||
mremap(copy, len, len, MREMAP_MAYMOVE | MREMAP_FIXED, (void *)lo) == MAP_FAILED) {
munmap(copy, len);
if (t) munmap(t, getauxval(AT_PAGESZ));
}
// The library's code pages are file mappings: patched anonymous copies are prepared first and then moved over the
// originals in one step each. Both hooks go in or neither: the pages of both functions (one range when they share a
// page) are copied, patched and made executable before anything is replaced, and a copy of the original pages is
// kept, so a failed second replacement puts the first range back.
struct range {
uintptr_t lo;
size_t len;
uint8_t *patched, *saved;
};
static bool prepare(struct range *r, uintptr_t lo, uintptr_t hi)
{
r->lo = lo;
r->len = hi - lo;
r->patched = mmap(NULL, r->len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
r->saved = mmap(NULL, r->len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (r->patched == MAP_FAILED || r->saved == MAP_FAILED) {
if (r->patched != MAP_FAILED) munmap(r->patched, r->len);
if (r->saved != MAP_FAILED) munmap(r->saved, r->len);
r->patched = r->saved = NULL;
return false;
}
flush(lo, len);
memcpy(r->patched, (void *)lo, r->len);
memcpy(r->saved, (void *)lo, r->len);
return true;
}
static void patch(struct range *r, uint32_t *fn, uintptr_t to)
{
if ((uintptr_t)fn >= r->lo && (uintptr_t)fn + 16 <= r->lo + r->len)
jump_code((uint32_t *)(r->patched + ((uintptr_t)fn - r->lo)), to);
}
static bool seal(struct range *r)
{
return !mprotect(r->patched, r->len, PROT_READ | PROT_EXEC) && !mprotect(r->saved, r->len, PROT_READ | PROT_EXEC);
}
static bool commit(struct range *r)
{
if (mremap(r->patched, r->len, r->len, MREMAP_MAYMOVE | MREMAP_FIXED, (void *)r->lo) == MAP_FAILED) return false;
r->patched = NULL; // now the mapping at lo
flush(r->lo, r->len);
return true;
}
static void restore(struct range *r)
{
if (r->saved && mremap(r->saved, r->len, r->len, MREMAP_MAYMOVE | MREMAP_FIXED, (void *)r->lo) != MAP_FAILED) {
r->saved = NULL;
flush(r->lo, r->len);
}
}
static void release(struct range *r)
{
if (r->patched) munmap(r->patched, r->len);
if (r->saved) munmap(r->saved, r->len);
r->patched = r->saved = NULL;
}
static bool redirect_both(uint32_t *fa, uintptr_t ta, uint32_t *fb, uintptr_t tb)
{
size_t page = getauxval(AT_PAGESZ);
uintptr_t alo = (uintptr_t)fa & ~(page - 1), ahi = ((uintptr_t)fa + 16 + page - 1) & ~(page - 1);
uintptr_t blo = (uintptr_t)fb & ~(page - 1), bhi = ((uintptr_t)fb + 16 + page - 1) & ~(page - 1);
struct range r[2] = {{0}};
int n = 1;
if (alo < bhi && blo < ahi) { // overlapping pages: one range holds both patches
if (!prepare(&r[0], alo < blo ? alo : blo, ahi > bhi ? ahi : bhi)) return false;
} else if (!prepare(&r[0], alo, ahi) || !prepare(&r[1], blo, bhi)) {
release(&r[0]);
release(&r[1]);
return false;
} else n = 2;
for (int i = 0; i < n; i++) {
patch(&r[i], fa, ta);
patch(&r[i], fb, tb);
}
bool ok = true;
for (int i = 0; i < n && ok; i++) ok = seal(&r[i]);
if (ok && !commit(&r[0])) ok = false;
else if (ok && n == 2 && !commit(&r[1])) {
restore(&r[0]);
ok = false;
}
for (int i = 0; i < n; i++) release(&r[i]);
return ok;
}
static void install(void)
{
const char *mode = getenv("DROIDDECK_BIONIC_STEAMSHM");
@@ -335,8 +430,12 @@ static void install(void)
}
orig_open = (shm_open_fn)trampoline(open_fn);
orig_unlink = (shm_unlink_fn)trampoline(unlink_fn);
if (!orig_open || !orig_unlink || !redirect(unlink_fn, (uintptr_t)hook_unlink) ||
!redirect(open_fn, (uintptr_t)hook_open)) {
if (!orig_open || !orig_unlink ||
!redirect_both(unlink_fn, (uintptr_t)hook_unlink, open_fn, (uintptr_t)hook_open)) {
drop_trampoline((void *)orig_open);
drop_trampoline((void *)orig_unlink);
orig_open = NULL;
orig_unlink = NULL;
say("steamshm: could not redirect the library's shared memory; left as it is\n");
return;
}
+33 -10
View File
@@ -253,7 +253,8 @@ programs_left() { # a program of this prefix is still running, apart from Wine's
[ "$q" = "$$" ] || [ "$q" = "${GT:-x}" ] && continue
c=; read -r c < "$p/comm" 2>/dev/null
case "$c" in
""|wineserver|services.exe|winedevice.exe|plugplay.exe|explorer.exe|svchost.exe|rpcss.exe|conhost.exe|start.exe) continue ;;
""|wineserver|services.exe|winedevice.exe|plugplay.exe|explorer.exe|svchost.exe|rpcss.exe|conhost.exe|start.exe|\
winemenubuilder|wineboot.exe|tabtip.exe) continue ;;
esac
tr '\0' '\n' < "$p/environ" 2>/dev/null | grep -qx "WINEPREFIX=$COMPAT/pfx" && return 0
done
@@ -311,15 +312,25 @@ prepare_prefix
# runs the prefix next. Code cached by another FEX build (DroidDeck-Proton's, or a different FEX picked in the
# Components tab) made this FEX crash in Steam's install-script step, and Wine then looped on the crash, so Steam never
# started the game. The cache is cleared when the FEX that runs the prefix is not the one that filled it.
# The stamp holds this FEX's ID and what the caches held when this tool last finished with the prefix, so a cache that
# another FEX wrote to since (DroidDeck-Proton running the same game) is cleared too.
fexid=$(cat "$F/lib/wine/aarch64-windows/libarm64ecfex.dll" "$F/lib/wine/aarch64-windows/libwow64fex.dll" \
"$F/lib/wine/aarch64-unix/libarm64ecfex.so" "$F/lib/wine/aarch64-unix/libwow64fex.so" 2>/dev/null | md5sum | cut -c1-16)
if [ "$(cat "$COMPAT/pfx/.droiddeck-fex-cache" 2>/dev/null)" != "$fexid" ]; then
fex_cache_sum() { # names, sizes and times of the files in the prefix's FEX code caches
for u in "$COMPAT/pfx/drive_c/users"/*; do
[ -L "$u" ] && continue
c=$u/AppData/Local/fex-emu/DiskCache
[ -d "$c" ] && find "$c" -type f -exec stat -c '%n %s %Y' {} + 2>/dev/null
done | sort | md5sum | cut -c1-16
}
save_fex_stamp() { echo "$fexid $(fex_cache_sum)" > "$COMPAT/pfx/.droiddeck-fex-cache" 2>/dev/null; }
if [ "$(cat "$COMPAT/pfx/.droiddeck-fex-cache" 2>/dev/null)" != "$fexid $(fex_cache_sum)" ]; then
for u in "$COMPAT/pfx/drive_c/users"/*; do
c=$u/AppData/Local/fex-emu/DiskCache
[ -L "$u" ] || [ ! -d "$c" ] && continue
rm -rf "$c" && echo "$(date +%H:%M:%S) FEX code cache of ${u##*/} cleared (filled by another FEX build or unknown)"
rm -rf "$c" && echo "$(date +%H:%M:%S) FEX code cache of ${u##*/} cleared (another FEX build wrote to it, or it is unknown)"
done
echo "$fexid" > "$COMPAT/pfx/.droiddeck-fex-cache"
save_fex_stamp
fi
# Steam's Windows client files in the prefix, the ones Proton copies from Steam's legacycompat folder: a game's
@@ -373,12 +384,19 @@ case "$EXE" in [A-Za-z]:[\\/]*) ;; *) cd "$(dirname "$EXE")" || exit 1 ;; esac
if [ "$verb" = runinprefix ]; then
wine_ "$EXE" "$@"
exit $?
rc=$?
save_fex_stamp
exit $rc
fi
name=${EXE##*/}; name=${name##*\\}
comm=$(echo "$name" | cut -c1-15)
game_pids() { grep -lx "$comm" /proc/[0-9]*/comm 2>/dev/null | cut -d/ -f3; }
game_pids() { # processes of this prefix with the game's name (two prefixes can run programs with the same name)
for p in $(grep -lFx "$comm" /proc/[0-9]*/comm 2>/dev/null); do
p=${p%/comm}
tr '\0' '\n' < "$p/environ" 2>/dev/null | grep -qxF "WINEPREFIX=$COMPAT/pfx" && echo "${p#/proc/}"
done
}
# The game's windows go straight to gamescope's X server, as with DroidDeck-Proton (no Wine desktop): the game
# sees gamescope's real modes and refresh rate, and gamescope manages its windows.
wine_ "$EXE" "$@" &
@@ -392,7 +410,7 @@ if [ "${SteamAppId:-0}" != 0 ]; then
"$LINKER" "$PACK/bin/gstag" "${XD:-0}" "$SteamAppId" .exe 86400 > "$COMPAT/gstag.log" 2>&1 &
GT=$!
fi
trap 'echo "$(date +%H:%M:%S) stopped by Steam"; [ -n "$GT" ] && kill $GT 2>/dev/null; stop_wine; exit 0' TERM INT HUP
trap 'echo "$(date +%H:%M:%S) stopped by Steam"; [ -n "$GT" ] && kill $GT 2>/dev/null; stop_wine; save_fex_stamp; exit 0' TERM INT HUP
n=0
while [ $n -lt 90 ] && [ -z "$(game_pids)" ]; do
# A program that ended before it was seen (Steam's install-script step takes about a second) leaves nothing
@@ -402,10 +420,15 @@ while [ $n -lt 90 ] && [ -z "$(game_pids)" ]; do
done
echo "$(date +%H:%M:%S) game process $(game_pids | tr '\n' ' ')after ${n}s"
# The program Steam started counts while it runs: a program that names its main thread (Chromium-based games call
# it CrBrowserMain) changes its process name, which Wine copies, and is then no longer found by name.
while [ -n "$(game_pids)" ] || kill -0 $WP 2>/dev/null; do sleep 2; done
# it CrBrowserMain) changes its process name, which Wine copies, and is then no longer found by name. A launcher
# that started the game and ended leaves the game as another program of this prefix, so the wait goes on while any
# program of the prefix runs, apart from Wine's own services (Proton waits for the prefix's programs the same way).
while [ -n "$(game_pids)" ] || kill -0 $WP 2>/dev/null || programs_left; do sleep 2; done
echo "$(date +%H:%M:%S) game exited"
[ -n "$GT" ] && kill $GT 2>/dev/null
stop_wine
wait $WP
echo "$(date +%H:%M:%S) done"
rc=$?
save_fex_stamp
echo "$(date +%H:%M:%S) done (exit $rc)"
exit $rc