]> git.proxmox.com Git - mirror_edk2.git/commitdiff
EmulatorPkg/Win/Host: Fix image unload regression
authorMichael D Kinney <michael.d.kinney@intel.com>
Thu, 22 Aug 2019 02:36:09 +0000 (10:36 +0800)
committerRay Ni <ray.ni@intel.com>
Mon, 26 Aug 2019 22:01:58 +0000 (06:01 +0800)
https://bugzilla.tianocore.org/show_bug.cgi?id=2104

When UEFI Applications or UEFI Drivers are unloaded,
the PeCoffLoaderUnloadImageExtraAction() needs to unload
the image using FreeLibrary() if the image was successfully
loaded using LoadLibrrayEx().

This is a regression from the Nt32Pkg that supported
unloading applications and drivers as well as loading
the same application or driver multiple times.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Andrew Fish <afish@apple.com>
Tested-by: Tim Lewis <tim.lewis@insyde.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
EmulatorPkg/Win/Host/WinHost.c

index dd52075f98e05ab7979ce86f461e8f87678ea2f7..9c6acac2795265d1834234d49b6f19e973e8f0a2 100644 (file)
@@ -19,6 +19,25 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #define SE_TIME_ZONE_NAME                 TEXT("SeTimeZonePrivilege")\r
 #endif\r
 \r
+//\r
+// The growth size for array of module handle entries\r
+//\r
+#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r
+\r
+//\r
+// Module handle entry structure\r
+//\r
+typedef struct {\r
+  CHAR8   *PdbPointer;\r
+  VOID    *ModHandle;\r
+} PDB_NAME_TO_MOD_HANDLE;\r
+\r
+//\r
+// An Array to hold the module handles\r
+//\r
+PDB_NAME_TO_MOD_HANDLE  *mPdbNameModHandleArray = NULL;\r
+UINTN                   mPdbNameModHandleArraySize = 0;\r
+\r
 //\r
 // Default information about where the FD is located.\r
 //  This array gets filled in with information from PcdWinNtFirmwareVolume\r
@@ -840,6 +859,120 @@ Returns:
   return Count;\r
 }\r
 \r
+/**\r
+  Store the ModHandle in an array indexed by the Pdb File name.\r
+  The ModHandle is needed to unload the image.\r
+  @param ImageContext - Input data returned from PE Laoder Library. Used to find the\r
+                 .PDB file name of the PE Image.\r
+  @param ModHandle    - Returned from LoadLibraryEx() and stored for call to\r
+                 FreeLibrary().\r
+  @return   return EFI_SUCCESS when ModHandle was stored.\r
+--*/\r
+EFI_STATUS\r
+AddModHandle (\r
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,\r
+  IN  VOID                                 *ModHandle\r
+  )\r
+\r
+{\r
+  UINTN                   Index;\r
+  PDB_NAME_TO_MOD_HANDLE  *Array;\r
+  UINTN                   PreviousSize;\r
+  PDB_NAME_TO_MOD_HANDLE  *TempArray;\r
+  HANDLE                  Handle;\r
+  UINTN                   Size;\r
+\r
+  //\r
+  // Return EFI_ALREADY_STARTED if this DLL has already been loaded\r
+  //\r
+  Array = mPdbNameModHandleArray;\r
+  for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
+    if (Array->PdbPointer != NULL && Array->ModHandle == ModHandle) {\r
+      return EFI_ALREADY_STARTED;\r
+    }\r
+  }\r
+\r
+  Array = mPdbNameModHandleArray;\r
+  for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
+    if (Array->PdbPointer == NULL) {\r
+      //\r
+      // Make a copy of the stirng and store the ModHandle\r
+      //\r
+      Handle = GetProcessHeap ();\r
+      Size = AsciiStrLen (ImageContext->PdbPointer) + 1;\r
+      Array->PdbPointer = HeapAlloc ( Handle, HEAP_ZERO_MEMORY, Size);\r
+      ASSERT (Array->PdbPointer != NULL);\r
+\r
+      AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);\r
+      Array->ModHandle = ModHandle;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  //\r
+  // No free space in mPdbNameModHandleArray so grow it by\r
+  // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.\r
+  //\r
+  PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
+  mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
+  //\r
+  // re-allocate a new buffer and copy the old values to the new locaiton.\r
+  //\r
+  TempArray = HeapAlloc (GetProcessHeap (),\r
+                                HEAP_ZERO_MEMORY,\r
+                                mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
+                               );\r
+\r
+  CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);\r
+\r
+  HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray);\r
+\r
+  mPdbNameModHandleArray = TempArray;\r
+  if (mPdbNameModHandleArray == NULL) {\r
+    ASSERT (FALSE);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  return AddModHandle (ImageContext, ModHandle);\r
+}\r
+\r
+/**\r
+  Return the ModHandle and delete the entry in the array.\r
+   @param  ImageContext - Input data returned from PE Laoder Library. Used to find the\r
+                 .PDB file name of the PE Image.\r
+  @return\r
+    ModHandle - ModHandle assoicated with ImageContext is returned\r
+    NULL      - No ModHandle associated with ImageContext\r
+**/\r
+VOID *\r
+RemoveModHandle (\r
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
+  )\r
+{\r
+  UINTN                   Index;\r
+  PDB_NAME_TO_MOD_HANDLE  *Array;\r
+\r
+  if (ImageContext->PdbPointer == NULL) {\r
+    //\r
+    // If no PDB pointer there is no ModHandle so return NULL\r
+    //\r
+    return NULL;\r
+  }\r
+\r
+  Array = mPdbNameModHandleArray;\r
+  for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
+    if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
+      //\r
+      // If you find a match return it and delete the entry\r
+      //\r
+      HeapFree (GetProcessHeap (), 0, Array->PdbPointer);\r
+      Array->PdbPointer = NULL;\r
+      return Array->ModHandle;\r
+    }\r
+  }\r
+\r
+  return NULL;\r
+}\r
 \r
 VOID\r
 EFIAPI\r
