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