[llvm] [TableGen] Fix a potential crash when operand doesn't appear in the instruction pattern (PR #87663)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 4 13:55:41 PDT 2024


https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/87663

>From f98085d8b7c29bd1fd525b95f54e14f3fa657dfe Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Thu, 4 Apr 2024 16:55:29 -0400
Subject: [PATCH] [TableGen] Fix a potential crash when operand doesn't appear
 in the instruction pattern

We have a check of whether an operand is in the instruction pattern, and emit an
error if it is not, but we simply continue execution, including directly
dereferencing a point-like object `InVal`, which will be just created when
accessing the map. It contains a `nullptr` so dereferencing it causes crash.
This is a very trivial fix.
---
 llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
index 076d0427a85971..ce56a0f2c8a0e2 100644
--- a/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
@@ -3858,8 +3858,10 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
   for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
     CGIOperandList::OperandInfo &Op = CGI.Operands[i];
     const std::string &OpName = Op.Name;
-    if (OpName.empty())
+    if (OpName.empty()) {
       I.error("Operand #" + Twine(i) + " in operands list has no name!");
+      continue;
+    }
 
     if (!InstInputs.count(OpName)) {
       // If this is an operand with a DefaultOps set filled in, we can ignore
@@ -3872,6 +3874,7 @@ void CodeGenDAGPatterns::parseInstructionPattern(CodeGenInstruction &CGI,
       }
       I.error("Operand $" + OpName +
               " does not appear in the instruction pattern");
+      continue;
     }
     TreePatternNodePtr InVal = InstInputs[OpName];
     InstInputs.erase(OpName); // It occurred, remove from map.



More information about the llvm-commits mailing list