[llvm] f8ad86c - [llvm-cov] Fix a bug about using `convert-for-testing` on multi-source object files

Gulfem Savrun Yeniceri via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 14 19:52:11 PDT 2023


Author: Yuhao Gu
Date: 2023-08-15T02:51:28Z
New Revision: f8ad86c23405168a8cd189590184fdcc296627e0

URL: https://github.com/llvm/llvm-project/commit/f8ad86c23405168a8cd189590184fdcc296627e0
DIFF: https://github.com/llvm/llvm-project/commit/f8ad86c23405168a8cd189590184fdcc296627e0.diff

LOG: [llvm-cov] Fix a bug about using `convert-for-testing` on multi-source object files

`llvm-cov convert-for-testing` is used to build the .covmapping files used in its regression tests. However the current implementation only works when there's only one source file in the mapping information data. If there are more than 1 source files, `llvm-cov convert-for-testing` can still produce a .covmapping file, but when read it back, `llvm-cov` will report:

```
error: Failed to load coverage: 'main.covmapping': Malformed coverage data
```

This is because the output .covmapping file doesn't have any mark to indicate the boundary between file records and function records, and current implementation jsut assume there's only one file record in the .covmapping file.

Changes to the code:

- Make `llvm-cov convert-for-testing` output a LEB128 number before file records to indicate its size.
- Change the testing format parsing code correspondingly.
- Update existing .covmapping files.

Reviewed By: gulfem

Differential Revision: https://reviews.llvm.org/D156611

Added: 
    

Modified: 
    llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
    llvm/test/tools/llvm-cov/Inputs/combine_expansions.covmapping
    llvm/test/tools/llvm-cov/Inputs/compilation_dir.covmapping
    llvm/test/tools/llvm-cov/Inputs/coverage_prefix_map/main.covmapping
    llvm/test/tools/llvm-cov/Inputs/deferred-regions.covmapping
    llvm/test/tools/llvm-cov/Inputs/dir-with-filtering.covmapping
    llvm/test/tools/llvm-cov/Inputs/double_dots.covmapping
    llvm/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping
    llvm/test/tools/llvm-cov/Inputs/ifdef.covmapping
    llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping
    llvm/test/tools/llvm-cov/Inputs/malformedRegions.covmapping
    llvm/test/tools/llvm-cov/Inputs/multiple-files.covmapping
    llvm/test/tools/llvm-cov/Inputs/multiple-files2.covmapping
    llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_1.covmapping
    llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_2.covmapping
    llvm/test/tools/llvm-cov/Inputs/multithreaded_report/main.covmapping
    llvm/test/tools/llvm-cov/Inputs/name_allowlist.covmapping
    llvm/test/tools/llvm-cov/Inputs/name_whitelist.covmapping
    llvm/test/tools/llvm-cov/Inputs/native_separators.covmapping
    llvm/test/tools/llvm-cov/Inputs/path_equivalence.covmapping
    llvm/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping
    llvm/test/tools/llvm-cov/Inputs/prevent_false_instantiations.covmapping
    llvm/test/tools/llvm-cov/Inputs/regionMarkers.covmapping
    llvm/test/tools/llvm-cov/Inputs/relative_dir/main.covmapping
    llvm/test/tools/llvm-cov/Inputs/report.covmapping
    llvm/test/tools/llvm-cov/Inputs/showExpansions.covmapping
    llvm/test/tools/llvm-cov/Inputs/showProjectSummary.covmapping
    llvm/test/tools/llvm-cov/Inputs/showTabsHTML.covmapping
    llvm/test/tools/llvm-cov/Inputs/sources_specified/main.covmapping
    llvm/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping
    llvm/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping
    llvm/tools/llvm-cov/TestingSupport.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
