aboutsummaryrefslogtreecommitdiffhomepage
path: root/samples/region/region.cpp
diff options
context:
space:
mode:
authorAndrzej Janik <[email protected]>2021-01-03 17:52:14 +0100
committerAndrzej Janik <[email protected]>2021-01-03 17:52:14 +0100
commitdabc40cb19bf4e297c32284d26c74adbd6775e49 (patch)
tree6f71c14ef264998a9bccb3b6891c71542d9ace7a /samples/region/region.cpp
downloadZLUDA-dabc40cb19bf4e297c32284d26c74adbd6775e49.tar.gz
ZLUDA-dabc40cb19bf4e297c32284d26c74adbd6775e49.zip
Squashed 'ext/detours/' content from commit 39aa864
git-subtree-dir: ext/detours git-subtree-split: 39aa864d2985099c8d847e29a5fb86618039b9c4
Diffstat (limited to 'samples/region/region.cpp')
-rw-r--r--samples/region/region.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/samples/region/region.cpp b/samples/region/region.cpp
new file mode 100644
index 0000000..d23f583
--- /dev/null
+++ b/samples/region/region.cpp
@@ -0,0 +1,104 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// Test the different system region bounds (region.cpp of region.exe)
+//
+// Microsoft Research Detours Package
+//
+// Copyright (c) Microsoft Corporation. All rights reserved.
+//
+#include <stdio.h>
+
+#include <windows.h>
+#include <detours.h>
+
+//////////////////////////////////////////////////////////////////////////////
+//
+static DWORD (WINAPI * TrueSleepEx)(DWORD dwMilliseconds, BOOL bAlertable) = SleepEx;
+
+DWORD WINAPI LoudSleepEx(DWORD dwMilliseconds, BOOL bAlertable)
+{
+ DWORD dwBeg = GetTickCount();
+ DWORD ret = TrueSleepEx(dwMilliseconds, bAlertable);
+ DWORD dwEnd = GetTickCount();
+
+ printf("Slept %lu ticks.\n", dwEnd - dwBeg);
+ return ret;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+//
+PVOID AttachAndDetach(DWORD dwMilliseconds)
+{
+ LONG error;
+ PVOID trampoline;
+
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ DetourAttach(&(PVOID&)TrueSleepEx, LoudSleepEx);
+ error = DetourTransactionCommit();
+
+ printf("Attach: %ld, Trampoline: %p\n", error, TrueSleepEx);
+
+ trampoline = TrueSleepEx;
+
+ printf("\n");
+ printf("Sleep(%lu)\n", dwMilliseconds);
+ Sleep(dwMilliseconds);
+ printf("\n");
+
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+ DetourDetach(&(PVOID&)TrueSleepEx, LoudSleepEx);
+ error = DetourTransactionCommit();
+
+ return trampoline;
+}
+
+int main(int argc, char **argv)
+{
+ (void)argc;
+ (void)argv;
+
+ // First, save the default system region.
+
+ PVOID pDefaultLower = DetourSetSystemRegionLowerBound(NULL);
+ PVOID pDefaultUpper = DetourSetSystemRegionUpperBound(NULL);
+
+ // Now attach the detour with the default system region.
+
+ DetourSetSystemRegionLowerBound(pDefaultLower);
+ DetourSetSystemRegionUpperBound(pDefaultUpper);
+
+ printf("%p..%p: ", pDefaultLower, pDefaultUpper);
+ PVOID pTramp1 = AttachAndDetach(10);
+
+ printf("%p..%p: ", pDefaultLower, pDefaultUpper);
+ PVOID pTramp2 = AttachAndDetach(10);
+
+ // Now attach the detour with a smaller system region.
+
+ PVOID pSmallerLower = (PVOID)( ((ULONG_PTR)pTramp1) & ~(ULONG_PTR)0x3fffffff );
+ PVOID pSmallerUpper = (PVOID)( ((ULONG_PTR)pTramp1 + 0x3fffffff) & ~(ULONG_PTR)0x3fffffff );
+
+ DetourSetSystemRegionLowerBound(pSmallerLower);
+ DetourSetSystemRegionUpperBound(pSmallerUpper);
+
+ printf("%p..%p: ", pSmallerLower, pSmallerUpper);
+ PVOID pTramp3 = AttachAndDetach(20);
+
+ printf("Sleep(30)\n");
+ Sleep(30);
+ printf("\n");
+
+ if (pTramp1 != pTramp2) {
+ printf("!!!!!! Trampoling allocation is not deterministic. %p != %p\n", pTramp1, pTramp2);
+ return 1;
+ }
+ else if (pTramp2 == pTramp3) {
+ printf("!!!!!! Trampoling allocation doesn't skip region. %p == %p\n", pTramp2, pTramp3);
+ return 2;
+ }
+
+ return 0;
+}
+