]> git.saurik.com Git - apple/xnu.git/blob - tests/posix_spawn_archpref.c
5cbfc1e26df0852fa668dddd1ce00d33a163d09a
[apple/xnu.git] / tests / posix_spawn_archpref.c
1 #include <darwintest.h>
2 #include <mach-o/dyld.h>
3 #include <spawn.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6
7 static void
8 run_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
43 T_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__) */
51
52 #if defined(__x86_64__)
53 run_test("any (x86_64)", CPU_TYPE_X86_64, CPU_SUBTYPE_ANY);
54 #elif defined(__arm64__) && defined(__LP64__)
55 run_test("any (arm64)", CPU_TYPE_ARM64, CPU_SUBTYPE_ANY);
56 #elif defined(__arm64__)
57 run_test("any (arm64_32)", CPU_TYPE_ARM64_32, CPU_SUBTYPE_ANY);
58 #elif defined(__arm__)
59 run_test("any (arm)", CPU_TYPE_ARM, CPU_SUBTYPE_ANY);
60 #else
61 #error unknown architecture
62 #endif
63 }