From 77facc4f7baa9d22b5106f8a9971d2a46fbec226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Sun, 17 Dec 2023 10:30:22 -0500 Subject: [PATCH] Cover more cases for detecting ape loader (#1022) Fixes a regression in GetProgramExecutableName on Linux against old loaders. In the loader case, /proc/self/exe gives the loader's path. We tried to detect this by checking for `/usr/bin/ape`. But that is only one of the possible places the loader could be. --- libc/calls/getprogramexecutablename.greg.c | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/libc/calls/getprogramexecutablename.greg.c b/libc/calls/getprogramexecutablename.greg.c index 05c690b98..7ab67b8f0 100644 --- a/libc/calls/getprogramexecutablename.greg.c +++ b/libc/calls/getprogramexecutablename.greg.c @@ -23,6 +23,7 @@ #include "libc/cosmo.h" #include "libc/dce.h" #include "libc/errno.h" +#include "libc/fmt/libgen.h" #include "libc/intrin/getenv.internal.h" #include "libc/serialize.h" #include "libc/limits.h" @@ -50,6 +51,25 @@ static inline int IsAlpha(int c) { return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); } +static inline int AllNumDot(const char *s) { + while (true) { + switch (*s++) { + default: return 0; + case 0: return 1; + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': case '.': + /* continue */ + } + } +} + +static int IsApeLoader(char *s) { + char *b; + return !strcmp(s, "/usr/bin/ape") || + (!strncmp((b = basename(s)), ".ape-", 5) && + AllNumDot(b + 5)); +} + static inline void InitProgramExecutableNameImpl(void) { size_t n; ssize_t got; @@ -96,7 +116,7 @@ static inline void InitProgramExecutableNameImpl(void) { } cmd[3] = -1; // current process if (sys_sysctl(cmd, ARRAYLEN(cmd), b, &n, 0, 0) != -1) { - if (strcmp(b, "/usr/bin/ape")) { // XX old loader; warn? + if (!IsApeLoader(b)) { goto UseBuf; } } @@ -105,7 +125,7 @@ static inline void InitProgramExecutableNameImpl(void) { if ((got = sys_readlinkat(AT_FDCWD, "/proc/self/exe", b, n)) > 0 || (got = sys_readlinkat(AT_FDCWD, "/proc/curproc/file", b, n)) > 0) { b[got] = 0; - if (strcmp(b, "/usr/bin/ape")) { + if (!IsApeLoader(b)) { goto UseBuf; } }