-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[analyzer] Harden safeguards for Z3 query times #95129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steakhal
merged 9 commits into
main
from
users/steakhal/spr/analyzer-harden-safeguards-for-z3-query-times
Jun 18, 2024
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b61e22
[𝘀𝗽𝗿] changes to main this commit is based on
steakhal f19de54
[𝘀𝗽𝗿] initial version
steakhal bf9c0ee
[𝘀𝗽𝗿] changes introduced through rebase
steakhal 9978f4f
Some minor cleanups
steakhal 0a21cf4
[𝘀𝗽𝗿] changes introduced through rebase
steakhal f2b4dd4
Implement move-only rule-of-5 for: - `Z3Config`
steakhal 00d8d11
[𝘀𝗽𝗿] changes introduced through rebase
steakhal 3813319
Landed first commit
steakhal 0da6f2f
NFC
steakhal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
clang/include/clang/StaticAnalyzer/Core/BugReporter/Z3CrosscheckVisitor.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//===- Z3CrosscheckVisitor.h - Crosscheck reports with Z3 -------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the visitor and utilities around it for Z3 report | ||
// refutation. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_Z3CROSSCHECKVISITOR_H | ||
#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_Z3CROSSCHECKVISITOR_H | ||
|
||
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" | ||
|
||
namespace clang::ento { | ||
|
||
/// The bug visitor will walk all the nodes in a path and collect all the | ||
/// constraints. When it reaches the root node, will create a refutation | ||
/// manager and check if the constraints are satisfiable. | ||
class Z3CrosscheckVisitor final : public BugReporterVisitor { | ||
public: | ||
struct Z3Result { | ||
std::optional<bool> IsSAT = std::nullopt; | ||
unsigned Z3QueryTimeMilliseconds = 0; | ||
unsigned UsedRLimit = 0; | ||
}; | ||
Z3CrosscheckVisitor(Z3CrosscheckVisitor::Z3Result &Result, | ||
const AnalyzerOptions &Opts); | ||
|
||
void Profile(llvm::FoldingSetNodeID &ID) const override; | ||
|
||
PathDiagnosticPieceRef VisitNode(const ExplodedNode *N, | ||
BugReporterContext &BRC, | ||
PathSensitiveBugReport &BR) override; | ||
|
||
void finalizeVisitor(BugReporterContext &BRC, const ExplodedNode *EndPathNode, | ||
PathSensitiveBugReport &BR) override; | ||
|
||
private: | ||
void addConstraints(const ExplodedNode *N, | ||
bool OverwriteConstraintsOnExistingSyms); | ||
|
||
/// Holds the constraints in a given path. | ||
ConstraintMap Constraints; | ||
Z3Result &Result; | ||
const AnalyzerOptions &Opts; | ||
}; | ||
|
||
/// The oracle will decide if a report should be accepted or rejected based on | ||
/// the results of the Z3 solver and the statistics of the queries of a report | ||
/// equivalenece class. | ||
class Z3CrosscheckOracle { | ||
public: | ||
explicit Z3CrosscheckOracle(const AnalyzerOptions &Opts) : Opts(Opts) {} | ||
|
||
enum Z3Decision { | ||
AcceptReport, // The report was SAT. | ||
RejectReport, // The report was UNSAT or UNDEF. | ||
RejectEQClass, // The heuristic suggests to skip the current eqclass. | ||
}; | ||
|
||
/// Updates the internal state with the new Z3Result and makes a decision how | ||
/// to proceed: | ||
/// - Accept the report if the Z3Result was SAT. | ||
/// - Suggest dropping the report equvalence class based on the accumulated | ||
/// statistics. | ||
/// - Otherwise, reject the report if the Z3Result was UNSAT or UNDEF. | ||
/// | ||
/// Conditions for dropping the equivalence class: | ||
/// - Accumulative time spent in Z3 checks is more than 700ms in the eqclass. | ||
/// - Hit the 300ms query timeout in the report eqclass. | ||
/// - Hit the 400'000 rlimit in the report eqclass. | ||
/// | ||
/// Refer to | ||
/// https://discourse.llvm.org/t/analyzer-rfc-taming-z3-query-times/79520 to | ||
/// see why this heuristic was chosen. | ||
Z3Decision interpretQueryResult(const Z3CrosscheckVisitor::Z3Result &Meta); | ||
|
||
private: | ||
const AnalyzerOptions &Opts; | ||
unsigned NumZ3QueriesDoneInEqClass = 0; | ||
unsigned AccumulatedZ3QueryTimeInEqClass = 0; // ms | ||
}; | ||
|
||
} // namespace clang::ento | ||
|
||
#endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_Z3CROSSCHECKVISITOR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.