You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/08/10 20:13:53 UTC

[GitHub] [tvm] Lunderberg commented on a diff in pull request #12352: [TIR] Add pass to check for out of bounds memory access

Lunderberg commented on code in PR #12352:
URL: https://github.com/apache/tvm/pull/12352#discussion_r942866132


##########
src/tir/analysis/oob_checker.cc:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ *  Out of bounds array access static analyzer.
+ */
+
+#include <tvm/tir/transform.h>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+#include "../../printer/text_printer.h"
+#include "../schedule/error.h"
+
+namespace tvm {
+namespace tir {
+namespace transform {
+struct OOBLocation {
+  Buffer buf;
+  size_t dimension;
+  ObjectRef index;
+  arith::IntSet index_bounds;
+  arith::IntSet shape_bounds;
+};
+
+class OOBError : public ScheduleError {
+ public:
+  OOBError(IRModule mod, std::vector<OOBLocation> locations) : mod_(mod), locations_(locations) {}
+  String FastErrorString() const final { return "Out of bound memory access"; }
+
+  String DetailRenderTemplate() const final {
+    std::stringstream s;
+    for (const auto& oob : locations_) {
+      s << "Out of bounds memory access on buffer " << oob.buf->name << " dimension "
+        << oob.dimension << ".";
+      s << " index " << oob.index << " with bounds [" << oob.index_bounds.min() << ", "
+        << oob.index_bounds.max() << "] is outside the range [0, " << oob.shape_bounds.min()
+        << "].";
+      s << "\n";
+    }
+    return s.str();
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final {
+    std::vector<ObjectRef> locs;
+    for (auto loc : locations_) {
+      locs.push_back(loc.index);
+    }
+    return locs;
+  }
+
+ private:
+  IRModule mod_;
+  std::vector<OOBLocation> locations_;
+};
+class OOBCheckerVisitor final : public arith::IRVisitorWithAnalyzer {
+  using IRVisitorWithAnalyzer::VisitExpr_;
+  using IRVisitorWithAnalyzer::VisitStmt_;
+
+ public:
+  void VisitStmt_(const BufferStoreNode* node) final {
+    for (size_t i = 0; i < node->buffer->shape.size(); i++) {
+      CheckBounds(node, i);
+    }
+    IRVisitorWithAnalyzer::VisitStmt_(node);
+  }
+  void VisitExpr_(const BufferLoadNode* node) final {
+    for (size_t i = 0; i < node->buffer->shape.size(); i++) {
+      CheckBounds(node, i);
+    }
+    IRVisitorWithAnalyzer::VisitExpr_(node);
+  }
+
+  template <class T>
+  void CheckBounds(const T* node, size_t i) {
+    auto ind_bounds = analyzer_.int_set(node->indices[i]);
+    auto shape_bounds = analyzer_.int_set(node->buffer->shape[i]);
+    // Only show an error if we can prove that the access occurs out of bounds.
+    // In some cases we may not be able to prove that the access is in or out
+    // of bounds and we would like to ignore these cases.
+    if (analyzer_.CanProve(ind_bounds.max() >= shape_bounds.min()) ||

Review Comment:
   Do we need to use the `int_set` analyzer here?  Why not `analyzer_.CanProve(node->indices[i] < 0 || node->indices[i] >= node->buffer->shape[i])`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org