2017-12-26 8 views
3

최근에 LLVM으로 작업하기 시작했습니다. 나는 다음과 같은 코드malloc 함수 호출을 감지하기위한 LLVM 패스 작성, 할당 된 바이트 수 및 해당 메모리를 가리키는 변수 이름

string = (char *)malloc(100); 
string = NULL; 

에게 주어진 LLVM의 패스를 작성하려고하고 해당 LLVM IR

%call = call noalias i8* @malloc(i64 100) #3 
store i8* %call, i8** %string, align 8 
store i8* null, i8** %string, align 8 

하고하는 것은, (이 경우 100)가 할당 number of bytes를 추출의 addressmalloc를 호출 지침을 감지 반환 된 주소와 주소가 할당 된 변수 이름.

std::map<std::string, std::tuple<size_t, int> > mem_addrs; // stores pointer name, address and no. of bytes allocated 
Count() : ModulePass(ID) {} 

virtual bool runOnModule(Module &M) { 
    for (Function &F: M) { 
    for (BasicBlock &B: F) { 
     for (Instruction &I: B) { 
      if(CallInst* call_inst = dyn_cast<CallInst>(&I)) { 
       Function* fn = call_inst->getCalledFunction(); 
       StringRef fn_name = fn->getName(); 
       errs() << fn_name << " : " << "\n"; 
       for(auto args = fn->arg_begin(); args != fn->arg_end(); ++args) { 
        ConstantInt* arg = dyn_cast<ConstantInt>(&(*args)); 
        if (arg != NULL) 
          errs() << arg->getValue() << "\n"; 
       }  
      } 
     } 
    } 
    } 

출력은 내가 malloc 지침을 감지 할 수 있어요

-VirtualBox:~/program_analysis$ opt -load $LLVMLIB/CSE231.so -analyze -count < $BENCHMARKS/leaktest/leaktest.bc > $OUTPUTLOGS/welcome.static.log 
ok 
allocaimw 
allocaleak 
allocamalloc : 0x2f5d9e0 
0 opt    0x0000000001315cf2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34 
1 opt    0x0000000001315914 
2 libpthread.so.0 0x00007f0b53f12330 
3 opt    0x00000000012ec78f llvm::APInt::toString(llvm::SmallVectorImpl<char>&, unsigned int, bool, bool) const + 79 
4 opt    0x00000000012ed309 llvm::APInt::print(llvm::raw_ostream&, bool) const + 57 
5 CSE231.so  0x00007f0b52f16661 
6 opt    0x00000000012ad6cd llvm::legacy::PassManagerImpl::run(llvm::Module&) + 797 
7 opt    0x000000000058e190 main + 2752 
8 libc.so.6  0x00007f0b5313af45 __libc_start_main + 245 
9 opt    0x00000000005ab2ca 
Stack dump: 
0. Program arguments: opt -load /home/hifza/program_analysis/llvm/build/Release+Asserts/lib/CSE231.so -analyze -count 
1. Running pass 'Instruction Counts Pass' on module '<stdin>'. 
Segmentation fault (core dumped) 

,하지만 난 해당 메모리 주소와 할당 된 바이트 수를 찾을 수 없습니다입니다. 누구든지 나를 어떻게 안내 할 수 있습니까? 감사.

+0

'args'를 반복하면 'ConstantInt'가 100이되지 않습니까? – arrowd

+0

올바르게 인쇄 할 수 없습니다. 루프의 일부 오류로 인해이 코드를 실행할 때 코드가 덤프됩니다. – Hif

+0

글쎄, 오류를 게시하십시오. – arrowd

답변

1

dyn_cast<ConstantInt>(&(*args))의 결과를 확인하지 않았습니다. 형 변환 형식이 ConstantInt이 아닌 경우 nullptr을 반환합니다. 그리고 다음 줄 (arg->getValue())에서 역 참조하십시오.

+0

NULL인지 확인했지만 아무 것도 출력하지 못했습니다. – Hif

+0

수표와 함께 코드를 보여주십시오. – arrowd

+0

if (arg! = NULL) // errs() << arg-> getValue() << "\ n"; – Hif

관련 문제