[Mlir-commits] [mlir] 86eb57b - [mlir][drr] Simple heuristic to reduce chance of accidental nullptr dereference

Jacques Pienaar llvmlistbot at llvm.org
Wed Dec 1 20:45:19 PST 2021


Author: Jacques Pienaar
Date: 2021-12-01T20:45:08-08:00
New Revision: 86eb57b728c85bd1658f542d6ce8340efa63b4df

URL: https://github.com/llvm/llvm-project/commit/86eb57b728c85bd1658f542d6ce8340efa63b4df
DIFF: https://github.com/llvm/llvm-project/commit/86eb57b728c85bd1658f542d6ce8340efa63b4df.diff

LOG: [mlir][drr] Simple heuristic to reduce chance of accidental nullptr dereference

When an attribute is optional & is given an additional constraint in
rewrite pattern that could lead to dereferencing null Attribute. Avoid
cases where the constraints checks attribute but has no check if null.

This should be improved to be more uniformly guarded.

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/RewriterGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
index 76f925072d0b0..2a98497ed7b25 100644
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -793,6 +793,18 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
     // If a constraint is specified, we need to generate function call to its
     // static verifier.
     StringRef verifier = staticMatcherHelper.getVerifierName(matcher);
+    if (attr.isOptional()) {
+      // Avoid dereferencing null attribute. This is using a simple heuristic to
+      // avoid common cases of attempting to dereference null attribute. This
+      // will return where there is no check if attribute is null unless the
+      // attribute's value is not used.
+      // FIXME: This could be improved as some null dereferences could slip
+      // through.
+      if (!StringRef(matcher.getConditionTemplate()).contains("!$_self") &&
+          StringRef(matcher.getConditionTemplate()).contains("$_self")) {
+        os << "if (!tblgen_attr) return failure();\n";
+      }
+    }
     emitStaticVerifierCall(
         verifier, opName, "tblgen_attr",
         formatv("\"op '{0}' attribute '{1}' failed to satisfy constraint: "


        


More information about the Mlir-commits mailing list