0

I tried to insert an assembly instruction into each base block using pass in the IR Pass of LLVM.

Update:

LLVMContext *Ctx = nullptr;
Ctx = &M.getContext();
BasicBlock::iterator IP = BB.getFirstInsertionPt();
IRBuilder<> IRB(&(*IP));
StringRef asmString = "int3";
StringRef constraints = "~{dirflag},~{fpsr},~{flags}";
llvm::InlineAsm *IA = llvm::InlineAsm::get(Ty,asmString,constraints,true,false,InlineAsm::AD_ATT); 
ArrayRef<Value *> Args = None;
llvm::CallInst *Ptr = IRB.CreateCall(IA,Args);
Ptr->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); 

However, when I ran the pass on one of the test files, test.bc, I found that no INT3 instructions were inserted into the file.I compared the statement I created with Ptr:

call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"() #4

And the real INT3 in IR is:

call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !2

I wonder how can I modify my code to make it work?

gazile
  • 105
  • 6

1 Answers1

0

The type of the inline assembly certainly don't have to match with the type of function it is used in.

For a int3 inline assembly you probably want a void (void) type, that is FunctionType::get(Type::getVoidTy(ctx), false).

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Thanks! This error has now been fixed, but even though it compiles, it doesn't seem to be doing what I wanted, no INT3 statements were inserted into the corresponding file, I updated my problem, can you help me see what went wrong? – gazile Sep 06 '21 at 10:09
  • @gazile Could it be that you are inserting this instruction into an unreachable basic block? – arrowd Sep 11 '21 at 14:00
  • Thank you for your answer. I have fixed the problem, it was not implemented correctly because I ran the pass at the wrong time. Now I have successfully inserted int3 interrupt. Thanks again. – gazile Sep 14 '21 at 08:18