Windows: add __try..__except
authorJeffrey Altman <jaltman@your-file-system.com>
Fri, 2 Mar 2012 15:54:38 +0000 (10:54 -0500)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Sat, 3 Mar 2012 23:21:18 +0000 (15:21 -0800)
Wrap potential exception locations in __try ... __except so
that the exceptions can be caught by us instead of Cc/Mm which
use exceptions to signal status.

Change-Id: Ifef02bbf2c76b8583b538e4b67d4136c832b6cc6
Reviewed-on: http://gerrit.openafs.org/6859
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Tested-by: Jeffrey Altman <jaltman@secure-endpoints.com>
Reviewed-by: Jeffrey Altman <jaltman@secure-endpoints.com>

src/WINNT/afsrdr/kernel/lib/AFSExtentsSupport.cpp

index 0ee0643..b482378 100644 (file)
@@ -623,47 +623,57 @@ BOOLEAN AFSDoExtentsMapRegion(IN AFSFcb *Fcb,
 
         AFSAcquireShared( &Fcb->NPFcb->Specific.File.ExtentsResource, TRUE );
 
-        entry = AFSExtentForOffsetHint(Fcb, Offset, TRUE, *FirstExtent);
-        *FirstExtent = entry;
-
-        if (NULL == entry || !AFSExtentContains(entry, Offset))
+        __try
         {
-            try_return (retVal = FALSE);
-        }
+            entry = AFSExtentForOffsetHint(Fcb, Offset, TRUE, *FirstExtent);
+            *FirstExtent = entry;
 
-        ASSERT(Offset->QuadPart >= entry->FileOffset.QuadPart);
-
-        while (TRUE)
-        {
-            if ((entry->FileOffset.QuadPart + entry->Size) >=
-                                                (Offset->QuadPart + Size))
+            if (NULL == entry || !AFSExtentContains(entry, Offset))
             {
-                //
-                // The end is inside the extent
-                //
-                try_return (retVal = TRUE);
-            }
-
-            if (entry->Lists[AFS_EXTENTS_LIST].Flink == &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
-            {
-                //
-                // Run out of extents
-                //
                 try_return (retVal = FALSE);
             }
 
-            newEntry = NextExtent( entry, AFS_EXTENTS_LIST );
+            ASSERT(Offset->QuadPart >= entry->FileOffset.QuadPart);
 
-            if (newEntry->FileOffset.QuadPart !=
-                (entry->FileOffset.QuadPart + entry->Size))
+            while (TRUE)
             {
-                //
-                // Gap
-                //
-                try_return (retVal = FALSE);
+                if ((entry->FileOffset.QuadPart + entry->Size) >=
+                     (Offset->QuadPart + Size))
+                {
+                    //
+                    // The end is inside the extent
+                    //
+                    try_return (retVal = TRUE);
+                }
+
+                if (entry->Lists[AFS_EXTENTS_LIST].Flink == &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
+                {
+                    //
+                    // Run out of extents
+                    //
+                    try_return (retVal = FALSE);
+                }
+
+                newEntry = NextExtent( entry, AFS_EXTENTS_LIST );
+
+                if (newEntry->FileOffset.QuadPart !=
+                     (entry->FileOffset.QuadPart + entry->Size))
+                {
+                    //
+                    // Gap
+                    //
+                    try_return (retVal = FALSE);
+                }
+
+                entry = newEntry;
             }
+        }
+        __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
+        {
 
-            entry = newEntry;
+            AFSDbgLogMsg( 0,
+                          0,
+                          "EXCEPTION - AFSDoExtentsMapRegion\n");
         }
 
 try_exit:
@@ -3455,140 +3465,151 @@ AFSMarkDirty( IN AFSFcb *Fcb,
     AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
                     TRUE);
 
-    //
-    // Find the insertion point
-    //
-
-    if( pNPFcb->Specific.File.DirtyListHead == NULL)
-    {
-
-        bInsertTail = TRUE;
-    }
-    else if( StartingByte->QuadPart == 0)
+    __try
     {
+        //
+        // Find the insertion point
+        //
 
-        bInsertHead = TRUE;
-    }
-    else
-    {
+        if( pNPFcb->Specific.File.DirtyListHead == NULL)
+        {
 
-        pCurrentExtent = pNPFcb->Specific.File.DirtyListHead;
+            bInsertTail = TRUE;
+        }
+        else if( StartingByte->QuadPart == 0)
+        {
 
-        while( pCurrentExtent != NULL)
+            bInsertHead = TRUE;
+        }
+        else
         {
 
-            if( pCurrentExtent->FileOffset.QuadPart + pCurrentExtent->Size >= StartingByte->QuadPart ||
-                pCurrentExtent->DirtyList.fLink == NULL)
-            {
+            pCurrentExtent = pNPFcb->Specific.File.DirtyListHead;
 
-                break;
-            }
+            while( pCurrentExtent != NULL)
+            {
 
-            pCurrentExtent = (AFSExtent *)pCurrentExtent->DirtyList.fLink;
-        }
-    }
+                if( pCurrentExtent->FileOffset.QuadPart + pCurrentExtent->Size >= StartingByte->QuadPart ||
+                    pCurrentExtent->DirtyList.fLink == NULL)
+                {
 
-    while( ulCount < ExtentsCount)
-    {
+                    break;
+                }
 
-        pNextExtent = NextExtent( pExtent, AFS_EXTENTS_LIST);
+                pCurrentExtent = (AFSExtent *)pCurrentExtent->DirtyList.fLink;
+            }
+        }
 
-        if( !BooleanFlagOn( pExtent->Flags, AFS_EXTENT_DIRTY))
+        while( ulCount < ExtentsCount)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
-                          AFS_TRACE_LEVEL_VERBOSE,
-                          "AFSMarkDirty Marking extent offset %I64X Length %08lX DIRTY\n",
-                          pExtent->FileOffset.QuadPart,
-                          pExtent->Size);
-
-            pExtent->DirtyList.fLink = NULL;
-            pExtent->DirtyList.bLink = NULL;
+            pNextExtent = NextExtent( pExtent, AFS_EXTENTS_LIST);
 
-            if( bInsertHead)
+            if( !BooleanFlagOn( pExtent->Flags, AFS_EXTENT_DIRTY))
             {
 
-                pExtent->DirtyList.fLink = (void *)pNPFcb->Specific.File.DirtyListHead;
+                AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
+                              AFS_TRACE_LEVEL_VERBOSE,
+                              "AFSMarkDirty Marking extent offset %I64X Length %08lX DIRTY\n",
+                              pExtent->FileOffset.QuadPart,
+                              pExtent->Size);
 
+                pExtent->DirtyList.fLink = NULL;
                 pExtent->DirtyList.bLink = NULL;
 
-                pNPFcb->Specific.File.DirtyListHead->DirtyList.bLink = (void *)pExtent;
+                if( bInsertHead)
+                {
 
-                pNPFcb->Specific.File.DirtyListHead = pExtent;
+                    pExtent->DirtyList.fLink = (void *)pNPFcb->Specific.File.DirtyListHead;
 
-                pCurrentExtent = pExtent;
+                    pExtent->DirtyList.bLink = NULL;
 
-                bInsertHead = FALSE;
-            }
-            else if( bInsertTail)
-            {
-
-                if( pNPFcb->Specific.File.DirtyListHead == NULL)
-                {
+                    pNPFcb->Specific.File.DirtyListHead->DirtyList.bLink = (void *)pExtent;
 
                     pNPFcb->Specific.File.DirtyListHead = pExtent;
-                }
-                else
-                {
 
-                    pNPFcb->Specific.File.DirtyListTail->DirtyList.fLink = (void *)pExtent;
+                    pCurrentExtent = pExtent;
 
-                    pExtent->DirtyList.bLink = (void *)pNPFcb->Specific.File.DirtyListTail;
+                    bInsertHead = FALSE;
                 }
+                else if( bInsertTail)
+                {
 
-                pNPFcb->Specific.File.DirtyListTail = pExtent;
-            }
-            else
-            {
+                    if( pNPFcb->Specific.File.DirtyListHead == NULL)
+                    {
 
-                pExtent->DirtyList.fLink = pCurrentExtent->DirtyList.fLink;
-                pExtent->DirtyList.bLink = (void *)pCurrentExtent;
+                        pNPFcb->Specific.File.DirtyListHead = pExtent;
+                    }
+                    else
+                    {
 
-                if( pExtent->DirtyList.fLink == NULL)
-                {
+                        pNPFcb->Specific.File.DirtyListTail->DirtyList.fLink = (void *)pExtent;
+
+                        pExtent->DirtyList.bLink = (void *)pNPFcb->Specific.File.DirtyListTail;
+                    }
 
                     pNPFcb->Specific.File.DirtyListTail = pExtent;
                 }
                 else
                 {
 
-                    ((AFSExtent *)pExtent->DirtyList.fLink)->DirtyList.bLink = (void *)pExtent;
+                    pExtent->DirtyList.fLink = pCurrentExtent->DirtyList.fLink;
+
+                    pExtent->DirtyList.bLink = (void *)pCurrentExtent;
+
+                    if( pExtent->DirtyList.fLink == NULL)
+                    {
+
+                        pNPFcb->Specific.File.DirtyListTail = pExtent;
+                    }
+                    else
+                    {
+
+                        ((AFSExtent *)pExtent->DirtyList.fLink)->DirtyList.bLink = (void *)pExtent;
+                    }
+
+                    pCurrentExtent->DirtyList.fLink = (void *)pExtent;
+
+                    pCurrentExtent = pExtent;
                 }
 
-                pCurrentExtent->DirtyList.fLink = (void *)pExtent;
+                pExtent->Flags |= AFS_EXTENT_DIRTY;
 
-                pCurrentExtent = pExtent;
-            }
+                //
+                // Up the dirty count
+                //
 
-            pExtent->Flags |= AFS_EXTENT_DIRTY;
+                lCount = InterlockedIncrement( &Fcb->Specific.File.ExtentsDirtyCount);
+            }
+            else
+            {
 
-            //
-            // Up the dirty count
-            //
+                pCurrentExtent = pExtent;
+            }
 
-            lCount = InterlockedIncrement( &Fcb->Specific.File.ExtentsDirtyCount);
-        }
-        else
-        {
+            AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_ACTIVE_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSMarkDirty Decrement count on extent %08lX Cnt %d\n",
+                          pExtent,
+                          pExtent->ActiveCount);
 
-            pCurrentExtent = pExtent;
-        }
+            if( DerefExtents)
+            {
+                ASSERT( pExtent->ActiveCount > 0);
+                lCount = InterlockedDecrement( &pExtent->ActiveCount);
+            }
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_ACTIVE_COUNTING,
-                      AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSMarkDirty Decrement count on extent %08lX Cnt %d\n",
-                      pExtent,
-                      pExtent->ActiveCount);
+            pExtent = pNextExtent;
 
