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