This is an archive of the discontinued LLVM Phabricator instance.

Treat allocations within dispatch_once blocks as global allocations
AbandonedPublic

Authored by fjricci on Jun 28 2017, 12:38 PM.

Details

Summary

Objective-C programs often treat dispatch_once blocks similarly to global
variables, since they are only executed once over the course of the process.
They are often used to initialize singletons and other process state.

For an example of a simple test case which reveals this behavior in darwin system
libraries:

// clang -fsanitize=leak -framework Foundation test.m
#import <Foundation/Foundation.h>

int main() {
    @autoreleasepool { id defaults = [NSUserDefaults standardUserDefaults]; };
    return 0;
}

This patch treats dispatch_once allocations as global variables, ignoring
them if use_globals is enabled.

Event Timeline

fjricci created this revision.Jun 28 2017, 12:38 PM
fjricci added a comment.EditedJun 28 2017, 12:40 PM

An alternative solution here would be to add a suppression for dispatch_once instead, but that would be less configurable (unless we required the user to provide it) and slightly less performant.

fjricci abandoned this revision.Jun 28 2017, 2:16 PM

This doesn't provide as much of a performance benefit as I expected. In addition, the fact that a leak comes from a system library doesn't make it less of a leak. It's not a great user experience to see system library leaks in your program output, but I don't think there is much we can/should do about it.