xref: /xnu-12377.41.6/san/coverage/kcov_ksancov_data.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
26  */
27 #ifndef _KCOV_KSANCOV_DATA_H_
28 #define _KCOV_KSANCOV_DATA_H_
29 
30 #if KERNEL_PRIVATE
31 
32 #if CONFIG_KSANCOV
33 
34 /*
35  * Supported coverage modes.
36  */
37 typedef enum {
38 	KS_MODE_NONE,
39 	KS_MODE_TRACE,
40 	KS_MODE_COUNTERS,
41 	KS_MODE_STKSIZE,
42 	KS_MODE_MAX
43 } ksancov_mode_t;
44 
45 /*
46  * A header that is always present in every ksancov mode shared memory structure.
47  */
48 typedef struct ksancov_header {
49 	uint32_t         kh_magic;
50 	_Atomic uint32_t kh_enabled;
51 } ksancov_header_t;
52 
53 /*
54  * TRACE mode data structure.
55  */
56 
57 /*
58  * All trace based tools share this structure.
59  */
60 typedef struct ksancov_trace {
61 	ksancov_header_t kt_hdr;         /* header (must be always first) */
62 	uint32_t         kt_maxent;      /* Maximum entries in this shared buffer. */
63 	_Atomic uint32_t kt_head;        /* Pointer to the first unused element. */
64 	uint64_t         kt_entries[];   /* Trace entries in this buffer. */
65 } ksancov_trace_t;
66 
67 /* PC tracing only records PCs */
68 typedef uintptr_t ksancov_trace_pc_ent_t;
69 
70 /* STKSIZE tracing records PCs and stack size. */
71 typedef struct ksancov_trace_stksize_entry {
72 	uintptr_t pc;                      /* PC */
73 	uint32_t  stksize;                 /* associated stack size */
74 } ksancov_trace_stksize_ent_t;
75 
76 /*
77  * COUNTERS mode data structure.
78  */
79 typedef struct ksancov_counters {
80 	ksancov_header_t kc_hdr;
81 	uint32_t         kc_nedges;       /* total number of edges */
82 	uint8_t          kc_hits[];       /* hits on each edge (8bit saturating) */
83 } ksancov_counters_t;
84 
85 /*
86  * Edge to PC mapping.
87  */
88 typedef struct ksancov_edgemap {
89 	uint32_t  ke_magic;
90 	uint32_t  ke_nedges;
91 	uintptr_t ke_addrs[];             /* address of each edge relative to 'offset' */
92 } ksancov_edgemap_t;
93 
94 /*
95  * Supported comparison logging modes.
96  */
97 typedef enum {
98 	KS_CMPS_MODE_NONE,
99 	KS_CMPS_MODE_TRACE,
100 	KS_CMPS_MODE_TRACE_FUNC,
101 	KS_CMPS_MODE_MAX
102 } ksancov_cmps_mode_t;
103 
104 #define KSANCOV_CMPS_TRACE_FUNC_MAX_BYTES 512
105 
106 /* CMPS TRACE mode tracks comparison values */
107 typedef struct __attribute__((__packed__)) ksancov_cmps_trace_entry {
108 	uint64_t pc;
109 	uint32_t type;
110 	uint16_t len1_func;
111 	uint16_t len2_func;
112 	union {
113 		uint64_t args[2];              /* cmp instruction arguments */
114 		uint8_t args_func[0];          /* cmp function arguments (variadic) */
115 	};
116 } ksancov_cmps_trace_ent_t;
117 
118 /* Calculate the total space that a ksancov_cmps_trace_ent_t tracing a function takes */
119 static inline size_t
ksancov_cmps_trace_func_space(size_t len1_func,size_t len2_func)120 ksancov_cmps_trace_func_space(size_t len1_func, size_t len2_func)
121 {
122 	static_assert(sizeof(ksancov_cmps_trace_ent_t) == sizeof(uint64_t) * 3 + sizeof(uint32_t) + sizeof(uint16_t) * 2, "ksancov_cmps_trace_ent_t invalid size");
123 
124 	size_t size = sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint16_t) * 2; // header
125 	size += len1_func + len2_func;
126 	size_t rem = size % sizeof(ksancov_cmps_trace_ent_t);
127 	if (rem == 0) {
128 		return size;
129 	}
130 	return size + sizeof(ksancov_cmps_trace_ent_t) - rem;
131 }
132 
133 static inline uint8_t *
ksancov_cmps_trace_func_arg1(ksancov_cmps_trace_ent_t * entry)134 ksancov_cmps_trace_func_arg1(ksancov_cmps_trace_ent_t *entry)
135 {
136 	return entry->args_func;
137 }
138 
139 static inline uint8_t *
ksancov_cmps_trace_func_arg2(ksancov_cmps_trace_ent_t * entry)140 ksancov_cmps_trace_func_arg2(ksancov_cmps_trace_ent_t *entry)
141 {
142 	uint8_t* func_args = entry->args_func;
143 	return &func_args[entry->len1_func];
144 }
145 
146 #define KSANCOV_SERIALIZED_TESTCASE_BYTES 16777216 // 16MiB
147 #define KSANCOV_SERIALIZED_TESTCASES_MAX_COUNT 100
148 
149 typedef struct ksancov_serialized_testcase {
150 	uint32_t size;
151 	uint8_t  buffer[KSANCOV_SERIALIZED_TESTCASE_BYTES];
152 } ksancov_serialized_testcase_t;
153 
154 /*
155  * Store the latest executed testcases in kernel to dump on panic.
156  */
157 typedef struct ksancov_serialized_testcases {
158 	uint32_t head;         /* current head of the circular buffer */
159 	uint32_t inner_index;  /* current inner index in the head testcase (e.g. current call being executed) */
160 	ksancov_serialized_testcase_t list[];  /* testcases circular buffer */
161 } ksancov_serialized_testcases_t;
162 
163 /*
164  * Represents state of a ksancov device when userspace asks for coverage data recording.
165  */
166 
167 struct ksancov_dev {
168 	ksancov_mode_t mode;
169 
170 	union {
171 		ksancov_header_t       *hdr;
172 		ksancov_trace_t        *trace;
173 		ksancov_counters_t     *counters;
174 	};
175 	size_t sz;     /* size of allocated trace/counters buffer */
176 
177 	size_t maxpcs;
178 
179 	ksancov_cmps_mode_t cmps_mode;
180 
181 	union {
182 		ksancov_header_t       *cmps_hdr;
183 		ksancov_trace_t        *cmps_trace;
184 	};
185 	size_t cmps_sz;     /* size of allocated cmps trace buffer */
186 
187 	ksancov_serialized_testcases_t* testcases;
188 	uint32_t testcases_count;     /* number of testcases in the buffer (less or equal than max count) */
189 
190 	thread_t thread;
191 	dev_t dev;
192 	lck_mtx_t lock;
193 };
194 typedef struct ksancov_dev * ksancov_dev_t;
195 
196 
197 #endif /* CONFIG_KSANCOV */
198 
199 #endif /* KERNEL_PRIVATE */
200 
201 #endif /* _KCOV_KSANCOV_DATA_H_ */
202