Fix memory corruption issue when copying CFDataRef bytes

Copy the bytes manually using -[NSData dataWithBytes:length:]
If we use -[NSData copy] instead, we may be using already freed memory
(according to ASAN), which is no good. This may be because the bytes aren't
deep copied.

Also add some @autoreleasepool wrappers because why not.
This commit is contained in:
Zorg committed 2016-03-19 22:06:33 -04:00
1 parent 56a75bd4b6
commit 4e8a39267d
2 files changed
+47 -38

No files matched your search

+29 -23
View File
@@ -61,36 +61,42 @@
// Called on non-main thread
static CFDataRef messagePortCallback(CFMessagePortRef __unused messagePort, SInt32 messageID, CFDataRef dataRef, void *info)
{
SULocalMessagePort *self = (__bridge SULocalMessagePort *)info;
NSData *data = (__bridge NSData *)dataRef;
if (self.messageCallback != nil) {
self.messageCallback(messageID, data);
@autoreleasepool {
SULocalMessagePort *self = (__bridge SULocalMessagePort *)info;
// Create a copy that we can safely pass it asynchronously
// Warning: Do not use a convenience -[NSData copy] or an equivalent call. This crashes, and ASAN will let you know it crashes
// Most likely because the bytes are set to be freed internally, and not deep copied or something
NSData *data = [NSData dataWithBytes:CFDataGetBytePtr(dataRef) length:(NSUInteger)CFDataGetLength(dataRef)];
if (self.messageCallback != nil) {
self.messageCallback(messageID, data);
}
return NULL;
}
// Don't have any use case where I need a reply, for now
return NULL;
}
// Called on non-main thread
static void messageInvalidationCallback(CFMessagePortRef messagePort, void *info)
{
SULocalMessagePort *self = (__bridge SULocalMessagePort *)info;
dispatch_async(self.messageQueue, ^{
// note that messageCallback is deallocated on same queue that we're receiving messages from, which is good
self.messageCallback = nil;
@autoreleasepool {
SULocalMessagePort *self = (__bridge SULocalMessagePort *)info;
if (self.invalidationCallback != nil) {
self.invalidationCallback();
self.invalidationCallback = nil;
}
self.messagePort = NULL;
CFRelease(messagePort);
CFRelease((__bridge CFTypeRef)(self));
});
dispatch_async(self.messageQueue, ^{
// note that messageCallback is deallocated on same queue that we're receiving messages from, which is good
self.messageCallback = nil;
if (self.invalidationCallback != nil) {
self.invalidationCallback();
self.invalidationCallback = nil;
}
self.messagePort = NULL;
CFRelease(messagePort);
CFRelease((__bridge CFTypeRef)(self));
});
}
}
@end
+18 -15
View File
@@ -54,7 +54,8 @@ static const char *SURemoteMessagePortSelfKey = "su_messagePort";
dispatch_async(self.messageQueue, ^{
SInt32 status = 0;
if (self.messagePort != NULL) {
status = CFMessagePortSendRequest(self.messagePort, identifier, (CFDataRef)data, 0.1, 0.0, NULL, NULL);
CFDataRef dataRef = (__bridge CFDataRef)(data);
status = CFMessagePortSendRequest(self.messagePort, identifier, dataRef, 0.2, 0.0, NULL, NULL);
} else {
status = kCFMessagePortIsInvalid;
}
@@ -75,21 +76,23 @@ static const char *SURemoteMessagePortSelfKey = "su_messagePort";
// For safetly, let's not assume what thread this may be called on
static void messageInvalidationCallback(CFMessagePortRef messagePort, void * __unused info)
{
SURemoteMessagePort *self = objc_getAssociatedObject((__bridge id)(messagePort), SURemoteMessagePortSelfKey);
dispatch_async(self.messageQueue, ^{
if (self.invalidationCallback != nil) {
self.invalidationCallback();
self.invalidationCallback = nil;
}
@autoreleasepool {
SURemoteMessagePort *self = objc_getAssociatedObject((__bridge id)(messagePort), SURemoteMessagePortSelfKey);
self.messagePort = NULL;
objc_setAssociatedObject((__bridge id)(messagePort), SURemoteMessagePortSelfKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
CFRelease(messagePort);
CFRelease((__bridge CFTypeRef)(self));
});
dispatch_async(self.messageQueue, ^{
if (self.invalidationCallback != nil) {
self.invalidationCallback();
self.invalidationCallback = nil;
}
self.messagePort = NULL;
objc_setAssociatedObject((__bridge id)(messagePort), SURemoteMessagePortSelfKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
CFRelease(messagePort);
CFRelease((__bridge CFTypeRef)(self));
});
}
}
@end