xref: /xnu-11417.140.69/tests/posix_spawn_alt_rosetta.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include <darwintest.h>
2*43a90889SApple OSS Distributions #include <mach-o/dyld.h>
3*43a90889SApple OSS Distributions #include <spawn.h>
4*43a90889SApple OSS Distributions #include <spawn_private.h>
5*43a90889SApple OSS Distributions #include <unistd.h>
6*43a90889SApple OSS Distributions #include <sys/wait.h>
7*43a90889SApple OSS Distributions #include <fcntl.h>
8*43a90889SApple OSS Distributions #include <stdlib.h>
9*43a90889SApple OSS Distributions 
10*43a90889SApple OSS Distributions #include <TargetConditionals.h>
11*43a90889SApple OSS Distributions 
12*43a90889SApple OSS Distributions T_DECL(posix_spawn_alt_rosetta,
13*43a90889SApple OSS Distributions     "verify posix_spawn_set_alt_rosetta_np switches to alternative rosetta runtime",
14*43a90889SApple OSS Distributions     T_META_ASROOT(true),
15*43a90889SApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1),
16*43a90889SApple OSS Distributions     T_META_ENABLED(false /* rdar://133955592 */))
17*43a90889SApple OSS Distributions {
18*43a90889SApple OSS Distributions #if TARGET_OS_OSX && defined(__arm64__)
19*43a90889SApple OSS Distributions 	int ret, pid;
20*43a90889SApple OSS Distributions 	posix_spawnattr_t spawnattr;
21*43a90889SApple OSS Distributions 	char path[1024];
22*43a90889SApple OSS Distributions 	uint32_t size = sizeof(path);
23*43a90889SApple OSS Distributions 	cpu_type_t cpuprefs[] = { CPU_TYPE_X86_64 };
24*43a90889SApple OSS Distributions 	cpu_type_t subcpuprefs[] = { CPU_SUBTYPE_ANY };
25*43a90889SApple OSS Distributions 	int wait_ret = 0;
26*43a90889SApple OSS Distributions 
27*43a90889SApple OSS Distributions 	if (access("/Library/Apple/usr/libexec/oah/libRosettaRuntime", O_RDONLY) != 0) {
28*43a90889SApple OSS Distributions 		T_SKIP("Rosetta not installed");
29*43a90889SApple OSS Distributions 		return;
30*43a90889SApple OSS Distributions 	}
31*43a90889SApple OSS Distributions 
32*43a90889SApple OSS Distributions 	if (access("/usr/local/libexec/rosetta/runtime_internal", O_RDONLY) != 0) {
33*43a90889SApple OSS Distributions 		system("mkdir -p /usr/local/libexec/oah");
34*43a90889SApple OSS Distributions 		system("cp /Library/Apple/usr/libexec/rosetta/runtime /usr/local/libexec/rosetta/runtime_internal");
35*43a90889SApple OSS Distributions 	}
36*43a90889SApple OSS Distributions 
37*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(_NSGetExecutablePath(path, &size), 0, NULL);
38*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_LT(strlcat(path, "_helper", size), (unsigned long)size, NULL);
39*43a90889SApple OSS Distributions 
40*43a90889SApple OSS Distributions 	ret = posix_spawnattr_init(&spawnattr);
41*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
42*43a90889SApple OSS Distributions 
43*43a90889SApple OSS Distributions 	// 1) Run natively
44*43a90889SApple OSS Distributions 
45*43a90889SApple OSS Distributions 	ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
46*43a90889SApple OSS Distributions 	T_ASSERT_EQ(ret, 0, "posix_spawn should succeed");
47*43a90889SApple OSS Distributions 	ret = waitpid(pid, &wait_ret, 0);
48*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, pid, "child pid");
49*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "child process should have called exit()");
50*43a90889SApple OSS Distributions 	T_ASSERT_EQ(WEXITSTATUS(wait_ret), 0, "running natively should return 0");
51*43a90889SApple OSS Distributions 
52*43a90889SApple OSS Distributions 	// 2) Set archpref to run under Rosetta
53*43a90889SApple OSS Distributions 
54*43a90889SApple OSS Distributions 	ret = posix_spawnattr_setarchpref_np(&spawnattr, sizeof(cpuprefs) / sizeof(cpuprefs[0]), cpuprefs, subcpuprefs, NULL);
55*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setarchpref_np");
56*43a90889SApple OSS Distributions 
57*43a90889SApple OSS Distributions 	ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
58*43a90889SApple OSS Distributions 	T_ASSERT_EQ(ret, 0, "posix_spawn should succeed");
59*43a90889SApple OSS Distributions 	ret = waitpid(pid, &wait_ret, 0);
60*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, pid, "child pid");
61*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "child process should have called exit()");
62*43a90889SApple OSS Distributions 	T_ASSERT_EQ(WEXITSTATUS(wait_ret), 1, "running in rosetta should return 1");
63*43a90889SApple OSS Distributions 
64*43a90889SApple OSS Distributions 	// 3) Request alternative Rosetta runtime
65*43a90889SApple OSS Distributions 
66*43a90889SApple OSS Distributions 	ret = posix_spawnattr_set_alt_rosetta_np(&spawnattr, 0);
67*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setarchpref_np");
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions 	ret = posix_spawn(&pid, path, NULL, &spawnattr, NULL, NULL);
70*43a90889SApple OSS Distributions 	T_ASSERT_EQ(ret, 0, "posix_spawn should succeed");
71*43a90889SApple OSS Distributions 	ret = waitpid(pid, &wait_ret, 0);
72*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, pid, "child pid");
73*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(WIFEXITED(wait_ret), 1, "child process should have called exit()");
74*43a90889SApple OSS Distributions 	T_ASSERT_EQ(WEXITSTATUS(wait_ret), 2, "running with alternative rosetta runtime should return 2");
75*43a90889SApple OSS Distributions 
76*43a90889SApple OSS Distributions 	ret = posix_spawnattr_destroy(&spawnattr);
77*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "posix_spawnattr_destroy");
78*43a90889SApple OSS Distributions #else
79*43a90889SApple OSS Distributions 	T_SKIP("Not arm64 macOS");
80*43a90889SApple OSS Distributions #endif
81*43a90889SApple OSS Distributions }
82