Guest User

Untitled

a guest
Dec 27th, 2019
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class OVERLAPPED_WRONG : public OVERLAPPED
  2. {
  3.     ~OVERLAPPED_WRONG() // private !!
  4.     {
  5.         DbgPrint("%x>%s<%p>\n", GetCurrentThreadId(), __FUNCTION__, this);
  6.     }
  7.  
  8.     VOID IoCompletionWin32(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered)
  9.     {
  10.         DbgPrint("%x>%s(%x, %x)\n", GetCurrentThreadId(), __FUNCTION__, dwErrorCode, dwNumberOfBytesTransfered);
  11.         delete this;// !!!
  12.     }
  13.  
  14.     static VOID WINAPI IoCompletionNT(
  15.         _In_    DWORD dwErrorCode,//really NTSTATUS
  16.         _In_    DWORD dwNumberOfBytesTransfered,
  17.         _Inout_ LPOVERLAPPED lpOverlapped
  18.         )
  19.     {
  20.         DbgPrint("%x> !! %s(%x, %x)\n", GetCurrentThreadId(), __FUNCTION__, dwErrorCode, dwNumberOfBytesTransfered);
  21.         static_cast<OVERLAPPED_WRONG*>(lpOverlapped)->IoCompletionWin32(RtlNtStatusToDosError(dwErrorCode), dwNumberOfBytesTransfered);
  22.     }
  23. public:
  24.     OVERLAPPED_WRONG()
  25.     {
  26.         Internal = STATUS_PENDING;
  27.         InternalHigh = 0;
  28.         hEvent = 0;
  29.         Offset = 0, OffsetHigh = 0;
  30.         DbgPrint("%x>%s<%p>\n", GetCurrentThreadId(), __FUNCTION__, this);
  31.     }
  32.  
  33.     void CheckError(BOOL fOk, BOOL bSkipOnSynchronous)
  34.     {
  35.         CheckError(fOk ? NOERROR : GetLastError(), bSkipOnSynchronous);
  36.     }
  37.  
  38.     void CheckError(ULONG dwErrorCode, BOOL bSkipOnSynchronous)
  39.     {
  40.         switch (dwErrorCode)
  41.         {
  42.         case ERROR_IO_PENDING:
  43.             return;
  44.         case NOERROR:
  45.             if (!bSkipOnSynchronous) return;
  46.             [[fallthrough]];
  47.         default:
  48.             IoCompletionWin32(dwErrorCode, (ULONG)InternalHigh);
  49.         }
  50.     }
  51.  
  52.     static BOOL BindIoCompletionCallback (HANDLE FileHandle)
  53.     {
  54.         return ::BindIoCompletionCallback(FileHandle, IoCompletionNT, 0);
  55.     }
  56. };
  57.  
  58. class OVERLAPPED_OK : public OVERLAPPED
  59. {
  60.     LONG dwRefCount;
  61.  
  62.     void Release()
  63.     {
  64.         if (!InterlockedDecrement(&dwRefCount)) delete this;
  65.     }
  66.  
  67.     ~OVERLAPPED_OK() // private !!
  68.     {
  69.         DbgPrint("%x>%s<%p>\n", GetCurrentThreadId(), __FUNCTION__, this);
  70.     }
  71.  
  72.     VOID IoCompletionWin32(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered)
  73.     {
  74.         DbgPrint("%x>%s(%x, %x)\n", GetCurrentThreadId(), __FUNCTION__, dwErrorCode, dwNumberOfBytesTransfered);
  75.         Release();
  76.     }
  77.  
  78.     static VOID WINAPI IoCompletionNT(
  79.         _In_    DWORD dwErrorCode,//really NTSTATUS
  80.         _In_    DWORD dwNumberOfBytesTransfered,
  81.         _Inout_ LPOVERLAPPED lpOverlapped
  82.         )
  83.     {
  84.         DbgPrint("%x> !! %s(%x, %x)\n", GetCurrentThreadId(), __FUNCTION__, dwErrorCode, dwNumberOfBytesTransfered);
  85.         static_cast<OVERLAPPED_OK*>(lpOverlapped)->IoCompletionWin32(RtlNtStatusToDosError(dwErrorCode), dwNumberOfBytesTransfered);
  86.     }
  87. public:
  88.     OVERLAPPED_OK() : dwRefCount(2)
  89.     {
  90.         Internal = STATUS_PENDING;
  91.         InternalHigh = 0;
  92.         hEvent = 0;
  93.         Offset = 0, OffsetHigh = 0;
  94.         DbgPrint("%x>%s<%p>\n", GetCurrentThreadId(), __FUNCTION__, this);
  95.     }
  96.  
  97.     void CheckError(BOOL fOk, BOOL bSkipOnSynchronous)
  98.     {
  99.         CheckError(fOk ? NOERROR : GetLastError(), bSkipOnSynchronous);
  100.     }
  101.  
  102.     void CheckError(ULONG dwErrorCode, BOOL bSkipOnSynchronous)
  103.     {
  104.         if (dwErrorCode != ERROR_IO_PENDING && (Internal == STATUS_PENDING || bSkipOnSynchronous))
  105.         {
  106.             IoCompletionWin32(dwErrorCode, (ULONG)InternalHigh);
  107.         }
  108.  
  109.         Release();
  110.     }
  111.  
  112.     static BOOL BindIoCompletionCallback (HANDLE FileHandle)
  113.     {
  114.         return ::BindIoCompletionCallback(FileHandle, IoCompletionNT, 0);
  115.     }
  116. };
  117.  
  118. #if 0
  119. #define OVERLAPPED_ OVERLAPPED_WRONG
  120. #else
  121. #define OVERLAPPED_ OVERLAPPED_OK
  122. #endif
  123.  
  124. void LockTest(HANDLE hFile, DWORD dwReserved, bool bSkipOnSynchronous)
  125. {
  126.     if (OVERLAPPED_* lpOverlapped = new OVERLAPPED_)
  127.     {
  128.         lpOverlapped->CheckError(
  129.             LockFileEx(hFile, 0, dwReserved, 1, 0, lpOverlapped), bSkipOnSynchronous);
  130.     }
  131.  
  132.     MessageBoxW(0, 0, 0, 0);
  133.     DbgPrint("\n=============================\n");
  134. }
  135.  
  136.  
  137.         HANDLE hFile = CreateFileW(L"c:/", FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING,
  138.             FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, 0);
  139.  
  140.         if (hFile != INVALID_HANDLE_VALUE)
  141.         {
  142.             if (OVERLAPPED_::BindIoCompletionCallback(hFile))
  143.             {
  144.                 LockTest(hFile, !0, false);//invalid value !0. error from user mode
  145.                 LockTest(hFile, 0, false);
  146.  
  147.                 if (SetFileCompletionNotificationModes(hFile, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS))
  148.                 {
  149.                     LockTest(hFile, 0, true);
  150.                 }
  151.             }
  152.             CloseHandle(hFile);
  153.         }
  154.  
  155. from wrong
  156.  
  157. cc0>OVERLAPPED_WRONG::OVERLAPPED_WRONG<000001593786E7B0>
  158. cc0>OVERLAPPED_WRONG::IoCompletionWin32(57, 0)
  159. cc0>OVERLAPPED_WRONG::~OVERLAPPED_WRONG<000001593786E7B0>
  160.  
  161. =============================
  162. cc0>OVERLAPPED_WRONG::OVERLAPPED_WRONG<000001593786E7B0>
  163. cc0>OVERLAPPED_WRONG::IoCompletionWin32(57, 0)
  164. cc0>OVERLAPPED_WRONG::~OVERLAPPED_WRONG<000001593786E7B0>
  165. 1600> !! OVERLAPPED_WRONG::IoCompletionNT(c000000d, 0)
  166. 1600>OVERLAPPED_WRONG::IoCompletionWin32(57, 0)
  167. 1600>OVERLAPPED_WRONG::~OVERLAPPED_WRONG<000001593786E7B0>
  168.  
  169. =============================
  170. cc0>OVERLAPPED_WRONG::OVERLAPPED_WRONG<000001593786E7B0>
  171. cc0>OVERLAPPED_WRONG::IoCompletionWin32(57, 0)
  172. cc0>OVERLAPPED_WRONG::~OVERLAPPED_WRONG<000001593786E7B0>
  173.  
  174. =============================
  175.  
  176. from ok
  177.  
  178. 17a8>OVERLAPPED_OK::OVERLAPPED_OK<0000017190BD0F50>
  179. 17a8>OVERLAPPED_OK::IoCompletionWin32(57, 0)
  180. 17a8>OVERLAPPED_OK::~OVERLAPPED_OK<0000017190BD0F50>
  181.  
  182. =============================
  183. 17a8>OVERLAPPED_OK::OVERLAPPED_OK<0000017190BD0F50>
  184. 1a80> !! OVERLAPPED_OK::IoCompletionNT(c000000d, 0)
  185. 1a80>OVERLAPPED_OK::IoCompletionWin32(57, 0)
  186. 1a80>OVERLAPPED_OK::~OVERLAPPED_OK<0000017190BD0F50>
  187.  
  188. =============================
  189. 17a8>OVERLAPPED_OK::OVERLAPPED_OK<0000017190BD0F50>
  190. 17a8>OVERLAPPED_OK::IoCompletionWin32(57, 0)
  191. 17a8>OVERLAPPED_OK::~OVERLAPPED_OK<0000017190BD0F50>
  192.  
  193. =============================
RAW Paste Data