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