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