@@ -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 )
0 commit comments