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_cr(" witness = %s%s", 834 (put_star? "*": ""), 835 witness->external_name()); 836 } 837 } 838 839 void Dependencies::DepStream::log_dependency(Klass* witness) { 840 if (_deps == nullptr && xtty == nullptr) return; // fast cutout for runtime 841 ResourceMark rm; 842 const int nargs = argument_count(); 843 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 844 for (int j = 0; j < nargs; j++) { 845 if (is_oop_argument(j)) { 846 args->push(argument_oop(j)); 847 } else { 848 args->push(argument(j)); 849 } 850 } 851 int argslen = args->length(); 852 if (_deps != nullptr && _deps->log() != nullptr) { 853 if (ciEnv::current() != nullptr) { 854 Dependencies::write_dependency_to(_deps->log(), type(), args, witness); 855 } else { 856 // Treat the CompileLog as an xmlstream instead 857 Dependencies::write_dependency_to((xmlStream*)_deps->log(), type(), args, witness); 858 } 859 } else { 860 Dependencies::write_dependency_to(xtty, type(), args, witness); 861 } 862 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 863 } 864 865 void Dependencies::DepStream::print_dependency(outputStream* st, Klass* witness, bool verbose) { 866 ResourceMark rm; 867 int nargs = argument_count(); 868 GrowableArray<DepArgument>* args = new GrowableArray<DepArgument>(nargs); 869 for (int j = 0; j < nargs; j++) { 870 if (is_oop_argument(j)) { 871 args->push(argument_oop(j)); 872 } else { 873 args->push(argument(j)); 874 } 875 } 876 int argslen = args->length(); 877 Dependencies::print_dependency(type(), args, witness, st); 878 if (verbose) { 879 if (_code != nullptr) { 880 st->print(" code: "); 881 _code->print_value_on(st); 882 st->cr(); 883 } 884 } 885 guarantee(argslen == args->length(), "args array cannot grow inside nested ResoureMark scope"); 886 } 887 888 889 /// Dependency stream support (decodes dependencies from an nmethod): 890 891 #ifdef ASSERT 892 void Dependencies::DepStream::initial_asserts(size_t byte_limit) { 893 assert(must_be_in_vm(), "raw oops here"); 894 _byte_limit = byte_limit; 895 _type = undefined_dependency; // defeat "already at end" assert 896 assert((_code!=nullptr) + (_deps!=nullptr) == 1, "one or t'other"); 897 } 898 #endif //ASSERT 899 900 bool Dependencies::DepStream::next() { 901 assert(_type != end_marker, "already at end"); 902 if (_bytes.position() == 0 && _code != nullptr 903 && _code->dependencies_size() == 0) { 904 // Method has no dependencies at all. 905 return false; 906 } 907 int code_byte = (_bytes.read_byte() & 0xFF); 908 if (code_byte == end_marker) { 909 DEBUG_ONLY(_type = end_marker); 910 return false; 911 } else { 912 int ctxk_bit = (code_byte & Dependencies::default_context_type_bit); 913 code_byte -= ctxk_bit; 914 DepType dept = (DepType)code_byte; 915 _type = dept; 916 Dependencies::check_valid_dependency_type(dept); 917 int stride = _dep_args[dept]; 918 assert(stride == dep_args(dept), "sanity"); 919 int skipj = -1; 920 if (ctxk_bit != 0) { 921 skipj = 0; // currently the only context argument is at zero 922 assert(skipj == dep_context_arg(dept), "zero arg always ctxk"); 923 } 924 for (int j = 0; j < stride; j++) { 925 _xi[j] = (j == skipj)? 0: _bytes.read_int(); 926 } 927 DEBUG_ONLY(_xi[stride] = -1); // help detect overruns 928 return true; 929 } 930 } 931 932 inline Metadata* Dependencies::DepStream::recorded_metadata_at(int i) { 933 Metadata* o = nullptr; 934 if (_code != nullptr) { 935 o = _code->metadata_at(i); 936 } else { 937 o = _deps->oop_recorder()->metadata_at(i); 938 } 939 return o; 940 } 941 942 inline oop Dependencies::DepStream::recorded_oop_at(int i) { 943 return (_code != nullptr) 944 ? _code->oop_at(i) 945 : JNIHandles::resolve(_deps->oop_recorder()->oop_at(i)); 946 } 947 948 Metadata* Dependencies::DepStream::argument(int i) { 949 Metadata* result = recorded_metadata_at(argument_index(i)); 950 951 if (result == nullptr) { // Explicit context argument can be compressed 952 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 953 if (ctxkj >= 0 && i == ctxkj && ctxkj+1 < argument_count()) { 954 result = ctxk_encoded_as_null(type(), argument(ctxkj+1)); 955 } 956 } 957 958 assert(result == nullptr || result->is_klass() || result->is_method(), "must be"); 959 return result; 960 } 961 962 /** 963 * Returns a unique identifier for each dependency argument. 964 */ 965 uintptr_t Dependencies::DepStream::get_identifier(int i) { 966 if (is_oop_argument(i)) { 967 return (uintptr_t)(oopDesc*)argument_oop(i); 968 } else { 969 return (uintptr_t)argument(i); 970 } 971 } 972 973 oop Dependencies::DepStream::argument_oop(int i) { 974 oop result = recorded_oop_at(argument_index(i)); 975 assert(oopDesc::is_oop_or_null(result), "must be"); 976 return result; 977 } 978 979 InstanceKlass* Dependencies::DepStream::context_type() { 980 assert(must_be_in_vm(), "raw oops here"); 981 982 // Most dependencies have an explicit context type argument. 983 { 984 int ctxkj = dep_context_arg(type()); // -1 if no explicit context arg 985 if (ctxkj >= 0) { 986 Metadata* k = argument(ctxkj); 987 assert(k != nullptr && k->is_klass(), "type check"); 988 return InstanceKlass::cast((Klass*)k); 989 } 990 } 991 992 // Some dependencies are using the klass of the first object 993 // argument as implicit context type. 994 { 995 int ctxkj = dep_implicit_context_arg(type()); 996 if (ctxkj >= 0) { 997 Klass* k = argument_oop(ctxkj)->klass(); 998 assert(k != nullptr, "type check"); 999 return InstanceKlass::cast(k); 1000 } 1001 } 1002 1003 // And some dependencies don't have a context type at all, 1004 // e.g. evol_method. 1005 return nullptr; 1006 } 1007 1008 // ----------------- DependencySignature -------------------------------------- 1009 bool DependencySignature::equals(DependencySignature const& s1, DependencySignature const& s2) { 1010 if ((s1.type() != s2.type()) || (s1.args_count() != s2.args_count())) { 1011 return false; 1012 } 1013 1014 for (int i = 0; i < s1.args_count(); i++) { 1015 if (s1.arg(i) != s2.arg(i)) { 1016 return false; 1017 } 1018 } 1019 return true; 1020 } 1021 1022 /// Checking dependencies 1023 1024 // This hierarchy walker inspects subtypes of a given type, trying to find a "bad" class which breaks a dependency. 1025 // Such a class is called a "witness" to the broken dependency. 1026 // While searching around, we ignore "participants", which are already known to the dependency. 1027 class AbstractClassHierarchyWalker { 1028 public: 1029 enum { PARTICIPANT_LIMIT = 3 }; 1030 1031 private: 1032 // if non-zero, tells how many witnesses to convert to participants 1033 uint _record_witnesses; 1034 1035 // special classes which are not allowed to be witnesses: 1036 Klass* _participants[PARTICIPANT_LIMIT+1]; 1037 uint _num_participants; 1038 1039 #ifdef ASSERT 1040 uint _nof_requests; // one-shot walker 1041 #endif // ASSERT 1042 1043 static PerfCounter* _perf_find_witness_anywhere_calls_count; 1044 static PerfCounter* _perf_find_witness_anywhere_steps_count; 1045 static PerfCounter* _perf_find_witness_in_calls_count; 1046 1047 protected: 1048 virtual Klass* find_witness_in(KlassDepChange& changes) = 0; 1049 virtual Klass* find_witness_anywhere(InstanceKlass* context_type) = 0; 1050 1051 AbstractClassHierarchyWalker(Klass* participant) : _record_witnesses(0), _num_participants(0) 1052 #ifdef ASSERT 1053 , _nof_requests(0) 1054 #endif // ASSERT 1055 { 1056 for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1057 _participants[i] = nullptr; 1058 } 1059 if (participant != nullptr) { 1060 add_participant(participant); 1061 } 1062 } 1063 1064 bool is_participant(Klass* k) { 1065 for (uint i = 0; i < _num_participants; i++) { 1066 if (_participants[i] == k) { 1067 return true; 1068 } 1069 } 1070 return false; 1071 } 1072 1073 bool record_witness(Klass* witness) { 1074 if (_record_witnesses > 0) { 1075 --_record_witnesses; 1076 add_participant(witness); 1077 return false; // not a witness 1078 } else { 1079 return true; // is a witness 1080 } 1081 } 1082 1083 class CountingClassHierarchyIterator : public ClassHierarchyIterator { 1084 private: 1085 jlong _nof_steps; 1086 public: 1087 CountingClassHierarchyIterator(InstanceKlass* root) : ClassHierarchyIterator(root), _nof_steps(0) {} 1088 1089 void next() { 1090 _nof_steps++; 1091 ClassHierarchyIterator::next(); 1092 } 1093 1094 ~CountingClassHierarchyIterator() { 1095 if (UsePerfData) { 1096 _perf_find_witness_anywhere_steps_count->inc(_nof_steps); 1097 } 1098 } 1099 }; 1100 1101 public: 1102 uint num_participants() { return _num_participants; } 1103 Klass* participant(uint n) { 1104 assert(n <= _num_participants, "oob"); 1105 if (n < _num_participants) { 1106 return _participants[n]; 1107 } else { 1108 return nullptr; 1109 } 1110 } 1111 1112 void add_participant(Klass* participant) { 1113 assert(!is_participant(participant), "sanity"); 1114 assert(_num_participants + _record_witnesses < PARTICIPANT_LIMIT, "oob"); 1115 uint np = _num_participants++; 1116 _participants[np] = participant; 1117 } 1118 1119 void record_witnesses(uint add) { 1120 if (add > PARTICIPANT_LIMIT) add = PARTICIPANT_LIMIT; 1121 assert(_num_participants + add < PARTICIPANT_LIMIT, "oob"); 1122 _record_witnesses = add; 1123 } 1124 1125 Klass* find_witness(InstanceKlass* context_type, KlassDepChange* changes = nullptr); 1126 1127 static void init(); 1128 static void print_statistics(); 1129 }; 1130 1131 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_calls_count = nullptr; 1132 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_anywhere_steps_count = nullptr; 1133 PerfCounter* AbstractClassHierarchyWalker::_perf_find_witness_in_calls_count = nullptr; 1134 1135 void AbstractClassHierarchyWalker::init() { 1136 if (UsePerfData) { 1137 EXCEPTION_MARK; 1138 _perf_find_witness_anywhere_calls_count = 1139 PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhere", PerfData::U_Events, CHECK); 1140 _perf_find_witness_anywhere_steps_count = 1141 PerfDataManager::create_counter(SUN_CI, "findWitnessAnywhereSteps", PerfData::U_Events, CHECK); 1142 _perf_find_witness_in_calls_count = 1143 PerfDataManager::create_counter(SUN_CI, "findWitnessIn", PerfData::U_Events, CHECK); 1144 } 1145 } 1146 1147 Klass* AbstractClassHierarchyWalker::find_witness(InstanceKlass* context_type, KlassDepChange* changes) { 1148 // Current thread must be in VM (not native mode, as in CI): 1149 assert(must_be_in_vm(), "raw oops here"); 1150 // Must not move the class hierarchy during this check: 1151 assert_locked_or_safepoint(Compile_lock); 1152 assert(_nof_requests++ == 0, "repeated requests are not supported"); 1153 1154 assert(changes == nullptr || changes->involves_context(context_type), "irrelevant dependency"); 1155 1156 // (Note: Interfaces do not have subclasses.) 1157 // If it is an interface, search its direct implementors. 1158 // (Their subclasses are additional indirect implementors. See InstanceKlass::add_implementor().) 1159 if (context_type->is_interface()) { 1160 int nof_impls = context_type->nof_implementors(); 1161 if (nof_impls == 0) { 1162 return nullptr; // no implementors 1163 } else if (nof_impls == 1) { // unique implementor 1164 assert(context_type != context_type->implementor(), "not unique"); 1165 context_type = context_type->implementor(); 1166 } else { // nof_impls >= 2 1167 // Avoid this case: *I.m > { A.m, C }; B.m > C 1168 // Here, I.m has 2 concrete implementations, but m appears unique 1169 // as A.m, because the search misses B.m when checking C. 1170 // The inherited method B.m was getting missed by the walker 1171 // when interface 'I' was the starting point. 1172 // %%% Until this is fixed more systematically, bail out. 1173 return context_type; 1174 } 1175 } 1176 assert(!context_type->is_interface(), "no interfaces allowed"); 1177 1178 if (changes != nullptr) { 1179 if (UsePerfData) { 1180 _perf_find_witness_in_calls_count->inc(); 1181 } 1182 return find_witness_in(*changes); 1183 } else { 1184 if (UsePerfData) { 1185 _perf_find_witness_anywhere_calls_count->inc(); 1186 } 1187 return find_witness_anywhere(context_type); 1188 } 1189 } 1190 1191 class ConcreteSubtypeFinder : public AbstractClassHierarchyWalker { 1192 private: 1193 bool is_witness(Klass* k); 1194 1195 protected: 1196 virtual Klass* find_witness_in(KlassDepChange& changes); 1197 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1198 1199 public: 1200 ConcreteSubtypeFinder(Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) {} 1201 }; 1202 1203 bool ConcreteSubtypeFinder::is_witness(Klass* k) { 1204 if (Dependencies::is_concrete_klass(k)) { 1205 return record_witness(k); // concrete subtype 1206 } else { 1207 return false; // not a concrete class 1208 } 1209 } 1210 1211 Klass* ConcreteSubtypeFinder::find_witness_in(KlassDepChange& changes) { 1212 // When looking for unexpected concrete types, do not look beneath expected ones: 1213 // * CX > CC > C' is OK, even if C' is new. 1214 // * CX > { CC, C' } is not OK if C' is new, and C' is the witness. 1215 Klass* new_type = changes.as_new_klass_change()->new_type(); 1216 assert(!is_participant(new_type), "only old classes are participants"); 1217 // If the new type is a subtype of a participant, we are done. 1218 for (uint i = 0; i < num_participants(); i++) { 1219 if (changes.involves_context(participant(i))) { 1220 // new guy is protected from this check by previous participant 1221 return nullptr; 1222 } 1223 } 1224 if (is_witness(new_type)) { 1225 return new_type; 1226 } 1227 // No witness found. The dependency remains unbroken. 1228 return nullptr; 1229 } 1230 1231 Klass* ConcreteSubtypeFinder::find_witness_anywhere(InstanceKlass* context_type) { 1232 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1233 Klass* sub = iter.klass(); 1234 // Do not report participant types. 1235 if (is_participant(sub)) { 1236 // Don't walk beneath a participant since it hides witnesses. 1237 iter.skip_subclasses(); 1238 } else if (is_witness(sub)) { 1239 return sub; // found a witness 1240 } 1241 } 1242 // No witness found. The dependency remains unbroken. 1243 return nullptr; 1244 } 1245 1246 class ConcreteMethodFinder : public AbstractClassHierarchyWalker { 1247 private: 1248 Symbol* _name; 1249 Symbol* _signature; 1250 1251 // cache of method lookups 1252 Method* _found_methods[PARTICIPANT_LIMIT+1]; 1253 1254 bool is_witness(Klass* k); 1255 1256 protected: 1257 virtual Klass* find_witness_in(KlassDepChange& changes); 1258 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1259 1260 public: 1261 bool witnessed_reabstraction_in_supers(Klass* k); 1262 1263 ConcreteMethodFinder(Method* m, Klass* participant = nullptr) : AbstractClassHierarchyWalker(participant) { 1264 assert(m != nullptr && m->is_method(), "sanity"); 1265 _name = m->name(); 1266 _signature = m->signature(); 1267 1268 for (int i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1269 _found_methods[i] = nullptr; 1270 } 1271 } 1272 1273 // Note: If n==num_participants, returns nullptr. 1274 Method* found_method(uint n) { 1275 assert(n <= num_participants(), "oob"); 1276 Method* fm = _found_methods[n]; 1277 assert(n == num_participants() || fm != nullptr, "proper usage"); 1278 if (fm != nullptr && fm->method_holder() != participant(n)) { 1279 // Default methods from interfaces can be added to classes. In 1280 // that case the holder of the method is not the class but the 1281 // interface where it's defined. 1282 assert(fm->is_default_method(), "sanity"); 1283 return nullptr; 1284 } 1285 return fm; 1286 } 1287 1288 void add_participant(Klass* participant) { 1289 AbstractClassHierarchyWalker::add_participant(participant); 1290 _found_methods[num_participants()] = nullptr; 1291 } 1292 1293 bool record_witness(Klass* witness, Method* m) { 1294 _found_methods[num_participants()] = m; 1295 return AbstractClassHierarchyWalker::record_witness(witness); 1296 } 1297 1298 private: 1299 static PerfCounter* _perf_find_witness_anywhere_calls_count; 1300 static PerfCounter* _perf_find_witness_anywhere_steps_count; 1301 static PerfCounter* _perf_find_witness_in_calls_count; 1302 1303 public: 1304 static void init(); 1305 static void print_statistics(); 1306 }; 1307 1308 bool ConcreteMethodFinder::is_witness(Klass* k) { 1309 if (is_participant(k)) { 1310 return false; // do not report participant types 1311 } 1312 if (k->is_instance_klass()) { 1313 InstanceKlass* ik = InstanceKlass::cast(k); 1314 // Search class hierarchy first, skipping private implementations 1315 // as they never override any inherited methods 1316 Method* m = ik->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1317 if (Dependencies::is_concrete_method(m, ik)) { 1318 return record_witness(k, m); // concrete method found 1319 } else { 1320 // Check for re-abstraction of method 1321 if (!ik->is_interface() && m != nullptr && m->is_abstract()) { 1322 // Found a matching abstract method 'm' in the class hierarchy. 1323 // This is fine iff 'k' is an abstract class and all concrete subtypes 1324 // of 'k' override 'm' and are participates of the current search. 1325 ConcreteSubtypeFinder wf; 1326 for (uint i = 0; i < num_participants(); i++) { 1327 Klass* p = participant(i); 1328 wf.add_participant(p); 1329 } 1330 Klass* w = wf.find_witness(ik); 1331 if (w != nullptr) { 1332 Method* wm = InstanceKlass::cast(w)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1333 if (!Dependencies::is_concrete_method(wm, w)) { 1334 // Found a concrete subtype 'w' which does not override abstract method 'm'. 1335 // Bail out because 'm' could be called with 'w' as receiver (leading to an 1336 // AbstractMethodError) and thus the method we are looking for is not unique. 1337 return record_witness(k, m); 1338 } 1339 } 1340 } 1341 // Check interface defaults also, if any exist. 1342 Array<Method*>* default_methods = ik->default_methods(); 1343 if (default_methods != nullptr) { 1344 Method* dm = ik->find_method(default_methods, _name, _signature); 1345 if (Dependencies::is_concrete_method(dm, nullptr)) { 1346 return record_witness(k, dm); // default method found 1347 } 1348 } 1349 return false; // no concrete method found 1350 } 1351 } else { 1352 return false; // no methods to find in an array type 1353 } 1354 } 1355 1356 Klass* ConcreteMethodFinder::find_witness_in(KlassDepChange& changes) { 1357 // When looking for unexpected concrete methods, look beneath expected ones, to see if there are overrides. 1358 // * CX.m > CC.m > C'.m is not OK, if C'.m is new, and C' is the witness. 1359 Klass* new_type = changes.as_new_klass_change()->new_type(); 1360 assert(!is_participant(new_type), "only old classes are participants"); 1361 if (is_witness(new_type)) { 1362 return new_type; 1363 } else { 1364 // No witness found, but is_witness() doesn't detect method re-abstraction in case of spot-checking. 1365 if (witnessed_reabstraction_in_supers(new_type)) { 1366 return new_type; 1367 } 1368 } 1369 // No witness found. The dependency remains unbroken. 1370 return nullptr; 1371 } 1372 1373 bool ConcreteMethodFinder::witnessed_reabstraction_in_supers(Klass* k) { 1374 if (!k->is_instance_klass()) { 1375 return false; // no methods to find in an array type 1376 } else { 1377 // Looking for a case when an abstract method is inherited into a concrete class. 1378 if (Dependencies::is_concrete_klass(k) && !k->is_interface()) { 1379 Method* m = InstanceKlass::cast(k)->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1380 if (m != nullptr) { 1381 return false; // no reabstraction possible: local method found 1382 } 1383 for (InstanceKlass* super = k->java_super(); super != nullptr; super = super->java_super()) { 1384 m = super->find_instance_method(_name, _signature, Klass::PrivateLookupMode::skip); 1385 if (m != nullptr) { // inherited method found 1386 if (m->is_abstract() || m->is_overpass()) { 1387 return record_witness(super, m); // abstract method found 1388 } 1389 return false; 1390 } 1391 } 1392 // Miranda. 1393 return true; 1394 } 1395 return false; 1396 } 1397 } 1398 1399 1400 Klass* ConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) { 1401 // Walk hierarchy under a context type, looking for unexpected types. 1402 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1403 Klass* sub = iter.klass(); 1404 if (is_witness(sub)) { 1405 return sub; // found a witness 1406 } 1407 } 1408 // No witness found. The dependency remains unbroken. 1409 return nullptr; 1410 } 1411 1412 // For some method m and some class ctxk (subclass of method holder), 1413 // enumerate all distinct overrides of m in concrete subclasses of ctxk. 1414 // It relies on vtable/itable information to perform method selection on each linked subclass 1415 // and ignores all non yet linked ones (speculatively treat them as "effectively abstract"). 1416 class LinkedConcreteMethodFinder : public AbstractClassHierarchyWalker { 1417 private: 1418 InstanceKlass* _resolved_klass; // resolved class (JVMS-5.4.3.1) 1419 InstanceKlass* _declaring_klass; // the holder of resolved method (JVMS-5.4.3.3) 1420 int _vtable_index; // vtable/itable index of the resolved method 1421 bool _do_itable_lookup; // choose between itable and vtable lookup logic 1422 1423 // cache of method lookups 1424 Method* _found_methods[PARTICIPANT_LIMIT+1]; 1425 1426 bool is_witness(Klass* k); 1427 Method* select_method(InstanceKlass* recv_klass); 1428 static int compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, bool& is_itable_index); 1429 static bool is_concrete_klass(InstanceKlass* ik); 1430 1431 void add_participant(Method* m, Klass* participant) { 1432 uint np = num_participants(); 1433 AbstractClassHierarchyWalker::add_participant(participant); 1434 assert(np + 1 == num_participants(), "sanity"); 1435 _found_methods[np] = m; // record the method for the participant 1436 } 1437 1438 bool record_witness(Klass* witness, Method* m) { 1439 for (uint i = 0; i < num_participants(); i++) { 1440 if (found_method(i) == m) { 1441 return false; // already recorded 1442 } 1443 } 1444 // Record not yet seen method. 1445 _found_methods[num_participants()] = m; 1446 return AbstractClassHierarchyWalker::record_witness(witness); 1447 } 1448 1449 void initialize(Method* participant) { 1450 for (uint i = 0; i < PARTICIPANT_LIMIT+1; i++) { 1451 _found_methods[i] = nullptr; 1452 } 1453 if (participant != nullptr) { 1454 add_participant(participant, participant->method_holder()); 1455 } 1456 } 1457 1458 protected: 1459 virtual Klass* find_witness_in(KlassDepChange& changes); 1460 virtual Klass* find_witness_anywhere(InstanceKlass* context_type); 1461 1462 public: 1463 // In order to perform method selection, the following info is needed: 1464 // (1) interface or virtual call; 1465 // (2) vtable/itable index; 1466 // (3) declaring class (in case of interface call). 1467 // 1468 // It is prepared based on the results of method resolution: resolved class and resolved method (as specified in JVMS-5.4.3.3). 1469 // Optionally, a method which was previously determined as a unique target (uniqm) is added as a participant 1470 // to enable dependency spot-checking and speed up the search. 1471 LinkedConcreteMethodFinder(InstanceKlass* resolved_klass, Method* resolved_method, Method* uniqm = nullptr) : AbstractClassHierarchyWalker(nullptr) { 1472 assert(resolved_klass->is_linked(), "required"); 1473 assert(resolved_method->method_holder()->is_linked(), "required"); 1474 assert(!resolved_method->can_be_statically_bound(), "no vtable index available"); 1475 1476 _resolved_klass = resolved_klass; 1477 _declaring_klass = resolved_method->method_holder(); 1478 _vtable_index = compute_vtable_index(resolved_klass, resolved_method, 1479 _do_itable_lookup); // out parameter 1480 assert(_vtable_index >= 0, "invalid vtable index"); 1481 1482 initialize(uniqm); 1483 } 1484 1485 // Note: If n==num_participants, returns nullptr. 1486 Method* found_method(uint n) { 1487 assert(n <= num_participants(), "oob"); 1488 assert(participant(n) != nullptr || n == num_participants(), "proper usage"); 1489 return _found_methods[n]; 1490 } 1491 }; 1492 1493 Klass* LinkedConcreteMethodFinder::find_witness_in(KlassDepChange& changes) { 1494 Klass* type = changes.type(); 1495 1496 assert(!is_participant(type), "only old classes are participants"); 1497 1498 if (is_witness(type)) { 1499 return type; 1500 } 1501 return nullptr; // No witness found. The dependency remains unbroken. 1502 } 1503 1504 Klass* LinkedConcreteMethodFinder::find_witness_anywhere(InstanceKlass* context_type) { 1505 for (CountingClassHierarchyIterator iter(context_type); !iter.done(); iter.next()) { 1506 Klass* sub = iter.klass(); 1507 if (is_witness(sub)) { 1508 return sub; 1509 } 1510 if (sub->is_instance_klass() && !InstanceKlass::cast(sub)->is_linked()) { 1511 iter.skip_subclasses(); // ignore not yet linked classes 1512 } 1513 } 1514 return nullptr; // No witness found. The dependency remains unbroken. 1515 } 1516 1517 bool LinkedConcreteMethodFinder::is_witness(Klass* k) { 1518 if (is_participant(k)) { 1519 return false; // do not report participant types 1520 } else if (k->is_instance_klass()) { 1521 InstanceKlass* ik = InstanceKlass::cast(k); 1522 if (is_concrete_klass(ik)) { 1523 Method* m = select_method(ik); 1524 return record_witness(ik, m); 1525 } else { 1526 return false; // ignore non-concrete holder class 1527 } 1528 } else { 1529 return false; // no methods to find in an array type 1530 } 1531 } 1532 1533 Method* LinkedConcreteMethodFinder::select_method(InstanceKlass* recv_klass) { 1534 Method* selected_method = nullptr; 1535 if (_do_itable_lookup) { 1536 assert(_declaring_klass->is_interface(), "sanity"); 1537 bool implements_interface; // initialized by method_at_itable_or_null() 1538 selected_method = recv_klass->method_at_itable_or_null(_declaring_klass, _vtable_index, 1539 implements_interface); // out parameter 1540 assert(implements_interface, "not implemented"); 1541 } else { 1542 selected_method = recv_klass->method_at_vtable(_vtable_index); 1543 } 1544 return selected_method; // nullptr when corresponding slot is empty (AbstractMethodError case) 1545 } 1546 1547 int LinkedConcreteMethodFinder::compute_vtable_index(InstanceKlass* resolved_klass, Method* resolved_method, 1548 // out parameter 1549 bool& is_itable_index) { 1550 if (resolved_klass->is_interface() && resolved_method->has_itable_index()) { 1551 is_itable_index = true; 1552 return resolved_method->itable_index(); 1553 } 1554 // Check for default or miranda method first. 1555 InstanceKlass* declaring_klass = resolved_method->method_holder(); 1556 if (!resolved_klass->is_interface() && declaring_klass->is_interface()) { 1557 is_itable_index = false; 1558 return resolved_klass->vtable_index_of_interface_method(resolved_method); 1559 } 1560 // At this point we are sure that resolved_method is virtual and not 1561 // a default or miranda method; therefore, it must have a valid vtable index. 1562 assert(resolved_method->has_vtable_index(), ""); 1563 is_itable_index = false; 1564 return resolved_method->vtable_index(); 1565 } 1566 1567 bool LinkedConcreteMethodFinder::is_concrete_klass(InstanceKlass* ik) { 1568 if (!Dependencies::is_concrete_klass(ik)) { 1569 return false; // not concrete 1570 } 1571 if (ik->is_interface()) { 1572 return false; // interfaces aren't concrete 1573 } 1574 if (!ik->is_linked()) { 1575 return false; // not yet linked classes don't have instances 1576 } 1577 return true; 1578 } 1579 1580 #ifdef ASSERT 1581 // Assert that m is inherited into ctxk, without intervening overrides. 1582 // (May return true even if this is not true, in corner cases where we punt.) 1583 bool Dependencies::verify_method_context(InstanceKlass* ctxk, Method* m) { 1584 if (m->is_private()) { 1585 return false; // Quick lose. Should not happen. 1586 } 1587 if (m->method_holder() == ctxk) { 1588 return true; // Quick win. 1589 } 1590 if (!(m->is_public() || m->is_protected())) { 1591 // The override story is complex when packages get involved. 1592 return true; // Must punt the assertion to true. 1593 } 1594 Method* lm = ctxk->lookup_method(m->name(), m->signature()); 1595 if (lm == nullptr) { 1596 // It might be an interface method 1597 lm = ctxk->lookup_method_in_ordered_interfaces(m->name(), m->signature()); 1598 } 1599 if (lm == m) { 1600 // Method m is inherited into ctxk. 1601 return true; 1602 } 1603 if (lm != nullptr) { 1604 if (!(lm->is_public() || lm->is_protected())) { 1605 // Method is [package-]private, so the override story is complex. 1606 return true; // Must punt the assertion to true. 1607 } 1608 if (lm->is_static()) { 1609 // Static methods don't override non-static so punt 1610 return true; 1611 } 1612 if (!Dependencies::is_concrete_method(lm, ctxk) && 1613 !Dependencies::is_concrete_method(m, ctxk)) { 1614 // They are both non-concrete 1615 if (lm->method_holder()->is_subtype_of(m->method_holder())) { 1616 // Method m is overridden by lm, but both are non-concrete. 1617 return true; 1618 } 1619 if (lm->method_holder()->is_interface() && m->method_holder()->is_interface() && 1620 ctxk->is_subtype_of(m->method_holder()) && ctxk->is_subtype_of(lm->method_holder())) { 1621 // Interface method defined in multiple super interfaces 1622 return true; 1623 } 1624 } 1625 } 1626 ResourceMark rm; 1627 tty->print_cr("Dependency method not found in the associated context:"); 1628 tty->print_cr(" context = %s", ctxk->external_name()); 1629 tty->print( " method = "); m->print_short_name(tty); tty->cr(); 1630 if (lm != nullptr) { 1631 tty->print( " found = "); lm->print_short_name(tty); tty->cr(); 1632 } 1633 return false; 1634 } 1635 #endif // ASSERT 1636 1637 bool Dependencies::is_concrete_klass(Klass* k) { 1638 if (k->is_abstract()) return false; 1639 // %%% We could treat classes which are concrete but 1640 // have not yet been instantiated as virtually abstract. 1641 // This would require a deoptimization barrier on first instantiation. 1642 //if (k->is_not_instantiated()) return false; 1643 return true; 1644 } 1645 1646 bool Dependencies::is_concrete_method(Method* m, Klass* k) { 1647 // nullptr is not a concrete method. 1648 if (m == nullptr) { 1649 return false; 1650 } 1651 // Statics are irrelevant to virtual call sites. 1652 if (m->is_static()) { 1653 return false; 1654 } 1655 // Abstract methods are not concrete. 1656 if (m->is_abstract()) { 1657 return false; 1658 } 1659 // Overpass (error) methods are not concrete if k is abstract. 1660 if (m->is_overpass() && k != nullptr) { 1661 return !k->is_abstract(); 1662 } 1663 // Note "true" is conservative answer: overpass clause is false if k == nullptr, 1664 // implies return true if answer depends on overpass clause. 1665 return true; 1666 } 1667 1668 Klass* Dependencies::find_finalizable_subclass(InstanceKlass* ik) { 1669 for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) { 1670 Klass* sub = iter.klass(); 1671 if (sub->has_finalizer() && !sub->is_interface()) { 1672 return sub; 1673 } 1674 } 1675 return nullptr; // not found 1676 } 1677 1678 bool Dependencies::is_concrete_klass(ciInstanceKlass* k) { 1679 if (k->is_abstract()) return false; 1680 // We could also return false if k does not yet appear to be 1681 // instantiated, if the VM version supports this distinction also. 1682 //if (k->is_not_instantiated()) return false; 1683 return true; 1684 } 1685 1686 bool Dependencies::has_finalizable_subclass(ciInstanceKlass* k) { 1687 return k->has_finalizable_subclass(); 1688 } 1689 1690 // Any use of the contents (bytecodes) of a method must be 1691 // marked by an "evol_method" dependency, if those contents 1692 // can change. (Note: A method is always dependent on itself.) 1693 Klass* Dependencies::check_evol_method(Method* m) { 1694 assert(must_be_in_vm(), "raw oops here"); 1695 // Did somebody do a JVMTI RedefineClasses while our backs were turned? 1696 // Or is there a now a breakpoint? 1697 // (Assumes compiled code cannot handle bkpts; change if UseFastBreakpoints.) 1698 if (m->is_old() 1699 || m->number_of_breakpoints() > 0) { 1700 return m->method_holder(); 1701 } else { 1702 return nullptr; 1703 } 1704 } 1705 1706 // This is a strong assertion: It is that the given type 1707 // has no subtypes whatever. It is most useful for 1708 // optimizing checks on reflected types or on array types. 1709 // (Checks on types which are derived from real instances 1710 // can be optimized more strongly than this, because we 1711 // know that the checked type comes from a concrete type, 1712 // and therefore we can disregard abstract types.) 1713 Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) { 1714 assert(must_be_in_vm(), "raw oops here"); 1715 assert_locked_or_safepoint(Compile_lock); 1716 Klass* sub = ctxk->subklass(); 1717 if (sub != nullptr) { 1718 return sub; 1719 } else if (ctxk->nof_implementors() != 0) { 1720 // if it is an interface, it must be unimplemented 1721 // (if it is not an interface, nof_implementors is always zero) 1722 InstanceKlass* impl = ctxk->implementor(); 1723 assert(impl != nullptr, "must be set"); 1724 return impl; 1725 } else { 1726 return nullptr; 1727 } 1728 } 1729 1730 // Test the assertion that conck is the only concrete subtype* of ctxk. 1731 // The type conck itself is allowed to have have further concrete subtypes. 1732 // This allows the compiler to narrow occurrences of ctxk by conck, 1733 // when dealing with the types of actual instances. 1734 Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, 1735 Klass* conck, 1736 NewKlassDepChange* changes) { 1737 ConcreteSubtypeFinder wf(conck); 1738 Klass* k = wf.find_witness(ctxk, changes); 1739 return k; 1740 } 1741 1742 1743 // Find the unique concrete proper subtype of ctxk, or nullptr if there 1744 // is more than one concrete proper subtype. If there are no concrete 1745 // proper subtypes, return ctxk itself, whether it is concrete or not. 1746 // The returned subtype is allowed to have have further concrete subtypes. 1747 // That is, return CC1 for CX > CC1 > CC2, but nullptr for CX > { CC1, CC2 }. 1748 Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) { 1749 ConcreteSubtypeFinder wf(ctxk); // Ignore ctxk when walking. 1750 wf.record_witnesses(1); // Record one other witness when walking. 1751 Klass* wit = wf.find_witness(ctxk); 1752 if (wit != nullptr) return nullptr; // Too many witnesses. 1753 Klass* conck = wf.participant(0); 1754 if (conck == nullptr) { 1755 return ctxk; // Return ctxk as a flag for "no subtypes". 1756 } else { 1757 #ifndef PRODUCT 1758 // Make sure the dependency mechanism will pass this discovery: 1759 if (VerifyDependencies) { 1760 // Turn off dependency tracing while actually testing deps. 1761 FlagSetting fs(_verify_in_progress, true); 1762 if (!Dependencies::is_concrete_klass(ctxk)) { 1763 guarantee(nullptr == (void *) 1764 check_abstract_with_unique_concrete_subtype(ctxk, conck), 1765 "verify dep."); 1766 } 1767 } 1768 #endif //PRODUCT 1769 return conck; 1770 } 1771 } 1772 1773 // Try to determine whether root method in some context is concrete or not based on the information about the unique method 1774 // in that context. It exploits the fact that concrete root method is always inherited into the context when there's a unique method. 1775 // Hence, unique method holder is always a supertype of the context class when root method is concrete. 1776 // Examples for concrete_root_method 1777 // C (C.m uniqm) 1778 // | 1779 // CX (ctxk) uniqm is inherited into context. 1780 // 1781 // CX (ctxk) (CX.m uniqm) here uniqm is defined in ctxk. 1782 // Examples for !concrete_root_method 1783 // CX (ctxk) 1784 // | 1785 // C (C.m uniqm) uniqm is in subtype of ctxk. 1786 bool Dependencies::is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk) { 1787 if (uniqm == nullptr) { 1788 return false; // match Dependencies::is_concrete_method() behavior 1789 } 1790 // Theoretically, the "direction" of subtype check matters here. 1791 // On one hand, in case of interface context with a single implementor, uniqm can be in a superclass of the implementor which 1792 // is not related to context class. 1793 // On another hand, uniqm could come from an interface unrelated to the context class, but right now it is not possible: 1794 // it is required that uniqm->method_holder() is the participant (uniqm->method_holder() <: ctxk), hence a default method 1795 // can't be used as unique. 1796 if (ctxk->is_interface()) { 1797 InstanceKlass* implementor = ctxk->implementor(); 1798 assert(implementor != ctxk, "single implementor only"); // should have been invalidated earlier 1799 ctxk = implementor; 1800 } 1801 InstanceKlass* holder = uniqm->method_holder(); 1802 assert(!holder->is_interface(), "no default methods allowed"); 1803 assert(ctxk->is_subclass_of(holder) || holder->is_subclass_of(ctxk), "not related"); 1804 return ctxk->is_subclass_of(holder); 1805 } 1806 1807 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1808 // Otherwise, return a class that contains an interfering method. 1809 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1810 Method* uniqm, 1811 NewKlassDepChange* changes) { 1812 ConcreteMethodFinder wf(uniqm, uniqm->method_holder()); 1813 Klass* k = wf.find_witness(ctxk, changes); 1814 if (k != nullptr) { 1815 return k; 1816 } 1817 if (!Dependencies::is_concrete_root_method(uniqm, ctxk) || changes != nullptr) { 1818 Klass* conck = find_witness_AME(ctxk, uniqm, changes); 1819 if (conck != nullptr) { 1820 // Found a concrete subtype 'conck' which does not override abstract root method. 1821 return conck; 1822 } 1823 } 1824 return nullptr; 1825 } 1826 1827 Klass* Dependencies::check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes) { 1828 assert(ctxk->is_interface(), "sanity"); 1829 assert(ctxk->nof_implementors() > 0, "no implementors"); 1830 if (ctxk->nof_implementors() == 1) { 1831 assert(ctxk->implementor() == uniqk, "sanity"); 1832 return nullptr; 1833 } 1834 return ctxk; // no unique implementor 1835 } 1836 1837 // Search for AME. 1838 // There are two version of checks. 1839 // 1) Spot checking version(Classload time). Newly added class is checked for AME. 1840 // Checks whether abstract/overpass method is inherited into/declared in newly added concrete class. 1841 // 2) Compile time analysis for abstract/overpass(abstract klass) root_m. The non uniqm subtrees are checked for concrete classes. 1842 Klass* Dependencies::find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes) { 1843 if (m != nullptr) { 1844 if (changes != nullptr) { 1845 // Spot checking version. 1846 ConcreteMethodFinder wf(m); 1847 Klass* new_type = changes->as_new_klass_change()->new_type(); 1848 if (wf.witnessed_reabstraction_in_supers(new_type)) { 1849 return new_type; 1850 } 1851 } else { 1852 // Note: It is required that uniqm->method_holder() is the participant (see ClassHierarchyWalker::found_method()). 1853 ConcreteSubtypeFinder wf(m->method_holder()); 1854 Klass* conck = wf.find_witness(ctxk); 1855 if (conck != nullptr) { 1856 Method* cm = InstanceKlass::cast(conck)->find_instance_method(m->name(), m->signature(), Klass::PrivateLookupMode::skip); 1857 if (!Dependencies::is_concrete_method(cm, conck)) { 1858 return conck; 1859 } 1860 } 1861 } 1862 } 1863 return nullptr; 1864 } 1865 1866 // This function is used by find_unique_concrete_method(non vtable based) 1867 // to check whether subtype method overrides the base method. 1868 static bool overrides(Method* sub_m, Method* base_m) { 1869 assert(base_m != nullptr, "base method should be non null"); 1870 if (sub_m == nullptr) { 1871 return false; 1872 } 1873 /** 1874 * If base_m is public or protected then sub_m always overrides. 1875 * If base_m is !public, !protected and !private (i.e. base_m is package private) 1876 * then sub_m should be in the same package as that of base_m. 1877 * For package private base_m this is conservative approach as it allows only subset of all allowed cases in 1878 * the jvm specification. 1879 **/ 1880 if (base_m->is_public() || base_m->is_protected() || 1881 base_m->method_holder()->is_same_class_package(sub_m->method_holder())) { 1882 return true; 1883 } 1884 return false; 1885 } 1886 1887 // Find the set of all non-abstract methods under ctxk that match m. 1888 // (The method m must be defined or inherited in ctxk.) 1889 // Include m itself in the set, unless it is abstract. 1890 // If this set has exactly one element, return that element. 1891 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) { 1892 // Return nullptr if m is marked old; must have been a redefined method. 1893 if (m->is_old()) { 1894 return nullptr; 1895 } 1896 if (m->is_default_method()) { 1897 return nullptr; // not supported 1898 } 1899 assert(verify_method_context(ctxk, m), "proper context"); 1900 ConcreteMethodFinder wf(m); 1901 wf.record_witnesses(1); 1902 Klass* wit = wf.find_witness(ctxk); 1903 if (wit != nullptr) return nullptr; // Too many witnesses. 1904 Method* fm = wf.found_method(0); // Will be nullptr if num_parts == 0. 1905 if (participant != nullptr) { 1906 (*participant) = wf.participant(0); 1907 } 1908 if (!Dependencies::is_concrete_method(fm, nullptr)) { 1909 fm = nullptr; // ignore abstract methods 1910 } 1911 if (Dependencies::is_concrete_method(m, ctxk)) { 1912 if (fm == nullptr) { 1913 // It turns out that m was always the only implementation. 1914 fm = m; 1915 } else if (fm != m) { 1916 // Two conflicting implementations after all. 1917 // (This can happen if m is inherited into ctxk and fm overrides it.) 1918 return nullptr; 1919 } 1920 } else if (Dependencies::find_witness_AME(ctxk, fm) != nullptr) { 1921 // Found a concrete subtype which does not override abstract root method. 1922 return nullptr; 1923 } else if (!overrides(fm, m)) { 1924 // Found method doesn't override abstract root method. 1925 return nullptr; 1926 } 1927 assert(Dependencies::is_concrete_root_method(fm, ctxk) == Dependencies::is_concrete_method(m, ctxk), "mismatch"); 1928 #ifndef PRODUCT 1929 // Make sure the dependency mechanism will pass this discovery: 1930 if (VerifyDependencies && fm != nullptr) { 1931 guarantee(nullptr == (void *)check_unique_concrete_method(ctxk, fm), 1932 "verify dep."); 1933 } 1934 #endif //PRODUCT 1935 return fm; 1936 } 1937 1938 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1939 // Otherwise, return a class that contains an interfering method. 1940 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1941 Method* uniqm, 1942 Klass* resolved_klass, 1943 Method* resolved_method, 1944 KlassDepChange* changes) { 1945 assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity"); 1946 assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity"); 1947 assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity"); 1948 1949 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1950 !resolved_method->method_holder()->is_linked() || 1951 resolved_method->can_be_statically_bound()) { 1952 // Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation. 1953 return nullptr; // no vtable index available 1954 } 1955 1956 LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm); 1957 return mf.find_witness(ctxk, changes); 1958 } 1959 1960 // Find the set of all non-abstract methods under ctxk that match m. 1961 // (The method m must be defined or inherited in ctxk.) 1962 // Include m itself in the set, unless it is abstract. 1963 // If this set has exactly one element, return that element. 1964 // Not yet linked subclasses of ctxk are ignored since they don't have any instances yet. 1965 // Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed. 1966 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) { 1967 // Return nullptr if m is marked old; must have been a redefined method. 1968 if (m->is_old()) { 1969 return nullptr; 1970 } 1971 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1972 !resolved_method->method_holder()->is_linked() || 1973 resolved_method->can_be_statically_bound()) { 1974 return m; // nothing to do: no witness under ctxk 1975 } 1976 LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method); 1977 assert(Dependencies::verify_method_context(ctxk, m), "proper context"); 1978 wf.record_witnesses(1); 1979 Klass* wit = wf.find_witness(ctxk); 1980 if (wit != nullptr) { 1981 return nullptr; // Too many witnesses. 1982 } 1983 // p == nullptr when no participants are found (wf.num_participants() == 0). 1984 // fm == nullptr case has 2 meanings: 1985 // * when p == nullptr: no method found; 1986 // * when p != nullptr: AbstractMethodError-throwing method found. 1987 // Also, found method should always be accompanied by a participant class. 1988 Klass* p = wf.participant(0); 1989 Method* fm = wf.found_method(0); 1990 assert(fm == nullptr || p != nullptr, "no participant"); 1991 // Normalize all error-throwing cases to nullptr. 1992 if (fm == Universe::throw_illegal_access_error() || 1993 fm == Universe::throw_no_such_method_error() || 1994 !Dependencies::is_concrete_method(fm, p)) { 1995 fm = nullptr; // error-throwing method 1996 } 1997 if (Dependencies::is_concrete_method(m, ctxk)) { 1998 if (p == nullptr) { 1999 // It turns out that m was always the only implementation. 2000 assert(fm == nullptr, "sanity"); 2001 fm = m; 2002 } 2003 } 2004 #ifndef PRODUCT 2005 // Make sure the dependency mechanism will pass this discovery: 2006 if (VerifyDependencies && fm != nullptr) { 2007 guarantee(nullptr == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method), 2008 "verify dep."); 2009 } 2010 #endif // PRODUCT 2011 assert(fm == nullptr || !fm->is_abstract(), "sanity"); 2012 // Old CHA conservatively reports concrete methods in abstract classes 2013 // irrespective of whether they have concrete subclasses or not. 2014 // Also, abstract root method case is not fully supported. 2015 #ifdef ASSERT 2016 Klass* uniqp = nullptr; 2017 Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp); 2018 assert(uniqm == nullptr || uniqm == fm || 2019 m->is_abstract() || 2020 uniqm->method_holder()->is_abstract() || 2021 (fm == nullptr && uniqm != nullptr && uniqp != nullptr && !InstanceKlass::cast(uniqp)->is_linked()), 2022 "sanity"); 2023 #endif // ASSERT 2024 return fm; 2025 } 2026 2027 Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) { 2028 InstanceKlass* search_at = ctxk; 2029 if (changes != nullptr) { 2030 search_at = changes->new_type(); // just look at the new bit 2031 } 2032 return find_finalizable_subclass(search_at); 2033 } 2034 2035 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) { 2036 assert(call_site != nullptr, "sanity"); 2037 assert(method_handle != nullptr, "sanity"); 2038 assert(call_site->is_a(vmClasses::CallSite_klass()), "sanity"); 2039 2040 if (changes == nullptr) { 2041 // Validate all CallSites 2042 if (java_lang_invoke_CallSite::target(call_site) != method_handle) 2043 return call_site->klass(); // assertion failed 2044 } else { 2045 // Validate the given CallSite 2046 if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) { 2047 assert(method_handle != changes->method_handle(), "must be"); 2048 return call_site->klass(); // assertion failed 2049 } 2050 } 2051 return nullptr; // assertion still valid 2052 } 2053 2054 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) { 2055 if (_verify_in_progress) return; // don't log 2056 if (witness != nullptr) { 2057 LogTarget(Debug, dependencies) lt; 2058 if (lt.is_enabled()) { 2059 LogStream ls(<); 2060 print_dependency(&ls, witness, /*verbose=*/ true); 2061 } 2062 // The following is a no-op unless logging is enabled: 2063 log_dependency(witness); 2064 } 2065 } 2066 2067 Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) { 2068 assert_locked_or_safepoint(Compile_lock); 2069 Dependencies::check_valid_dependency_type(type()); 2070 2071 Klass* witness = nullptr; 2072 switch (type()) { 2073 case evol_method: 2074 witness = check_evol_method(method_argument(0)); 2075 break; 2076 case leaf_type: 2077 witness = check_leaf_type(context_type()); 2078 break; 2079 case abstract_with_unique_concrete_subtype: 2080 witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes); 2081 break; 2082 case unique_concrete_method_2: 2083 witness = check_unique_concrete_method(context_type(), method_argument(1), changes); 2084 break; 2085 case unique_concrete_method_4: 2086 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2087 break; 2088 case unique_implementor: 2089 witness = check_unique_implementor(context_type(), type_argument(1), changes); 2090 break; 2091 case no_finalizable_subclasses: 2092 witness = check_has_no_finalizable_subclasses(context_type(), changes); 2093 break; 2094 default: 2095 witness = nullptr; 2096 break; 2097 } 2098 trace_and_log_witness(witness); 2099 return witness; 2100 } 2101 2102 Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) { 2103 assert_locked_or_safepoint(Compile_lock); 2104 Dependencies::check_valid_dependency_type(type()); 2105 2106 // No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes. 2107 Klass* witness = nullptr; 2108 switch (type()) { 2109 case unique_concrete_method_4: 2110 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2111 break; 2112 default: 2113 witness = nullptr; 2114 break; 2115 } 2116 trace_and_log_witness(witness); 2117 return witness; 2118 } 2119 2120 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) { 2121 assert_locked_or_safepoint(Compile_lock); 2122 Dependencies::check_valid_dependency_type(type()); 2123 2124 if (changes != nullptr) { 2125 if (changes->is_klass_init_change()) { 2126 return check_klass_init_dependency(changes->as_klass_init_change()); 2127 } else { 2128 return check_new_klass_dependency(changes->as_new_klass_change()); 2129 } 2130 } else { 2131 Klass* witness = check_new_klass_dependency(nullptr); 2132 // check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent. 2133 assert(witness != nullptr || check_klass_init_dependency(nullptr) == nullptr, "missed dependency"); 2134 return witness; 2135 } 2136 } 2137 2138 Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) { 2139 assert_locked_or_safepoint(Compile_lock); 2140 Dependencies::check_valid_dependency_type(type()); 2141 2142 Klass* witness = nullptr; 2143 switch (type()) { 2144 case call_site_target_value: 2145 witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes); 2146 break; 2147 default: 2148 witness = nullptr; 2149 break; 2150 } 2151 trace_and_log_witness(witness); 2152 return witness; 2153 } 2154 2155 2156 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) { 2157 // Handle klass dependency 2158 if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type())) 2159 return check_klass_dependency(changes.as_klass_change()); 2160 2161 // Handle CallSite dependency 2162 if (changes.is_call_site_change()) 2163 return check_call_site_dependency(changes.as_call_site_change()); 2164 2165 // irrelevant dependency; skip it 2166 return nullptr; 2167 } 2168 2169 2170 void DepChange::print() { print_on(tty); } 2171 2172 void DepChange::print_on(outputStream* st) { 2173 int nsup = 0, nint = 0; 2174 for (ContextStream str(*this); str.next(); ) { 2175 InstanceKlass* k = str.klass(); 2176 switch (str.change_type()) { 2177 case Change_new_type: 2178 st->print_cr(" dependee = %s", k->external_name()); 2179 break; 2180 case Change_new_sub: 2181 if (!WizardMode) { 2182 ++nsup; 2183 } else { 2184 st->print_cr(" context super = %s", k->external_name()); 2185 } 2186 break; 2187 case Change_new_impl: 2188 if (!WizardMode) { 2189 ++nint; 2190 } else { 2191 st->print_cr(" context interface = %s", k->external_name()); 2192 } 2193 break; 2194 default: 2195 break; 2196 } 2197 } 2198 if (nsup + nint != 0) { 2199 st->print_cr(" context supers = %d, interfaces = %d", nsup, nint); 2200 } 2201 } 2202 2203 void DepChange::ContextStream::start() { 2204 InstanceKlass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (InstanceKlass*) nullptr); 2205 _change_type = (type == nullptr ? NO_CHANGE : Start_Klass); 2206 _klass = type; 2207 _ti_base = nullptr; 2208 _ti_index = 0; 2209 _ti_limit = 0; 2210 } 2211 2212 bool DepChange::ContextStream::next() { 2213 switch (_change_type) { 2214 case Start_Klass: // initial state; _klass is the new type 2215 _ti_base = _klass->transitive_interfaces(); 2216 _ti_index = 0; 2217 _change_type = Change_new_type; 2218 return true; 2219 case Change_new_type: 2220 // fall through: 2221 _change_type = Change_new_sub; 2222 case Change_new_sub: 2223 // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277 2224 { 2225 _klass = _klass->java_super(); 2226 if (_klass != nullptr) { 2227 return true; 2228 } 2229 } 2230 // else set up _ti_limit and fall through: 2231 _ti_limit = (_ti_base == nullptr) ? 0 : _ti_base->length(); 2232 _change_type = Change_new_impl; 2233 case Change_new_impl: 2234 if (_ti_index < _ti_limit) { 2235 _klass = _ti_base->at(_ti_index++); 2236 return true; 2237 } 2238 // fall through: 2239 _change_type = NO_CHANGE; // iterator is exhausted 2240 case NO_CHANGE: 2241 break; 2242 default: 2243 ShouldNotReachHere(); 2244 } 2245 return false; 2246 } 2247 2248 void KlassDepChange::initialize() { 2249 // entire transaction must be under this lock: 2250 assert_lock_strong(Compile_lock); 2251 2252 // Mark all dependee and all its superclasses 2253 // Mark transitive interfaces 2254 for (ContextStream str(*this); str.next(); ) { 2255 InstanceKlass* d = str.klass(); 2256 assert(!d->is_marked_dependent(), "checking"); 2257 d->set_is_marked_dependent(true); 2258 } 2259 } 2260 2261 KlassDepChange::~KlassDepChange() { 2262 // Unmark all dependee and all its superclasses 2263 // Unmark transitive interfaces 2264 for (ContextStream str(*this); str.next(); ) { 2265 InstanceKlass* d = str.klass(); 2266 d->set_is_marked_dependent(false); 2267 } 2268 } 2269 2270 bool KlassDepChange::involves_context(Klass* k) { 2271 if (k == nullptr || !k->is_instance_klass()) { 2272 return false; 2273 } 2274 InstanceKlass* ik = InstanceKlass::cast(k); 2275 bool is_contained = ik->is_marked_dependent(); 2276 assert(is_contained == type()->is_subtype_of(k), 2277 "correct marking of potential context types"); 2278 return is_contained; 2279 } 2280 2281 void Dependencies::print_statistics() { 2282 AbstractClassHierarchyWalker::print_statistics(); 2283 } 2284 2285 void AbstractClassHierarchyWalker::print_statistics() { 2286 if (UsePerfData) { 2287 jlong deps_find_witness_calls = _perf_find_witness_anywhere_calls_count->get_value(); 2288 jlong deps_find_witness_steps = _perf_find_witness_anywhere_steps_count->get_value(); 2289 jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value(); 2290 2291 ttyLocker ttyl; 2292 tty->print_cr("Dependency check (find_witness) " 2293 "calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT, 2294 deps_find_witness_calls, 2295 deps_find_witness_steps, 2296 (double)deps_find_witness_steps / deps_find_witness_calls, 2297 deps_find_witness_singles); 2298 if (xtty != nullptr) { 2299 xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'", 2300 deps_find_witness_calls, 2301 deps_find_witness_steps, 2302 deps_find_witness_singles); 2303 } 2304 } 2305 } 2306 2307 CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) : 2308 _call_site(call_site), 2309 _method_handle(method_handle) { 2310 assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be"); 2311 assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be"); 2312 } 2313 2314 void dependencies_init() { 2315 AbstractClassHierarchyWalker::init(); 2316 }