index 05737323314a8a..2f290b2a2dbf8d 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
@@ -886,41 +886,46 @@ loadTestingFormat(StringRef Data, StringRef CompilationDir) {
   if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
     return std::move(E);
   Data = Data.substr(ProfileNamesSize);
+
+  N = 0;
+  uint64_t CoverageMappingSize = decodeULEB128(Data.bytes_begin(), &N);
+  if (N > Data.size())
+    return make_error<CoverageMapError>(coveragemap_error::malformed);
+  Data = Data.substr(N);
+
   // Skip the padding bytes because coverage map data has an alignment of 8.
   size_t Pad = offsetToAlignedAddr(Data.data(), Align(8));
   if (Data.size() < Pad)
     return make_error<CoverageMapError>(coveragemap_error::malformed);
   Data = Data.substr(Pad);
-  if (Data.size() < sizeof(CovMapHeader))
+  if (Data.size() < CoverageMappingSize)
+    return make_error<CoverageMapError>(coveragemap_error::malformed);
+  StringRef CoverageMapping = Data.substr(0, CoverageMappingSize);
+  Data = Data.substr(CoverageMappingSize);
+
+  // Skip the padding bytes because coverage records data has an alignment of 8.
+  Pad = offsetToAlignedAddr(Data.data(), Align(8));
+  if (Data.size() < Pad)
     return make_error<CoverageMapError>(coveragemap_error::malformed);
-  auto const *CovHeader = reinterpret_cast<const CovMapHeader *>(
-      Data.substr(0, sizeof(CovMapHeader)).data());
+  Data = Data.substr(Pad);
+  BinaryCoverageReader::FuncRecordsStorage CoverageRecords =
+      MemoryBuffer::getMemBuffer(Data);
+
+  // Some extra checking.
+  if (CoverageMapping.size() < sizeof(CovMapHeader))
+    return make_error<CoverageMapError>(coveragemap_error::truncated);
+  auto const *CovHeader =
+      reinterpret_cast<const CovMapHeader *>(CoverageMapping.data());
   CovMapVersion Version =
       (CovMapVersion)CovHeader->getVersion<support::endianness::little>();
-  StringRef CoverageMapping;
-  BinaryCoverageReader::FuncRecordsStorage CoverageRecords;
   if (Version < CovMapVersion::Version4) {
-    CoverageMapping = Data;
-    if (CoverageMapping.empty())
-      return make_error<CoverageMapError>(coveragemap_error::truncated);
-    CoverageRecords = MemoryBuffer::getMemBuffer("");
-  } else {
-    uint32_t FilenamesSize =
-        CovHeader->getFilenamesSize<support::endianness::little>();
-    uint32_t CoverageMappingSize = sizeof(CovMapHeader) + FilenamesSize;
-    CoverageMapping = Data.substr(0, CoverageMappingSize);
-    if (CoverageMapping.empty())
-      return make_error<CoverageMapError>(coveragemap_error::truncated);
-    Data = Data.substr(CoverageMappingSize);
-    // Skip the padding bytes because coverage records data has an alignment
-    // of 8.
-    Pad = offsetToAlignedAddr(Data.data(), Align(8));
-    if (Data.size() < Pad)
+    if (CoverageRecords->getBufferSize() != 0)
       return make_error<CoverageMapError>(coveragemap_error::malformed);
-    CoverageRecords = MemoryBuffer::getMemBuffer(Data.substr(Pad));
+  } else {
     if (CoverageRecords->getBufferSize() == 0)
       return make_error<CoverageMapError>(coveragemap_error::truncated);
   }
+
   return BinaryCoverageReader::createCoverageReaderFromBuffer(
       CoverageMapping, std::move(CoverageRecords), std::move(ProfileNames),
       BytesInAddress, Endian, CompilationDir);

diff  --git a/llvm/test/tools/llvm-cov/Inputs/combine_expansions.covmapping b/llvm/test/tools/llvm-cov/Inputs/combine_expansions.covmapping
index 744bb291efabde..d46cb617cc2421 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/combine_expansions.covmapping and b/llvm/test/tools/llvm-cov/Inputs/combine_expansions.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/compilation_dir.covmapping b/llvm/test/tools/llvm-cov/Inputs/compilation_dir.covmapping
index 01879d7bd95290..1477d41be27b23 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/compilation_dir.covmapping and b/llvm/test/tools/llvm-cov/Inputs/compilation_dir.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/coverage_prefix_map/main.covmapping b/llvm/test/tools/llvm-cov/Inputs/coverage_prefix_map/main.covmapping
index 6f3062c20c5739..427cd102f89728 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/coverage_prefix_map/main.covmapping and b/llvm/test/tools/llvm-cov/Inputs/coverage_prefix_map/main.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/deferred-regions.covmapping b/llvm/test/tools/llvm-cov/Inputs/deferred-regions.covmapping
index 750dd81aff2196..977d4ef7b58098 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/deferred-regions.covmapping and b/llvm/test/tools/llvm-cov/Inputs/deferred-regions.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/dir-with-filtering.covmapping b/llvm/test/tools/llvm-cov/Inputs/dir-with-filtering.covmapping
index 0f52ba896ec317..406e2fac52bc30 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/dir-with-filtering.covmapping and b/llvm/test/tools/llvm-cov/Inputs/dir-with-filtering.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/double_dots.covmapping b/llvm/test/tools/llvm-cov/Inputs/double_dots.covmapping
index b03e19e03e877b..ba2f2ef3281f88 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/double_dots.covmapping and b/llvm/test/tools/llvm-cov/Inputs/double_dots.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping b/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping
index 5c3d67f24e1eb2..9d939cf74d49eb 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping and b/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/ifdef.covmapping b/llvm/test/tools/llvm-cov/Inputs/ifdef.covmapping
index 212a1bae83347f..8f114a52ff6ab5 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/ifdef.covmapping and b/llvm/test/tools/llvm-cov/Inputs/ifdef.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping b/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping
index 0b2458ae4fd001..e5eeb28097c769 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping and b/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/malformedRegions.covmapping b/llvm/test/tools/llvm-cov/Inputs/malformedRegions.covmapping
index 20d6abfffa73ed..58aceb1fd689db 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/malformedRegions.covmapping and b/llvm/test/tools/llvm-cov/Inputs/malformedRegions.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/multiple-files.covmapping b/llvm/test/tools/llvm-cov/Inputs/multiple-files.covmapping
index 3e229a8c301a75..eabb6112aaee13 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/multiple-files.covmapping and b/llvm/test/tools/llvm-cov/Inputs/multiple-files.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/multiple-files2.covmapping b/llvm/test/tools/llvm-cov/Inputs/multiple-files2.covmapping
index 770817a53806e4..f377c0843e414f 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/multiple-files2.covmapping and b/llvm/test/tools/llvm-cov/Inputs/multiple-files2.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_1.covmapping b/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_1.covmapping
index 641bdb441c24e9..a393cd6e9f1c96 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_1.covmapping and b/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_1.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_2.covmapping b/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_2.covmapping
index f26caea74803f5..46a939ed98a7cd 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_2.covmapping and b/llvm/test/tools/llvm-cov/Inputs/multiple_objects/use_2.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/multithreaded_report/main.covmapping b/llvm/test/tools/llvm-cov/Inputs/multithreaded_report/main.covmapping
index 75bd4cb760b8af..c597087c46df6d 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/multithreaded_report/main.covmapping and b/llvm/test/tools/llvm-cov/Inputs/multithreaded_report/main.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/name_allowlist.covmapping b/llvm/test/tools/llvm-cov/Inputs/name_allowlist.covmapping
index 7a7ab700f367c5..dc057c1f58c17a 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/name_allowlist.covmapping and b/llvm/test/tools/llvm-cov/Inputs/name_allowlist.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/name_whitelist.covmapping b/llvm/test/tools/llvm-cov/Inputs/name_whitelist.covmapping
index 6c067abd027997..a5f05b016af87e 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/name_whitelist.covmapping and b/llvm/test/tools/llvm-cov/Inputs/name_whitelist.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/native_separators.covmapping b/llvm/test/tools/llvm-cov/Inputs/native_separators.covmapping
index ce8d6bb7d9e3a8..840ffe893d1dab 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/native_separators.covmapping and b/llvm/test/tools/llvm-cov/Inputs/native_separators.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/path_equivalence.covmapping b/llvm/test/tools/llvm-cov/Inputs/path_equivalence.covmapping
index ea09bf3130ced9..17a2125983e75d 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/path_equivalence.covmapping and b/llvm/test/tools/llvm-cov/Inputs/path_equivalence.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping b/llvm/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping
index c4e1c805b93e1e..927db4dc523407 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping and b/llvm/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/prevent_false_instantiations.covmapping b/llvm/test/tools/llvm-cov/Inputs/prevent_false_instantiations.covmapping
index bbaefe5a75f624..78609d1cf9492f 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/prevent_false_instantiations.covmapping and b/llvm/test/tools/llvm-cov/Inputs/prevent_false_instantiations.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/regionMarkers.covmapping b/llvm/test/tools/llvm-cov/Inputs/regionMarkers.covmapping
index af920c2d169a59..09e02162b4f6bd 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/regionMarkers.covmapping and b/llvm/test/tools/llvm-cov/Inputs/regionMarkers.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/relative_dir/main.covmapping b/llvm/test/tools/llvm-cov/Inputs/relative_dir/main.covmapping
index 665aa963ebd366..9211ab65eddbc6 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/relative_dir/main.covmapping and b/llvm/test/tools/llvm-cov/Inputs/relative_dir/main.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/report.covmapping b/llvm/test/tools/llvm-cov/Inputs/report.covmapping
index f9858ed272b90f..39d17a966b1034 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/report.covmapping and b/llvm/test/tools/llvm-cov/Inputs/report.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/showExpansions.covmapping b/llvm/test/tools/llvm-cov/Inputs/showExpansions.covmapping
index d4eb527660662e..d859eed3ac66b1 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/showExpansions.covmapping and b/llvm/test/tools/llvm-cov/Inputs/showExpansions.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/showProjectSummary.covmapping b/llvm/test/tools/llvm-cov/Inputs/showProjectSummary.covmapping
index d95caf27f2bdea..4d5b4e7db0e6ab 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/showProjectSummary.covmapping and b/llvm/test/tools/llvm-cov/Inputs/showProjectSummary.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/showTabsHTML.covmapping b/llvm/test/tools/llvm-cov/Inputs/showTabsHTML.covmapping
index 96df49dcc591a7..37d4174652f9e4 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/showTabsHTML.covmapping and b/llvm/test/tools/llvm-cov/Inputs/showTabsHTML.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/sources_specified/main.covmapping b/llvm/test/tools/llvm-cov/Inputs/sources_specified/main.covmapping
index 440d59a74ca530..a73971e6fb1086 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/sources_specified/main.covmapping and b/llvm/test/tools/llvm-cov/Inputs/sources_specified/main.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping b/llvm/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping
index b604e0604ad3cc..ebcbd2ceba83cb 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping and b/llvm/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping 
diff er

diff  --git a/llvm/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping b/llvm/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping
index b2c198a8fe0e5f..c46cf26581c335 100644
Binary files a/llvm/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping and b/llvm/test/tools/llvm-cov/Inputs/zeroFunctionFile.covmapping 
diff er

diff  --git a/llvm/tools/llvm-cov/TestingSupport.cpp b/llvm/tools/llvm-cov/TestingSupport.cpp
index 289a1621660b06..0d7874a267af0a 100644
--- a/llvm/tools/llvm-cov/TestingSupport.cpp
+++ b/llvm/tools/llvm-cov/TestingSupport.cpp
@@ -111,6 +111,7 @@ int convertForTestingMain(int argc, const char *argv[]) {
   encodeULEB128(ProfileNamesData.size(), OS);
   encodeULEB128(ProfileNamesAddress, OS);
   OS << ProfileNamesData;
+  encodeULEB128(CoverageMappingData.size(), OS);
   // Coverage mapping data is expected to have an alignment of 8.
   for (unsigned Pad = offsetToAlignment(OS.tell(), Align(8)); Pad; --Pad)
     OS.write(uint8_t(0));


        


More information about the llvm-commits mailing list