読者です 読者をやめる 読者になる 読者になる

HARDCODED msvc++ 関数のサイズを調べる

#include "stdafx.h"

#define FUNC_GARBAGE \
	__asm _emit 0xEB \
	__asm _emit 0xEC \
	__asm _emit 0xED \
	__asm _emit 0xEF \

__declspec(naked) int __cdecl print_char()
{
	__asm
	{
		push ebp
		mov ebp,esp
		sub esp, __LOCAL_SIZE
		push ebx
		push edi
		push esi
	}
	
	char c;
	int ret;

	c = getchar();
	
	printf("%c\n", c);

	ret = 12456;

	__asm
	{
		mov eax, ret
		pop esi
		pop edi
		pop ebx
		mov esp,ebp
		pop ebp
		ret
	}

	FUNC_GARBAGE
}

int main()
{
	LPVOID lpvFunction = print_char;
	LPBYTE lpbStart, lpbEnd, lpbTemp;
	DWORD dw;
	BOOL fSuccess;

	// search garbage
	lpbStart = LPBYTE(lpvFunction);
	lpbEnd = lpbStart + 0x1000;
	fSuccess = FALSE;

	for (lpbTemp = lpbStart; lpbTemp < lpbEnd; lpbTemp++)
	{
		dw = *(LPDWORD)lpbTemp;
		if (dw == 0xEFEDECEB)
		{
			fSuccess = TRUE;
			break;
		}
	}

	if (fSuccess)
	{
		printf("start = %p, end = %p, size = %d\n", lpbStart, lpbTemp, lpbTemp - lpbStart);
	}

	int ret = print_char();
	printf("print_char ret : %d\n", ret);

	return 0;
}