pid_t pid; \ int status; \ while (!dt_stat_stable(s)) { \ T_STAT_MEASURE(s) { \ err = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); \ } \ if (err) { \ T_FAIL("posix_spawn returned %d", err); \ } \ waitpid(pid, &status, 0); \ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { \ T_FAIL("Child process of posix_spawn failed to run"); \ } \ } T_DECL(posix_spawn_platform_binary_latency, "posix_spawn platform binary latency", T_META_TYPE_PERF, T_META_CHECK_LEAKS(NO)) { { dt_stat_time_t s = dt_stat_time_create("time"); SPAWN_MEASURE_LOOP(s); dt_stat_finalize(s); } { dt_stat_thread_cpu_time_t s = dt_stat_thread_cpu_time_create("on-cpu time"); SPAWN_MEASURE_LOOP(s); dt_stat_finalize(s); } } #define FORK_MEASURE_LOOP(s) \ pid_t pid; \
#include <Block.h> #include <darwintest.h> #include <dispatch/dispatch.h> #include <fcntl.h> #include <limits.h> #include <signal.h> #include <stdbool.h> #include <stdlib.h> #include <stdint.h> #include <unistd.h> #include <util.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.kevent"), T_META_CHECK_LEAKS(false)); #define TIMEOUT_SECS 10 static int child_ready[2]; static void child_tty_client(void) { dispatch_source_t src; char buf[16] = ""; ssize_t bytes_wr; src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)STDIN_FILENO, 0, NULL); if (!src) {
#include <sys/kdebug.h> #include <ktrace.h> #include <spawn.h> #include <stdio.h> #include <stdlib.h> #include <darwintest.h> // From bsd/sys/proc_internal.h #define PID_MAX 99999 T_DECL(exit, "exit(2) time from syscall start to end", T_META_TYPE_PERF, T_META_CHECK_LEAKS(NO)) { _Atomic static int ended = 0; dispatch_queue_t spawn_queue; dt_stat_time_t s = dt_stat_time_create("time"); uint64_t *begin_ts = malloc(sizeof(uint64_t) * PID_MAX); if (begin_ts == NULL) { T_FAIL("Error allocating timestamp array"); } ktrace_session_t session; session = ktrace_session_create(); if (session == NULL) { T_FAIL("Error creating ktrace session"); } ktrace_set_completion_handler(session, ^{ free(begin_ts); dt_stat_finalize(s);
static int exitcode = 0x6789BEEF; int should_exit = 0; void handler (int sig, siginfo_t *sip, __unused void *uconp) { /* Should handle the SIGCHLD signal */ T_ASSERT_EQ_INT(sig, SIGCHLD, "Captured signal returns 0x%x, expected SIGCHLD (0x%x).", sig, SIGCHLD); T_QUIET; T_ASSERT_NOTNULL(sip, "siginfo_t returned NULL but should have returned data."); T_ASSERT_EQ_INT(sip->si_code, CLD_EXITED, "si_code returns 0x%x, expected CLD_EXITED (0x%x).", sip->si_code, CLD_EXITED); T_ASSERT_EQ_INT(sip->si_status, exitcode, "si_status returns 0x%08X, expected the child's exit code (0x%08X).", sip->si_status, exitcode); should_exit = 1; } T_DECL(sigchldreturn, "checks that a child process exited with an exitcode returns correctly to parent", T_META_CHECK_LEAKS(false)) { struct sigaction act; int pid; act.sa_sigaction = handler; act.sa_flags = SA_SIGINFO; /* Set action for signal */ T_QUIET; T_ASSERT_POSIX_SUCCESS(sigaction (SIGCHLD, &act, NULL), "Calling sigaction() failed for SIGCHLD"); /* Now fork a child that just exits */ pid = fork(); T_QUIET; T_ASSERT_NE_INT(pid, -1, "fork() failed!"); if (pid == 0) {
#elif defined(__i386__) #define call_chkstk(value) \ __asm__ volatile("movl %0, %%eax\t\n" \ "calll _thread_chkstk_darwin" : : "i"(value) : "eax") #define TRAPSIG SIGILL #endif static void got_signal(int signo __unused) { T_PASS("calling with 1 << 24 crashed"); T_END; } T_DECL(chkstk, "chkstk", T_META_ALL_VALID_ARCHS(YES), T_META_CHECK_LEAKS(NO)) { #if defined(__arm__) T_SKIP("not on armv7"); #else call_chkstk(1 << 8); T_PASS("calling with 1 << 8"); call_chkstk(1 << 16); T_PASS("calling with 1 << 16"); signal(TRAPSIG, got_signal); call_chkstk(1 << 24); T_FAIL("should have crashed");