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 #include <TargetConditionals.h>
8
9 static void
run_test(const char * name,cpu_type_t type,cpu_subtype_t subtype)10 run_test(const char *name, cpu_type_t type, cpu_subtype_t subtype)
11 {
12 int ret, pid;
13 posix_spawnattr_t spawnattr;
14 char path[1024];
15 uint32_t size = sizeof(path);
16 cpu_type_t cpuprefs[] = { type };
17 cpu_type_t subcpuprefs[] = { subtype };
18
19 T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
20 T_QUIET; T_ASSERT_LT(strlcat(path, "_helper", size), (unsigned long)size, NULL);
21
22 ret = posix_spawnattr_init(&spawnattr);
23 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "%s: posix_spawnattr_init", name);
24
25 ret = posix_spawnattr_setarchpref_np(&spawnattr, sizeof(cpuprefs) / sizeof(cpuprefs[0]), cpuprefs, subcpuprefs, NULL);
26 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "%s: posix_spawnattr_setarchpref_np", name);
27
28 ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
29 T_ASSERT_EQ(ret, 0, "%s: posix_spawn should succeed", name);
30
31 int wait_ret = 0;
32 ret = waitpid(pid, &wait_ret, 0);
33 T_QUIET; T_ASSERT_EQ(ret, pid, "%s: child pid", name);
34
35 T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "%s: child process should have called exit()", name);
36
37 if (subtype != CPU_SUBTYPE_ANY) {
38 T_ASSERT_EQ(WEXITSTATUS(wait_ret), subtype, "%s: child process should be running with %d subtype", name, subtype);
39 }
40
41 ret = posix_spawnattr_destroy(&spawnattr);
42 T_QUIET; T_ASSERT_EQ(ret, 0, "%s: posix_spawnattr_destroy", name);
43 }
44
45 T_DECL(posix_spawn_archpref, "verify posix_spawn_setarchpref_np can select slices", T_META_TAG_VM_PREFERRED)
46 {
47 #if defined(__x86_64__)
48 run_test("x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
49 #endif /* defined(__x86_64__) */
50 /* macOS does not build a arm64 slice */
51 #if defined(__arm64__) && defined(__LP64__) && !TARGET_OS_OSX
52 run_test("arm64", CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL);
53 #endif /* defined(__arm64__) && defined(__LP64__) */
54 #if defined(__arm64e__)
55 run_test("arm64e", CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64E);
56 #endif /* defined(__arm64e__) */
57
58 #if defined(__x86_64__)
59 run_test("any (x86_64)", CPU_TYPE_X86_64, CPU_SUBTYPE_ANY);
60 #elif defined(__arm64__) && defined(__LP64__)
61 run_test("any (arm64)", CPU_TYPE_ARM64, CPU_SUBTYPE_ANY);
62 #elif defined(__arm64__)
63 run_test("any (arm64_32)", CPU_TYPE_ARM64_32, CPU_SUBTYPE_ANY);
64 #else
65 #error unknown architecture
66 #endif
67 }
68