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