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