Skip to content
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Notable changes are grouped by release. See [all releases](https://github.com/Co
- 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.
- With `--jobs` above 1, sources compile in parallel. Each compilation runs in its compile
command's directory through coretrace-compiler's `-working-directory`, instead of moving the
whole process there under a lock that let one source compile at a time. Compilations that pass
`-mllvm`, `-mdebug-pass`, `-mlimit-float-precision`, `-ftime-report` or `-fno-integrated-cc1`
still run one at a time. coretrace-compiler is pinned to a commit of
CoreTrace/coretrace-compiler#262, which gives each compilation a working directory of its own.

## v0.22.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ To generate `compile_commands.json` with CMake, configure with

If analysis feels slow, `--compdb-fast` disables heavy flags (optimizations,
sanitizers, profiling) while keeping include paths and macros.
For multi-file runs, `--jobs=<N|auto>` parallelizes input loading; with cross-TU enabled it also parallelizes summary construction.
For multi-file runs, `--jobs=<N|auto>` parallelizes input loading, compilations included; with cross-TU enabled it also parallelizes summary construction.
`--compile-ir-cache-dir=<path>` reuses compiled LLVM IR for unchanged translation units
based on source/dependency stamps, which reduces repeated C/C++ frontend cost across runs.
IR that another compiler produced (another coretrace-compiler revision or LLVM version) is compiled again.
Expand Down
2 changes: 1 addition & 1 deletion cmake/compiler/coretrace-compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ endif()
FetchContent_Declare(
cc
GIT_REPOSITORY https://github.com/CoreTrace/coretrace-compiler.git
GIT_TAG b4ad33638e9020a93f052a7a299e4105bd48f975 # v0.10.0
GIT_TAG 8a99cf3d7be21dd6e15c1cc8f9523c685fd307ef # CoreTrace/coretrace-compiler#262
)
FetchContent_MakeAvailable(cc)

Expand Down
141 changes: 80 additions & 61 deletions src/analysis/InputPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <iostream>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <sstream>
#include <string_view>
#include <system_error>
#include <unordered_set>
#include <vector>
Expand All @@ -40,15 +42,53 @@ namespace ctrace::stack::analysis
{
namespace
{
// compilerlib drives an in-process clang CompilerInstance. clang/LLVM keep global
// mutable state (target registry, cl options, timers); two worker threads compiling
// at once hung intermittently on the first compile of a run. Compilation is
// serialised here; module analysis stays parallel, and the IR cache makes the
// compile step cheap on repeated runs. A compile that needs its compile command's
// directory changes the working directory of the whole process: it holds the same
// lock from before that change until it is undone, so no other compile runs in a
// directory that is not its own, and two changes never interleave.
std::mutex gCompileInvokeMutex;
// coretrace-compiler compiles from several threads at once, each compilation in the
// directory of its -working-directory, without moving the process: compilations share
// this lock. Some change state that the whole process shares, and hold it alone: those
// with an option that Clang applies to LLVM's state, and those with -fno-integrated-cc1,
// whose jobs run as programs, for which coretrace-compiler moves the process to the
// directory and leaves it there (ProcessDirectoryScope).
std::shared_mutex gCompileInvokeMutex;

// Brings the process back to the directory it was in. A compilation that holds
// gCompileInvokeMutex alone ends its scope before it releases the lock: the next
// compilation, and the relative paths of the host, start from that directory again.
class ProcessDirectoryScope
{
public:
ProcessDirectoryScope() : directory_(std::filesystem::current_path(error_)) {}

ProcessDirectoryScope(const ProcessDirectoryScope&) = delete;
ProcessDirectoryScope& operator=(const ProcessDirectoryScope&) = delete;

~ProcessDirectoryScope()
{
if (error_)
return;
std::error_code ignored;
std::filesystem::current_path(directory_, ignored);
}

private:
std::error_code error_;
std::filesystem::path directory_;
};

bool compilesAlone(const std::vector<std::string>& args)
{
static constexpr std::string_view kOptionsOfLLVMState[] = {
"-mllvm", "-mdebug-pass", "-mlimit-float-precision", "-ftime-report"};
return std::any_of(args.begin(), args.end(),
[](const std::string& arg)
{
for (std::string_view option : kOptionsOfLLVMState)
{
if (std::string_view(arg).starts_with(option))
return true;
}
return arg == "-fno-integrated-cc1";
});
}

void appendIfMissing(std::vector<std::string>& args, const std::string& flag)
{
Expand Down Expand Up @@ -745,43 +785,6 @@ namespace ctrace::stack::analysis
return true;
}

class ScopedCurrentPath
{
public:
explicit ScopedCurrentPath(const std::string& path, std::string& error)
{
if (path.empty())
return;
std::error_code ec;
previousPath_ = std::filesystem::current_path(ec);
if (ec)
{
error = "Failed to read current working directory";
return;
}
std::filesystem::current_path(path, ec);
if (ec)
{
error = "Failed to change working directory to: " + path;
return;
}
active_ = true;
}

~ScopedCurrentPath()
{
if (!active_)
return;
std::error_code ec;
std::filesystem::current_path(previousPath_, ec);
}

private:
std::filesystem::path previousPath_;
std::uint64_t active_ : 1 = false;
std::uint64_t reservedFlags_ : 63 = 0;
};

class ScopedFileCleanup
{
public:
Expand Down Expand Up @@ -859,9 +862,7 @@ namespace ctrace::stack::analysis
using ctrace::stack::analyzer::ScopedHotspot;
ModuleLoadResult result;
const ScopedHotspot totalHotspot(config.timing, "input.load_module.total");
// A relative input resolves from the directory the analysis started in, not from the
// current one: another thread may be compiling from its compile command's directory,
// which the whole process shares meanwhile.
// A relative input resolves from the directory the analysis started in.
std::error_code cwdErr;
const std::filesystem::path baseDir = config.inputBaseDir.empty()
? std::filesystem::current_path(cwdErr)
Expand Down Expand Up @@ -923,22 +924,40 @@ namespace ctrace::stack::analysis
[&](const std::vector<std::string>& compileArgs,
compilerlib::OutputMode outputMode) -> std::optional<compilerlib::CompileResult>
{
std::lock_guard<std::mutex> compileLock(gCompileInvokeMutex);
if (workingDir.empty())
std::vector<std::string> argsInWorkingDir;
argsInWorkingDir.reserve(compileArgs.size() + 2);
if (!workingDir.empty())
{
const ScopedHotspot hotspot(config.timing, "input.compiler.invoke");
return compilerlib::compile(compileArgs, outputMode);
// coretrace-compiler reports a directory it cannot enter, yet compiles to
// memory all the same: the compilation fails here instead.
std::error_code directoryError;
if (!std::filesystem::is_directory(workingDir, directoryError))
{
result.error =
"Failed to change working directory to: " + workingDir + "\n";
return std::nullopt;
}
argsInWorkingDir.push_back("-working-directory");
argsInWorkingDir.push_back(workingDir);
}

std::string cwdError;
ScopedCurrentPath cwdGuard(workingDir, cwdError);
if (!cwdError.empty())
argsInWorkingDir.insert(argsInWorkingDir.end(), compileArgs.begin(),
compileArgs.end());

std::shared_lock<std::shared_mutex> shared(gCompileInvokeMutex, std::defer_lock);
std::unique_lock<std::shared_mutex> alone(gCompileInvokeMutex, std::defer_lock);
// After the locks: it ends before the one it was taken under is released.
std::optional<ProcessDirectoryScope> processDirectory;
if (compilesAlone(compileArgs))
{
result.error = cwdError + "\n";
return std::nullopt;
alone.lock();
processDirectory.emplace();
}
const ScopedHotspot hotspot(config.timing, "input.compiler.invoke.cwd");
return compilerlib::compile(compileArgs, outputMode);
else
shared.lock();
const ScopedHotspot hotspot(config.timing, workingDir.empty()
? "input.compiler.invoke"
: "input.compiler.invoke.cwd");
return compilerlib::compile(argsInWorkingDir, outputMode);
};

if (cachePaths.enabled)
Expand Down
53 changes: 53 additions & 0 deletions test/unit/analyzer_module_unit_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#include "StackUsageAnalyzer.hpp"
#include "analysis/AnalyzerUtils.hpp"
#include "analysis/CompileCommands.hpp"
#include "app/CrossTUSummaryDriver.hpp"
#include "app/AnalyzerApp.hpp"
#include "cli/ArgParser.hpp"
Expand Down Expand Up @@ -3472,6 +3473,57 @@ namespace
return report.failures == 0;
}

/// With -fno-integrated-cc1, the jobs of a compile command run as programs:
/// coretrace-compiler moves the process to the command's directory and leaves it there. The
/// load must bring the process back. A source without a compile command is compiled by its
/// relative path, from the directory the process is in: left in the command's directory,
/// the process no longer finds it.
bool testLoadLeavesTheProcessDirectory(TestReport& report)
{
char rootTemplate[] = "/tmp/ctrace-load-directory-XXXXXX";
if (mkdtemp(rootTemplate) == nullptr)
{
report.expect(false, "Load directory: no temporary directory");
return false;
}
std::error_code ec;
const std::filesystem::path start = std::filesystem::current_path(ec);
const std::filesystem::path root = std::filesystem::canonical(rootTemplate, ec);
const std::filesystem::path project = root / "project";
std::filesystem::create_directories(project, ec);
std::ofstream(project / "unit.c") << "int unit(void) { return 1; }\n";
std::ofstream(root / "host.c") << "int host(void) { return 2; }\n";
std::ofstream(project / "compile_commands.json")
<< "[{\"directory\": \"" << project.string() << "\", \"file\": \"unit.c\", "
<< "\"arguments\": [\"cc\", \"-fno-integrated-cc1\", \"-c\", \"unit.c\"]}]\n";

ctrace::stack::AnalysisConfig config;
std::string error;
config.compilationDatabase = ctrace::stack::analysis::CompilationDatabase::loadFromFile(
(project / "compile_commands.json").string(), error);
config.inputBaseDir = root.string();
const auto loads = [&](const char* source)
{
llvm::LLVMContext context;
llvm::SMDiagnostic err;
return ctrace::stack::analysis::loadModuleForAnalysis(source, config, context, err)
.module != nullptr;
};

std::filesystem::current_path(root, ec);
const bool compiled = config.compilationDatabase && loads("project/unit.c");
const bool stayed =
std::filesystem::equivalent(std::filesystem::current_path(ec), root, ec);
const bool found = loads("host.c");
std::filesystem::current_path(start, ec);
std::filesystem::remove_all(root, ec);

report.expect(compiled, "Load directory: a command whose jobs run as programs compiles");
report.expect(stayed, "Load directory: the process is where it was before that load");
report.expect(found, "Load directory: a relative source without a command then compiles");
return compiled && stayed && found;
}

/// A ring of pointer variables with one array source, as -O0 code leaves it: p0 = buf,
/// p1 = p0, ..., p0 = p(n-1). Every pointer must find the array, however far from it; the
/// order of the accesses must not change the alias paths; and the walk must finish on a
Expand Down Expand Up @@ -3609,6 +3661,7 @@ int main(int argc, char** argv)
(void)testDuplicateIfIndexWidths(repoRoot, report);
(void)testStackBufferPhiCycle(repoRoot, report);
(void)testStackBufferPointerRing(report);
(void)testLoadLeavesTheProcessDirectory(report);
(void)testResourceModelConditions(report);
(void)testResourceRuleMatchesFunction(report);
(void)testOwnershipFactCollector(repoRoot, report);
Expand Down
Loading