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/09 21:16:51 UTC

[GitHub] [tvm] tkonolige opened a new pull request, #12352: [TIR] Add pass to check for out of bounds memory access

tkonolige opened a new pull request, #12352:
URL: https://github.com/apache/tvm/pull/12352

   This is a conservative static analysis that checks to see if any out of bounds array access occurs. It is not enabled by default.
   
   @Lunderberg @AndrewZhaoLuo @jwfromm 


-- 
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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
tkonolige commented on code in PR #12352:
URL: https://github.com/apache/tvm/pull/12352#discussion_r942888251


##########
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:
   Good question! `CanProve` uses universal quantification — are all indices out of bounds. What we really want is existential quantification — is any index out of bounds. We could check `!analyzer_.CanProve(node->indices[i] >= 0 && node->indices[i] < node->buffer->shape[I])` but then we run into the issue of having some access patterns that are all inbounds but not provably so. I wanted this analysis to be conservative (no false positives) so we can't use this check either. The remaining solution is to check if the bounds of the index lie outside the bounds for the shape.
   
   I've updated the comment to explain this.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #12352:
URL: https://github.com/apache/tvm/pull/12352#discussion_r943553263


##########
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:
   Thank you, and that makes sense!



-- 
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


[GitHub] [tvm] AndrewZhaoLuo merged pull request #12352: [TIR] Add pass to check for out of bounds memory access

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo merged PR #12352:
URL: https://github.com/apache/tvm/pull/12352


-- 
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


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

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on PR #12352:
URL: https://github.com/apache/tvm/pull/12352#issuecomment-1220064039

   I'll take a look over the weekend


-- 
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


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

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on code in PR #12352:
URL: https://github.com/apache/tvm/pull/12352#discussion_r951067578


##########
src/tir/analysis/oob_checker.cc:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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 "

Review Comment:
   nit: It might be nice to have a test where you show the full rendered strings.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
tkonolige commented on code in PR #12352:
URL: https://github.com/apache/tvm/pull/12352#discussion_r942888251


##########
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:
   Good question! `CanProve` uses universal quantification — are all indices out of bounds. What we really want is existential quantification — is any index out of bounds. We could check `!analyzer_.CanProve(node->indices[i] >= 0 && node->indices[i] < node->buffer->shape[I])` but then we run into the issue of having some access patterns that are all inbounds but not provably so. I wanted this analysis to be conservative (no false positives) so we can't use this check either. The remaining solution is to check if the bounds of the index lie outside the bounds for the shape.



-- 
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