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