Skip to content

Commit 82923c7

Browse files
committed
[analyzer] Add Fuchsia Handle checker
The checker can diagnose handle use after releases, double releases, and handle leaks. Differential Revision: https://reviews.llvm.org/D70470
1 parent 07861e9 commit 82923c7

File tree

7 files changed

+830
-0
lines changed

7 files changed

+830
-0
lines changed

clang/docs/analyzer/checkers.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,31 @@ Warns if 'CFArray', 'CFDictionary', 'CFSet' are created with non-pointer-size va
13351335
&kCFTypeArrayCallBacks); // warn
13361336
}
13371337
1338+
Fuchsia
1339+
^^^^^^^
1340+
1341+
Fuchsia is an open source capability-based operating system currently being
1342+
developed by Google. This section describes checkers that can find various
1343+
misuses of Fuchsia APIs.
1344+
1345+
.. _fuchsia-HandleChecker:
1346+
1347+
fuchsia.HandleChecker
1348+
""""""""""""""""""""""""""""
1349+
Handles identify resources. Similar to pointers they can be leaked,
1350+
double freed, or use after freed. This check attempts to find such problems.
1351+
1352+
.. code-block:: cpp
1353+
1354+
void checkLeak08(int tag) {
1355+
zx_handle_t sa, sb;
1356+
zx_channel_create(0, &sa, &sb);
1357+
if (tag)
1358+
zx_handle_close(sa);
1359+
use(sb); // Warn: Potential leak of handle
1360+
zx_handle_close(sb);
1361+
}
1362+
13381363
13391364
.. _alpha-checkers:
13401365

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def CloneDetectionAlpha : Package<"clone">, ParentPackage<Alpha>;
108108

109109
def NonDeterminismAlpha : Package<"nondeterminism">, ParentPackage<Alpha>;
110110

111+
def Fuchsia : Package<"fuchsia">;
112+
111113
//===----------------------------------------------------------------------===//
112114
// Core Checkers.
113115
//===----------------------------------------------------------------------===//
@@ -1423,3 +1425,16 @@ def PointerSortingChecker : Checker<"PointerSorting">,
14231425
Documentation<HasDocumentation>;
14241426

14251427
} // end alpha.nondeterminism
1428+
1429+
//===----------------------------------------------------------------------===//
1430+
// Fuchsia checkers.
1431+
//===----------------------------------------------------------------------===//
1432+
1433+
let ParentPackage = Fuchsia in {
1434+
1435+
def FuchsiaHandleChecker : Checker<"HandleChecker">,
1436+
HelpText<"A Checker that detect leaks related to Fuchsia handles">,
1437+
Documentation<HasDocumentation>;
1438+
1439+
} // end fuchsia
1440+

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,22 @@ class CheckerContext {
213213
return addTransition(State, (Tag ? Tag : Location.getTag()));
214214
}
215215

216+
/// Generate a transition to a node that will be used to report
217+
/// an error. This node will not be a sink. That is, exploration will
218+
/// continue along this path.
219+
///
220+
/// @param State The state of the generated node.
221+
/// @param Pred The transition will be generated from the specified Pred node
222+
/// to the newly generated node.
223+
/// @param Tag The tag to uniquely identify the creation site. If null,
224+
/// the default tag for the checker will be used.
225+
ExplodedNode *
226+
generateNonFatalErrorNode(ProgramStateRef State,
227+
ExplodedNode *Pred,
228+
const ProgramPointTag *Tag = nullptr) {
229+
return addTransition(State, Pred, (Tag ? Tag : Location.getTag()));
230+
}
231+
216232
/// Emit the diagnostics report.
217233
void emitReport(std::unique_ptr<BugReport> R) {
218234
Changed = true;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,8 @@ static void RenderAnalyzerOptions(const ArgList &Args, ArgStringList &CmdArgs,
27942794
CmdArgs.push_back(
27952795
"-analyzer-checker=security.insecureAPI.decodeValueOfObjCType");
27962796
}
2797+
else if (Triple.isOSFuchsia())
2798+
CmdArgs.push_back("-analyzer-checker=fuchsia");
27972799

27982800
CmdArgs.push_back("-analyzer-checker=deadcode");
27992801

clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_clang_library(clangStaticAnalyzerCheckers
3838
EnumCastOutOfRangeChecker.cpp
3939
ExprInspectionChecker.cpp
4040
FixedAddressChecker.cpp
41+
FuchsiaHandleChecker.cpp
4142
GCDAntipatternChecker.cpp
4243
GenericTaintChecker.cpp
4344
GTestChecker.cpp

0 commit comments

Comments
 (0)