-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOReport_decompile.m
533 lines (420 loc) · 19.5 KB
/
IOReport_decompile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* libIOReport.dylib decompiled by hand by dehydratedpotato, 2023
*/
#include <Foundation/Foundation.h>
#include <objc/runtime.h>
#include "IOReport_decompile.h"
#include "IOReportTypes.h"
#include "IOKernelReportStructs.h"
/* Macros for hub userclient methods
*/
#define kIOReportUserClientOpen 0
#define kIOReportUserClientConfigureInterests 2
#define kIOReportUserClientUpdateKernelBuffer 3
/* Missing constants for dictionary stuff */
#define kDriverIdKey CFSTR("DriverID")
#define kDrivernameKey CFSTR("DriverName")
#define kRawElementskey CFSTR("RawElements")
#define kStatenamesKey CFSTR("StateNames")
#define kIOReportRawElementChunkSize 64
/* CFRuntime extern syms
* IOReportSubscription uses this stuff.
*/
typedef struct __CFRuntimeClass {
CFIndex version;
const char *className;
void (*init)(CFTypeRef cf);
CFTypeRef (*copy)(CFAllocatorRef allocator, CFTypeRef cf);
void (*finalize)(CFTypeRef cf);
Boolean (*equal)(CFTypeRef cf1, CFTypeRef cf2);
CFHashCode (*hash)(CFTypeRef cf);
CFStringRef (*copyFormattingDesc)(CFTypeRef cf, CFDictionaryRef formatOptions);
CFStringRef (*copyDebugDesc)(CFTypeRef cf);
void (*reclaim)(CFTypeRef cf);
} CFRuntimeClass;
typedef struct __CFRuntimeBase {
uintptr_t _cfisa;
uint8_t _cfinfo[4];
#if __LP64__
uint32_t _rc;
#endif
} CFRuntimeBase;
CFTypeID __CFGenericTypeID(void *cf);
const CFRuntimeClass* _CFRuntimeGetClassWithTypeID(CFTypeID typeID);
CFTypeID _CFRuntimeRegisterClass(const CFRuntimeClass* const cls);
CFTypeRef _CFRuntimeCreateInstance(CFAllocatorRef allocator,
CFTypeID typeID,
CFIndex extraBytes,
unsigned char* category);
/* subscription decompile
*/
static CFRuntimeClass _IOReportSubscriptionClass = { 0, "IOReportSubscription" };
struct IOReportSubscription {
CFRuntimeBase base; /* cfruntime base */
io_connect_t connection; /* userclient connection */
uint64_t dwordPtr; /* dword pointer, seems always empty */
mach_vm_address_t addr; /* addr to data from userclient*/
mach_vm_size_t addrSize; /* data size */
};
struct _IOReportRawElement {
int64_t value;
uint64_t reserved;
IOReportFormat type;
};
struct _IOReportSimpleInteger {
int64_t value;
uint64_t reserved_a;
IOReportFormat type;
uint8_t align;
uint16_t reserved_b;
uint64_t reserved_c;
int64_t qword;
};
// MARK: Channel Functions
/* Filter the IOReg for properties with ioreport legends. We reuse this for copying channels in group,
* then pass NULL to use for copying everything. This is different than libIOReport which reads all and then filters.
*/
CFMutableDictionaryRef _copy_chann(NSString* group) {
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
CFMutableArrayRef channels = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
io_iterator_t iter;
kern_return_t kr;
io_registry_entry_t entry;
kr = IORegistryCreateIterator(kIOMainPortDefault, kIOServicePlane, kIORegistryIterateRecursively, &iter);
if (kr != kIOReturnSuccess) return NULL;
while ((entry = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
char name[56];
uint64_t entid;
CFArrayRef legend = (CFArrayRef) IORegistryEntryCreateCFProperty(entry, CFSTR(kIOReportLegendKey), kCFAllocatorDefault, 0);
if (legend == NULL) continue;
for (int i = 0; i < CFArrayGetCount(legend); i++) {
CFDictionaryRef key = CFArrayGetValueAtIndex(legend, i);
if (CFDictionaryContainsValue(key, (CFStringRef) group ) || group == NULL) {
CFArrayRef chann_array = CFDictionaryGetValue(key, CFSTR(kIOReportLegendChannelsKey));
IORegistryEntryGetName(entry, name);
IORegistryEntryGetRegistryEntryID(entry, &entid);
NSString* dname = [[NSString alloc] initWithFormat:@"%s <id: 0x%.2llx>", name, entid];
for (int ii = 0; ii < CFArrayGetCount(chann_array); ii++) {
CFMutableDictionaryRef subbdict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
CFDictionaryAddValue(subbdict, kDriverIdKey, CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, &entid));
// TODO: uncomment this. for some reason it's value is mutated after the loops are done and turns it into an array???
CFDictionaryAddValue(subbdict, kDrivernameKey, (CFStringRef) dname);
CFDictionaryAddValue(subbdict, CFSTR(kIOReportLegendInfoKey), CFDictionaryGetValue(key, CFSTR(kIOReportLegendInfoKey)));
CFDictionaryAddValue(subbdict, CFSTR(kIOReportLegendGroupNameKey), CFDictionaryGetValue(key, CFSTR(kIOReportLegendGroupNameKey)));
CFDictionaryAddValue(subbdict, CFSTR(kIOReportLegendSubGroupNameKey), CFDictionaryGetValue(key, CFSTR(kIOReportLegendSubGroupNameKey)));
CFDictionaryAddValue(subbdict, CFSTR(kIOReportLegendChannelsKey), CFArrayGetValueAtIndex(chann_array, ii));
CFArrayAppendValue(channels, subbdict);
}
}
}
}
IOObjectRelease(iter);
if (CFArrayGetCount(channels) != 0)
CFDictionarySetValue(dict, CFSTR(kIOReportLegendChannelsKey), channels);
CFDictionarySetValue(dict, CFSTR("QueryOpts"), CFSTR("0"));
return dict;
}
/* Ignoring a,b,c params for now because they just add uneeded complexity to the returned format
*/
CFMutableDictionaryRef IOReportCopyChannelsInGroup(NSString* group, NSString* subgroup, uint64_t a, uint64_t b, uint64_t c) {
return _copy_chann(group);
}
CFMutableDictionaryRef IOReportCopyAllChannels(uint64_t a, uint64_t b) {
return _copy_chann(NULL);
}
// MARK: Subscription Functions
/* This helps determine what exactly we want to subscribe to from the hub userclient.
* Originally this may have been a code block for some reason.
*/
IOReportInterestList* _create_interlist(CFArrayRef channels, int count) {
IOReportInterestList * interestList = malloc(count * 0x18 + 8);
interestList->ninterests = count;
for (int i = 0; i < count; i++) {
uint64_t channel_id = 0;
uint64_t provider_id = 0;
uint64_t channel_type_raw = 0;
CFDictionaryRef chann = (CFDictionaryRef) CFArrayGetValueAtIndex(channels, i);
CFArrayRef legend_chann = (CFArrayRef) CFDictionaryGetValue(chann, CFSTR(kIOReportLegendChannelsKey));
CFNumberRef legend_channel_id = CFArrayGetValueAtIndex(legend_chann, kIOReportChannelIDIdx);
CFNumberGetValue(legend_channel_id, kCFNumberLongType, &channel_id);
CFNumberRef legend_channel_type = CFArrayGetValueAtIndex(legend_chann, kIOReportChannelTypeIdx);
CFNumberGetValue(legend_channel_type, kCFNumberLongType, &channel_type_raw);
IOReportChannelType channel_type = *(IOReportChannelType*)&channel_type_raw;
IOReportChannel channel = {
.channel_id = channel_id,
.channel_type = channel_type
};
CFNumberRef driver_id = CFDictionaryGetValue(chann, kDriverIdKey);
CFNumberGetValue(driver_id, kCFNumberLongLongType, &provider_id);
IOReportInterest interest = {
.provider_id = provider_id,
.channel = channel
};
interestList->interests[i] = interest;
}
return interestList;
}
IOReportSubscriptionRef IOReportCreateSubscription(void* a,
CFMutableDictionaryRef desiredChannels,
CFMutableDictionaryRef* subbedChannels,
uint64_t channel_id,
CFTypeRef b) {
const uint32_t count = IOReportGetChannelCount(desiredChannels);
if (count <= 0) return NULL;
CFTypeID iorepTypeId;
IOReportSubscriptionRef iorepSubscription = NULL;
kern_return_t kr;
io_iterator_t iter;
io_service_t service = 0;
io_connect_t connection = 0;
const uint32_t input = count * 0x18 + 8;
uint32_t output = 1;
iorepTypeId = _CFRuntimeRegisterClass(&_IOReportSubscriptionClass);
iorepSubscription = (IOReportSubscriptionRef)_CFRuntimeCreateInstance(a, iorepTypeId, 0x20, 0);
// Prep the inerest list
CFArrayRef channs = (CFArrayRef) CFDictionaryGetValue(desiredChannels, CFSTR(kIOReportLegendChannelsKey));
IOReportInterestList* interestList = _create_interlist(channs, count);
kr = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("IOReportHub"), &iter);
if (kr != KERN_SUCCESS) {
//NSLog(@"Could not match IOReportHub, failed with %s", mach_error_string(kr));
return NULL;
}
while ((service = IOIteratorNext(iter)) != IO_OBJECT_NULL) {
kr = IOServiceOpen(service, mach_task_self(), 0, &connection);
if (kr != KERN_SUCCESS) {
//NSLog(@"Could not open IOReportHub, failed with %s", mach_error_string(kr));
goto exit;
}
break;
}
IOObjectRelease(iter);
// open the userclient for communication
kr = IOConnectCallScalarMethod(connection, kIOReportUserClientOpen, 0, 0, 0, 0);
if (kr != KERN_SUCCESS) {
//NSLog(@"kIOReportUserClientOpen failed with %s", mach_error_string(kr));
goto exit;
}
IOObjectRelease(service);
iorepSubscription->connection = connection;
// config interests so the hub knows what data to provide
kr = IOConnectCallMethod(iorepSubscription->connection,
kIOReportUserClientConfigureInterests,
NULL, 0,
interestList,
input,
&iorepSubscription->dwordPtr,
&output,
NULL, 0);
if (kr != KERN_SUCCESS) {
//NSLog(@"kIOReportUserClientConfigureInterests failed with %s", mach_error_string(kr));
free(interestList);
goto exit;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
// map memory from the user client so we may get our datas later
kr = IOConnectMapMemory(iorepSubscription->connection,
iorepSubscription->dwordPtr,
mach_task_self(),
&iorepSubscription->addr,
&iorepSubscription->addrSize, 1);
if (kr != KERN_SUCCESS) {
//NSLog(@"IOConnectMapMemory) failed with %s", mach_error_string(kr));
IOConnectUnmapMemory(connection,
iorepSubscription->dwordPtr,
mach_task_self(),
iorepSubscription->addr);
free(interestList);
goto exit;
}
#pragma clang diagnostic pop
exit:
IOServiceClose(service);
return iorepSubscription;
}
// MARK: Sampling Functions
// TODO: - Find State Names for State Formatted channels
CFDictionaryRef IOReportCreateSamples(IOReportSubscriptionRef iorsub, CFMutableDictionaryRef subbedChannels, CFTypeRef a) {
if (iorsub == NULL && subbedChannels == NULL) return NULL;
if (iorsub->connection == 0) return NULL;
/* get datas
*/
kern_return_t kr = IOConnectCallMethod(iorsub->connection, kIOReportUserClientUpdateKernelBuffer,
&iorsub->dwordPtr, 1, 0, 0, 0, 0, 0, 0);
if (kr != KERN_SUCCESS) {
//NSLog(@"_updateKernelBuffer() failed failed with %s", mach_error_string(kr));
return NULL;
}
// IOServiceClose(iorsub->connection);
CFMutableDictionaryRef channels = CFDictionaryCreateMutableCopy(0, 2, subbedChannels);
CFMutableArrayRef array = (CFMutableArrayRef) CFDictionaryGetValue(channels, CFSTR(kIOReportLegendChannelsKey));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
CFDataRef deets = CFDataCreateWithBytesNoCopy(0, iorsub->addr, iorsub->addrSize, kCFAllocatorNull);
#pragma clang diagnostic pop
int byteIndex = 0;
for (int i = 0; i < IOReportGetChannelCount(subbedChannels); i++) {
CFMutableDictionaryRef channelArrayDict = (CFMutableDictionaryRef) CFArrayGetValueAtIndex(array, i);
uint64_t channel_type_ptr = 0;
CFArrayRef legend_chann = (CFArrayRef) CFDictionaryGetValue(channelArrayDict, CFSTR(kIOReportLegendChannelsKey));
CFNumberRef legend_chann_type = CFArrayGetValueAtIndex(legend_chann, kIOReportChannelTypeIdx);
CFNumberGetValue(legend_chann_type, kCFNumberLongType, &channel_type_ptr);
IOReportChannelType* channel_type = (IOReportChannelType*)&channel_type_ptr;
long size = channel_type->nelements * 64;
UInt8 buf[size];
CFRange range = CFRangeMake(byteIndex * 64, size);
CFDataGetBytes(deets, range, buf);
CFDataRef bytes = CFDataCreateWithBytesNoCopy(0, buf, size, kCFAllocatorNull);
CFDictionaryAddValue(channelArrayDict, kRawElementskey, bytes);
CFDictionaryAddValue(channelArrayDict, kStatenamesKey, CFSTR("")); // TODO: Setup
CFArraySetValueAtIndex(array, i, channelArrayDict);
// CFDictionarySetValue(channels, kIOReportChannelsKey, array);
byteIndex += channel_type->nelements;
// CFRelease(bytes);
channel_type = nil;
}
CFDictionarySetValue(channels, CFSTR(kIOReportLegendChannelsKey), array);
// CFRelease(deets);
return channels;
}
// MARK: Iterating Functions
void IOReportIterate(CFDictionaryRef samples, IOReportiterateblock handler) {
if (samples == NULL) return;
uint32_t count = IOReportGetChannelCount(samples);
CFArrayRef array = (CFArrayRef) CFDictionaryGetValue(samples, CFSTR(kIOReportLegendChannelsKey));
for (int i = 0; i < count; i++) {
IOReportSampleRef channel = (IOReportSampleRef) CFArrayGetValueAtIndex(array, i);
int ret = handler(channel);
if (ret == re_kIOReportIterFailed) return;
}
}
// MARK: Iteration exclusive functions for samples
int IOReportGetChannelCount(CFDictionaryRef a) {
if (a == NULL) return 0;
CFArrayRef channArray = CFDictionaryGetValue(a, CFSTR(kIOReportLegendChannelsKey));
int count = (int) CFArrayGetCount(channArray);
return count;
}
NSString* IOReportChannelGetChannelName(CFDictionaryRef a) {
if (a == NULL) return NULL;
CFArrayRef arr = (CFArrayRef) CFDictionaryGetValue(a, CFSTR(kIOReportLegendChannelsKey));
if (CFArrayGetCount(arr) < kIOReportChannelNameIdx + 1) return 0;
NSString * str = (NSString*) CFArrayGetValueAtIndex(arr, kIOReportChannelNameIdx);
return str;
}
NSString* IOReportChannelGetGroup(CFDictionaryRef a) {
if (a == NULL) return NULL;
NSString * str = (NSString*) CFDictionaryGetValue(a,CFSTR(kIOReportLegendGroupNameKey));
return str;
}
NSString* IOReportChannelGetSubGroup(CFDictionaryRef a) {
if (a == NULL) return NULL;
NSString * str = (NSString*) CFDictionaryGetValue(a,CFSTR(kIOReportLegendSubGroupNameKey));
return str;
}
NSString* IOReportChannelGetDriverName(CFDictionaryRef a) {
if (a == NULL) return NULL;
NSString * str = (NSString*) CFDictionaryGetValue(a, kDrivernameKey);
return str;
}
int IOReportChannelGetFormat(CFDictionaryRef samples) {
if (samples == NULL) return 0;
uint64_t channel_type_ptr = 0;
CFArrayRef legend_chann = (CFArrayRef) CFDictionaryGetValue(samples, CFSTR(kIOReportLegendChannelsKey));
CFNumberRef legend_chann_type = CFArrayGetValueAtIndex(legend_chann, kIOReportChannelTypeIdx);
CFNumberGetValue(legend_chann_type, kCFNumberLongLongType, &channel_type_ptr);
IOReportChannelType* channel_type = (IOReportChannelType*)&channel_type_ptr;
return (int)channel_type->report_format;
}
NSString* IOReportChannelGetUnitLabel(CFDictionaryRef a) {
if (a == NULL) return NULL;
uint64_t unit_label_qword = 0;
CFDictionaryRef chann_inf = (CFDictionaryRef) CFDictionaryGetValue(a, CFSTR(kIOReportLegendInfoKey));
CFNumberRef unit_label = CFDictionaryGetValue(chann_inf, CFSTR(kIOReportLegendUnitKey));
CFNumberGetValue(unit_label, kCFNumberLongLongType, &unit_label_qword);
switch (unit_label_qword) {
case kIOReportUnit1GHzTicks:
return @"1GTicks";
case kIOReportUnit24MHzTicks:
return @"24MTicks";
case kIOReportUnitHWTicks:
return @"HWTicks";
case kIOReportUnitPackets:
return @"packets";
case kIOReportUnitInstrs:
return @"instrs";
case kIOReportUnitEvents:
return @"events";
case kIOReportUnitBits:
return @"bits";
case kIOReportUnitBytes:
return @"bytes";
case kIOReportUnit_GI:
return @"gi";
case kIOReportUnit_KI:
return @"ki";
case kIOReportUnit_MI:
return @"mi";
case kIOReportUnit_ms:
return @"ms";
case kIOReportUnit_ns:
return @"ns";
case kIOReportUnit_s:
return @"s";
case kIOReportUnit_J:
return @"j";
case kIOReportUnit_mJ:
return @"mj";
case kIOReportUnit_pJ:
return @"pj";
case kIOReportUnit_uJ:
return @"uj";
case kIOReportUnit_nJ:
return @"nj";
case kIOReportUnit_GiB:
return @"gib";
case kIOReportUnit_MiB:
return @"mib";
case kIOReportUnit_KiB:
return @"kib";
case kIOReportUnitNone:
default:
return NULL;
}
}
long IOReportStateGetCount(CFDictionaryRef a) {
if (a == NULL) return 0;
CFDataRef data = CFDictionaryGetValue(a, kRawElementskey);
long length = CFDataGetLength(data);
if (length == 0) return 0;
return length / 64;
}
/* private func for reading raw elements */
void* _get_raw_elements(CFDictionaryRef a, int index, uint8_t type) {
if (a == NULL) return NULL;
CFDataRef data = CFDictionaryGetValue(a, kRawElementskey);
CFMutableDataRef mdata = CFDataCreateMutableCopy(kCFAllocatorDefault, CFDataGetLength(data), data);
if (data == NULL) return NULL;
void* ptr = CFDataGetMutableBytePtr(mdata);
return ptr;
}
long IOReportSimpleGetIntegerValue(CFDictionaryRef a, int b) {
uint64_t* ptr = (uint64_t*)_get_raw_elements(a, b, 2);
//
return *(ptr + 32);
}
uint64_t IOReportStateGetResidency(CFDictionaryRef a, int b) {
uint64_t* ptr = (uint64_t*)_get_raw_elements(a, b, 2);
return 0;
}
NSString* IOReportStateGetNameForIndex(CFDictionaryRef a, int b) {
if (a == NULL) return NULL;
return NULL;
}
// TODO: I haven't gotten around to decompiling this stuff
uint64_t IOReportArrayGetValueAtIndex(CFDictionaryRef a, int b) { return 0; }
int IOReportHistogramGetBucketCount(CFDictionaryRef a) { return 0; }
int IOReportHistogramGetBucketMinValue(CFDictionaryRef a, int b) { return 0; }
int IOReportHistogramGetBucketMaxValue(CFDictionaryRef a, int b) { return 0; }
int IOReportHistogramGetBucketSum(CFDictionaryRef a, int b) { return 0; }
int IOReportHistogramGetBucketHits(CFDictionaryRef a, int b) { return 0; }