Skip to content

Commit

Permalink
[CHERI_CSA] ProvenanceSource: suppress with -Wno-cheri-provenance
Browse files Browse the repository at this point in the history
If [-Wcheri-provenance] compiler warning is disabled, do not emit
ambiguous provenance warning for cases when the default choice of
LHS as a source of provenance will probably be fine.

Binary operations with provenance-carrying RHS are reported anyway,
as this may indicate a real bug.
  • Loading branch information
eupharina committed Nov 17, 2023
1 parent f309723 commit 13fe607
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
9 changes: 8 additions & 1 deletion clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,14 @@ def ProvenanceSourceChecker : Checker<"ProvenanceSource">,
"ShowFixIts",
"Enable fix-it hints for this checker",
"false",
InAlpha>
InAlpha>,
CmdLineOption<Boolean,
"ReportForAmbiguousProvenance",
"Report for binary operations with ambiguous provenance "
"for which the default capability derivation from LHS is fine. "
"Disabled if [-Wcheri-provenance] is disabled.",
"true",
Released>
]>,
Documentation<NotDocumented>;

Expand Down
16 changes: 14 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "PS4CPU.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/CodeGenOptions.h"
#include <clang/Basic/DiagnosticSema.h>
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/ObjCRuntime.h"
#include "clang/Basic/Version.h"
Expand Down Expand Up @@ -3057,7 +3058,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,

static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
const llvm::Triple &Triple,
const InputInfo &Input) {
const InputInfo &Input,
DiagnosticsEngine &Diags) {
// Enable region store model by default.
CmdArgs.push_back("-analyzer-store=region");

Expand Down Expand Up @@ -3116,6 +3118,16 @@ static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
(Triple.isRISCV() && tools::riscv::isCheriPurecap(Args, Triple)) ||
(Triple.isAArch64() && tools::aarch64::isPurecap(Args, Triple))) {
CmdArgs.push_back("-analyzer-checker=cheri");

// disable AmbiguousProvenance war if [-Wcheri-provenance] is disabled
if (Diags.getDiagnosticLevel(
diag::warn_ambiguous_provenance_capability_binop,
SourceLocation()) < DiagnosticsEngine::Warning) {
CmdArgs.push_back("-analyzer-config");
CmdArgs.push_back(
"cheri.ProvenanceSource:ReportForAmbiguousProvenance=false");
}

CmdArgs.push_back("-analyzer-checker=optin.portability.PointerAlignment");
CmdArgs.push_back("-analyzer-checker=alpha.core.PointerSub");
}
Expand Down Expand Up @@ -4758,7 +4770,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-DUNICODE");

if (isa<AnalyzeJobAction>(JA))
RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
RenderAnalyzerOptions(Args, CmdArgs, Triple, Input, D.getDiags());

if (isa<AnalyzeJobAction>(JA) ||
(isa<PreprocessJobAction>(JA) && Args.hasArg(options::OPT__analyze)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

#include "CHERIUtils.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include <clang/StaticAnalyzer/Core/BugReporter/BugReporter.h>
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include <clang/Basic/DiagnosticSema.h>
#include <clang/StaticAnalyzer/Core/BugReporter/BugReporter.h>

using namespace clang;
using namespace ento;
Expand Down Expand Up @@ -81,6 +82,7 @@ class ProvenanceSourceChecker : public Checker<check::PostStmt<CastExpr>,
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;

bool ShowFixIts = false;
bool ReportForAmbiguousProvenance = true;

private:
// Utility
Expand Down Expand Up @@ -379,8 +381,8 @@ ExplodedNode *ProvenanceSourceChecker::emitAmbiguousProvenanceWarn(
const BinaryOperator *BO, CheckerContext &C,
bool LHSIsAddr, bool LHSIsNullDerived,
bool RHSIsAddr, bool RHSIsNullDerived) const {
bool const IsUnsigned = BO->getType()->isUnsignedIntegerType();

bool const IsUnsigned = BO->getType()->isUnsignedIntegerType();
SmallString<350> ErrorMessage;
llvm::raw_svector_ostream OS(ErrorMessage);

Expand Down Expand Up @@ -485,6 +487,14 @@ void ProvenanceSourceChecker::checkPostStmt(const BinaryOperator *BO,
ExplodedNode *N;
bool InvalidCap;
if (LHSActiveProv && RHSActiveProv && !IsSub) {
if (!RHSIsAddr && !this->ReportForAmbiguousProvenance) {
// No strong evidence of a real bug here. This code will probably
// be fine with the default choice of LHS for capability derivation.
// Don't report if [-Wcheri-provenance] compiler warning is disabled
// and don't propagate ambiguous provenance flag further
return;
}

N = emitAmbiguousProvenanceWarn(BO, C, LHSIsAddr, LHSIsNullDerived,
RHSIsAddr, RHSIsNullDerived);
if (!N)
Expand Down Expand Up @@ -649,6 +659,9 @@ void ento::registerProvenanceSourceChecker(CheckerManager &mgr) {
auto *Checker = mgr.registerChecker<ProvenanceSourceChecker>();
Checker->ShowFixIts = mgr.getAnalyzerOptions().getCheckerBooleanOption(
Checker, "ShowFixIts");
Checker->ReportForAmbiguousProvenance =
mgr.getAnalyzerOptions().getCheckerBooleanOption(
Checker, "ReportForAmbiguousProvenance");
}

bool ento::shouldRegisterProvenanceSourceChecker(const CheckerManager &Mgr) {
Expand Down

0 comments on commit 13fe607

Please sign in to comment.