Llvm 如何制作我自己的StackProtector?

Llvm 如何制作我自己的StackProtector?,llvm,Llvm,作为我大学项目的一部分,我正在LLVM中实现我自己的堆栈保护器。它不需要非常先进。我只需要通过编译器传递来防止数组溢出。我正在考虑在每次编译程序时将缓冲区的大小增加一个随机值(我知道这不是控制溢出的好方法,但正如我所说,它不需要非常高级)。我做了以下几行: for(Function::iterator Begin = F.begin(), End = F.end(); Begin!=End; ++Begin){ for(BasicBlock::iterator I = Begin -&g

作为我大学项目的一部分,我正在LLVM中实现我自己的堆栈保护器。它不需要非常先进。我只需要通过编译器传递来防止数组溢出。我正在考虑在每次编译程序时将缓冲区的大小增加一个随机值(我知道这不是控制溢出的好方法,但正如我所说,它不需要非常高级)。我做了以下几行:

for(Function::iterator Begin = F.begin(), End = F.end(); Begin!=End; ++Begin){
    for(BasicBlock::iterator I = Begin -> begin(), E = Begin -> end(); I != E; ++I){
        if(AllocaInst *instToReplace = dyn_cast<AllocaInst>(E){
            if(instToReplace -> isArrayAllocation()){
                randNo = rand() % 50 + 5;
                ReplaceInstWithInst(instToReplace -> getParent() -> getInstList, E, new AllocaInst(Type::ArrayType, randNo, "instToReplace");
            }
        }
    }
}
for(函数::迭代器Begin=F.Begin(),End=F.End();Begin!=End;++Begin){
对于(基本块::迭代器I=Begin->Begin(),E=Begin->end();I!=E;++I){
如果(AllocaInst*InstStoreReplace=dyn_铸造(E){
如果(instoreplace->isArrayAllocation()){
randNo=rand()%50+5;
ReplaceInstWithInst(instoreplace->getParent()->getInstList,E,新的AllocaInst(Type::ArrayType,randNo,“instoreplace”);
}
}
}
}
然而,我有两件事要做

  • LLVM没有
    rand()
    函数(至少在我看来是这样)
  • 我的
    ReplaceInstWithInst
    是错误的,因为我用不同大小的相同指令替换相同的指令(尽管这正是我想要的)

  • 有谁能帮助我了解这两个说明吗?

    你的意思是想添加一个额外的AllocInst而不是替换旧的吗?是的。事实上,我不知道添加一个额外的AllocInst不会导致数据更改。其他说明可能取决于AllocInst的大小。你可以修复Indexing(查找GetElementPtrInsts)。我建议查看AddressSanitizer在LLVM中的实现。它还使用红色区域进行堆栈分配。