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