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