Skip to content

Commit 7014a10

Browse files
committed
[profile] Skip mmap() if there are no counters
If there are no counters, an mmap() of the counters section would fail due to the size argument being too small (EINVAL). rdar://78175925 Differential Revision: https://reviews.llvm.org/D102735
1 parent 84ae1cf commit 7014a10

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -638,34 +638,38 @@ static void initializeProfileForContinuousMode(void) {
638638
}
639639
}
640640

641-
int Fileno = fileno(File);
642-
643-
/* Determine how much padding is needed before/after the counters and after
644-
* the names. */
645-
uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
646-
PaddingBytesAfterNames;
647-
__llvm_profile_get_padding_sizes_for_counters(
648-
DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
649-
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
650-
651-
uint64_t PageAlignedCountersLength =
652-
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters;
653-
uint64_t FileOffsetToCounters =
654-
CurrentFileOffset + sizeof(__llvm_profile_header) +
655-
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters;
656-
657-
uint64_t *CounterMmap = (uint64_t *)mmap(
658-
(void *)CountersBegin, PageAlignedCountersLength, PROT_READ | PROT_WRITE,
659-
MAP_FIXED | MAP_SHARED, Fileno, FileOffsetToCounters);
660-
if (CounterMmap != CountersBegin) {
661-
PROF_ERR(
662-
"Continuous counter sync mode is enabled, but mmap() failed (%s).\n"
663-
" - CountersBegin: %p\n"
664-
" - PageAlignedCountersLength: %" PRIu64 "\n"
665-
" - Fileno: %d\n"
666-
" - FileOffsetToCounters: %" PRIu64 "\n",
667-
strerror(errno), CountersBegin, PageAlignedCountersLength, Fileno,
668-
FileOffsetToCounters);
641+
/* mmap() the profile counters so long as there is at least one counter.
642+
* If there aren't any counters, mmap() would fail with EINVAL. */
643+
if (CountersSize > 0) {
644+
int Fileno = fileno(File);
645+
646+
/* Determine how much padding is needed before/after the counters and after
647+
* the names. */
648+
uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
649+
PaddingBytesAfterNames;
650+
__llvm_profile_get_padding_sizes_for_counters(
651+
DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
652+
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
653+
654+
uint64_t PageAlignedCountersLength =
655+
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters;
656+
uint64_t FileOffsetToCounters =
657+
CurrentFileOffset + sizeof(__llvm_profile_header) +
658+
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters;
659+
660+
uint64_t *CounterMmap = (uint64_t *)mmap(
661+
(void *)CountersBegin, PageAlignedCountersLength, PROT_READ | PROT_WRITE,
662+
MAP_FIXED | MAP_SHARED, Fileno, FileOffsetToCounters);
663+
if (CounterMmap != CountersBegin) {
664+
PROF_ERR(
665+
"Continuous counter sync mode is enabled, but mmap() failed (%s).\n"
666+
" - CountersBegin: %p\n"
667+
" - PageAlignedCountersLength: %" PRIu64 "\n"
668+
" - Fileno: %d\n"
669+
" - FileOffsetToCounters: %" PRIu64 "\n",
670+
strerror(errno), CountersBegin, PageAlignedCountersLength, Fileno,
671+
FileOffsetToCounters);
672+
}
669673
}
670674

671675
if (ProfileRequiresUnlock)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// REQUIRES: darwin
2+
3+
// RUN: echo "static void dead_code(void) {}" > %t.dso.c
4+
// RUN: %clang_profgen -fcoverage-mapping -O3 -dynamiclib -o %t.dso.dylib %t.dso.c
5+
// RUN: %clang_profgen -fcoverage-mapping -O3 -o %t.exe %s %t.dso.dylib
6+
// RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe 2>&1 | count 0
7+
// RUN: llvm-profdata show --counts --all-functions %t.profraw | FileCheck %s
8+
9+
// CHECK: Total functions: 1
10+
11+
int main() {}

0 commit comments

Comments
 (0)