Hi, all,<br><br> I managed to insert the assertion in my bytecode, but the result is not really what I expected :(<br><br> Let me do a quick recap: I am trying to instrument the bytecode with some assertions, and to do this, I want to insert the abort() function into the bytecode. The problem is that LLVM is creating a new abort() function, instead of using the one that already exists in libc. Let me show my code to add the abort into the code:<br>
<br>// Create abort function<br>FunctionType *AbortFTy = FunctionType::get(Type::getVoidTy(*context), false);<br>Function *AbortF = Function::Create(AbortFTy, GlobalValue::ExternalLinkage, "abort", &M);<br>
<br>
// Call to abort function<br>CallInst *abort = CallInst::Create(AbortF, Twine(), assertfail); <br><br>// Add atributes to the abort call instruction: no return and no unwind<br>abort->addAttribute(~0, Attribute::NoReturn);<br>
abort->addAttribute(~0, Attribute::NoUnwind); <br><br>// Unreachable instruction<br>new UnreachableInst(*context, assertfail);<br><br>So, my bytecode now looks like this one below:<br><br>define i32 @main() nounwind {<br>
....<br>"assert fail": ; preds = %bb1, %13, %bb, %3, %entry<br> call void @abort() noreturn nounwind<br> unreachable<br>....<br>}<br><br>define void @abort() {<br>"assert fail":<br>
call void @abort() noreturn nounwind<br> unreachable<br>}<br><br>But, in truth, what I really wanted to do is to create a bytecode like the one below:<br><br>define i32 @main() nounwind {<br>....<br>bb2: ; preds = %bb, %entry<br>
call void @abort() noreturn nounwind<br> unreachable<br>}<br><br>declare void @abort() noreturn nounwind<br><br>So, could some of you tell me what I need to do to get this "declare void @abort ..." into my bytecode, instead of the "define void @abort..."?<br>
<br>Thank you very much,<br><br>Victor<br><br><div class="gmail_quote">2011/8/19 Nick Lewycky <span dir="ltr"><<a href="mailto:nicholas@mxc.ca">nicholas@mxc.ca</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">Victor Campos wrote:<br>
> Guys,<br>
><br>
> I would like to instrument the bytecode that LLVM produces with<br>
> assertions. I have written the instrumentation code manually, but I do<br>
> not know how to halt the program in case the assertion is false. I took<br>
> a look into the bytecode that LLVM produces for a program like:<br>
><br>
> #include <stdlib.h><br>
> int main() {<br>
> exit(1);<br>
> }<br>
><br>
> And it is like this:<br>
><br>
> define i32 @main() nounwind {<br>
> entry:<br>
> %retval = alloca i32 ; <i32*> [#uses=1]<br>
> %0 = alloca i32 ; <i32*> [#uses=0]<br>
> %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]<br>
> call void @exit(i32 1) noreturn nounwind<br>
> unreachable<br>
> return: ; No predecessors!<br>
> %retval1 = load i32* %retval ; <i32> [#uses=1]<br>
> ret i32 %retval1<br>
> }<br>
><br>
> So, what is the LLVM code to insert the call "call void @exit(i32 1)<br>
> noreturn nounwind"? I thought about something like:<br>
><br>
> CallInst *abort = CallInst::Create(/*POINTER TO ABORT*/,<br>
> ArrayRef<Value*>(), Twine(), assertfail);<br>
> abort->addAttribute(~0, Attribute::NoReturn);<br>
> abort->addAttribute(~0, Attribute::NoUnwind);<br>
><br>
> However, I do not know what to fill up in /*POINTER TO ABORT*/. Can<br>
> anyone help me?<br>
<br>
</div></div>Something like:<br>
<br>
// Once, M = llvm::Module*, C = LLVMContext&:<br>
FunctionType *AbortFTy = FunctionType::get(Type::getVoidTy(C), false);<br>
Function *AbortF = Function::Create(AbortFTy,<br>
GlobalValue::ExternalLinkage, "abort", M);<br>
<br>
// Each call site:<br>
CallInst *abort = CallInst::Create(AbortF, ...<br>
<br>
Nick<br>
<div class="im"><br>
><br>
> Thank you very much,<br>
><br>
> Victor<br>
><br>
><br>
><br>
</div>> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</blockquote></div><br>