rts/win32/IOManager: Fix integer types
authorBen Gamari <bgamari.foss@gmail.com>
Sat, 17 Dec 2016 23:08:00 +0000 (18:08 -0500)
committerBen Gamari <ben@smart-cactus.org>
Sat, 17 Dec 2016 23:09:40 +0000 (18:09 -0500)
This code has been broken on 64-bit systems for some time: the length
and timeout arguments of `addIORequest` and `addDelayRequest`,
respectively, were declared as `int`. However, they were passed Haskell
integers from their respective primops. Integer overflow and madness
ensued. This resulted in #7325 and who knows what else.

Also, there were a few left-over `BOOL`s in here which were not passed
to Windows system calls; these were changed to C99 `bool`s.

However, there is still a bit of signedness inconsistency within the
`delay#` call-chain,

 * `GHC.Conc.IO.threadDelay` and the `delay#` primop accept `Int`
   arguments

 * The `delay#` implementation in `PrimOps.cmm` expects the timeout as
   a `W_`

 * `AsyncIO.c:addDelayRequest` expects an `HsInt` (was `int` prior to
   this patch)

 * `IOManager.c:AddDelayRequest` expects an `HsInt`` (was `int`)

 * The Windows `Sleep` function expects a `DWORD` (which is unsigned)

Test Plan: Validate on Windows

Reviewers: erikd, austin, simonmar, Phyx

Reviewed By: Phyx

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D2861

GHC Trac Issues: #7325

rts/win32/AsyncIO.c
rts/win32/AsyncIO.h
rts/win32/IOManager.c
rts/win32/IOManager.h

index 6a05f68..9e6594f 100644 (file)
@@ -40,8 +40,8 @@
 
 typedef struct CompletedReq {
     unsigned int   reqID;
-    int            len;
-    int            errCode;
+    HsInt          len;
+    HsInt          errCode;
 } CompletedReq;
 
 #define MAX_REQUESTS 200
@@ -58,9 +58,9 @@ static int              issued_reqs;
 static void
 onIOComplete(unsigned int reqID,
              int   fd STG_UNUSED,
-             int   len,
+             HsInt len,
              void* buf STG_UNUSED,
-             int   errCode)
+             HsInt errCode)
 {
     DWORD dwRes;
     /* Deposit result of request in queue/table..when there's room. */
@@ -106,9 +106,9 @@ onIOComplete(unsigned int reqID,
 
 unsigned int
 addIORequest(int   fd,
-             int   forWriting,
-             int   isSock,
-             int   len,
+             bool  forWriting,
+             bool  isSock,
+             HsInt len,
              char* buf)
 {
     EnterCriticalSection(&queue_lock);
@@ -122,7 +122,7 @@ addIORequest(int   fd,
 }
 
 unsigned int
-addDelayRequest(int usecs)
+addDelayRequest(HsInt usecs)
 {
     EnterCriticalSection(&queue_lock);
     issued_reqs++;
index bedbf5b..3737db0 100644 (file)
 
 extern unsigned int
 addIORequest(int   fd,
-             int   forWriting,
-             int   isSock,
-             int   len,
+             bool  forWriting,
+             bool  isSock,
+             HsInt len,
              char* buf);
-extern unsigned int addDelayRequest(int   usecs);
+extern unsigned int addDelayRequest(HsInt usecs);
 extern unsigned int addDoProcRequest(void* proc, void* param);
 extern int  startupAsyncIO(void);
 extern void shutdownAsyncIO(bool wait_threads);
index f25b006..c5cae75 100644 (file)
@@ -284,7 +284,7 @@ IOWorkerProc(PVOID param)
 }
 
 static
-BOOL
+bool
 NewIOWorkerThread(IOManagerState* iom)
 {
     unsigned threadId;
@@ -296,7 +296,7 @@ NewIOWorkerThread(IOManagerState* iom)
                                  &threadId) );
 }
 
-BOOL
+bool
 StartIOManager(void)
 {
     HANDLE hExit;
@@ -429,9 +429,9 @@ depositWorkItem( unsigned int reqID,
  */
 int
 AddIORequest ( int   fd,
-               BOOL  forWriting,
-               BOOL  isSocket,
-               int   len,
+               bool  forWriting,
+               bool  isSocket,
+               HsInt len,
                char* buffer,
                CompletionProc onCompletion)
 {
@@ -461,7 +461,7 @@ AddIORequest ( int   fd,
  * the request queue.
  */
 BOOL
-AddDelayRequest ( unsigned int   usecs,
+AddDelayRequest ( HsInt          usecs,
                   CompletionProc onCompletion)
 {
     WorkItem* wItem = (WorkItem*)malloc(sizeof(WorkItem));
index 94821a8..01521ca 100644 (file)
@@ -30,9 +30,9 @@
  */
 typedef void (*CompletionProc)(unsigned int requestID,
                                int   fd,
-                               int   len,
+                               HsInt len,
                                void* buf,
-                               int   errCode);
+                               HsInt errCode);
 
 /*
  * Asynchronous procedure calls executed by a worker thread
@@ -44,11 +44,11 @@ typedef int (*DoProcProc)(void *param);
 typedef union workData {
     struct {
         int   fd;
-        int   len;
+        HsInt len;
         char *buf;
     } ioData;
     struct {
-        int   usecs;
+        HsInt usecs;
     } delayData;
     struct {
         DoProcProc proc;
@@ -80,7 +80,7 @@ extern CompletionProc onComplete;
 /*
  * Starting up and shutting down.
  */
-extern BOOL StartIOManager     ( void );
+extern bool StartIOManager     ( void );
 extern void ShutdownIOManager  ( bool wait_threads );
 
 /*
@@ -88,13 +88,13 @@ extern void ShutdownIOManager  ( bool wait_threads );
  * completion routine is supplied, which the worker thread
  * will invoke upon completion.
  */
-extern int AddDelayRequest ( unsigned int   usecs,
+extern int AddDelayRequest ( HsInt          usecs,
                              CompletionProc onCompletion);
 
 extern int AddIORequest ( int            fd,
-                          BOOL           forWriting,
-                          BOOL           isSocket,
-                          int            len,
+                          bool           forWriting,
+                          bool           isSocket,
+                          HsInt          len,
                           char*          buffer,
                           CompletionProc onCompletion);