1 /* 2 * Copyright (c) 2016, 2018, 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 #include "precompiled.hpp" 25 #include "jvm.h" 26 27 #include "aot/aotCodeHeap.hpp" 28 #include "aot/aotLoader.inline.hpp" 29 #include "jvmci/jvmciRuntime.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "oops/method.hpp" 32 #include "runtime/handles.inline.hpp" 33 #include "runtime/os.hpp" 34 #include "runtime/timerTrace.hpp" 35 36 GrowableArray<AOTCodeHeap*>* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTCodeHeap*> (2, true); 37 GrowableArray<AOTLib*>* AOTLoader::_libraries = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTLib*> (2, true); 38 39 // Iterate over all AOT CodeHeaps 40 #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator<AOTCodeHeap*> heap = heaps()->begin(); heap != heaps()->end(); ++heap) 41 // Iterate over all AOT Libraries 42 #define FOR_ALL_AOT_LIBRARIES(lib) for (GrowableArrayIterator<AOTLib*> lib = libraries()->begin(); lib != libraries()->end(); ++lib) 43 44 void AOTLoader::load_for_klass(InstanceKlass* ik, Thread* thread) { 45 if (ik->is_unsafe_anonymous()) { 46 // don't even bother 47 return; 48 } 49 if (UseAOT) { 50 if (JvmtiExport::can_hotswap_or_post_breakpoint()) { 51 if (PrintAOT) { 52 warning("JVMTI capability to hotswap and post breakpoint is not compatible with AOT (switching AOT off)"); 53 } 54 FLAG_SET_DEFAULT(UseAOT, false); 55 return; 56 } 57 FOR_ALL_AOT_HEAPS(heap) { 58 (*heap)->load_klass_data(ik, thread); 59 } 60 } 61 } 62 63 uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) { 64 assert(UseAOT, "called only when AOT is enabled"); 65 if (ik->is_unsafe_anonymous()) { 66 // don't even bother 67 return 0; 68 } 69 FOR_ALL_AOT_HEAPS(heap) { 70 AOTKlassData* klass_data = (*heap)->find_klass(ik); 71 if (klass_data != NULL) { 72 return klass_data->_fingerprint; 73 } 74 } 75 return 0; 76 } 77 78 void AOTLoader::oops_do(OopClosure* f) { 79 if (UseAOT) { 80 FOR_ALL_AOT_HEAPS(heap) { 81 (*heap)->oops_do(f); 82 } 83 } 84 } 85 86 void AOTLoader::metadata_do(void f(Metadata*)) { 87 if (UseAOT) { 88 FOR_ALL_AOT_HEAPS(heap) { 89 (*heap)->metadata_do(f); 90 } 91 } 92 } 93 94 /** 95 * List of core modules for which we search for shared libraries. 96 */ 97 static const char* modules[] = { 98 "java.base", 99 "java.logging", 100 "jdk.compiler", 101 "jdk.scripting.nashorn", 102 "jdk.internal.vm.ci", 103 "jdk.internal.vm.compiler" 104 }; 105 106 void AOTLoader::initialize() { 107 TraceTime timer("AOT initialization", TRACETIME_LOG(Info, aot, startuptime)); 108 109 if (FLAG_IS_DEFAULT(UseAOT) && AOTLibrary != NULL) { 110 // Don't need to set UseAOT on command line when AOTLibrary is specified 111 FLAG_SET_DEFAULT(UseAOT, true); 112 } 113 if (UseAOT) { 114 // EagerInitialization is not compatible with AOT 115 if (EagerInitialization) { 116 if (PrintAOT) { 117 warning("EagerInitialization is not compatible with AOT (switching AOT off)"); 118 } 119 FLAG_SET_DEFAULT(UseAOT, false); 120 return; 121 } 122 123 if (JvmtiExport::can_hotswap_or_post_breakpoint()) { 124 if (PrintAOT) { 125 warning("JVMTI capability to hotswap and post breakpoint is not compatible with AOT (switching AOT off)"); 126 } 127 FLAG_SET_DEFAULT(UseAOT, false); 128 return; 129 } 130 131 // -Xint is not compatible with AOT 132 if (Arguments::is_interpreter_only()) { 133 if (PrintAOT) { 134 warning("-Xint is not compatible with AOT (switching AOT off)"); 135 } 136 FLAG_SET_DEFAULT(UseAOT, false); 137 return; 138 } 139 140 #ifdef _WINDOWS 141 const char pathSep = ';'; 142 #else 143 const char pathSep = ':'; 144 #endif 145 146 // Scan the AOTLibrary option. 147 if (AOTLibrary != NULL) { 148 const int len = (int)strlen(AOTLibrary); 149 char* cp = NEW_C_HEAP_ARRAY(char, len+1, mtCode); 150 if (cp != NULL) { // No memory? 151 memcpy(cp, AOTLibrary, len); 152 cp[len] = '\0'; 153 char* end = cp + len; 154 while (cp < end) { 155 const char* name = cp; 156 while ((*cp) != '\0' && (*cp) != '\n' && (*cp) != ',' && (*cp) != pathSep) cp++; 157 cp[0] = '\0'; // Terminate name 158 cp++; 159 load_library(name, true); 160 } 161 } 162 } 163 164 // Load well-know AOT libraries from Java installation directory. 165 const char* home = Arguments::get_java_home(); 166 const char* file_separator = os::file_separator(); 167 168 for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) { 169 char library[JVM_MAXPATHLEN]; 170 jio_snprintf(library, sizeof(library), "%s%slib%slib%s%s%s%s", home, file_separator, file_separator, modules[i], UseCompressedOops ? "-coop" : "", UseG1GC ? "" : "-nong1", os::dll_file_extension()); 171 load_library(library, false); 172 } 173 } 174 } 175 176 void AOTLoader::universe_init() { 177 if (UseAOT && libraries_count() > 0) { 178 // Shifts are static values which initialized by 0 until java heap initialization. 179 // AOT libs are loaded before heap initialized so shift values are not set. 180 // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded. 181 // AOT sets shift values during heap and metaspace initialization. 182 // Check shifts value to make sure thay did not change. 183 if (UseCompressedOops && AOTLib::narrow_oop_shift_initialized()) { 184 int oop_shift = Universe::narrow_oop_shift(); 185 FOR_ALL_AOT_LIBRARIES(lib) { 186 (*lib)->verify_flag((*lib)->config()->_narrowOopShift, oop_shift, "Universe::narrow_oop_shift"); 187 } 188 if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set 189 int klass_shift = Universe::narrow_klass_shift(); 190 FOR_ALL_AOT_LIBRARIES(lib) { 191 (*lib)->verify_flag((*lib)->config()->_narrowKlassShift, klass_shift, "Universe::narrow_klass_shift"); 192 } 193 } 194 } 195 // Create heaps for all valid libraries 196 FOR_ALL_AOT_LIBRARIES(lib) { 197 if ((*lib)->is_valid()) { 198 AOTCodeHeap* heap = new AOTCodeHeap(*lib); 199 { 200 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 201 add_heap(heap); 202 CodeCache::add_heap(heap); 203 } 204 } else { 205 // Unload invalid libraries 206 os::dll_unload((*lib)->dl_handle()); 207 } 208 } 209 } 210 if (heaps_count() == 0) { 211 if (FLAG_IS_DEFAULT(UseAOT)) { 212 FLAG_SET_DEFAULT(UseAOT, false); 213 } 214 } 215 } 216 217 // Set shift value for compressed oops and classes based on first AOT library config. 218 // AOTLoader::universe_init(), which is called later, will check the shift value again to make sure nobody change it. 219 // This code is not executed during CDS dump because it runs in Interpreter mode and AOT is disabled in this mode. 220 221 void AOTLoader::set_narrow_oop_shift() { 222 // This method is called from Universe::initialize_heap(). 223 if (UseAOT && libraries_count() > 0 && 224 UseCompressedOops && AOTLib::narrow_oop_shift_initialized()) { 225 if (Universe::narrow_oop_shift() == 0) { 226 // 0 is valid shift value for small heap but we can safely increase it 227 // at this point when nobody used it yet. 228 Universe::set_narrow_oop_shift(AOTLib::narrow_oop_shift()); 229 } 230 } 231 } 232 233 void AOTLoader::set_narrow_klass_shift() { 234 // This method is called from Metaspace::set_narrow_klass_base_and_shift(). 235 if (UseAOT && libraries_count() > 0 && 236 UseCompressedOops && AOTLib::narrow_oop_shift_initialized() && 237 UseCompressedClassPointers) { 238 if (Universe::narrow_klass_shift() == 0) { 239 Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift()); 240 } 241 } 242 } 243 244 void AOTLoader::load_library(const char* name, bool exit_on_error) { 245 // Skip library if a library with the same name is already loaded. 246 const int file_separator = *os::file_separator(); 247 const char* start = strrchr(name, file_separator); 248 const char* new_name = (start == NULL) ? name : (start + 1); 249 FOR_ALL_AOT_LIBRARIES(lib) { 250 const char* lib_name = (*lib)->name(); 251 start = strrchr(lib_name, file_separator); 252 const char* old_name = (start == NULL) ? lib_name : (start + 1); 253 if (strcmp(old_name, new_name) == 0) { 254 if (PrintAOT) { 255 warning("AOT library %s is already loaded as %s.", name, lib_name); 256 } 257 return; 258 } 259 } 260 char ebuf[1024]; 261 void* handle = os::dll_load(name, ebuf, sizeof ebuf); 262 if (handle == NULL) { 263 if (exit_on_error) { 264 tty->print_cr("error opening file: %s", ebuf); 265 vm_exit(1); 266 } 267 return; 268 } 269 const int dso_id = libraries_count() + 1; 270 AOTLib* lib = new AOTLib(handle, name, dso_id); 271 if (!lib->is_valid()) { 272 delete lib; 273 os::dll_unload(handle); 274 return; 275 } 276 add_library(lib); 277 } 278 279 #ifndef PRODUCT 280 void AOTLoader::print_statistics() { 281 { ttyLocker ttyl; 282 tty->print_cr("--- AOT Statistics ---"); 283 tty->print_cr("AOT libraries loaded: %d", heaps_count()); 284 AOTCodeHeap::print_statistics(); 285 } 286 } 287 #endif 288 289 290 bool AOTLoader::reconcile_dynamic_invoke(InstanceKlass* holder, int index, Method* adapter_method, Klass* appendix_klass) { 291 if (!UseAOT) { 292 return true; 293 } 294 JavaThread* thread = JavaThread::current(); 295 ResourceMark rm(thread); 296 RegisterMap map(thread, false); 297 frame caller_frame = thread->last_frame().sender(&map); // Skip stub 298 CodeBlob* caller_cb = caller_frame.cb(); 299 guarantee(caller_cb != NULL && caller_cb->is_compiled(), "must be called from compiled method"); 300 CompiledMethod* cm = caller_cb->as_compiled_method(); 301 302 if (!cm->is_aot()) { 303 return true; 304 } 305 AOTCompiledMethod* aot = (AOTCompiledMethod*)cm; 306 307 AOTCodeHeap* caller_heap = NULL; 308 FOR_ALL_AOT_HEAPS(heap) { 309 if ((*heap)->contains_blob(aot)) { 310 caller_heap = *heap; 311 break; 312 } 313 } 314 guarantee(caller_heap != NULL, "CodeHeap not found"); 315 bool success = caller_heap->reconcile_dynamic_invoke(aot, holder, index, adapter_method, appendix_klass); 316 vmassert(success || thread->last_frame().sender(&map).is_deoptimized_frame(), "caller not deoptimized on failure"); 317 return success; 318 }