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