1 #include <darwintest.h>
2 #include <mach-o/dyld.h>
8 run_test(const char *name
, cpu_type_t type
, cpu_subtype_t subtype
)
11 posix_spawnattr_t spawnattr
;
13 uint32_t size
= sizeof(path
);
14 cpu_type_t cpuprefs
[] = { type
};
15 cpu_type_t subcpuprefs
[] = { subtype
};
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
);
20 ret
= posix_spawnattr_init(&spawnattr
);
21 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "%s: posix_spawnattr_init", name
);
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
);
26 ret
= posix_spawn(&pid
, path
, NULL
, &spawnattr
, NULL
, NULL
);
27 T_ASSERT_EQ(ret
, 0, "%s: posix_spawn should succeed", name
);
30 ret
= waitpid(pid
, &wait_ret
, 0);
31 T_QUIET
; T_ASSERT_EQ(ret
, pid
, "%s: child pid", name
);
33 T_QUIET
; T_ASSERT_EQ(WIFEXITED(wait_ret
), 1, "%s: child process should have called exit()", name
);
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
);
39 ret
= posix_spawnattr_destroy(&spawnattr
);
40 T_QUIET
; T_ASSERT_EQ(ret
, 0, "%s: posix_spawnattr_destroy", name
);
43 T_DECL(posix_spawn_archpref
, "verify posix_spawn_setarchpref_np can select slices")
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__) */
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
);
61 #error unknown architecture