// // PortCommunication.m // iOS16Bug // // Created by Pawan Dixit on 14/09/2022. // #import "PortCommunication.h" typedef pthread_mutex_t CFLock_t; typedef struct __CFRuntimeBase { uintptr_t _cfisa; uint8_t _cfinfo[4]; #if __LP64__ uint32_t _rc; #endif } CFRuntimeBase; typedef CFDataRef (*CFMessagePortCallBackEx)(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info, void *trailer, uintptr_t); struct __CFMessagePort { CFRuntimeBase _base; CFLock_t _lock; CFStringRef _name; CFMachPortRef _port; /* immutable; invalidated */ CFMutableDictionaryRef _replies; int32_t _convCounter; int32_t _perPID; /* zero if not per-pid, else pid */ CFMachPortRef _replyPort; /* only used by remote port; immutable once created; invalidated */ CFRunLoopSourceRef _source; /* only used by local port; immutable once created; invalidated */ dispatch_source_t _dispatchSource; /* only used by local port; invalidated */ dispatch_queue_t _dispatchQ; /* only used by local port */ CFMessagePortInvalidationCallBack _icallout; CFMessagePortCallBack _callout; /* only used by local port; immutable */ CFMessagePortCallBackEx _calloutEx; /* only used by local port; immutable */ CFMessagePortContext _context; /* not part of remote port; immutable; invalidated */ }; @interface PortCommunication () { dispatch_queue_t machServerQueue; } @property(nonatomic, assign) CFMessagePortRef sendPort; @end CFDataRef callback(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) { return NULL; } @implementation PortCommunication -(instancetype)initWithGroupName:(NSString *)groupName sdkId:(NSString *)sdkId { self = [super init]; if (nil != self) { NSString *portIdentifier = [[groupName stringByAppendingString:@"."] stringByAppendingString:sdkId]; NSString *portName = [[portIdentifier stringByAppendingString:@"."] stringByAppendingString:@"screen.mach.port"]; CFMessagePortContext context = {0,(__bridge void *)self,nil,nil,nil}; self.sendPort = CFMessagePortCreateLocal(kCFAllocatorDefault, (__bridge CFStringRef)portName, &callback, &context, false); machServerQueue = dispatch_queue_create("machServerQueue", DISPATCH_QUEUE_SERIAL); CFMessagePortSetDispatchQueue(self.sendPort, machServerQueue); CFMessagePortRef ms = self.sendPort; mach_port_t machPort = CFMachPortGetPort(ms->_port); } return self; } @end