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