Skip to content

Commit db38cdc

Browse files
committed
Minor changes
1 parent d266292 commit db38cdc

File tree

5 files changed

+154
-86
lines changed

5 files changed

+154
-86
lines changed

Detours.cpp

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ namespace Detours {
190190
typedef struct _MEMORY_HOOK_RECORD {
191191
_MEMORY_HOOK_RECORD() {
192192
m_pCallBack = nullptr;
193+
m_pPostCallBack = nullptr;
193194
m_pAddress = nullptr;
194195
m_unSize = 0;
195196
m_unActiveThreads.store(0, std::memory_order_relaxed);
@@ -198,6 +199,7 @@ namespace Detours {
198199
}
199200

200201
fnMemoryHookCallBack m_pCallBack;
202+
fnMemoryHookCallBack m_pPostCallBack;
201203
void* m_pAddress;
202204
size_t m_unSize;
203205
std::deque<std::unique_ptr<Page>> m_Pages;
@@ -206,6 +208,24 @@ namespace Detours {
206208
SRWLOCK m_Lock;
207209
} MEMORY_HOOK_RECORD, *PMEMORY_HOOK_RECORD;
208210

211+
// ----------------------------------------------------------------
212+
// MEMORY_HOOK_POST_CTX
213+
// ----------------------------------------------------------------
214+
215+
typedef struct _MEMORY_HOOK_POST_CTX {
216+
_MEMORY_HOOK_POST_CTX() {
217+
m_pRecord = nullptr;
218+
m_pExceptionAddress = nullptr;
219+
m_pFaultAddress = nullptr;
220+
m_unOperation = MEMORY_HOOK_OPERATION::MEMORY_READ;
221+
}
222+
223+
PMEMORY_HOOK_RECORD m_pRecord;
224+
void* m_pExceptionAddress;
225+
void* m_pFaultAddress;
226+
MEMORY_HOOK_OPERATION m_unOperation;
227+
} MEMORY_HOOK_POST_CTX, *PMEMORY_HOOK_POST_CTX;
228+
209229
// ----------------------------------------------------------------
210230
// INTERRUPT_HOOK_RECORD
211231
// ----------------------------------------------------------------
@@ -233,6 +253,7 @@ namespace Detours {
233253
// ----------------------------------------------------------------
234254

235255
static std::unordered_map<DWORD, std::vector<PMEMORY_HOOK_RECORD>> g_MemoryHookOpenedStacks;
256+
static std::unordered_map<DWORD, std::vector<MEMORY_HOOK_POST_CTX>> g_MemoryHookPostStacks;
236257

237258
static std::deque<std::unique_ptr<HARDWARE_HOOK_RECORD>> g_HardwareHookRecords;
238259
static std::deque<std::unique_ptr<MEMORY_HOOK_RECORD>> g_MemoryHookRecords;
@@ -7390,9 +7411,9 @@ namespace Detours {
73907411
eflags.m_unTF = 0;
73917412

73927413
PMEMORY_HOOK_RECORD pRecord = nullptr;
7414+
MEMORY_HOOK_POST_CTX PostCTX = {};
73937415

73947416
AcquireSRWLockExclusive(&g_MemoryHookStacksLock);
7395-
73967417
{
73977418
auto it = g_MemoryHookOpenedStacks.find(unCurrentTID);
73987419
if (it != g_MemoryHookOpenedStacks.end() && !it->second.empty()) {
@@ -7402,18 +7423,44 @@ namespace Detours {
74027423
g_MemoryHookOpenedStacks.erase(it);
74037424
}
74047425
}
7405-
}
74067426

7427+
auto jt = g_MemoryHookPostStacks.find(unCurrentTID);
7428+
if (jt != g_MemoryHookPostStacks.end() && !jt->second.empty()) {
7429+
auto& vecStacks = jt->second;
7430+
7431+
if (!vecStacks.empty() && vecStacks.back().m_pRecord == pRecord) {
7432+
PostCTX = vecStacks.back();
7433+
vecStacks.pop_back();
7434+
} else {
7435+
for (size_t k = vecStacks.size(); k > 0; --k) {
7436+
if (vecStacks[k - 1].m_pRecord == pRecord) {
7437+
PostCTX = vecStacks[k - 1];
7438+
vecStacks.erase(vecStacks.begin() + (k - 1));
7439+
break;
7440+
}
7441+
}
7442+
}
7443+
7444+
if (vecStacks.empty()) {
7445+
g_MemoryHookPostStacks.erase(jt);
7446+
}
7447+
}
7448+
}
74077449
ReleaseSRWLockExclusive(&g_MemoryHookStacksLock);
74087450

74097451
if (!pRecord) {
74107452
return false;
74117453
}
74127454

7455+
if (pRecord->m_pPostCallBack) {
7456+
if (PostCTX.m_pFaultAddress && __is_in_range(pRecord->m_pAddress, pRecord->m_unSize, PostCTX.m_pFaultAddress)) {
7457+
pRecord->m_pPostCallBack(pCTX, PostCTX.m_pExceptionAddress ? PostCTX.m_pExceptionAddress : reinterpret_cast<void*>(Exception.ExceptionAddress), PostCTX.m_unOperation, pRecord->m_pAddress, PostCTX.m_pFaultAddress);
7458+
}
7459+
}
7460+
74137461
bool bNeedErase = false;
74147462

74157463
AcquireSRWLockExclusive(&pRecord->m_Lock);
7416-
74177464
{
74187465
const uint32_t unPrev = pRecord->m_unActiveThreads.fetch_sub(1, std::memory_order_acq_rel);
74197466
if (unPrev == 1) {
@@ -7429,19 +7476,15 @@ namespace Detours {
74297476
}
74307477
}
74317478
}
7432-
74337479
ReleaseSRWLockExclusive(&pRecord->m_Lock);
74347480

74357481
if (bNeedErase) {
74367482
AcquireSRWLockExclusive(&g_MemoryHookRecordsLock);
7437-
7438-
for (auto it = g_MemoryHookRecords.begin(); it != g_MemoryHookRecords.end(); ++it) {
7439-
if (it->get() == pRecord) {
7440-
g_MemoryHookRecords.erase(it);
7441-
break;
7483+
{
7484+
for (auto it = g_MemoryHookRecords.begin(); it != g_MemoryHookRecords.end(); ++it) {
7485+
if (it->get() == pRecord) { g_MemoryHookRecords.erase(it); break; }
74427486
}
74437487
}
7444-
74457488
ReleaseSRWLockExclusive(&g_MemoryHookRecordsLock);
74467489
}
74477490

@@ -7474,7 +7517,6 @@ namespace Detours {
74747517
PMEMORY_HOOK_RECORD pTargetRecord = nullptr;
74757518

74767519
AcquireSRWLockShared(&g_MemoryHookRecordsLock);
7477-
74787520
{
74797521
for (const auto& pRecord : g_MemoryHookRecords) {
74807522
if (!pRecord) {
@@ -7493,15 +7535,13 @@ namespace Detours {
74937535
}
74947536
}
74957537
}
7496-
74977538
ReleaseSRWLockShared(&g_MemoryHookRecordsLock);
74987539

74997540
if (!pTargetRecord) {
75007541
return false;
75017542
}
75027543

75037544
AcquireSRWLockExclusive(&pTargetRecord->m_Lock);
7504-
75057545
{
75067546
const uint32_t unPrev = pTargetRecord->m_unActiveThreads.load(std::memory_order_acquire);
75077547
if (unPrev == 0) {
@@ -7512,13 +7552,24 @@ namespace Detours {
75127552
}
75137553
}
75147554
}
7555+
75157556
pTargetRecord->m_unActiveThreads.fetch_add(1, std::memory_order_acq_rel);
75167557
}
7517-
75187558
ReleaseSRWLockExclusive(&pTargetRecord->m_Lock);
75197559

75207560
AcquireSRWLockExclusive(&g_MemoryHookStacksLock);
7521-
g_MemoryHookOpenedStacks[unCurrentTID].push_back(pTargetRecord);
7561+
{
7562+
g_MemoryHookOpenedStacks[unCurrentTID].push_back(pTargetRecord);
7563+
7564+
MEMORY_HOOK_POST_CTX PostCTX = {};
7565+
7566+
PostCTX.m_pRecord = pTargetRecord;
7567+
PostCTX.m_pExceptionAddress = pExceptionAddress;
7568+
PostCTX.m_pFaultAddress = pFaultAddress;
7569+
PostCTX.m_unOperation = unOperation;
7570+
7571+
g_MemoryHookPostStacks[unCurrentTID].push_back(PostCTX);
7572+
}
75227573
ReleaseSRWLockExclusive(&g_MemoryHookStacksLock);
75237574

75247575
eflags.m_unTF = 1;
@@ -84917,7 +84968,7 @@ namespace Detours {
8491784968
// Memory Hook
8491884969
// ----------------------------------------------------------------
8491984970

84920-
bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize) {
84971+
bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize, const fnMemoryHookCallBack pPostCallBack) {
8492184972
if (!g_Suspender.Suspend()) {
8492284973
return false;
8492384974
}
@@ -84990,6 +85041,7 @@ namespace Detours {
8499085041
}
8499185042

8499285043
pRecord->m_pCallBack = pCallBack;
85044+
pRecord->m_pPostCallBack = pPostCallBack;
8499385045
pRecord->m_pAddress = pAddress;
8499485046
pRecord->m_unSize = unSize;
8499585047

Detours.h

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5570,8 +5570,8 @@ namespace Detours {
55705570

55715571
typedef enum _HARDWARE_HOOK_TYPE : unsigned char {
55725572
TYPE_EXECUTE = 0,
5573-
TYPE_WRITE = 1,
5574-
TYPE_ACCESS = 3
5573+
TYPE_WRITE = 1,
5574+
TYPE_ACCESS = 3
55755575
} HARDWARE_HOOK_TYPE, *PHARDWARE_HOOK_TYPE;
55765576

55775577
// ----------------------------------------------------------------
@@ -5592,8 +5592,8 @@ namespace Detours {
55925592
// ----------------------------------------------------------------
55935593

55945594
typedef enum _MEMORY_HOOK_OPERATION : unsigned char {
5595-
MEMORY_READ = 0,
5596-
MEMORY_WRITE = 1,
5595+
MEMORY_READ = 0,
5596+
MEMORY_WRITE = 1,
55975597
MEMORY_EXECUTE = 2
55985598
} MEMORY_HOOK_OPERATION, *PMEMORY_HOOK_OPERATION;
55995599

@@ -5607,7 +5607,7 @@ namespace Detours {
56075607
// Memory Hook
56085608
// ----------------------------------------------------------------
56095609

5610-
bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize);
5610+
bool HookMemory(const fnMemoryHookCallBack pCallBack, void* pAddress, size_t unSize, const fnMemoryHookCallBack pPostCallBack = nullptr);
56115611
bool UnHookMemory(const fnMemoryHookCallBack pCallBack);
56125612

56135613
// ----------------------------------------------------------------
@@ -5873,30 +5873,30 @@ namespace Detours {
58735873
unsigned int m_unEFLAGS;
58745874
unsigned short m_unFLAGS;
58755875
struct {
5876-
unsigned int m_unCF : 1; // Bit 0: Carry Flag
5877-
unsigned int : 1; // Bit 1: Reserved
5878-
unsigned int m_unPF : 1; // Bit 2: Parity Flag
5879-
unsigned int : 1; // Bit 3: Reserved
5880-
unsigned int m_unAF : 1; // Bit 4: Auxiliary Carry Flag
5881-
unsigned int : 1; // Bit 5: Reserved
5882-
unsigned int m_unZF : 1; // Bit 6: Zero Flag
5883-
unsigned int m_unSF : 1; // Bit 7: Sign Flag
5884-
unsigned int m_unTF : 1; // Bit 8: Trap Flag
5885-
unsigned int m_unIF : 1; // Bit 9: Interrupt Enable Flag
5886-
unsigned int m_unDF : 1; // Bit 10: Direction Flag
5887-
unsigned int m_unOF : 1; // Bit 11: Overflow Flag
5888-
unsigned int m_unIOPL : 2; // Bit 12-13: I/O Privilege Level
5889-
unsigned int m_unNT : 1; // Bit 14: Nested Task
5890-
unsigned int m_unMD : 1; // Bit 15: Mode Flag
5891-
unsigned int m_unRF : 1; // Bit 16: Resume Flag
5892-
unsigned int m_unVM : 1; // Bit 17: Virtual 8086 Mode Flag
5893-
unsigned int m_unAC : 1; // Bit 18: Alignment Check
5894-
unsigned int m_unVIF : 1; // Bit 19: Virtual Interrupt Flag
5895-
unsigned int m_unVIP : 1; // Bit 20: Virtual Interrupt Pending
5896-
unsigned int m_unID : 1; // Bit 21: ID Flag
5897-
unsigned int : 8; // Bit 22-29: Reserved
5898-
unsigned int : 1; // Bit 30: Reserved
5899-
unsigned int m_unAI : 1; // Bit 31: Alignment Indicator
5876+
unsigned int m_unCF : 1; // Bit 0: Carry Flag
5877+
unsigned int : 1; // Bit 1: Reserved
5878+
unsigned int m_unPF : 1; // Bit 2: Parity Flag
5879+
unsigned int : 1; // Bit 3: Reserved
5880+
unsigned int m_unAF : 1; // Bit 4: Auxiliary Carry Flag
5881+
unsigned int : 1; // Bit 5: Reserved
5882+
unsigned int m_unZF : 1; // Bit 6: Zero Flag
5883+
unsigned int m_unSF : 1; // Bit 7: Sign Flag
5884+
unsigned int m_unTF : 1; // Bit 8: Trap Flag
5885+
unsigned int m_unIF : 1; // Bit 9: Interrupt Enable Flag
5886+
unsigned int m_unDF : 1; // Bit 10: Direction Flag
5887+
unsigned int m_unOF : 1; // Bit 11: Overflow Flag
5888+
unsigned int m_unIOPL : 2; // Bit 12-13: I/O Privilege Level
5889+
unsigned int m_unNT : 1; // Bit 14: Nested Task
5890+
unsigned int m_unMD : 1; // Bit 15: Mode Flag
5891+
unsigned int m_unRF : 1; // Bit 16: Resume Flag
5892+
unsigned int m_unVM : 1; // Bit 17: Virtual 8086 Mode Flag
5893+
unsigned int m_unAC : 1; // Bit 18: Alignment Check
5894+
unsigned int m_unVIF : 1; // Bit 19: Virtual Interrupt Flag
5895+
unsigned int m_unVIP : 1; // Bit 20: Virtual Interrupt Pending
5896+
unsigned int m_unID : 1; // Bit 21: ID Flag
5897+
unsigned int : 8; // Bit 22-29: Reserved
5898+
unsigned int : 1; // Bit 30: Reserved
5899+
unsigned int m_unAI : 1; // Bit 31: Alignment Indicator
59005900
};
59015901
};
59025902

@@ -6068,31 +6068,31 @@ namespace Detours {
60686068
unsigned int m_unEFLAGS;
60696069
unsigned short m_unFLAGS;
60706070
struct {
6071-
unsigned int m_unCF : 1; // Bit 0: Carry Flag
6072-
unsigned int : 1; // Bit 1: Reserved
6073-
unsigned int m_unPF : 1; // Bit 2: Parity Flag
6074-
unsigned int : 1; // Bit 3: Reserved
6075-
unsigned int m_unAF : 1; // Bit 4: Auxiliary Carry Flag
6076-
unsigned int : 1; // Bit 5: Reserved
6077-
unsigned int m_unZF : 1; // Bit 6: Zero Flag
6078-
unsigned int m_unSF : 1; // Bit 7: Sign Flag
6079-
unsigned int m_unTF : 1; // Bit 8: Trap Flag
6080-
unsigned int m_unIF : 1; // Bit 9: Interrupt Enable Flag
6081-
unsigned int m_unDF : 1; // Bit 10: Direction Flag
6082-
unsigned int m_unOF : 1; // Bit 11: Overflow Flag
6083-
unsigned int m_unIOPL : 2; // Bit 12-13: I/O Privilege Level
6084-
unsigned int m_unNT : 1; // Bit 14: Nested Task
6085-
unsigned int m_unMD : 1; // Bit 15: Mode Flag
6086-
unsigned int m_unRF : 1; // Bit 16: Resume Flag
6087-
unsigned int m_unVM : 1; // Bit 17: Virtual 8086 Mode Flag
6088-
unsigned int m_unAC : 1; // Bit 18: Alignment Check
6089-
unsigned int m_unVIF : 1; // Bit 19: Virtual Interrupt Flag
6090-
unsigned int m_unVIP : 1; // Bit 20: Virtual Interrupt Pending
6091-
unsigned int m_unID : 1; // Bit 21: ID Flag
6092-
unsigned int : 8; // Bit 22-29: Reserved
6093-
unsigned int : 1; // Bit 30: Reserved
6094-
unsigned int m_unAI : 1; // Bit 31: Alignment Indicator
6095-
unsigned int : 32; // Bit 32-63: Reserved
6071+
unsigned int m_unCF : 1; // Bit 0: Carry Flag
6072+
unsigned int : 1; // Bit 1: Reserved
6073+
unsigned int m_unPF : 1; // Bit 2: Parity Flag
6074+
unsigned int : 1; // Bit 3: Reserved
6075+
unsigned int m_unAF : 1; // Bit 4: Auxiliary Carry Flag
6076+
unsigned int : 1; // Bit 5: Reserved
6077+
unsigned int m_unZF : 1; // Bit 6: Zero Flag
6078+
unsigned int m_unSF : 1; // Bit 7: Sign Flag
6079+
unsigned int m_unTF : 1; // Bit 8: Trap Flag
6080+
unsigned int m_unIF : 1; // Bit 9: Interrupt Enable Flag
6081+
unsigned int m_unDF : 1; // Bit 10: Direction Flag
6082+
unsigned int m_unOF : 1; // Bit 11: Overflow Flag
6083+
unsigned int m_unIOPL : 2; // Bit 12-13: I/O Privilege Level
6084+
unsigned int m_unNT : 1; // Bit 14: Nested Task
6085+
unsigned int m_unMD : 1; // Bit 15: Mode Flag
6086+
unsigned int m_unRF : 1; // Bit 16: Resume Flag
6087+
unsigned int m_unVM : 1; // Bit 17: Virtual 8086 Mode Flag
6088+
unsigned int m_unAC : 1; // Bit 18: Alignment Check
6089+
unsigned int m_unVIF : 1; // Bit 19: Virtual Interrupt Flag
6090+
unsigned int m_unVIP : 1; // Bit 20: Virtual Interrupt Pending
6091+
unsigned int m_unID : 1; // Bit 21: ID Flag
6092+
unsigned int : 8; // Bit 22-29: Reserved
6093+
unsigned int : 1; // Bit 30: Reserved
6094+
unsigned int m_unAI : 1; // Bit 31: Alignment Indicator
6095+
unsigned int : 32; // Bit 32-63: Reserved
60966096
};
60976097
};
60986098

interrupts32.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
pop ebx
4040
pop edx
4141
pop ecx
42-
pop eax
42+
; pop eax
43+
add esp, 4
4344
ret
4445
CallInterrupt endp
4546

interrupts64.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
pop rbx
5858
pop rdx
5959
pop rcx
60-
pop rax
60+
; pop rax
61+
add rsp, 8
6162
ret
6263
CallInterrupt endp
6364

0 commit comments

Comments
 (0)