Zubin pushed to branch wip/27971 at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • changelog.d/27971
    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

  • libraries/base/tests/IO/T27971.hs
    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)

  • libraries/base/tests/IO/T27971.stdout
    1
    +65536

  • libraries/base/tests/IO/all.T
    ... ... @@ -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, [''])

  • libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc
    ... ... @@ -591,7 +591,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do
    591 591
                     ++ " ptr_lpol:" ++ show ptr_lpol
    
    592 592
                     ++ " *ptr_lpol:" ++ show cdDataCheck
    
    593 593
     
    
    594
    -        startCBResult <- startCB lpol `onException`
    
    594
    +        (startCBResult, startErr) <- startCB lpol `onException`
    
    595 595
                             (CbError `fmap` Win32.getLastError) >>= \result -> do
    
    596 596
               -- Check to see if the operation was completed on a
    
    597 597
               -- non-overlapping handle or was completed immediately.
    
    ... ... @@ -629,7 +629,7 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do
    629 629
                                  -- I don't know what the best default here is...
    
    630 630
                                  | otherwise                                   -> CbPending
    
    631 631
                       _                                                        -> result
    
    632
    -          case result' of
    
    632
    +          fmap (\r -> (r, err)) $ case result' of
    
    633 633
                 CbNone    _ -> error "withOverlappedEx: CbNone shouldn't happen."
    
    634 634
                 CbIncomplete -> do
    
    635 635
                    debugIO $ "handling incomplete request synchronously " ++ show (h, lpol)
    
    ... ... @@ -728,13 +728,13 @@ withOverlappedEx mgr fname h async offset startCB completionCB = do
    728 728
                        debugIO $ dbgMsg $ ":: done bytes: " ++ show bytes
    
    729 729
                        case bytes of
    
    730 730
                          Just res -> completionCB 0 res
    
    731
    -                     Nothing  -> do err <- FFI.overlappedIOStatus lpol
    
    731
    +                     Nothing  -> do status <- FFI.overlappedIOStatus lpol
    
    732
    +                                    err <- if status == #{const STATUS_PENDING}
    
    733
    +                                             then return startErr
    
    734
    +                                             else FFI.overlappedIOErrCode lpol
    
    732 735
                                         numBytes <- FFI.overlappedIONumBytes lpol
    
    733
    -                                    -- TODO: Remap between STATUS_ and ERROR_ instead
    
    734
    -                                    -- of re-interpret here. But for now, don't care.
    
    735
    -                                    let err' = fromIntegral err
    
    736
    -                                    debugIO $ dbgMsg $ ":: done callback: " ++ show err' ++ " - " ++ show numBytes
    
    737
    -                                    completionCB err' (fromIntegral numBytes)
    
    736
    +                                    debugIO $ dbgMsg $ ":: done callback: " ++ show err ++ " - " ++ show numBytes
    
    737
    +                                    completionCB err (fromIntegral numBytes)
    
    738 738
                   else readMVar signal
    
    739 739
               CbError err  -> do
    
    740 740
                 reqs3 <- removeRequest
    
    ... ... @@ -1097,10 +1097,7 @@ processCompletion Manager{..} n delay = do
    1097 1097
                    cb <- deRefStablePtr (cdCallback payload)
    
    1098 1098
                    reqs <- removeRequest
    
    1099 1099
                    debugIO $ "-1.. " ++ show reqs ++ " requests queued."
    
    1100
    -               status <- FFI.overlappedIOStatus (lpOverlapped oe)
    
    1101
    -               -- TODO: Remap between STATUS_ and ERROR_ instead
    
    1102
    -               -- of re-interpret here. But for now, don't care.
    
    1103
    -               let status' = fromIntegral status
    
    1100
    +               status' <- FFI.overlappedIOErrCode (lpOverlapped oe)
    
    1104 1101
                    -- We no longer explicitly free the memory, this is because we
    
    1105 1102
                    -- now require the callback to free the memory since the
    
    1106 1103
                    -- 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 (
    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
    

  • libraries/ghc-internal/src/GHC/Internal/IO/Windows/Handle.hsc
    ... ... @@ -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