17
17
│ PERFORMANCE OF THIS SOFTWARE. │
18
18
╚─────────────────────────────────────────────────────────────────────────────*/
19
19
#include "libc/calls/calls.h"
20
+ #include "libc/calls/metalfile.internal.h"
20
21
#include "libc/dce.h"
21
22
#include "libc/limits.h"
22
23
#include "libc/runtime/runtime.h"
24
+ #include "libc/serialize.h"
23
25
#include "libc/stdio/stdio.h"
24
26
#include "libc/str/str.h"
25
27
#include "libc/sysv/consts/o.h"
28
+ #include "libc/sysv/consts/ok.h"
26
29
#include "libc/testlib/ezbench.h"
27
30
#include "libc/testlib/subprocess.h"
28
31
#include "libc/testlib/testlib.h"
29
32
30
33
static char * self ;
34
+ static bool skiptests ;
31
35
32
36
void SetUpOnce (void ) {
33
37
self = GetProgramExecutableName ();
34
38
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
+ }
35
56
}
36
57
37
58
__attribute__((__constructor__ )) static void Child (int argc , char * argv []) {
38
59
if (argc >= 2 && !strcmp (argv [1 ], "Child" )) {
39
60
if (strcmp (argv [2 ], GetProgramExecutableName ())) {
40
61
exit (123 );
41
62
}
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." */
42
69
if (strcmp (argv [3 ], argv [0 ])) {
43
70
exit (124 );
44
71
}
@@ -47,34 +74,33 @@ __attribute__((__constructor__)) static void Child(int argc, char *argv[]) {
47
74
}
48
75
49
76
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
+ }
52
84
}
53
85
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
-
60
86
TEST (GetProgramExecutableName , nullEnv ) {
61
- if (! IsXnuSilicon () ) return ;
87
+ if (skiptests ) return ;
62
88
SPAWN (fork );
63
89
execve (self , (char * []){self , "Child" , self , self , 0 }, (char * []){0 });
64
90
abort ();
65
91
EXITS (0 );
66
92
}
67
93
68
94
TEST (GetProramExecutableName , weirdArgv0NullEnv ) {
69
- if (! IsXnuSilicon () ) return ;
95
+ if (skiptests ) return ;
70
96
SPAWN (fork );
71
97
execve (self , (char * []){"hello" , "Child" , self , "hello" , 0 }, (char * []){0 });
72
98
abort ();
73
99
EXITS (0 );
74
100
}
75
101
76
102
TEST (GetProgramExecutableName , weirdArgv0CosmoVar ) {
77
- if (! IsXnuSilicon () ) return ;
103
+ if (skiptests ) return ;
78
104
char buf [32 + PATH_MAX ];
79
105
stpcpy (stpcpy (buf , "COSMOPOLITAN_PROGRAM_EXECUTABLE=" ), self );
80
106
SPAWN (fork );
@@ -85,7 +111,7 @@ TEST(GetProgramExecutableName, weirdArgv0CosmoVar) {
85
111
}
86
112
87
113
TEST (GetProgramExecutableName , weirdArgv0WrongCosmoVar ) {
88
- if (! IsXnuSilicon () ) return ;
114
+ if (skiptests ) return ;
89
115
char * bad = "COSMOPOLITAN_PROGRAM_EXECUTABLE=hi" ;
90
116
SPAWN (fork );
91
117
execve (self , (char * []){"hello" , "Child" , self , "hello" , 0 },
@@ -95,7 +121,7 @@ TEST(GetProgramExecutableName, weirdArgv0WrongCosmoVar) {
95
121
}
96
122
97
123
TEST (GetProgramExecutableName , movedSelf ) {
98
- if (! IsXnuSilicon () ) return ;
124
+ if (skiptests ) return ;
99
125
char buf [BUFSIZ ];
100
126
ASSERT_SYS (0 , 3 , open (GetProgramExecutableName (), O_RDONLY ));
101
127
ASSERT_SYS (0 , 4 , creat ("test" , 0755 ));
0 commit comments