Skip to content

Commit b02d13c

Browse files
authored
Re-enable/fix non-argv[0] exe name tests (#1046)
1 parent 81949f0 commit b02d13c

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

test/libc/calls/getprogramexecutablename_test.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,55 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/calls.h"
20+
#include "libc/calls/metalfile.internal.h"
2021
#include "libc/dce.h"
2122
#include "libc/limits.h"
2223
#include "libc/runtime/runtime.h"
24+
#include "libc/serialize.h"
2325
#include "libc/stdio/stdio.h"
2426
#include "libc/str/str.h"
2527
#include "libc/sysv/consts/o.h"
28+
#include "libc/sysv/consts/ok.h"
2629
#include "libc/testlib/ezbench.h"
2730
#include "libc/testlib/subprocess.h"
2831
#include "libc/testlib/testlib.h"
2932

3033
static char *self;
34+
static bool skiptests;
3135

3236
void SetUpOnce(void) {
3337
self = GetProgramExecutableName();
3438
testlib_enable_tmp_setup_teardown();
39+
if (IsMetal()) {
40+
skiptests = true;
41+
} else if (IsOpenbsd() || (IsXnu() && !IsXnuSilicon())) {
42+
ASSERT_STRNE(self, "");
43+
ASSERT_SYS(0, 3, open(self, O_RDONLY));
44+
char buf[8];
45+
ASSERT_SYS(0, 8, pread(3, buf, 8, 0));
46+
ASSERT_SYS(0, 0, close(3));
47+
if (READ64LE(buf) != READ64LE("MZqFpD='") &&
48+
READ64LE(buf) != READ64LE("jartsr='") &&
49+
READ64LE(buf) != READ64LE("APEDBG='")) {
50+
fprintf(stderr,
51+
"we appear to be running as an assimilated binary on OpenBSD or "
52+
"x86_64 XNU;\nGetProgramExecutableName is unreliable here\n");
53+
skiptests = true;
54+
}
55+
}
3556
}
3657

3758
__attribute__((__constructor__)) static void Child(int argc, char *argv[]) {
3859
if (argc >= 2 && !strcmp(argv[1], "Child")) {
3960
if (strcmp(argv[2], GetProgramExecutableName())) {
4061
exit(123);
4162
}
63+
if (!IsXnuSilicon()) exit(0);
64+
/* TODO(mrdomino): argv[0] tests only pass on XnuSilicon right now because
65+
__sys_execve fails there, so the ape loader is used.
66+
the correct check is "we have been invoked either as an
67+
assimilated binary or via the ape loader, and not via a
68+
raw __sys_execve." */
4269
if (strcmp(argv[3], argv[0])) {
4370
exit(124);
4471
}
@@ -47,34 +74,33 @@ __attribute__((__constructor__)) static void Child(int argc, char *argv[]) {
4774
}
4875

4976
TEST(GetProgramExecutableName, ofThisFile) {
50-
EXPECT_EQ('/', *self);
51-
EXPECT_TRUE(!!strstr(self, "getprogramexecutablename_test"));
77+
if (IsMetal()) {
78+
EXPECT_STREQ(self, APE_COM_NAME);
79+
} else {
80+
EXPECT_EQ('/', *self);
81+
EXPECT_TRUE(!!strstr(self, "getprogramexecutablename_test"));
82+
EXPECT_SYS(0, 0, access(self, X_OK));
83+
}
5284
}
5385

54-
/* TODO(mrdomino): these tests only pass on XnuSilicon right now because
55-
__sys_execve fails there, so the ape loader is used.
56-
the correct check here is "we have been invoked either
57-
as an assimilated binary or via the ape loader, and not
58-
via a raw __sys_execve." */
59-
6086
TEST(GetProgramExecutableName, nullEnv) {
61-
if (!IsXnuSilicon()) return;
87+
if (skiptests) return;
6288
SPAWN(fork);
6389
execve(self, (char *[]){self, "Child", self, self, 0}, (char *[]){0});
6490
abort();
6591
EXITS(0);
6692
}
6793

6894
TEST(GetProramExecutableName, weirdArgv0NullEnv) {
69-
if (!IsXnuSilicon()) return;
95+
if (skiptests) return;
7096
SPAWN(fork);
7197
execve(self, (char *[]){"hello", "Child", self, "hello", 0}, (char *[]){0});
7298
abort();
7399
EXITS(0);
74100
}
75101

76102
TEST(GetProgramExecutableName, weirdArgv0CosmoVar) {
77-
if (!IsXnuSilicon()) return;
103+
if (skiptests) return;
78104
char buf[32 + PATH_MAX];
79105
stpcpy(stpcpy(buf, "COSMOPOLITAN_PROGRAM_EXECUTABLE="), self);
80106
SPAWN(fork);
@@ -85,7 +111,7 @@ TEST(GetProgramExecutableName, weirdArgv0CosmoVar) {
85111
}
86112

87113
TEST(GetProgramExecutableName, weirdArgv0WrongCosmoVar) {
88-
if (!IsXnuSilicon()) return;
114+
if (skiptests) return;
89115
char *bad = "COSMOPOLITAN_PROGRAM_EXECUTABLE=hi";
90116
SPAWN(fork);
91117
execve(self, (char *[]){"hello", "Child", self, "hello", 0},
@@ -95,7 +121,7 @@ TEST(GetProgramExecutableName, weirdArgv0WrongCosmoVar) {
95121
}
96122

97123
TEST(GetProgramExecutableName, movedSelf) {
98-
if (!IsXnuSilicon()) return;
124+
if (skiptests) return;
99125
char buf[BUFSIZ];
100126
ASSERT_SYS(0, 3, open(GetProgramExecutableName(), O_RDONLY));
101127
ASSERT_SYS(0, 4, creat("test", 0755));

0 commit comments

Comments
 (0)