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