开发者

How to get the return address on the stack on LLVM

I want to implement the Xor random canary, so I have to get the return address in the prologue and epilogue of the function.

In the prologue of the function, before I insert into the canary on the stack, I can get the return address by:

ConstantInt* ci = llvm::ConstantInt::get(Type::getInt32Ty(RI->getContext()), 0);
Value* Args1[] = {ci};
CallInst* callInst = CallInst::Create(Intrinsic::getDeclaration(M, Intrinsic::returnaddress),
               &Args1[0], array_endof(Args1), "Call Return Address", InsPt);

callInst will get the return address and it works.

While, in the epilogue of the function, due to the canary has been inserted. I write the similar code:

ConstantInt* ci2 = llvm::ConstantInt::get(Type::getInt32Ty(RI->getContext()), 1);
Value* Args3[] = {ci2};
CallInst* callInst1 = CallInst::Create(Intrinsic::getDeclaration(M,    Intrinsic::returnaddress),
             &Args3[0], array_endof(Args3), "Caaall Return Address", BB);

But i开发者_开发百科t does not work this time. I cannot get the return address.

What is problem? How can I get the return address?


I don't know why you do this but in the epilogue, you are calling

llvm.returnaddress i32 1

which tries to get the return address of the previous function on the call stack. Even though you inserted a canary, you still want the return address of the current function in the epilogue. So you should, like you do in the prologue, call

 llvm.returnaddress i32 0

Just as a side note, calling llvm.returnaddress with an argument other than 0 will probably not work. From the docs:

The value returned by this intrinsic is likely to be incorrect or 0 for arguments other than zero, so it should only be used for debugging purposes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