Skip to content

Commit f6b4440

Browse files
authored
Merge pull request #107 from CoreTrace/ci/smt-z3-backend
ci(smt): build and exercise the Z3 backend
2 parents cc4edcb + 6a738a0 commit f6b4440

9 files changed

Lines changed: 3032 additions & 16 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: |
4141
sudo apt-get update
4242
sudo apt-get install -y build-essential cmake python3 \
43-
ninja-build ccache lld
43+
ninja-build ccache lld libz3-dev pkg-config
4444
# Install LLVM and Clang 20
4545
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
4646
UBUNTU_CODENAME=$(lsb_release -cs)
@@ -54,7 +54,7 @@ jobs:
5454
- name: Install dependencies (macOS)
5555
if: runner.os == 'macOS'
5656
run: |
57-
brew install cmake python llvm@20 ninja ccache
57+
brew install cmake python llvm@20 ninja ccache z3
5858
echo "LLVM_DIR=$(brew --prefix llvm@20)/lib/cmake/llvm" >> $GITHUB_ENV
5959
echo "Clang_DIR=$(brew --prefix llvm@20)/lib/cmake/clang" >> $GITHUB_ENV
6060
echo "$(brew --prefix llvm@20)/bin" >> $GITHUB_PATH

‎.github/workflows/concurrency.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ jobs:
4343
# Generated here, never committed: a compilation database records absolute
4444
# paths and the toolchain that produced it, so one made on a developer's
4545
# machine names an SDK this runner does not have and every unit then fails
46-
# to compile. Configuring is enough -- nothing needs building.
46+
# to compile. Configuring is enough -- nothing needs building. The runner
47+
# image has a z3.pc but the analyzer image has no Z3 headers, so the
48+
# optional Z3 backend stays out of the database.
4749
- name: Generate a compilation database
4850
run: |
4951
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
5052
-DLLVM_DIR=/usr/lib/llvm-20/lib/cmake/llvm \
51-
-DClang_DIR=/usr/lib/llvm-20/lib/cmake/clang
53+
-DClang_DIR=/usr/lib/llvm-20/lib/cmake/clang \
54+
-DENABLE_Z3_BACKEND=OFF
5255
5356
# fail-on: error from the start rather than the usual none-then-tighten,
5457
# because there is no backlog to absorb: the tree is clean today.

‎CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ set(STACK_ANALYZER_SOURCES
114114
set(CTRACE_STACK_HAVE_Z3_BACKEND OFF)
115115
if(ENABLE_Z3_BACKEND)
116116
find_package(Z3 QUIET CONFIG)
117+
if(NOT Z3_FOUND)
118+
# Debian and Ubuntu ship z3.pc but no Z3Config.cmake.
119+
find_package(PkgConfig QUIET)
120+
if(PkgConfig_FOUND)
121+
pkg_check_modules(Z3 QUIET IMPORTED_TARGET z3)
122+
endif()
123+
endif()
117124
if(Z3_FOUND)
118125
list(APPEND STACK_ANALYZER_SOURCES src/analysis/smt/backends/Z3Backend.cpp)
119126
set(CTRACE_STACK_HAVE_Z3_BACKEND ON)
@@ -146,6 +153,8 @@ if(CTRACE_STACK_HAVE_Z3_BACKEND)
146153
target_link_libraries(stack_usage_analyzer_lib PUBLIC z3::libz3)
147154
elseif(TARGET z3)
148155
target_link_libraries(stack_usage_analyzer_lib PUBLIC z3)
156+
elseif(TARGET PkgConfig::Z3)
157+
target_link_libraries(stack_usage_analyzer_lib PUBLIC PkgConfig::Z3)
149158
elseif(DEFINED Z3_LIBRARIES)
150159
target_link_libraries(stack_usage_analyzer_lib PUBLIC ${Z3_LIBRARIES})
151160
if(DEFINED Z3_INCLUDE_DIRS)

‎docs/superpowers/plans/2026-09-24-smt-solver-precision.md‎

Lines changed: 2499 additions & 0 deletions
Large diffs are not rendered by default.

‎docs/superpowers/specs/2026-09-24-smt-solver-precision-design.md‎

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

‎include/analysis/smt/SolverOrchestrator.hpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <cstdint>
77
#include <string>
8+
#include <string_view>
89

910
namespace ctrace::stack::analysis::smt
1011
{
@@ -27,4 +28,10 @@ namespace ctrace::stack::analysis::smt
2728
private:
2829
SolverOrchestratorConfig config_;
2930
};
31+
32+
/// @brief Whether @p name designates a backend compiled into this build.
33+
///
34+
/// Names match case-insensitively, as `--smt-backend` accepts them; an empty name selects
35+
/// the interval backend. A missing backend answers every query Unknown.
36+
[[nodiscard]] bool isSmtBackendAvailable(std::string_view name);
3037
} // namespace ctrace::stack::analysis::smt

