1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/d8/cov.h"
6
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16
17 #define SHM_SIZE 0x100000
18 #define MAX_EDGES ((SHM_SIZE - 4) * 8)
19
20 struct shmem_data {
21 uint32_t num_edges;
22 unsigned char edges[];
23 };
24
25 struct shmem_data* shmem;
26
27 uint32_t *edges_start, *edges_stop;
28 uint32_t builtins_start;
29 uint32_t builtins_edge_count;
30
sanitizer_cov_reset_edgeguards()31 void sanitizer_cov_reset_edgeguards() {
32 uint32_t N = 0;
33 for (uint32_t* x = edges_start; x < edges_stop && N < MAX_EDGES; x++)
34 *x = ++N;
35 }
36
__sanitizer_cov_trace_pc_guard_init(uint32_t * start,uint32_t * stop)37 extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t* start,
38 uint32_t* stop) {
39 // Map the shared memory region
40 const char* shm_key = getenv("SHM_ID");
41 if (!shm_key) {
42 puts("[COV] no shared memory bitmap available, skipping");
43 shmem = (struct shmem_data*)malloc(SHM_SIZE);
44 } else {
45 int fd = shm_open(shm_key, O_RDWR, S_IREAD | S_IWRITE);
46 if (fd <= -1) {
47 fprintf(stderr, "[COV] Failed to open shared memory region\n");
48 _exit(-1);
49 }
50
51 shmem = (struct shmem_data*)mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE,
52 MAP_SHARED, fd, 0);
53 if (shmem == MAP_FAILED) {
54 fprintf(stderr, "[COV] Failed to mmap shared memory region\n");
55 _exit(-1);
56 }
57 }
58
59 edges_start = start;
60 edges_stop = stop;
61 sanitizer_cov_reset_edgeguards();
62
63 shmem->num_edges = static_cast<uint32_t>(stop - start);
64 builtins_start = 1 + shmem->num_edges;
65 printf("[COV] edge counters initialized. Shared memory: %s with %u edges\n",
66 shm_key, shmem->num_edges);
67 }
68
sanitizer_cov_count_discovered_edges()69 uint32_t sanitizer_cov_count_discovered_edges() {
70 uint32_t on_edges_counter = 0;
71 for (uint32_t i = 1; i < builtins_start; ++i) {
72 // TODO(ralbovsky): Can be optimized for fewer divisions.
73 if (shmem->edges[i / 8] & (1 << (i % 8))) {
74 ++on_edges_counter;
75 }
76 }
77 return on_edges_counter;
78 }
79
__sanitizer_cov_trace_pc_guard(uint32_t * guard)80 extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t* guard) {
81 // There's a small race condition here: if this function executes in two
82 // threads for the same edge at the same time, the first thread might disable
83 // the edge (by setting the guard to zero) before the second thread fetches
84 // the guard value (and thus the index). However, our instrumentation ignores
85 // the first edge (see libcoverage.c) and so the race is unproblematic.
86 uint32_t index = *guard;
87 shmem->edges[index / 8] |= 1 << (index % 8);
88 *guard = 0;
89 }
90
cov_init_builtins_edges(uint32_t num_edges)91 void cov_init_builtins_edges(uint32_t num_edges) {
92 if (num_edges + shmem->num_edges > MAX_EDGES) {
93 printf(
94 "[COV] Error: Insufficient amount of edges left for builtins "
95 "coverage.\n");
96 exit(-1);
97 }
98 builtins_edge_count = num_edges;
99 builtins_start = 1 + shmem->num_edges;
100 shmem->num_edges += builtins_edge_count;
101 printf("[COV] Additional %d edges for builtins initialized.\n", num_edges);
102 }
103
104 // This function is ran once per REPRL loop. In case of crash the coverage of
105 // crash will not be stored in shared memory. Therefore, it would be useful, if
106 // we could store these coverage information into shared memory in real time.
cov_update_builtins_basic_block_coverage(const std::vector<bool> & cov_map)107 void cov_update_builtins_basic_block_coverage(
108 const std::vector<bool>& cov_map) {
109 if (cov_map.size() != builtins_edge_count) {
110 printf("[COV] Error: Size of builtins cov map changed.\n");
111 exit(-1);
112 }
113 for (uint32_t i = 0; i < cov_map.size(); ++i) {
114 if (cov_map[i]) {
115 // TODO(ralbovsky): Can be optimized for fewer divisions.
116 shmem->edges[(i + builtins_start) / 8] |=
117 (1 << ((i + builtins_start) % 8));
118 }
119 }
120 }
121