2 * Copyright (c) 2015 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@
30 #import <Foundation/Foundation.h>
32 #import "KCDBasicTypeDescription.h"
33 #import "KCDStructTypeDescription.h"
34 #import "KCDEmbeddedBufferDescription.h"
36 #define LIB_KCD_ERR_DOMAIN @"KCDataError"
38 #define GEN_ERROR(code, msg) gen_error(__LINE__, code, @msg)
39 #define GEN_ERRORF(code, msg, ...) gen_error(__LINE__, code, [NSString stringWithFormat:@msg, __VA_ARGS__])
41 #define MAX_KCDATATYPE_BUFFER_SIZE 2048
42 extern struct kcdata_type_definition * kcdata_get_typedescription(unsigned type_id, uint8_t * buffer, uint32_t buffer_size);
44 BOOL setKCDataTypeForID(uint32_t newTypeID, KCDataType *newTypeObj);
47 gen_error(int line, NSInteger code, NSString *message)
49 return [NSError errorWithDomain:LIB_KCD_ERR_DOMAIN
51 userInfo:@{ @"line": @(line), @"message": message }];
55 mergedict(NSMutableDictionary * container, NSDictionary * object, NSError ** error)
57 for (id key in object) {
58 id existing = container[key];
61 if ([existing isKindOfClass:[NSMutableArray class]] && [new isKindOfClass:[ NSArray class ]]) {
62 [existing addObjectsFromArray:new];
65 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated key: %@", key);
70 [container setValue:new forKey:key];
77 * @function getTypeFromTypeDef
80 * Build a KCDataType from a type definition.
83 * A pointer to kcdata_type_definition_t that specifies the type fields and has subtype definitions
84 * in the memory immediately following the type_definition.
86 * @return KCDataType * type object which can be used to parse data into dictionaries.
87 * This may return nil if it finds the data to be invalid.
90 * This routine tries to decode the typeDef structure and create either a basic type (KCDBasicTypeDescription)
93 static KCDataType * getTypeFromTypeDef(struct kcdata_type_definition * typeDef);
96 getTypeFromTypeDef(struct kcdata_type_definition * typeDef)
98 if (typeDef == NULL) {
101 NSString * kct_name = [NSString stringWithFormat:@"%s", typeDef->kct_name];
102 if (typeDef->kct_num_elements == 1 && !(typeDef->kct_elements[0].kcs_flags & KCS_SUBTYPE_FLAGS_STRUCT)) {
103 KCDBasicTypeDescription * retval = [[KCDBasicTypeDescription alloc] initWithKCTypeDesc:&typeDef->kct_elements[0]];
106 KCDStructTypeDescription * retval =
107 [[KCDStructTypeDescription alloc] initWithType:typeDef->kct_type_identifier withName:kct_name];
108 /* need to do work here to get the array of elements setup here */
109 KCDBasicTypeDescription * curField = nil;
110 for (unsigned int i = 0; i < typeDef->kct_num_elements; i++) {
111 curField = [[KCDBasicTypeDescription alloc] initWithKCTypeDesc:&typeDef->kct_elements[i]];
112 [retval addFieldBasicType:curField];
113 if (typeDef->kct_elements[i].kcs_flags & KCS_SUBTYPE_FLAGS_MERGE) {
114 [retval setFlagsRequestedMerge];
122 static dispatch_once_t onceToken;
123 static NSMutableDictionary * knownTypes = nil;
126 getKCDataTypeForID(uint32_t typeID)
128 dispatch_once(&onceToken, ^{
130 knownTypes = [[NSMutableDictionary alloc] init];
134 NSNumber * type = [NSNumber numberWithUnsignedInt:typeID];
135 if (!knownTypes[type]) {
136 if (typeID == KCDATA_TYPE_NESTED_KCDATA) {
137 knownTypes[type] = [[KCDEmbeddedBufferDescription alloc] init];
138 return knownTypes[type];
140 /* code to query system for type information */
141 uint8_t buffer[MAX_KCDATATYPE_BUFFER_SIZE];
142 struct kcdata_type_definition * sys_def = kcdata_get_typedescription(typeID, buffer, MAX_KCDATATYPE_BUFFER_SIZE);
143 if (sys_def == NULL) {
144 knownTypes[type] = [[KCDBasicTypeDescription alloc] createDefaultForType:typeID];
146 knownTypes[type] = getTypeFromTypeDef(sys_def);
149 assert(knownTypes[type] != nil);
150 return knownTypes[type];
154 setKCDataTypeForID(uint32_t newTypeID, KCDataType *newTypeObj) {
155 if (newTypeObj == NULL || newTypeID == 0) {
159 dispatch_once(&onceToken, ^{
161 knownTypes = [[NSMutableDictionary alloc] init];
165 NSNumber * type = [NSNumber numberWithUnsignedInt:newTypeID];
167 if (!knownTypes[type]) {
168 knownTypes[type] = newTypeObj;
177 KCDataTypeNameForID(uint32_t typeID)
179 NSString * retval = [NSString stringWithFormat:@"%u", typeID];
180 KCDataType * t = getKCDataTypeForID(typeID);
182 if (![[t name] containsString:@"Type_"]) {
188 NSMutableDictionary *
189 parseKCDataArray(kcdata_iter_t iter, NSError **error)
191 if (!kcdata_iter_array_valid(iter)) {
193 *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid array");
197 uint32_t typeID = kcdata_iter_array_elem_type(iter);
198 uint32_t count = kcdata_iter_array_elem_count(iter);
199 uint32_t size = kcdata_iter_array_elem_size(iter);
200 uint8_t * buffer = (uint8_t *)kcdata_iter_payload(iter);
201 KCDataType * datatype = getKCDataTypeForID(typeID);
202 NSMutableDictionary * retval = [[NSMutableDictionary alloc] initWithCapacity:1];
203 NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:count];
204 retval[[datatype name]] = arr;
205 NSDictionary * tmpdict = NULL;
206 for (uint32_t i = 0; i < count; i++) {
207 tmpdict = [datatype parseData:(void *)&buffer[i * size] ofLength:size];
210 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse array element. type=0x%x", (int)typeID);
213 if ([datatype shouldMergeData]) {
214 assert([tmpdict count] == 1);
215 [arr addObject: [tmpdict allValues][0]];
217 [arr addObject:tmpdict];
223 NSMutableDictionary *
224 parseKCDataContainer(kcdata_iter_t *iter_p, NSError **error)
226 kcdata_iter_t iter = *iter_p;
228 if (!kcdata_iter_container_valid(iter)) {
230 *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid container");
233 uint64_t containerID = kcdata_iter_container_id(iter);
235 /* setup collection object for sub containers */
236 NSMutableDictionary * sub_containers = [[NSMutableDictionary alloc] init];
237 NSMutableDictionary * retval = [[NSMutableDictionary alloc] init];
238 NSMutableDictionary * container = [[NSMutableDictionary alloc] init];
240 KCDataType * tmptype;
244 NSDictionary * tmpdict;
245 BOOL found_end = FALSE;
246 retval[KCDataTypeNameForID(kcdata_iter_container_type(iter))] = container;
248 iter = kcdata_iter_next(iter);
250 KCDATA_ITER_FOREACH(iter)
252 _t = kcdata_iter_type(iter);
253 _d = kcdata_iter_payload(iter);
254 if (_t == KCDATA_TYPE_CONTAINER_END) {
255 if (kcdata_iter_container_id(iter) != containerID) {
257 *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "container marker mismatch");
264 if (_t == KCDATA_TYPE_ARRAY) {
265 tmpdict = parseKCDataArray(iter, error);
269 ok = mergedict(container, tmpdict, error);
276 if (_t == KCDATA_TYPE_CONTAINER_BEGIN) {
277 NSString * subcontainerID = [NSString stringWithFormat:@"%llu", kcdata_iter_container_id(iter)];
278 tmpdict = parseKCDataContainer(&iter, error);
281 assert([tmpdict count] == 1);
282 for (NSString * k in [tmpdict keyEnumerator]) {
283 if (sub_containers[k] == nil) {
284 sub_containers[k] = [[NSMutableDictionary alloc] init];
286 if (sub_containers[k][subcontainerID] != nil) {
288 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated container id: %@", subcontainerID);
291 sub_containers[k][subcontainerID] = tmpdict[k];
296 tmptype = getKCDataTypeForID(_t);
297 tmpdict = [tmptype parseData:_d ofLength:kcdata_iter_size(iter)];
300 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_t);
303 if (![tmptype shouldMergeData]) {
304 tmpdict = @{[tmptype name] : tmpdict};
306 ok = mergedict(container, tmpdict, error);
313 *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "missing container end");
317 ok = mergedict(container, sub_containers, error);
326 parseKCDataBuffer(void * dataBuffer, uint32_t size, NSError ** error)
328 if (dataBuffer == NULL) {
330 *error = GEN_ERROR(KERN_INVALID_ARGUMENT, "buffer is null");
334 uint32_t _type = (size >= sizeof(uint32_t)) ? *(uint32_t*)dataBuffer : 0;
337 void * _datap = NULL;
338 KCDataType * kcd_type = NULL;
339 NSString * rootKey = NULL;
340 uint32_t rootType = _type;
343 /* validate begin tag and get root key */
345 case KCDATA_BUFFER_BEGIN_CRASHINFO:
346 rootKey = @"kcdata_crashinfo";
348 case KCDATA_BUFFER_BEGIN_STACKSHOT:
349 rootKey = @"kcdata_stackshot";
351 case KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT:
352 rootKey = @"kcdata_delta_stackshot";
354 case KCDATA_BUFFER_BEGIN_OS_REASON:
355 rootKey = @"kcdata_reason";
357 case KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG:
358 rootKey = @"xnupost_testconfig";
362 *error = GEN_ERROR(KERN_INVALID_VALUE, "invalid magic number");
367 assert(rootKey != NULL);
369 kcdata_iter_t iter = kcdata_iter(dataBuffer, size);
371 if (!kcdata_iter_valid(iter)) {
373 *error = GEN_ERROR(KERN_INVALID_OBJECT, "initial item is invalid");
378 NSMutableDictionary * rootObject = [NSMutableDictionary dictionary];
379 NSDictionary * retval = [NSMutableDictionary dictionaryWithObject:rootObject forKey:rootKey];
381 /* iterate over each kcdata item */
382 KCDATA_ITER_FOREACH(iter)
384 _type = kcdata_iter_type(iter);
385 _size = kcdata_iter_size(iter);
386 _flags = kcdata_iter_flags(iter);
387 _datap = kcdata_iter_payload(iter);
389 if (_type == rootType)
392 if (_type == KCDATA_TYPE_ARRAY) {
393 NSDictionary * dict = parseKCDataArray(iter, error);
397 ok = mergedict(rootObject, dict, error);
404 if (_type == KCDATA_TYPE_CONTAINER_BEGIN) {
405 NSString * containerID = [NSString stringWithFormat:@"%llu", kcdata_iter_container_id(iter)];
406 NSMutableDictionary *container = parseKCDataContainer(&iter, error);
409 assert([container count] == 1);
410 for (NSString * k in [container keyEnumerator]) {
411 if (rootObject[k] == nil) {
412 rootObject[k] = [[NSMutableDictionary alloc] init];
414 if (rootObject[k][containerID] != nil) {
416 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "repeated container id: %@", containerID);
419 rootObject[k][containerID] = container[k];
424 if (_type == KCDATA_TYPE_TYPEDEFINTION) {
425 KCDataType *new_type = getTypeFromTypeDef((struct kcdata_type_definition *)_datap);
426 if (new_type != NULL) {
427 setKCDataTypeForID([new_type typeID], new_type);
428 kcd_type = getKCDataTypeForID(_type);
429 NSDictionary * tmpdict = [kcd_type parseData:_datap ofLength:_size];
432 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_type);
435 NSString *k = [NSString stringWithFormat:@"typedef[%@]", [new_type name]];
436 rootObject[k] = tmpdict;
439 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "Failed to parse type definition for type %u", _type);
445 kcd_type = getKCDataTypeForID(_type);
446 NSDictionary * tmpdict = [kcd_type parseData:_datap ofLength:_size];
449 *error = GEN_ERRORF(KERN_INVALID_OBJECT, "failed to parse. type=0x%x", (int)_type);
452 if (![kcd_type shouldMergeData]) {
453 tmpdict = @{[kcd_type name] : tmpdict};
455 ok = mergedict(rootObject, tmpdict, error);
460 if (KCDATA_ITER_FOREACH_FAILED(iter)) {
463 *error = GEN_ERROR(KERN_INVALID_OBJECT, "invalid item or missing buffer end marker");