‎main.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0
22
#include "app/AnalyzerApp.hpp"
33
#include "cli/ArgParser.hpp"
4+
#include "analysis/smt/SolverOrchestrator.hpp"
45

56
#include <algorithm>
67
#include <sstream>
78
#include <string>
89
#include <utility>
10+
#include <vector>
911

1012
#include <llvm/Support/raw_ostream.h>
1113

@@ -188,6 +190,33 @@ static void printEffectiveConfig(const ctrace::stack::cli::ParsedArguments& pars
188190
llvm::errs() << "========================================\n";
189191
}
190192

193+
// A backend missing from this build answers every query Unknown, which silently turns
194+
// --smt=on into a no-op; say so once, before any analysis.
195+
static void warnAboutUnavailableSmtBackends(const AnalysisConfig& cfg)
196+
{
197+
using ctrace::stack::analysis::smt::SolverMode;
198+
if (!cfg.smtEnabled)
199+
return;
200+
201+
std::vector<std::string> used{cfg.smtBackend};
202+
if (cfg.smtMode != SolverMode::Single && !cfg.smtSecondaryBackend.empty() &&
203+
cfg.smtSecondaryBackend != cfg.smtBackend)
204+
{
205+
used.push_back(cfg.smtSecondaryBackend);
206+
}
207+
208+
for (const std::string& backend : used)
209+
{
210+
if (!ctrace::stack::analysis::smt::isSmtBackendAvailable(backend))
211+
{
212+
coretrace::log(coretrace::Level::Warn,
213+
"SMT backend '{}' is not available in this build; its queries are "
214+
"inconclusive\n",
215+
backend);
216+
}
217+
}
218+
}
219+
191220
int main(int argc, char** argv)
192221
{
193222
coretrace::enable_logging();
@@ -222,6 +251,8 @@ int main(int argc, char** argv)
222251
if (parseResult.parsed.printEffectiveConfig)
223252
printEffectiveConfig(parseResult.parsed);
224253

254+
warnAboutUnavailableSmtBackends(parseResult.parsed.config);
255+
225256
ctrace::stack::app::RunResult runResult =
226257
ctrace::stack::app::runAnalyzerApp(std::move(parseResult.parsed));
227258
if (!runResult.isOk())

‎run_test.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,53 @@ def check_help_flags() -> bool:
11851185
return ok
11861186

11871187

1188+
_SMT_BACKEND_UNAVAILABLE = "is not available in this build"
1189+
1190+
1191+
def check_smt_unavailable_backend_warning() -> bool:
1192+
"""
1193+
`--smt=on` must say on stderr when a backend it uses is not compiled in. This run also
1194+
needs Z3: without it the dedicated smt-z3 fixture pass silently checks nothing.
1195+
"""
1196+
print("=== Testing SMT backend availability ===")
1197+
if _runner_has_explicit_smt_args():
1198+
print(" [info] skipped (runner already has --smt args)")
1199+
print()
1200+
return True
1201+
1202+
fixture = str(fixture_path_with_fallback("integer-overflow/nsw-flag-must-not-discharge-itself.c"))
1203+
# (analyzer arguments, backend that must be reported once, or None for no warning)
1204+
cases = [
1205+
(["--smt=on", "--smt-backend=z3"], None),
1206+
(["--smt=on", "--smt-backend=cvc5"], "cvc5"),
1207+
(["--smt=on", "--smt-backend=Interval"], None),
1208+
(["--smt=on", "--smt-mode=portfolio", "--smt-secondary-backend=cvc5"], "cvc5"),
1209+
(["--smt=on", "--smt-mode=single", "--smt-secondary-backend=cvc5"], None),
1210+
(["--smt-backend=cvc5", "--smt=off"], None),
1211+
]
1212+
ok = True
1213+
for args, backend in cases:
1214+
result = run_analyzer([*args, fixture])
1215+
stderr = result.stderr or ""
1216+
warnings = stderr.count(_SMT_BACKEND_UNAVAILABLE)
1217+
label = " ".join(args)
1218+
if _SMT_BACKEND_UNAVAILABLE in (result.stdout or ""):
1219+
print(f" ❌ {label}: the warning must go to stderr, not stdout")
1220+
ok = False
1221+
elif backend is None and warnings:
1222+
print(f" ❌ {label}: unexpected warning")
1223+
if "SMT backend 'z3'" in stderr:
1224+
print(" this analyzer has no Z3: install it (libz3-dev or brew z3) and rebuild")
1225+
ok = False
1226+
elif backend is not None and (warnings != 1 or f"SMT backend '{backend}'" not in stderr):
1227+
print(f" ❌ {label}: expected one warning for '{backend}', got {warnings}")
1228+
ok = False
1229+
if ok:
1230+
print(" ✅ SMT backend availability OK")
1231+
print()
1232+
return ok
1233+
1234+
11881235
def check_multi_file_json() -> bool:
11891236
"""
11901237
Check that analysis accepts multiple files and JSON aggregates correctly.
@@ -3440,6 +3487,7 @@ def record_ok(ok: bool):
34403487
# after the pool completes.
34413488
parallel_checks = [
34423489
check_help_flags,
3490+
check_smt_unavailable_backend_warning,
34433491
check_analyzer_module_unit_tests,
34443492
check_multi_file_json,
34453493
check_multi_file_total_summary,

‎src/analysis/smt/SolverOrchestrator.cpp‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,13 @@ namespace ctrace::stack::analysis::smt
7070

7171
static std::shared_ptr<ISmtBackend> createBackend(std::string_view name)
7272
{
73-
const std::string lowered = toLowerAscii(name);
74-
if (lowered.empty() || lowered == "interval")
75-
return std::make_shared<IntervalBackend>();
76-
if (lowered == "z3")
77-
{
73+
if (!isSmtBackendAvailable(name))
74+
return std::make_shared<UnavailableExternalBackend>(std::string(name));
7875
#ifdef CTRACE_STACK_ENABLE_Z3_BACKEND
76+
if (toLowerAscii(name) == "z3")
7977
return std::make_shared<Z3Backend>();
80-
#else
81-
return std::make_shared<UnavailableExternalBackend>("z3");
8278
#endif
83-
}
84-
if (lowered == "cvc5")
85-
return std::make_shared<UnavailableExternalBackend>("cvc5");
86-
87-
return std::make_shared<UnavailableExternalBackend>(std::string(name));
79+
return std::make_shared<IntervalBackend>();
8880
}
8981

9082
static void appendBackendIfMissing(std::vector<std::shared_ptr<ISmtBackend>>& out,
@@ -234,6 +226,18 @@ namespace ctrace::stack::analysis::smt
234226
}
235227
} // namespace
236228

229+
bool isSmtBackendAvailable(std::string_view name)
230+
{
231+
const std::string lowered = toLowerAscii(name);
232+
if (lowered.empty() || lowered == "interval")
233+
return true;
234+
#ifdef CTRACE_STACK_ENABLE_Z3_BACKEND
235+
if (lowered == "z3")
236+
return true;
237+
#endif
238+
return false;
239+
}
240+
237241
SolverOrchestrator::SolverOrchestrator(SolverOrchestratorConfig config)
238242
: config_(std::move(config))
239243
{

0 commit comments

Comments
 (0)