]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_fork.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_fork.c
index 2f09ba8eed297bf3f50b492a6272e59df3d710a9..9417b09117d740b8be1b3a6e9420770d0f5a7f31 100644 (file)
 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
 extern void dtrace_fasttrap_fork(proc_t, proc_t);
 extern void (*dtrace_helpers_fork)(proc_t, proc_t);
 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
 extern void dtrace_fasttrap_fork(proc_t, proc_t);
 extern void (*dtrace_helpers_fork)(proc_t, proc_t);
+extern void (*dtrace_proc_waitfor_exec_ptr)(proc_t);
 extern void dtrace_lazy_dofs_duplicate(proc_t, proc_t);
 
 extern void dtrace_lazy_dofs_duplicate(proc_t, proc_t);
 
+/*
+ * Since dtrace_proc_waitfor_exec_ptr can be added/removed in dtrace_subr.c,
+ * we will store its value before actually calling it.
+ */
+static void (*dtrace_proc_waitfor_hook)(proc_t) = NULL;
+
 #include <sys/dtrace_ptss.h>
 #endif
 
 #include <security/audit/audit.h>
 
 #include <mach/mach_types.h>
 #include <sys/dtrace_ptss.h>
 #endif
 
 #include <security/audit/audit.h>
 
 #include <mach/mach_types.h>
+#include <kern/coalition.h>
 #include <kern/kern_types.h>
 #include <kern/kalloc.h>
 #include <kern/mach_param.h>
 #include <kern/kern_types.h>
 #include <kern/kalloc.h>
 #include <kern/mach_param.h>
@@ -141,10 +149,10 @@ void thread_set_child(thread_t child, int pid);
 void *act_thread_csave(void);
 
 
 void *act_thread_csave(void);
 
 
-thread_t cloneproc(task_t, proc_t, int);
+thread_t cloneproc(task_t, coalition_t, proc_t, int, int);
 proc_t forkproc(proc_t);
 void forkproc_free(proc_t);
 proc_t forkproc(proc_t);
 void forkproc_free(proc_t);
-thread_t fork_create_child(task_t parent_task, proc_t child, int inherit_memory, int is64bit);
+thread_t fork_create_child(task_t parent_task, coalition_t parent_coalition, proc_t child, int inherit_memory, int is64bit);
 void proc_vfork_begin(proc_t parent_proc);
 void proc_vfork_end(proc_t parent_proc);
 
 void proc_vfork_begin(proc_t parent_proc);
 void proc_vfork_end(proc_t parent_proc);
 
@@ -278,15 +286,11 @@ vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval)
        thread_t child_thread;
        int err;
 
        thread_t child_thread;
        int err;
 
-       if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_VFORK)) != 0) {
+       if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_VFORK, COALITION_NULL)) != 0) {
                retval[1] = 0;
        } else {
                retval[1] = 0;
        } else {
-               /*
-                * kludge: rely on uu_proc being set in the vfork case,
-                * rather than returning the actual thread.  We can remove
-                * this when we remove the uu_proc/current_proc() kludge.
-                */
-               proc_t child_proc = current_proc();
+               uthread_t ut = get_bsdthread_info(current_thread());
+               proc_t child_proc = ut->uu_proc;
 
                retval[0] = child_proc->p_pid;
                retval[1] = 1;          /* flag child return for user space */
 
                retval[0] = child_proc->p_pid;
                retval[1] = 1;          /* flag child return for user space */
@@ -299,12 +303,12 @@ vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval)
                proc_signalend(child_proc, 0);
                proc_transend(child_proc, 0);
 
                proc_signalend(child_proc, 0);
                proc_transend(child_proc, 0);
 
-               /* flag the fork has occurred */
                proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid);
                DTRACE_PROC1(create, proc_t, child_proc);
                proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid);
                DTRACE_PROC1(create, proc_t, child_proc);
+               ut->uu_flag &= ~UT_VFORKING;
        }
 
        }
 
-       return(err);
+       return (err);
 }
 
 
 }
 
 
@@ -319,6 +323,11 @@ vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval)
  *                                     Mach thread_t of the child process
  *                                     breated
  *             kind                    kind of creation being requested
  *                                     Mach thread_t of the child process
  *                                     breated
  *             kind                    kind of creation being requested
+ *             coalition               if spawn, coalition the child process
+ *                                     should join, or COALITION_NULL to
+ *                                     inherit the parent's. On non-spawns,
+ *                                     this param is ignored and the child
+ *                                     always inherits the parent's coalition.
  *
  * Notes:      Permissable values for 'kind':
  *
  *
  * Notes:      Permissable values for 'kind':
  *
