Skip to content

Commit 1458b36

Browse files
executor: introduce cover_close()
Right now closing a kcov fd on Linux won't disable coverage, so further attempts to open an fd and enable coverage on the same thread will not work. Add cover_close() which will disable the coverage if necessary, and close the file descriptor.
1 parent a4acbeb commit 1458b36

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

executor/executor.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ struct cover_t {
376376
intptr_t pc_offset;
377377
// The coverage buffer has overflowed and we have truncated coverage.
378378
bool overflow;
379+
// True if cover_enable() was called for this object.
380+
bool enabled;
379381
};
380382

381383
struct thread_t {

executor/executor_linux.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ static void cover_open(cover_t* cov, bool extra)
119119
debug("pkey protection enabled\n");
120120
}
121121

122+
static void cover_close(cover_t* cov)
123+
{
124+
if (cov->fd == -1)
125+
fail("attempting to close an invalid cover fd");
126+
if (cov->enabled) {
127+
if (ioctl(cov->fd, KCOV_DISABLE, 0))
128+
fail("KCOV_DISABLE failed");
129+
cov->enabled = false;
130+
}
131+
close(cov->fd);
132+
cov->fd = -1;
133+
}
134+
122135
static void cover_protect(cover_t* cov)
123136
{
124137
if (pkeys_enabled && pkey_set(RESERVED_PKEY, PKEY_DISABLE_WRITE))
@@ -175,6 +188,7 @@ static void cover_enable(cover_t* cov, bool collect_comps, bool extra)
175188
if (!extra) {
176189
if (ioctl(cov->fd, KCOV_ENABLE, kcov_mode))
177190
exitf("cover enable write trace failed, mode=%d", kcov_mode);
191+
cov->enabled = true;
178192
return;
179193
}
180194
kcov_remote_arg<1> arg = {
@@ -187,6 +201,7 @@ static void cover_enable(cover_t* cov, bool collect_comps, bool extra)
187201
arg.handles[0] = kcov_remote_handle(KCOV_SUBSYSTEM_USB, procid + 1);
188202
if (ioctl(cov->fd, KCOV_REMOTE_ENABLE, &arg))
189203
exitf("remote cover enable write trace failed");
204+
cov->enabled = true;
190205
}
191206

192207
static void cover_reset(cover_t* cov)
@@ -321,7 +336,7 @@ static const char* setup_delay_kcov()
321336
munmap(cov.mmap_alloc_ptr, cov.mmap_alloc_size);
322337
}
323338
munmap(first, cov.mmap_alloc_size);
324-
close(cov.fd);
339+
cover_close(&cov);
325340
return error;
326341
}
327342

@@ -347,7 +362,7 @@ static const char* setup_kcov_reset_ioctl()
347362
error = "kernel does not support ioctl(KCOV_RESET_TRACE)";
348363
}
349364
cover_munmap(&cov);
350-
close(cov.fd);
365+
cover_close(&cov);
351366
return error;
352367
}
353368

0 commit comments

Comments
 (0)