Skip to content

Commit 259f297

Browse files
committed
Fixes
1 parent 372530d commit 259f297

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

Detours.cpp

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5545,6 +5545,21 @@ namespace Detours {
55455545
return false;
55465546
};
55475547

5548+
// ----------------------------------------------------------------
5549+
// __is_range_in_range
5550+
// ----------------------------------------------------------------
5551+
5552+
static bool inline __is_range_in_range(void const* const pOuterRangeStart, const size_t unOuterRangeSize, void const* const pInnerRangeStart, const size_t unInnerRangeSize) {
5553+
if (!pOuterRangeStart || !pInnerRangeStart) {
5554+
return false;
5555+
}
5556+
5557+
const char* const pOuterRangeEnd = static_cast<const char* const>(pOuterRangeStart) + unOuterRangeSize;
5558+
const char* const pInnerRangeEnd = static_cast<const char* const>(pInnerRangeStart) + unInnerRangeSize;
5559+
5560+
return (pInnerRangeStart < pOuterRangeEnd) && (pOuterRangeStart < pInnerRangeEnd);
5561+
}
5562+
55485563
// ----------------------------------------------------------------
55495564
// Shared
55505565
// ----------------------------------------------------------------
@@ -84837,17 +84852,6 @@ namespace Detours {
8483784852
return false;
8483884853
}
8483984854

84840-
for (auto& pRecord : g_MemoryHookRecords) {
84841-
if (pRecord->m_bPendingDeletion) {
84842-
continue;
84843-
}
84844-
84845-
if (pRecord->m_pCallBack == pCallBack) {
84846-
g_Suspender.Resume();
84847-
return false;
84848-
}
84849-
}
84850-
8485184855
auto vecPages = __get_pages_info(pAddress, unSize, true);
8485284856
if (vecPages.empty()) {
8485384857
g_Suspender.Resume();
@@ -84871,6 +84875,36 @@ namespace Detours {
8487184875
return false;
8487284876
}
8487384877

84878+
for (auto& pRecord : g_MemoryHookRecords) {
84879+
if (pRecord->m_bPendingDeletion) {
84880+
continue;
84881+
}
84882+
84883+
if (pRecord->m_pCallBack == pCallBack) {
84884+
g_Suspender.Resume();
84885+
return false;
84886+
}
84887+
84888+
if (__is_range_in_range(pRecord->m_pAddress, pRecord->m_unSize, pAddress, unSize)) {
84889+
g_Suspender.Resume();
84890+
return false;
84891+
}
84892+
84893+
auto vecRecordPages = __get_pages_info(pRecord->m_pAddress, pRecord->m_unSize, true);
84894+
if (vecRecordPages.empty()) {
84895+
continue;
84896+
}
84897+
84898+
for (const auto& pPage : vecPages) {
84899+
for (const auto& pRecordPage : vecRecordPages) {
84900+
if (__is_range_in_range(pRecordPage.m_pBaseAddress, pRecordPage.m_unSize, pPage.m_pBaseAddress, pPage.m_unSize)) {
84901+
g_Suspender.Resume();
84902+
return false;
84903+
}
84904+
}
84905+
}
84906+
}
84907+
8487484908
auto pRecord = std::make_unique<MEMORY_HOOK_RECORD>();
8487584909
if (!pRecord) {
8487684910
g_Suspender.Resume();

main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ TEST_SUITE("Detours::Hook") {
15581558
void HardwareHook(const PCONTEXT pCTX) {
15591559
UNREFERENCED_PARAMETER(pCTX);
15601560

1561-
_tprintf_s(_T("Mem access! TID=%lu\n"), GetCurrentThreadId());
1561+
//_tprintf_s(_T("Mem access! TID=%lu\n"), GetCurrentThreadId());
15621562
}
15631563

15641564
void HardwareSelfUnHook(const PCONTEXT pCTX) {
@@ -1899,6 +1899,7 @@ TEST_SUITE("Detours::Hook") {
18991899
CHECK(hThread != INVALID_HANDLE_VALUE);
19001900

19011901
CHECK(Detours::Hook::HookMemory(MemoryHook, pArray, sizeof(int) * 3) == true);
1902+
CHECK(Detours::Hook::HookMemory(MemoryHook, pArray, sizeof(int) * 3) == false);
19021903

19031904
pArray[0] = 0xDEEDBEEF;
19041905
pArray[1] = 0xDEEDFACE;
@@ -1932,6 +1933,7 @@ TEST_SUITE("Detours::Hook") {
19321933
CHECK(hThread2 != INVALID_HANDLE_VALUE);
19331934

19341935
CHECK(Detours::Hook::HookMemory(MemoryHook, pArray, sizeof(int) * 3) == true);
1936+
CHECK(Detours::Hook::HookMemory(MemoryHook, pArray, sizeof(int) * 3) == false);
19351937

19361938
pArray[0] = 0xDEEDBEEF;
19371939
pArray[1] = 0xDEEDFACE;
@@ -1965,6 +1967,7 @@ TEST_SUITE("Detours::Hook") {
19651967
printf("pArray[4] = %i\n", pArray[4]);
19661968

19671969
CHECK(Detours::Hook::HookMemory(MemoryHookSelfUnHook, pArray, sizeof(int) * 3) == true);
1970+
CHECK(Detours::Hook::HookMemory(MemoryHookSelfUnHook, pArray, sizeof(int) * 3) == false);
19681971

19691972
printf("pArray[2] = %i\n", pArray[2]);
19701973
printf("pArray[3] = %i\n", pArray[3]);

0 commit comments

Comments
 (0)