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