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
|
//===--- Leaks.mm -----------------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// See Leaks.h for a description of this leaks detector.
//
//===----------------------------------------------------------------------===//
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
#include "Leaks.h"
#include "swift/Basic/Lazy.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#import <objc/objc.h>
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#include <set>
#include <cstdio>
extern "C" {
#include <pthread.h>
}
using namespace swift;
//===----------------------------------------------------------------------===//
// Extra Interfaces
//===----------------------------------------------------------------------===//
static void _swift_leaks_stopTrackingObjCObject(id obj);
static void _swift_leaks_startTrackingObjCObject(id obj);
//===----------------------------------------------------------------------===//
// State
//===----------------------------------------------------------------------===//
/// A set of allocated swift only objects that we are tracking for leaks.
static Lazy<std::set<HeapObject *>> TrackedSwiftObjects;
/// A set of allocated objc objects that we are tracking for leaks.
static Lazy<std::set<id>> TrackedObjCObjects;
/// Whether or not we should be collecting objects.
static bool ShouldTrackObjects = false;
/// A course grain lock that we use to synchronize our leak dictionary.
static pthread_mutex_t LeaksMutex = PTHREAD_MUTEX_INITIALIZER;
/// Where we store the dealloc, alloc, and allocWithZone functions we swizzled.
static IMP old_dealloc_fun;
static IMP old_alloc_fun;
static IMP old_allocWithZone_fun;
//===----------------------------------------------------------------------===//
// Init and Deinit Code
//===----------------------------------------------------------------------===//
static void __swift_leaks_dealloc(id self, SEL _cmd) {
_swift_leaks_stopTrackingObjCObject(self);
((void (*)(id, SEL))old_dealloc_fun)(self, _cmd);
}
static id __swift_leaks_alloc(id self, SEL _cmd) {
id result = ((id (*)(id, SEL))old_alloc_fun)(self, _cmd);
_swift_leaks_startTrackingObjCObject(result);
return result;
}
static id __swift_leaks_allocWithZone(id self, SEL _cmd, id zone) {
id result = ((id (*)(id, SEL, id))old_allocWithZone_fun)(self, _cmd, zone);
_swift_leaks_startTrackingObjCObject(result);
return result;
}
void _swift_leaks_startTrackingObjects(const char *name) {
pthread_mutex_lock(&LeaksMutex);
// First clear our tracked objects set.
TrackedSwiftObjects->clear();
TrackedObjCObjects->clear();
// Set that we should track objects.
ShouldTrackObjects = true;
// Swizzle out -(void)dealloc, +(id)alloc, and +(id)allocWithZone: for our
// custom implementations.
IMP new_dealloc_fun = (IMP)__swift_leaks_dealloc;
IMP new_alloc_fun = (IMP)__swift_leaks_alloc;
IMP new_allocWithZone_fun = (IMP)__swift_leaks_allocWithZone;
Method deallocMethod =
class_getInstanceMethod([NSObject class], @selector(dealloc));
Method allocMethod = class_getClassMethod([NSObject class], @selector(alloc));
Method allocWithZoneMethod =
class_getClassMethod([NSObject class], @selector(allocWithZone:));
old_dealloc_fun = method_setImplementation(deallocMethod, new_dealloc_fun);
old_alloc_fun = method_setImplementation(allocMethod, new_alloc_fun);
old_allocWithZone_fun =
method_setImplementation(allocWithZoneMethod, new_allocWithZone_fun);
pthread_mutex_unlock(&LeaksMutex);
}
/// This assumes that the LeaksMutex is already being held.
static void dumpSwiftHeapObjects() {
const char *comma = "";
for (HeapObject *Obj : *TrackedSwiftObjects) {
const HeapMetadata *Metadata = Obj->metadata;
fprintf(stderr, "%s", comma);
comma = ",";
if (!Metadata) {
fprintf(stderr, "{\"type\": \"null\"}");
continue;
}
const char *kindDescriptor = "";
switch (Metadata->getKind()) {
#define METADATAKIND(name, value) \
case MetadataKind::name: \
kindDescriptor = #name; \
break;
#include "swift/ABI/MetadataKind.def"
default:
kindDescriptor = "unknown";
break;
}
if (auto *NTD =
Metadata->getTypeContextDescriptor()) {
fprintf(stderr, "{"
"\"type\": \"nominal\", "
"\"name\": \"%s\", "
"\"kind\": \"%s\""
"}",
NTD->Name.get(), kindDescriptor);
continue;
}
fprintf(stderr, "{\"type\": \"unknown\", \"kind\": \"%s\"}",
kindDescriptor);
}
}
/// This assumes that the LeaksMutex is already being held.
static void dumpObjCHeapObjects() {
const char *comma = "";
for (id Obj : *TrackedObjCObjects) {
// Just print out the class of Obj.
fprintf(stderr, "%s\"%s\"", comma, object_getClassName(Obj));
comma = ",";
}
}
int _swift_leaks_stopTrackingObjects(const char *name) {
pthread_mutex_lock(&LeaksMutex);
unsigned Result = TrackedSwiftObjects->size() + TrackedObjCObjects->size();
fprintf(stderr, "{\"name\":\"%s\", \"swift_count\": %u, \"objc_count\": %u, "
"\"swift_objects\": [",
name, unsigned(TrackedSwiftObjects->size()),
unsigned(TrackedObjCObjects->size()));
dumpSwiftHeapObjects();
fprintf(stderr, "], \"objc_objects\": [");
dumpObjCHeapObjects();
fprintf(stderr, "]}\n");
fflush(stderr);
TrackedSwiftObjects->clear();
TrackedObjCObjects->clear();
ShouldTrackObjects = false;
// Undo our swizzling.
Method deallocMethod =
class_getInstanceMethod([NSObject class], @selector(dealloc));
Method allocMethod = class_getClassMethod([NSObject class], @selector(alloc));
Method allocWithZoneMethod =
class_getClassMethod([NSObject class], @selector(allocWithZone:));
method_setImplementation(deallocMethod, old_dealloc_fun);
method_setImplementation(allocMethod, old_alloc_fun);
method_setImplementation(allocWithZoneMethod, old_allocWithZone_fun);
pthread_mutex_unlock(&LeaksMutex);
return Result;
}
//===----------------------------------------------------------------------===//
// Tracking Code
//===----------------------------------------------------------------------===//
void _swift_leaks_startTrackingObject(HeapObject *Object) {
pthread_mutex_lock(&LeaksMutex);
if (ShouldTrackObjects) {
TrackedSwiftObjects->insert(Object);
}
pthread_mutex_unlock(&LeaksMutex);
}
void _swift_leaks_stopTrackingObject(HeapObject *Object) {
pthread_mutex_lock(&LeaksMutex);
TrackedSwiftObjects->erase(Object);
pthread_mutex_unlock(&LeaksMutex);
}
static void _swift_leaks_startTrackingObjCObject(id Object) {
pthread_mutex_lock(&LeaksMutex);
if (ShouldTrackObjects) {
TrackedObjCObjects->insert(Object);
}
pthread_mutex_unlock(&LeaksMutex);
}
static void _swift_leaks_stopTrackingObjCObject(id Object) {
pthread_mutex_lock(&LeaksMutex);
TrackedObjCObjects->erase(Object);
pthread_mutex_unlock(&LeaksMutex);
}
#else
static char DummyDecl = '';
#endif
|