Skip to content

Commit a064157

Browse files
committed
[Coverage] Fix UB in use of the CoverageMappingWriter API
Passing {FileID} into the CoverageMappingWriter constructor causes UB. The ArrayRef constructed from the initializer list becomes invalid once the constructor returns, because the lifetime of the initializer list expires. Use an alternate ArrayRef constructor which outlives the CoverageMappingWriter instance. This commit also re-reenables coverage_smoke.swift. rdar://problem/29591622
1 parent 20a023b commit a064157

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

lib/IRGen/GenCoverage.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ void IRGenModule::emitCoverageMapping() {
9797
MR.Counter, /*FileID=*/0, MR.StartLine, MR.StartCol, MR.EndLine,
9898
MR.EndCol));
9999
// Append each function's regions into the encoded buffer.
100-
llvm::coverage::CoverageMappingWriter W({FileID}, M.getExpressions(),
101-
Regions);
100+
ArrayRef<unsigned> VirtualFileMapping(FileID);
101+
llvm::coverage::CoverageMappingWriter W(VirtualFileMapping,
102+
M.getExpressions(), Regions);
102103
W.write(OS);
103104

104105
std::string NameValue = llvm::getPGOFuncName(

test/SILGen/coverage_smoke.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// REQUIRES: rdar://29591622
21
// RUN: rm -rf %t && mkdir -p %t
32
// RUN: %target-build-swift %s -profile-generate -profile-coverage-mapping -Xfrontend -disable-incremental-llvm-codegen -o %t/main
43
// RUN: env LLVM_PROFILE_FILE=%t/default.profraw %target-run %t/main

0 commit comments

Comments
 (0)