1 /* 2 * Copyright (c) 2023, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "jvm_io.h" 27 #include "runtime/arguments.hpp" 28 #include "runtime/interfaceSupport.inline.hpp" 29 #include "runtime/os.inline.hpp" 30 #include "runtime/semaphore.inline.hpp" 31 #include "runtime/thread.inline.hpp" 32 #include "utilities/zipLibrary.hpp" 33 34 // Entry points in zip.dll for loading zip/jar file entries 35 typedef void**(*ZIP_Open_t)(const char* name, char** pmsg); 36 typedef void(*ZIP_Close_t)(jzfile* zip); 37 typedef jzentry* (*ZIP_FindEntry_t)(jzfile* zip, const char* name, jint* sizeP, jint* nameLen); 38 typedef jboolean(*ZIP_ReadEntry_t)(jzfile* zip, jzentry* entry, unsigned char* buf, char* namebuf); 39 typedef void(*ZIP_FreeEntry_t)(jzfile *zip, jzentry *entry); 40 typedef jint(*ZIP_CRC32_t)(jint crc, const jbyte* buf, jint len); 41 typedef const char* (*ZIP_GZip_InitParams_t)(size_t, size_t*, size_t*, int); 42 typedef size_t(*ZIP_GZip_Fully_t)(char*, size_t, char*, size_t, char*, size_t, int, char*, char const**); 43 44 static ZIP_Open_t ZIP_Open = nullptr; 45 static ZIP_Close_t ZIP_Close = nullptr; 46 static ZIP_FindEntry_t ZIP_FindEntry = nullptr; 47 static ZIP_ReadEntry_t ZIP_ReadEntry = nullptr; 48 static ZIP_FreeEntry_t ZIP_FreeEntry = nullptr; 49 static ZIP_CRC32_t ZIP_CRC32 = nullptr; 50 static ZIP_GZip_InitParams_t ZIP_GZip_InitParams = nullptr; 51 static ZIP_GZip_Fully_t ZIP_GZip_Fully = nullptr; 52 53 static void* _zip_handle = nullptr; 54 static bool _loaded = false; 55 56 static inline bool is_loaded() { 57 return Atomic::load_acquire(&_loaded); 58 } 59 60 static inline bool not_loaded() { 61 return !is_loaded(); 62 } 63 64 static void* dll_lookup(const char* name, const char* path, bool vm_exit_on_failure) { 65 assert(_zip_handle != nullptr, "invariant"); 66 void* func = os::dll_lookup(_zip_handle, name); 67 if (func == nullptr && vm_exit_on_failure) { 68 char msg[256] = ""; 69 jio_snprintf(&msg[0], sizeof msg, "Could not resolve \"%s\"", name); 70 vm_exit_during_initialization(&msg[0], path); 71 } 72 return func; 73 } 74 75 static void store_function_pointers(const char* path, bool vm_exit_on_failure) { 76 assert(_zip_handle != nullptr, "invariant"); 77 ZIP_Open = CAST_TO_FN_PTR(ZIP_Open_t, dll_lookup("ZIP_Open", path, vm_exit_on_failure)); 78 ZIP_Close = CAST_TO_FN_PTR(ZIP_Close_t, dll_lookup("ZIP_Close", path, vm_exit_on_failure)); 79 ZIP_FindEntry = CAST_TO_FN_PTR(ZIP_FindEntry_t, dll_lookup("ZIP_FindEntry", path, vm_exit_on_failure)); 80 ZIP_ReadEntry = CAST_TO_FN_PTR(ZIP_ReadEntry_t, dll_lookup("ZIP_ReadEntry", path, vm_exit_on_failure)); 81 ZIP_FreeEntry = CAST_TO_FN_PTR(ZIP_FreeEntry_t, dll_lookup("ZIP_FreeEntry", path, vm_exit_on_failure)); 82 ZIP_CRC32 = CAST_TO_FN_PTR(ZIP_CRC32_t, dll_lookup("ZIP_CRC32", path, vm_exit_on_failure)); 83 // The following entry points are most likely optional from a zip library implementation perspective. 84 // Hence no vm_exit on a resolution failure. Further refactorings should investigate this, 85 // and if possible, streamline setting all entry points consistently. 86 ZIP_GZip_InitParams = CAST_TO_FN_PTR(ZIP_GZip_InitParams_t, dll_lookup("ZIP_GZip_InitParams", path, false)); 87 ZIP_GZip_Fully = CAST_TO_FN_PTR(ZIP_GZip_Fully_t, dll_lookup("ZIP_GZip_Fully", path, false)); 88 } 89 90 static void load_zip_library(bool vm_exit_on_failure) { 91 assert(!is_loaded(), "should not load zip library twice"); 92 char path[JVM_MAXPATHLEN]; 93 if (os::dll_locate_lib(&path[0], sizeof path, Arguments::get_dll_dir(), "zip")) { 94 char ebuf[1024]; 95 _zip_handle = os::dll_load(&path[0], &ebuf[0], sizeof ebuf); 96 } 97 if (_zip_handle == nullptr) { 98 if (vm_exit_on_failure) { 99 vm_exit_during_initialization("Unable to load zip library", &path[0]); 100 } 101 return; 102 } 103 store_function_pointers(&path[0], vm_exit_on_failure); 104 Atomic::release_store(&_loaded, true); 105 assert(is_loaded(), "invariant"); 106 } 107 108 // 109 // Helper mutex class that also ensures that java threads 110 // are in _thread_in_native when loading the zip library. 111 // 112 class ZipLibraryLoaderLock : public StackObj { 113 private: 114 static Semaphore _lock; 115 JavaThread* _jt; 116 public: 117 ZipLibraryLoaderLock() : _jt(nullptr) { 118 Thread* thread = Thread::current_or_null(); 119 if (thread != nullptr && thread->is_Java_thread()) { 120 JavaThread* const jt = JavaThread::cast(thread); 121 if (jt->thread_state() != _thread_in_native) { 122 _jt = jt; 123 ThreadStateTransition::transition_from_vm(jt, _thread_in_native, false); 124 } 125 } 126 _lock.wait(); 127 } 128 ~ZipLibraryLoaderLock() { 129 _lock.signal(); 130 if (_jt != nullptr) { 131 ThreadStateTransition::transition_from_native(_jt, _thread_in_vm, false); 132 } 133 } 134 }; 135 136 Semaphore ZipLibraryLoaderLock::_lock(1); 137 138 static void initialize(bool vm_exit_on_failure = true) { 139 if (is_loaded()) { 140 return; 141 } 142 ZipLibraryLoaderLock lock; 143 if (not_loaded()) { 144 load_zip_library(vm_exit_on_failure); 145 } 146 } 147 148 void** ZipLibrary::open(const char* name, char** pmsg) { 149 initialize(); 150 assert(ZIP_Open != nullptr, "invariant"); 151 return ZIP_Open(name, pmsg); 152 } 153 154 void ZipLibrary::close(jzfile* zip) { 155 assert(is_loaded(), "invariant"); 156 assert(ZIP_Close != nullptr, "invariant"); 157 ZIP_Close(zip); 158 } 159 160 jzentry* ZipLibrary::find_entry(jzfile* zip, const char* name, jint* sizeP, jint* nameLen) { 161 initialize(); 162 assert(ZIP_FindEntry != nullptr, "invariant"); 163 return ZIP_FindEntry(zip, name, sizeP, nameLen); 164 } 165 166 jboolean ZipLibrary::read_entry(jzfile* zip, jzentry* entry, unsigned char* buf, char* namebuf) { 167 initialize(); 168 assert(ZIP_ReadEntry != nullptr, "invariant"); 169 return ZIP_ReadEntry(zip, entry, buf, namebuf); 170 } 171 172 void ZipLibrary::free_entry(jzfile* zip, jzentry* entry) { 173 initialize(); 174 assert(ZIP_FreeEntry != nullptr, "invariant"); 175 ZIP_FreeEntry(zip, entry); 176 } 177 178 jint ZipLibrary::crc32(jint crc, const jbyte* buf, jint len) { 179 initialize(); 180 assert(ZIP_CRC32 != nullptr, "invariant"); 181 return ZIP_CRC32(crc, buf, len); 182 } 183 184 const char* ZipLibrary::init_params(size_t block_size, size_t* needed_out_size, size_t* needed_tmp_size, int level) { 185 initialize(false); 186 if (ZIP_GZip_InitParams == nullptr) { 187 return "Cannot get ZIP_GZip_InitParams function"; 188 } 189 return ZIP_GZip_InitParams(block_size, needed_out_size, needed_tmp_size, level); 190 } 191 192 size_t ZipLibrary::compress(char* in, size_t in_size, char* out, size_t out_size, char* tmp, size_t tmp_size, int level, char* buf, const char** pmsg) { 193 initialize(false); 194 if (ZIP_GZip_Fully == nullptr) { 195 *pmsg = "Cannot get ZIP_GZip_Fully function"; 196 return 0; 197 } 198 return ZIP_GZip_Fully(in, in_size, out, out_size, tmp, tmp_size, level, buf, pmsg); 199 } 200 201 void* ZipLibrary::handle() { 202 initialize(); 203 assert(is_loaded(), "invariant"); 204 assert(_zip_handle != nullptr, "invariant"); 205 return _zip_handle; 206 }