Zubin pushed to branch wip/27971 at Glasgow Haskell Compiler / GHC Commits: 4183ff2e by Zubin Duggal at 2026-10-20T16:14:57+05:30 WinIO: Don't confuse Win32 errors and NTSTATUS codes WinIO misinterpreted NTSTATUS codes as Win32 errors, and had some ad hoc logic to deal with this. However, this logic was incomplete and resulted in the mishandling of results of certain I/O operations (see testcase). Now the `ErrCode` argument in `CompletionCallback` is always the Win32 error code, and we convert the NTSTATUS using RtlNtStatusToDosError. Also when an overlapped/async operation such as ReadFile fails immediately, with a Win32 error such as ERROR_BROKEN_PIPE, we don't get a further completion notification, so we never recorded the failure. This worked by coincidence earlier, because `STATUS_PENDING` happened to have the same numerical code as `ERROR_NO_MORE_ITEMS`, but it doesn't now that we translate `STATUS_PENDING` into the correct Win32 error code (`ERROR_IO_PENDING`). Fix this by using the Win32 error value we obtained when an operation returns `STATUS_PENDING`. Fixes #27971 - - - - - 7 changed files: - + changelog.d/27971 - + libraries/base/tests/IO/T27971.hs - + libraries/base/tests/IO/T27971.stdout - libraries/base/tests/IO/all.T - libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc - libraries/ghc-internal/src/GHC/Internal/Event/Windows/FFI.hsc - libraries/ghc-internal/src/GHC/Internal/IO/Windows/Handle.hsc Changes: ===================================== changelog.d/27971 ===================================== @@ -0,0 +1,5 @@ +section: base +synopsis: WinIO: Fix a bug where WinIO could confuse Win32 and NTSTATUS error codes +mrs: !16838 +issues: #27971 +description: This could lead to the result of certian I/O operations being misinterpreted, mishandled and misreported ===================================== libraries/base/tests/IO/T27971.hs ===================================== @@ -0,0 +1,18 @@ +import Control.Concurrent +import qualified Data.ByteString as B +import System.IO +import System.Process.CommunicationHandle + +main :: IO () +main = do + (r, ch) <- createWeReadTheyWritePipe + w <- openCommunicationHandleWrite ch + mapM_ (`hSetBinaryMode` True) [r, w] + done <- newEmptyMVar + _ <- forkIO $ do + B.hPut w (B.replicate 65536 0) + hClose w + putMVar done () + s <- B.hGetContents r + takeMVar done + print (B.length s) ===================================== libraries/base/tests/IO/T27971.stdout ===================================== @@ -0,0 +1 @@ +65536 ===================================== libraries/base/tests/IO/all.T ===================================== @@ -198,3 +198,10 @@ test('osHandles002WindowsHandles', only_ways(['winio', 'winio_threaded']), compi # executables, emulates part of POSIX, enough for this test. As a # result, this test could be generalized to also supporting Windows, but # this would likely involve creating bindings to C code. +test('T27971', + [ unless(opsys('mingw32'), skip) + , extra_ways(['winio_threaded']) + , only_ways(['threaded1', 'winio_threaded']) + , extra_hc_opts('-package process') + ], + compile_and_run, ['']) ===================================== libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc ===================================== @@ -591,7 +591,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do ++ " ptr_lpol:" ++ show ptr_lpol ++ " *ptr_lpol:" ++ show cdDataCheck - startCBResult <- startCB lpol `onException` + (startCBResult, startErr) <- startCB lpol `onException` (CbError `fmap` Win32.getLastError) >>= \result -> do -- Check to see if the operation was completed on a -- non-overlapping handle or was completed immediately. @@ -629,7 +629,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do -- I don't know what the best default here is... | otherwise -> CbPending _ -> result - case result' of + fmap (\r -> (r, err)) $ case result' of CbNone _ -> error "withOverlappedEx: CbNone shouldn't happen." CbIncomplete -> do debugIO $ "handling incomplete request synchronously " ++ show (h, lpol) @@ -728,13 +728,13 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do debugIO $ dbgMsg $ ":: done bytes: " ++ show bytes case bytes of Just res -> completionCB 0 res - Nothing -> do err <- FFI.overlappedIOStatus lpol + Nothing -> do status <- FFI.overlappedIOStatus lpol + err <- if status == #{const STATUS_PENDING} + then return startErr + else FFI.overlappedIOErrCode lpol numBytes <- FFI.overlappedIONumBytes lpol - -- TODO: Remap between STATUS_ and ERROR_ instead - -- of re-interpret here. But for now, don't care. - let err' = fromIntegral err - debugIO $ dbgMsg $ ":: done callback: " ++ show err' ++ " - " ++ show numBytes - completionCB err' (fromIntegral numBytes) + debugIO $ dbgMsg $ ":: done callback: " ++ show err ++ " - " ++ show numBytes + completionCB err (fromIntegral numBytes) else readMVar signal CbError err -> do reqs3 <- removeRequest @@ -1097,10 +1097,7 @@ processCompletion Manager{..} n delay = do cb <- deRefStablePtr (cdCallback payload) reqs <- removeRequest debugIO $ "-1.. " ++ show reqs ++ " requests queued." - status <- FFI.overlappedIOStatus (lpOverlapped oe) - -- TODO: Remap between STATUS_ and ERROR_ instead - -- of re-interpret here. But for now, don't care. - let status' = fromIntegral status + status' <- FFI.overlappedIOErrCode (lpOverlapped oe) -- We no longer explicitly free the memory, this is because we -- now require the callback to free the memory since the -- callback allocated it. This allows us to simplify memory ===================================== libraries/ghc-internal/src/GHC/Internal/Event/Windows/FFI.hsc ===================================== @@ -46,6 +46,7 @@ module GHC.Internal.Event.Windows.FFI ( zeroOverlapped, pokeOffsetOverlapped, overlappedIOStatus, + overlappedIOErrCode, overlappedIONumBytes, -- * Cancel pending I/O @@ -209,11 +210,17 @@ getQueuedCompletionStatusEx iocp arr timeout = overlappedIOStatus :: LPOVERLAPPED -> IO NTSTATUS overlappedIOStatus lpol = do status <- #{peek OVERLAPPED, Internal} lpol - -- TODO: Map NTSTATUS to ErrCode? - -- See https://github.com/libuv/libuv/blob/b12624c13693c4d29ca84b3556eadc9e9c0936a4... return status {-# INLINE overlappedIOStatus #-} +foreign import ccall unsafe "winternl.h RtlNtStatusToDosError" + c_RtlNtStatusToDosError :: NTSTATUS -> IO ULONG + +overlappedIOErrCode :: LPOVERLAPPED -> IO ErrCode +overlappedIOErrCode lpol = do + status <- overlappedIOStatus lpol + c_RtlNtStatusToDosError status + overlappedIONumBytes :: LPOVERLAPPED -> IO ULONG_PTR overlappedIONumBytes lpol = do bytes <- #{peek OVERLAPPED, InternalHigh} lpol ===================================== libraries/ghc-internal/src/GHC/Internal/IO/Windows/Handle.hsc ===================================== @@ -463,9 +463,7 @@ hwndRead hwnd ptr offset bytes = do completionCB err dwBytes | err == #{const ERROR_SUCCESS} = Mgr.ioSuccess $ fromIntegral dwBytes | err == #{const ERROR_HANDLE_EOF} = Mgr.ioSuccess 0 - | err == #{const STATUS_END_OF_FILE} = Mgr.ioSuccess 0 | err == #{const ERROR_BROKEN_PIPE} = Mgr.ioSuccess 0 - | err == #{const STATUS_PIPE_BROKEN} = Mgr.ioSuccess 0 | err == #{const ERROR_NO_MORE_ITEMS} = Mgr.ioSuccess $ fromIntegral dwBytes | err == #{const ERROR_MORE_DATA} = Mgr.ioSuccess $ fromIntegral dwBytes | otherwise = Mgr.ioFailed err @@ -493,9 +491,7 @@ hwndReadNonBlocking hwnd ptr offset bytes completionCB err dwBytes | err == #{const ERROR_SUCCESS} = Mgr.ioSuccess $ Just $! fromIntegral dwBytes | err == #{const ERROR_HANDLE_EOF} = Mgr.ioSuccess Nothing - | err == #{const STATUS_END_OF_FILE} = Mgr.ioSuccess Nothing | err == #{const ERROR_BROKEN_PIPE} = Mgr.ioSuccess Nothing - | err == #{const STATUS_PIPE_BROKEN} = Mgr.ioSuccess Nothing | err == #{const ERROR_NO_MORE_ITEMS} = Mgr.ioSuccess Nothing | err == #{const ERROR_MORE_DATA} = Mgr.ioSuccess $ Just $! fromIntegral dwBytes | otherwise = Mgr.ioFailedAny err View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/4183ff2e42555e9f974bd0428bed9eff... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/4183ff2e42555e9f974bd0428bed9eff... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Zubin (@wz1000)