1 /*
   2  * Copyright (c) 2016, 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 
  31 GrowableArray<AOTCodeHeap*>* AOTLoader::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<AOTCodeHeap*> (2, true);
  32 
  33 // Iterate over all AOT CodeHeaps
  34 #define FOR_ALL_AOT_HEAPS(heap) for (GrowableArrayIterator<AOTCodeHeap*> heap = heaps()->begin(); heap != heaps()->end(); ++heap)
  35 
  36 void AOTLoader::load_for_klass(instanceKlassHandle kh, Thread* thread) {
  37   if (UseAOT) {
  38     FOR_ALL_AOT_HEAPS(heap) {
  39       (*heap)->load_klass_data(kh, thread);
  40     }
  41   }
  42 }
  43 
  44 uint64_t AOTLoader::get_saved_fingerprint(InstanceKlass* ik) {
  45   FOR_ALL_AOT_HEAPS(heap) {
  46     AOTKlassData* klass_data = (*heap)->find_klass(ik);
  47     if (klass_data != NULL) {
  48       return klass_data->_fingerprint;
  49     }
  50   }
  51   return 0;
  52 }
  53 
  54 bool AOTLoader::find_klass(InstanceKlass* ik) {
  55   FOR_ALL_AOT_HEAPS(heap) {
  56     if ((*heap)->find_klass(ik) != NULL) {
  57       return true;
  58     }
  59   }
  60   return false;
  61 }
  62 
  63 bool AOTLoader::contains(address p) {
  64   FOR_ALL_AOT_HEAPS(heap) {
  65     if ((*heap)->contains(p)) {
  66       return true;
  67     }
  68   }
  69   return false;
  70 }
  71 
  72 void AOTLoader::oops_do(OopClosure* f) {
  73   if (UseAOT) {
  74     FOR_ALL_AOT_HEAPS(heap) {
  75       (*heap)->oops_do(f);
  76     }
  77   }
  78 }
  79 
  80 void AOTLoader::metadata_do(void f(Metadata*)) {
  81   if (UseAOT) {
  82     FOR_ALL_AOT_HEAPS(heap) {
  83       (*heap)->metadata_do(f);
  84     }
  85   }
  86 }
  87 
  88 address AOTLoader::exception_begin(JavaThread* thread, CodeBlob* blob, address return_address) {
  89   assert(blob->is_aot(), "sanity");
  90   AOTCompiledMethod* aotm = (AOTCompiledMethod*)blob;
  91   // Set flag if return address is a method handle call site.
  92   thread->set_is_method_handle_return(aotm->is_method_handle_return(return_address));
  93   return aotm->exception_begin();
  94 }
  95 
  96 // Flushing and deoptimization in case of evolution
  97 void AOTLoader::flush_evol_dependents_on(instanceKlassHandle dependee) {
  98   // make non entrant and mark for deoptimization
  99   FOR_ALL_AOT_HEAPS(heap) {
 100     (*heap)->flush_evol_dependents_on(dependee);
 101   }
 102   Deoptimization::deoptimize_dependents();
 103 }
 104 
 105 /**
 106  * List of core modules for which we search for shared libraries.
 107  */
 108 static const char* modules[] = {
 109   "java.base",
 110   "java.logging",
 111   "jdk.compiler",
 112   "jdk.scripting.nashorn",
 113   "jdk.vm.ci",
 114   "jdk.vm.compiler"
 115 };
 116 
 117 void AOTLoader::initialize() {
 118   // Probe if we have libraries for core modules and load them if available.
 119   bool libraries_found = false;
 120 
 121   if (FLAG_IS_DEFAULT(UseAOT) && AOTLibrary != NULL) {
 122     // Don't need to set UseAOT on command line when AOTLibrary is specified
 123     FLAG_SET_DEFAULT(UseAOT, true);
 124   }
 125   if (UseAOT) {
 126     // EagerInitialization is not compatible with AOT
 127     if (EagerInitialization) {
 128       warning("EagerInitialization is not compatible with AOT (switching AOT off)");
 129       FLAG_SET_DEFAULT(UseAOT, false);
 130       return;
 131     }
 132 
 133     // -Xint is not compatible with AOT
 134     if (Arguments::is_interpreter_only()) {
 135       warning("-Xint is not compatible with AOT (switching AOT off)");
 136       FLAG_SET_DEFAULT(UseAOT, false);
 137       return;
 138     }
 139 
 140     const char* home = Arguments::get_java_home();
 141     const char* file_separator = os::file_separator();
 142 
 143     for (int i = 0; i < (int) (sizeof(modules) / sizeof(const char*)); i++) {
 144       char library[JVM_MAXPATHLEN];
 145       jio_snprintf(library, sizeof(library), "%s%slib%slib%s%s%s.so", home, file_separator, file_separator, modules[i], UseCompressedOops ? "-coop" : "", UseG1GC ? "" : "-nong1");
 146       bool found = load_library(library, false);
 147       if (found) {
 148         libraries_found = true;
 149       }
 150     }
 151 
 152     // Scan the AOTLibrary option.
 153     if (AOTLibrary != NULL) {
 154       const int len = strlen(AOTLibrary);
 155       char* cp  = NEW_C_HEAP_ARRAY(char, len+1, mtCode);
 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         bool found = load_library(name, true);
 165         if (found) {
 166           libraries_found = true;
 167         }
 168       }
 169     }
 170 
 171     // If we didn't find a library we are turning AOT off, if not requested otherwise.
 172     if (!libraries_found) {
 173       if (FLAG_IS_DEFAULT(UseAOT)) {
 174         FLAG_SET_DEFAULT(UseAOT, false);
 175       }
 176     }
 177   }
 178 }
 179 
 180 void AOTLoader::universe_init() {
 181   if (UseAOT && heaps_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 && AOTCodeHeap::narrow_oop_shift_initialized()) {
 187       int oop_shift = Universe::narrow_oop_shift();
 188       if (oop_shift == 0) {
 189         Universe::set_narrow_oop_shift(AOTCodeHeap::narrow_oop_shift());
 190       } else {
 191         FOR_ALL_AOT_HEAPS(heap) {
 192           (*heap)->verify_flag(AOTCodeHeap::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(AOTCodeHeap::narrow_klass_shift());
 199         } else {
 200           FOR_ALL_AOT_HEAPS(heap) {
 201             (*heap)->verify_flag(AOTCodeHeap::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift");
 202           }
 203         }
 204       }
 205     }
 206   }
 207 }
 208 
 209 void AOTLoader::set_narrow_klass_shift() {
 210   // This method could be called from Metaspace::set_narrow_klass_base_and_shift().
 211   // In case it is not called (during dump CDS, for example) the corresponding code in
 212   // AOTLoader::universe_init(), which is called later, will set the shift value.
 213   if (UseAOT && heaps_count() > 0 &&
 214       UseCompressedOops && AOTCodeHeap::narrow_oop_shift_initialized() &&
 215       UseCompressedClassPointers) {
 216     int klass_shift = Universe::narrow_klass_shift();
 217     if (klass_shift == 0) {
 218       Universe::set_narrow_klass_shift(AOTCodeHeap::narrow_klass_shift());
 219     } else {
 220       FOR_ALL_AOT_HEAPS(heap) {
 221         (*heap)->verify_flag(AOTCodeHeap::narrow_klass_shift(), klass_shift, "Universe::narrow_klass_shift");
 222       }
 223     }
 224   }
 225 }
 226 
 227 bool AOTLoader::load_library(const char* name, bool exit_on_error) {
 228   void* handle = dlopen(name, RTLD_LAZY);
 229   if (handle == NULL) {
 230     if (exit_on_error) {
 231       tty->print_cr("error opening file: %s", dlerror());
 232       vm_exit(1);
 233     }
 234     return false;
 235   }
 236   const int dso_id = heaps_count() + 1;
 237   AOTCodeHeap* heap = new AOTCodeHeap(handle, name, dso_id);
 238   if (!heap->is_valid()) {
 239     delete heap;
 240     dlclose(handle);
 241     return false;
 242   }
 243   {
 244     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 245     add_heap(heap);
 246     CodeCache::add_heap(heap);
 247   }
 248   return true;
 249 }
 250 
 251 #ifndef PRODUCT
 252 void AOTLoader::print_statistics() {
 253   { ttyLocker ttyl;
 254     tty->print_cr("--- AOT Statistics ---");
 255     tty->print_cr("AOT libraries loaded: %d", heaps_count());
 256     AOTCodeHeap::print_statistics();
 257   }
 258 }
 259 #endif