1 /* 2 * Copyright (c) 2005, 2023, 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 25 #include "precompiled.hpp" 26 #include "ci/ciArrayKlass.hpp" 27 #include "ci/ciEnv.hpp" 28 #include "ci/ciKlass.hpp" 29 #include "ci/ciMethod.hpp" 30 #include "classfile/javaClasses.inline.hpp" 31 #include "classfile/vmClasses.hpp" 32 #include "code/dependencies.hpp" 33 #include "compiler/compileLog.hpp" 34 #include "compiler/compileBroker.hpp" 35 #include "compiler/compileTask.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "oops/klass.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "oops/objArrayKlass.hpp" 40 #include "runtime/flags/flagSetting.hpp" 41 #include "runtime/handles.hpp" 42 #include "runtime/handles.inline.hpp" 43 #include "runtime/javaThread.inline.hpp" 44 #include "runtime/jniHandles.inline.hpp" 45 #include "runtime/mutexLocker.hpp" 46 #include "runtime/perfData.hpp" 47 #include "runtime/vmThread.hpp" 48 #include "utilities/copy.hpp" 49 50 51 #ifdef ASSERT 52 static bool must_be_in_vm() { 53 Thread* thread = Thread::current(); 54 if (thread->is_Java_thread()) { 55 return JavaThread::cast(thread)->thread_state() == _thread_in_vm; 56 } else { 57 return true; // Could be VMThread or GC thread 58 } 59 } 60 #endif //ASSERT 61 62 bool Dependencies::_verify_in_progress = false; // don't -Xlog:dependencies 63 64 void Dependencies::initialize(ciEnv* env) { 65 Arena* arena = env->arena(); 66 _oop_recorder = env->oop_recorder(); 67 _log = env->log(); 68 _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0); 69 #if INCLUDE_JVMCI 70 _using_dep_values = false; 71 #endif 72 DEBUG_ONLY(_deps[end_marker] = nullptr); 73 for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) { 74 _deps[i] = new(arena) GrowableArray<ciBaseObject*>(arena, 10, 0, 0); 75 } 76 _content_bytes = nullptr; 77 _size_in_bytes = (size_t)-1; 78 79 assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity"); 80 } 81 82 void Dependencies::assert_evol_method(ciMethod* m) { 83 assert_common_1(evol_method, m); 84 } 85 86 void Dependencies::assert_leaf_type(ciKlass* ctxk) { 87 if (ctxk->is_array_klass()) { 88 // As a special case, support this assertion on an array type, 89 // which reduces to an assertion on its element type. 90 // Note that this cannot be done with assertions that 91 // relate to concreteness or abstractness. 92 ciType* elemt = ctxk->as_array_klass()->base_element_type(); 93 if (!elemt->is_instance_klass()) return; // Ex: int[][] 94 ctxk = elemt->as_instance_klass(); 95 //if (ctxk->is_final()) return; // Ex: String[][] 96 } 97 check_ctxk(ctxk); 98 assert_common_1(leaf_type, ctxk); 99 } 100 101 void Dependencies::assert_abstract_with_unique_concrete_subtype(ciKlass* ctxk, ciKlass* conck) { 102 check_ctxk_abstract(ctxk); 103 assert_common_2(abstract_with_unique_concrete_subtype, ctxk, conck); 104 } 105 106 void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm) { 107 check_ctxk(ctxk); 108 check_unique_method(ctxk, uniqm); 109 assert_common_2(unique_concrete_method_2, ctxk, uniqm); 110 } 111 112 void Dependencies::assert_unique_concrete_method(ciKlass* ctxk, ciMethod* uniqm, ciKlass* resolved_klass, ciMethod* resolved_method) { 113 check_ctxk(ctxk); 114 check_unique_method(ctxk, uniqm); 115 if (UseVtableBasedCHA) { 116 assert_common_4(unique_concrete_method_4, ctxk, uniqm, resolved_klass, resolved_method); 117 } else { 118 assert_common_2(unique_concrete_method_2, ctxk, uniqm); 119 } 120 } 121 122 void Dependencies::assert_unique_implementor(ciInstanceKlass* ctxk, ciInstanceKlass* uniqk) { 123 check_ctxk(ctxk); 124 check_unique_implementor(ctxk, uniqk); 125 assert_common_2(unique_implementor, ctxk, uniqk); 126 } 127 128 void Dependencies::assert_has_no_finalizable_subclasses(ciKlass* ctxk) { 129 check_ctxk(ctxk); 130 assert_common_1(no_finalizable_subclasses, ctxk); 131 } 132 133 void Dependencies::assert_call_site_target_value(ciCallSite* call_site, ciMethodHandle* method_handle) { 134 assert_common_2(call_site_target_value, call_site, method_handle); 135 } 136 137 #if INCLUDE_JVMCI 138 139 Dependencies::Dependencies(Arena* arena, OopRecorder* oop_recorder, CompileLog* log) { 140 _oop_recorder = oop_recorder; 141 _log = log; 142 _dep_seen = new(arena) GrowableArray<int>(arena, 500, 0, 0); 143 _using_dep_values = true; 144 DEBUG_ONLY(_dep_values[end_marker] = nullptr); 145 for (int i = (int)FIRST_TYPE; i < (int)TYPE_LIMIT; i++) { 146 _dep_values[i] = new(arena) GrowableArray<DepValue>(arena, 10, 0, DepValue()); 147 } 148 _content_bytes = nullptr; 149 _size_in_bytes = (size_t)-1; 150 151 assert(TYPE_LIMIT <= (1<<LG2_TYPE_LIMIT), "sanity"); 152 } 153 154 void Dependencies::assert_evol_method(Method* m) { 155 assert_common_1(evol_method, DepValue(_oop_recorder, m)); 156 } 157 158 void Dependencies::assert_has_no_finalizable_subclasses(Klass* ctxk) { 159 check_ctxk(ctxk); 160 assert_common_1(no_finalizable_subclasses, DepValue(_oop_recorder, ctxk)); 161 } 162 163 void Dependencies::assert_leaf_type(Klass* ctxk) { 164 if (ctxk->is_array_klass()) { 165 // As a special case, support this assertion on an array type, 166 // which reduces to an assertion on its element type. 167 // Note that this cannot be done with assertions that 168 // relate to concreteness or abstractness. 169 BasicType elemt = ArrayKlass::cast(ctxk)->element_type(); 170 if (is_java_primitive(elemt)) return; // Ex: int[][] 171 ctxk = ObjArrayKlass::cast(ctxk)->bottom_klass(); 172 //if (ctxk->is_final()) return; // Ex: String[][] 173 } 174 check_ctxk(ctxk); 175 assert_common_1(leaf_type, DepValue(_oop_recorder, ctxk)); 176 } 177 178 void Dependencies::assert_abstract_with_unique_concrete_subtype(Klass* ctxk, Klass* conck) { 179 check_ctxk_abstract(ctxk); 180 DepValue ctxk_dv(_oop_recorder, ctxk); 181 DepValue conck_dv(_oop_recorder, conck, &ctxk_dv); 182 assert_common_2(abstract_with_unique_concrete_subtype, ctxk_dv, conck_dv); 183 } 184 185 void Dependencies::assert_unique_implementor(InstanceKlass* ctxk, InstanceKlass* uniqk) { 186 check_ctxk(ctxk); 187 assert(ctxk->is_interface(), "not an interface"); 188 assert(ctxk->implementor() == uniqk, "not a unique implementor"); 189 assert_common_2(unique_implementor, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqk)); 190 } 191 192 void Dependencies::assert_unique_concrete_method(Klass* ctxk, Method* uniqm) { 193 check_ctxk(ctxk); 194 check_unique_method(ctxk, uniqm); 195 assert_common_2(unique_concrete_method_2, DepValue(_oop_recorder, ctxk), DepValue(_oop_recorder, uniqm)); 196 } 197 198 void Dependencies::assert_call_site_target_value(oop call_site, oop method_handle) { 199 assert_common_2(call_site_target_value, DepValue(_oop_recorder, JNIHandles::make_local(call_site)), DepValue(_oop_recorder, JNIHandles::make_local(method_handle))); 200 } 201 202 #endif // INCLUDE_JVMCI 203 204 205 // Helper function. If we are adding a new dep. under ctxk2, 206 // try to find an old dep. under a broader* ctxk1. If there is 207 // 208 bool Dependencies::maybe_merge_ctxk(GrowableArray<ciBaseObject*>* deps, 209 int ctxk_i, ciKlass* ctxk2) { 210 ciKlass* ctxk1 = deps->at(ctxk_i)->as_metadata()->as_klass(); 211 if (ctxk2->is_subtype_of(ctxk1)) { 212 return true; // success, and no need to change 213 } else if (ctxk1->is_subtype_of(ctxk2)) { 214 // new context class fully subsumes previous one 215 deps->at_put(ctxk_i, ctxk2); 216 return true; 217 } else { 218 return false; 219 } 220 } 221 222 void Dependencies::assert_common_1(DepType dept, ciBaseObject* x) { 223 assert(dep_args(dept) == 1, "sanity"); 224 log_dependency(dept, x); 225 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 226 227 // see if the same (or a similar) dep is already recorded 228 if (note_dep_seen(dept, x)) { 229 assert(deps->find(x) >= 0, "sanity"); 230 } else { 231 deps->append(x); 232 } 233 } 234 235 void Dependencies::assert_common_2(DepType dept, 236 ciBaseObject* x0, ciBaseObject* x1) { 237 assert(dep_args(dept) == 2, "sanity"); 238 log_dependency(dept, x0, x1); 239 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 240 241 // see if the same (or a similar) dep is already recorded 242 bool has_ctxk = has_explicit_context_arg(dept); 243 if (has_ctxk) { 244 assert(dep_context_arg(dept) == 0, "sanity"); 245 if (note_dep_seen(dept, x1)) { 246 // look in this bucket for redundant assertions 247 const int stride = 2; 248 for (int i = deps->length(); (i -= stride) >= 0; ) { 249 ciBaseObject* y1 = deps->at(i+1); 250 if (x1 == y1) { // same subject; check the context 251 if (maybe_merge_ctxk(deps, i+0, x0->as_metadata()->as_klass())) { 252 return; 253 } 254 } 255 } 256 } 257 } else { 258 bool dep_seen_x0 = note_dep_seen(dept, x0); // records x0 for future queries 259 bool dep_seen_x1 = note_dep_seen(dept, x1); // records x1 for future queries 260 if (dep_seen_x0 && dep_seen_x1) { 261 // look in this bucket for redundant assertions 262 const int stride = 2; 263 for (int i = deps->length(); (i -= stride) >= 0; ) { 264 ciBaseObject* y0 = deps->at(i+0); 265 ciBaseObject* y1 = deps->at(i+1); 266 if (x0 == y0 && x1 == y1) { 267 return; 268 } 269 } 270 } 271 } 272 273 // append the assertion in the correct bucket: 274 deps->append(x0); 275 deps->append(x1); 276 } 277 278 void Dependencies::assert_common_4(DepType dept, 279 ciKlass* ctxk, ciBaseObject* x1, ciBaseObject* x2, ciBaseObject* x3) { 280 assert(has_explicit_context_arg(dept), "sanity"); 281 assert(dep_context_arg(dept) == 0, "sanity"); 282 assert(dep_args(dept) == 4, "sanity"); 283 log_dependency(dept, ctxk, x1, x2, x3); 284 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 285 286 // see if the same (or a similar) dep is already recorded 287 bool dep_seen_x1 = note_dep_seen(dept, x1); // records x1 for future queries 288 bool dep_seen_x2 = note_dep_seen(dept, x2); // records x2 for future queries 289 bool dep_seen_x3 = note_dep_seen(dept, x3); // records x3 for future queries 290 if (dep_seen_x1 && dep_seen_x2 && dep_seen_x3) { 291 // look in this bucket for redundant assertions 292 const int stride = 4; 293 for (int i = deps->length(); (i -= stride) >= 0; ) { 294 ciBaseObject* y1 = deps->at(i+1); 295 ciBaseObject* y2 = deps->at(i+2); 296 ciBaseObject* y3 = deps->at(i+3); 297 if (x1 == y1 && x2 == y2 && x3 == y3) { // same subjects; check the context 298 if (maybe_merge_ctxk(deps, i+0, ctxk)) { 299 return; 300 } 301 } 302 } 303 } 304 // append the assertion in the correct bucket: 305 deps->append(ctxk); 306 deps->append(x1); 307 deps->append(x2); 308 deps->append(x3); 309 } 310 311 #if INCLUDE_JVMCI 312 bool Dependencies::maybe_merge_ctxk(GrowableArray<DepValue>* deps, 313 int ctxk_i, DepValue ctxk2_dv) { 314 Klass* ctxk1 = deps->at(ctxk_i).as_klass(_oop_recorder); 315 Klass* ctxk2 = ctxk2_dv.as_klass(_oop_recorder); 316 if (ctxk2->is_subtype_of(ctxk1)) { 317 return true; // success, and no need to change 318 } else if (ctxk1->is_subtype_of(ctxk2)) { 319 // new context class fully subsumes previous one 320 deps->at_put(ctxk_i, ctxk2_dv); 321 return true; 322 } else { 323 return false; 324 } 325 } 326 327 void Dependencies::assert_common_1(DepType dept, DepValue x) { 328 assert(dep_args(dept) == 1, "sanity"); 329 //log_dependency(dept, x); 330 GrowableArray<DepValue>* deps = _dep_values[dept]; 331 332 // see if the same (or a similar) dep is already recorded 333 if (note_dep_seen(dept, x)) { 334 assert(deps->find(x) >= 0, "sanity"); 335 } else { 336 deps->append(x); 337 } 338 } 339 340 void Dependencies::assert_common_2(DepType dept, 341 DepValue x0, DepValue x1) { 342 assert(dep_args(dept) == 2, "sanity"); 343 //log_dependency(dept, x0, x1); 344 GrowableArray<DepValue>* deps = _dep_values[dept]; 345 346 // see if the same (or a similar) dep is already recorded 347 bool has_ctxk = has_explicit_context_arg(dept); 348 if (has_ctxk) { 349 assert(dep_context_arg(dept) == 0, "sanity"); 350 if (note_dep_seen(dept, x1)) { 351 // look in this bucket for redundant assertions 352 const int stride = 2; 353 for (int i = deps->length(); (i -= stride) >= 0; ) { 354 DepValue y1 = deps->at(i+1); 355 if (x1 == y1) { // same subject; check the context 356 if (maybe_merge_ctxk(deps, i+0, x0)) { 357 return; 358 } 359 } 360 } 361 } 362 } else { 363 bool dep_seen_x0 = note_dep_seen(dept, x0); // records x0 for future queries 364 bool dep_seen_x1 = note_dep_seen(dept, x1); // records x1 for future queries 365 if (dep_seen_x0 && dep_seen_x1) { 366 // look in this bucket for redundant assertions 367 const int stride = 2; 368 for (int i = deps->length(); (i -= stride) >= 0; ) { 369 DepValue y0 = deps->at(i+0); 370 DepValue y1 = deps->at(i+1); 371 if (x0 == y0 && x1 == y1) { 372 return; 373 } 374 } 375 } 376 } 377 378 // append the assertion in the correct bucket: 379 deps->append(x0); 380 deps->append(x1); 381 } 382 #endif // INCLUDE_JVMCI 383 384 /// Support for encoding dependencies into an nmethod: 385 386 void Dependencies::copy_to(nmethod* nm) { 387 address beg = nm->dependencies_begin(); 388 address end = nm->dependencies_end(); 389 guarantee(end - beg >= (ptrdiff_t) size_in_bytes(), "bad sizing"); 390 Copy::disjoint_words((HeapWord*) content_bytes(), 391 (HeapWord*) beg, 392 size_in_bytes() / sizeof(HeapWord)); 393 assert(size_in_bytes() % sizeof(HeapWord) == 0, "copy by words"); 394 } 395 396 static int sort_dep(ciBaseObject** p1, ciBaseObject** p2, int narg) { 397 for (int i = 0; i < narg; i++) { 398 int diff = p1[i]->ident() - p2[i]->ident(); 399 if (diff != 0) return diff; 400 } 401 return 0; 402 } 403 static int sort_dep_arg_1(ciBaseObject** p1, ciBaseObject** p2) 404 { return sort_dep(p1, p2, 1); } 405 static int sort_dep_arg_2(ciBaseObject** p1, ciBaseObject** p2) 406 { return sort_dep(p1, p2, 2); } 407 static int sort_dep_arg_3(ciBaseObject** p1, ciBaseObject** p2) 408 { return sort_dep(p1, p2, 3); } 409 static int sort_dep_arg_4(ciBaseObject** p1, ciBaseObject** p2) 410 { return sort_dep(p1, p2, 4); } 411 412 #if INCLUDE_JVMCI 413 // metadata deps are sorted before object deps 414 static int sort_dep_value(Dependencies::DepValue* p1, Dependencies::DepValue* p2, int narg) { 415 for (int i = 0; i < narg; i++) { 416 int diff = p1[i].sort_key() - p2[i].sort_key(); 417 if (diff != 0) return diff; 418 } 419 return 0; 420 } 421 static int sort_dep_value_arg_1(Dependencies::DepValue* p1, Dependencies::DepValue* p2) 422 { return sort_dep_value(p1, p2, 1); } 423 static int sort_dep_value_arg_2(Dependencies::DepValue* p1, Dependencies::DepValue* p2) 424 { return sort_dep_value(p1, p2, 2); } 425 static int sort_dep_value_arg_3(Dependencies::DepValue* p1, Dependencies::DepValue* p2) 426 { return sort_dep_value(p1, p2, 3); } 427 #endif // INCLUDE_JVMCI 428 429 void Dependencies::sort_all_deps() { 430 #if INCLUDE_JVMCI 431 if (_using_dep_values) { 432 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 433 DepType dept = (DepType)deptv; 434 GrowableArray<DepValue>* deps = _dep_values[dept]; 435 if (deps->length() <= 1) continue; 436 switch (dep_args(dept)) { 437 case 1: deps->sort(sort_dep_value_arg_1, 1); break; 438 case 2: deps->sort(sort_dep_value_arg_2, 2); break; 439 case 3: deps->sort(sort_dep_value_arg_3, 3); break; 440 default: ShouldNotReachHere(); break; 441 } 442 } 443 return; 444 } 445 #endif // INCLUDE_JVMCI 446 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 447 DepType dept = (DepType)deptv; 448 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 449 if (deps->length() <= 1) continue; 450 switch (dep_args(dept)) { 451 case 1: deps->sort(sort_dep_arg_1, 1); break; 452 case 2: deps->sort(sort_dep_arg_2, 2); break; 453 case 3: deps->sort(sort_dep_arg_3, 3); break; 454 case 4: deps->sort(sort_dep_arg_4, 4); break; 455 default: ShouldNotReachHere(); break; 456 } 457 } 458 } 459 460 size_t Dependencies::estimate_size_in_bytes() { 461 size_t est_size = 100; 462 #if INCLUDE_JVMCI 463 if (_using_dep_values) { 464 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 465 DepType dept = (DepType)deptv; 466 GrowableArray<DepValue>* deps = _dep_values[dept]; 467 est_size += deps->length() * 2; // tags and argument(s) 468 } 469 return est_size; 470 } 471 #endif // INCLUDE_JVMCI 472 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 473 DepType dept = (DepType)deptv; 474 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 475 est_size += deps->length()*2; // tags and argument(s) 476 } 477 return est_size; 478 } 479 480 ciKlass* Dependencies::ctxk_encoded_as_null(DepType dept, ciBaseObject* x) { 481 switch (dept) { 482 case unique_concrete_method_2: 483 case unique_concrete_method_4: 484 return x->as_metadata()->as_method()->holder(); 485 default: 486 return nullptr; // let nullptr be nullptr 487 } 488 } 489 490 Klass* Dependencies::ctxk_encoded_as_null(DepType dept, Metadata* x) { 491 assert(must_be_in_vm(), "raw oops here"); 492 switch (dept) { 493 case unique_concrete_method_2: 494 case unique_concrete_method_4: 495 assert(x->is_method(), "sanity"); 496 return ((Method*)x)->method_holder(); 497 default: 498 return nullptr; // let nullptr be nullptr 499 } 500 } 501 502 void Dependencies::encode_content_bytes() { 503 sort_all_deps(); 504 505 // cast is safe, no deps can overflow INT_MAX 506 CompressedWriteStream bytes((int)estimate_size_in_bytes()); 507 508 #if INCLUDE_JVMCI 509 if (_using_dep_values) { 510 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 511 DepType dept = (DepType)deptv; 512 GrowableArray<DepValue>* deps = _dep_values[dept]; 513 if (deps->length() == 0) continue; 514 int stride = dep_args(dept); 515 int ctxkj = dep_context_arg(dept); // -1 if no context arg 516 assert(stride > 0, "sanity"); 517 for (int i = 0; i < deps->length(); i += stride) { 518 jbyte code_byte = (jbyte)dept; 519 int skipj = -1; 520 if (ctxkj >= 0 && ctxkj+1 < stride) { 521 Klass* ctxk = deps->at(i+ctxkj+0).as_klass(_oop_recorder); 522 DepValue x = deps->at(i+ctxkj+1); // following argument 523 if (ctxk == ctxk_encoded_as_null(dept, x.as_metadata(_oop_recorder))) { 524 skipj = ctxkj; // we win: maybe one less oop to keep track of 525 code_byte |= default_context_type_bit; 526 } 527 } 528 bytes.write_byte(code_byte); 529 for (int j = 0; j < stride; j++) { 530 if (j == skipj) continue; 531 DepValue v = deps->at(i+j); 532 int idx = v.index(); 533 bytes.write_int(idx); 534 } 535 } 536 } 537 } else { 538 #endif // INCLUDE_JVMCI 539 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 540 DepType dept = (DepType)deptv; 541 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 542 if (deps->length() == 0) continue; 543 int stride = dep_args(dept); 544 int ctxkj = dep_context_arg(dept); // -1 if no context arg 545 assert(stride > 0, "sanity"); 546 for (int i = 0; i < deps->length(); i += stride) { 547 jbyte code_byte = (jbyte)dept; 548 int skipj = -1; 549 if (ctxkj >= 0 && ctxkj+1 < stride) { 550 ciKlass* ctxk = deps->at(i+ctxkj+0)->as_metadata()->as_klass(); 551 ciBaseObject* x = deps->at(i+ctxkj+1); // following argument 552 if (ctxk == ctxk_encoded_as_null(dept, x)) { 553 skipj = ctxkj; // we win: maybe one less oop to keep track of 554 code_byte |= default_context_type_bit; 555 } 556 } 557 bytes.write_byte(code_byte); 558 for (int j = 0; j < stride; j++) { 559 if (j == skipj) continue; 560 ciBaseObject* v = deps->at(i+j); 561 int idx; 562 if (v->is_object()) { 563 idx = _oop_recorder->find_index(v->as_object()->constant_encoding()); 564 } else { 565 ciMetadata* meta = v->as_metadata(); 566 idx = _oop_recorder->find_index(meta->constant_encoding()); 567 } 568 bytes.write_int(idx); 569 } 570 } 571 } 572 #if INCLUDE_JVMCI 573 } 574 #endif 575 576 // write a sentinel byte to mark the end 577 bytes.write_byte(end_marker); 578 579 // round it out to a word boundary 580 while (bytes.position() % sizeof(HeapWord) != 0) { 581 bytes.write_byte(end_marker); 582 } 583 584 // check whether the dept byte encoding really works 585 assert((jbyte)default_context_type_bit != 0, "byte overflow"); 586 587 _content_bytes = bytes.buffer(); 588 _size_in_bytes = bytes.position(); 589 } 590 591 592 const char* Dependencies::_dep_name[TYPE_LIMIT] = { 593 "end_marker", 594 "evol_method", 595 "leaf_type", 596 "abstract_with_unique_concrete_subtype", 597 "unique_concrete_method_2", 598 "unique_concrete_method_4", 599 "unique_implementor", 600 "no_finalizable_subclasses", 601 "call_site_target_value" 602 }; 603 604 int Dependencies::_dep_args[TYPE_LIMIT] = { 605 -1,// end_marker 606 1, // evol_method m 607 1, // leaf_type ctxk 608 2, // abstract_with_unique_concrete_subtype ctxk, k 609 2, // unique_concrete_method_2 ctxk, m 610 4, // unique_concrete_method_4 ctxk, m, resolved_klass, resolved_method 611 2, // unique_implementor ctxk, implementor 612 1, // no_finalizable_subclasses ctxk 613 2 // call_site_target_value call_site, method_handle 614 }; 615 616 const char* Dependencies::dep_name(Dependencies::DepType dept) { 617 if (!dept_in_mask(dept, all_types)) return "?bad-dep?"; 618 return _dep_name[dept]; 619 } 620 621 int Dependencies::dep_args(Dependencies::DepType dept) { 622 if (!dept_in_mask(dept, all_types)) return -1; 623 return _dep_args[dept]; 624 } 625 626 void Dependencies::check_valid_dependency_type(DepType dept) { 627 guarantee(FIRST_TYPE <= dept && dept < TYPE_LIMIT, "invalid dependency type: %d", (int) dept); 628 } 629 630 Dependencies::DepType Dependencies::validate_dependencies(CompileTask* task, char** failure_detail) { 631 int klass_violations = 0; 632 DepType result = end_marker; 633 for (Dependencies::DepStream deps(this); deps.next(); ) { 634 Klass* witness = deps.check_dependency(); 635 if (witness != nullptr) { 636 if (klass_violations == 0) { 637 result = deps.type(); 638 if (failure_detail != nullptr && klass_violations == 0) { 639 // Use a fixed size buffer to prevent the string stream from 640 // resizing in the context of an inner resource mark. 641 char* buffer = NEW_RESOURCE_ARRAY(char, O_BUFLEN); 642 stringStream st(buffer, O_BUFLEN); 643 deps.print_dependency(&st, witness, true); 644 *failure_detail = st.as_string(); 645 } 646 } 647 klass_violations++; 648 if (xtty == nullptr) { 649 // If we're not logging then a single violation is sufficient, 650 // otherwise we want to log all the dependences which were 651 // violated. 652 break; 653 } 654 } 655 } 656 657 return result; 658 } 659 660 // for the sake of the compiler log, print out current dependencies: 661 void Dependencies::log_all_dependencies() { 662 if (log() == nullptr) return; 663 ResourceMark rm; 664 for (int deptv = (int)FIRST_TYPE; deptv < (int)TYPE_LIMIT; deptv++) { 665 DepType dept = (DepType)deptv; 666 GrowableArray<ciBaseObject*>* deps = _deps[dept]; 667 int deplen = deps->length(); 668 if (deplen == 0) { 669 continue; 670 } 671 int stride = dep_args(dept); 672 GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(stride); 673 for (int i = 0; i < deps->length(); i += stride) { 674 for (int j = 0; j < stride; j++) { 675 // flush out the identities before printing 676 ciargs->push(deps->at(i+j)); 677 } 678 write_dependency_to(log(), dept, ciargs); 679 ciargs->clear(); 680 } 681 guarantee(deplen == deps->length(), "deps array cannot grow inside nested ResoureMark scope"); 682 } 683 } 684 685 void Dependencies::write_dependency_to(CompileLog* log, 686 DepType dept, 687 GrowableArray<DepArgument>* args, 688 Klass* witness) { 689 if (log == nullptr) { 690 return; 691 } 692 ResourceMark rm; 693 ciEnv* env = ciEnv::current(); 694 GrowableArray<ciBaseObject*>* ciargs = new GrowableArray<ciBaseObject*>(args->length()); 695 for (GrowableArrayIterator<DepArgument> it = args->begin(); it != args->end(); ++it) { 696 DepArgument arg = *it; 697 if (arg.is_oop()) { 698 ciargs->push(env->get_object(arg.oop_value())); 699 } else { 700 ciargs->push(env->get_metadata(arg.metadata_value())); 701 } 702 } 703 int argslen = ciargs->length(); 704 Dependencies::write_dependency_to(log, dept, ciargs, witness); 705 guarantee(argslen == ciargs->length(), "ciargs array cannot grow inside nested ResoureMark scope"); 706 } 707 708 void Dependencies::write_dependency_to(CompileLog* log, 709 DepType dept, 710 GrowableArray<ciBaseObject*>* args, 711 Klass* witness) { 712 if (log == nullptr) { 713 return; 714 } 715 ResourceMark rm; 716 GrowableArray<int>* argids = new GrowableArray<int>(args->length()); 717 for (GrowableArrayIterator<ciBaseObject*> it = args->begin(); it != args->end(); ++it) { 718 ciBaseObject* obj = *it; 719 if (obj->is_object()) { 720 argids->push(log->identify(obj->as_object())); 721 } else { 722 argids->push(log->identify(obj->as_metadata())); 723 } 724 } 725 if (witness != nullptr) { 726 log->begin_elem("dependency_failed"); 727 } else { 728 log->begin_elem("dependency"); 729 } 730 log->print(" type='%s'", dep_name(dept)); 731 const int ctxkj = dep_context_arg(dept); // -1 if no context arg 732 if (ctxkj >= 0 && ctxkj < argids->length()) { 733 log->print(" ctxk='%d'", argids->at(ctxkj)); 734 } 735 // write remaining arguments, if any. 736 for (int j = 0; j < argids->length(); j++) { 737 if (j == ctxkj) continue; // already logged 738 if (j == 1) { 739 log->print( " x='%d'", argids->at(j)); 740 } else { 741 log->print(" x%d='%d'", j, argids->at(j)); 742 } 743 } 744 if (witness != nullptr) { 745 log->object("witness", witness); 746 log->stamp(); 747 } 748 log->end_elem(); 749 } 750 751 void Dependencies::write_dependency_to(xmlStream* xtty, 752 DepType dept, 753 GrowableArray<DepArgument>* args, 754 Klass* witness) { 755 if (xtty == nullptr) { 756 return; 757 } 758 Thread* thread = Thread::current(); 759 HandleMark rm(thread); 760 ttyLocker ttyl; 761 int ctxkj = dep_context_arg(dept); // -1 if no context arg 762 if (witness != nullptr) { 763 xtty->begin_elem("dependency_failed"); 764 } else { 765 xtty->begin_elem("dependency"); 766 } 767 xtty->print(" type='%s'", dep_name(dept)); 768 if (ctxkj >= 0) { 769 xtty->object("ctxk", args->at(ctxkj).metadata_value()); 770 } 771 // write remaining arguments, if any. 772 for (int j = 0; j < args->length(); j++) { 773 if (j == ctxkj) continue; // already logged 774 DepArgument arg = args->at(j); 775 if (j == 1) { 776 if (arg.is_oop()) { 777 xtty->object("x", Handle(thread, arg.oop_value())); 778 } else { 779 xtty->object("x", arg.metadata_value()); 780 } 781 } else { 782 char xn[12]; 783 os::snprintf_checked(xn, sizeof(xn), "x%d", j); 784 if (arg.is_oop()) { 785 xtty->object(xn, Handle(thread, arg.oop_value())); 786 } else { 787 xtty->object(xn, arg.metadata_value()); 788 } 789 } 790 } 791 if (witness != nullptr) { 792 xtty->object("witness", witness); 793 xtty->stamp(); 794 } 795 xtty->end_elem(); 796 } 797 798 void Dependencies::print_dependency(DepType dept, GrowableArray<DepArgument>* args, 799 Klass* witness, outputStream* st) { 800 ResourceMark rm; 801 ttyLocker ttyl; // keep the following output all in one block 802 st->print_cr("%s of type %s", 803 (witness == nullptr)? "Dependency": "Failed dependency", 804 dep_name(dept)); 805 // print arguments 806 int ctxkj = dep_context_arg(dept); // -1 if no context arg 807 for (int j = 0; j < args->length(); j++) { 808 DepArgument arg = args->at(j); 809 bool put_star = false; 810 if (arg.is_null()) continue; 811 const char* what; 812 if (j == ctxkj) { 813 assert(arg.is_metadata(), "must be"); 814 what = "context"; 815 put_star = !Dependencies::is_concrete_klass((Klass*)arg.metadata_value()); 816 } else if (arg.is_method()) { 817 what = "method "; 818 put_star = !Dependencies::is_concrete_method((Method*)arg.metadata_value(), nullptr); 819 } else if (arg.is_klass()) { 820 what = "class "; 821 } else { 822 what = "object "; 823 } 824 st->print(" %s = %s", what, (put_star? "*": "")); 825 if (arg.is_klass()) { 826 st->print("%s", ((Klass*)arg.metadata_value())->external_name()); 827 } else if (arg.is_method()) { 828 ((Method*)arg.metadata_value())->print_value_on(st); 829 } else if (arg.is_oop()) { 830 arg.oop_value()->print_value_on(st); 831 } else { 832 ShouldNotReachHere(); // Provide impl for this type. 833 } 834 835 st->cr(); 836 } 837 if (witness != nullptr) { 838 bool put_star = !Dependencies::is_concrete_klass(witness); 839 st->print_cr(" witness = %s%s", 840 (put_star? "*": ""), 841 witness->external_name()); 842 } 843 } 844 845 void Dependencies::DepStream::log_dependency(Klass* witness) { 846 if (_deps == nullptr && xtty == nullptr) return; // fast cutout for runtime 847 ResourceMark rm; 848 const int nargs = argument_count(); 849 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 850 for (int j = 0; j < nargs; j++) { 851 if (is_oop_argument(j)) { 852 args->push(argument_oop(j)); 853 } else { 854 args->push(argument(j)); 855 } 856 } 857 int argslen = args->length(); 858 if (_deps != nullptr && _deps->log() != nullptr) { 859 if (ciEnv::current() != nullptr) { 860 Dependencies::write_dependency_to(_deps->log(), type(), args, witness); 861 } else { 862 // Treat the CompileLog as an xmlstream instead 863 Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness); 864 } 865 } else { 866 Dependencies::write_dependency_to(xtty, type(), args, witness); 867 } 868 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 869 } 870 871 void Dependencies::DepStream::print_dependency(outputStream* st, Klass* witness, bool verbose) { 872 ResourceMark rm; 873 int nargs = argument_count(); 874 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 875 for (int j = 0; j < nargs; j++) { 876 if (is_oop_argument(j)) { 877 args->push(argument_oop(j)); 878 } else { 879 args->push(argument(j)); 880 } 881 } 882 int argslen = args->length(); 883 Dependencies::print_dependency(type(), args, witness, st); 884 if (verbose) { 885 if (_code != nullptr) { 886 st->print(" code: "); 887 _code->print_value_on(st); 888 st->cr(); 889 } 890 } 891 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 892 } 893 894 895 /// Dependency stream support (decodes dependencies from an nmethod): 896 897 #ifdef ASSERT 898 void Dependencies::DepStream::initial_asserts(size_t byte_limit) { 899 assert(must_be_in_vm(), "raw oops here"); 900 _byte_limit = byte_limit; 901 _type = (DepType)(end_marker-1); // defeat "already at end" assert 902 assert((_code!=nullptr) + (_deps!=nullptr) == 1, "one or t'other"); 903 } 904 #endif //ASSERT 905 906 bool Dependencies::DepStream::next() { 907 assert(_type != end_marker, "already at end"); 908 if (_bytes.position() == 0 && _code != nullptr 909 && _code->dependencies_size() == 0) { 910 // Method has no dependencies at all. 911 return false; 912 } 913 int code_byte = (_bytes.read_byte() & 0xFF); 914 if (code_byte == end_marker) { 915 DEBUG_ONLY(_type = end_marker); 916 return false; 917 } else { 918 int ctxk_bit = (code_byte & Dependencies::default_context_type_bit); 919 code_byte -= ctxk_bit; 920 DepType dept = (DepType)code_byte; 921 _type = dept; 922 Dependencies::check_valid_dependency_type(dept); 923 int stride = _dep_args[dept]; 924 assert(stride == dep_args(dept), "sanity"); 925 int skipj = -1; 926 if (ctxk_bit != 0) { 927 skipj = 0; // currently the only context argument is at zero 928 assert(skipj == dep_context_arg(dept), "zero arg always ctxk"); 929 } 930 for (int j = 0; j < stride; j++) { 931 _xi[j] = (j == skipj)? 0: _bytes.read_int(); 932 } 933 DEBUG_ONLY(_xi[stride] = -1); // help detect overruns 934 return true; 935 } 936 } 937 938 inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) { 939 Metadata* o = nullptr; 940 if (_code != nullptr) { 941 o = _code->metadata_at(i); 942 } else { 943 o = _deps->oop_recorder()->metadata_at(i); 944 } 945 return o; 946 } 947 948 inline oop Dependencies::DepStream::recorded_oop_at(int i) { 949 return (_code != nullptr) 950 ? _code->oop_at(i) 951 : JNIHandles::resolve(_deps->oop_recorder()->oop_at(i)); 952 } 953 954 Metadata* Dependencies::DepStream::argument(int i) { 955 Metadata* result = recorded_metadata_at(argument_index(i)); 956 957 if (result == nullptr) { // Explicit context argument can be compressed 958 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 959 if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) { 960 result = ctxk_encoded_as_null(type(), argument(ctxkj+1)); 961 } 962 } 963 964 assert(result == nullptr || result->is_klass() || result->is_method(), "must be"); 965 return result; 966 } 967 968 /** 969 * Returns a unique identifier for each dependency argument. 970 */ 971 uintptr_t Dependencies::DepStream::get_identifier(int i) { 972 if (is_oop_argument(i)) { 973 return (uintptr_t)(oopDesc*)argument_oop(i); 974 } else { 975 return (uintptr_t)argument(i); 976 } 977 } 978 979 oop Dependencies::DepStream::argument_oop(int i) { 980 oop result = recorded_oop_at(argument_index(i)); 981 assert(oopDesc::is_oop_or_null(result), "must be"); 982 return result; 983 } 984 985 InstanceKlass* Dependencies::DepStream::context_type() { 986 assert(must_be_in_vm(), "raw oops here"); 987 988 // Most dependencies have an explicit context type argument. 989 { 990 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 991 if (ctxkj >= 0) { 992 Metadata* k = argument(ctxkj); 993 assert(k != nullptr && k->is_klass(), "type check"); 994 return InstanceKlass::cast((Klass*)k); 995 } 996 } 997 998 // Some dependencies are using the klass of the first object 999 // argument as implicit context type. 1000 { 1001 int ctxkj = dep_implicit_context_arg(type()); 1002 if (ctxkj >= 0) { 1003 Klass* k = argument_oop(ctxkj)->klass(); 1004 assert(k != nullptr, "type check"); 1005 return InstanceKlass::cast(k); 1006 } 1007 } 1008 1009 // And some dependencies don't have a context type at all, 1010 // e.g. evol_method. 1011 return nullptr; 1012 } 1013 1014 // ----------------- DependencySignature -------------------------------------- 1015 bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) { 1016 if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) { 1017 return false; 1018 } 1019 1020 for (int i = 0; i < s1.args_count(); i++) { 1021 if (s1.arg(i) != s2.arg(i)) { 1022 return false; 1023 } 1024 } 1025 return true; 1026 } 1027 1028 /// Checking dependencies 1029 1030 // This hierarchy walker inspects subtypes of a given type, trying to find a "bad" class which breaks a dependency. 1031 // Such a class is called a "witness" to the broken dependency. 1032 // While searching around, we ignore "participants", which are already known to the dependency. 1033 class AbstractClassHierarchyWalker { 1034 public: 1035 enum { PARTICIPANT_LIMIT = 3 }; 1036 1037 private: 1038 // if non-zero, tells how many witnesses to convert to participants 1039 uint _record_witnesses; 1040 1041 // special classes which are not allowed to be witnesses: 1042 Klass* _participants[PARTICIPANT_LIMIT+1]; 1043 uint _num_participants; 1044 1045 #ifdef ASSERT 1046 uint _nof_requests; // one-shot walker 1047 #endif // ASSERT 1048 1049 static PerfCounter* _perf_find_witness_anywhere_calls_count; 1050 static PerfCounter* _perf_find_witness_anywhere_steps_count; 1051 static PerfCounter* _perf_find_witness_in_calls_count; 1052 1053 protected: 1054 virtual Klass* find_witness_in(KlassDepChange& changes) = 0; 1055 virtual Klass* find_witness_anywhere(InstanceKlass* context_type) = 0; 1056 1057 AbstractClassHierarchyWalker(Klass* participant) : _record_witnesses(0), _num_participants(0) 1058 #ifdef ASSERT 1059 , _nof_requests(0) 1060 #endif // ASSERT 1061 { 1062 for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1063 _participants[i] = nullptr; 1064 } 1065 if (participant != nullptr) { 1066 add_participant(participant); 1067 } 1068 } 1069 1070 bool is_participant(Klass* k) { 1071 for (uint i = 0; i < _num_participants; i++) { 1072 if (_participants[i] == k) { 1073 return true; 1074 } 1075 } 1076 return false; 1077 } 1078 1079 bool record_witness(Klass* witness) { 1080 if (_record_witnesses > 0) { 1081 --_record_witnesses; 1082 add_participant(witness); 1083 return false; // not a witness 1084 } else { 1085 return true; // is a witness 1086 } 1087 } 1088 1089 class CountingClassHierarchyIterator : public ClassHierarchyIterator { 1090 private: 1091 jlong _nof_steps; 1092 public: 1093 CountingClassHierarchyIterator(InstanceKlass* root) : ClassHierarchyIterator(root), _nof_steps(0) {} 1094 1095 void next() { 1096 _nof_steps++; 1097 ClassHierarchyIterator::next(); 1098 } 1099 1100 ~CountingClassHierarchyIterator() { 1101 if (UsePerfData) { 1102 _perf_find_witness_anywhere_steps_count->inc(_nof_steps); 1103 } 1104 } 1105 }; 1106 1107 public: 1108 uint num_participants() { return _num_participants; } 1109 Klass* participant(uint n) { 1110 assert(n <= _num_participants, "oob"); 1111 if (n < _num_participants) { 1112 return _participants[n]; 1113 } else { 1114 return nullptr; 1115 } 1116 } 1117 1118 void add_participant(Klass* participant) { 1119 assert(!is_participant(participant), "sanity"); 1120 assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob"); 1121 uint np = _num_participants++; 1122 _participants[np] = participant; 1123 } 1124 1125 void record_witnesses(uint add) { 1126 if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT; 1127 assert(_num_participants + add < PARTICIPANT_LIMIT, "oob"); 1128 _record_witnesses = add; 1129 } 1130 1131 Klass* find_witness(InstanceKlass* context_type, KlassDepChange* changes = nullptr); 1132 1133 static void init(); 1134 static void print_statistics(); 1135 }; 1136 1137 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_calls_count = nullptr; 1138 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_steps_count = nullptr; 1139 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_in_calls_count = nullptr; 1140 1141 void AbstractClassHierarchyWalker::init() { 1142 if (UsePerfData) { 1143 EXCEPTION_MARK; 1144 _perf_find_witness_anywhere_calls_count = 1145 PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhere", PerfData::U_Events, CHECK); 1146 _perf_find_witness_anywhere_steps_count = 1147 PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhereSteps", PerfData::U_Events, CHECK); 1148 _perf_find_witness_in_calls_count = 1149 PerfDataManager::create_counter(SUN_CI, "findWitnessIn", PerfData::U_Events, CHECK); 1150 } 1151 } 1152 1153 Klass* AbstractClassHierarchyWalker::find_witness(InstanceKlass* context_type, KlassDepChange* changes) { 1154 // Current thread must be in VM (not native mode, as in CI): 1155 assert(must_be_in_vm(), "raw oops here"); 1156 // Must not move the class hierarchy during this check: 1157 assert_locked_or_safepoint(Compile_lock); 1158 assert(_nof_requests++ == 0, "repeated requests are not supported"); 1159 1160 assert(changes == nullptr || changes->involves_context(context_type), "irrelevant dependency"); 1161 1162 // (Note: Interfaces do not have subclasses.) 1163 // If it is an interface, search its direct implementors. 1164 // (Their subclasses are additional indirect implementors. See InstanceKlass::add_implementor().) 1165 if (context_type->is_interface()) { 1166 int nof_impls = context_type->nof_implementors(); 1167 if (nof_impls == 0) { 1168 return nullptr; // no implementors 1169 } else if (nof_impls == 1) { // unique implementor 1170 assert(context_type != context_type->implementor(), "not unique"); 1171 context_type = context_type->implementor(); 1172 } else { // nof_impls >= 2 1173 // Avoid this case: *I.m > { A.m, C }; B.m > C 1174 // Here, I.m has 2 concrete implementations, but m appears unique 1175 // as A.m, because the search misses B.m when checking C. 1176 // The inherited method B.m was getting missed by the walker 1177 // when interface 'I' was the starting point. 1178 // %%% Until this is fixed more systematically, bail out. 1179 return context_type; 1180 } 1181 } 1182 assert(!context_type->is_interface(), "no interfaces allowed"); 1183 1184 if (changes != nullptr) { 1185 if (UsePerfData) { 1186 _perf_find_witness_in_calls_count->inc(); 1187 } 1188 return find_witness_in(*changes); 1189 } else { 1190 if (UsePerfData) { 1191 _perf_find_witness_anywhere_calls_count->inc(); 1192 } 1193 return find_witness_anywhere(context_type); 1194 } 1195 } 1196 1197 class ConcreteSubtypeFinder : public AbstractClassHierarchyWalker { 1198 private: 1199 bool is_witness(Klass* k); 1200 1201 protected: 1202 virtual Klass* find_witness_in(KlassDepChange& changes); 1203 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1204 1205 public: 1206 ConcreteSubtypeFinder(Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) {} 1207 }; 1208 1209 bool ConcreteSubtypeFinder::is_witness(Klass* k) { 1210 if (Dependencies::is_concrete_klass(k)) { 1211 return record_witness(k); // concrete subtype 1212 } else { 1213 return false; // not a concrete class 1214 } 1215 } 1216 1217 Klass* ConcreteSubtypeFinder::find_witness_in(KlassDepChange& changes) { 1218 // When looking for unexpected concrete types, do not look beneath expected ones: 1219 // * CX > CC > C' is OK, even if C' is new. 1220 // * CX > { CC, C' } is not OK if C' is new, and C' is the witness. 1221 Klass* new_type = changes.as_new_klass_change()->new_type(); 1222 assert(!is_participant(new_type), "only old classes are participants"); 1223 // If the new type is a subtype of a participant, we are done. 1224 for (uint i = 0; i < num_participants(); i++) { 1225 if (changes.involves_context(participant(i))) { 1226 // new guy is protected from this check by previous participant 1227 return nullptr; 1228 } 1229 } 1230 if (is_witness(new_type)) { 1231 return new_type; 1232 } 1233 // No witness found. The dependency remains unbroken. 1234 return nullptr; 1235 } 1236 1237 Klass* ConcreteSubtypeFinder::find_witness_anywhere(InstanceKlass* context_type) { 1238 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1239 Klass* sub = iter.klass(); 1240 // Do not report participant types. 1241 if (is_participant(sub)) { 1242 // Don't walk beneath a participant since it hides witnesses. 1243 iter.skip_subclasses(); 1244 } else if (is_witness(sub)) { 1245 return sub; // found a witness 1246 } 1247 } 1248 // No witness found. The dependency remains unbroken. 1249 return nullptr; 1250 } 1251 1252 class ConcreteMethodFinder : public AbstractClassHierarchyWalker { 1253 private: 1254 Symbol* _name; 1255 Symbol* _signature; 1256 1257 // cache of method lookups 1258 Method* _found_methods[PARTICIPANT_LIMIT+1]; 1259 1260 bool is_witness(Klass* k); 1261 1262 protected: 1263 virtual Klass* find_witness_in(KlassDepChange& changes); 1264 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1265 1266 public: 1267 bool witnessed_reabstraction_in_supers(Klass* k); 1268 1269 ConcreteMethodFinder(Method* m, Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) { 1270 assert(m != nullptr && m->is_method(), "sanity"); 1271 _name = m->name(); 1272 _signature = m->signature(); 1273 1274 for (int i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1275 _found_methods[i] = nullptr; 1276 } 1277 } 1278 1279 // Note: If n==num_participants, returns nullptr. 1280 Method* found_method(uint n) { 1281 assert(n <= num_participants(), "oob"); 1282 Method* fm = _found_methods[n]; 1283 assert(n == num_participants() || fm != nullptr, "proper usage"); 1284 if (fm != nullptr && fm->method_holder() != participant(n)) { 1285 // Default methods from interfaces can be added to classes. In 1286 // that case the holder of the method is not the class but the 1287 // interface where it's defined. 1288 assert(fm->is_default_method(), "sanity"); 1289 return nullptr; 1290 } 1291 return fm; 1292 } 1293 1294 void add_participant(Klass* participant) { 1295 AbstractClassHierarchyWalker::add_participant(participant); 1296 _found_methods[num_participants()] = nullptr; 1297 } 1298 1299 bool record_witness(Klass* witness, Method* m) { 1300 _found_methods[num_participants()] = m; 1301 return AbstractClassHierarchyWalker::record_witness(witness); 1302 } 1303 1304 private: 1305 static PerfCounter* _perf_find_witness_anywhere_calls_count; 1306 static PerfCounter* _perf_find_witness_anywhere_steps_count; 1307 static PerfCounter* _perf_find_witness_in_calls_count; 1308 1309 public: 1310 static void init(); 1311 static void print_statistics(); 1312 }; 1313 1314 bool ConcreteMethodFinder::is_witness(Klass* k) { 1315 if (is_participant(k)) { 1316 return false; // do not report participant types 1317 } 1318 if (k->is_instance_klass()) { 1319 InstanceKlass* ik = InstanceKlass::cast(k); 1320 // Search class hierarchy first, skipping private implementations 1321 // as they never override any inherited methods 1322 Method* m = ik->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1323 if (Dependencies::is_concrete_method(m, ik)) { 1324 return record_witness(k, m); // concrete method found 1325 } else { 1326 // Check for re-abstraction of method 1327 if (!ik->is_interface() && m != nullptr && m->is_abstract()) { 1328 // Found a matching abstract method 'm' in the class hierarchy. 1329 // This is fine iff 'k' is an abstract class and all concrete subtypes 1330 // of 'k' override 'm' and are participates of the current search. 1331 ConcreteSubtypeFinder wf; 1332 for (uint i = 0; i < num_participants(); i++) { 1333 Klass* p = participant(i); 1334 wf.add_participant(p); 1335 } 1336 Klass* w = wf.find_witness(ik); 1337 if (w != nullptr) { 1338 Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1339 if (!Dependencies::is_concrete_method(wm, w)) { 1340 // Found a concrete subtype 'w' which does not override abstract method 'm'. 1341 // Bail out because 'm' could be called with 'w' as receiver (leading to an 1342 // AbstractMethodError) and thus the method we are looking for is not unique. 1343 return record_witness(k, m); 1344 } 1345 } 1346 } 1347 // Check interface defaults also, if any exist. 1348 Array<Method*>* default_methods = ik->default_methods(); 1349 if (default_methods != nullptr) { 1350 Method* dm = ik->find_method(default_methods, _name, _signature); 1351 if (Dependencies::is_concrete_method(dm, nullptr)) { 1352 return record_witness(k, dm); // default method found 1353 } 1354 } 1355 return false; // no concrete method found 1356 } 1357 } else { 1358 return false; // no methods to find in an array type 1359 } 1360 } 1361 1362 Klass* ConcreteMethodFinder::find_witness_in(KlassDepChange& changes) { 1363 // When looking for unexpected concrete methods, look beneath expected ones, to see if there are overrides. 1364 // * CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness. 1365 Klass* new_type = changes.as_new_klass_change()->new_type(); 1366 assert(!is_participant(new_type), "only old classes are participants"); 1367 if (is_witness(new_type)) { 1368 return new_type; 1369 } else { 1370 // No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking. 1371 if (witnessed_reabstraction_in_supers(new_type)) { 1372 return new_type; 1373 } 1374 } 1375 // No witness found. The dependency remains unbroken. 1376 return nullptr; 1377 } 1378 1379 bool ConcreteMethodFinder::witnessed_reabstraction_in_supers(Klass* k) { 1380 if (!k->is_instance_klass()) { 1381 return false; // no methods to find in an array type 1382 } else { 1383 // Looking for a case when an abstract method is inherited into a concrete class. 1384 if (Dependencies::is_concrete_klass(k) && !k->is_interface()) { 1385 Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1386 if (m != nullptr) { 1387 return false; // no reabstraction possible: local method found 1388 } 1389 for (InstanceKlass* super = k->java_super(); super != nullptr; super = super->java_super()) { 1390 m = super->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1391 if (m != nullptr) { // inherited method found 1392 if (m->is_abstract() || m->is_overpass()) { 1393 return record_witness(super, m); // abstract method found 1394 } 1395 return false; 1396 } 1397 } 1398 // Miranda. 1399 return true; 1400 } 1401 return false; 1402 } 1403 } 1404 1405 1406 Klass* ConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) { 1407 // Walk hierarchy under a context type, looking for unexpected types. 1408 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1409 Klass* sub = iter.klass(); 1410 if (is_witness(sub)) { 1411 return sub; // found a witness 1412 } 1413 } 1414 // No witness found. The dependency remains unbroken. 1415 return nullptr; 1416 } 1417 1418 // For some method m and some class ctxk (subclass of method holder), 1419 // enumerate all distinct overrides of m in concrete subclasses of ctxk. 1420 // It relies on vtable/itable information to perform method selection on each linked subclass 1421 // and ignores all non yet linked ones (speculatively treat them as "effectively abstract"). 1422 class LinkedConcreteMethodFinder : public AbstractClassHierarchyWalker { 1423 private: 1424 InstanceKlass* _resolved_klass; // resolved class (JVMS-5.4.3.1) 1425 InstanceKlass* _declaring_klass; // the holder of resolved method (JVMS-5.4.3.3) 1426 int _vtable_index; // vtable/itable index of the resolved method 1427 bool _do_itable_lookup; // choose between itable and vtable lookup logic 1428 1429 // cache of method lookups 1430 Method* _found_methods[PARTICIPANT_LIMIT+1]; 1431 1432 bool is_witness(Klass* k); 1433 Method* select_method(InstanceKlass* recv_klass); 1434 static int compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, bool& is_itable_index); 1435 static bool is_concrete_klass(InstanceKlass* ik); 1436 1437 void add_participant(Method* m, Klass* participant) { 1438 uint np = num_participants(); 1439 AbstractClassHierarchyWalker::add_participant(participant); 1440 assert(np + 1 == num_participants(), "sanity"); 1441 _found_methods[np] = m; // record the method for the participant 1442 } 1443 1444 bool record_witness(Klass* witness, Method* m) { 1445 for (uint i = 0; i < num_participants(); i++) { 1446 if (found_method(i) == m) { 1447 return false; // already recorded 1448 } 1449 } 1450 // Record not yet seen method. 1451 _found_methods[num_participants()] = m; 1452 return AbstractClassHierarchyWalker::record_witness(witness); 1453 } 1454 1455 void initialize(Method* participant) { 1456 for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1457 _found_methods[i] = nullptr; 1458 } 1459 if (participant != nullptr) { 1460 add_participant(participant, participant->method_holder()); 1461 } 1462 } 1463 1464 protected: 1465 virtual Klass* find_witness_in(KlassDepChange& changes); 1466 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1467 1468 public: 1469 // In order to perform method selection, the following info is needed: 1470 // (1) interface or virtual call; 1471 // (2) vtable/itable index; 1472 // (3) declaring class (in case of interface call). 1473 // 1474 // It is prepared based on the results of method resolution: resolved class and resolved method (as specified in JVMS-5.4.3.3). 1475 // Optionally, a method which was previously determined as a unique target (uniqm) is added as a participant 1476 // to enable dependency spot-checking and speed up the search. 1477 LinkedConcreteMethodFinder(InstanceKlass* resolved_klass, Method* resolved_method, Method* uniqm = nullptr) : AbstractClassHierarchyWalker(nullptr) { 1478 assert(UseVtableBasedCHA, "required"); 1479 assert(resolved_klass->is_linked(), "required"); 1480 assert(resolved_method->method_holder()->is_linked(), "required"); 1481 assert(!resolved_method->can_be_statically_bound(), "no vtable index available"); 1482 1483 _resolved_klass = resolved_klass; 1484 _declaring_klass = resolved_method->method_holder(); 1485 _vtable_index = compute_vtable_index(resolved_klass, resolved_method, 1486 _do_itable_lookup); // out parameter 1487 assert(_vtable_index >= 0, "invalid vtable index"); 1488 1489 initialize(uniqm); 1490 } 1491 1492 // Note: If n==num_participants, returns nullptr. 1493 Method* found_method(uint n) { 1494 assert(n <= num_participants(), "oob"); 1495 assert(participant(n) != nullptr || n == num_participants(), "proper usage"); 1496 return _found_methods[n]; 1497 } 1498 }; 1499 1500 Klass* LinkedConcreteMethodFinder::find_witness_in(KlassDepChange& changes) { 1501 Klass* type = changes.type(); 1502 1503 assert(!is_participant(type), "only old classes are participants"); 1504 1505 if (is_witness(type)) { 1506 return type; 1507 } 1508 return nullptr; // No witness found. The dependency remains unbroken. 1509 } 1510 1511 Klass* LinkedConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) { 1512 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1513 Klass* sub = iter.klass(); 1514 if (is_witness(sub)) { 1515 return sub; 1516 } 1517 if (sub->is_instance_klass() && !InstanceKlass::cast(sub)->is_linked()) { 1518 iter.skip_subclasses(); // ignore not yet linked classes 1519 } 1520 } 1521 return nullptr; // No witness found. The dependency remains unbroken. 1522 } 1523 1524 bool LinkedConcreteMethodFinder::is_witness(Klass* k) { 1525 if (is_participant(k)) { 1526 return false; // do not report participant types 1527 } else if (k->is_instance_klass()) { 1528 InstanceKlass* ik = InstanceKlass::cast(k); 1529 if (is_concrete_klass(ik)) { 1530 Method* m = select_method(ik); 1531 return record_witness(ik, m); 1532 } else { 1533 return false; // ignore non-concrete holder class 1534 } 1535 } else { 1536 return false; // no methods to find in an array type 1537 } 1538 } 1539 1540 Method* LinkedConcreteMethodFinder::select_method(InstanceKlass* recv_klass) { 1541 Method* selected_method = nullptr; 1542 if (_do_itable_lookup) { 1543 assert(_declaring_klass->is_interface(), "sanity"); 1544 bool implements_interface; // initialized by method_at_itable_or_null() 1545 selected_method = recv_klass->method_at_itable_or_null(_declaring_klass, _vtable_index, 1546 implements_interface); // out parameter 1547 assert(implements_interface, "not implemented"); 1548 } else { 1549 selected_method = recv_klass->method_at_vtable(_vtable_index); 1550 } 1551 return selected_method; // nullptr when corresponding slot is empty (AbstractMethodError case) 1552 } 1553 1554 int LinkedConcreteMethodFinder::compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, 1555 // out parameter 1556 bool& is_itable_index) { 1557 if (resolved_klass->is_interface() && resolved_method->has_itable_index()) { 1558 is_itable_index = true; 1559 return resolved_method->itable_index(); 1560 } 1561 // Check for default or miranda method first. 1562 InstanceKlass* declaring_klass = resolved_method->method_holder(); 1563 if (!resolved_klass->is_interface() && declaring_klass->is_interface()) { 1564 is_itable_index = false; 1565 return resolved_klass->vtable_index_of_interface_method(resolved_method); 1566 } 1567 // At this point we are sure that resolved_method is virtual and not 1568 // a default or miranda method; therefore, it must have a valid vtable index. 1569 assert(resolved_method->has_vtable_index(), ""); 1570 is_itable_index = false; 1571 return resolved_method->vtable_index(); 1572 } 1573 1574 bool LinkedConcreteMethodFinder::is_concrete_klass(InstanceKlass* ik) { 1575 if (!Dependencies::is_concrete_klass(ik)) { 1576 return false; // not concrete 1577 } 1578 if (ik->is_interface()) { 1579 return false; // interfaces aren't concrete 1580 } 1581 if (!ik->is_linked()) { 1582 return false; // not yet linked classes don't have instances 1583 } 1584 return true; 1585 } 1586 1587 #ifdef ASSERT 1588 // Assert that m is inherited into ctxk, without intervening overrides. 1589 // (May return true even if this is not true, in corner cases where we punt.) 1590 bool Dependencies::verify_method_context(InstanceKlass* ctxk, Method* m) { 1591 if (m->is_private()) { 1592 return false; // Quick lose. Should not happen. 1593 } 1594 if (m->method_holder() == ctxk) { 1595 return true; // Quick win. 1596 } 1597 if (!(m->is_public() || m->is_protected())) { 1598 // The override story is complex when packages get involved. 1599 return true; // Must punt the assertion to true. 1600 } 1601 Method* lm = ctxk->lookup_method(m->name(), m->signature()); 1602 if (lm == nullptr) { 1603 // It might be an interface method 1604 lm = ctxk->lookup_method_in_ordered_interfaces(m->name(), m->signature()); 1605 } 1606 if (lm == m) { 1607 // Method m is inherited into ctxk. 1608 return true; 1609 } 1610 if (lm != nullptr) { 1611 if (!(lm->is_public() || lm->is_protected())) { 1612 // Method is [package-]private, so the override story is complex. 1613 return true; // Must punt the assertion to true. 1614 } 1615 if (lm->is_static()) { 1616 // Static methods don't override non-static so punt 1617 return true; 1618 } 1619 if (!Dependencies::is_concrete_method(lm, ctxk) && 1620 !Dependencies::is_concrete_method(m, ctxk)) { 1621 // They are both non-concrete 1622 if (lm->method_holder()->is_subtype_of(m->method_holder())) { 1623 // Method m is overridden by lm, but both are non-concrete. 1624 return true; 1625 } 1626 if (lm->method_holder()->is_interface() && m->method_holder()->is_interface() && 1627 ctxk->is_subtype_of(m->method_holder()) && ctxk->is_subtype_of(lm->method_holder())) { 1628 // Interface method defined in multiple super interfaces 1629 return true; 1630 } 1631 } 1632 } 1633 ResourceMark rm; 1634 tty->print_cr("Dependency method not found in the associated context:"); 1635 tty->print_cr(" context = %s", ctxk->external_name()); 1636 tty->print( " method = "); m->print_short_name(tty); tty->cr(); 1637 if (lm != nullptr) { 1638 tty->print( " found = "); lm->print_short_name(tty); tty->cr(); 1639 } 1640 return false; 1641 } 1642 #endif // ASSERT 1643 1644 bool Dependencies::is_concrete_klass(Klass* k) { 1645 if (k->is_abstract()) return false; 1646 // %%% We could treat classes which are concrete but 1647 // have not yet been instantiated as virtually abstract. 1648 // This would require a deoptimization barrier on first instantiation. 1649 //if (k->is_not_instantiated()) return false; 1650 return true; 1651 } 1652 1653 bool Dependencies::is_concrete_method(Method* m, Klass* k) { 1654 // nullptr is not a concrete method. 1655 if (m == nullptr) { 1656 return false; 1657 } 1658 // Statics are irrelevant to virtual call sites. 1659 if (m->is_static()) { 1660 return false; 1661 } 1662 // Abstract methods are not concrete. 1663 if (m->is_abstract()) { 1664 return false; 1665 } 1666 // Overpass (error) methods are not concrete if k is abstract. 1667 if (m->is_overpass() && k != nullptr) { 1668 return !k->is_abstract(); 1669 } 1670 // Note "true" is conservative answer: overpass clause is false if k == nullptr, 1671 // implies return true if answer depends on overpass clause. 1672 return true; 1673 } 1674 1675 Klass* Dependencies::find_finalizable_subclass(InstanceKlass* ik) { 1676 for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) { 1677 Klass* sub = iter.klass(); 1678 if (sub->has_finalizer() && !sub->is_interface()) { 1679 return sub; 1680 } 1681 } 1682 return nullptr; // not found 1683 } 1684 1685 bool Dependencies::is_concrete_klass(ciInstanceKlass* k) { 1686 if (k->is_abstract()) return false; 1687 // We could also return false if k does not yet appear to be 1688 // instantiated, if the VM version supports this distinction also. 1689 //if (k->is_not_instantiated()) return false; 1690 return true; 1691 } 1692 1693 bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) { 1694 return k->has_finalizable_subclass(); 1695 } 1696 1697 // Any use of the contents (bytecodes) of a method must be 1698 // marked by an "evol_method" dependency, if those contents 1699 // can change. (Note: A method is always dependent on itself.) 1700 Klass* Dependencies::check_evol_method(Method* m) { 1701 assert(must_be_in_vm(), "raw oops here"); 1702 // Did somebody do a JVMTI RedefineClasses while our backs were turned? 1703 // Or is there a now a breakpoint? 1704 // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.) 1705 if (m->is_old() 1706 || m->number_of_breakpoints() > 0 1707 || m->mismatch()) { 1708 return m->method_holder(); 1709 } else { 1710 return nullptr; 1711 } 1712 } 1713 1714 // This is a strong assertion: It is that the given type 1715 // has no subtypes whatever. It is most useful for 1716 // optimizing checks on reflected types or on array types. 1717 // (Checks on types which are derived from real instances 1718 // can be optimized more strongly than this, because we 1719 // know that the checked type comes from a concrete type, 1720 // and therefore we can disregard abstract types.) 1721 Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) { 1722 assert(must_be_in_vm(), "raw oops here"); 1723 assert_locked_or_safepoint(Compile_lock); 1724 Klass* sub = ctxk->subklass(); 1725 if (sub != nullptr) { 1726 return sub; 1727 } else if (ctxk->nof_implementors() != 0) { 1728 // if it is an interface, it must be unimplemented 1729 // (if it is not an interface, nof_implementors is always zero) 1730 InstanceKlass* impl = ctxk->implementor(); 1731 assert(impl != nullptr, "must be set"); 1732 return impl; 1733 } else { 1734 return nullptr; 1735 } 1736 } 1737 1738 // Test the assertion that conck is the only concrete subtype* of ctxk. 1739 // The type conck itself is allowed to have have further concrete subtypes. 1740 // This allows the compiler to narrow occurrences of ctxk by conck, 1741 // when dealing with the types of actual instances. 1742 Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, 1743 Klass* conck, 1744 NewKlassDepChange* changes) { 1745 ConcreteSubtypeFinder wf(conck); 1746 Klass* k = wf.find_witness(ctxk, changes); 1747 return k; 1748 } 1749 1750 1751 // Find the unique concrete proper subtype of ctxk, or nullptr if there 1752 // is more than one concrete proper subtype. If there are no concrete 1753 // proper subtypes, return ctxk itself, whether it is concrete or not. 1754 // The returned subtype is allowed to have have further concrete subtypes. 1755 // That is, return CC1 for CX > CC1 > CC2, but nullptr for CX > { CC1, CC2 }. 1756 Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) { 1757 ConcreteSubtypeFinder wf(ctxk); // Ignore ctxk when walking. 1758 wf.record_witnesses(1); // Record one other witness when walking. 1759 Klass* wit = wf.find_witness(ctxk); 1760 if (wit != nullptr) return nullptr; // Too many witnesses. 1761 Klass* conck = wf.participant(0); 1762 if (conck == nullptr) { 1763 return ctxk; // Return ctxk as a flag for "no subtypes". 1764 } else { 1765 #ifndef PRODUCT 1766 // Make sure the dependency mechanism will pass this discovery: 1767 if (VerifyDependencies) { 1768 // Turn off dependency tracing while actually testing deps. 1769 FlagSetting fs(_verify_in_progress, true); 1770 if (!Dependencies::is_concrete_klass(ctxk)) { 1771 guarantee(nullptr == (void *) 1772 check_abstract_with_unique_concrete_subtype(ctxk, conck), 1773 "verify dep."); 1774 } 1775 } 1776 #endif //PRODUCT 1777 return conck; 1778 } 1779 } 1780 1781 // Try to determine whether root method in some context is concrete or not based on the information about the unique method 1782 // in that context. It exploits the fact that concrete root method is always inherited into the context when there's a unique method. 1783 // Hence, unique method holder is always a supertype of the context class when root method is concrete. 1784 // Examples for concrete_root_method 1785 // C (C.m uniqm) 1786 // | 1787 // CX (ctxk) uniqm is inherited into context. 1788 // 1789 // CX (ctxk) (CX.m uniqm) here uniqm is defined in ctxk. 1790 // Examples for !concrete_root_method 1791 // CX (ctxk) 1792 // | 1793 // C (C.m uniqm) uniqm is in subtype of ctxk. 1794 bool Dependencies::is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk) { 1795 if (uniqm == nullptr) { 1796 return false; // match Dependencies::is_concrete_method() behavior 1797 } 1798 // Theoretically, the "direction" of subtype check matters here. 1799 // On one hand, in case of interface context with a single implementor, uniqm can be in a superclass of the implementor which 1800 // is not related to context class. 1801 // On another hand, uniqm could come from an interface unrelated to the context class, but right now it is not possible: 1802 // it is required that uniqm->method_holder() is the participant (uniqm->method_holder() <: ctxk), hence a default method 1803 // can't be used as unique. 1804 if (ctxk->is_interface()) { 1805 InstanceKlass* implementor = ctxk->implementor(); 1806 assert(implementor != ctxk, "single implementor only"); // should have been invalidated earlier 1807 ctxk = implementor; 1808 } 1809 InstanceKlass* holder = uniqm->method_holder(); 1810 assert(!holder->is_interface(), "no default methods allowed"); 1811 assert(ctxk->is_subclass_of(holder) || holder->is_subclass_of(ctxk), "not related"); 1812 return ctxk->is_subclass_of(holder); 1813 } 1814 1815 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1816 // Otherwise, return a class that contains an interfering method. 1817 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1818 Method* uniqm, 1819 NewKlassDepChange* changes) { 1820 ConcreteMethodFinder wf(uniqm, uniqm->method_holder()); 1821 Klass* k = wf.find_witness(ctxk, changes); 1822 if (k != nullptr) { 1823 return k; 1824 } 1825 if (!Dependencies::is_concrete_root_method(uniqm, ctxk) || changes != nullptr) { 1826 Klass* conck = find_witness_AME(ctxk, uniqm, changes); 1827 if (conck != nullptr) { 1828 // Found a concrete subtype 'conck' which does not override abstract root method. 1829 return conck; 1830 } 1831 } 1832 return nullptr; 1833 } 1834 1835 Klass* Dependencies::check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes) { 1836 assert(ctxk->is_interface(), "sanity"); 1837 assert(ctxk->nof_implementors() > 0, "no implementors"); 1838 if (ctxk->nof_implementors() == 1) { 1839 assert(ctxk->implementor() == uniqk, "sanity"); 1840 return nullptr; 1841 } 1842 return ctxk; // no unique implementor 1843 } 1844 1845 // Search for AME. 1846 // There are two version of checks. 1847 // 1) Spot checking version(Classload time). Newly added class is checked for AME. 1848 // Checks whether abstract/overpass method is inherited into/declared in newly added concrete class. 1849 // 2) Compile time analysis for abstract/overpass(abstract klass) root_m. The non uniqm subtrees are checked for concrete classes. 1850 Klass* Dependencies::find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes) { 1851 if (m != nullptr) { 1852 if (changes != nullptr) { 1853 // Spot checking version. 1854 ConcreteMethodFinder wf(m); 1855 Klass* new_type = changes->as_new_klass_change()->new_type(); 1856 if (wf.witnessed_reabstraction_in_supers(new_type)) { 1857 return new_type; 1858 } 1859 } else { 1860 // Note: It is required that uniqm->method_holder() is the participant (see ClassHierarchyWalker::found_method()). 1861 ConcreteSubtypeFinder wf(m->method_holder()); 1862 Klass* conck = wf.find_witness(ctxk); 1863 if (conck != nullptr) { 1864 Method* cm = InstanceKlass::cast(conck)->find_instance_method(m->name(), m->signature(), Klass::PrivateLookupMode::skip); 1865 if (!Dependencies::is_concrete_method(cm, conck)) { 1866 return conck; 1867 } 1868 } 1869 } 1870 } 1871 return nullptr; 1872 } 1873 1874 // This function is used by find_unique_concrete_method(non vtable based) 1875 // to check whether subtype method overrides the base method. 1876 static bool overrides(Method* sub_m, Method* base_m) { 1877 assert(base_m != nullptr, "base method should be non null"); 1878 if (sub_m == nullptr) { 1879 return false; 1880 } 1881 /** 1882 * If base_m is public or protected then sub_m always overrides. 1883 * If base_m is !public, !protected and !private (i.e. base_m is package private) 1884 * then sub_m should be in the same package as that of base_m. 1885 * For package private base_m this is conservative approach as it allows only subset of all allowed cases in 1886 * the jvm specification. 1887 **/ 1888 if (base_m->is_public() || base_m->is_protected() || 1889 base_m->method_holder()->is_same_class_package(sub_m->method_holder())) { 1890 return true; 1891 } 1892 return false; 1893 } 1894 1895 // Find the set of all non-abstract methods under ctxk that match m. 1896 // (The method m must be defined or inherited in ctxk.) 1897 // Include m itself in the set, unless it is abstract. 1898 // If this set has exactly one element, return that element. 1899 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) { 1900 // Return nullptr if m is marked old; must have been a redefined method. 1901 if (m->is_old()) { 1902 return nullptr; 1903 } 1904 if (m->is_default_method()) { 1905 return nullptr; // not supported 1906 } 1907 assert(verify_method_context(ctxk, m), "proper context"); 1908 ConcreteMethodFinder wf(m); 1909 wf.record_witnesses(1); 1910 Klass* wit = wf.find_witness(ctxk); 1911 if (wit != nullptr) return nullptr; // Too many witnesses. 1912 Method* fm = wf.found_method(0); // Will be nullptr if num_parts == 0. 1913 if (participant != nullptr) { 1914 (*participant) = wf.participant(0); 1915 } 1916 if (!Dependencies::is_concrete_method(fm, nullptr)) { 1917 fm = nullptr; // ignore abstract methods 1918 } 1919 if (Dependencies::is_concrete_method(m, ctxk)) { 1920 if (fm == nullptr) { 1921 // It turns out that m was always the only implementation. 1922 fm = m; 1923 } else if (fm != m) { 1924 // Two conflicting implementations after all. 1925 // (This can happen if m is inherited into ctxk and fm overrides it.) 1926 return nullptr; 1927 } 1928 } else if (Dependencies::find_witness_AME(ctxk, fm) != nullptr) { 1929 // Found a concrete subtype which does not override abstract root method. 1930 return nullptr; 1931 } else if (!overrides(fm, m)) { 1932 // Found method doesn't override abstract root method. 1933 return nullptr; 1934 } 1935 assert(Dependencies::is_concrete_root_method(fm, ctxk) == Dependencies::is_concrete_method(m, ctxk), "mismatch"); 1936 #ifndef PRODUCT 1937 // Make sure the dependency mechanism will pass this discovery: 1938 if (VerifyDependencies && fm != nullptr) { 1939 guarantee(nullptr == (void *)check_unique_concrete_method(ctxk, fm), 1940 "verify dep."); 1941 } 1942 #endif //PRODUCT 1943 return fm; 1944 } 1945 1946 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1947 // Otherwise, return a class that contains an interfering method. 1948 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1949 Method* uniqm, 1950 Klass* resolved_klass, 1951 Method* resolved_method, 1952 KlassDepChange* changes) { 1953 assert(UseVtableBasedCHA, "required"); 1954 assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity"); 1955 assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity"); 1956 assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity"); 1957 1958 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1959 !resolved_method->method_holder()->is_linked() || 1960 resolved_method->can_be_statically_bound()) { 1961 // Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation. 1962 return nullptr; // no vtable index available 1963 } 1964 1965 LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm); 1966 return mf.find_witness(ctxk, changes); 1967 } 1968 1969 // Find the set of all non-abstract methods under ctxk that match m. 1970 // (The method m must be defined or inherited in ctxk.) 1971 // Include m itself in the set, unless it is abstract. 1972 // If this set has exactly one element, return that element. 1973 // Not yet linked subclasses of ctxk are ignored since they don't have any instances yet. 1974 // Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed. 1975 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) { 1976 // Return nullptr if m is marked old; must have been a redefined method. 1977 if (m->is_old()) { 1978 return nullptr; 1979 } 1980 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1981 !resolved_method->method_holder()->is_linked() || 1982 resolved_method->can_be_statically_bound()) { 1983 return m; // nothing to do: no witness under ctxk 1984 } 1985 LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method); 1986 assert(Dependencies::verify_method_context(ctxk, m), "proper context"); 1987 wf.record_witnesses(1); 1988 Klass* wit = wf.find_witness(ctxk); 1989 if (wit != nullptr) { 1990 return nullptr; // Too many witnesses. 1991 } 1992 // p == nullptr when no participants are found (wf.num_participants() == 0). 1993 // fm == nullptr case has 2 meanings: 1994 // * when p == nullptr: no method found; 1995 // * when p != nullptr: AbstractMethodError-throwing method found. 1996 // Also, found method should always be accompanied by a participant class. 1997 Klass* p = wf.participant(0); 1998 Method* fm = wf.found_method(0); 1999 assert(fm == nullptr || p != nullptr, "no participant"); 2000 // Normalize all error-throwing cases to nullptr. 2001 if (fm == Universe::throw_illegal_access_error() || 2002 fm == Universe::throw_no_such_method_error() || 2003 !Dependencies::is_concrete_method(fm, p)) { 2004 fm = nullptr; // error-throwing method 2005 } 2006 if (Dependencies::is_concrete_method(m, ctxk)) { 2007 if (p == nullptr) { 2008 // It turns out that m was always the only implementation. 2009 assert(fm == nullptr, "sanity"); 2010 fm = m; 2011 } 2012 } 2013 #ifndef PRODUCT 2014 // Make sure the dependency mechanism will pass this discovery: 2015 if (VerifyDependencies && fm != nullptr) { 2016 guarantee(nullptr == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method), 2017 "verify dep."); 2018 } 2019 #endif // PRODUCT 2020 assert(fm == nullptr || !fm->is_abstract(), "sanity"); 2021 // Old CHA conservatively reports concrete methods in abstract classes 2022 // irrespective of whether they have concrete subclasses or not. 2023 // Also, abstract root method case is not fully supported. 2024 #ifdef ASSERT 2025 Klass* uniqp = nullptr; 2026 Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp); 2027 assert(uniqm == nullptr || uniqm == fm || 2028 m->is_abstract() || 2029 uniqm->method_holder()->is_abstract() || 2030 (fm == nullptr && uniqm != nullptr && uniqp != nullptr && !InstanceKlass::cast(uniqp)->is_linked()), 2031 "sanity"); 2032 #endif // ASSERT 2033 return fm; 2034 } 2035 2036 Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) { 2037 InstanceKlass* search_at = ctxk; 2038 if (changes != nullptr) { 2039 search_at = changes->new_type(); // just look at the new bit 2040 } 2041 return find_finalizable_subclass(search_at); 2042 } 2043 2044 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) { 2045 assert(call_site != nullptr, "sanity"); 2046 assert(method_handle != nullptr, "sanity"); 2047 assert(call_site->is_a(vmClasses::CallSite_klass()), "sanity"); 2048 2049 if (changes == nullptr) { 2050 // Validate all CallSites 2051 if (java_lang_invoke_CallSite::target(call_site) != method_handle) 2052 return call_site->klass(); // assertion failed 2053 } else { 2054 // Validate the given CallSite 2055 if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) { 2056 assert(method_handle != changes->method_handle(), "must be"); 2057 return call_site->klass(); // assertion failed 2058 } 2059 } 2060 return nullptr; // assertion still valid 2061 } 2062 2063 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) { 2064 if (_verify_in_progress) return; // don't log 2065 if (witness != nullptr) { 2066 LogTarget(Debug, dependencies) lt; 2067 if (lt.is_enabled()) { 2068 LogStream ls(<); 2069 print_dependency(&ls, witness, /*verbose=*/ true); 2070 } 2071 // The following is a no-op unless logging is enabled: 2072 log_dependency(witness); 2073 } 2074 } 2075 2076 Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) { 2077 assert_locked_or_safepoint(Compile_lock); 2078 Dependencies::check_valid_dependency_type(type()); 2079 2080 Klass* witness = nullptr; 2081 switch (type()) { 2082 case evol_method: 2083 witness = check_evol_method(method_argument(0)); 2084 break; 2085 case leaf_type: 2086 witness = check_leaf_type(context_type()); 2087 break; 2088 case abstract_with_unique_concrete_subtype: 2089 witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes); 2090 break; 2091 case unique_concrete_method_2: 2092 witness = check_unique_concrete_method(context_type(), method_argument(1), changes); 2093 break; 2094 case unique_concrete_method_4: 2095 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2096 break; 2097 case unique_implementor: 2098 witness = check_unique_implementor(context_type(), type_argument(1), changes); 2099 break; 2100 case no_finalizable_subclasses: 2101 witness = check_has_no_finalizable_subclasses(context_type(), changes); 2102 break; 2103 default: 2104 witness = nullptr; 2105 break; 2106 } 2107 trace_and_log_witness(witness); 2108 return witness; 2109 } 2110 2111 Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) { 2112 assert_locked_or_safepoint(Compile_lock); 2113 Dependencies::check_valid_dependency_type(type()); 2114 2115 // No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes. 2116 Klass* witness = nullptr; 2117 switch (type()) { 2118 case unique_concrete_method_4: 2119 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2120 break; 2121 default: 2122 witness = nullptr; 2123 break; 2124 } 2125 trace_and_log_witness(witness); 2126 return witness; 2127 } 2128 2129 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) { 2130 assert_locked_or_safepoint(Compile_lock); 2131 Dependencies::check_valid_dependency_type(type()); 2132 2133 if (changes != nullptr) { 2134 if (UseVtableBasedCHA && changes->is_klass_init_change()) { 2135 return check_klass_init_dependency(changes->as_klass_init_change()); 2136 } else { 2137 return check_new_klass_dependency(changes->as_new_klass_change()); 2138 } 2139 } else { 2140 Klass* witness = check_new_klass_dependency(nullptr); 2141 // check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent. 2142 assert(witness != nullptr || check_klass_init_dependency(nullptr) == nullptr, "missed dependency"); 2143 return witness; 2144 } 2145 } 2146 2147 Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) { 2148 assert_locked_or_safepoint(Compile_lock); 2149 Dependencies::check_valid_dependency_type(type()); 2150 2151 Klass* witness = nullptr; 2152 switch (type()) { 2153 case call_site_target_value: 2154 witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes); 2155 break; 2156 default: 2157 witness = nullptr; 2158 break; 2159 } 2160 trace_and_log_witness(witness); 2161 return witness; 2162 } 2163 2164 2165 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) { 2166 // Handle klass dependency 2167 if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type())) 2168 return check_klass_dependency(changes.as_klass_change()); 2169 2170 // Handle CallSite dependency 2171 if (changes.is_call_site_change()) 2172 return check_call_site_dependency(changes.as_call_site_change()); 2173 2174 // irrelevant dependency; skip it 2175 return nullptr; 2176 } 2177 2178 2179 void DepChange::print() { print_on(tty); } 2180 2181 void DepChange::print_on(outputStream* st) { 2182 int nsup = 0, nint = 0; 2183 for (ContextStream str(*this); str.next(); ) { 2184 InstanceKlass* k = str.klass(); 2185 switch (str.change_type()) { 2186 case Change_new_type: 2187 st->print_cr(" dependee = %s", k->external_name()); 2188 break; 2189 case Change_new_sub: 2190 if (!WizardMode) { 2191 ++nsup; 2192 } else { 2193 st->print_cr(" context super = %s", k->external_name()); 2194 } 2195 break; 2196 case Change_new_impl: 2197 if (!WizardMode) { 2198 ++nint; 2199 } else { 2200 st->print_cr(" context interface = %s", k->external_name()); 2201 } 2202 break; 2203 default: 2204 break; 2205 } 2206 } 2207 if (nsup + nint != 0) { 2208 st->print_cr(" context supers = %d, interfaces = %d", nsup, nint); 2209 } 2210 } 2211 2212 void DepChange::ContextStream::start() { 2213 InstanceKlass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (InstanceKlass*) nullptr); 2214 _change_type = (type == nullptr ? NO_CHANGE : Start_Klass); 2215 _klass = type; 2216 _ti_base = nullptr; 2217 _ti_index = 0; 2218 _ti_limit = 0; 2219 } 2220 2221 bool DepChange::ContextStream::next() { 2222 switch (_change_type) { 2223 case Start_Klass: // initial state; _klass is the new type 2224 _ti_base = _klass->transitive_interfaces(); 2225 _ti_index = 0; 2226 _change_type = Change_new_type; 2227 return true; 2228 case Change_new_type: 2229 // fall through: 2230 _change_type = Change_new_sub; 2231 case Change_new_sub: 2232 // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277 2233 { 2234 _klass = _klass->java_super(); 2235 if (_klass != nullptr) { 2236 return true; 2237 } 2238 } 2239 // else set up _ti_limit and fall through: 2240 _ti_limit = (_ti_base == nullptr) ? 0 : _ti_base->length(); 2241 _change_type = Change_new_impl; 2242 case Change_new_impl: 2243 if (_ti_index < _ti_limit) { 2244 _klass = _ti_base->at(_ti_index++); 2245 return true; 2246 } 2247 // fall through: 2248 _change_type = NO_CHANGE; // iterator is exhausted 2249 case NO_CHANGE: 2250 break; 2251 default: 2252 ShouldNotReachHere(); 2253 } 2254 return false; 2255 } 2256 2257 void KlassDepChange::initialize() { 2258 // entire transaction must be under this lock: 2259 assert_lock_strong(Compile_lock); 2260 2261 // Mark all dependee and all its superclasses 2262 // Mark transitive interfaces 2263 for (ContextStream str(*this); str.next(); ) { 2264 InstanceKlass* d = str.klass(); 2265 assert(!d->is_marked_dependent(), "checking"); 2266 d->set_is_marked_dependent(true); 2267 } 2268 } 2269 2270 KlassDepChange::~KlassDepChange() { 2271 // Unmark all dependee and all its superclasses 2272 // Unmark transitive interfaces 2273 for (ContextStream str(*this); str.next(); ) { 2274 InstanceKlass* d = str.klass(); 2275 d->set_is_marked_dependent(false); 2276 } 2277 } 2278 2279 bool KlassDepChange::involves_context(Klass* k) { 2280 if (k == nullptr || !k->is_instance_klass()) { 2281 return false; 2282 } 2283 InstanceKlass* ik = InstanceKlass::cast(k); 2284 bool is_contained = ik->is_marked_dependent(); 2285 assert(is_contained == type()->is_subtype_of(k), 2286 "correct marking of potential context types"); 2287 return is_contained; 2288 } 2289 2290 void Dependencies::print_statistics() { 2291 AbstractClassHierarchyWalker::print_statistics(); 2292 } 2293 2294 void AbstractClassHierarchyWalker::print_statistics() { 2295 if (UsePerfData) { 2296 jlong deps_find_witness_calls = _perf_find_witness_anywhere_calls_count->get_value(); 2297 jlong deps_find_witness_steps = _perf_find_witness_anywhere_steps_count->get_value(); 2298 jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value(); 2299 2300 ttyLocker ttyl; 2301 tty->print_cr("Dependency check (find_witness) " 2302 "calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT, 2303 deps_find_witness_calls, 2304 deps_find_witness_steps, 2305 (double)deps_find_witness_steps / deps_find_witness_calls, 2306 deps_find_witness_singles); 2307 if (xtty != nullptr) { 2308 xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'", 2309 deps_find_witness_calls, 2310 deps_find_witness_steps, 2311 deps_find_witness_singles); 2312 } 2313 } 2314 } 2315 2316 CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) : 2317 _call_site(call_site), 2318 _method_handle(method_handle) { 2319 assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be"); 2320 assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be"); 2321 } 2322 2323 void dependencies_init() { 2324 AbstractClassHierarchyWalker::init(); 2325 }