1 /*
   2  * Copyright (c) 2016, 2019, 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/cardTableBarrierSet.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "oops/method.inline.hpp"
  36 #include "runtime/frame.inline.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/java.hpp"
  39 #include "runtime/os.hpp"
  40 #include "runtime/safepointVerifiers.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "utilities/sizes.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 address* AOTCompiledMethod::orig_pc_addr(const frame* fr) {
  74   return (address*) ((address)fr->unextended_sp() + _meta->orig_pc_offset());
  75 }
  76 
  77 oop AOTCompiledMethod::oop_at(int index) const {
  78   if (index == 0) { // 0 is reserved
  79     return NULL;
  80   }
  81   Metadata** entry = _metadata_got + (index - 1);
  82   intptr_t meta = (intptr_t)*entry;
  83   if ((meta & 1) == 1) {
  84     // already resolved
  85     Klass* k = (Klass*)(meta & ~1);
  86     return k->java_mirror();
  87   }
  88   // The entry is string which we need to resolve.
  89   const char* meta_name = _heap->get_name_at((int)meta);
  90   int klass_len = Bytes::get_Java_u2((address)meta_name);
  91   const char* klass_name = meta_name + 2;
  92   // Quick check the current method's holder.
  93   Klass* k = _method->method_holder();
  94 
  95   ResourceMark rm; // for signature_name()
  96   if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
  97     // Search klass in got cells in DSO which have this compiled method.
  98     k = _heap->get_klass_from_got(klass_name, klass_len, _method);
  99   }
 100   int method_name_len = Bytes::get_Java_u2((address)klass_name + klass_len);
 101   guarantee(method_name_len == 0, "only klass is expected here");
 102   meta = ((intptr_t)k) | 1;
 103   *entry = (Metadata*)meta; // Should be atomic on x64
 104   return k->java_mirror();
 105 }
 106 
 107 Metadata* AOTCompiledMethod::metadata_at(int index) const {
 108   if (index == 0) { // 0 is reserved
 109     return NULL;
 110   }
 111   assert(index - 1 < _metadata_size, "");
 112   {
 113     Metadata** entry = _metadata_got + (index - 1);
 114     intptr_t meta = (intptr_t)*entry;
 115     if ((meta & 1) == 1) {
 116       // already resolved
 117       Metadata *m = (Metadata*)(meta & ~1);
 118       return m;
 119     }
 120     // The entry is string which we need to resolve.
 121     const char* meta_name = _heap->get_name_at((int)meta);
 122     int klass_len = Bytes::get_Java_u2((address)meta_name);
 123     const char* klass_name = meta_name + 2;
 124     // Quick check the current method's holder.
 125     Klass* k = _method->method_holder();
 126     bool klass_matched = true;
 127 
 128     ResourceMark rm; // for signature_name() and find_method()
 129     if (strncmp(k->signature_name(), klass_name, klass_len) != 0) { // Does not match?
 130       // Search klass in got cells in DSO which have this compiled method.
 131       k = _heap->get_klass_from_got(klass_name, klass_len, _method);
 132       klass_matched = false;
 133     }
 134     int method_name_len = Bytes::get_Java_u2((address)klass_name + klass_len);
 135     if (method_name_len == 0) { // Array or Klass name only?
 136       meta = ((intptr_t)k) | 1;
 137       *entry = (Metadata*)meta; // Should be atomic on x64
 138       return (Metadata*)k;
 139     } else { // Method
 140       // Quick check the current method's name.
 141       Method* m = _method;
 142       int signature_len = Bytes::get_Java_u2((address)klass_name + klass_len + 2 + method_name_len);
 143       int full_len = 2 + klass_len + 2 + method_name_len + 2 + signature_len;
 144       if (!klass_matched || memcmp(_name, meta_name, full_len) != 0) { // Does not match?
 145         Thread* thread = Thread::current();
 146         const char* method_name = klass_name + klass_len;
 147         m = AOTCodeHeap::find_method(k, thread, method_name);
 148       }
 149       meta = ((intptr_t)m) | 1;
 150       *entry = (Metadata*)meta; // Should be atomic on x64
 151       return (Metadata*)m;
 152     }
 153   }
 154   ShouldNotReachHere(); return NULL;
 155 }
 156 
 157 void AOTCompiledMethod::do_unloading(bool unloading_occurred) {
 158   unload_nmethod_caches(unloading_occurred);
 159 }
 160 
 161 bool AOTCompiledMethod::make_not_entrant_helper(int new_state) {
 162   // Make sure the method is not flushed in case of a safepoint in code below.
 163   methodHandle the_method(method());
 164   NoSafepointVerifier nsv;
 165 
 166   {
 167     // Enter critical section.  Does not block for safepoint.
 168     MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
 169 
 170     if (*_state_adr == new_state) {
 171       // another thread already performed this transition so nothing
 172       // to do, but return false to indicate this.
 173       return false;
 174     }
 175 
 176     // Change state
 177     OrderAccess::storestore();
 178     *_state_adr = new_state;
 179 
 180     // Log the transition once
 181     log_state_change();
 182 
 183 #ifdef TIERED
 184     // Remain non-entrant forever
 185     if (new_state == not_entrant && method() != NULL) {
 186         method()->set_aot_code(NULL);
 187     }
 188 #endif
 189 
 190     // Remove AOTCompiledMethod from method.
 191     Method::unlink_code(method(), this);
 192   } // leave critical region under CompiledMethod_lock
 193 
 194 
 195   if (TraceCreateZombies) {
 196     ResourceMark m;
 197     const char *new_state_str = (new_state == not_entrant) ? "not entrant" : "not used";
 198     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);
 199   }
 200 
 201   return true;
 202 }
 203 
 204 #ifdef TIERED
 205 bool AOTCompiledMethod::make_entrant() {
 206   assert(!method()->is_old(), "reviving evolved method!");
 207   assert(*_state_adr != not_entrant, "%s", method()->has_aot_code() ? "has_aot_code() not cleared" : "caller didn't check has_aot_code()");
 208 
 209   // Make sure the method is not flushed in case of a safepoint in code below.
 210   methodHandle the_method(method());
 211   NoSafepointVerifier nsv;
 212 
 213   {
 214     // Enter critical section.  Does not block for safepoint.
 215     MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
 216 
 217     if (*_state_adr == in_use) {
 218       // another thread already performed this transition so nothing
 219       // to do, but return false to indicate this.
 220       return false;
 221     }
 222 
 223     // Change state
 224     OrderAccess::storestore();
 225     *_state_adr = in_use;
 226 
 227     // Log the transition once
 228     log_state_change();
 229   } // leave critical region under CompiledMethod_lock
 230 
 231 
 232   if (TraceCreateZombies) {
 233     ResourceMark m;
 234     tty->print_cr("aot method <" INTPTR_FORMAT "> %s code made entrant", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null");
 235   }
 236 
 237   return true;
 238 }
 239 #endif // TIERED
 240 
 241 // Iterate over metadata calling this function.   Used by RedefineClasses
 242 // Copied from nmethod::metadata_do
 243 void AOTCompiledMethod::metadata_do(MetadataClosure* f) {
 244   address low_boundary = verified_entry_point();
 245   {
 246     // Visit all immediate references that are embedded in the instruction stream.
 247     RelocIterator iter(this, low_boundary);
 248     while (iter.next()) {
 249       if (iter.type() == relocInfo::metadata_type ) {
 250         metadata_Relocation* r = iter.metadata_reloc();
 251         // In this metadata, we must only follow those metadatas directly embedded in
 252         // the code.  Other metadatas (oop_index>0) are seen as part of
 253         // the metadata section below.
 254         assert(1 == (r->metadata_is_immediate()) +
 255                (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
 256                "metadata must be found in exactly one place");
 257         if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
 258           Metadata* md = r->metadata_value();
 259           if (md != _method) f->do_metadata(md);
 260         }
 261       } else if (iter.type() == relocInfo::virtual_call_type) {
 262         ResourceMark rm;
 263         // Check compiledIC holders associated with this nmethod
 264         CompiledIC *ic = CompiledIC_at(&iter);
 265         if (ic->is_icholder_call()) {
 266           CompiledICHolder* cichk = ic->cached_icholder();
 267           f->do_metadata(cichk->holder_metadata());
 268           f->do_metadata(cichk->holder_klass());
 269         } else {
 270           // Get Klass* or NULL (if value is -1) from GOT cell of virtual call PLT stub.
 271           Metadata* ic_oop = ic->cached_metadata();
 272           if (ic_oop != NULL) {
 273             f->do_metadata(ic_oop);
 274           }
 275         }
 276       } else if (iter.type() == relocInfo::static_call_type ||
 277                  iter.type() == relocInfo::opt_virtual_call_type){
 278         // Check Method* in AOT c2i stub for other calls.
 279         Metadata* meta = (Metadata*)nativeLoadGot_at(nativePltCall_at(iter.addr())->plt_c2i_stub())->data();
 280         if (meta != NULL) {
 281           f->do_metadata(meta);
 282         }
 283       }
 284     }
 285   }
 286 
 287   // Visit the metadata section
 288   for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
 289     Metadata* m = *p;
 290 
 291     intptr_t meta = (intptr_t)m;
 292     if ((meta & 1) == 1) {
 293       // already resolved
 294       m = (Metadata*)(meta & ~1);
 295     } else {
 296       continue;
 297     }
 298     assert(Metaspace::contains(m), "");
 299     f->do_metadata(m);
 300   }
 301 
 302   // Visit metadata not embedded in the other places.
 303   if (_method != NULL) f->do_metadata(_method);
 304 }
 305 
 306 void AOTCompiledMethod::print() const {
 307   print_on(tty, "AOTCompiledMethod");
 308 }
 309 
 310 void AOTCompiledMethod::print_on(outputStream* st) const {
 311   print_on(st, "AOTCompiledMethod");
 312 }
 313 
 314 // Print out more verbose output usually for a newly created aot method.
 315 void AOTCompiledMethod::print_on(outputStream* st, const char* msg) const {
 316   if (st != NULL) {
 317     ttyLocker ttyl;
 318     st->print("%7d ", (int) st->time_stamp().milliseconds());
 319     st->print("%4d ", _aot_id);    // print compilation number
 320     st->print("    aot[%2d]", _heap->dso_id());
 321     // Stubs have _method == NULL
 322     if (_method == NULL) {
 323       st->print("   %s", _name);
 324     } else {
 325       ResourceMark m;
 326       st->print("   %s", _method->name_and_sig_as_C_string());
 327     }
 328     if (Verbose) {
 329       st->print(" entry at " INTPTR_FORMAT, p2i(_code));
 330     }
 331     if (msg != NULL) {
 332       st->print("   %s", msg);
 333     }
 334     st->cr();
 335   }
 336 }
 337 
 338 void AOTCompiledMethod::print_value_on(outputStream* st) const {
 339   st->print("AOTCompiledMethod ");
 340   print_on(st, NULL);
 341 }
 342 
 343 // Print a short set of xml attributes to identify this aot method.  The
 344 // output should be embedded in some other element.
 345 void AOTCompiledMethod::log_identity(xmlStream* log) const {
 346   log->print(" aot_id='%d'", _aot_id);
 347   log->print(" aot='%2d'", _heap->dso_id());
 348 }
 349 
 350 void AOTCompiledMethod::log_state_change() const {
 351   if (LogCompilation) {
 352     ResourceMark m;
 353     if (xtty != NULL) {
 354       ttyLocker ttyl;  // keep the following output all in one block
 355       if (*_state_adr == not_entrant) {
 356         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'",
 357                          os::current_thread_id());
 358       } else if (*_state_adr == not_used) {
 359         xtty->begin_elem("make_not_used thread='" UINTX_FORMAT "'",
 360                          os::current_thread_id());
 361       } else if (*_state_adr == in_use) {
 362         xtty->begin_elem("make_entrant thread='" UINTX_FORMAT "'",
 363                          os::current_thread_id());
 364       }
 365       log_identity(xtty);
 366       xtty->stamp();
 367       xtty->end_elem();
 368     }
 369   }
 370   if (PrintCompilation) {
 371     ResourceMark m;
 372     if (*_state_adr == not_entrant) {
 373       print_on(tty, "made not entrant");
 374     } else if (*_state_adr == not_used) {
 375       print_on(tty, "made not used");
 376     } else if (*_state_adr == in_use) {
 377       print_on(tty, "made entrant");
 378     }
 379   }
 380 }
 381 
 382 
 383 NativeInstruction* PltNativeCallWrapper::get_load_instruction(virtual_call_Relocation* r) const {
 384   return nativeLoadGot_at(_call->plt_load_got());
 385 }
 386 
 387 void PltNativeCallWrapper::verify_resolve_call(address dest) const {
 388   CodeBlob* db = CodeCache::find_blob_unsafe(dest);
 389   if (db == NULL) {
 390     assert(dest == _call->plt_resolve_call(), "sanity");
 391   }
 392 }
 393 
 394 void PltNativeCallWrapper::set_to_interpreted(const methodHandle& method, CompiledICInfo& info) {
 395   assert(!info.to_aot(), "only for nmethod");
 396   CompiledPltStaticCall* csc = CompiledPltStaticCall::at(instruction_address());
 397   csc->set_to_interpreted(method, info.entry());
 398 }
 399 
 400 NativeCallWrapper* AOTCompiledMethod::call_wrapper_at(address call) const {
 401   return new PltNativeCallWrapper((NativePltCall*) call);
 402 }
 403 
 404 NativeCallWrapper* AOTCompiledMethod::call_wrapper_before(address return_pc) const {
 405   return new PltNativeCallWrapper(nativePltCall_before(return_pc));
 406 }
 407 
 408 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(Relocation* call_site) const {
 409   return CompiledPltStaticCall::at(call_site);
 410 }
 411 
 412 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_at(address call_site) const {
 413   return CompiledPltStaticCall::at(call_site);
 414 }
 415 
 416 CompiledStaticCall* AOTCompiledMethod::compiledStaticCall_before(address return_addr) const {
 417   return CompiledPltStaticCall::before(return_addr);
 418 }
 419 
 420 address AOTCompiledMethod::call_instruction_address(address pc) const {
 421   NativePltCall* pltcall = nativePltCall_before(pc);
 422   return pltcall->instruction_address();
 423 }
 424 
 425 void AOTCompiledMethod::clear_inline_caches() {
 426   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
 427   if (is_zombie()) {
 428     return;
 429   }
 430 
 431   ResourceMark rm;
 432   RelocIterator iter(this);
 433   while (iter.next()) {
 434     iter.reloc()->clear_inline_cache();
 435     if (iter.type() == relocInfo::opt_virtual_call_type) {
 436       CompiledIC* cic = CompiledIC_at(&iter);
 437       assert(cic->is_clean(), "!");
 438       nativePltCall_at(iter.addr())->set_stub_to_clean();
 439     }
 440   }
 441 }