2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
31 #include <kern/host.h>
32 #include <kern/thread.h>
33 #include <kern/task.h>
34 #include <kern/extmod_statistics.h>
35 #include <libkern/OSAtomic.h>
37 #include <uuid/uuid.h>
40 * This code module adds statistics to track when
41 * a userspace task is modified by another userspace
42 * task. This can facilitate triage of crashes
43 * and abberant behavior, which are not expected
44 * to occur when the program is running in its
45 * qualified environment.
47 * We assume the target task has a lifecycle lock
48 * that will prevent it from exiting
49 * (task_reference/task_reference_internal), which
50 * should be called either explicitly, or implicitly
51 * via MIG glue code (convert_port_to_task).
53 * Host-wide statistics don't asssume any locks are
54 * held, and use atomic operations.
56 * If we can detect that the kernel proper is
57 * performing these operations, don't count
58 * it as an external modification. Some of the
59 * external modification routines are called
60 * by the kernel during thread setup, in which
61 * case we rename the userspace entrypoint called
62 * by the MIG demuxer to have a "_from_user" suffix.
65 /* externs for BSD kernel */
66 extern void fslog_extmod_msgtracer(void *, void *);
70 extmod_statistics_log(task_t current_task
, task_t target
);
73 extmod_statistics_incr_task_for_pid(task_t target
)
75 task_t ctask
= current_task();
77 if ((ctask
== kernel_task
) || (target
== TASK_NULL
))
80 if (target
!= ctask
) {
81 ctask
->extmod_statistics
.task_for_pid_caller_count
++;
82 target
->extmod_statistics
.task_for_pid_count
++;
83 OSIncrementAtomic64(&host_extmod_statistics
.task_for_pid_count
);
88 extmod_statistics_incr_thread_set_state(thread_t target
)
90 task_t ctask
= current_task();
93 if ((ctask
== kernel_task
) || (target
== THREAD_NULL
))
96 ttask
= get_threadtask(target
);
98 if (ttask
== TASK_NULL
)
101 if (ttask
!= ctask
) {
102 ctask
->extmod_statistics
.thread_set_state_caller_count
++;
103 ttask
->extmod_statistics
.thread_set_state_count
++;
104 OSIncrementAtomic64(&host_extmod_statistics
.thread_set_state_count
);
109 extmod_statistics_incr_thread_create(task_t target
)
111 task_t ctask
= current_task();
113 if ((ctask
== kernel_task
) || (target
== TASK_NULL
))
116 if (target
!= ctask
) {
117 ctask
->extmod_statistics
.thread_creation_caller_count
++;
118 target
->extmod_statistics
.thread_creation_count
++;
119 OSIncrementAtomic64(&host_extmod_statistics
.thread_creation_count
);
121 extmod_statistics_log(ctask
, target
);
126 extmod_statistics_log(task_t current_task
, task_t target
)
131 c_proc
= get_bsdtask_info(current_task
);
132 t_proc
= get_bsdtask_info(target
);
133 if (c_proc
&& t_proc
) {
134 fslog_extmod_msgtracer(c_proc
, t_proc
);