1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "Platform.h"
  27 
  28 #ifdef WINDOWS
  29 
  30 #ifndef WINDOWSPLATFORM_H
  31 #define WINDOWSPLATFORM_H
  32 
  33 #include "GenericPlatform.h"
  34 
  35 #include <Windows.h>
  36 
  37 #pragma warning( push )
  38 // C4250 - 'class1' : inherits 'class2::member'
  39 #pragma warning( disable : 4250 )
  40 class WindowsPlatform : virtual public Platform, GenericPlatform {
  41 private:
  42     DWORD FMainThread;
  43 
  44 public:
  45     WindowsPlatform(void);
  46     virtual ~WindowsPlatform(void);
  47 
  48     virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source,
  49             bool &release);
  50     virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source,
  51             bool &release);
  52 
  53     virtual void ShowMessage(TString title, TString description);
  54     virtual void ShowMessage(TString description);
  55     virtual MessageResponse ShowResponseMessage(TString title,
  56             TString description);
  57     //virtual MessageResponse ShowResponseMessage(TString description);
  58 
  59     virtual void SetCurrentDirectory(TString Value);
  60     virtual TString GetPackageRootDirectory();
  61     virtual TString GetAppDataDirectory();
  62     virtual TString GetBundledJVMLibraryFileName(TString RuntimePath);
  63 
  64     virtual ISectionalPropertyContainer* GetConfigFile(TString FileName);
  65 
  66     virtual TString GetModuleFileName();
  67     virtual Module LoadLibrary(TString FileName);
  68     virtual void FreeLibrary(Module AModule);
  69     virtual Procedure GetProcAddress(Module AModule, std::string MethodName);
  70     virtual std::vector<TString> GetLibraryImports(const TString FileName);
  71     virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(
  72             std::vector<TString> Imports);
  73 
  74     virtual Process* CreateProcess();
  75 
  76     virtual bool IsMainThread();
  77     virtual TPlatformNumber GetMemorySize();
  78 
  79     virtual TString GetTempDirectory();
  80 
  81 #ifdef DEBUG
  82     virtual bool IsNativeDebuggerPresent();
  83     virtual int GetProcessID();
  84 #endif //DEBUG
  85 };
  86 #pragma warning( pop ) // C4250
  87 
  88 
  89 class FileHandle {
  90 private:
  91     HANDLE FHandle;
  92 
  93 public:
  94     FileHandle(std::wstring FileName);
  95     ~FileHandle();
  96 
  97     bool IsValid();
  98     HANDLE GetHandle();
  99 };
 100 
 101 
 102 class FileMappingHandle {
 103 private:
 104     HANDLE FHandle;
 105 
 106 public:
 107     FileMappingHandle(HANDLE FileHandle);
 108     ~FileMappingHandle();
 109 
 110     bool IsValid();
 111     HANDLE GetHandle();
 112 };
 113 
 114 
 115 class FileData {
 116 private:
 117     LPVOID FBaseAddress;
 118 
 119 public:
 120     FileData(HANDLE Handle);
 121     ~FileData();
 122 
 123     bool IsValid();
 124     LPVOID GetBaseAddress();
 125 };
 126 
 127 
 128 class WindowsLibrary {
 129 private:
 130     TString FFileName;
 131 
 132     // Given an RVA, look up the section header that encloses it and return a
 133     // pointer to its IMAGE_SECTION_HEADER
 134     static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva,
 135             PIMAGE_NT_HEADERS pNTHeader);
 136     static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader,
 137             DWORD imageBase);
 138     static std::vector<TString> GetImportsSection(DWORD base,
 139             PIMAGE_NT_HEADERS pNTHeader);
 140     static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader);
 141 
 142 public:
 143     WindowsLibrary(const TString FileName);
 144 
 145     std::vector<TString> GetImports();
 146 };
 147 
 148 
 149 class WindowsJob {
 150 private:
 151     HANDLE FHandle;
 152 
 153 public:
 154     WindowsJob();
 155     ~WindowsJob();
 156 
 157     HANDLE GetHandle();
 158 };
 159 
 160 
 161 class WindowsProcess : public Process {
 162 private:
 163     bool FRunning;
 164 
 165     PROCESS_INFORMATION FProcessInfo;
 166     static WindowsJob FJob;
 167 
 168     void Cleanup();
 169     bool ReadOutput();
 170 
 171 public:
 172     WindowsProcess();
 173     virtual ~WindowsProcess();
 174 
 175     virtual bool IsRunning();
 176     virtual bool Terminate();
 177     virtual bool Execute(const TString Application,
 178             const std::vector<TString> Arguments, bool AWait = false);
 179     virtual bool Wait();
 180     virtual TProcessID GetProcessID();
 181     virtual void SetInput(TString Value);
 182     virtual std::list<TString> GetOutput();
 183 };
 184 
 185 
 186 
 187 
 188 #endif // WINDOWSPLATFORM_H
 189 
 190 #endif // WINDOWS