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