llvm.org GIT mirror llvm / 9f0d281
[Coverage] PR33517: Check for failure to load func records With PR33517, it became apparent that symbol table creation can fail when presented with malformed inputs. This patch makes that sort of error detectable, so llvm-cov etc. can fail more gracefully. Specifically, we now check that function records loaded from corrupted coverage mapping data are rejected, e.g when the recorded function name is garbage. Testing: check-{llvm,clang,profile}, some unit test updates. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305767 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 2 years ago
2 changed file(s) with 42 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
199199 const CoverageMappingRecord &Record,
200200 IndexedInstrProfReader &ProfileReader) {
201201 StringRef OrigFuncName = Record.FunctionName;
202 if (OrigFuncName.empty())
203 return make_error(coveragemap_error::malformed);
204
202205 if (Record.Filenames.empty())
203206 OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName);
204207 else
2525 return ::testing::AssertionSuccess();
2626 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
2727 << "\n";
28 }
29
30 static ::testing::AssertionResult ErrorEquals(coveragemap_error Expected,
31 Error E) {
32 coveragemap_error Found;
33 std::string FoundMsg;
34 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
35 Found = CME.get();
36 FoundMsg = CME.message();
37 });
38 if (Expected == Found)
39 return ::testing::AssertionSuccess();
40 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
2841 }
2942
3043 namespace llvm {
231244 return CoverageMapping::load(CoverageReaders, *ProfileReader);
232245 }
233246
234 void loadCoverageMapping(bool EmitFilenames = true) {
247 Error loadCoverageMapping(bool EmitFilenames = true) {
235248 readProfCounts();
236249 writeAndReadCoverageRegions(EmitFilenames);
237250 auto CoverageOrErr = readOutputFunctions();
238 ASSERT_TRUE(NoError(CoverageOrErr.takeError()));
251 if (!CoverageOrErr)
252 return CoverageOrErr.takeError();
239253 LoadedCoverage = std::move(CoverageOrErr.get());
254 return Error::success();
240255 }
241256 };
242257
303318 // in order to preserve that information during possible sorting of CMRs.
304319 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
305320
306 loadCoverageMapping();
321 NoError(loadCoverageMapping());
307322
308323 for (unsigned I = 0; I < N; ++I) {
309324 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
310325 ASSERT_TRUE(!Data.empty());
311326 EXPECT_EQ(I, Data.begin()->Line);
312327 }
328 }
329
330 TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
331 InstrProfRecord RecordFunc1("", 0x1234, {10});
332 NoError(ProfileWriter.addRecord(std::move(RecordFunc1)));
333 startFunction("", 0x1234);
334 addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
335 ErrorEquals(coveragemap_error::malformed, loadCoverageMapping());
313336 }
314337
315338 TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
324347 startFunction("func2", 0x2345);
325348 addCMR(Counter::getCounter(0), "bar", 2, 2, 6, 6);
326349
327 loadCoverageMapping();
350 NoError(loadCoverageMapping());
328351
329352 const auto FunctionRecords = LoadedCoverage->getCoveredFunctions();
330353 EXPECT_EQ(2, std::distance(FunctionRecords.begin(), FunctionRecords.end()));
368391 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
369392 addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
370393 addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
371 loadCoverageMapping();
394 NoError(loadCoverageMapping());
372395
373396 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
374397 std::vector Segments(Data.begin(), Data.end());
385408 TEST_P(CoverageMappingTest, uncovered_function) {
386409 startFunction("func", 0x1234);
387410 addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
388 loadCoverageMapping();
411 NoError(loadCoverageMapping());
389412
390413 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
391414 std::vector Segments(Data.begin(), Data.end());
398421 startFunction("func", 0x1234);
399422 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
400423 addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
401 loadCoverageMapping();
424 NoError(loadCoverageMapping());
402425
403426 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
404427 std::vector Segments(Data.begin(), Data.end());
416439 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
417440 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
418441 addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
419 loadCoverageMapping();
442 NoError(loadCoverageMapping());
420443
421444 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
422445 std::vector Segments(Data.begin(), Data.end());
435458 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
436459 addCMR(Counter::getCounter(1), "file1", 1, 1, 9, 9);
437460 addCMR(Counter::getCounter(2), "file1", 3, 3, 5, 5);
438 loadCoverageMapping();
461 NoError(loadCoverageMapping());
439462
440463 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
441464 std::vector Segments(Data.begin(), Data.end());
459482 addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
460483 addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
461484 addExpansionCMR("file1", "include1", 3, 3, 4, 4);
462 loadCoverageMapping();
485 NoError(loadCoverageMapping());
463486
464487 CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
465488 std::vector Segments(Data.begin(), Data.end());
482505 addExpansionCMR("file", "include1", 3, 1, 3, 5);
483506 addExpansionCMR("file", "include2", 3, 1, 3, 5);
484507
485 loadCoverageMapping();
508 NoError(loadCoverageMapping());
486509
487510 CoverageData Data = LoadedCoverage->getCoverageForFile("file");
488511 std::vector Segments(Data.begin(), Data.end());
499522
500523 startFunction("file1:func", 0x1234);
501524 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
502 loadCoverageMapping();
525 NoError(loadCoverageMapping());
503526
504527 std::vector Names;
505528 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
514537
515538 startFunction(":func", 0x1234);
516539 addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
517 loadCoverageMapping(/*EmitFilenames=*/false);
540 NoError(loadCoverageMapping(/*EmitFilenames=*/false));
518541
519542 std::vector Names;
520543 for (const auto &Func : LoadedCoverage->getCoveredFunctions())
537560 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
538561 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
539562
540 loadCoverageMapping();
563 NoError(loadCoverageMapping());
541564
542565 std::vector Instantiations =
543566 LoadedCoverage->getInstantiations("expanded");
552575 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
553576 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
554577
555 loadCoverageMapping();
578 NoError(loadCoverageMapping());
556579
557580 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
558581 std::vector Segments(Data.begin(), Data.end());
571594 startFunction("func", 0x1234);
572595 addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
573596
574 loadCoverageMapping();
597 NoError(loadCoverageMapping());
575598
576599 auto Funcs = LoadedCoverage->getCoveredFunctions();
577600 unsigned NumFuncs = std::distance(Funcs.begin(), Funcs.end());