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 "runtime/os.hpp"
  31 
  32 GrowableArray<AOTCodeHeap*>* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTCodeHeap*> (2, true);
  33 GrowableArray<AOTLib*>* AOTLoader::_libraries = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTLib*> (2, true);
  34 
  35 // Iterate over all AOT CodeHeaps
  36 #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator<AOTCodeHeap*> heap = heaps()->begin(); heap != heaps()->end(); ++heap)
  37 // Iterate over all AOT Libraries
  38 #define FOR_ALL_AOT_LIBRARIES(lib) for (GrowableArrayIterator<AOTLib*> lib = libraries()->begin(); lib != libraries()->end(); ++lib)
  39 
  40 void AOTLoader::load_for_klass(InstanceKlass* ik, Thread* thread) {
  41   if (UseAOT) {
  42     FOR_ALL_AOT_HEAPS(heap) {
  43       (*heap)->load_klass_data(ik, thread);
  44     }
  45   }
  46 }
  47 
  48 uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) {
  49   FOR_ALL_AOT_HEAPS(heap) {
  50     AOTKlassData* klass_data = (*heap)->find_klass(ik);
  51     if (klass_data != NULL) {
  52       return klass_data->_fingerprint;
  53     }
  54   }
  55   return 0;
  56 }
  57 
  58 bool AOTLoader::find_klass(InstanceKlass* ik) {
  59   FOR_ALL_AOT_HEAPS(heap) {
  60     if ((*heap)->find_klass(ik) != NULL) {
  61       return true;
  62     }
  63   }
  64   return false;
  65 }
  66 
  67 bool AOTLoader::contains(address p) {
  68   FOR_ALL_AOT_HEAPS(heap) {
  69     if ((*heap)->contains(p)) {
  70       return true;
  71     }
  72   }
  73   return false;
  74 }
  75 
  76 void AOTLoader::oops_do(OopClosure* f) {
  77   if (UseAOT) {
  78     FOR_ALL_AOT_HEAPS(heap) {
  79       (*heap)->oops_do(f);
  80     }
  81   }
  82 }
  83 
  84 void AOTLoader::metadata_do(void f(Metadata*)) {
  85   if (UseAOT) {
  86     FOR_ALL_AOT_HEAPS(heap) {
  87       (*heap)->metadata_do(f);
  88     }
  89   }
  90 }
  91 
  92 // Flushing and deoptimization in case of evolution
  93 void AOTLoader::flush_evol_dependents_on(InstanceKlass* dependee) {
  94   // make non entrant and mark for deoptimization
  95   FOR_ALL_AOT_HEAPS(heap) {
  96     (*heap)->flush_evol_dependents_on(dependee);
  97   }
  98   Deoptimization::deoptimize_dependents();
  99 }
 100 
 101 /**
 102  * List of core modules for which we search for shared libraries.
 103  */
 104 static const char* modules[] = {
 105   "java.base",
 106   "java.logging",
 107   "jdk.compiler",
 108   "jdk.scripting.nashorn",
 109   "jdk.internal.vm.ci",
 110   "jdk.internal.vm.compiler"
 111 };
 112 
 113 void AOTLoader::initialize() {
 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     // -Xint is not compatible with AOT
 129     if (Arguments::is_interpreter_only()) {
 130       if (PrintAOT) {
 131         warning("-Xint is not compatible with AOT (switching AOT off)");
 132       }
 133       FLAG_SET_DEFAULT(UseAOT, false);
 134       return;
 135     }
 136 
 137     const char* home = Arguments::get_java_home();
 138     const char* file_separator = os::file_separator();
 139 
 140     for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) {
 141       char library[JVM_MAXPATHLEN];
 142       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());
 143       load_library(library, false);
 144     }
 145 
 146     // Scan the AOTLibrary option.
 147     if (AOTLibrary != NULL) {
 148       const int len = (int)strlen(AOTLibrary);
 149       char* cp  = NEW_C_HEAP_ARRAY(char, len+1, mtCode);
 150       if (cp != NULL) { // No memory?
 151         memcpy(cp, AOTLibrary, len);
 152         cp[len] = '\0';
 153         char* end = cp + len;
 154         while (cp < end) {
 155           const char* name = cp;
 156           while ((*cp) != '\0' && (*cp) != '\n' && (*cp) != ',' && (*cp) != ':' && (*cp) != ';')  cp++;
 157           cp[0] = '\0';  // Terminate name
 158           cp++;
 159           load_library(name, true);
 160         }
 161       }
 162     }
 163   }
 164 }
 165 
 166 void AOTLoader::universe_init() {
 167   if (UseAOT && libraries_count() > 0) {
 168     // Shifts are static values which initialized by 0 until java heap initialization.
 169     // AOT libs are loaded before heap initialized so shift values are not set.
 170     // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded.
 171     // Set shifts value based on first AOT library config.
 172     if (UseCompressedOops && AOTLib::narrow_oop_shift_initialized()) {
 173       int oop_shift = Universe::narrow_oop_shift();
 174       if (oop_shift == 0) {
 175         Universe::set_narrow_oop_shift(AOTLib::narrow_oop_shift());
 176       } else {
 177         FOR_ALL_AOT_LIBRARIES(lib) {
 178           (*lib)->verify_flag(AOTLib::narrow_oop_shift(), oop_shift, "Universe::narrow_oop_shift");
 179         }
 180       }
 181       if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set
 182         int klass_shift = Universe::narrow_klass_shift();
 183         if (klass_shift == 0) {
 184           Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift());
 185         } else {
 186           FOR_ALL_AOT_LIBRARIES(lib) {
 187             (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift");
 188           }
 189         }
 190       }
 191     }
 192     // Create heaps for all the libraries
 193     FOR_ALL_AOT_LIBRARIES(lib) {
 194       if ((*lib)->is_valid()) {
 195         AOTCodeHeap* heap = new AOTCodeHeap(*lib);
 196         {
 197           MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 198           add_heap(heap);
 199           CodeCache::add_heap(heap);
 200         }
 201       }
 202     }
 203   }
 204   if (heaps_count() == 0) {
 205     if (FLAG_IS_DEFAULT(UseAOT)) {
 206       FLAG_SET_DEFAULT(UseAOT, false);
 207     }
 208   }
 209 }
 210 
 211 void AOTLoader::set_narrow_klass_shift() {
 212   // This method could be called from Metaspace::set_narrow_klass_base_and_shift().
 213   // In case it is not called (during dump CDS, for example) the corresponding code in
 214   // AOTLoader::universe_init(), which is called later, will set the shift value.
 215   if (UseAOT && libraries_count() > 0 &&
 216       UseCompressedOops && AOTLib::narrow_oop_shift_initialized() &&
 217       UseCompressedClassPointers) {
 218     int klass_shift = Universe::narrow_klass_shift();
 219     if (klass_shift == 0) {
 220       Universe::set_narrow_klass_shift(AOTLib::narrow_klass_shift());
 221     } else {
 222       FOR_ALL_AOT_LIBRARIES(lib) {
 223         (*lib)->verify_flag(AOTLib::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift");
 224       }
 225     }
 226   }
 227 }
 228 
 229 void AOTLoader::load_library(const char* name, bool exit_on_error) {
 230   char ebuf[1024];
 231   void* handle = os::dll_load(name, ebuf, sizeof ebuf);
 232   if (handle == NULL) {
 233     if (exit_on_error) {
 234       tty->print_cr("error opening file: %s", ebuf);
 235       vm_exit(1);
 236     }
 237     return;
 238   }
 239   const int dso_id = libraries_count() + 1;
 240   AOTLib* lib = new AOTLib(handle, name, dso_id);
 241   if (!lib->is_valid()) {
 242     delete lib;
 243     os::dll_unload(handle);
 244     return;
 245   }
 246   add_library(lib);
 247 }
 248 
 249 #ifndef PRODUCT
 250 void AOTLoader::print_statistics() {
 251   { ttyLocker ttyl;
 252     tty->print_cr("--- AOT Statistics ---");
 253     tty->print_cr("AOT libraries loaded: %d", heaps_count());
 254     AOTCodeHeap::print_statistics();
 255   }
 256 }
 257 #endif