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