@@ -847,6 +980,7 @@ PeCoffLoaderRelocateImageExtraAction (
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
   )\r
 {\r
+  EFI_STATUS        Status;\r
   VOID              *DllEntryPoint;\r
   CHAR16            *DllFileName;\r
   HMODULE           Library;\r
@@ -855,7 +989,7 @@ PeCoffLoaderRelocateImageExtraAction (
   ASSERT (ImageContext != NULL);\r
   //\r
   // If we load our own PE COFF images the Windows debugger can not source\r
-  //  level debug our code. If a valid PDB pointer exists usw it to load\r
+  //  level debug our code. If a valid PDB pointer exists use it to load\r
   //  the *.dll file as a library using Windows* APIs. This allows\r
   //  source level debug. The image is still loaded and relocated\r
   //  in the Framework memory space like on a real system (by the code above),\r
@@ -913,10 +1047,22 @@ PeCoffLoaderRelocateImageExtraAction (
     }\r
 \r
     if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
-      ImageContext->EntryPoint  = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
-      SecPrint ("LoadLibraryEx (%S,\n               NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
+      Status = AddModHandle (ImageContext, Library);\r
+      if (Status == EFI_ALREADY_STARTED) {\r
+        //\r
+        // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.\r
+        //\r
+        ImageContext->PdbPointer = NULL;\r
+        SecPrint ("WARNING: DLL already loaded.  No source level debug %S.\n\r", DllFileName);\r
+      } else {\r
+        //\r
+        // This DLL is not already loaded, so source level debugging is supported.\r
+        //\r
+        ImageContext->EntryPoint  = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
+        SecPrint ("LoadLibraryEx (\n\r  %S,\n\r  NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName);\r
+      }\r
     } else {\r
-      SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
+      SecPrint ("WARNING: No source level debug %S. \n\r", DllFileName);\r
     }\r
 \r
     free (DllFileName);\r
@@ -926,12 +1072,21 @@ PeCoffLoaderRelocateImageExtraAction (
 VOID\r
 EFIAPI\r
 PeCoffLoaderUnloadImageExtraAction (\r
-  IN PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
+  IN PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
 )\r
 {\r
+  VOID  *ModHandle;\r
+\r
   ASSERT (ImageContext != NULL);\r
-}\r
 \r
+  ModHandle = RemoveModHandle (ImageContext);\r
+  if (ModHandle != NULL) {\r
+    FreeLibrary (ModHandle);\r
+    SecPrint ("FreeLibrary (\n\r  %s)\n\r", ImageContext->PdbPointer);\r
+  } else {\r
+    SecPrint ("WARNING: Unload image without source level debug\n\r");\r
+  }\r
+}\r
 \r
 VOID\r
 _ModuleEntryPoint (\r