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