資源簡(jiǎn)介
C語(yǔ)言UT單元測(cè)試中需要實(shí)現(xiàn)動(dòng)態(tài),本代碼實(shí)現(xiàn)了該功能,原理和網(wǎng)絡(luò)上的描述一樣,通過(guò)修改機(jī)器指令實(shí)現(xiàn)。
平臺(tái)是Linux
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“stub.h“
static?long?pagesize?=?-1;
static?inline?void?*pageof(const?void*?p)
{?
return?(void?*)((unsigned?long)p?&?~(pagesize?-?1));
}
void?stub_set(struct?func_stub?*pstub?void?*fn?void?*fn_stub)
{
????pstub->fn?=?fn;
????memcpy(pstub->code_buf?fn?CODESIZE);
????
????if?(-1?==?mprotect(pageof(fn)?pagesize?*?2?PROT_READ?|?PROT_WRITE?|?PROT_EXEC))
????{
????????perror(“mprotect?to?w+r+x?faild“);
????????exit(errno);
????}
*(unsigned?char?*)fn?=?(unsigned?char)0xE9;
????*(unsigned?int?*)((unsigned?char?*)fn?+?1)?=?(unsigned?char?*)fn_stub?-?(unsigned?char?*)fn?-?CODESIZE;
????
????if?(-1?==?mprotect(pageof(fn)?pagesize?*?2?PROT_
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????1643??2014-07-27?17:29??stub\stub.c
?????文件?????????319??2014-07-27?17:22??stub\stub.h
?????文件?????????670??2014-07-27?17:30??stub\test_stub.c
?????目錄???????????0??2014-07-28?00:46??stub\
評(píng)論
共有 條評(píng)論