Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
llvm中的内存分配函数_Llvm_Llvm Clang_Llvm Ir_Llvm C++ Api_Llvm 4.0 - Fatal编程技术网

llvm中的内存分配函数

llvm中的内存分配函数,llvm,llvm-clang,llvm-ir,llvm-c++-api,llvm-4.0,Llvm,Llvm Clang,Llvm Ir,Llvm C++ Api,Llvm 4.0,如何在llvm过程中检测malloc和free函数调用,并通过获取llvm中的参数和返回类型,将其替换为新函数调用 i、 e.是否有方法创建新函数(Malloc和free),然后在检测到Malloc和free时创建函数调用 1.Create malloc and free Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext())); FunctionType *malloc_Fty = FunctionTyp

如何在llvm过程中检测malloc和free函数调用,并通过获取llvm中的参数和返回类型,将其替换为新函数调用

i、 e.是否有方法创建新函数(Malloc和free),然后在检测到Malloc和free时创建函数调用

1.Create malloc and free

  Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext()));
  FunctionType *malloc_Fty = FunctionType::get(BPTy, llvm::ArrayRef<Type*>(), true);
  Function *malloc_func = cast<Function>(M.getOrInsertFunction("malloc", malloc_Fty));
  FunctionType *free_Fty = FunctionType::get(Type::getVoidTy(M.getContext()), BPTy, false);
  Function *free_func = cast<Function>(M.getOrInsertFunction("free",free_Fty));

2. Detect malloc and free and replace it with new malloc and free function calls

if(CallInst *CI = dyn_cast<CallInst>(I)) {
   Function *Callee = CI->getCalledFunction();
   if (Callee->getName() == "malloc") {
       FunctionType *Ty = Callee->getFunctionType();
       PointerType *PtrTy = dyn_cast<PointerType>(Ty);
       Type *AllocTy = PtrTy->getElementType();
       const DataLayout &DL = M.getDataLayout();
       Type *IntPtrTy = DL.getIntPtrType(M.getContext());
       Value *mallocArgs;
       mallocArgs = ConstantExpr::getSizeOf(AllocTy);
       mallocArgs = ConstantExpr::getTruncOrBitCast(cast<Constant>(mallocArgs), IntPtrTy);
       if (const AllocaInst *AI = dyn_cast<AllocaInst>(&*I)) {
           if (AI->isArrayAllocation()) {
               if (isa<ConstantInt>(mallocArgs) && cast<ConstantInt>(mallocArgs)->isOne()) {
                   mallocArgs = I->getOperand(0);         
                   } else if (Constant *CO = dyn_cast<Constant>(I->getOperand(0))) {
                             CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false );
                            mallocArgs = ConstantExpr::getMul(CO, cast<Constant>(mallocArgs));
                   } else {
                          Value *Scale = I->getOperand(0);
                          if (Scale->getType() != IntPtrTy)
                             Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false,"", &*I);
   // Multiply it by the array size if necessary...
                            mallocArgs =BinaryOperator::Create(Instruction::Mul, Scale,                                                                 mallocArgs, "", &*I);
                        }
                      }
                    }
    // Create the call to malloc
         CallInst *malloc_Call = CallInst::Create(malloc_func, mallocArgs, "", &*I);
     // Cast the instruction to convert to the correct type
       Value *malloc_PtrCast;
       Type * voidty=Type::getVoidTy (M.getContext());
       if (malloc_Call->getType() !=voidty )
           malloc_PtrCast = new BitCastInst(malloc_Call, I->getType(), "", &*I);
          else
          malloc_PtrCast = Constant::getNullValue(I->getType());
          Instruction *II=dyn_cast<Instruction>(&*I);
          II->replaceAllUsesWith(malloc_PtrCast);
         }
       if (Callee->getName() == "free") {
            Value *free_PtrCast =
            new BitCastInst(I->getOperand(0),                                               PointerType::getUnqual(Type::getInt8Ty(M.getContext())), "", &*I);
    // Create the call to free function
                            CallInst::Create(free_func, free_PtrCast, "", &*I);
                            Instruction *II=dyn_cast<Instruction>(&*I);
                            II->replaceAllUsesWith(free_PtrCast);

                        }

                    }

3. Example on which I am testing

    void* ptr;
    void malloc_func()
    {
        ptr = malloc(512);
    }

    void free_func()
    {
        free(ptr);
    }
1.创建malloc并释放
类型*BPTy=PointerType::getUnqual(类型::getInt8Ty(M.getContext());
FunctionType*malloc_Fty=FunctionType::get(BPTy,llvm::ArrayRef(),true);
函数*malloc_func=cast(M.getOrInsertFunction(“malloc”,malloc_Fty));
FunctionType*free\u Fty=FunctionType::get(Type::getVoidTy(M.getContext()),BPTy,false);
函数*free_func=cast(M.getOrInsertFunction(“free”,free_Fty));
2.检测malloc和free并将其替换为新的malloc和free函数调用
if(CallInst*CI=dyn_cast(I)){
函数*Callee=CI->getCalledFunction();
如果(被调用方->getName()=“malloc”){
FunctionType*Ty=Callee->getFunctionType();
指针类型*PtrTy=dyn_cast(Ty);
Type*AllocTy=PtrTy->getElementType();
const-DataLayout&DL=M.getDataLayout();
类型*IntPtrTy=DL.getIntPtrType(M.getContext());
价值*mallocArgs;
mallocags=ConstantExpr::getSizeOf(AllocTy);
mallocags=ConstantExpr::getTruncOrBitCast(cast(mallocags),IntPtrTy);
如果(常数AllocaInst*AI=dyn_cast(&*I)){
如果(AI->isArrayAllocation()){
if(isa(mallocArgs)&&cast(mallocArgs)->isOne(){
mallocArgs=I->GetOperator(0);
}else if(常数*CO=dyn_cast(I->getOperand(0))){
CO=ConstantExpr::getIntegerCast(CO,IntPtrTy,false);
mallocArgs=ConstantExpr::getMul(公司,铸造(mallocArgs));
}否则{
值*Scale=I->GetOperator(0);
如果(缩放->获取类型()!=IntPtrTy)
Scale=CastInst::CreateIntegerCast(Scale,IntPtrTy,false,“,&*I);
//如果需要,将其乘以数组大小。。。
mallocArgs=BinaryOperator::Create(指令::Mul,Scale,mallocArgs,“,&*I);
}
}
}
//创建对malloc的调用
CallInst*malloc_Call=CallInst::Create(malloc_func,mallocgs,“,&*I);
//强制转换指令以转换为正确的类型
值*malloc_PtrCast;
Type*voidty=Type::getVoidTy(M.getContext());
if(malloc_Call->getType()!=voidty)
malloc_PtrCast=newbitcastinst(malloc_调用,I->getType(),“”,&*I);
其他的
malloc_PtrCast=Constant::getNullValue(I->getType());
指令*II=动态铸件(&*I);
II->替换为(malloc_PtrCast);
}
如果(被调用方->getName()=“自由”){
值*自由值=
新的BitCastInst(I->GetOperator(0),指针类型::getUnqual(类型::getInt8Ty(M.getContext()),“”,&*I);
//创建对free函数的调用
CallInst::Create(free_func,free_PtrCast,“,&*I);
指令*II=动态铸件(&*I);
II->替换为(免费);
}
}
3.我正在测试的示例
无效*ptr;
void malloc_func()
{
ptr=malloc(512);
}
void free_func()
{
免费(ptr);
}
但是,我遇到了分段错误,这是用新函数替换malloc和free函数调用的正确方法吗