================
@@ -233,6 +236,47 @@ class UnsafeBufferReachableAnalysis
 
       updateReachablesWithOutgoings(Node, Worklist);
     }
+  }
+
+  // Filter out non-transformable pointers from `getResult().Result`, leaving
+  // only those that satisfy C3.
+  void filterTransformablePointers() {
+    assert(TypeConstrainedEntities &&
+           "The initialize(...) method should initialize "
+           "TypeConstrainedEntities to non-null");
+    auto &Result = getResult().Reachables;
+
+    for (auto &[Key, EPLs] : Result) {
+      for (auto ConstrainedEntityId : *TypeConstrainedEntities) {
+        // FIXME: optimization chance here.  Since both sets are sorted, the
+        // next 'equal_range' search can ignore everything before
+        // `NonTransEPLs.second`.
+        auto NonTransEPLs = EPLs.equal_range(ConstrainedEntityId);
+
+        EPLs.erase(NonTransEPLs.first, NonTransEPLs.second);
+      }
+    }
+  }
+
+public:
+  llvm::Error
+  initialize(const PointerFlowAnalysisResult &PtrFlowGraph,
+             const TypeConstrainedPointersAnalysisResult &TypeConstraints,
+             const UnsafeBufferUsageAnalysisResult &UnsafePtrs) override {
+    for (auto &[Id, SubGraph] : PtrFlowGraph.Edges)
+      BPG.try_emplace(Id, BoundsPropagationGraph(SubGraph));
+    TypeConstrainedEntities = &TypeConstraints.Entities;
+    assert(getResult().Reachables.empty());
+    // C1: all pointers in UnsafeBufferUsageAnalysisResult are unsafe
+    getResult().Reachables.insert(UnsafePtrs.begin(), UnsafePtrs.end());
+    return llvm::Error::success();
+  }
+
+  llvm::Expected<bool> step() override {
+    // result meets C1 & C2:
+    computeReachableUnsafePointers();
+    // result meets C1 & C2 & C3:
+    filterTransformablePointers();
----------------
ziqingluo-90 wrote:

I see your point.  We don't need to transform `foo`, if `argv` is the only 
reason that it becomes unsafe.

So it is indeed that we can remove type-constrained pointers from `UnsafePtrs` 
as well as the graph before the reachable computation. 

https://github.com/llvm/llvm-project/pull/209354
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to