Author: Manuel Carrasco
Date: 2025-12-16T10:11:39Z
New Revision: 3ed5c191c78359a2cb17528fc8724410415f47d1

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

LOG: [sancov] Add -diff and -union options to compute set difference and union 
of sancov files  (#171191)

Add two new actions for `.sancov` files:

`-diff`: Computes the difference between two `.sancov` files (A - B) and
writes the result to a new file. Requires exactly two inputs and an
`--output` option. The output preserves the binary format (magic number
and bitness) of the first input.

`-union`: Computes the union of one or more `.sancov` files and writes
the result to a new file. Requires at least one input and an `--output`
option. The output preserves the binary format of the first input.

A warning is shown if input files differ in bitness (32-bit vs 64-bit),
but the operation proceeds using the first file’s bitness.

Added: 
    llvm/test/tools/sancov/Inputs/dummy-32bits.0.sancov
    llvm/test/tools/sancov/diff-different-bitness.test
    llvm/test/tools/sancov/diff-different-files.test
    llvm/test/tools/sancov/diff-same-file.test
    llvm/test/tools/sancov/union-different-bitness.test
    llvm/test/tools/sancov/union-different-files.test
    llvm/test/tools/sancov/union-same-file.test

Modified: 
    clang/docs/SanitizerCoverage.rst
    llvm/tools/sancov/Opts.td
    llvm/tools/sancov/sancov.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/SanitizerCoverage.rst 
b/clang/docs/SanitizerCoverage.rst
index 23720e542e4e9..4ab2d09366f4f 100644
--- a/clang/docs/SanitizerCoverage.rst
+++ b/clang/docs/SanitizerCoverage.rst
@@ -563,18 +563,39 @@ Sancov matches these files using module names and 
binaries file names.
 
 .. code-block:: console
 
-    USAGE: sancov [options] <action> (<binary file>|<.sancov file>)...
-
-    Action (required)
-      -print                    - Print coverage addresses
-      -covered-functions        - Print all covered functions.
-      -not-covered-functions    - Print all not covered functions.
-      -symbolize                - Symbolizes the report.
-
-    Options
-      -blocklist=<string>         - Blocklist file (sanitizer blocklist 
format).
-      -demangle                   - Print demangled function name.
-      -strip_path_prefix=<string> - Strip this prefix from file paths in 
reports
+    USAGE: sancov [options] <action> <binary files...> <.sancov files...> 
<.symcov files...>
+
+    Action (required):
+      -covered-functions     Print all covered funcions.
+      -
diff                   Compute 
diff erence between two sancov files (A - B) and write to the new output sancov 
file
+      -html-report           REMOVED. Use -symbolize & 
coverage-report-server.py.
+      -merge                 Merges reports.
+      -not-covered-functions Print all not covered funcions.
+      -print-coverage-pcs    Print coverage instrumentation points addresses.
+      -print-coverage-stats  Print coverage statistics.
+      -print                 Print coverage addresses
+      -symbolize             Produces a symbolized JSON report from binary 
report.
+      -union                 Compute union of multiple sancov files and write 
to the new output sancov file
+
+    Generic Options:
+      -help    Display this help
+      -h       Alias for --help
+      -version Display the version
+      -v       Alias for --version
+
+    OPTIONS:
+      -demangle=0          Alias for --no-demangle
+      -demangle            Demangle function names
+      -ignorelist=<string> Ignorelist file (sanitizer ignorelist format)
+      -no-demangle         Do not demangle function names
+      -no-skip-dead-files  List dead source files in reports
+      -output=<string>     Output file for 
diff  and union actions
+      -skip-dead-files=0   Alias for --no-skip-dead-files
+      -skip-dead-files     Do not list dead source files in reports
+      -strip_path_prefix=<string>
+                          Strip this prefix from files paths in reports
+      -use_default_ignorelist=0
+                          Alias for --no-use_default_ignore_list
 
 
 Coverage Reports

diff  --git a/llvm/test/tools/sancov/Inputs/dummy-32bits.0.sancov 
b/llvm/test/tools/sancov/Inputs/dummy-32bits.0.sancov
new file mode 100644
index 0000000000000..fee9a125ef08b
Binary files /dev/null and 
b/llvm/test/tools/sancov/Inputs/dummy-32bits.0.sancov 
diff er

diff  --git a/llvm/test/tools/sancov/
diff -
diff erent-bitness.test b/llvm/test/tools/sancov/
diff -
diff erent-bitness.test
new file mode 100644
index 0000000000000..506db06a552a5
--- /dev/null
+++ b/llvm/test/tools/sancov/
diff -
diff erent-bitness.test
@@ -0,0 +1,6 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -
diff  --output=%t.out.sancov %p/Inputs/dummy-32bits.0.sancov 
%p/Inputs/test-linux_x86_64.0.sancov 2>&1 | FileCheck %s 
+
+CHECK: Input files have 
diff erent bitness
+CHECK: 64-bit addresses will be truncated to 32 bits. This may result in data 
loss.

diff  --git a/llvm/test/tools/sancov/
diff -
diff erent-files.test b/llvm/test/tools/sancov/
diff -
diff erent-files.test
new file mode 100644
index 0000000000000..db46593099e74
--- /dev/null
+++ b/llvm/test/tools/sancov/
diff -
diff erent-files.test
@@ -0,0 +1,7 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -
diff  --output=%t.out.sancov %p/Inputs/test-linux_x86_64.1.sancov 
%p/Inputs/test-linux_x86_64.0.sancov
+RUN: sancov -print %t.out.sancov | FileCheck %s 
+
+CHECK: 0x4e14c2
+CHECK: 0x4e178c

diff  --git a/llvm/test/tools/sancov/
diff -same-file.test b/llvm/test/tools/sancov/
diff -same-file.test
new file mode 100644
index 0000000000000..fd3b5de79d9a6
--- /dev/null
+++ b/llvm/test/tools/sancov/
diff -same-file.test
@@ -0,0 +1,6 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -
diff  --output=%t.out.sancov %p/Inputs/test-linux_x86_64.0.sancov 
%p/Inputs/test-linux_x86_64.0.sancov
+RUN: sancov -print %t.out.sancov | FileCheck %s --allow-empty 
--check-prefix=EMPTY
+
+EMPTY-NOT: {{.}} 

diff  --git a/llvm/test/tools/sancov/union-
diff erent-bitness.test b/llvm/test/tools/sancov/union-
diff erent-bitness.test
new file mode 100644
index 0000000000000..fa37451a59b71
--- /dev/null
+++ b/llvm/test/tools/sancov/union-
diff erent-bitness.test
@@ -0,0 +1,6 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -union --output=%t.out.sancov %p/Inputs/dummy-32bits.0.sancov 
%p/Inputs/test-linux_x86_64.0.sancov 2>&1 | FileCheck %s 
+
+CHECK: Input files have 
diff erent bitness
+CHECK: 64-bit addresses will be truncated to 32 bits. This may result in data 
loss.

diff  --git a/llvm/test/tools/sancov/union-
diff erent-files.test b/llvm/test/tools/sancov/union-
diff erent-files.test
new file mode 100644
index 0000000000000..47dafc97ad3c2
--- /dev/null
+++ b/llvm/test/tools/sancov/union-
diff erent-files.test
@@ -0,0 +1,11 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -union --output=%t.out.sancov %p/Inputs/test-linux_x86_64.1.sancov 
%p/Inputs/test-linux_x86_64.0.sancov
+RUN: sancov -print %t.out.sancov | FileCheck %s 
+
+CHECK: 0x4e1472
+CHECK: 0x4e14c2
+CHECK: 0x4e1520
+CHECK: 0x4e1553
+CHECK: 0x4e1586
+CHECK: 0x4e178c

diff  --git a/llvm/test/tools/sancov/union-same-file.test 
b/llvm/test/tools/sancov/union-same-file.test
new file mode 100644
index 0000000000000..e7991d24fb7ac
--- /dev/null
+++ b/llvm/test/tools/sancov/union-same-file.test
@@ -0,0 +1,10 @@
+REQUIRES: x86-registered-target && host-byteorder-little-endian
+RUN: rm -f %t.out.sancov
+RUN: sancov -union --output=%t.out.sancov %p/Inputs/test-linux_x86_64.0.sancov 
%p/Inputs/test-linux_x86_64.0.sancov
+RUN: sancov -print %t.out.sancov | FileCheck %s
+
+CHECK: 0x4e132b
+CHECK: 0x4e1472
+CHECK: 0x4e1520
+CHECK: 0x4e1553
+CHECK: 0x4e1586

diff  --git a/llvm/tools/sancov/Opts.td b/llvm/tools/sancov/Opts.td
index 2e8af81b2a40d..411d08a033050 100644
--- a/llvm/tools/sancov/Opts.td
+++ b/llvm/tools/sancov/Opts.td
@@ -22,6 +22,10 @@ def : Flag<["-"], "v">, Alias<version>, HelpText<"Alias for 
--version">, Group<g
 def action_grp : OptionGroup<"Action">, HelpText<"Action (required)">;
 def print : F<"print", "Print coverage addresses">,
   Group<action_grp>;
+def 
diff  : F<"
diff ", "Compute 
diff erence between two sancov files (A - B) and write to the new output sancov 
file">,
+  Group<action_grp>;
+def union_files : F<"union", "Compute union of multiple sancov files and write 
to the new output sancov file">,
+  Group<action_grp>;
 def printCoveragePcs : F<"print-coverage-pcs", "Print coverage instrumentation 
points addresses.">,
   Group<action_grp>;
 def coveredFunctions : F<"covered-functions", "Print all covered funcions.">,
@@ -56,3 +60,7 @@ defm stripPathPrefix
 defm ignorelist
     : Eq<"ignorelist", "Ignorelist file (sanitizer ignorelist format)">,
       MetaVarName<"<string>">;
+
+defm output
+    : Eq<"output", "Output file for 
diff  and union actions">,
+      MetaVarName<"<string>">;

diff  --git a/llvm/tools/sancov/sancov.cpp b/llvm/tools/sancov/sancov.cpp
index a0585fad024c7..f2e71f97dda34 100644
--- a/llvm/tools/sancov/sancov.cpp
+++ b/llvm/tools/sancov/sancov.cpp
@@ -92,13 +92,15 @@ class SancovOptTable : public opt::GenericOptTable {
 
 enum ActionType {
   CoveredFunctionsAction,
+  DiffAction,
   HtmlReportAction,
   MergeAction,
   NotCoveredFunctionsAction,
   PrintAction,
   PrintCovPointsAction,
   StatsAction,
-  SymbolizeAction
+  SymbolizeAction,
+  UnionAction
 };
 
 static ActionType Action;
@@ -108,6 +110,7 @@ static bool ClSkipDeadFiles;
 static bool ClUseDefaultIgnorelist;
 static std::string ClStripPathPrefix;
 static std::string ClIgnorelist;
+static std::string ClOutputFile;
 
 static const char *const DefaultIgnorelistStr = "fun:__sanitizer_.*\n"
                                                 "src:/usr/include/.*\n"
@@ -132,14 +135,19 @@ static const Regex SymcovFileRegex(".*\\.symcov");
 // Contents of .sancov file: list of coverage point addresses that were
 // executed.
 struct RawCoverage {
-  explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs)
-      : Addrs(std::move(Addrs)) {}
+  explicit RawCoverage(std::unique_ptr<std::set<uint64_t>> Addrs,
+                       FileHeader Header)
+      : Addrs(std::move(Addrs)), Header(Header) {}
 
   // Read binary .sancov file.
   static ErrorOr<std::unique_ptr<RawCoverage>>
   read(const std::string &FileName);
 
+  // Write binary .sancov file.
+  static void write(const std::string &FileName, const RawCoverage &Coverage);
+
   std::unique_ptr<std::set<uint64_t>> Addrs;
+  FileHeader Header;
 };
 
 // Coverage point has an opaque Id and corresponds to multiple source 
locations.
@@ -264,7 +272,7 @@ RawCoverage::read(const std::string &FileName) {
   // to compactify the data.
   Addrs->erase(0);
 
-  return std::make_unique<RawCoverage>(std::move(Addrs));
+  return std::make_unique<RawCoverage>(std::move(Addrs), *Header);
 }
 
 // Print coverage addresses.
@@ -277,6 +285,34 @@ raw_ostream &operator<<(raw_ostream &OS, const RawCoverage 
&CoverageData) {
   return OS;
 }
 
+// Write coverage addresses in binary format.
+void RawCoverage::write(const std::string &FileName,
+                        const RawCoverage &Coverage) {
+  std::error_code EC;
+  raw_fd_ostream OS(FileName, EC, sys::fs::OF_None);
+  failIfError(EC);
+
+  OS.write(reinterpret_cast<const char *>(&Coverage.Header),
+           sizeof(Coverage.Header));
+
+  switch (Coverage.Header.Bitness) {
+  case Bitness64:
+    for (auto Addr : *Coverage.Addrs) {
+      uint64_t Addr64 = Addr;
+      OS.write(reinterpret_cast<const char *>(&Addr64), sizeof(Addr64));
+    }
+    break;
+  case Bitness32:
+    for (auto Addr : *Coverage.Addrs) {
+      uint32_t Addr32 = static_cast<uint32_t>(Addr);
+      OS.write(reinterpret_cast<const char *>(&Addr32), sizeof(Addr32));
+    }
+    break;
+  default:
+    fail("Unsupported bitness: " + std::to_string(Coverage.Header.Bitness));
+  }
+}
+
 static raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
   OS << "all-edges: " << Stats.AllPoints << "\n";
   OS << "cov-edges: " << Stats.CovPoints << "\n";
@@ -1015,6 +1051,88 @@ static void readAndPrintRawCoverage(const 
std::vector<std::string> &FileNames,
   }
 }
 