@@ -350,7 +359,7 @@ vfork(proc_t parent_proc, __unused struct vfork_args *uap, int32_t *retval)
  *             back to the other information.
  */
 int
  *             back to the other information.
  */
 int
-fork1(proc_t parent_proc, thread_t *child_threadp, int kind)
+fork1(proc_t parent_proc, thread_t *child_threadp, int kind, coalition_t coalition)
 {
        thread_t parent_thread = (thread_t)current_thread();
        uthread_t parent_uthread = (uthread_t)get_bsdthread_info(parent_thread);
 {
        thread_t parent_thread = (thread_t)current_thread();
        uthread_t parent_uthread = (uthread_t)get_bsdthread_info(parent_thread);
@@ -484,7 +493,20 @@ fork1(proc_t parent_proc, thread_t *child_threadp, int kind)
                child_proc->p_vforkact = parent_thread;
                child_proc->p_stat = SRUN;
 
                child_proc->p_vforkact = parent_thread;
                child_proc->p_stat = SRUN;
 
-               parent_uthread->uu_flag |= UT_VFORK;
+               /*
+                * Until UT_VFORKING is cleared at the end of the vfork
+                * syscall, the process identity of this thread is slightly
+                * murky.
+                *
+                * As long as UT_VFORK and it's associated field (uu_proc)
+                * is set, current_proc() will always return the child process.
+                *
+                * However dtrace_proc_selfpid() returns the parent pid to
+                * ensure that e.g. the proc:::create probe actions accrue
+                * to the parent.  (Otherwise the child magically seems to
+                * have created itself!)
+                */
+               parent_uthread->uu_flag |= UT_VFORK | UT_VFORKING;
                parent_uthread->uu_proc = child_proc;
                parent_uthread->uu_userstate = (void *)act_thread_csave();
                parent_uthread->uu_vforkmask = parent_uthread->uu_sigmask;
                parent_uthread->uu_proc = child_proc;
                parent_uthread->uu_userstate = (void *)act_thread_csave();
                parent_uthread->uu_vforkmask = parent_uthread->uu_sigmask;
@@ -529,7 +551,11 @@ fork1(proc_t parent_proc, thread_t *child_threadp, int kind)
                 * will, in effect, create a duplicate of it, with only minor
                 * differences.  Contrarily, spawned processes do not inherit.
                 */
                 * will, in effect, create a duplicate of it, with only minor
                 * differences.  Contrarily, spawned processes do not inherit.
                 */
-               if ((child_thread = cloneproc(parent_proc->task, parent_proc, spawn ? FALSE : TRUE)) == NULL) {
+               if ((child_thread = cloneproc(parent_proc->task,
+                                               spawn ? coalition : COALITION_NULL,
+                                               parent_proc,
+                                               spawn ? FALSE : TRUE,
+                                               FALSE)) == NULL) {
                        /* Failed to create thread */
                        err = EAGAIN;
                        goto bad;
                        /* Failed to create thread */
                        err = EAGAIN;
                        goto bad;
@@ -654,12 +680,6 @@ fork1(proc_t parent_proc, thread_t *child_threadp, int kind)
        /* return the thread pointer to the caller */
        *child_threadp = child_thread;
 
        /* return the thread pointer to the caller */
        *child_threadp = child_thread;
 
-#if CONFIG_MEMORYSTATUS
-       if (!err) {
-               memorystatus_list_add(child_proc->p_pid, DEFAULT_JETSAM_PRIORITY, -1);
-       }
-#endif
-
 bad:
        /*
         * In the error case, we return a 0 value for the returned pid (but
 bad:
        /*
         * In the error case, we return a 0 value for the returned pid (but
@@ -738,6 +758,7 @@ vfork_return(proc_t child_proc, int32_t *retval, int rval)
  *             process
  *
  * Parameters: parent_task             parent task
  *             process
  *
  * Parameters: parent_task             parent task
+ *             parent_coalition        parent_coalition
  *             child_proc              child process
  *             inherit_memory          TRUE, if the parents address space is
  *                                     to be inherited by the child
  *             child_proc              child process
  *             inherit_memory          TRUE, if the parents address space is
  *                                     to be inherited by the child
@@ -751,17 +772,18 @@ vfork_return(proc_t child_proc, int32_t *retval, int rval)
  *             vfork() equivalent call, and in the system bootstrap case.
  *
  *             It creates a new task and thread (and as a side effect of the
  *             vfork() equivalent call, and in the system bootstrap case.
  *
  *             It creates a new task and thread (and as a side effect of the
- *             thread creation, a uthread), which is then associated with the
- *             process 'child'.  If the parent process address space is to
- *             be inherited, then a flag indicates that the newly created
- *             task should inherit this from the child task.
+ *             thread creation, a uthread) in the parent coalition, which is
+ *             then associated with the process 'child'.  If the parent
+ *             process address space is to be inherited, then a flag
+ *             indicates that the newly created task should inherit this from
+ *             the child task.
  *
  *             As a special concession to bootstrapping the initial process
  *             in the system, it's possible for 'parent_task' to be TASK_NULL;
  *             in this case, 'inherit_memory' MUST be FALSE.
  */
 thread_t
  *
  *             As a special concession to bootstrapping the initial process
  *             in the system, it's possible for 'parent_task' to be TASK_NULL;
  *             in this case, 'inherit_memory' MUST be FALSE.
  */
 thread_t
-fork_create_child(task_t parent_task, proc_t child_proc, int inherit_memory, int is64bit)
+fork_create_child(task_t parent_task, coalition_t parent_coalition, proc_t child_proc, int inherit_memory, int is64bit)
 {
        thread_t        child_thread = NULL;
        task_t          child_task;
 {
        thread_t        child_thread = NULL;
        task_t          child_task;
@@ -769,11 +791,13 @@ fork_create_child(task_t parent_task, proc_t child_proc, int inherit_memory, int
 
        /* Create a new task for the child process */
        result = task_create_internal(parent_task,
 
        /* Create a new task for the child process */
        result = task_create_internal(parent_task,
+                                       parent_coalition,
                                        inherit_memory,
                                        is64bit,
                                        &child_task);
        if (result != KERN_SUCCESS) {
                                        inherit_memory,
                                        is64bit,
                                        &child_task);
        if (result != KERN_SUCCESS) {
-               printf("execve: task_create_internal failed.  Code: %d\n", result);
+               printf("%s: task_create_internal failed.  Code: %d\n",
+                   __func__, result);
                goto bad;
        }
 
                goto bad;
        }
 
@@ -793,12 +817,6 @@ fork_create_child(task_t parent_task, proc_t child_proc, int inherit_memory, int
        else
                vm_map_set_32bit(get_task_map(child_task));
 
        else
                vm_map_set_32bit(get_task_map(child_task));
 
-#if CONFIG_MACF
-       /* Update task for MAC framework */
-       /* valid to use p_ucred as child is still not running ... */
-       mac_task_label_update_cred(child_proc->p_ucred, child_task);
-#endif
-
        /*
         * Set child process BSD visible scheduler priority if nice value
         * inherited from parent
        /*
         * Set child process BSD visible scheduler priority if nice value
         * inherited from parent
@@ -809,14 +827,15 @@ fork_create_child(task_t parent_task, proc_t child_proc, int inherit_memory, int
        /* Create a new thread for the child process */
        result = thread_create(child_task, &child_thread);
        if (result != KERN_SUCCESS) {
        /* Create a new thread for the child process */
        result = thread_create(child_task, &child_thread);
        if (result != KERN_SUCCESS) {
-               printf("execve: thread_create failed. Code: %d\n", result);
+               printf("%s: thread_create failed. Code: %d\n",
+                   __func__, result);
                task_deallocate(child_task);
                child_task = NULL;
        }
 
        /*
                task_deallocate(child_task);
                child_task = NULL;
        }
 
        /*
-        * Tag thread as being the first thread in its task.
-        */
+         * Tag thread as being the first thread in its task.
+         */
        thread_set_tag(child_thread, THREAD_TAG_MAINTHREAD);
 
 bad:
        thread_set_tag(child_thread, THREAD_TAG_MAINTHREAD);
 
 bad:
@@ -868,7 +887,7 @@ fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval)
 
        retval[1] = 0;          /* flag parent return for user space */
 
 
        retval[1] = 0;          /* flag parent return for user space */
 
-       if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_FORK)) == 0) {
+       if ((err = fork1(parent_proc, &child_thread, PROC_CREATE_FORK, COALITION_NULL)) == 0) {
                task_t child_task;
                proc_t child_proc;
 
                task_t child_task;
                proc_t child_proc;
 
@@ -888,6 +907,11 @@ fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval)
                proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid);
                DTRACE_PROC1(create, proc_t, child_proc);
 
                proc_knote(parent_proc, NOTE_FORK | child_proc->p_pid);
                DTRACE_PROC1(create, proc_t, child_proc);
 
+#if CONFIG_DTRACE
+               if ((dtrace_proc_waitfor_hook = dtrace_proc_waitfor_exec_ptr) != NULL)
+                       (*dtrace_proc_waitfor_hook)(child_proc);
+#endif
+
                /* "Return" to the child */
                (void)thread_resume(child_thread);
 
                /* "Return" to the child */
                (void)thread_resume(child_thread);
 
@@ -918,6 +942,8 @@ fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval)
  *                                     memory from the parent; if this is
  *                                     non-NULL, then the parent_task must
  *                                     also be non-NULL
  *                                     memory from the parent; if this is
  *                                     non-NULL, then the parent_task must
  *                                     also be non-NULL
+ *             memstat_internal        Whether to track the process in the
+ *                                     jetsam priority list (if configured)
  *
  * Returns:    !NULL                   pointer to new child thread
  *             NULL                    Failure (unspecified)
  *
  * Returns:    !NULL                   pointer to new child thread
  *             NULL                    Failure (unspecified)
@@ -939,8 +965,11 @@ fork(proc_t parent_proc, __unused struct fork_args *uap, int32_t *retval)
  *             live with this being somewhat awkward.
  */
 thread_t
  *             live with this being somewhat awkward.
  */
 thread_t
-cloneproc(task_t parent_task, proc_t parent_proc, int inherit_memory)
+cloneproc(task_t parent_task, coalition_t parent_coalition, proc_t parent_proc, int inherit_memory, int memstat_internal)
 {
 {
+#if !CONFIG_MEMORYSTATUS
+#pragma unused(memstat_internal)
+#endif
        task_t child_task;
        proc_t child_proc;
        thread_t child_thread = NULL;
        task_t child_task;
        proc_t child_proc;
        thread_t child_thread = NULL;
@@ -950,7 +979,7 @@ cloneproc(task_t parent_task, proc_t parent_proc, int inherit_memory)
                goto bad;
        }
 
                goto bad;
        }
 
-       child_thread = fork_create_child(parent_task, child_proc, inherit_memory, (parent_task == TASK_NULL) ? FALSE : (parent_proc->p_flag & P_LP64));
+       child_thread = fork_create_child(parent_task, parent_coalition, child_proc, inherit_memory, (parent_task == TASK_NULL) ? FALSE : (parent_proc->p_flag & P_LP64));
 
        if (child_thread == NULL) {
                /*
 
        if (child_thread == NULL) {
                /*
@@ -970,6 +999,14 @@ cloneproc(task_t parent_task, proc_t parent_proc, int inherit_memory)
                OSBitAndAtomic(~((uint32_t)P_LP64), (UInt32 *)&child_proc->p_flag);
        }
 
                OSBitAndAtomic(~((uint32_t)P_LP64), (UInt32 *)&child_proc->p_flag);
        }
 
+#if CONFIG_MEMORYSTATUS
+       if (memstat_internal) {
+               proc_list_lock();
+               child_proc->p_memstat_state |= P_MEMSTAT_INTERNAL;
+               proc_list_unlock();
+       }
+#endif
+
        /* make child visible */
        pinsertchild(parent_proc, child_proc);
 
        /* make child visible */
        pinsertchild(parent_proc, child_proc);
 
@@ -1027,12 +1064,6 @@ forkproc_free(proc_t p)
        /* Need to undo the effects of the fdcopy(), if any */
        fdfree(p);
 
        /* Need to undo the effects of the fdcopy(), if any */
        fdfree(p);
 
-#if !CONFIG_EMBEDDED
-       if (p->p_legacy_behavior & PROC_LEGACY_BEHAVIOR_IOTHROTTLE) {
-               throttle_legacy_process_decr();
-       }
-#endif
-
        /*
         * Drop the reference on a text vnode pointer, if any
         * XXX This code is broken in forkproc(); see <rdar://4256419>;
        /*
         * Drop the reference on a text vnode pointer, if any
         * XXX This code is broken in forkproc(); see <rdar://4256419>;
@@ -1201,19 +1232,11 @@ retry:
         * Increase reference counts on shared objects.
         * The p_stats and p_sigacts substructs are set in vm_fork.
         */
         * Increase reference counts on shared objects.
         * The p_stats and p_sigacts substructs are set in vm_fork.
         */
-#if !CONFIG_EMBEDDED
        child_proc->p_flag = (parent_proc->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY | P_DISABLE_ASLR | P_DELAYIDLESLEEP));
        child_proc->p_flag = (parent_proc->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY | P_DISABLE_ASLR | P_DELAYIDLESLEEP));
-#else /*  !CONFIG_EMBEDDED */
-       child_proc->p_flag = (parent_proc->p_flag & (P_LP64 | P_TRANSLATED | P_AFFINITY | P_DISABLE_ASLR));
-#endif /* !CONFIG_EMBEDDED */
        if (parent_proc->p_flag & P_PROFIL)
                startprofclock(child_proc);
 
        if (parent_proc->p_flag & P_PROFIL)
                startprofclock(child_proc);
 
-#if !CONFIG_EMBEDDED
-       if (child_proc->p_legacy_behavior & PROC_LEGACY_BEHAVIOR_IOTHROTTLE) {
-               throttle_legacy_process_incr();
-       }
-#endif
+       child_proc->p_vfs_iopolicy = (parent_proc->p_vfs_iopolicy & (P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY));
 
        /*
         * Note that if the current thread has an assumed identity, this
 
        /*
         * Note that if the current thread has an assumed identity, this
@@ -1281,12 +1304,8 @@ retry:
 
        /* Intialize new process stats, including start time */
        /* <rdar://6640543> non-zeroed portion contains garbage AFAICT */
 
        /* Intialize new process stats, including start time */
        /* <rdar://6640543> non-zeroed portion contains garbage AFAICT */
-       bzero(&child_proc->p_stats->pstat_startzero,
-           (unsigned) ((caddr_t)&child_proc->p_stats->pstat_endzero -
-           (caddr_t)&child_proc->p_stats->pstat_startzero));
-       bzero(&child_proc->p_stats->user_p_prof, sizeof(struct user_uprof));
-       microtime(&child_proc->p_start);
-       child_proc->p_stats->p_start = child_proc->p_start;     /* for compat */
+       bzero(child_proc->p_stats, sizeof(*child_proc->p_stats));
+       microtime_with_abstime(&child_proc->p_start, &child_proc->p_stats->ps_start);
 
        if (parent_proc->p_sigacts != NULL)
                (void)memcpy(child_proc->p_sigacts,
 
        if (parent_proc->p_sigacts != NULL)
                (void)memcpy(child_proc->p_sigacts,
@@ -1305,9 +1324,10 @@ retry:
         * but indicate that the process is in (the creation) transition.
         */
        proc_signalstart(child_proc, 0);
         * but indicate that the process is in (the creation) transition.
         */
        proc_signalstart(child_proc, 0);
-       proc_transstart(child_proc, 0);
+       proc_transstart(child_proc, 0, 0);
+
+       child_proc->p_pcaction = 0;
 
 
-       child_proc->p_pcaction = (parent_proc->p_pcaction) & P_PCMAX;
        TAILQ_INIT(&child_proc->p_uthlist);
        TAILQ_INIT(&child_proc->p_aio_activeq);
        TAILQ_INIT(&child_proc->p_aio_doneq);
        TAILQ_INIT(&child_proc->p_uthlist);
        TAILQ_INIT(&child_proc->p_aio_activeq);
        TAILQ_INIT(&child_proc->p_aio_doneq);
@@ -1340,6 +1360,7 @@ retry:
                child_proc->p_lflag |= P_LREGISTER;
        }
        child_proc->p_dispatchqueue_offset = parent_proc->p_dispatchqueue_offset;
                child_proc->p_lflag |= P_LREGISTER;
        }
        child_proc->p_dispatchqueue_offset = parent_proc->p_dispatchqueue_offset;
+       child_proc->p_dispatchqueue_serialno_offset = parent_proc->p_dispatchqueue_serialno_offset;
 #if PSYNCH
        pth_proc_hashinit(child_proc);
 #endif /* PSYNCH */
 #if PSYNCH
        pth_proc_hashinit(child_proc);
 #endif /* PSYNCH */
@@ -1357,8 +1378,18 @@ retry:
        }
 #endif
 
        }
 #endif
 
-       /* Default to no tracking of dirty state */
-       child_proc->p_dirty = 0;
+#if CONFIG_MEMORYSTATUS
+       /* Memorystatus + jetsam init */
+       child_proc->p_memstat_state = 0;
+       child_proc->p_memstat_effectivepriority = JETSAM_PRIORITY_DEFAULT;
+       child_proc->p_memstat_requestedpriority = JETSAM_PRIORITY_DEFAULT;
+       child_proc->p_memstat_userdata = 0;
+#if CONFIG_FREEZE
+       child_proc->p_memstat_suspendedfootprint = 0;
+#endif
+       child_proc->p_memstat_dirty = 0;
+       child_proc->p_memstat_idledeadline = 0;
+#endif /* CONFIG_MEMORYSTATUS */
 
 bad:
        return(child_proc);
 
 bad:
        return(child_proc);
@@ -1433,7 +1464,6 @@ uthread_alloc(task_t task, thread_t thread, int noinherit)
 
        p = (proc_t) get_bsdtask_info(task);
        uth = (uthread_t)ut;
 
        p = (proc_t) get_bsdtask_info(task);
        uth = (uthread_t)ut;
-       uth->uu_kwe.kwe_uth = uth;
        uth->uu_thread = thread;
 
        /*
        uth->uu_thread = thread;
 
        /*
@@ -1507,7 +1537,7 @@ uthread_cleanup(task_t task, void *uthread, void * bsd_info)
        struct _select *sel;
        uthread_t uth = (uthread_t)uthread;
        proc_t p = (proc_t)bsd_info;
        struct _select *sel;
        uthread_t uth = (uthread_t)uthread;
        proc_t p = (proc_t)bsd_info;
-
+       void *pth_name;
 
        if (uth->uu_lowpri_window || uth->uu_throttle_info) {
                /*
 
        if (uth->uu_lowpri_window || uth->uu_throttle_info) {
                /*
@@ -1520,7 +1550,7 @@ uthread_cleanup(task_t task, void *uthread, void * bsd_info)
                 * Calling this routine will clean up any throttle info reference
                 * still inuse by the thread.
                 */
                 * Calling this routine will clean up any throttle info reference
                 * still inuse by the thread.
                 */
-               throttle_lowpri_io(FALSE);
+               throttle_lowpri_io(0);
        }
        /*
         * Per-thread audit state should never last beyond system
        }
        /*
         * Per-thread audit state should never last beyond system
@@ -1545,17 +1575,25 @@ uthread_cleanup(task_t task, void *uthread, void * bsd_info)
 
        if (uth->uu_allocsize && uth->uu_wqset){
                kfree(uth->uu_wqset, uth->uu_allocsize);
 
        if (uth->uu_allocsize && uth->uu_wqset){
                kfree(uth->uu_wqset, uth->uu_allocsize);
-               sel->count = 0;
                uth->uu_allocsize = 0;
                uth->uu_wqset = 0;
                uth->uu_allocsize = 0;
                uth->uu_wqset = 0;
-               sel->wql = 0;
        }
        }
-
-       if(uth->pth_name != NULL)
-       {
-               kfree(uth->pth_name, MAXTHREADNAMESIZE);
-               uth->pth_name = 0;
+       
+       /* 
+        * <rdar://17834538>
+        * Set pth_name to NULL before calling free().
+        * Previously there was a race condition in the 
+        * case this code was executing during a stackshot
+        * where the stackshot could try and copy pth_name
+        * after it had been freed and before if was marked
+        * as null.
+        */
+       if (uth->pth_name != NULL) {
+               pth_name = uth->pth_name;
+               uth->pth_name = NULL;
+               kfree(pth_name, MAXTHREADNAMESIZE);
        }
        }
+
        if ((task != kernel_task) && p) {
 
                if (((uth->uu_flag & UT_VFORK) == UT_VFORK) && (uth->uu_proc != PROC_NULL))  {
        if ((task != kernel_task) && p) {
 
                if (((uth->uu_flag & UT_VFORK) == UT_VFORK) && (uth->uu_proc != PROC_NULL))  {
@@ -1602,6 +1640,13 @@ uthread_cred_free(void *uthread)
 void
 uthread_zone_free(void *uthread)
 {
 void
 uthread_zone_free(void *uthread)
 {
+       uthread_t uth = (uthread_t)uthread;
+
+       if (uth->t_tombstone) {
+               kfree(uth->t_tombstone, sizeof(struct doc_tombstone));
+               uth->t_tombstone = NULL;
+       }
+
        /* and free the uthread itself */
        zfree(uthread_zone, uthread);
 }
        /* and free the uthread itself */
        zfree(uthread_zone, uthread);
 }