From 6ebba0d28b7345fb517b058c8d3c0735b29e3b9d Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 05:02:43 +0100 Subject: [PATCH 1/6] fix(input): keep the order of uses in the cache entry of a .ll input A .ll input is parsed from its text by the run that fills the compile IR cache, and from the bitcode of its entry by the runs that read it. That bitcode held no order of uses, so the two modules could list the users of a value another way. The entry is now written with ShouldPreserveUseListOrder. Refs #279 --- src/analysis/InputPipeline.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/analysis/InputPipeline.cpp b/src/analysis/InputPipeline.cpp index 0828f54e..7d8dd904 100644 --- a/src/analysis/InputPipeline.cpp +++ b/src/analysis/InputPipeline.cpp @@ -1421,9 +1421,14 @@ namespace ctrace::stack::analysis std::string sourceIR; (void)readTextFile(inputPath, sourceIR); + // The order of each value's uses is written too. Some analyses depend on it: + // the first match of a walk, the order of a variable's reports. The reader + // otherwise rebuilds that order its own way, and a run that reads the cache + // would not walk the uses as the run that parsed the text did. std::string llvmBitcode; llvm::raw_string_ostream bitcodeStream(llvmBitcode); - llvm::WriteBitcodeToFile(*result.module, bitcodeStream); + llvm::WriteBitcodeToFile(*result.module, bitcodeStream, + /*ShouldPreserveUseListOrder=*/true); bitcodeStream.flush(); std::vector dependencySnapshots; From 1b4d0c877b7a70be03871cc2a667ec6d7b6ed0e4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 05:02:43 +0100 Subject: [PATCH 2/6] test(unit): check that a .ll input read from the cache keeps the order of uses The test loads a .ll whose callee is declared after its two callers, then loads it again from the cache, and compares the callers of the callee in the two modules. A third load, on the entry written again without the order of uses, gives another order: the file tells the two apart, and a load reads the entry. Refs #279 --- test/unit/analyzer_module_unit_tests.cpp | 104 +++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/test/unit/analyzer_module_unit_tests.cpp b/test/unit/analyzer_module_unit_tests.cpp index 82fa8b97..c2caf26e 100644 --- a/test/unit/analyzer_module_unit_tests.cpp +++ b/test/unit/analyzer_module_unit_tests.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -3287,6 +3288,108 @@ namespace return std::nullopt; } + /// A `.ll` given as input is parsed from its text by the run that fills the compile IR + /// cache, and from the bitcode of its cache entry by the next run. Analyses walk the uses of + /// a value, and can keep the first match, stop on a budget or report in the order of the + /// walk: the module read from the cache must list each value's uses in the order the module + /// parsed from the text does. + /// + /// `callee` is declared after its two callers, as Clang prints declarations. The text parser + /// meets its uses before its declaration; a bitcode reader knows every function before it + /// reads a body, and lists the callers another way unless it is told the order. The last + /// load checks it, on the entry written again without that order: it also shows that a load + /// reads the entry. + bool testCompileIRCacheKeepsUseOrderOfIRInput(TestReport& report) + { + char rootTemplate[] = "/tmp/ctrace-ir-use-order-XXXXXX"; + if (mkdtemp(rootTemplate) == nullptr) + { + report.expect(false, "Compile IR cache of a .ll input: no temporary directory"); + return false; + } + const std::filesystem::path root(rootTemplate); + const std::filesystem::path source = root / "callers.ll"; + std::ofstream(source) << "define i32 @first(i32 %x) {\n" + " %r = call i32 @callee(i32 %x)\n" + " ret i32 %r\n" + "}\n" + "\n" + "define i32 @second(i32 %x) {\n" + " %r = call i32 @callee(i32 %x)\n" + " ret i32 %r\n" + "}\n" + "\n" + "declare i32 @callee(i32)\n"; + + ctrace::stack::AnalysisConfig config; + config.compileIRCacheDir = (root / "cache").string(); + const auto load = [&](LoadedModule& out) + { + llvm::SMDiagnostic err; + out.module = ctrace::stack::analysis::detail::loadModuleForAnalysis( + source.string(), config, out.context, err, "known") + .module; + return out.module != nullptr; + }; + // The functions that call `callee`, in the order of its use list. + const auto callers = [](const llvm::Module& module) + { + std::vector names; + if (const llvm::Function* callee = module.getFunction("callee")) + { + for (const llvm::User* user : callee->users()) + { + if (const auto* call = llvm::dyn_cast(user)) + names.push_back(call->getFunction()->getName().str()); + } + } + return names; + }; + + LoadedModule parsed; + std::filesystem::path entry; + if (load(parsed)) + { + std::error_code ec; + for (const auto& file : std::filesystem::directory_iterator(root / "cache", ec)) + { + if (file.path().extension() == ".bc") + entry = file.path(); + } + } + report.expect( + !entry.empty(), + "Compile IR cache of a .ll input: the first load writes the module's bitcode"); + LoadedModule cached; + if (entry.empty() || !load(cached)) + { + report.expect(false, "Compile IR cache of a .ll input: the second load gives a module"); + std::filesystem::remove_all(root); + return false; + } + + const std::vector expected = callers(*parsed.module); + report.expect(expected.size() == 2, + "Compile IR cache of a .ll input: the parsed text lists the two callers"); + report.expect(callers(*cached.module) == expected, + "Compile IR cache of a .ll input: the module read from the cache lists the " + "callers of a function as the parsed text does"); + + { + std::error_code ec; + llvm::raw_fd_ostream bitcode(entry.string(), ec); + if (!ec) + llvm::WriteBitcodeToFile(*parsed.module, bitcode); + } + LoadedModule unordered; + report.expect(load(unordered) && callers(*unordered.module) != expected, + "Compile IR cache of a .ll input: an entry written without the order of " + "uses lists the callers another way"); + + std::filesystem::remove_all(root); + return report.failures == 0; + } + /// The compile IR cache records the coretrace-compiler revision of the build. A build that /// does not know it (coretrace-compiler.cmake) must neither read the cache, whose entries /// could come from any compiler, nor write it, with entries no build could check. @@ -3710,6 +3813,7 @@ int main(int argc, char** argv) (void)testLocationResolver(repoRoot, report); (void)testReachabilityService(repoRoot, report); (void)testModulePreparationService(repoRoot, report); + (void)testCompileIRCacheKeepsUseOrderOfIRInput(report); (void)testIntRangeFacts(repoRoot, report); (void)testEveryReportedCweIsScored(repoRoot, report); (void)testAnalysisReportContract(repoRoot, report); From 5f71208dc95e88b3e6214f195941dd204e21a236 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 05:02:43 +0100 Subject: [PATCH 3/6] docs(changelog): note the order of uses in the cache of a .ll input Refs #279 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2ad54ff..67d36631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,10 @@ Notable changes are grouped by release. See [all releases](https://github.com/Co assigned from one another in a cycle that reaches two different arrays now resolve to no array, as any pointer with several possible arrays does, where each used to be reported against its own array. +- With the compile IR cache, a `.ll` input read from the cache lists each value's uses in the + order the run that parsed its text had (#279). The entry's bitcode holds that order; it was + written without it, and the two runs could list the callers of a function, or the users of + a global variable, another way. No diagnostic was seen to change. - Added an authors file and linked the repository documents from the README. - SARIF rules carry a title (`shortDescription`) and a description (`fullDescription`) instead of their id, and `describeRule` (`RuleCatalog.hpp`) gives them to programs linking the analyzer. From c9825060c8a023bfd51f52ffe39776d227861586 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 08:49:41 +0100 Subject: [PATCH 4/6] fix(input): move the compile IR cache to schema v5 The entry of a .ll input now holds the order of each value's uses. An entry that a build of schema v4 wrote does not, and would be reused as long as its source is unchanged. The key and the metadata of an entry hold the schema: under v5, an earlier entry is not looked up, and the first run writes a new one. Refs #279 --- src/analysis/InputPipeline.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/analysis/InputPipeline.cpp b/src/analysis/InputPipeline.cpp index 7d8dd904..522fca8a 100644 --- a/src/analysis/InputPipeline.cpp +++ b/src/analysis/InputPipeline.cpp @@ -197,7 +197,9 @@ namespace ctrace::stack::analysis // v4: entries record the compiler that produced them (below). Builds of v3 ignore those // fields and would reuse such an entry; under v4, they do not even look it up, since the // key holds the schema. - constexpr llvm::StringLiteral kCompileIRCacheSchema = "compile-ir-cache-v4"; + // v5: the entry of a `.ll` input holds the order of each value's uses (#279). An entry of + // v4 does not, and a build of v5 does not look it up: the first run writes it again. + constexpr llvm::StringLiteral kCompileIRCacheSchema = "compile-ir-cache-v5"; // The compiler that produced an entry: compile IR depends on it as much as on the // command. An entry another coretrace-compiler revision or LLVM version produced is From a3edac9544ef7e47f30a133951d3d5f218ccb5a4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 08:49:41 +0100 Subject: [PATCH 5/6] test(unit): check that a .ll entry of the previous cache schema is not reused The entry written without the order of uses is recorded under schema v4, as the builds that wrote no order left it. The next load must give the callers in the order of the parsed text, and leave the entry under the current schema. Refs #279 --- test/unit/analyzer_module_unit_tests.cpp | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/unit/analyzer_module_unit_tests.cpp b/test/unit/analyzer_module_unit_tests.cpp index c2caf26e..c009661f 100644 --- a/test/unit/analyzer_module_unit_tests.cpp +++ b/test/unit/analyzer_module_unit_tests.cpp @@ -3299,6 +3299,9 @@ namespace /// reads a body, and lists the callers another way unless it is told the order. The last /// load checks it, on the entry written again without that order: it also shows that a load /// reads the entry. + /// + /// An entry without the order is what a build of the cache schema v4 wrote. Recorded under + /// that schema, it must not be reused: the load parses the text and writes the entry again. bool testCompileIRCacheKeepsUseOrderOfIRInput(TestReport& report) { char rootTemplate[] = "/tmp/ctrace-ir-use-order-XXXXXX"; @@ -3386,6 +3389,42 @@ namespace "Compile IR cache of a .ll input: an entry written without the order of " "uses lists the callers another way"); + // The schema the entry records, which becomes `replacement` when one is given. + const std::filesystem::path meta = std::filesystem::path(entry).replace_extension(".json"); + const auto schema = [&](const char* replacement) -> std::string + { + std::ostringstream text; + text << std::ifstream(meta).rdbuf(); + llvm::Expected record = llvm::json::parse(text.str()); + if (!record) + { + llvm::consumeError(record.takeError()); + return {}; + } + llvm::json::Object* object = record->getAsObject(); + if (object == nullptr) + return {}; + const std::string recorded = object->getString("schema").value_or("").str(); + if (replacement != nullptr) + { + (*object)["schema"] = replacement; + std::string written; + llvm::raw_string_ostream out(written); + out << *record; + out.flush(); + std::ofstream(meta, std::ios::trunc) << written; + } + return recorded; + }; + const std::string current = schema("compile-ir-cache-v4"); + LoadedModule rebuilt; + report.expect(load(rebuilt) && callers(*rebuilt.module) == expected, + "Compile IR cache of a .ll input: an entry of the schema that held no " + "order is not reused"); + report.expect(!current.empty() && schema(nullptr) == current, + "Compile IR cache of a .ll input: the load writes that entry again, under " + "the current schema"); + std::filesystem::remove_all(root); return report.failures == 0; } From 5a32343d7263ca110afc36306fd2976fc8daa054 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 08:49:41 +0100 Subject: [PATCH 6/6] docs(changelog): note that the cache is filled again once Refs #279 --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67d36631..69345ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,10 @@ Notable changes are grouped by release. See [all releases](https://github.com/Co - With the compile IR cache, a `.ll` input read from the cache lists each value's uses in the order the run that parsed its text had (#279). The entry's bitcode holds that order; it was written without it, and the two runs could list the callers of a function, or the users of - a global variable, another way. No diagnostic was seen to change. + a global variable, another way. No diagnostic was seen to change. The cache schema changes + to `compile-ir-cache-v5`: entries written by an earlier build are no longer looked up, so + the first run after the update compiles or parses each input once more and writes its + entry again. The earlier entries stay in the cache directory, unused. - Added an authors file and linked the repository documents from the README. - SARIF rules carry a title (`shortDescription`) and a description (`fullDescription`) instead of their id, and `describeRule` (`RuleCatalog.hpp`) gives them to programs linking the analyzer.