码迷,mamicode.com
首页 > 其他好文 > 详细

_ReturnAddress 使用

时间:2015-01-24 22:50:28      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:returnaddress

msdn:

The _ReturnAddress intrinsic provides the address of the instruction in the calling function that will be executed after control returns to the caller.

_ReturnAddress 返回当前调用函数返回的地址,即函数吓一跳指令地址

读起来有些拗口,我们通过实例观察下

代码:

// compiler_intrinsics__ReturnAddress.cpp
#include <stdio.h>
#include <windows.h>
#include <intrin.h>

#pragma intrinsic(_ReturnAddress)
__declspec(noinline) ULONG_PTR caller( VOID ) { return (ULONG_PTR)_ReturnAddress(); }

	__declspec(noinline)
	void noinline_func(void)
{
	 void *callerAddress = _ReturnAddress();
	printf("Return address from %s: %p\n", __FUNCTION__, callerAddress);
}

__forceinline
	void inline_func(void)
{
	 void *callerAddress = _ReturnAddress();
	printf("Return address from %s: %p\n", __FUNCTION__, callerAddress);
}

int main(void)
{
	noinline_func(); 
	ULONG_PTR uiLibraryAddress = caller();
	__asm
	{
		nop
		nop
		nop
		nop
	}
	inline_func();
	printf("Return address from %s: %p\n", __FUNCTION__, _ReturnAddress());
	printf("noinlineaddr = %p, inline_funcaddr = %p,uiLibraryAddress = %p \n",noinline_func,inline_func,uiLibraryAddress);
	system("pause");
	return 0;
}

技术分享

程序中函数noinline_func()调用了 ReturnAddress(), ReturnAddress()返回调用函数noinline_func()的下条指令的地址,即001D1483

程序中函数inline_func()调用了 ReturnAddress(), ReturnAddress()返回调用函数inline_func()的下条指令的地址,即001D1494

caller()函数调用了 ReturnAddress()函数,返回地址为caller()下一条指令地址,001D1488

其他同样。。。。,

运行结果如下:

技术分享


_ReturnAddress 使用

标签:returnaddress

原文地址:http://blog.csdn.net/hjxyshell/article/details/43092457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!