llvm存取fs段内容

来源:互联网 发布:电子画板手绘软件 编辑:程序博客网 时间:2024/06/10 20:31

问题

最近想使用llvm的pass来对每个函数进行插桩,来模拟stack guard的功能。其中碰到了一个很棘手的问题就是如何通过llvm C API实现对fs段内容的存取。要想实现该问题,我通过查看llvm如何实现stack guard,找到了具体的解决方案。下面首先看一下llvm中stackProtector pass如何实现对fs段的存取,然后再介绍一下我自己简化版本的Stack guard。

stackProtector对fs段的存取

在stackProtector中,调用getIRStackGuard(IRBuilder<> &IRB) const函数对fs段进行存取的,其在x86下实现的具体代码如下:
fs_fetch

fs_address

其中fs段的addressSpace为257

实现存取

所以对fs段的存取代码如下:

//Address为257表示在用户模式下的fs段寄存器static Constant* SegmentOffsetStack(IRBuilder<> &IRB, unsigned Offset, unsigned AddressSpace) {    return ConstantExpr::getIntToPtr(        ConstantInt::get(Type::getInt32Ty(IRB.getContext()), Offset),        Type::getInt8PtrTy(IRB.getContext())->getPointerTo(AddressSpace)    );}static Value* getTheStackGuardValue(IRBuilder<> &IRB, unsigned offset) {    return SegmentOffsetStack(IRB, offset, 257);}

其中offset为从fs段偏移offset处取数据。

下附我对stack guard的一个简单的实现:
stackguard