xref: /xnu-8796.121.2/libkern/os/log_encode.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2015-2020 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*c54f35caSApple OSS Distributions  * file.
12*c54f35caSApple OSS Distributions  *
13*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
14*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
19*c54f35caSApple OSS Distributions  * limitations under the License.
20*c54f35caSApple OSS Distributions  *
21*c54f35caSApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*c54f35caSApple OSS Distributions  */
23*c54f35caSApple OSS Distributions 
24*c54f35caSApple OSS Distributions #include <stdbool.h>
25*c54f35caSApple OSS Distributions #include <firehose/tracepoint_private.h>
26*c54f35caSApple OSS Distributions #include <kern/assert.h>
27*c54f35caSApple OSS Distributions #include <kern/counter.h>
28*c54f35caSApple OSS Distributions #include <kern/locks.h>
29*c54f35caSApple OSS Distributions #include <pexpert/pexpert.h>
30*c54f35caSApple OSS Distributions #include <sys/param.h>
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions #if __has_feature(ptrauth_calls)
33*c54f35caSApple OSS Distributions #include <mach/vm_param.h>
34*c54f35caSApple OSS Distributions #include <ptrauth.h>
35*c54f35caSApple OSS Distributions #endif /* __has_feature(ptrauth_calls) */
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions #include "log_encode.h"
38*c54f35caSApple OSS Distributions #include "log_mem.h"
39*c54f35caSApple OSS Distributions 
40*c54f35caSApple OSS Distributions #define LOG_FMT_MAX_PRECISION (1024)
41*c54f35caSApple OSS Distributions #define log_context_cursor(ctx) &(ctx)->ctx_hdr->hdr_data[(ctx)->ctx_content_off]
42*c54f35caSApple OSS Distributions 
43*c54f35caSApple OSS Distributions extern boolean_t doprnt_hide_pointers;
44*c54f35caSApple OSS Distributions 
45*c54f35caSApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_fmt_invalid_msgcount);
46*c54f35caSApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_fmt_max_args_msgcount);
47*c54f35caSApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_truncated_msgcount);
48*c54f35caSApple OSS Distributions 
49*c54f35caSApple OSS Distributions static bool
is_digit(char ch)50*c54f35caSApple OSS Distributions is_digit(char ch)
51*c54f35caSApple OSS Distributions {
52*c54f35caSApple OSS Distributions 	return (ch >= '0') && (ch <= '9');
53*c54f35caSApple OSS Distributions }
54*c54f35caSApple OSS Distributions 
55*c54f35caSApple OSS Distributions static bool
is_kernel_pointer(void * arg,size_t arg_len)56*c54f35caSApple OSS Distributions is_kernel_pointer(void *arg, size_t arg_len)
57*c54f35caSApple OSS Distributions {
58*c54f35caSApple OSS Distributions 	if (arg_len < sizeof(void *)) {
59*c54f35caSApple OSS Distributions 		return false;
60*c54f35caSApple OSS Distributions 	}
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions 	unsigned long long value = 0;
63*c54f35caSApple OSS Distributions 	assert(arg_len <= sizeof(value));
64*c54f35caSApple OSS Distributions 	(void) memcpy(&value, arg, arg_len);
65*c54f35caSApple OSS Distributions 
66*c54f35caSApple OSS Distributions #if __has_feature(ptrauth_calls)
67*c54f35caSApple OSS Distributions 	/**
68*c54f35caSApple OSS Distributions 	 * Strip out the pointer authentication code before
69*c54f35caSApple OSS Distributions 	 * checking whether the pointer is a kernel address.
70*c54f35caSApple OSS Distributions 	 */
71*c54f35caSApple OSS Distributions 	value = (unsigned long long)VM_KERNEL_STRIP_PTR(value);
72*c54f35caSApple OSS Distributions #endif /* __has_feature(ptrauth_calls) */
73*c54f35caSApple OSS Distributions 
74*c54f35caSApple OSS Distributions 	return value >= VM_MIN_KERNEL_AND_KEXT_ADDRESS && value <= VM_MAX_KERNEL_ADDRESS;
75*c54f35caSApple OSS Distributions }
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions static void
log_context_cursor_advance(os_log_context_t ctx,size_t amount)78*c54f35caSApple OSS Distributions log_context_cursor_advance(os_log_context_t ctx, size_t amount)
79*c54f35caSApple OSS Distributions {
80*c54f35caSApple OSS Distributions 	ctx->ctx_content_off += amount;
81*c54f35caSApple OSS Distributions 	assert(log_context_cursor(ctx) <= (ctx->ctx_buffer + ctx->ctx_buffer_sz));
82*c54f35caSApple OSS Distributions }
83*c54f35caSApple OSS Distributions 
84*c54f35caSApple OSS Distributions static bool
log_fits(os_log_context_t ctx,size_t data_size)85*c54f35caSApple OSS Distributions log_fits(os_log_context_t ctx, size_t data_size)
86*c54f35caSApple OSS Distributions {
87*c54f35caSApple OSS Distributions 	return (ctx->ctx_content_off + data_size) <= ctx->ctx_content_sz;
88*c54f35caSApple OSS Distributions }
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions static bool
log_fits_cmd(os_log_context_t ctx,size_t data_size)91*c54f35caSApple OSS Distributions log_fits_cmd(os_log_context_t ctx, size_t data_size)
92*c54f35caSApple OSS Distributions {
93*c54f35caSApple OSS Distributions 	return log_fits(ctx, sizeof(*ctx->ctx_hdr) + data_size);
94*c54f35caSApple OSS Distributions }
95*c54f35caSApple OSS Distributions 
96*c54f35caSApple OSS Distributions static void
log_range_update(os_log_fmt_range_t range,uint16_t offset,uint16_t length)97*c54f35caSApple OSS Distributions log_range_update(os_log_fmt_range_t range, uint16_t offset, uint16_t length)
98*c54f35caSApple OSS Distributions {
99*c54f35caSApple OSS Distributions 	range->offset = offset;
100*c54f35caSApple OSS Distributions 	/*
101*c54f35caSApple OSS Distributions 	 * Truncated flag may have already been set earlier, hence do not
102*c54f35caSApple OSS Distributions 	 * overwrite it blindly.
103*c54f35caSApple OSS Distributions 	 */
104*c54f35caSApple OSS Distributions 	if (length < range->length) {
105*c54f35caSApple OSS Distributions 		range->truncated = true;
106*c54f35caSApple OSS Distributions 	}
107*c54f35caSApple OSS Distributions 	range->length = length;
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions 
110*c54f35caSApple OSS Distributions /*
111*c54f35caSApple OSS Distributions  * Stores a command in the main section. The value itself is wrapped in
112*c54f35caSApple OSS Distributions  * the os_log_fmt_cmd_t struct.
113*c54f35caSApple OSS Distributions  */
114*c54f35caSApple OSS Distributions static void
log_add_cmd(os_log_context_t ctx,os_log_fmt_cmd_type_t type,uint8_t flags,void * arg,size_t arg_size)115*c54f35caSApple OSS Distributions log_add_cmd(os_log_context_t ctx, os_log_fmt_cmd_type_t type, uint8_t flags,
116*c54f35caSApple OSS Distributions     void *arg, size_t arg_size)
117*c54f35caSApple OSS Distributions {
118*c54f35caSApple OSS Distributions 	os_log_fmt_cmd_t cmd;
119*c54f35caSApple OSS Distributions 	const size_t cmd_sz = sizeof(*cmd) + arg_size;
120*c54f35caSApple OSS Distributions 
121*c54f35caSApple OSS Distributions 	assert(log_fits_cmd(ctx, cmd_sz));
122*c54f35caSApple OSS Distributions 	assert(arg_size <= UINT8_MAX);
123*c54f35caSApple OSS Distributions 
124*c54f35caSApple OSS Distributions 	cmd = (os_log_fmt_cmd_t)log_context_cursor(ctx);
125*c54f35caSApple OSS Distributions 	cmd->cmd_type = type;
126*c54f35caSApple OSS Distributions 	cmd->cmd_flags = flags;
127*c54f35caSApple OSS Distributions 	cmd->cmd_size = (uint8_t)arg_size;
128*c54f35caSApple OSS Distributions 	(void) memcpy(cmd->cmd_data, arg, cmd->cmd_size);
129*c54f35caSApple OSS Distributions 
130*c54f35caSApple OSS Distributions 	assert(cmd_sz == sizeof(*cmd) + cmd->cmd_size);
131*c54f35caSApple OSS Distributions 	log_context_cursor_advance(ctx, cmd_sz);
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions 
134*c54f35caSApple OSS Distributions /*
135*c54f35caSApple OSS Distributions  * Collect details about argument which needs to be stored in the pubdata
136*c54f35caSApple OSS Distributions  * section.
137*c54f35caSApple OSS Distributions  */
138*c54f35caSApple OSS Distributions static void
log_collect_public_range_data(os_log_context_t ctx,os_log_fmt_range_t range,void * arg)139*c54f35caSApple OSS Distributions log_collect_public_range_data(os_log_context_t ctx, os_log_fmt_range_t range, void *arg)
140*c54f35caSApple OSS Distributions {
141*c54f35caSApple OSS Distributions 	ctx->ctx_pubdata[ctx->ctx_pubdata_cnt++] = (char *)arg;
142*c54f35caSApple OSS Distributions 	ctx->ctx_pubdata_sz += range->length;
143*c54f35caSApple OSS Distributions }
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions static void
log_add_range_data(os_log_context_t ctx,os_log_fmt_range_t range,void * arg)146*c54f35caSApple OSS Distributions log_add_range_data(os_log_context_t ctx, os_log_fmt_range_t range, void *arg)
147*c54f35caSApple OSS Distributions {
148*c54f35caSApple OSS Distributions 	assert(log_fits(ctx, range->length));
149*c54f35caSApple OSS Distributions 	(void) memcpy(log_context_cursor(ctx), arg, range->length);
150*c54f35caSApple OSS Distributions 	log_context_cursor_advance(ctx, range->length);
151*c54f35caSApple OSS Distributions }
152*c54f35caSApple OSS Distributions 
153*c54f35caSApple OSS Distributions static struct os_log_fmt_range_s
log_create_range(os_log_context_t ctx,size_t arg_len)154*c54f35caSApple OSS Distributions log_create_range(os_log_context_t ctx, size_t arg_len)
155*c54f35caSApple OSS Distributions {
156*c54f35caSApple OSS Distributions 	const size_t final_arg_len = MIN(arg_len, UINT16_MAX);
157*c54f35caSApple OSS Distributions 
158*c54f35caSApple OSS Distributions 	return (struct os_log_fmt_range_s) {
159*c54f35caSApple OSS Distributions 		       .offset = ctx->ctx_pubdata_sz,
160*c54f35caSApple OSS Distributions 		       .length = (uint16_t)final_arg_len,
161*c54f35caSApple OSS Distributions 		       .truncated = (final_arg_len < arg_len)
162*c54f35caSApple OSS Distributions 	};
163*c54f35caSApple OSS Distributions }
164*c54f35caSApple OSS Distributions 
165*c54f35caSApple OSS Distributions static int
log_add_range_arg(os_log_context_t ctx,os_log_fmt_cmd_type_t type,os_log_fmt_cmd_flags_t flags,void * arg,size_t arg_len)166*c54f35caSApple OSS Distributions log_add_range_arg(os_log_context_t ctx, os_log_fmt_cmd_type_t type, os_log_fmt_cmd_flags_t flags,
167*c54f35caSApple OSS Distributions     void *arg, size_t arg_len)
168*c54f35caSApple OSS Distributions {
169*c54f35caSApple OSS Distributions 	struct os_log_fmt_range_s range;
170*c54f35caSApple OSS Distributions 
171*c54f35caSApple OSS Distributions 	if (!log_fits_cmd(ctx, sizeof(range))) {
172*c54f35caSApple OSS Distributions 		return ENOMEM;
173*c54f35caSApple OSS Distributions 	}
174*c54f35caSApple OSS Distributions 
175*c54f35caSApple OSS Distributions 	range = log_create_range(ctx, arg_len);
176*c54f35caSApple OSS Distributions 
177*c54f35caSApple OSS Distributions 	if (flags == OSLF_CMD_FLAG_PUBLIC) {
178*c54f35caSApple OSS Distributions 		if (ctx->ctx_pubdata_cnt == OS_LOG_MAX_PUB_ARGS) {
179*c54f35caSApple OSS Distributions 			return ENOMEM;
180*c54f35caSApple OSS Distributions 		}
181*c54f35caSApple OSS Distributions 		assert(ctx->ctx_pubdata_cnt < OS_LOG_MAX_PUB_ARGS);
182*c54f35caSApple OSS Distributions 		log_collect_public_range_data(ctx, &range, arg);
183*c54f35caSApple OSS Distributions 	}
184*c54f35caSApple OSS Distributions 	log_add_cmd(ctx, type, flags, &range, sizeof(range));
185*c54f35caSApple OSS Distributions 	ctx->ctx_hdr->hdr_cmd_cnt++;
186*c54f35caSApple OSS Distributions 
187*c54f35caSApple OSS Distributions 	return 0;
188*c54f35caSApple OSS Distributions }
189*c54f35caSApple OSS Distributions 
190*c54f35caSApple OSS Distributions /*
191*c54f35caSApple OSS Distributions  * Adds a scalar argument value to the main section.
192*c54f35caSApple OSS Distributions  */
193*c54f35caSApple OSS Distributions static int
log_add_arg(os_log_context_t ctx,os_log_fmt_cmd_type_t type,void * arg,size_t arg_len)194*c54f35caSApple OSS Distributions log_add_arg(os_log_context_t ctx, os_log_fmt_cmd_type_t type, void *arg, size_t arg_len)
195*c54f35caSApple OSS Distributions {
196*c54f35caSApple OSS Distributions 	assert(type == OSLF_CMD_TYPE_COUNT || type == OSLF_CMD_TYPE_SCALAR);
197*c54f35caSApple OSS Distributions 	assert(arg_len < UINT16_MAX);
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions 	if (log_fits_cmd(ctx, arg_len)) {
200*c54f35caSApple OSS Distributions 		log_add_cmd(ctx, type, OSLF_CMD_FLAG_PUBLIC, arg, arg_len);
201*c54f35caSApple OSS Distributions 		ctx->ctx_hdr->hdr_cmd_cnt++;
202*c54f35caSApple OSS Distributions 		return 0;
203*c54f35caSApple OSS Distributions 	}
204*c54f35caSApple OSS Distributions 
205*c54f35caSApple OSS Distributions 	return ENOMEM;
206*c54f35caSApple OSS Distributions }
207*c54f35caSApple OSS Distributions 
208*c54f35caSApple OSS Distributions static void
log_encode_public_data(os_log_context_t ctx)209*c54f35caSApple OSS Distributions log_encode_public_data(os_log_context_t ctx)
210*c54f35caSApple OSS Distributions {
211*c54f35caSApple OSS Distributions 	const uint16_t orig_content_off = ctx->ctx_content_off;
212*c54f35caSApple OSS Distributions 	os_log_fmt_hdr_t const hdr = ctx->ctx_hdr;
213*c54f35caSApple OSS Distributions 	os_log_fmt_cmd_t cmd = (os_log_fmt_cmd_t)hdr->hdr_data;
214*c54f35caSApple OSS Distributions 
215*c54f35caSApple OSS Distributions 	assert(ctx->ctx_pubdata_cnt <= hdr->hdr_cmd_cnt);
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions 	for (int i = 0, pub_i = 0; i < hdr->hdr_cmd_cnt; i++, cmd = (os_log_fmt_cmd_t)(cmd->cmd_data + cmd->cmd_size)) {
218*c54f35caSApple OSS Distributions 		if (cmd->cmd_type != OSLF_CMD_TYPE_STRING) {
219*c54f35caSApple OSS Distributions 			continue;
220*c54f35caSApple OSS Distributions 		}
221*c54f35caSApple OSS Distributions 
222*c54f35caSApple OSS Distributions 		os_log_fmt_range_t const range __attribute__((aligned(8))) = (os_log_fmt_range_t)&cmd->cmd_data;
223*c54f35caSApple OSS Distributions 
224*c54f35caSApple OSS Distributions 		// Fix offset and length of the argument data in the hdr.
225*c54f35caSApple OSS Distributions 		log_range_update(range, ctx->ctx_content_off - orig_content_off,
226*c54f35caSApple OSS Distributions 		    MIN(range->length, ctx->ctx_content_sz - ctx->ctx_content_off));
227*c54f35caSApple OSS Distributions 
228*c54f35caSApple OSS Distributions 		if (range->truncated) {
229*c54f35caSApple OSS Distributions 			ctx->ctx_truncated = true;
230*c54f35caSApple OSS Distributions 		}
231*c54f35caSApple OSS Distributions 
232*c54f35caSApple OSS Distributions 		assert(pub_i < ctx->ctx_pubdata_cnt);
233*c54f35caSApple OSS Distributions 		log_add_range_data(ctx, range, ctx->ctx_pubdata[pub_i++]);
234*c54f35caSApple OSS Distributions 	}
235*c54f35caSApple OSS Distributions }
236*c54f35caSApple OSS Distributions 
237*c54f35caSApple OSS Distributions static bool
log_expand(os_log_context_t ctx,size_t new_size)238*c54f35caSApple OSS Distributions log_expand(os_log_context_t ctx, size_t new_size)
239*c54f35caSApple OSS Distributions {
240*c54f35caSApple OSS Distributions 	assert(new_size > ctx->ctx_buffer_sz);
241*c54f35caSApple OSS Distributions 
242*c54f35caSApple OSS Distributions 	if (!oslog_is_safe()) {
243*c54f35caSApple OSS Distributions 		return false;
244*c54f35caSApple OSS Distributions 	}
245*c54f35caSApple OSS Distributions 
246*c54f35caSApple OSS Distributions 	size_t final_size = new_size;
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	void *buf = logmem_alloc_locked(ctx->ctx_logmem, &final_size);
249*c54f35caSApple OSS Distributions 	if (!buf) {
250*c54f35caSApple OSS Distributions 		return false;
251*c54f35caSApple OSS Distributions 	}
252*c54f35caSApple OSS Distributions 	assert(final_size >= new_size);
253*c54f35caSApple OSS Distributions 
254*c54f35caSApple OSS Distributions 	// address length header + already stored data
255*c54f35caSApple OSS Distributions 	const size_t hdr_size = (uint8_t *)ctx->ctx_hdr - ctx->ctx_buffer;
256*c54f35caSApple OSS Distributions 	const size_t copy_size = hdr_size + sizeof(*ctx->ctx_hdr) + ctx->ctx_content_sz;
257*c54f35caSApple OSS Distributions 	assert(copy_size <= new_size);
258*c54f35caSApple OSS Distributions 	(void) memcpy(buf, ctx->ctx_buffer, copy_size);
259*c54f35caSApple OSS Distributions 
260*c54f35caSApple OSS Distributions 	if (ctx->ctx_allocated) {
261*c54f35caSApple OSS Distributions 		logmem_free_locked(ctx->ctx_logmem, ctx->ctx_buffer, ctx->ctx_buffer_sz);
262*c54f35caSApple OSS Distributions 	}
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions 	ctx->ctx_buffer = buf;
265*c54f35caSApple OSS Distributions 	ctx->ctx_buffer_sz = final_size;
266*c54f35caSApple OSS Distributions 	ctx->ctx_content_sz = (uint16_t)(ctx->ctx_buffer_sz - hdr_size - sizeof(*ctx->ctx_hdr));
267*c54f35caSApple OSS Distributions 	ctx->ctx_hdr = (os_log_fmt_hdr_t)&ctx->ctx_buffer[hdr_size];
268*c54f35caSApple OSS Distributions 	ctx->ctx_allocated = true;
269*c54f35caSApple OSS Distributions 
270*c54f35caSApple OSS Distributions 	return true;
271*c54f35caSApple OSS Distributions }
272*c54f35caSApple OSS Distributions 
273*c54f35caSApple OSS Distributions static int
log_encode_fmt_arg(void * arg,size_t arg_len,os_log_fmt_cmd_type_t type,os_log_context_t ctx)274*c54f35caSApple OSS Distributions log_encode_fmt_arg(void *arg, size_t arg_len, os_log_fmt_cmd_type_t type, os_log_context_t ctx)
275*c54f35caSApple OSS Distributions {
276*c54f35caSApple OSS Distributions 	int rc = 0;
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions 	switch (type) {
279*c54f35caSApple OSS Distributions 	case OSLF_CMD_TYPE_COUNT:
280*c54f35caSApple OSS Distributions 	case OSLF_CMD_TYPE_SCALAR:
281*c54f35caSApple OSS Distributions 		// Scrub kernel pointers.
282*c54f35caSApple OSS Distributions 		if (doprnt_hide_pointers && is_kernel_pointer(arg, arg_len)) {
283*c54f35caSApple OSS Distributions 			rc = log_add_range_arg(ctx, type, OSLF_CMD_FLAG_PRIVATE, NULL, 0);
284*c54f35caSApple OSS Distributions 			ctx->ctx_hdr->hdr_flags |= OSLF_HDR_FLAG_HAS_PRIVATE;
285*c54f35caSApple OSS Distributions 		} else {
286*c54f35caSApple OSS Distributions 			rc = log_add_arg(ctx, type, arg, arg_len);
287*c54f35caSApple OSS Distributions 		}
288*c54f35caSApple OSS Distributions 		break;
289*c54f35caSApple OSS Distributions 	case OSLF_CMD_TYPE_STRING:
290*c54f35caSApple OSS Distributions 		rc = log_add_range_arg(ctx, type, OSLF_CMD_FLAG_PUBLIC, arg, arg_len);
291*c54f35caSApple OSS Distributions 		ctx->ctx_hdr->hdr_flags |= OSLF_HDR_FLAG_HAS_NON_SCALAR;
292*c54f35caSApple OSS Distributions 		break;
293*c54f35caSApple OSS Distributions 	default:
294*c54f35caSApple OSS Distributions 		panic("Unsupported log value type");
295*c54f35caSApple OSS Distributions 	}
296*c54f35caSApple OSS Distributions 
297*c54f35caSApple OSS Distributions 	return rc;
298*c54f35caSApple OSS Distributions }
299*c54f35caSApple OSS Distributions 
300*c54f35caSApple OSS Distributions static int
log_encode_fmt(os_log_context_t ctx,const char * format,va_list args)301*c54f35caSApple OSS Distributions log_encode_fmt(os_log_context_t ctx, const char *format, va_list args)
302*c54f35caSApple OSS Distributions {
303*c54f35caSApple OSS Distributions 	const char *position = format;
304*c54f35caSApple OSS Distributions 
305*c54f35caSApple OSS Distributions 	while ((position = strchr(position, '%'))) {
306*c54f35caSApple OSS Distributions 		position++; // Look at character(s) after %.
307*c54f35caSApple OSS Distributions 
308*c54f35caSApple OSS Distributions 		int type = OST_INT;
309*c54f35caSApple OSS Distributions 		boolean_t has_precision = false;
310*c54f35caSApple OSS Distributions 		int precision = 0;
311*c54f35caSApple OSS Distributions 
312*c54f35caSApple OSS Distributions 		for (bool done = false; !done; position++) {
313*c54f35caSApple OSS Distributions 			union os_log_fmt_types_u value;
314*c54f35caSApple OSS Distributions 			size_t str_length;
315*c54f35caSApple OSS Distributions 			int err = 0;
316*c54f35caSApple OSS Distributions 
317*c54f35caSApple OSS Distributions 			switch (position[0]) {
318*c54f35caSApple OSS Distributions 			case '%':
319*c54f35caSApple OSS Distributions 				// %% prints % character
320*c54f35caSApple OSS Distributions 				done = true;
321*c54f35caSApple OSS Distributions 				break;
322*c54f35caSApple OSS Distributions 
323*c54f35caSApple OSS Distributions 			/* type of types or other */
324*c54f35caSApple OSS Distributions 			case 'l': // longer
325*c54f35caSApple OSS Distributions 				type++;
326*c54f35caSApple OSS Distributions 				break;
327*c54f35caSApple OSS Distributions 
328*c54f35caSApple OSS Distributions 			case 'h': // shorter
329*c54f35caSApple OSS Distributions 				type--;
330*c54f35caSApple OSS Distributions 				break;
331*c54f35caSApple OSS Distributions 
332*c54f35caSApple OSS Distributions 			case 'z':
333*c54f35caSApple OSS Distributions 				type = OST_SIZE;
334*c54f35caSApple OSS Distributions 				break;
335*c54f35caSApple OSS Distributions 
336*c54f35caSApple OSS Distributions 			case 'j':
337*c54f35caSApple OSS Distributions 				type = OST_INTMAX;
338*c54f35caSApple OSS Distributions 				break;
339*c54f35caSApple OSS Distributions 
340*c54f35caSApple OSS Distributions 			case 't':
341*c54f35caSApple OSS Distributions 				type = OST_PTRDIFF;
342*c54f35caSApple OSS Distributions 				break;
343*c54f35caSApple OSS Distributions 
344*c54f35caSApple OSS Distributions 			case 'q':
345*c54f35caSApple OSS Distributions 				type = OST_LONGLONG;
346*c54f35caSApple OSS Distributions 				break;
347*c54f35caSApple OSS Distributions 
348*c54f35caSApple OSS Distributions 			case '.': // precision
349*c54f35caSApple OSS Distributions 				if (position[1] == '*') {
350*c54f35caSApple OSS Distributions 					// Dynamic precision, argument holds actual value.
351*c54f35caSApple OSS Distributions 					precision = va_arg(args, int);
352*c54f35caSApple OSS Distributions 					position++;
353*c54f35caSApple OSS Distributions 				} else {
354*c54f35caSApple OSS Distributions 					// Static precision, the value follows in the fmt.
355*c54f35caSApple OSS Distributions 					precision = 0;
356*c54f35caSApple OSS Distributions 					while (is_digit(position[1])) {
357*c54f35caSApple OSS Distributions 						if (precision < LOG_FMT_MAX_PRECISION) {
358*c54f35caSApple OSS Distributions 							precision = 10 * precision + (position[1] - '0');
359*c54f35caSApple OSS Distributions 						}
360*c54f35caSApple OSS Distributions 						position++;
361*c54f35caSApple OSS Distributions 					}
362*c54f35caSApple OSS Distributions 					precision = MIN(precision, LOG_FMT_MAX_PRECISION);
363*c54f35caSApple OSS Distributions 				}
364*c54f35caSApple OSS Distributions 				err = log_encode_fmt_arg(&precision, sizeof(precision), OSLF_CMD_TYPE_COUNT, ctx);
365*c54f35caSApple OSS Distributions 				// A negative precision is treated as though it were missing.
366*c54f35caSApple OSS Distributions 				if (precision >= 0) {
367*c54f35caSApple OSS Distributions 					has_precision = true;
368*c54f35caSApple OSS Distributions 				}
369*c54f35caSApple OSS Distributions 				break;
370*c54f35caSApple OSS Distributions 
371*c54f35caSApple OSS Distributions 			case '-': // left-align
372*c54f35caSApple OSS Distributions 			case '+': // force sign
373*c54f35caSApple OSS Distributions 			case ' ': // prefix non-negative with space
374*c54f35caSApple OSS Distributions 			case '#': // alternate
375*c54f35caSApple OSS Distributions 			case '\'': // group by thousands
376*c54f35caSApple OSS Distributions 				break;
377*c54f35caSApple OSS Distributions 
378*c54f35caSApple OSS Distributions 			/* fixed types */
379*c54f35caSApple OSS Distributions 			case 'd': // integer
380*c54f35caSApple OSS Distributions 			case 'i': // integer
381*c54f35caSApple OSS Distributions 			case 'o': // octal
382*c54f35caSApple OSS Distributions 			case 'u': // unsigned
383*c54f35caSApple OSS Distributions 			case 'x': // hex
384*c54f35caSApple OSS Distributions 			case 'X': // upper-hex
385*c54f35caSApple OSS Distributions 				switch (type) {
386*c54f35caSApple OSS Distributions 				case OST_CHAR:
387*c54f35caSApple OSS Distributions 					value.ch = (char) va_arg(args, int);
388*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.ch, sizeof(value.ch), OSLF_CMD_TYPE_SCALAR, ctx);
389*c54f35caSApple OSS Distributions 					break;
390*c54f35caSApple OSS Distributions 
391*c54f35caSApple OSS Distributions 				case OST_SHORT:
392*c54f35caSApple OSS Distributions 					value.s = (short) va_arg(args, int);
393*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.s, sizeof(value.s), OSLF_CMD_TYPE_SCALAR, ctx);
394*c54f35caSApple OSS Distributions 					break;
395*c54f35caSApple OSS Distributions 
396*c54f35caSApple OSS Distributions 				case OST_INT:
397*c54f35caSApple OSS Distributions 					value.i = va_arg(args, int);
398*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.i, sizeof(value.i), OSLF_CMD_TYPE_SCALAR, ctx);
399*c54f35caSApple OSS Distributions 					break;
400*c54f35caSApple OSS Distributions 
401*c54f35caSApple OSS Distributions 				case OST_LONG:
402*c54f35caSApple OSS Distributions 					value.l = va_arg(args, long);
403*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.l, sizeof(value.l), OSLF_CMD_TYPE_SCALAR, ctx);
404*c54f35caSApple OSS Distributions 					break;
405*c54f35caSApple OSS Distributions 
406*c54f35caSApple OSS Distributions 				case OST_LONGLONG:
407*c54f35caSApple OSS Distributions 					value.ll = va_arg(args, long long);
408*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.ll, sizeof(value.ll), OSLF_CMD_TYPE_SCALAR, ctx);
409*c54f35caSApple OSS Distributions 					break;
410*c54f35caSApple OSS Distributions 
411*c54f35caSApple OSS Distributions 				case OST_SIZE:
412*c54f35caSApple OSS Distributions 					value.z = va_arg(args, size_t);
413*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.z, sizeof(value.z), OSLF_CMD_TYPE_SCALAR, ctx);
414*c54f35caSApple OSS Distributions 					break;
415*c54f35caSApple OSS Distributions 
416*c54f35caSApple OSS Distributions 				case OST_INTMAX:
417*c54f35caSApple OSS Distributions 					value.im = va_arg(args, intmax_t);
418*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.im, sizeof(value.im), OSLF_CMD_TYPE_SCALAR, ctx);
419*c54f35caSApple OSS Distributions 					break;
420*c54f35caSApple OSS Distributions 
421*c54f35caSApple OSS Distributions 				case OST_PTRDIFF:
422*c54f35caSApple OSS Distributions 					value.pd = va_arg(args, ptrdiff_t);
423*c54f35caSApple OSS Distributions 					err = log_encode_fmt_arg(&value.pd, sizeof(value.pd), OSLF_CMD_TYPE_SCALAR, ctx);
424*c54f35caSApple OSS Distributions 					break;
425*c54f35caSApple OSS Distributions 
426*c54f35caSApple OSS Distributions 				default:
427*c54f35caSApple OSS Distributions 					return EINVAL;
428*c54f35caSApple OSS Distributions 				}
429*c54f35caSApple OSS Distributions 				done = true;
430*c54f35caSApple OSS Distributions 				break;
431*c54f35caSApple OSS Distributions 
432*c54f35caSApple OSS Distributions 			case 'p': // pointer
433*c54f35caSApple OSS Distributions 				value.p = va_arg(args, void *);
434*c54f35caSApple OSS Distributions 				err = log_encode_fmt_arg(&value.p, sizeof(value.p), OSLF_CMD_TYPE_SCALAR, ctx);
435*c54f35caSApple OSS Distributions 				done = true;
436*c54f35caSApple OSS Distributions 				break;
437*c54f35caSApple OSS Distributions 
438*c54f35caSApple OSS Distributions 			case 'c': // char
439*c54f35caSApple OSS Distributions 				value.ch = (char) va_arg(args, int);
440*c54f35caSApple OSS Distributions 				err = log_encode_fmt_arg(&value.ch, sizeof(value.ch), OSLF_CMD_TYPE_SCALAR, ctx);
441*c54f35caSApple OSS Distributions 				done = true;
442*c54f35caSApple OSS Distributions 				break;
443*c54f35caSApple OSS Distributions 
444*c54f35caSApple OSS Distributions 			case 's': // string
445*c54f35caSApple OSS Distributions 				value.pch = va_arg(args, char *);
446*c54f35caSApple OSS Distributions 				if (!value.pch) {
447*c54f35caSApple OSS Distributions 					str_length = 0;
448*c54f35caSApple OSS Distributions 				} else if (has_precision) {
449*c54f35caSApple OSS Distributions 					assert(precision >= 0);
450*c54f35caSApple OSS Distributions 					str_length = strnlen(value.pch, precision);
451*c54f35caSApple OSS Distributions 				} else {
452*c54f35caSApple OSS Distributions 					str_length = strlen(value.pch) + 1;
453*c54f35caSApple OSS Distributions 				}
454*c54f35caSApple OSS Distributions 				err = log_encode_fmt_arg(value.pch, str_length, OSLF_CMD_TYPE_STRING, ctx);
455*c54f35caSApple OSS Distributions 				done = true;
456*c54f35caSApple OSS Distributions 				break;
457*c54f35caSApple OSS Distributions 
458*c54f35caSApple OSS Distributions 			case 'm':
459*c54f35caSApple OSS Distributions 				value.i = 0; // Does %m make sense in the kernel?
460*c54f35caSApple OSS Distributions 				err = log_encode_fmt_arg(&value.i, sizeof(value.i), OSLF_CMD_TYPE_SCALAR, ctx);
461*c54f35caSApple OSS Distributions 				done = true;
462*c54f35caSApple OSS Distributions 				break;
463*c54f35caSApple OSS Distributions 
464*c54f35caSApple OSS Distributions 			case '0' ... '9':
465*c54f35caSApple OSS Distributions 				// Skipping field width, libtrace takes care of it.
466*c54f35caSApple OSS Distributions 				break;
467*c54f35caSApple OSS Distributions 
468*c54f35caSApple OSS Distributions 			default:
469*c54f35caSApple OSS Distributions 				return EINVAL;
470*c54f35caSApple OSS Distributions 			}
471*c54f35caSApple OSS Distributions 
472*c54f35caSApple OSS Distributions 			if (slowpath(err)) {
473*c54f35caSApple OSS Distributions 				return err;
474*c54f35caSApple OSS Distributions 			}
475*c54f35caSApple OSS Distributions 		}
476*c54f35caSApple OSS Distributions 	}
477*c54f35caSApple OSS Distributions 
478*c54f35caSApple OSS Distributions 	return 0;
479*c54f35caSApple OSS Distributions }
480*c54f35caSApple OSS Distributions 
481*c54f35caSApple OSS Distributions static inline size_t
write_address_location(uint8_t buf[static sizeof (uint64_t)],uintptr_t loc,size_t loc_size)482*c54f35caSApple OSS Distributions write_address_location(uint8_t buf[static sizeof(uint64_t)], uintptr_t loc, size_t loc_size)
483*c54f35caSApple OSS Distributions {
484*c54f35caSApple OSS Distributions 	if (loc_size == sizeof(uintptr_t)) {
485*c54f35caSApple OSS Distributions #if __LP64__
486*c54f35caSApple OSS Distributions 		loc_size = 6; // 48 bits are enough
487*c54f35caSApple OSS Distributions #endif
488*c54f35caSApple OSS Distributions 		memcpy(buf, (uintptr_t[]){ loc }, loc_size);
489*c54f35caSApple OSS Distributions 	} else {
490*c54f35caSApple OSS Distributions 		assert(loc_size == sizeof(uint32_t));
491*c54f35caSApple OSS Distributions 		memcpy(buf, (uint32_t[]){ (uint32_t)loc }, loc_size);
492*c54f35caSApple OSS Distributions 	}
493*c54f35caSApple OSS Distributions 	return loc_size;
494*c54f35caSApple OSS Distributions }
495*c54f35caSApple OSS Distributions 
496*c54f35caSApple OSS Distributions static void
os_log_encode_location(os_log_context_t ctx,uintptr_t loc,size_t loc_size)497*c54f35caSApple OSS Distributions os_log_encode_location(os_log_context_t ctx, uintptr_t loc, size_t loc_size)
498*c54f35caSApple OSS Distributions {
499*c54f35caSApple OSS Distributions 	const size_t hdr_size = write_address_location(ctx->ctx_buffer, loc, loc_size);
500*c54f35caSApple OSS Distributions 	ctx->ctx_hdr = (os_log_fmt_hdr_t)&ctx->ctx_buffer[hdr_size];
501*c54f35caSApple OSS Distributions 	bzero(ctx->ctx_hdr, sizeof(*ctx->ctx_hdr));
502*c54f35caSApple OSS Distributions 	ctx->ctx_content_sz = (uint16_t)(ctx->ctx_buffer_sz - hdr_size - sizeof(*ctx->ctx_hdr));
503*c54f35caSApple OSS Distributions }
504*c54f35caSApple OSS Distributions 
505*c54f35caSApple OSS Distributions /*
506*c54f35caSApple OSS Distributions  * Encodes argument (meta)data into a format consumed by libtrace. Stores
507*c54f35caSApple OSS Distributions  * metadada for all arguments first. Metadata also include scalar argument
508*c54f35caSApple OSS Distributions  * values. Second step saves data which are encoded separately from respective
509*c54f35caSApple OSS Distributions  * metadata (like strings).
510*c54f35caSApple OSS Distributions  */
511*c54f35caSApple OSS Distributions bool
os_log_context_encode(os_log_context_t ctx,const char * fmt,va_list args,uintptr_t loc,size_t loc_size)512*c54f35caSApple OSS Distributions os_log_context_encode(os_log_context_t ctx, const char *fmt, va_list args,
513*c54f35caSApple OSS Distributions     uintptr_t loc, size_t loc_size)
514*c54f35caSApple OSS Distributions {
515*c54f35caSApple OSS Distributions 	os_log_encode_location(ctx, loc, loc_size);
516*c54f35caSApple OSS Distributions 
517*c54f35caSApple OSS Distributions 	va_list args_copy;
518*c54f35caSApple OSS Distributions 	va_copy(args_copy, args);
519*c54f35caSApple OSS Distributions 
520*c54f35caSApple OSS Distributions 	int rc = log_encode_fmt(ctx, fmt, args);
521*c54f35caSApple OSS Distributions 
522*c54f35caSApple OSS Distributions 	va_end(args_copy);
523*c54f35caSApple OSS Distributions 
524*c54f35caSApple OSS Distributions 	switch (rc) {
525*c54f35caSApple OSS Distributions 	case EINVAL:
526*c54f35caSApple OSS Distributions 		// Bogus/Unsupported fmt string
527*c54f35caSApple OSS Distributions 		counter_inc(&oslog_p_fmt_invalid_msgcount);
528*c54f35caSApple OSS Distributions 		return false;
529*c54f35caSApple OSS Distributions 	case ENOMEM:
530*c54f35caSApple OSS Distributions 		/*
531*c54f35caSApple OSS Distributions 		 * The fmt contains unreasonable number of arguments (> 32) and
532*c54f35caSApple OSS Distributions 		 * we ran out of space. We could call log_expand()
533*c54f35caSApple OSS Distributions 		 * here and retry. However, using such formatting strings rather
534*c54f35caSApple OSS Distributions 		 * seem like a misuse of the logging system, hence error.
535*c54f35caSApple OSS Distributions 		 */
536*c54f35caSApple OSS Distributions 		counter_inc(&oslog_p_fmt_max_args_msgcount);
537*c54f35caSApple OSS Distributions 		return false;
538*c54f35caSApple OSS Distributions 	case 0:
539*c54f35caSApple OSS Distributions 		break;
540*c54f35caSApple OSS Distributions 	default:
541*c54f35caSApple OSS Distributions 		panic("unhandled return value");
542*c54f35caSApple OSS Distributions 	}
543*c54f35caSApple OSS Distributions 
544*c54f35caSApple OSS Distributions 	if (ctx->ctx_pubdata_sz == 0) {
545*c54f35caSApple OSS Distributions 		goto finish;
546*c54f35caSApple OSS Distributions 	}
547*c54f35caSApple OSS Distributions 
548*c54f35caSApple OSS Distributions 	/*
549*c54f35caSApple OSS Distributions 	 * Logmem may not have been set up yet when logging very early during
550*c54f35caSApple OSS Distributions 	 * the boot. Be sure to check its state.
551*c54f35caSApple OSS Distributions 	 */
552*c54f35caSApple OSS Distributions 	if (!log_fits(ctx, ctx->ctx_pubdata_sz) && logmem_ready(ctx->ctx_logmem)) {
553*c54f35caSApple OSS Distributions 		size_t space_needed = log_context_cursor(ctx) + ctx->ctx_pubdata_sz - ctx->ctx_buffer;
554*c54f35caSApple OSS Distributions 		space_needed = MIN(space_needed, logmem_max_size(ctx->ctx_logmem));
555*c54f35caSApple OSS Distributions 		(void) log_expand(ctx, space_needed);
556*c54f35caSApple OSS Distributions 	}
557*c54f35caSApple OSS Distributions 
558*c54f35caSApple OSS Distributions 	log_encode_public_data(ctx);
559*c54f35caSApple OSS Distributions 
560*c54f35caSApple OSS Distributions 	if (ctx->ctx_truncated) {
561*c54f35caSApple OSS Distributions 		counter_inc(&oslog_p_truncated_msgcount);
562*c54f35caSApple OSS Distributions 	}
563*c54f35caSApple OSS Distributions finish:
564*c54f35caSApple OSS Distributions 	ctx->ctx_content_sz = (uint16_t)(log_context_cursor(ctx) - ctx->ctx_buffer);
565*c54f35caSApple OSS Distributions 	ctx->ctx_content_off = 0;
566*c54f35caSApple OSS Distributions 	return true;
567*c54f35caSApple OSS Distributions }
568*c54f35caSApple OSS Distributions 
569*c54f35caSApple OSS Distributions void
os_log_context_init(os_log_context_t ctx,logmem_t * logmem,uint8_t * buffer,size_t buffer_sz)570*c54f35caSApple OSS Distributions os_log_context_init(os_log_context_t ctx, logmem_t *logmem, uint8_t *buffer, size_t buffer_sz)
571*c54f35caSApple OSS Distributions {
572*c54f35caSApple OSS Distributions 	assert(logmem);
573*c54f35caSApple OSS Distributions 	assert(buffer);
574*c54f35caSApple OSS Distributions 	assert(buffer_sz > 0);
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions 	bzero(ctx, sizeof(*ctx));
577*c54f35caSApple OSS Distributions 	ctx->ctx_logmem = logmem;
578*c54f35caSApple OSS Distributions 	ctx->ctx_buffer = buffer;
579*c54f35caSApple OSS Distributions 	ctx->ctx_buffer_sz = buffer_sz;
580*c54f35caSApple OSS Distributions }
581*c54f35caSApple OSS Distributions 
582*c54f35caSApple OSS Distributions void
os_log_context_free(os_log_context_t ctx)583*c54f35caSApple OSS Distributions os_log_context_free(os_log_context_t ctx)
584*c54f35caSApple OSS Distributions {
585*c54f35caSApple OSS Distributions 	if (ctx->ctx_allocated) {
586*c54f35caSApple OSS Distributions 		logmem_free_locked(ctx->ctx_logmem, ctx->ctx_buffer, ctx->ctx_buffer_sz);
587*c54f35caSApple OSS Distributions 	}
588*c54f35caSApple OSS Distributions }
589