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.hpp"
  28 #include "aot/compiledIC_aot.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "code/compiledIC.hpp"
  31 #include "code/nativeInst.hpp"
  32 #include "compiler/compilerOracle.hpp"
  33 #include "gc/shared/cardTableModRefBS.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "gc/shared/gcLocker.hpp"
  36 #include "jvmci/compilerRuntime.hpp"
  37 #include "jvmci/jvmciRuntime.hpp"
  38 #include "oops/method.hpp"
  39 #include "runtime/java.hpp"
  40 #include "runtime/os.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "utilities/array.hpp"
  43 #include "utilities/xmlstream.hpp"
  44 
  45 #include <stdio.h>
  46 
  47 #if 0
  48 static void metadata_oops_do(Metadata** metadata_begin, Metadata **metadata_end, OopClosure* f) {
  49   // Visit the metadata/oops section
  50   for (Metadata** p = metadata_begin; p < metadata_end; p++) {
  51     Metadata* m = *p;
  52 
  53     intptr_t meta = (intptr_t)m;
  54     if ((meta & 1) == 1) {
  55       // already resolved
  56       m = (Metadata*)(meta & ~1);
  57     } else {
  58       continue;
  59     }
  60     assert(Metaspace::contains(m), "");
  61     if (m->is_method()) {
  62       m = ((Method*)m)->method_holder();
  63     }
  64     assert(m->is_klass(), "must be");
  65     oop o = ((Klass*)m)->klass_holder();
  66     if (o != NULL) {
  67       f->do_oop(&o);
  68     }
  69   }
  70 }
  71 #endif
  72 
  73 void AOTCompiledMethod::oops_do(OopClosure* f) {
  74   if (_oop != NULL) {
  75     f->do_oop(&_oop);
  76   }
  77 #if 0
  78   metadata_oops_do(metadata_begin(), metadata_end(), f);
  79 #endif
  80 }
  81 
  82 bool AOTCompiledMethod::do_unloading_oops(address low_boundary, BoolObjectClosure* is_alive, bool unloading_occurred) {
  83   return false;
  84 }
  85 
  86 oop AOTCompiledMethod::oop_at(int index) const {
  87   if (index == 0) { // 0 is reserved
  88     return NULL;
  89   }
  90   Metadata** entry = _metadata_got + (index - 1);
  91   intptr_t meta = (intptr_t)*entry;
  92   if ((meta & 1) == 1) {
  93     // already resolved
  94     Klass* k = (Klass*)(meta & ~1);
  95     return k->java_mirror();
  96   }
  97   // The entry is string which we need to resolve.
  98   const char* meta_name = _heap->get_name_at((int)meta);
  99   int klass_len = build_u2_from((address)meta_name);
 100   const char* klass_name = meta_name + 2;
 101   // Quick check the current method's holder.
 102   Klass* k = _method->method_holder();
 103 
 104   ResourceMark rm; // for signature_name()
 105   if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
 106     // Search klass in got cells in DSO which have this compiled method.
 107     k = _heap->get_klass_from_got(klass_name, klass_len, _method);
 108   }
 109   int method_name_len = build_u2_from((address)klass_name + klass_len);
 110   guarantee(method_name_len == 0, "only klass is expected here");
 111   meta = ((intptr_t)k) | 1;
 112   *entry = (Metadata*)meta; // Should be atomic on x64
 113   return k->java_mirror();
 114 }
 115 
 116 Metadata* AOTCompiledMethod::metadata_at(int index) const {
 117   if (index == 0) { // 0 is reserved
 118     return NULL;
 119   }
 120   assert(index - 1 < _metadata_size, "");
 121   {
 122     Metadata** entry = _metadata_got + (index - 1);
 123     intptr_t meta = (intptr_t)*entry;
 124     if ((meta & 1) == 1) {
 125       // already resolved
 126       Metadata *m = (Metadata*)(meta & ~1);
 127       return m;
 128     }
 129     // The entry is string which we need to resolve.
 130     const char* meta_name = _heap->get_name_at((int)meta);
 131     int klass_len = build_u2_from((address)meta_name);
 132     const char* klass_name = meta_name + 2;
 133     // Quick check the current method's holder.
 134     Klass* k = _method->method_holder();
 135     bool klass_matched = true;
 136 
 137     ResourceMark rm; // for signature_name() and find_method()
 138     if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
 139       // Search klass in got cells in DSO which have this compiled method.
 140       k = _heap->get_klass_from_got(klass_name, klass_len, _method);
 141       klass_matched = false;
 142     }
 143     int method_name_len = build_u2_from((address)klass_name + klass_len);
 144     if (method_name_len == 0) { // Array or Klass name only?
 145       meta = ((intptr_t)k) | 1;
 146       *entry = (Metadata*)meta; // Should be atomic on x64
 147       return (Metadata*)k;
 148     } else { // Method
 149       // Quick check the current method's name.
 150       Method* m = _method;
 151       int signature_len = build_u2_from((address)klass_name + klass_len + 2 + method_name_len);
 152       int full_len = 2 + klass_len + 2 + method_name_len + 2 + signature_len;
 153       if (!klass_matched || memcmp(_name, meta_name, full_len) != 0) { // Does not match?
 154         Thread* thread = Thread::current();

 155         const char* method_name = klass_name + klass_len;
 156         m = AOTCodeHeap::find_method(k, thread, method_name);
 157       }
 158       meta = ((intptr_t)m) | 1;
 159       *entry = (Metadata*)meta; // Should be atomic on x64
 160       return (Metadata*)m;
 161     }
 162     // need to resolve it here..., patching of GOT need to be CAS or atomic operation.
 163     // FIXIT: need methods for debuginfo.
 164     // return _method;
 165   }
 166   ShouldNotReachHere(); return NULL;
 167 }
 168 
 169 bool AOTCompiledMethod::make_not_entrant_helper(int new_state) {
 170   // Make sure the method is not flushed in case of a safepoint in code below.
 171   methodHandle the_method(method());
 172   NoSafepointVerifier nsv;
 173 
 174   {
 175     // Enter critical section.  Does not block for safepoint.
 176     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
 177 
 178     if (*_state_adr == new_state) {
 179       // another thread already performed this transition so nothing
 180       // to do, but return false to indicate this.
 181       return false;
 182     }
 183 
 184     // Change state
 185     OrderAccess::storestore();
 186     *_state_adr = new_state;
 187 
 188     // Log the transition once
 189     log_state_change();
 190 
 191 #ifdef TIERED
 192     // Remain non-entrant forever
 193     if (new_state == not_entrant && method() != NULL) {
 194         method()->set_aot_code(NULL);
 195     }
 196 #endif
 197 
 198     // Remove AOTCompiledMethod from method.
 199     if (method() != NULL && (method()->code() == this ||
 200                              method()->from_compiled_entry() == verified_entry_point())) {
 201       HandleMark hm;
 202       method()->clear_code(false /* already owns Patching_lock */);
 203     }
 204   } // leave critical region under Patching_lock
 205 
 206 
 207   if (TraceCreateZombies) {
 208     ResourceMark m;
 209     const char *new_state_str = (new_state == not_entrant) ? "not entrant" : "not used";
 210     tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null", new_state_str);
 211   }
 212 
 213   return true;
 214 }
 215 
 216 bool AOTCompiledMethod::make_entrant() {
 217   assert(!method()->is_old(), "reviving evolved method!");
 218   assert(*_state_adr != not_entrant, "%s", method()->has_aot_code() ? "has_aot_code() not cleared" : "caller didn't check has_aot_code()");
 219 
 220   // Make sure the method is not flushed in case of a safepoint in code below.
 221   methodHandle the_method(method());
 222   NoSafepointVerifier nsv;
 223 
 224   {
 225     // Enter critical section.  Does not block for safepoint.
 226     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
 227 
 228     if (*_state_adr == in_use) {
 229       // another thread already performed this transition so nothing
 230       // to do, but return false to indicate this.
 231       return false;
 232     }
 233 
 234     // Change state
 235     OrderAccess::storestore();
 236     *_state_adr = in_use;
 237 
 238     // Log the transition once
 239     log_state_change();
 240   } // leave critical region under Patching_lock
 241 
 242 
 243   if (TraceCreateZombies) {
 244     ResourceMark m;
 245     tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made entrant", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null");
 246   }
 247 
 248   return true;
 249 }
 250 
 251 // We don't have full dependencies for AOT methods, so flushing is
 252 // more conservative than for nmethods.
 253 void AOTCompiledMethod::flush_evol_dependents_on(InstanceKlass* dependee) {
 254   if (is_java_method()) {
 255     cleanup_inline_caches();
 256     mark_for_deoptimization();
 257     make_not_entrant();
 258   }
 259 }
 260 
 261 // Iterate over metadata calling this function.   Used by RedefineClasses
 262 // Copied from nmethod::metadata_do
 263 void AOTCompiledMethod::metadata_do(void f(Metadata*)) {
 264   address low_boundary = verified_entry_point();
 265   {
 266     // Visit all immediate references that are embedded in the instruction stream.
 267     RelocIterator iter(this, low_boundary);
 268     while (iter.next()) {
 269       if (iter.type() == relocInfo::metadata_type ) {
 270         metadata_Relocation* r = iter.metadata_reloc();
 271         // In this metadata, we must only follow those metadatas directly embedded in
 272         // the code.  Other metadatas (oop_index>0) are seen as part of
 273         // the metadata section below.
 274         assert(1 == (r->metadata_is_immediate()) +
 275                (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
 276                "metadata must be found in exactly one place");
 277         if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
 278           Metadata* md = r->metadata_value();
 279           if (md != _method) f(md);
 280         }
 281       } else if (iter.type() == relocInfo::virtual_call_type) {
 282         // Check compiledIC holders associated with this nmethod
 283         CompiledIC *ic = CompiledIC_at(&iter);
 284         if (ic->is_icholder_call()) {
 285           CompiledICHolder* cichk = ic->cached_icholder();
 286           f(cichk->holder_method());
 287           f(cichk->holder_klass());
 288         } else {
 289           Metadata* ic_oop = ic->cached_metadata();
 290           if (ic_oop != NULL) {
 291             f(ic_oop);
 292           }
 293         }
 294       }
 295     }
 296   }
 297 
 298   // Visit the metadata section
 299   for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
 300     Metadata* m = *p;
 301 
 302     intptr_t meta = (intptr_t)m;
 303     if ((meta & 1) == 1) {
 304       // already resolved
 305       m = (Metadata*)(meta & ~1);
 306     } else {
 307       continue;
 308     }
 309     assert(Metaspace::contains(m), "");
 310     f(m);
 311   }
 312 
 313   // Visit metadata not embedded in the other places.
 314   if (_method != NULL) f(_method);
 315 }
 316 
 317 void AOTCompiledMethod::print() const {
 318   print_on(tty, "AOTCompiledMethod");
 319 }
 320 
 321 void AOTCompiledMethod::print_on(outputStream* st) const {
 322   print_on(st, "AOTCompiledMethod");
 323 }
 324 
 325 // Print out more verbose output usually for a newly created aot method.
 326 void AOTCompiledMethod::print_on(outputStream* st, const char* msg) const {
 327   if (st != NULL) {
 328     ttyLocker ttyl;
 329     st->print("%7d ", (int) st->time_stamp().milliseconds());
 330     st->print("%4d ", _aot_id);    // print compilation number
 331     st->print("    aot[%2d]", _heap->dso_id());
 332     // Stubs have _method == NULL
 333     st->print("   %s", (_method == NULL ? _name : _method->name_and_sig_as_C_string()));
 334     if (Verbose) {
 335       st->print(" entry at " INTPTR_FORMAT, p2i(_code));
 336     }
 337     if (msg != NULL) {
 338       st->print("   %s", msg);
 339     }
 340     st->cr();
 341   }
 342 }
 343 
 344 void AOTCompiledMethod::print_value_on(outputStream* st) const {
 345   st->print("AOTCompiledMethod ");
 346   print_on(st, NULL);
 347 }
 348 
 349 // Print a short set of xml attributes to identify this aot method.  The
 350 // output should be embedded in some other element.
 351 void AOTCompiledMethod::log_identity(xmlStream* log) const {
 352   log->print(" aot_id='%d'", _aot_id);
 353   log->print(" aot='%2d'", _heap->dso_id());
 354 }
 355 
 356 void AOTCompiledMethod::log_state_change() const {
 357   if (LogCompilation) {
 358     ResourceMark m;
 359     if (xtty != NULL) {
 360       ttyLocker ttyl;  // keep the following output all in one block
 361       if (*_state_adr == not_entrant) {
 362         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'",
 363                          os::current_thread_id());
 364       } else if (*_state_adr == not_used) {
 365         xtty->begin_elem("make_not_used thread='" UINTX_FORMAT "'",
 366                          os::current_thread_id());
 367       } else if (*_state_adr == in_use) {
 368         xtty->begin_elem("make_entrant thread='" UINTX_FORMAT "'",
 369                          os::current_thread_id());
 370       }
 371       log_identity(xtty);
 372       xtty->stamp();
 373       xtty->end_elem();
 374     }
 375   }
 376   if (PrintCompilation) {
 377     ResourceMark m;
 378     if (*_state_adr == not_entrant) {
 379       print_on(tty, "made not entrant");
 380     } else if (*_state_adr == not_used) {
 381       print_on(tty, "made not used");
 382     } else if (*_state_adr == in_use) {
 383       print_on(tty, "made entrant");
 384     }
 385   }
 386 }
 387 
 388 
 389 NativeInstruction* PltNativeCallWrapper::get_load_instruction(virtual_call_Relocation* r) const {
 390   return nativeLoadGot_at(_call->plt_load_got());
 391 }
 392 
 393 void PltNativeCallWrapper::verify_resolve_call(address dest) const {
 394   CodeBlob* db = CodeCache::find_blob_unsafe(dest);
 395   if (db == NULL) {
 396     assert(dest == _call->plt_resolve_call(), "sanity");
 397   }
 398 }
 399 
 400 void PltNativeCallWrapper::set_to_interpreted(const methodHandle& method, CompiledICInfo& info) {
 401   assert(!info.to_aot(), "only for nmethod");
 402   CompiledPltStaticCall* csc = CompiledPltStaticCall::at(instruction_address());
 403   csc->set_to_interpreted(method, info.entry());
 404 }
 405 
 406 NativeCallWrapper* AOTCompiledMethod::call_wrapper_at(address call) const {
 407   return new PltNativeCallWrapper((NativePltCall*) call);
 408 }
 409 
 410 NativeCallWrapper* AOTCompiledMethod::call_wrapper_before(address return_pc) const {
 411   return new PltNativeCallWrapper(nativePltCall_before(return_pc));
 412 }
 413 
 414 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(Relocation* call_site) const {
 415   return CompiledPltStaticCall::at(call_site);
 416 }
 417 
 418 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(address call_site) const {
 419   return CompiledPltStaticCall::at(call_site);
 420 }
 421 
 422 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_before(address return_addr) const {
 423   return CompiledPltStaticCall::before(return_addr);
 424 }
 425 
 426 address AOTCompiledMethod::call_instruction_address(address pc) const {
 427   NativePltCall* pltcall = nativePltCall_before(pc);
 428   return pltcall->instruction_address();
 429 }
 430 
 431 bool AOTCompiledMethod::is_evol_dependent_on(Klass* dependee) {
 432   return !is_aot_runtime_stub() && _heap->is_dependent_method(dependee, this);
 433 }
 434 
 435 void AOTCompiledMethod::clear_inline_caches() {
 436   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
 437   if (is_zombie()) {
 438     return;
 439   }
 440 
 441   RelocIterator iter(this);
 442   while (iter.next()) {
 443     iter.reloc()->clear_inline_cache();
 444     if (iter.type() == relocInfo::opt_virtual_call_type) {
 445       CompiledIC* cic = CompiledIC_at(&iter);
 446       assert(cic->is_clean(), "!");
 447       nativePltCall_at(iter.addr())->set_stub_to_clean();
 448     }
 449   }
 450 }
 451 
--- EOF ---