LLVM load instruction query

HI,
I am creating a pass that will pass loaded value by load instruction to an
external function.
I don't know how to do it.Please Help.

Hi

I think you have the same question on stackoverflow. Please send the error that you have.

Ok, so there is a general segfault. You can use a tool for detecting seg fault, but better try to use the “return false” statement in your pass. I assume you have your code into runOnFunction or runOnModule. So check with return false where the program crashes. identify the statement that is causing the segfault. The most common segfaults in LLVM are due

  1. reference a NULL pointer…put if statements that check if the structures that you have are NULL
  2. check your boundaries or arrays, if for example the number of operands is 2 and you have getOperand(2), here it is.

Hi Anshul,

> I am creating a pass that will pass loaded value by load instruction to an

external function.
I don't know how to do it.Please Help.

your question is too vague for anyone to be able to help you. Add details,
for example provide the code for your pass.

Ciao, Duncan.

Try :

INITIALIZE a1 if you didnt.

if (cpProf EXISTS){
CallInst* newInst = CallInst::Create(cpProf,a1,“”);

if (BB->getTerminator() && CI)
BB->getInstList().insert((Instruction*)CI, newInst);

Duncan Sands <baldrick <at> free.fr> writes:

Hi Anshul,

> I am creating a pass that will pass loaded value by load instruction to an
> external function.
> I don't know how to do it.Please Help.

your question is too vague for anyone to be able to help you. Add details,
for example provide the code for your pass.

Ciao, Duncan.

Here is the code that i did.
I tried to found error.I think whatever i am doing for getting loaded value is
wrong.But i dont know how to correct it

        virtual bool runOnModule(Module &M)
        {
            Constant *cpProfFunc;
            Context = &M.getContext();
            cpProfFunc =
M.getOrInsertFunction("_Z6cpProfi",Type::getVoidTy(*Context),
                                                
PointerType::getUnqual(Type::getInt8Ty(*Context)),
                                                ,NULL);
            cpProf= cast<Function>(cpProfFunc);

            for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
            {
            
                    for(Function::iterator BB = F->begin(), E = F->end(); BB !=
E; ++BB)
                    {
                        anshul_insert::runOnBasicBlock(BB);
                    }
             }

            return false;
        }
        virtual bool runOnBasicBlock(Function::iterator &BB)
        {
            for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
++BI)
            {
                
                    if(isa<LoadInst>(&(*BI)) )
                    {
                           
                            std::vector<Value*> a1(1);
                            LoadInst *CI = dyn_cast<LoadInst>(BI);
                            
a1[0]=ConstantInt::get(Type::getInt32Ty(*Context),CI->getPointerOperand());
                           
                         CallInst* newInst = CallInst::Create(cpProf,a1,"");
                         BB->getInstList().insert((Instruction*)CI, newInst);
                        
                    }
                   
            }
            return true;
        }
    };
}

Duncan Sands <baldrick <at> free.fr> writes:

Hi Anshul,

  > I am creating a pass that will pass loaded value by load instruction to an

external function.
I don't know how to do it.Please Help.

your question is too vague for anyone to be able to help you. Add details,
for example provide the code for your pass.

Ciao, Duncan.

Here is the code that i did.
I tried to found error.I think whatever i am doing for getting loaded value is
wrong.But i dont know how to correct it

There are a few things for which you should look or fix:

1) You should make sure that certain values are not NULL (namely CI).

2) Inserting a call instruction may invalidate the BasicBlock iterator BI. If you want to be clever, read up on the BasicBlock::iterator and see under what conditions it can be invalidated. If you're lazy like me, you may want to record (in a std::set or std::vector) the Load instructions that you wish to instrument in one loop and then use a second loop to add the call instructions.

3) Better yet, you should use the InstVistor class to find load instructions. I believe the InstVistor's internal iterator doesn't get invalidated when its visitLoadInstruction() method adds instructions. See http://llvm.org/viewvc/llvm-project/safecode/branches/release_32/lib/CommonMemorySafety/InstrumentMemoryAccesses.cpp?revision=168085&view=markup for an example.

4) I think your runOnModule() method is returning false unconditionally. It should return true if it modifies the program.

-- John T.