Skip to content

Cope with MCOperand null Insts #91794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2024
Merged

Cope with MCOperand null Insts #91794

merged 1 commit into from
May 14, 2024

Conversation

urnathan
Copy link
Contributor

I encountered a bolt segfault due to an MCOperand of type Inst with a null pointer. It seems bolt is intentionally creating such operands, so it would be good for the printer to not barf on them.

The bolt code is bolt/include/bolt/Core/MCPlusBuilder.h where the annotation machinery is. For example line 131 in setAnnotationOpValue:
Inst.addOperand(MCOperand::createInst(nullptr));

In my case it was annotating an X86 JCC instruction, and using --debug crashed.

@urnathan urnathan marked this pull request as ready for review May 12, 2024 13:45
@llvmbot llvmbot added the mc Machine (object) code label May 12, 2024
@urnathan urnathan requested a review from maksfb May 12, 2024 13:45
@llvmbot
Copy link
Member

llvmbot commented May 12, 2024

@llvm/pr-subscribers-mc

Author: Nathan Sidwell (urnathan)

Changes

I encountered a bolt segfault due to an MCOperand of type Inst with a null pointer. It seems bolt is intentionally creating such operands, so it would be good for the printer to not barf on them.

The bolt code is bolt/include/bolt/Core/MCPlusBuilder.h where the annotation machinery is. For example line 131 in setAnnotationOpValue:
Inst.addOperand(MCOperand::createInst(nullptr));

In my case it was annotating an X86 JCC instruction, and using --debug crashed.


Full diff: https://github.com/llvm/llvm-project/pull/91794.diff

1 Files Affected:

  • (modified) llvm/lib/MC/MCInst.cpp (+4-1)
diff --git a/llvm/lib/MC/MCInst.cpp b/llvm/lib/MC/MCInst.cpp
index 3cc50ff43513e..639619fe4e991 100644
--- a/llvm/lib/MC/MCInst.cpp
+++ b/llvm/lib/MC/MCInst.cpp
@@ -38,7 +38,10 @@ void MCOperand::print(raw_ostream &OS, const MCRegisterInfo *RegInfo) const {
     OS << "Expr:(" << *getExpr() << ")";
   } else if (isInst()) {
     OS << "Inst:(";
-    getInst()->print(OS, RegInfo);
+    if (const auto *Inst = getInst())
+      Inst->print(OS, RegInfo);
+    else
+      OS << "NULL";
     OS << ")";
   } else
     OS << "UNDEFINED";

Copy link
Contributor

@maksfb maksfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@urnathan urnathan merged commit cfa0947 into llvm:main May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants