1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 |*===----------------------------------------------------------------------===*|
8 |*
9 |* This file implements the call back routines for the gcov profiling
10 |* instrumentation pass. Link against this library when running code through
11 |* the -insert-gcov-profiling LLVM pass.
12 |*
13 |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
14 |* are only close enough that LCOV will happily parse them. Anything that lcov
15 |* ignores is missing.
16 |*
17 |* TODO: gcov is multi-process safe by having each exit open the existing file
18 |* and append to it. We'd like to achieve that and be thread-safe too.
19 |*
20 \*===----------------------------------------------------------------------===*/
21
22 #if !defined(__Fuchsia__)
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #if defined(_WIN32)
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33 #include "WindowsMMap.h"
34 #else
35 #include <sys/file.h>
36 #include <sys/mman.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #endif
40
41 #if defined(__FreeBSD__) && defined(__i386__)
42 #define I386_FREEBSD 1
43 #else
44 #define I386_FREEBSD 0
45 #endif
46
47 #if !defined(_MSC_VER) && !I386_FREEBSD
48 #include <stdint.h>
49 #endif
50
51 #if defined(_MSC_VER)
52 typedef unsigned char uint8_t;
53 typedef unsigned int uint32_t;
54 typedef unsigned long long uint64_t;
55 #elif I386_FREEBSD
56 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
57 * FreeBSD 10, r232261) when compiled in 32-bit mode.
58 */
59 typedef unsigned char uint8_t;
60 typedef unsigned int uint32_t;
61 typedef unsigned long long uint64_t;
62 #endif
63
64 #include "InstrProfiling.h"
65 #include "InstrProfilingUtil.h"
66
67 /* #define DEBUG_GCDAPROFILING */
68
69 enum {
70 GCOV_DATA_MAGIC = 0x67636461, // "gcda"
71
72 GCOV_TAG_FUNCTION = 0x01000000,
73 GCOV_TAG_COUNTER_ARCS = 0x01a10000,
74 // GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.
75 GCOV_TAG_OBJECT_SUMMARY = 0xa1000000,
76 GCOV_TAG_PROGRAM_SUMMARY = 0xa3000000,
77 };
78
79 /*
80 * --- GCOV file format I/O primitives ---
81 */
82
83 /*
84 * The current file name we're outputting. Used primarily for error logging.
85 */
86 static char *filename = NULL;
87
88 /*
89 * The current file we're outputting.
90 */
91 static FILE *output_file = NULL;
92
93 /*
94 * Buffer that we write things into.
95 */
96 #define WRITE_BUFFER_SIZE (128 * 1024)
97 static unsigned char *write_buffer = NULL;
98 static uint64_t cur_buffer_size = 0;
99 static uint64_t cur_pos = 0;
100 static uint64_t file_size = 0;
101 static int new_file = 0;
102 static int gcov_version;
103 #if defined(_WIN32)
104 static HANDLE mmap_handle = NULL;
105 #endif
106 static int fd = -1;
107
108 typedef void (*fn_ptr)();
109
110 typedef void* dynamic_object_id;
111 // The address of this variable identifies a given dynamic object.
112 static dynamic_object_id current_id;
113 #define CURRENT_ID (¤t_id)
114
115 struct fn_node {
116 dynamic_object_id id;
117 fn_ptr fn;
118 struct fn_node* next;
119 };
120
121 struct fn_list {
122 struct fn_node *head, *tail;
123 };
124
125 /*
126 * A list of functions to write out the data, shared between all dynamic objects.
127 */
128 struct fn_list writeout_fn_list;
129
130 /*
131 * A list of reset functions, shared between all dynamic objects.
132 */
133 struct fn_list reset_fn_list;
134
fn_list_insert(struct fn_list * list,fn_ptr fn)135 static void fn_list_insert(struct fn_list* list, fn_ptr fn) {
136 struct fn_node* new_node = malloc(sizeof(struct fn_node));
137 new_node->fn = fn;
138 new_node->next = NULL;
139 new_node->id = CURRENT_ID;
140
141 if (!list->head) {
142 list->head = list->tail = new_node;
143 } else {
144 list->tail->next = new_node;
145 list->tail = new_node;
146 }
147 }
148
fn_list_remove(struct fn_list * list)149 static void fn_list_remove(struct fn_list* list) {
150 struct fn_node* curr = list->head;
151 struct fn_node* prev = NULL;
152 struct fn_node* next = NULL;
153
154 while (curr) {
155 next = curr->next;
156
157 if (curr->id == CURRENT_ID) {
158 if (curr == list->head) {
159 list->head = next;
160 }
161
162 if (curr == list->tail) {
163 list->tail = prev;
164 }
165
166 if (prev) {
167 prev->next = next;
168 }
169
170 free(curr);
171 } else {
172 prev = curr;
173 }
174
175 curr = next;
176 }
177 }
178
resize_write_buffer(uint64_t size)179 static void resize_write_buffer(uint64_t size) {
180 if (!new_file) return;
181 size += cur_pos;
182 if (size <= cur_buffer_size) return;
183 size = (size - 1) / WRITE_BUFFER_SIZE + 1;
184 size *= WRITE_BUFFER_SIZE;
185 write_buffer = realloc(write_buffer, size);
186 cur_buffer_size = size;
187 }
188
write_bytes(const char * s,size_t len)189 static void write_bytes(const char *s, size_t len) {
190 resize_write_buffer(len);
191 memcpy(&write_buffer[cur_pos], s, len);
192 cur_pos += len;
193 }
194
write_32bit_value(uint32_t i)195 static void write_32bit_value(uint32_t i) {
196 write_bytes((char*)&i, 4);
197 }
198
write_64bit_value(uint64_t i)199 static void write_64bit_value(uint64_t i) {
200 // GCOV uses a lo-/hi-word format even on big-endian systems.
201 // See also GCOVBuffer::readInt64 in LLVM.
202 uint32_t lo = (uint32_t) i;
203 uint32_t hi = (uint32_t) (i >> 32);
204 write_32bit_value(lo);
205 write_32bit_value(hi);
206 }
207
read_32bit_value()208 static uint32_t read_32bit_value() {
209 uint32_t val;
210
211 if (new_file)
212 return (uint32_t)-1;
213
214 val = *(uint32_t*)&write_buffer[cur_pos];
215 cur_pos += 4;
216 return val;
217 }
218
read_64bit_value()219 static uint64_t read_64bit_value() {
220 // GCOV uses a lo-/hi-word format even on big-endian systems.
221 // See also GCOVBuffer::readInt64 in LLVM.
222 uint32_t lo = read_32bit_value();
223 uint32_t hi = read_32bit_value();
224 return ((uint64_t)hi << 32) | ((uint64_t)lo);
225 }
226
mangle_filename(const char * orig_filename)227 static char *mangle_filename(const char *orig_filename) {
228 char *new_filename;
229 size_t prefix_len;
230 int prefix_strip;
231 const char *prefix = lprofGetPathPrefix(&prefix_strip, &prefix_len);
232
233 if (prefix == NULL)
234 return strdup(orig_filename);
235
236 new_filename = malloc(prefix_len + 1 + strlen(orig_filename) + 1);
237 lprofApplyPathPrefix(new_filename, orig_filename, prefix, prefix_len,
238 prefix_strip);
239
240 return new_filename;
241 }
242
map_file()243 static int map_file() {
244 fseek(output_file, 0L, SEEK_END);
245 file_size = ftell(output_file);
246
247 /* A size of 0 means the file has been created just now (possibly by another
248 * process in lock-after-open race condition). No need to mmap. */
249 if (file_size == 0)
250 return -1;
251
252 #if defined(_WIN32)
253 HANDLE mmap_fd;
254 if (fd == -1)
255 mmap_fd = INVALID_HANDLE_VALUE;
256 else
257 mmap_fd = (HANDLE)_get_osfhandle(fd);
258
259 mmap_handle = CreateFileMapping(mmap_fd, NULL, PAGE_READWRITE, DWORD_HI(file_size), DWORD_LO(file_size), NULL);
260 if (mmap_handle == NULL) {
261 fprintf(stderr, "profiling: %s: cannot create file mapping: %lu\n",
262 filename, GetLastError());
263 return -1;
264 }
265
266 write_buffer = MapViewOfFile(mmap_handle, FILE_MAP_WRITE, 0, 0, file_size);
267 if (write_buffer == NULL) {
268 fprintf(stderr, "profiling: %s: cannot map: %lu\n", filename,
269 GetLastError());
270 CloseHandle(mmap_handle);
271 return -1;
272 }
273 #else
274 write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
275 MAP_FILE | MAP_SHARED, fd, 0);
276 if (write_buffer == (void *)-1) {
277 int errnum = errno;
278 fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
279 strerror(errnum));
280 return -1;
281 }
282 #endif
283
284 return 0;
285 }
286
unmap_file()287 static void unmap_file() {
288 #if defined(_WIN32)
289 if (!FlushViewOfFile(write_buffer, file_size)) {
290 fprintf(stderr, "profiling: %s: cannot flush mapped view: %lu\n", filename,
291 GetLastError());
292 }
293
294 if (!UnmapViewOfFile(write_buffer)) {
295 fprintf(stderr, "profiling: %s: cannot unmap mapped view: %lu\n", filename,
296 GetLastError());
297 }
298
299 if (!CloseHandle(mmap_handle)) {
300 fprintf(stderr, "profiling: %s: cannot close file mapping handle: %lu\n",
301 filename, GetLastError());
302 }
303
304 mmap_handle = NULL;
305 #else
306 if (munmap(write_buffer, file_size) == -1) {
307 int errnum = errno;
308 fprintf(stderr, "profiling: %s: cannot munmap: %s\n", filename,
309 strerror(errnum));
310 }
311 #endif
312
313 write_buffer = NULL;
314 file_size = 0;
315 }
316
317 /*
318 * --- LLVM line counter API ---
319 */
320
321 /* A file in this case is a translation unit. Each .o file built with line
322 * profiling enabled will emit to a different file. Only one file may be
323 * started at a time.
324 */
325 COMPILER_RT_VISIBILITY
llvm_gcda_start_file(const char * orig_filename,uint32_t version,uint32_t checksum)326 void llvm_gcda_start_file(const char *orig_filename, uint32_t version,
327 uint32_t checksum) {
328 const char *mode = "r+b";
329 filename = mangle_filename(orig_filename);
330
331 /* Try just opening the file. */
332 fd = open(filename, O_RDWR | O_BINARY);
333
334 if (fd == -1) {
335 /* Try creating the file. */
336 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
337 if (fd != -1) {
338 mode = "w+b";
339 } else {
340 /* Try creating the directories first then opening the file. */
341 __llvm_profile_recursive_mkdir(filename);
342 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644);
343 if (fd != -1) {
344 mode = "w+b";
345 } else {
346 /* Another process may have created the file just now.
347 * Try opening it without O_CREAT and O_EXCL. */
348 fd = open(filename, O_RDWR | O_BINARY);
349 if (fd == -1) {
350 /* Bah! It's hopeless. */
351 int errnum = errno;
352 fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
353 strerror(errnum));
354 return;
355 }
356 }
357 }
358 }
359
360 /* Try to flock the file to serialize concurrent processes writing out to the
361 * same GCDA. This can fail if the filesystem doesn't support it, but in that
362 * case we'll just carry on with the old racy behaviour and hope for the best.
363 */
364 lprofLockFd(fd);
365 output_file = fdopen(fd, mode);
366
367 /* Initialize the write buffer. */
368 new_file = 0;
369 write_buffer = NULL;
370 cur_buffer_size = 0;
371 cur_pos = 0;
372
373 if (map_file() == -1) {
374 /* The file has been created just now (file_size == 0) or mmap failed
375 * unexpectedly. In the latter case, try to recover by clobbering. */
376 new_file = 1;
377 write_buffer = NULL;
378 resize_write_buffer(WRITE_BUFFER_SIZE);
379 memset(write_buffer, 0, WRITE_BUFFER_SIZE);
380 }
381
382 /* gcda file, version, stamp checksum. */
383 {
384 uint8_t c3 = version >> 24;
385 uint8_t c2 = (version >> 16) & 255;
386 uint8_t c1 = (version >> 8) & 255;
387 gcov_version = c3 >= 'A' ? (c3 - 'A') * 100 + (c2 - '0') * 10 + c1 - '0'
388 : (c3 - '0') * 10 + c1 - '0';
389 }
390 write_32bit_value(GCOV_DATA_MAGIC);
391 write_32bit_value(version);
392 write_32bit_value(checksum);
393
394 #ifdef DEBUG_GCDAPROFILING
395 fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
396 #endif
397 }
398
399 COMPILER_RT_VISIBILITY
llvm_gcda_emit_function(uint32_t ident,uint32_t func_checksum,uint32_t cfg_checksum)400 void llvm_gcda_emit_function(uint32_t ident, uint32_t func_checksum,
401 uint32_t cfg_checksum) {
402 uint32_t len = 2;
403 int use_extra_checksum = gcov_version >= 47;
404
405 if (use_extra_checksum)
406 len++;
407 #ifdef DEBUG_GCDAPROFILING
408 fprintf(stderr, "llvmgcda: function id=0x%08x\n", ident);
409 #endif
410 if (!output_file) return;
411
412 /* function tag */
413 write_32bit_value(GCOV_TAG_FUNCTION);
414 write_32bit_value(len);
415 write_32bit_value(ident);
416 write_32bit_value(func_checksum);
417 if (use_extra_checksum)
418 write_32bit_value(cfg_checksum);
419 }
420
421 COMPILER_RT_VISIBILITY
llvm_gcda_emit_arcs(uint32_t num_counters,uint64_t * counters)422 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
423 uint32_t i;
424 uint64_t *old_ctrs = NULL;
425 uint32_t val = 0;
426 uint64_t save_cur_pos = cur_pos;
427
428 if (!output_file) return;
429
430 val = read_32bit_value();
431
432 if (val != (uint32_t)-1) {
433 /* There are counters present in the file. Merge them. */
434 if (val != GCOV_TAG_COUNTER_ARCS) {
435 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
436 "corrupt arc tag (0x%08x)\n",
437 filename, val);
438 return;
439 }
440
441 val = read_32bit_value();
442 if (val == (uint32_t)-1 || val / 2 != num_counters) {
443 fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: "
444 "mismatched number of counters (%d)\n",
445 filename, val);
446 return;
447 }
448
449 old_ctrs = malloc(sizeof(uint64_t) * num_counters);
450 for (i = 0; i < num_counters; ++i)
451 old_ctrs[i] = read_64bit_value();
452 }
453
454 cur_pos = save_cur_pos;
455
456 /* Counter #1 (arcs) tag */
457 write_32bit_value(GCOV_TAG_COUNTER_ARCS);
458 write_32bit_value(num_counters * 2);
459 for (i = 0; i < num_counters; ++i) {
460 counters[i] += (old_ctrs ? old_ctrs[i] : 0);
461 write_64bit_value(counters[i]);
462 }
463
464 free(old_ctrs);
465
466 #ifdef DEBUG_GCDAPROFILING
467 fprintf(stderr, "llvmgcda: %u arcs\n", num_counters);
468 for (i = 0; i < num_counters; ++i)
469 fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
470 #endif
471 }
472
473 COMPILER_RT_VISIBILITY
llvm_gcda_summary_info()474 void llvm_gcda_summary_info() {
475 uint32_t runs = 1;
476 static uint32_t run_counted = 0; // We only want to increase the run count once.
477 uint32_t val = 0;
478 uint64_t save_cur_pos = cur_pos;
479
480 if (!output_file) return;
481
482 val = read_32bit_value();
483
484 if (val != (uint32_t)-1) {
485 /* There are counters present in the file. Merge them. */
486 if (val != (gcov_version >= 90 ? GCOV_TAG_OBJECT_SUMMARY
487 : GCOV_TAG_PROGRAM_SUMMARY)) {
488 fprintf(stderr,
489 "profiling: %s: cannot merge previous run count: "
490 "corrupt object tag (0x%08x)\n",
491 filename, val);
492 return;
493 }
494
495 val = read_32bit_value(); /* length */
496 uint32_t prev_runs;
497 if (gcov_version < 90) {
498 read_32bit_value();
499 read_32bit_value();
500 prev_runs = read_32bit_value();
501 } else {
502 prev_runs = read_32bit_value();
503 read_32bit_value();
504 }
505 for (uint32_t i = gcov_version < 90 ? 3 : 2; i < val; ++i)
506 read_32bit_value();
507 /* Add previous run count to new counter, if not already counted before. */
508 runs = run_counted ? prev_runs : prev_runs + 1;
509 }
510
511 cur_pos = save_cur_pos;
512
513 if (gcov_version >= 90) {
514 write_32bit_value(GCOV_TAG_OBJECT_SUMMARY);
515 write_32bit_value(2);
516 write_32bit_value(runs);
517 write_32bit_value(0); // sum_max
518 } else {
519 // Before gcov 4.8 (r190952), GCOV_TAG_SUMMARY_LENGTH was 9. r190952 set
520 // GCOV_TAG_SUMMARY_LENGTH to 22. We simply use the smallest length which
521 // can make gcov read "Runs:".
522 write_32bit_value(GCOV_TAG_PROGRAM_SUMMARY);
523 write_32bit_value(3);
524 write_32bit_value(0);
525 write_32bit_value(0);
526 write_32bit_value(runs);
527 }
528
529 run_counted = 1;
530
531 #ifdef DEBUG_GCDAPROFILING
532 fprintf(stderr, "llvmgcda: %u runs\n", runs);
533 #endif
534 }
535
536 COMPILER_RT_VISIBILITY
llvm_gcda_end_file()537 void llvm_gcda_end_file() {
538 /* Write out EOF record. */
539 if (output_file) {
540 write_bytes("\0\0\0\0\0\0\0\0", 8);
541
542 if (new_file) {
543 fwrite(write_buffer, cur_pos, 1, output_file);
544 free(write_buffer);
545 } else {
546 unmap_file();
547 }
548
549 fflush(output_file);
550 lprofUnlockFd(fd);
551 fclose(output_file);
552 output_file = NULL;
553 write_buffer = NULL;
554 }
555 free(filename);
556
557 #ifdef DEBUG_GCDAPROFILING
558 fprintf(stderr, "llvmgcda: -----\n");
559 #endif
560 }
561
562 COMPILER_RT_VISIBILITY
llvm_register_writeout_function(fn_ptr fn)563 void llvm_register_writeout_function(fn_ptr fn) {
564 fn_list_insert(&writeout_fn_list, fn);
565 }
566
567 COMPILER_RT_VISIBILITY
llvm_writeout_files(void)568 void llvm_writeout_files(void) {
569 struct fn_node *curr = writeout_fn_list.head;
570
571 while (curr) {
572 if (curr->id == CURRENT_ID) {
573 curr->fn();
574 }
575 curr = curr->next;
576 }
577 }
578
579 #ifndef _WIN32
580 // __attribute__((destructor)) and destructors whose priorities are greater than
581 // 100 run before this function and can thus be tracked. The priority is
582 // compatible with GCC 7 onwards.
583 #if __GNUC__ >= 9
584 #pragma GCC diagnostic ignored "-Wprio-ctor-dtor"
585 #endif
586 __attribute__((destructor(100)))
587 #endif
llvm_writeout_and_clear(void)588 static void llvm_writeout_and_clear(void) {
589 llvm_writeout_files();
590 fn_list_remove(&writeout_fn_list);
591 }
592
593 COMPILER_RT_VISIBILITY
llvm_register_reset_function(fn_ptr fn)594 void llvm_register_reset_function(fn_ptr fn) {
595 fn_list_insert(&reset_fn_list, fn);
596 }
597
598 COMPILER_RT_VISIBILITY
llvm_delete_reset_function_list(void)599 void llvm_delete_reset_function_list(void) { fn_list_remove(&reset_fn_list); }
600
601 COMPILER_RT_VISIBILITY
llvm_reset_counters(void)602 void llvm_reset_counters(void) {
603 struct fn_node *curr = reset_fn_list.head;
604
605 while (curr) {
606 if (curr->id == CURRENT_ID) {
607 curr->fn();
608 }
609 curr = curr->next;
610 }
611 }
612
613 #if !defined(_WIN32)
614 COMPILER_RT_VISIBILITY
__gcov_fork()615 pid_t __gcov_fork() {
616 pid_t parent_pid = getpid();
617 pid_t pid = fork();
618
619 if (pid == 0) {
620 pid_t child_pid = getpid();
621 if (child_pid != parent_pid) {
622 // The pid changed so we've a fork (one could have its own fork function)
623 // Just reset the counters for this child process
624 // threads.
625 llvm_reset_counters();
626 }
627 }
628 return pid;
629 }
630 #endif
631
632 COMPILER_RT_VISIBILITY
llvm_gcov_init(fn_ptr wfn,fn_ptr rfn)633 void llvm_gcov_init(fn_ptr wfn, fn_ptr rfn) {
634 static int atexit_ran = 0;
635
636 if (wfn)
637 llvm_register_writeout_function(wfn);
638
639 if (rfn)
640 llvm_register_reset_function(rfn);
641
642 if (atexit_ran == 0) {
643 atexit_ran = 1;
644
645 /* Make sure we write out the data and delete the data structures. */
646 atexit(llvm_delete_reset_function_list);
647 #ifdef _WIN32
648 atexit(llvm_writeout_and_clear);
649 #endif
650 }
651 }
652
__gcov_dump(void)653 void __gcov_dump(void) {
654 for (struct fn_node *f = writeout_fn_list.head; f; f = f->next)
655 f->fn();
656 }
657
__gcov_reset(void)658 void __gcov_reset(void) {
659 for (struct fn_node *f = reset_fn_list.head; f; f = f->next)
660 f->fn();
661 }
662
663 #endif
664