Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 5 | #ifndef CRUBIT_NULLABILITY_POINTER_NULLABILITY_ANALYSIS_H_ |
| 6 | #define CRUBIT_NULLABILITY_POINTER_NULLABILITY_ANALYSIS_H_ |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 7 | |
Googler | 2c9f53f | 2023-10-24 07:33:00 -0700 | [diff] [blame] | 8 | #include <optional> |
Sam McCall | e644e1d | 2023-07-18 19:19:12 -0700 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 11 | #include "nullability/pointer_nullability_lattice.h" |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 12 | #include "nullability/type_nullability.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Googler | 2c9f53f | 2023-10-24 07:33:00 -0700 | [diff] [blame] | 15 | #include "clang/AST/DeclBase.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 16 | #include "clang/AST/Type.h" |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 17 | #include "clang/Analysis/FlowSensitive/Arena.h" |
Wei Yi Tee | 217eb5f | 2022-09-15 03:18:28 -0700 | [diff] [blame] | 18 | #include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 19 | #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" |
| 20 | #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 21 | #include "clang/Analysis/FlowSensitive/Value.h" |
Googler | 2c9f53f | 2023-10-24 07:33:00 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/FunctionExtras.h" |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | namespace tidy { |
| 26 | namespace nullability { |
| 27 | |
Wei Yi Tee | 8b58e19 | 2022-08-02 10:15:40 -0700 | [diff] [blame] | 28 | /// Analyses constructs in the source code to collect nullability information |
| 29 | /// about pointers at each program point. |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 30 | class PointerNullabilityAnalysis |
| 31 | : public dataflow::DataflowAnalysis<PointerNullabilityAnalysis, |
Dani Ferreira Franco Moura | 3d56a22 | 2022-11-29 03:12:55 -0800 | [diff] [blame] | 32 | PointerNullabilityLattice> { |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 33 | private: |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 34 | PointerNullabilityLattice::NonFlowSensitiveState NFS; |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 35 | |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 36 | public: |
Sam McCall | 7d9afee | 2023-06-27 01:43:24 -0700 | [diff] [blame] | 37 | explicit PointerNullabilityAnalysis(ASTContext &context); |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 38 | |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 39 | PointerNullabilityLattice initialElement() { |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 40 | return PointerNullabilityLattice(NFS); |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 41 | } |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 42 | |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 43 | // Instead of fixing D's nullability invariants from its annotations, |
| 44 | // bind them to symbolic variables, and return those variables. |
| 45 | // This is useful to infer the annotations that should be present on D. |
| 46 | // |
| 47 | // For example, given the following program: |
| 48 | // void target(int* p) { |
| 49 | // int* q = p; |
| 50 | // *q; |
| 51 | // } |
| 52 | // |
| 53 | // By default, p is treated as having unspecified nullability. |
| 54 | // When we reach the dereference, our flow condition will say: |
Googler | 1a55cfb | 2023-07-17 13:45:18 -0700 | [diff] [blame] | 55 | // from_nullable = false |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 56 | // |
| 57 | // However, if we bind p's nullability to a variable: |
Sam McCall | e644e1d | 2023-07-18 19:19:12 -0700 | [diff] [blame] | 58 | // pn = assignNullabilityVariable(p) |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 59 | // Then the flow condition at dereference includes: |
Sam McCall | e644e1d | 2023-07-18 19:19:12 -0700 | [diff] [blame] | 60 | // from_nullable = pn.Nullable |
| 61 | // pn.Nonnull => !is_null |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 62 | // Logically connecting dereferenced values and possible invariants on p |
| 63 | // allows us to infer p's proper annotations (here: Nonnull). |
| 64 | // |
| 65 | // For now, only the top-level nullability is assigned, and the returned |
| 66 | // variables are only associated with direct reads of pointer values from D. |
Sam McCall | e644e1d | 2023-07-18 19:19:12 -0700 | [diff] [blame] | 67 | // |
| 68 | // The returned nullability is guaranteed to be symbolic. |
Sam McCall | 7d9afee | 2023-06-27 01:43:24 -0700 | [diff] [blame] | 69 | PointerTypeNullability assignNullabilityVariable(const ValueDecl *D, |
| 70 | dataflow::Arena &); |
Sam McCall | 74bf864 | 2023-06-16 11:21:42 -0700 | [diff] [blame] | 71 | |
Googler | 2c9f53f | 2023-10-24 07:33:00 -0700 | [diff] [blame] | 72 | void assignNullabilityOverride( |
| 73 | llvm::unique_function< |
| 74 | std::optional<const PointerTypeNullability *>(const Decl &) const> |
| 75 | Override) { |
| 76 | NFS.ConcreteNullabilityOverride = std::move(Override); |
| 77 | } |
| 78 | |
Sam McCall | 7d9afee | 2023-06-27 01:43:24 -0700 | [diff] [blame] | 79 | void transfer(const CFGElement &Elt, PointerNullabilityLattice &Lattice, |
| 80 | dataflow::Environment &Env); |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 81 | |
Sam McCall | 7d9afee | 2023-06-27 01:43:24 -0700 | [diff] [blame] | 82 | bool merge(QualType Type, const dataflow::Value &Val1, |
| 83 | const dataflow::Environment &Env1, const dataflow::Value &Val2, |
| 84 | const dataflow::Environment &Env2, dataflow::Value &MergedVal, |
| 85 | dataflow::Environment &MergedEnv) override; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 86 | |
Martin Brænne | dd2a770 | 2023-10-26 06:57:18 -0700 | [diff] [blame] | 87 | dataflow::ComparisonResult compare( |
| 88 | QualType Type, const dataflow::Value &Val1, |
| 89 | const dataflow::Environment &Env1, const dataflow::Value &Val2, |
| 90 | const dataflow::Environment &Env2) override; |
| 91 | |
| 92 | dataflow::Value *widen(QualType Type, dataflow::Value &Prev, |
| 93 | const dataflow::Environment &PrevEnv, |
| 94 | dataflow::Value &Current, |
| 95 | dataflow::Environment &CurrentEnv) override; |
| 96 | |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 97 | private: |
Martin Brænne | dd2a770 | 2023-10-26 06:57:18 -0700 | [diff] [blame] | 98 | // Returns a storage location representing "top", i.e. a storage location of |
| 99 | // type `Ty` about which nothing else is known. |
| 100 | // Known limitation: We can't prevent a "top" storage location from being |
| 101 | // associated with a value. This is somewhat strange but does not appear to |
| 102 | // have any ill effects in practice. To disallow this, we may at some point |
| 103 | // want to move the concept of "top" storage locations to the framework. |
| 104 | dataflow::StorageLocation &getTopStorageLocation( |
| 105 | dataflow::DataflowAnalysisContext &DACtx, QualType Ty); |
| 106 | |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 107 | // Applies non-flow-sensitive transfer functions on statements |
Dani Ferreira Franco Moura | 3d56a22 | 2022-11-29 03:12:55 -0800 | [diff] [blame] | 108 | dataflow::CFGMatchSwitch<dataflow::TransferState<PointerNullabilityLattice>> |
Dani Ferreira Franco Moura | 97f5c61 | 2022-12-17 13:28:24 -0800 | [diff] [blame] | 109 | NonFlowSensitiveTransferer; |
| 110 | |
| 111 | // Applies flow-sensitive transfer functions on statements |
| 112 | dataflow::CFGMatchSwitch<dataflow::TransferState<PointerNullabilityLattice>> |
| 113 | FlowSensitiveTransferer; |
Martin Brænne | dd2a770 | 2023-10-26 06:57:18 -0700 | [diff] [blame] | 114 | |
| 115 | // Storage locations that represent "top" for each given type. |
| 116 | llvm::DenseMap<QualType, dataflow::StorageLocation *> TopStorageLocations; |
Wei Yi Tee | 543af74 | 2022-06-01 06:52:24 -0700 | [diff] [blame] | 117 | }; |
| 118 | } // namespace nullability |
| 119 | } // namespace tidy |
| 120 | } // namespace clang |
| 121 | |
Googler | 7f19b2b | 2023-05-01 09:44:57 -0700 | [diff] [blame] | 122 | #endif // CRUBIT_NULLABILITY_POINTER_NULLABILITY_ANALYSIS_H_ |