Repository navigation
Expand file tree
/
Copy pathCoverageProfile.mm
More file actions
358 lines (316 loc) · 11.1 KB
/
Copy pathCoverageProfile.mm
File metadata and controls
358 lines (316 loc) · 11.1 KB
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
#import "CoverageProfile.h"
#import "CoverageConfig.h"
#import <mach-o/dyld.h>
#import <mach-o/loader.h>
#import <mach-o/nlist.h>
#import <stdlib.h>
#import <string.h>
extern "C" {
int __llvm_profile_write_file(void);
void __llvm_profile_set_filename(const char *Name);
const char *__llvm_profile_get_filename(void);
}
typedef int (*CoverageProfileWriteFn)(void);
typedef void (*CoverageProfileSetFilenameFn)(const char *);
typedef struct {
const struct mach_header *header;
intptr_t slide;
CoverageProfileWriteFn writeFile;
const char *imageName;
} CoverageTrackedProfileImage;
enum { CoverageMaxTrackedProfileImages = 64 };
static CoverageTrackedProfileImage gCoverageTracked[CoverageMaxTrackedProfileImages];
static uint32_t gCoverageTrackedCount = 0;
/**
* `%m` expands to a unique module signature so each instrumented Mach-O image
* writes its own profraw instead of overwriting a single shared file.
*/
static NSString *CoverageProfilePattern(void)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths firstObject] stringByAppendingPathComponent:@COVERAGE_PROFILE_FILE_PATTERN];
}
/**
* clang_rt.profile symbols are linked into each instrumented framework as local
* (`t`) symbols — dlsym cannot see them. Resolve via the on-disk symbol table
* mapped through __LINKEDIT.
*/
static const void *CoverageFindSymbolInHeader(
const struct mach_header *header, intptr_t slide, const char *symbolName)
{
if (header == NULL || symbolName == NULL) {
return NULL;
}
if (header->magic != MH_MAGIC_64 && header->magic != MH_CIGAM_64) {
return NULL;
}
const struct mach_header_64 *header64 = (const struct mach_header_64 *)header;
const uint8_t *cursor = (const uint8_t *)(header64 + 1);
const struct symtab_command *symtab = NULL;
const struct segment_command_64 *linkedit = NULL;
for (uint32_t i = 0; i < header64->ncmds; i++) {
const struct load_command *command = (const struct load_command *)cursor;
if (command->cmd == LC_SYMTAB) {
symtab = (const struct symtab_command *)command;
} else if (command->cmd == LC_SEGMENT_64) {
const struct segment_command_64 *segment = (const struct segment_command_64 *)command;
if (strcmp(segment->segname, SEG_LINKEDIT) == 0) {
linkedit = segment;
}
}
cursor += command->cmdsize;
}
if (symtab == NULL || linkedit == NULL || symtab->nsyms == 0) {
return NULL;
}
const uintptr_t linkeditBase =
(uintptr_t)slide + (uintptr_t)linkedit->vmaddr - (uintptr_t)linkedit->fileoff;
const char *strings = (const char *)(linkeditBase + symtab->stroff);
const struct nlist_64 *symbols =
(const struct nlist_64 *)(linkeditBase + symtab->symoff);
const size_t nameLength = strlen(symbolName);
for (uint32_t i = 0; i < symtab->nsyms; i++) {
uint32_t stringIndex = symbols[i].n_un.n_strx;
if (stringIndex == 0) {
continue;
}
const char *name = strings + stringIndex;
if (strncmp(name, symbolName, nameLength) != 0 || name[nameLength] != '\0') {
continue;
}
if ((symbols[i].n_type & N_TYPE) == N_UNDF || symbols[i].n_value == 0) {
continue;
}
return (const void *)((uintptr_t)symbols[i].n_value + (uintptr_t)slide);
}
return NULL;
}
static const char *CoverageImageNameForHeader(const struct mach_header *header)
{
uint32_t imageCount = _dyld_image_count();
for (uint32_t index = 0; index < imageCount; index += 1) {
if (_dyld_get_image_header(index) == header) {
return _dyld_get_image_name(index);
}
}
return NULL;
}
static bool CoverageIsConfiguredFrameworkImage(const char *imageName)
{
if (imageName == NULL) {
return false;
}
#if !COVERAGE_ENABLED
return false;
#endif
if (COVERAGE_FRAMEWORK_PREFIX_COUNT <= 0) {
return false;
}
const char *framework = strstr(imageName, ".framework");
if (framework == NULL) {
return false;
}
const char *basename = framework;
while (basename > imageName && basename[-1] != '/') {
basename -= 1;
}
for (int i = 0; i < COVERAGE_FRAMEWORK_PREFIX_COUNT; i++) {
const char *prefix = COVERAGE_FRAMEWORK_PREFIXES[i];
if (prefix == NULL) {
continue;
}
size_t prefixLen = strlen(prefix);
if (prefixLen > 0 && strncmp(basename, prefix, prefixLen) == 0) {
return true;
}
}
return false;
}
/**
* React Native / Expo Debug builds often place app + static-pod object code in
* `AppName.debug.dylib` (MH_DYLIB), not MH_EXECUTE. Flush that companion image
* via LINKEDIT so fixture/static-lib counters are not missed when Expo forces
* MACH_O_TYPE=staticlib on pods under use_frameworks.
*/
static bool CoverageIsAppCompanionDebugDylib(const char *imageName)
{
if (imageName == NULL) {
return false;
}
if (strstr(imageName, ".app/") == NULL) {
return false;
}
return strstr(imageName, ".debug.dylib") != NULL;
}
static bool CoverageShouldTrackImage(const char *imageName)
{
return CoverageIsConfiguredFrameworkImage(imageName) ||
CoverageIsAppCompanionDebugDylib(imageName);
}
static bool CoverageIsMainExecutable(const struct mach_header *header)
{
return header != NULL && header->filetype == MH_EXECUTE;
}
static void CoverageSetFilenameOnImage(
const struct mach_header *header, intptr_t slide, const char *pattern)
{
CoverageProfileSetFilenameFn setFilename =
(CoverageProfileSetFilenameFn)CoverageFindSymbolInHeader(
header, slide, "___llvm_profile_set_filename");
if (setFilename != NULL && pattern != NULL) {
setFilename(pattern);
return;
}
char **filenameVar = (char **)CoverageFindSymbolInHeader(
header, slide, "___llvm_profile_filename");
if (filenameVar != NULL && pattern != NULL) {
*filenameVar = (char *)pattern;
}
}
static void CoverageTrackProfileImage(const struct mach_header *header, intptr_t slide)
{
const char *imageName = CoverageImageNameForHeader(header);
if (!CoverageShouldTrackImage(imageName)) {
return;
}
CoverageProfileWriteFn writeFile = (CoverageProfileWriteFn)CoverageFindSymbolInHeader(
header, slide, "___llvm_profile_write_file");
// Skip if missing or if it resolves to *this* Pod's runtime (not a distinct image).
if (writeFile == NULL || writeFile == __llvm_profile_write_file) {
NSLog(@"[ios-native-coverage] skip image (no distinct profile runtime): %s", imageName);
return;
}
for (uint32_t i = 0; i < gCoverageTrackedCount; i++) {
if (gCoverageTracked[i].writeFile == writeFile) {
return;
}
}
if (gCoverageTrackedCount >= CoverageMaxTrackedProfileImages) {
NSLog(@"[ios-native-coverage] tracked profile image cap reached (%d)",
CoverageMaxTrackedProfileImages);
return;
}
gCoverageTracked[gCoverageTrackedCount].header = header;
gCoverageTracked[gCoverageTrackedCount].slide = slide;
gCoverageTracked[gCoverageTrackedCount].writeFile = writeFile;
gCoverageTracked[gCoverageTrackedCount].imageName = imageName;
gCoverageTrackedCount += 1;
NSLog(@"[ios-native-coverage] tracked profile image[%u]: %s",
gCoverageTrackedCount - 1,
imageName);
}
static void CoverageOnAddImage(const struct mach_header *header, intptr_t slide)
{
CoverageTrackProfileImage(header, slide);
}
__attribute__((constructor)) static void CoverageProfileInit(void)
{
_dyld_register_func_for_add_image(CoverageOnAddImage);
}
static void CoverageRefreshTrackedFromDyld(void)
{
uint32_t imageCount = _dyld_image_count();
for (uint32_t index = 0; index < imageCount; index += 1) {
CoverageTrackProfileImage(
_dyld_get_image_header(index), _dyld_get_image_vmaddr_slide(index));
}
}
static int CoverageFlushTracked(const char *pattern)
{
int worstStatus = 0;
int wrote = 0;
for (uint32_t i = 0; i < gCoverageTrackedCount; i++) {
CoverageSetFilenameOnImage(
gCoverageTracked[i].header, gCoverageTracked[i].slide, pattern);
int status = gCoverageTracked[i].writeFile();
NSLog(
@"[ios-native-coverage] flush tracked[%u] status=%d image=%s",
i,
status,
gCoverageTracked[i].imageName ?: "(null)");
if (status != 0) {
worstStatus = status;
} else {
wrote += 1;
}
}
return wrote > 0 ? 0 : worstStatus;
}
static int CoverageFlushMainViaLinkedit(const char *pattern)
{
uint32_t imageCount = _dyld_image_count();
for (uint32_t index = 0; index < imageCount; index += 1) {
const struct mach_header *header = _dyld_get_image_header(index);
intptr_t slide = _dyld_get_image_vmaddr_slide(index);
if (!CoverageIsMainExecutable(header)) {
continue;
}
const char *imageName = CoverageImageNameForHeader(header);
CoverageProfileWriteFn writeFile = (CoverageProfileWriteFn)CoverageFindSymbolInHeader(
header, slide, "___llvm_profile_write_file");
NSLog(
@"[ios-native-coverage] main-exe image=%s writeFile=%p podWriteFile=%p same=%d",
imageName ?: "(null)",
(void *)writeFile,
(void *)__llvm_profile_write_file,
writeFile == __llvm_profile_write_file ? 1 : 0);
if (writeFile == NULL) {
NSLog(@"[ios-native-coverage] main executable has no ___llvm_profile_write_file via LINKEDIT");
return -1;
}
CoverageSetFilenameOnImage(header, slide, pattern);
int status = writeFile();
NSLog(@"[ios-native-coverage] flush main-via-LINKEDIT status=%d", status);
return status;
}
NSLog(@"[ios-native-coverage] no MH_EXECUTE image found");
return -1;
}
extern "C" void CoverageConfigureProfilePath(void)
{
const char *pattern = CoverageProfilePattern().UTF8String;
// Dynamic frameworks often lack set_filename (dead-stripped). getenv is consulted
// by every runtime copy on write.
setenv("LLVM_PROFILE_FILE", pattern, 1);
__llvm_profile_set_filename(pattern);
NSLog(
@"[ios-native-coverage] configure pattern=%@ podRuntimePath=%s",
CoverageProfilePattern(),
__llvm_profile_get_filename() ?: "(null)");
}
/**
* Mode (c): Pod LINKEDIT for configured frameworks + main executable, then pod write.
* Prefer this under use_frameworks! :linkage => :dynamic (05S1 verdict).
*/
extern "C" int CoverageFlushProfile(void)
{
#if !COVERAGE_ENABLED
NSLog(@"[ios-native-coverage] flush skipped (COVERAGE_ENABLED=0)");
return -1;
#endif
CoverageConfigureProfilePath();
CoverageRefreshTrackedFromDyld();
const char *pattern = getenv("LLVM_PROFILE_FILE");
int trackedStatus = CoverageFlushTracked(pattern);
int mainStatus = CoverageFlushMainViaLinkedit(pattern);
int podStatus = __llvm_profile_write_file();
NSLog(
@"[ios-native-coverage] flush complete tracked=%u trackedStatus=%d mainStatus=%d podStatus=%d pattern=%s",
gCoverageTrackedCount,
trackedStatus,
mainStatus,
podStatus,
pattern ?: "(null)");
int okTracked = (trackedStatus == 0 && gCoverageTrackedCount > 0) ? 1 : 0;
int okMain = (mainStatus == 0) ? 1 : 0;
// Under Expo static-pod merge, fixture counters live in the same image as this
// Pod's profile runtime — pod write is the success path.
int okPod = (podStatus == 0) ? 1 : 0;
if (okTracked || okMain || okPod) {
return 0;
}
if (mainStatus != 0) {
return mainStatus;
}
return trackedStatus != 0 ? trackedStatus : podStatus;
}