| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include "jpype.h" |
| 17 | #include "jp_platform.h" |
| 18 | |
| 19 | JPPlatformAdapter::~JPPlatformAdapter() |
| 20 | = default; |
| 21 | |
| 22 | #ifdef WIN32 |
| 23 | #include <windows.h> |
| 24 | |
| 25 | /** |
| 26 | * Windows-specific platform adapter |
| 27 | */ |
| 28 | class Win32PlatformAdapter : public JPPlatformAdapter |
| 29 | { |
| 30 | private: |
| 31 | HINSTANCE jvmLibrary; |
| 32 | |
| 33 | std::string formatMessage(DWORD msgCode) |
| 34 | { |
| 35 | LPVOID lpMsgBuf; |
| 36 | |
| 37 | DWORD rc = ::FormatMessage( |
| 38 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 39 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 40 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 41 | NULL, |
| 42 | msgCode, |
| 43 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 44 | (LPTSTR) & lpMsgBuf, |
| 45 | 0, NULL ); |
| 46 | |
| 47 | // Fail to get format |
| 48 | if (rc == 0) |
| 49 | { |
| 50 | std::stringstream ss; |
| 51 | ss << "error code " << msgCode; |
| 52 | return ss.str(); |
| 53 | } |
| 54 | |
| 55 | std::string res((LPTSTR) lpMsgBuf); |
| 56 | |
| 57 | LocalFree(lpMsgBuf); |
| 58 | |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | public: |
| 63 | |
| 64 | virtual void loadLibrary(const char* path) override |
| 65 | { |
| 66 | JP_TRACE_IN("Win32PlatformAdapter::loadLibrary"); |
| 67 | wchar_t *wpath = Py_DecodeLocale(path, NULL); |
| 68 | if (wpath == NULL) |
| 69 | { |
| 70 | JP_RAISE(PyExc_SystemError, "Unable to get JVM path with locale."); |
| 71 | } |
| 72 | jvmLibrary = LoadLibraryW(wpath); |
| 73 | PyMem_RawFree(wpath); |
| 74 | if (jvmLibrary == NULL) |
| 75 | { |
| 76 | JP_RAISE_OS_ERROR_WINDOWS( GetLastError(), path); |
| 77 | } |
| 78 | JP_TRACE_OUT; |
| 79 | } |
| 80 | |
| 81 | virtual void unloadLibrary() override |
| 82 | { |
| 83 | // on success return code is nonzero, TODO: handle error? |
| 84 | FreeLibrary(jvmLibrary); |
| 85 | } |
| 86 | |
| 87 | virtual void* getSymbol(const char* name) override |
| 88 | { |
| 89 | void* res = (void*) GetProcAddress(jvmLibrary, name); |
| 90 | if (res == NULL) |
| 91 | { |
| 92 | std::stringstream msg; |
| 93 | msg << "Unable to load symbol [" << name << "], error = " << formatMessage(GetLastError()); |
| 94 | JP_RAISE(PyExc_RuntimeError, msg.str().c_str()); |
| 95 | } |
| 96 | return res; |
| 97 | } |
| 98 | } ; |
| 99 | |
| 100 | #define PLATFORM_ADAPTER Win32PlatformAdapter |
| 101 | #else |
| 102 | |
| 103 | #if defined(_HPUX) && !defined(_IA64) |
| 104 | #include <dl.h> |
| 105 | #else |
| 106 | #include <dlfcn.h> |
| 107 | #endif // HPUX |
| 108 | #include <errno.h> |
| 109 | |
| 110 | // The code in this modules is mostly excluded from coverage as it is only |
| 111 | // possible to execute during a fatal error. |
| 112 | |
| 113 | class LinuxPlatformAdapter : public JPPlatformAdapter |
| 114 | { |
| 115 | private: |
| 116 | void* jvmLibrary; |
| 117 | |
| 118 | public: |
| 119 | |
| 120 | void loadLibrary(const char* path) override |
| 121 | { |
| 122 | JP_TRACE_IN("LinuxPlatformAdapter::loadLibrary"); |
| 123 | #if defined(_HPUX) && !defined(_IA64) |
| 124 | JP_TRACE("shl_load", path); |
| 125 | jvmLibrary = shl_load(path, BIND_DEFERRED | BIND_VERBOSE, 0L); |
| 126 | #else |
| 127 | JP_TRACE("dlopen", path); |
| 128 | jvmLibrary = dlopen(path, RTLD_LAZY | RTLD_GLOBAL); |
| 129 | #endif // HPUX |
| 130 | // GCOVR_EXCL_START |
| 131 | if (jvmLibrary == nullptr) |
| 132 | { |
| 133 | JP_TRACE("null library"); |
| 134 | JP_TRACE("errno", errno); |
| 135 | if (errno == ENOEXEC) |
| 136 | { |
| 137 | JP_TRACE("dignostics", dlerror()); |
| 138 | } |
| 139 | JP_RAISE_OS_ERROR_UNIX( errno, path); |
| 140 | } |
| 141 | // GCOVR_EXCL_STOP |
| 142 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 143 | } |
| 144 | |
| 145 | void unloadLibrary() override |
| 146 | { |
| 147 | JP_TRACE_IN("LinuxPlatformAdapter::unloadLibrary"); |
| 148 | int r = dlclose(jvmLibrary); |
| 149 | // GCOVR_EXCL_START |
| 150 | if (r != 0) // error |
| 151 | { |
| 152 | std::cerr << dlerror() << std::endl; |
| 153 | } |
| 154 | // GCOVR_EXCL_STOP |
| 155 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 156 | } |
| 157 | |
| 158 | void* getSymbol(const char* name) override |
| 159 | { |
| 160 | JP_TRACE_IN("LinuxPlatformAdapter::getSymbol"); |
| 161 | JP_TRACE("Load", name); |
| 162 | void* res = dlsym(jvmLibrary, name); |
| 163 | JP_TRACE("Res", res); |
| 164 | // GCOVR_EXCL_START |
| 165 | if (res == nullptr) |
| 166 | { |
| 167 | JP_TRACE("errno", errno); |
| 168 | std::stringstream msg; |
| 169 | msg << "Unable to load symbol [" << name << "], error = " << dlerror(); |
| 170 | JP_RAISE(PyExc_RuntimeError, msg.str().c_str()); |
| 171 | } |
| 172 | // GCOVR_EXCL_STOP |
| 173 | return res; |
| 174 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 175 | } |
| 176 | } ; |
| 177 | |
| 178 | #define PLATFORM_ADAPTER LinuxPlatformAdapter |
| 179 | #endif |
| 180 | |
| 181 | namespace |
| 182 | { |
| 183 | PLATFORM_ADAPTER* adapter; |
| 184 | } |
| 185 | |
| 186 | JPPlatformAdapter* JPPlatformAdapter::getAdapter() |
| 187 | { |
| 188 | if (adapter == nullptr) |
| 189 | adapter = new PLATFORM_ADAPTER(); |
| 190 | return adapter; |
| 191 | } |