]> git.saurik.com Git - apple/xnu.git/blame - tests/posix_spawn_archpref.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / tests / posix_spawn_archpref.c
CommitLineData
f427ee49
A
1#include <darwintest.h>
2#include <mach-o/dyld.h>
3#include <spawn.h>
4#include <unistd.h>
5#include <sys/wait.h>
6
7static void
8run_test(const char *name, cpu_type_t type, cpu_subtype_t subtype)
9{
10 int ret, pid;
11 posix_spawnattr_t spawnattr;
12 char path[1024];
13 uint32_t size = sizeof(path);
14 cpu_type_t cpuprefs[] = { type };
15 cpu_type_t subcpuprefs[] = { subtype };
16
17 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
18 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper", size), (unsigned long)size, NULL);
19
20 ret = posix_spawnattr_init(&spawnattr);
21 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "%s: posix_spawnattr_init", name);
22
23 ret = posix_spawnattr_setarchpref_np(&spawnattr, sizeof(cpuprefs) / sizeof(cpuprefs[0]), cpuprefs, subcpuprefs, NULL);
24 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "%s: posix_spawnattr_setarchpref_np", name);
25
26 ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
27 T_ASSERT_EQ(ret, 0, "%s: posix_spawn should succeed", name);
28
29 int wait_ret = 0;
30 ret = waitpid(pid, &wait_ret, 0);
31 T_QUIET; T_ASSERT_EQ(ret, pid, "%s: child pid", name);
32
33 T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "%s: child process should have called exit()", name);
34
35 if (subtype != CPU_SUBTYPE_ANY) {
36 T_ASSERT_EQ(WEXITSTATUS(wait_ret), subtype, "%s: child process should be running with %d subtype", name, subtype);
37 }
38
39 ret = posix_spawnattr_destroy(&spawnattr);
40 T_QUIET; T_ASSERT_EQ(ret, 0, "%s: posix_spawnattr_destroy", name);
41}
42
43T_DECL(posix_spawn_archpref, "verify posix_spawn_setarchpref_np can select slices")
44{
45#if defined(__x86_64__)
46 run_test("x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
47#endif /* defined(__x86_64__) */
48#if defined(__arm64__) && defined(__LP64__)
49 run_test("arm64", CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL);
50#endif /* defined(__arm64__) && defined(__LP64__) */
2a1bd2d3
A
51#if defined(__arm64e__)
52 run_test("arm64e", CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64E);
53#endif /* defined(__arm64e__) */
f427ee49
A
54
55#if defined(__x86_64__)
56 run_test("any (x86_64)", CPU_TYPE_X86_64, CPU_SUBTYPE_ANY);
57#elif defined(__arm64__) && defined(__LP64__)
58 run_test("any (arm64)", CPU_TYPE_ARM64, CPU_SUBTYPE_ANY);
59#elif defined(__arm64__)
60 run_test("any (arm64_32)", CPU_TYPE_ARM64_32, CPU_SUBTYPE_ANY);
61#elif defined(__arm__)
62 run_test("any (arm)", CPU_TYPE_ARM, CPU_SUBTYPE_ANY);
63#else
64#error unknown architecture
65#endif
66}