3 #include <darwintest.h>
4 #include <mach-o/dyld.h>
10 T_DECL(no32exec_bootarg_with_spawn
, "make sure we can't posix_spawn 32-bit")
14 uint32_t size
= sizeof(path
);
16 T_QUIET
; T_ASSERT_EQ(_NSGetExecutablePath(path
, &size
), 0, NULL
);
17 T_QUIET
; T_ASSERT_LT(strlcat(path
, "_helper", size
), (unsigned long)size
, NULL
);
19 spawn_ret
= posix_spawn(&pid
, path
, NULL
, NULL
, NULL
, NULL
);
22 waitpid(pid
, &wait_ret
, 0);
23 T_ASSERT_FALSE(WIFEXITED(wait_ret
), "i386 helper should not run");
25 T_ASSERT_EQ(spawn_ret
, EBADARCH
, NULL
);
28 T_DECL(no32_exec_bootarg_with_exec
, "make sure we can't fork and exec 32-bit")
32 uint32_t size
= sizeof(path
);
34 T_QUIET
; T_ASSERT_EQ(_NSGetExecutablePath(path
, &size
), 0, NULL
);
35 T_QUIET
; T_ASSERT_LT(strlcat(path
, "_helper", size
), (unsigned long)size
, NULL
);
38 T_QUIET
; T_ASSERT_POSIX_SUCCESS(pid
, "fork");
40 if (pid
== 0) { /* child */
41 execve(path
, NULL
, NULL
); /* this should fail, resulting in the call to exit below */
45 waitpid(pid
, &wait_ret
, 0);
46 T_QUIET
; T_ASSERT_TRUE(WIFEXITED(wait_ret
), "child should have called exit()");
47 T_ASSERT_EQ(WEXITSTATUS(wait_ret
), EBADARCH
, "execve should set errno = EBADARCH");
51 T_DECL(no32exec_bootarg_with_spawn_binprefs
, "make sure we honor no32exec, using posix_spawn with binprefs on a fat i386/x86_64 Mach-O")
54 posix_spawnattr_t spawnattr
;
55 cpu_type_t cpuprefs
[] = { CPU_TYPE_X86
, CPU_TYPE_X86_64
};
58 uint32_t size
= sizeof(path
);
59 T_QUIET
; T_ASSERT_EQ(_NSGetExecutablePath(path
, &size
), 0, NULL
);
60 T_QUIET
; T_ASSERT_LT(strlcat(path
, "_helper_binprefs", size
), (unsigned long)size
, NULL
);
62 ret
= posix_spawnattr_init(&spawnattr
);
63 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "posix_spawnattr_init");
65 ret
= posix_spawnattr_setbinpref_np(&spawnattr
, sizeof(cpuprefs
) / sizeof(cpuprefs
[0]), cpuprefs
, NULL
);
66 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "posix_spawnattr_setbinpref_np");
68 ret
= posix_spawn(&pid
, path
, NULL
, &spawnattr
, NULL
, NULL
);
69 T_ASSERT_EQ(ret
, 0, "posix_spawn should succeed despite 32-bit binpref appearing first");
72 ret
= waitpid(pid
, &wait_ret
, 0);
73 T_QUIET
; T_ASSERT_EQ(ret
, pid
, "child pid");
75 T_QUIET
; T_ASSERT_EQ(WIFEXITED(wait_ret
), 1, "child process should have called exit()");
76 T_ASSERT_EQ(WEXITSTATUS(wait_ret
), 8, "child process should be running in 64-bit mode");
78 ret
= posix_spawnattr_destroy(&spawnattr
);
79 T_QUIET
; T_ASSERT_EQ(ret
, 0, "posix_spawnattr_destroy");
82 T_DECL(no32exec_bootarg_with_32only_spawn_binprefs
, "make sure we honor no32exec, using posix_spawn with 32-bit only binprefs on a fat i386/x86_64 Mach-O")
84 int pid
, ret
, spawn_ret
;
85 posix_spawnattr_t spawnattr
;
86 cpu_type_t cpuprefs
[] = { CPU_TYPE_X86
};
89 uint32_t size
= sizeof(path
);
90 T_QUIET
; T_ASSERT_EQ(_NSGetExecutablePath(path
, &size
), 0, NULL
);
91 T_QUIET
; T_ASSERT_LT(strlcat(path
, "_helper_binprefs", size
), (unsigned long)size
, NULL
);
93 ret
= posix_spawnattr_init(&spawnattr
);
94 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "posix_spawnattr_init");
96 ret
= posix_spawnattr_setbinpref_np(&spawnattr
, sizeof(cpuprefs
) / sizeof(cpuprefs
[0]), cpuprefs
, NULL
);
97 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "posix_spawnattr_setbinpref_np");
99 spawn_ret
= posix_spawn(&pid
, path
, NULL
, &spawnattr
, NULL
, NULL
);
100 T_ASSERT_EQ(spawn_ret
, EBADARCH
, "posix_spawn should return EBADARCH since only 32-bit binpref is requested");
102 ret
= posix_spawnattr_destroy(&spawnattr
);
103 T_QUIET
; T_ASSERT_EQ(ret
, 0, "posix_spawnattr_destroy");