[LLVMdev] How to halt a program

Nick Lewycky nicholas at mxc.ca
Fri Aug 19 05:57:48 PDT 2011


Victor Campos wrote:
> Guys,
>
>      I would like to instrument the bytecode that LLVM produces with
> assertions. I have written the instrumentation code manually, but I do
> not know how to halt the program in case the assertion is false. I took
> a look into the bytecode that LLVM produces for a program like:
>
> #include <stdlib.h>
> int main() {
>    exit(1);
> }
>
> And it is like this:
>
> define i32 @main() nounwind {
> entry:
>    %retval = alloca i32                            ; <i32*> [#uses=1]
>    %0 = alloca i32                                 ; <i32*> [#uses=0]
>    %"alloca point" = bitcast i32 0 to i32          ; <i32> [#uses=0]
>    call void @exit(i32 1) noreturn nounwind
>    unreachable
> return:                                           ; No predecessors!
>    %retval1 = load i32* %retval                    ; <i32> [#uses=1]
>    ret i32 %retval1
> }
>
> So, what is the LLVM code to insert the call "call void @exit(i32 1)
> noreturn nounwind"? I thought about something like:
>
> CallInst *abort = CallInst::Create(/*POINTER TO ABORT*/,
> ArrayRef<Value*>(), Twine(), assertfail);
> abort->addAttribute(~0, Attribute::NoReturn);
> abort->addAttribute(~0, Attribute::NoUnwind);
>
> However, I do not know what to fill up in /*POINTER TO ABORT*/. Can
> anyone help me?

Something like:

// Once, M = llvm::Module*, C = LLVMContext&:
FunctionType *AbortFTy = FunctionType::get(Type::getVoidTy(C), false);
Function *AbortF = Function::Create(AbortFTy, 
GlobalValue::ExternalLinkage, "abort", M);

// Each call site:
CallInst *abort = CallInst::Create(AbortF, ...

Nick

>
> Thank you very much,
>
> Victor
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list