+static const char *bitnessToString(uint32_t Bitness) {
+  switch (Bitness) {
+  case Bitness64:
+    return "64-bit";
+  case Bitness32:
+    return "32-bit";
+  default:
+    fail("Unsupported bitness: " + std::to_string(Bitness));
+    return nullptr;
+  }
+}
+
+// Warn if two file headers have 
diff erent bitness.
+static void warnIfDifferentBitness(const FileHeader &Header1,
+                                   const FileHeader &Header2,
+                                   const std::string &File1Desc,
+                                   const std::string &File2Desc) {
+  if (Header1.Bitness != Header2.Bitness) {
+    errs() << "WARNING: Input files have 
diff erent bitness (" << File1Desc
+           << ": " << bitnessToString(Header1.Bitness) << ", " << File2Desc
+           << ": " << bitnessToString(Header2.Bitness)
+           << "). Using bitness from " << File1Desc << ".\n";
+
+    if (Header1.Bitness == Bitness32 && Header2.Bitness == Bitness64) {
+      errs() << "WARNING: 64-bit addresses will be truncated to 32 bits. "
+             << "This may result in data loss.\n";
+    }
+  }
+}
+
+// Compute 
diff erence between two coverage files (A - B) and write to output
+// file.
+static void 
diff RawCoverage(const std::string &FileA, const std::string &FileB,
+                            const std::string &OutputFile) {
+  auto CovA = RawCoverage::read(FileA);
+  failIfError(CovA);
+
+  auto CovB = RawCoverage::read(FileB);
+  failIfError(CovB);
+
+  const FileHeader &HeaderA = CovA.get()->Header;
+  const FileHeader &HeaderB = CovB.get()->Header;
+
+  warnIfDifferentBitness(HeaderA, HeaderB, FileA, FileB);
+
+  // Compute A - B
+  auto DiffAddrs = std::make_unique<std::set<uint64_t>>();
+  std::set_
diff erence(CovA.get()->Addrs->begin(), CovA.get()->Addrs->end(),
+                      CovB.get()->Addrs->begin(), CovB.get()->Addrs->end(),
+                      std::inserter(*DiffAddrs, DiffAddrs->end()));
+
+  RawCoverage DiffCov(std::move(DiffAddrs), HeaderA);
+  RawCoverage::write(OutputFile, DiffCov);
+}
+
+// Compute union of multiple coverage files and write to output file.
+static void unionRawCoverage(const std::vector<std::string> &InputFiles,
+                             const std::string &OutputFile) {
+  failIf(InputFiles.empty(), "union action requires at least one input file");
+
+  // Read the first file to get the header and initial coverage
+  auto UnionCov = RawCoverage::read(InputFiles[0]);
+  failIfError(UnionCov);
+
+  const FileHeader &UnionHeader = UnionCov.get()->Header;
+
+  for (size_t I = 1; I < InputFiles.size(); ++I) {
+    auto Cov = RawCoverage::read(InputFiles[I]);
+    failIfError(Cov);
+
+    const FileHeader &CurHeader = Cov.get()->Header;
+
+    warnIfDifferentBitness(UnionHeader, CurHeader, InputFiles[0],
+                           InputFiles[I]);
+
+    UnionCov.get()->Addrs->insert(Cov.get()->Addrs->begin(),
+                                  Cov.get()->Addrs->end());
+  }
+
+  RawCoverage::write(OutputFile, *UnionCov.get());
+}
+
 static std::unique_ptr<SymbolizedCoverage>
 merge(const std::vector<std::unique_ptr<SymbolizedCoverage>> &Coverages) {
   if (Coverages.empty())
@@ -1153,6 +1271,9 @@ static void parseArgs(int Argc, char **Argv) {
         "  Depending on chosen action the tool expects 
diff erent input files:\n"
         "    -print-coverage-pcs     - coverage-instrumented binary files\n"
         "    -print-coverage         - .sancov files\n"
+        "    -
diff                    - two .sancov files & --output option\n"
+        "    -union                  - one or more .sancov files & --output "
+        "option\n"
         "    <other actions>         - .sancov files & corresponding binary "
         "files, .symcov files\n");
     std::exit(0);
@@ -1176,6 +1297,12 @@ static void parseArgs(int Argc, char **Argv) {
     case OPT_print:
       Action = ActionType::PrintAction;
       break;
+    case OPT_
diff :
+      Action = ActionType::DiffAction;
+      break;
+    case OPT_union_files:
+      Action = ActionType::UnionAction;
+      break;
     case OPT_printCoveragePcs:
       Action = ActionType::PrintCovPointsAction;
       break;
@@ -1209,6 +1336,7 @@ static void parseArgs(int Argc, char **Argv) {
 
   ClStripPathPrefix = Args.getLastArgValue(OPT_stripPathPrefix_EQ);
   ClIgnorelist = Args.getLastArgValue(OPT_ignorelist_EQ);
+  ClOutputFile = Args.getLastArgValue(OPT_output_EQ);
 }
 
 int sancov_main(int Argc, char **Argv, const llvm::ToolContext &) {
@@ -1223,6 +1351,26 @@ int sancov_main(int Argc, char **Argv, const 
llvm::ToolContext &) {
     readAndPrintRawCoverage(ClInputFiles, outs());
     return 0;
   }
+  if (Action == DiffAction) {
+    // -
diff  requires exactly 2 input files and an output file.
+    failIf(ClInputFiles.size() != 2,
+           "
diff  action requires exactly 2 input sancov files");
+    failIf(
+        ClOutputFile.empty(),
+        "
diff  action requires --output option to specify output sancov file");
+    
diff RawCoverage(ClInputFiles[0], ClInputFiles[1], ClOutputFile);
+    return 0;
+  }
+  if (Action == UnionAction) {
+    // -union requires at least 1 input file and an output file.
+    failIf(ClInputFiles.empty(),
+           "union action requires at least one input sancov file");
+    failIf(
+        ClOutputFile.empty(),
+        "union action requires --output option to specify output sancov file");
+    unionRawCoverage(ClInputFiles, ClOutputFile);
+    return 0;
+  }
   if (Action == PrintCovPointsAction) {
     // -print-coverage-points doesn't need coverage files.
     for (const std::string &ObjFile : ClInputFiles) {
@@ -1257,6 +1405,8 @@ int sancov_main(int Argc, char **Argv, const 
llvm::ToolContext &) {
     errs() << "-html-report option is removed: "
               "use -symbolize & coverage-report-server.py instead\n";
     return 1;
+  case DiffAction:
+  case UnionAction:
   case PrintAction:
   case PrintCovPointsAction:
     llvm_unreachable("unsupported action");


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to