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