-        if( DerefExtents)
-        {
-            ASSERT( pExtent->ActiveCount > 0);
-            lCount = InterlockedDecrement( &pExtent->ActiveCount);
+            ulCount++;
         }
+    }
+    __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
+    {
 
-        pExtent = pNextExtent;
-
-        ulCount++;
+        AFSDbgLogMsg( 0,
+                      0,
+                      "EXCEPTION - AFSMarkDirty\n");
     }
 
     AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
@@ -4235,127 +4256,139 @@ AFSSetupMD5Hash( IN AFSFcb *Fcb,
 
         AFSAcquireShared( &Fcb->NPFcb->Specific.File.ExtentsResource, TRUE);
 
-        liByteOffset.QuadPart = ByteOffset->QuadPart;
-
-        while( ulCount < ExtentsCount)
+        __try
         {
+            liByteOffset.QuadPart = ByteOffset->QuadPart;
 
-            RtlZeroMemory( pExtent->MD5,
-                           sizeof( pExtent->MD5));
+            while( ulCount < ExtentsCount)
+            {
 
-            pNextExtent = NextExtent( pExtent, AFS_EXTENTS_LIST);
+                RtlZeroMemory( pExtent->MD5,
+                               sizeof( pExtent->MD5));
 
-            if( liByteOffset.QuadPart == pExtent->FileOffset.QuadPart &&
-                ByteCount < pExtent->Size)
-            {
+                pNextExtent = NextExtent( pExtent, AFS_EXTENTS_LIST);
 
-                if( pExtentBuffer == NULL)
+                if( liByteOffset.QuadPart == pExtent->FileOffset.QuadPart &&
+                    ByteCount < pExtent->Size)
                 {
 
-                    pExtentBuffer = AFSExAllocatePoolWithTag( PagedPool,
-                                                              pExtent->Size,
-                                                              AFS_GENERIC_MEMORY_9_TAG);
-
                     if( pExtentBuffer == NULL)
                     {
 
-                        break;
-                    }
-                }
+                        pExtentBuffer = AFSExAllocatePoolWithTag( PagedPool,
+                                                                  pExtent->Size,
+                                                                  AFS_GENERIC_MEMORY_9_TAG);
 
-                RtlZeroMemory( pExtentBuffer,
-                               pExtent->Size);
+                        if( pExtentBuffer == NULL)
+                        {
 
-                RtlCopyMemory( pExtentBuffer,
-                               pCurrentBuffer,
-                               ByteCount);
+                            break;
+                        }
+                    }
 
-                pMD5Buffer = (char *)pExtentBuffer;
+                    RtlZeroMemory( pExtentBuffer,
+                                   pExtent->Size);
 
-                ulCurrentLen = ByteCount;
-            }
-            else if( liByteOffset.QuadPart != pExtent->FileOffset.QuadPart)
-            {
+                    RtlCopyMemory( pExtentBuffer,
+                                   pCurrentBuffer,
+                                   ByteCount);
 
-                pExtentBuffer = AFSExAllocatePoolWithTag( PagedPool,
-                                                          pExtent->Size,
-                                                          AFS_GENERIC_MEMORY_10_TAG);
+                    pMD5Buffer = (char *)pExtentBuffer;
 
-                if( pExtentBuffer == NULL)
+                    ulCurrentLen = ByteCount;
+                }
+                else if( liByteOffset.QuadPart != pExtent->FileOffset.QuadPart)
                 {
 
-                    break;
-                }
+                    pExtentBuffer = AFSExAllocatePoolWithTag( PagedPool,
+                                                              pExtent->Size,
+                                                              AFS_GENERIC_MEMORY_10_TAG);
 
-                RtlZeroMemory( pExtentBuffer,
-                               pExtent->Size);
+                    if( pExtentBuffer == NULL)
+                    {
 
-                if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
-                {
+                        break;
+                    }
 
-#ifdef AMD64
-                    RtlCopyMemory( pExtentBuffer,
-                                   ((char *)AFSLibCacheBaseAddress + pExtent->CacheOffset.QuadPart),
+                    RtlZeroMemory( pExtentBuffer,
                                    pExtent->Size);
+
+                    if( BooleanFlagOn( AFSLibControlFlags, AFS_REDIR_LIB_FLAGS_NONPERSISTENT_CACHE))
+                    {
+
+#ifdef AMD64
+                        RtlCopyMemory( pExtentBuffer,
+                                       ((char *)AFSLibCacheBaseAddress + pExtent->CacheOffset.QuadPart),
+                                       pExtent->Size);
 #else
-                    ASSERT( pExtent->CacheOffset.HighPart == 0);
-                    RtlCopyMemory( pExtentBuffer,
-                                   ((char *)AFSLibCacheBaseAddress + pExtent->CacheOffset.LowPart),
-                                   pExtent->Size);
+                        ASSERT( pExtent->CacheOffset.HighPart == 0);
+                        RtlCopyMemory( pExtentBuffer,
+                                       ((char *)AFSLibCacheBaseAddress + pExtent->CacheOffset.LowPart),
+                                       pExtent->Size);
 #endif
 
-                    ulBytesRead = pExtent->Size;
-                }
-                else
-                {
+                        ulBytesRead = pExtent->Size;
+                    }
+                    else
+                    {
 
-                    ntStatus = AFSReadCacheFile( pExtentBuffer,
-                                                 &pExtent->CacheOffset,
-                                                 pExtent->Size,
-                                                 &ulBytesRead);
+                        ntStatus = AFSReadCacheFile( pExtentBuffer,
+                                                     &pExtent->CacheOffset,
+                                                     pExtent->Size,
+                                                     &ulBytesRead);
 
-                    if( !NT_SUCCESS( ntStatus))
-                    {
-                        break;
+                        if( !NT_SUCCESS( ntStatus))
+                        {
+
+                            break;
+                        }
                     }
-                }
 
-                pMD5Buffer = (char *)pExtentBuffer;
+                    pMD5Buffer = (char *)pExtentBuffer;
 
-                ulCurrentLen = min( ByteCount, pExtent->Size - (ULONG)(liByteOffset.QuadPart - pExtent->FileOffset.QuadPart));
+                    ulCurrentLen = min( ByteCount, pExtent->Size - (ULONG)(liByteOffset.QuadPart - pExtent->FileOffset.QuadPart));
 
-                RtlCopyMemory( (void *)((char *)pExtentBuffer + (ULONG)(liByteOffset.QuadPart - pExtent->FileOffset.QuadPart)),
-                               pCurrentBuffer,
-                               ulCurrentLen);
-            }
-            else
-            {
+                    RtlCopyMemory( (void *)((char *)pExtentBuffer + (ULONG)(liByteOffset.QuadPart - pExtent->FileOffset.QuadPart)),
+                                   pCurrentBuffer,
+                                   ulCurrentLen);
+                }
+                else
+                {
 
-                ulCurrentLen = pExtent->Size;
+                    ulCurrentLen = pExtent->Size;
 
-                pMD5Buffer = pCurrentBuffer;
-            }
+                    pMD5Buffer = pCurrentBuffer;
+                }
 
-            AFSGenerateMD5( pMD5Buffer,
-                            pExtent->Size,
-                            pExtent->MD5);
+                AFSGenerateMD5( pMD5Buffer,
+                                pExtent->Size,
+                                pExtent->MD5);
 
-            pExtent = pNextExtent;
+                pExtent = pNextExtent;
 
-            ulCount++;
+                ulCount++;
 
-            ByteCount -= ulCurrentLen;
+                ByteCount -= ulCurrentLen;
 
-            pCurrentBuffer += ulCurrentLen;
+                pCurrentBuffer += ulCurrentLen;
+
+                liByteOffset.QuadPart += ulCurrentLen;
+            }
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSSetupMD5Hash Releasing Fcb extents lock %08lX SHARED %08lX\n",
+                          &Fcb->NPFcb->Specific.File.ExtentsResource,
+                          PsGetCurrentThread());
 
-            liByteOffset.QuadPart += ulCurrentLen;
         }
+        __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
+        {
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
-                      AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSSetupMD5Hash Releasing Fcb extents lock %08lX SHARED %08lX\n",
-                      &Fcb->NPFcb->Specific.File.ExtentsResource,
-                      PsGetCurrentThread());
+            AFSDbgLogMsg( 0,
+                          0,
+                          "EXCEPTION - AFSSetupMD5Hash\n");
+        }
 
         AFSReleaseResource( &Fcb->NPFcb->Specific.File.ExtentsResource );