Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
5e8ae81b
by Zubin Duggal at 2026-10-23T20:26:47-04:00
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:
| 1 | +section: base
|
|
| 2 | +synopsis: WinIO: Fix a bug where WinIO could confuse Win32 and NTSTATUS error codes
|
|
| 3 | +mrs: !16838
|
|
| 4 | +issues: #27971
|
|
| 5 | +description: This could lead to the result of certian I/O operations being misinterpreted, mishandled and misreported |
| 1 | +import Control.Concurrent
|
|
| 2 | +import qualified Data.ByteString as B
|
|
| 3 | +import System.IO
|
|
| 4 | +import System.Process.CommunicationHandle
|
|
| 5 | + |
|
| 6 | +main :: IO ()
|
|
| 7 | +main = do
|
|
| 8 | + (r, ch) <- createWeReadTheyWritePipe
|
|
| 9 | + w <- openCommunicationHandleWrite ch
|
|
| 10 | + mapM_ (`hSetBinaryMode` True) [r, w]
|
|
| 11 | + done <- newEmptyMVar
|
|
| 12 | + _ <- forkIO $ do
|
|
| 13 | + B.hPut w (B.replicate 65536 0)
|
|
| 14 | + hClose w
|
|
| 15 | + putMVar done ()
|
|
| 16 | + s <- B.hGetContents r
|
|
| 17 | + takeMVar done
|
|
| 18 | + print (B.length s) |
| 1 | +65536 |
| ... | ... | @@ -198,3 +198,10 @@ test('osHandles002WindowsHandles', only_ways(['winio', 'winio_threaded']), compi |
| 198 | 198 | # executables, emulates part of POSIX, enough for this test. As a
|
| 199 | 199 | # result, this test could be generalized to also supporting Windows, but
|
| 200 | 200 | # this would likely involve creating bindings to C code.
|
| 201 | +test('T27971',
|
|
| 202 | + [ unless(opsys('mingw32'), skip)
|
|
| 203 | + , extra_ways(['winio_threaded'])
|
|
| 204 | + , only_ways(['threaded1', 'winio_threaded'])
|
|
| 205 | + , extra_hc_opts('-package process')
|
|
| 206 | + ],
|
|
| 207 | + compile_and_run, ['']) |
| ... | ... | @@ -590,7 +590,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do |
| 590 | 590 | ++ " ptr_lpol:" ++ show ptr_lpol
|
| 591 | 591 | ++ " *ptr_lpol:" ++ show cdDataCheck
|
| 592 | 592 | |
| 593 | - startCBResult <- startCB lpol `onException`
|
|
| 593 | + (startCBResult, startErr) <- startCB lpol `onException`
|
|
| 594 | 594 | (CbError `fmap` Win32.getLastError) >>= \result -> do
|
| 595 | 595 | -- Check to see if the operation was completed on a
|
| 596 | 596 | -- non-overlapping handle or was completed immediately.
|
| ... | ... | @@ -628,7 +628,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do |
| 628 | 628 | -- I don't know what the best default here is...
|
| 629 | 629 | | otherwise -> CbPending
|
| 630 | 630 | _ -> result
|
| 631 | - case result' of
|
|
| 631 | + fmap (\r -> (r, err)) $ case result' of
|
|
| 632 | 632 | CbNone _ -> error "withOverlappedEx: CbNone shouldn't happen."
|
| 633 | 633 | CbIncomplete -> do
|
| 634 | 634 | debugIO $ "handling incomplete request synchronously " ++ show (h, lpol)
|
| ... | ... | @@ -727,13 +727,13 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do |
| 727 | 727 | debugIO $ dbgMsg $ ":: done bytes: " ++ show bytes
|
| 728 | 728 | case bytes of
|
| 729 | 729 | Just res -> completionCB 0 res
|
| 730 | - Nothing -> do err <- FFI.overlappedIOStatus lpol
|
|
| 730 | + Nothing -> do status <- FFI.overlappedIOStatus lpol
|
|
| 731 | + err <- if status == #{const STATUS_PENDING}
|
|
| 732 | + then return startErr
|
|
| 733 | + else FFI.overlappedIOErrCode lpol
|
|
| 731 | 734 | numBytes <- FFI.overlappedIONumBytes lpol
|
| 732 | - -- TODO: Remap between STATUS_ and ERROR_ instead
|
|
| 733 | - -- of re-interpret here. But for now, don't care.
|
|
| 734 | - let err' = fromIntegral err
|
|
| 735 | - debugIO $ dbgMsg $ ":: done callback: " ++ show err' ++ " - " ++ show numBytes
|
|
| 736 | - completionCB err' (fromIntegral numBytes)
|
|
| 735 | + debugIO $ dbgMsg $ ":: done callback: " ++ show err ++ " - " ++ show numBytes
|
|
| 736 | + completionCB err (fromIntegral numBytes)
|
|
| 737 | 737 | else readMVar signal
|
| 738 | 738 | CbError err -> do
|
| 739 | 739 | reqs3 <- removeRequest
|
| ... | ... | @@ -1096,10 +1096,7 @@ processCompletion Manager{..} n delay = do |
| 1096 | 1096 | cb <- deRefStablePtr (cdCallback payload)
|
| 1097 | 1097 | reqs <- removeRequest
|
| 1098 | 1098 | debugIO $ "-1.. " ++ show reqs ++ " requests queued."
|
| 1099 | - status <- FFI.overlappedIOStatus (lpOverlapped oe)
|
|
| 1100 | - -- TODO: Remap between STATUS_ and ERROR_ instead
|
|
| 1101 | - -- of re-interpret here. But for now, don't care.
|
|
| 1102 | - let status' = fromIntegral status
|
|
| 1099 | + status' <- FFI.overlappedIOErrCode (lpOverlapped oe)
|
|
| 1103 | 1100 | -- We no longer explicitly free the memory, this is because we
|
| 1104 | 1101 | -- now require the callback to free the memory since the
|
| 1105 | 1102 | -- callback allocated it. This allows us to simplify memory
|
| ... | ... | @@ -46,6 +46,7 @@ module GHC.Internal.Event.Windows.FFI ( |
| 46 | 46 | zeroOverlapped,
|
| 47 | 47 | pokeOffsetOverlapped,
|
| 48 | 48 | overlappedIOStatus,
|
| 49 | + overlappedIOErrCode,
|
|
| 49 | 50 | overlappedIONumBytes,
|
| 50 | 51 | |
| 51 | 52 | -- * Cancel pending I/O
|
| ... | ... | @@ -209,11 +210,17 @@ getQueuedCompletionStatusEx iocp arr timeout = |
| 209 | 210 | overlappedIOStatus :: LPOVERLAPPED -> IO NTSTATUS
|
| 210 | 211 | overlappedIOStatus lpol = do
|
| 211 | 212 | status <- #{peek OVERLAPPED, Internal} lpol
|
| 212 | - -- TODO: Map NTSTATUS to ErrCode?
|
|
| 213 | - -- See https://github.com/libuv/libuv/blob/b12624c13693c4d29ca84b3556eadc9e9c0936a4/src/win/winsock.c#L153
|
|
| 214 | 213 | return status
|
| 215 | 214 | {-# INLINE overlappedIOStatus #-}
|
| 216 | 215 | |
| 216 | +foreign import ccall unsafe "winternl.h RtlNtStatusToDosError"
|
|
| 217 | + c_RtlNtStatusToDosError :: NTSTATUS -> IO ULONG
|
|
| 218 | + |
|
| 219 | +overlappedIOErrCode :: LPOVERLAPPED -> IO ErrCode
|
|
| 220 | +overlappedIOErrCode lpol = do
|
|
| 221 | + status <- overlappedIOStatus lpol
|
|
| 222 | + c_RtlNtStatusToDosError status
|
|
| 223 | + |
|
| 217 | 224 | overlappedIONumBytes :: LPOVERLAPPED -> IO ULONG_PTR
|
| 218 | 225 | overlappedIONumBytes lpol = do
|
| 219 | 226 | bytes <- #{peek OVERLAPPED, InternalHigh} lpol
|
| ... | ... | @@ -463,9 +463,7 @@ hwndRead hwnd ptr offset bytes = do |
| 463 | 463 | completionCB err dwBytes
|
| 464 | 464 | | err == #{const ERROR_SUCCESS} = Mgr.ioSuccess $ fromIntegral dwBytes
|
| 465 | 465 | | err == #{const ERROR_HANDLE_EOF} = Mgr.ioSuccess 0
|
| 466 | - | err == #{const STATUS_END_OF_FILE} = Mgr.ioSuccess 0
|
|
| 467 | 466 | | err == #{const ERROR_BROKEN_PIPE} = Mgr.ioSuccess 0
|
| 468 | - | err == #{const STATUS_PIPE_BROKEN} = Mgr.ioSuccess 0
|
|
| 469 | 467 | | err == #{const ERROR_NO_MORE_ITEMS} = Mgr.ioSuccess $ fromIntegral dwBytes
|
| 470 | 468 | | err == #{const ERROR_MORE_DATA} = Mgr.ioSuccess $ fromIntegral dwBytes
|
| 471 | 469 | | otherwise = Mgr.ioFailed err
|
| ... | ... | @@ -493,9 +491,7 @@ hwndReadNonBlocking hwnd ptr offset bytes |
| 493 | 491 | completionCB err dwBytes
|
| 494 | 492 | | err == #{const ERROR_SUCCESS} = Mgr.ioSuccess $ Just $! fromIntegral dwBytes
|
| 495 | 493 | | err == #{const ERROR_HANDLE_EOF} = Mgr.ioSuccess Nothing
|
| 496 | - | err == #{const STATUS_END_OF_FILE} = Mgr.ioSuccess Nothing
|
|
| 497 | 494 | | err == #{const ERROR_BROKEN_PIPE} = Mgr.ioSuccess Nothing
|
| 498 | - | err == #{const STATUS_PIPE_BROKEN} = Mgr.ioSuccess Nothing
|
|
| 499 | 495 | | err == #{const ERROR_NO_MORE_ITEMS} = Mgr.ioSuccess Nothing
|
| 500 | 496 | | err == #{const ERROR_MORE_DATA} = Mgr.ioSuccess $ Just $! fromIntegral dwBytes
|
| 501 | 497 | | otherwise = Mgr.ioFailedAny err
|