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 || m->mismatch()) { 1701 return m->method_holder(); 1702 } else { 1703 return nullptr; 1704 } 1705 } 1706 1707 // This is a strong assertion: It is that the given type 1708 // has no subtypes whatever. It is most useful for 1709 // optimizing checks on reflected types or on array types. 1710 // (Checks on types which are derived from real instances 1711 // can be optimized more strongly than this, because we 1712 // know that the checked type comes from a concrete type, 1713 // and therefore we can disregard abstract types.) 1714 Klass* Dependencies::check_leaf_type(InstanceKlass* ctxk) { 1715 assert(must_be_in_vm(), "raw oops here"); 1716 assert_locked_or_safepoint(Compile_lock); 1717 Klass* sub = ctxk->subklass(); 1718 if (sub != nullptr) { 1719 return sub; 1720 } else if (ctxk->nof_implementors() != 0) { 1721 // if it is an interface, it must be unimplemented 1722 // (if it is not an interface, nof_implementors is always zero) 1723 InstanceKlass* impl = ctxk->implementor(); 1724 assert(impl != nullptr, "must be set"); 1725 return impl; 1726 } else { 1727 return nullptr; 1728 } 1729 } 1730 1731 // Test the assertion that conck is the only concrete subtype* of ctxk. 1732 // The type conck itself is allowed to have have further concrete subtypes. 1733 // This allows the compiler to narrow occurrences of ctxk by conck, 1734 // when dealing with the types of actual instances. 1735 Klass* Dependencies::check_abstract_with_unique_concrete_subtype(InstanceKlass* ctxk, 1736 Klass* conck, 1737 NewKlassDepChange* changes) { 1738 ConcreteSubtypeFinder wf(conck); 1739 Klass* k = wf.find_witness(ctxk, changes); 1740 return k; 1741 } 1742 1743 1744 // Find the unique concrete proper subtype of ctxk, or nullptr if there 1745 // is more than one concrete proper subtype. If there are no concrete 1746 // proper subtypes, return ctxk itself, whether it is concrete or not. 1747 // The returned subtype is allowed to have have further concrete subtypes. 1748 // That is, return CC1 for CX > CC1 > CC2, but nullptr for CX > { CC1, CC2 }. 1749 Klass* Dependencies::find_unique_concrete_subtype(InstanceKlass* ctxk) { 1750 ConcreteSubtypeFinder wf(ctxk); // Ignore ctxk when walking. 1751 wf.record_witnesses(1); // Record one other witness when walking. 1752 Klass* wit = wf.find_witness(ctxk); 1753 if (wit != nullptr) return nullptr; // Too many witnesses. 1754 Klass* conck = wf.participant(0); 1755 if (conck == nullptr) { 1756 return ctxk; // Return ctxk as a flag for "no subtypes". 1757 } else { 1758 #ifndef PRODUCT 1759 // Make sure the dependency mechanism will pass this discovery: 1760 if (VerifyDependencies) { 1761 // Turn off dependency tracing while actually testing deps. 1762 FlagSetting fs(_verify_in_progress, true); 1763 if (!Dependencies::is_concrete_klass(ctxk)) { 1764 guarantee(nullptr == (void *) 1765 check_abstract_with_unique_concrete_subtype(ctxk, conck), 1766 "verify dep."); 1767 } 1768 } 1769 #endif //PRODUCT 1770 return conck; 1771 } 1772 } 1773 1774 // Try to determine whether root method in some context is concrete or not based on the information about the unique method 1775 // in that context. It exploits the fact that concrete root method is always inherited into the context when there's a unique method. 1776 // Hence, unique method holder is always a supertype of the context class when root method is concrete. 1777 // Examples for concrete_root_method 1778 // C (C.m uniqm) 1779 // | 1780 // CX (ctxk) uniqm is inherited into context. 1781 // 1782 // CX (ctxk) (CX.m uniqm) here uniqm is defined in ctxk. 1783 // Examples for !concrete_root_method 1784 // CX (ctxk) 1785 // | 1786 // C (C.m uniqm) uniqm is in subtype of ctxk. 1787 bool Dependencies::is_concrete_root_method(Method* uniqm, InstanceKlass* ctxk) { 1788 if (uniqm == nullptr) { 1789 return false; // match Dependencies::is_concrete_method() behavior 1790 } 1791 // Theoretically, the "direction" of subtype check matters here. 1792 // On one hand, in case of interface context with a single implementor, uniqm can be in a superclass of the implementor which 1793 // is not related to context class. 1794 // On another hand, uniqm could come from an interface unrelated to the context class, but right now it is not possible: 1795 // it is required that uniqm->method_holder() is the participant (uniqm->method_holder() <: ctxk), hence a default method 1796 // can't be used as unique. 1797 if (ctxk->is_interface()) { 1798 InstanceKlass* implementor = ctxk->implementor(); 1799 assert(implementor != ctxk, "single implementor only"); // should have been invalidated earlier 1800 ctxk = implementor; 1801 } 1802 InstanceKlass* holder = uniqm->method_holder(); 1803 assert(!holder->is_interface(), "no default methods allowed"); 1804 assert(ctxk->is_subclass_of(holder) || holder->is_subclass_of(ctxk), "not related"); 1805 return ctxk->is_subclass_of(holder); 1806 } 1807 1808 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1809 // Otherwise, return a class that contains an interfering method. 1810 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1811 Method* uniqm, 1812 NewKlassDepChange* changes) { 1813 ConcreteMethodFinder wf(uniqm, uniqm->method_holder()); 1814 Klass* k = wf.find_witness(ctxk, changes); 1815 if (k != nullptr) { 1816 return k; 1817 } 1818 if (!Dependencies::is_concrete_root_method(uniqm, ctxk) || changes != nullptr) { 1819 Klass* conck = find_witness_AME(ctxk, uniqm, changes); 1820 if (conck != nullptr) { 1821 // Found a concrete subtype 'conck' which does not override abstract root method. 1822 return conck; 1823 } 1824 } 1825 return nullptr; 1826 } 1827 1828 Klass* Dependencies::check_unique_implementor(InstanceKlass* ctxk, Klass* uniqk, NewKlassDepChange* changes) { 1829 assert(ctxk->is_interface(), "sanity"); 1830 assert(ctxk->nof_implementors() > 0, "no implementors"); 1831 if (ctxk->nof_implementors() == 1) { 1832 assert(ctxk->implementor() == uniqk, "sanity"); 1833 return nullptr; 1834 } 1835 return ctxk; // no unique implementor 1836 } 1837 1838 // Search for AME. 1839 // There are two version of checks. 1840 // 1) Spot checking version(Classload time). Newly added class is checked for AME. 1841 // Checks whether abstract/overpass method is inherited into/declared in newly added concrete class. 1842 // 2) Compile time analysis for abstract/overpass(abstract klass) root_m. The non uniqm subtrees are checked for concrete classes. 1843 Klass* Dependencies::find_witness_AME(InstanceKlass* ctxk, Method* m, KlassDepChange* changes) { 1844 if (m != nullptr) { 1845 if (changes != nullptr) { 1846 // Spot checking version. 1847 ConcreteMethodFinder wf(m); 1848 Klass* new_type = changes->as_new_klass_change()->new_type(); 1849 if (wf.witnessed_reabstraction_in_supers(new_type)) { 1850 return new_type; 1851 } 1852 } else { 1853 // Note: It is required that uniqm->method_holder() is the participant (see ClassHierarchyWalker::found_method()). 1854 ConcreteSubtypeFinder wf(m->method_holder()); 1855 Klass* conck = wf.find_witness(ctxk); 1856 if (conck != nullptr) { 1857 Method* cm = InstanceKlass::cast(conck)->find_instance_method(m->name(), m->signature(), Klass::PrivateLookupMode::skip); 1858 if (!Dependencies::is_concrete_method(cm, conck)) { 1859 return conck; 1860 } 1861 } 1862 } 1863 } 1864 return nullptr; 1865 } 1866 1867 // This function is used by find_unique_concrete_method(non vtable based) 1868 // to check whether subtype method overrides the base method. 1869 static bool overrides(Method* sub_m, Method* base_m) { 1870 assert(base_m != nullptr, "base method should be non null"); 1871 if (sub_m == nullptr) { 1872 return false; 1873 } 1874 /** 1875 * If base_m is public or protected then sub_m always overrides. 1876 * If base_m is !public, !protected and !private (i.e. base_m is package private) 1877 * then sub_m should be in the same package as that of base_m. 1878 * For package private base_m this is conservative approach as it allows only subset of all allowed cases in 1879 * the jvm specification. 1880 **/ 1881 if (base_m->is_public() || base_m->is_protected() || 1882 base_m->method_holder()->is_same_class_package(sub_m->method_holder())) { 1883 return true; 1884 } 1885 return false; 1886 } 1887 1888 // Find the set of all non-abstract methods under ctxk that match m. 1889 // (The method m must be defined or inherited in ctxk.) 1890 // Include m itself in the set, unless it is abstract. 1891 // If this set has exactly one element, return that element. 1892 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass** participant) { 1893 // Return nullptr if m is marked old; must have been a redefined method. 1894 if (m->is_old()) { 1895 return nullptr; 1896 } 1897 if (m->is_default_method()) { 1898 return nullptr; // not supported 1899 } 1900 assert(verify_method_context(ctxk, m), "proper context"); 1901 ConcreteMethodFinder wf(m); 1902 wf.record_witnesses(1); 1903 Klass* wit = wf.find_witness(ctxk); 1904 if (wit != nullptr) return nullptr; // Too many witnesses. 1905 Method* fm = wf.found_method(0); // Will be nullptr if num_parts == 0. 1906 if (participant != nullptr) { 1907 (*participant) = wf.participant(0); 1908 } 1909 if (!Dependencies::is_concrete_method(fm, nullptr)) { 1910 fm = nullptr; // ignore abstract methods 1911 } 1912 if (Dependencies::is_concrete_method(m, ctxk)) { 1913 if (fm == nullptr) { 1914 // It turns out that m was always the only implementation. 1915 fm = m; 1916 } else if (fm != m) { 1917 // Two conflicting implementations after all. 1918 // (This can happen if m is inherited into ctxk and fm overrides it.) 1919 return nullptr; 1920 } 1921 } else if (Dependencies::find_witness_AME(ctxk, fm) != nullptr) { 1922 // Found a concrete subtype which does not override abstract root method. 1923 return nullptr; 1924 } else if (!overrides(fm, m)) { 1925 // Found method doesn't override abstract root method. 1926 return nullptr; 1927 } 1928 assert(Dependencies::is_concrete_root_method(fm, ctxk) == Dependencies::is_concrete_method(m, ctxk), "mismatch"); 1929 #ifndef PRODUCT 1930 // Make sure the dependency mechanism will pass this discovery: 1931 if (VerifyDependencies && fm != nullptr) { 1932 guarantee(nullptr == (void *)check_unique_concrete_method(ctxk, fm), 1933 "verify dep."); 1934 } 1935 #endif //PRODUCT 1936 return fm; 1937 } 1938 1939 // If a class (or interface) has a unique concrete method uniqm, return nullptr. 1940 // Otherwise, return a class that contains an interfering method. 1941 Klass* Dependencies::check_unique_concrete_method(InstanceKlass* ctxk, 1942 Method* uniqm, 1943 Klass* resolved_klass, 1944 Method* resolved_method, 1945 KlassDepChange* changes) { 1946 assert(!ctxk->is_interface() || ctxk == resolved_klass, "sanity"); 1947 assert(!resolved_method->can_be_statically_bound() || resolved_method == uniqm, "sanity"); 1948 assert(resolved_klass->is_subtype_of(resolved_method->method_holder()), "sanity"); 1949 1950 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1951 !resolved_method->method_holder()->is_linked() || 1952 resolved_method->can_be_statically_bound()) { 1953 // Dependency is redundant, but benign. Just keep it to avoid unnecessary recompilation. 1954 return nullptr; // no vtable index available 1955 } 1956 1957 LinkedConcreteMethodFinder mf(InstanceKlass::cast(resolved_klass), resolved_method, uniqm); 1958 return mf.find_witness(ctxk, changes); 1959 } 1960 1961 // Find the set of all non-abstract methods under ctxk that match m. 1962 // (The method m must be defined or inherited in ctxk.) 1963 // Include m itself in the set, unless it is abstract. 1964 // If this set has exactly one element, return that element. 1965 // Not yet linked subclasses of ctxk are ignored since they don't have any instances yet. 1966 // Additionally, resolved_klass and resolved_method complete the description of the call site being analyzed. 1967 Method* Dependencies::find_unique_concrete_method(InstanceKlass* ctxk, Method* m, Klass* resolved_klass, Method* resolved_method) { 1968 // Return nullptr if m is marked old; must have been a redefined method. 1969 if (m->is_old()) { 1970 return nullptr; 1971 } 1972 if (!InstanceKlass::cast(resolved_klass)->is_linked() || 1973 !resolved_method->method_holder()->is_linked() || 1974 resolved_method->can_be_statically_bound()) { 1975 return m; // nothing to do: no witness under ctxk 1976 } 1977 LinkedConcreteMethodFinder wf(InstanceKlass::cast(resolved_klass), resolved_method); 1978 assert(Dependencies::verify_method_context(ctxk, m), "proper context"); 1979 wf.record_witnesses(1); 1980 Klass* wit = wf.find_witness(ctxk); 1981 if (wit != nullptr) { 1982 return nullptr; // Too many witnesses. 1983 } 1984 // p == nullptr when no participants are found (wf.num_participants() == 0). 1985 // fm == nullptr case has 2 meanings: 1986 // * when p == nullptr: no method found; 1987 // * when p != nullptr: AbstractMethodError-throwing method found. 1988 // Also, found method should always be accompanied by a participant class. 1989 Klass* p = wf.participant(0); 1990 Method* fm = wf.found_method(0); 1991 assert(fm == nullptr || p != nullptr, "no participant"); 1992 // Normalize all error-throwing cases to nullptr. 1993 if (fm == Universe::throw_illegal_access_error() || 1994 fm == Universe::throw_no_such_method_error() || 1995 !Dependencies::is_concrete_method(fm, p)) { 1996 fm = nullptr; // error-throwing method 1997 } 1998 if (Dependencies::is_concrete_method(m, ctxk)) { 1999 if (p == nullptr) { 2000 // It turns out that m was always the only implementation. 2001 assert(fm == nullptr, "sanity"); 2002 fm = m; 2003 } 2004 } 2005 #ifndef PRODUCT 2006 // Make sure the dependency mechanism will pass this discovery: 2007 if (VerifyDependencies && fm != nullptr) { 2008 guarantee(nullptr == check_unique_concrete_method(ctxk, fm, resolved_klass, resolved_method), 2009 "verify dep."); 2010 } 2011 #endif // PRODUCT 2012 assert(fm == nullptr || !fm->is_abstract(), "sanity"); 2013 // Old CHA conservatively reports concrete methods in abstract classes 2014 // irrespective of whether they have concrete subclasses or not. 2015 // Also, abstract root method case is not fully supported. 2016 #ifdef ASSERT 2017 Klass* uniqp = nullptr; 2018 Method* uniqm = Dependencies::find_unique_concrete_method(ctxk, m, &uniqp); 2019 assert(uniqm == nullptr || uniqm == fm || 2020 m->is_abstract() || 2021 uniqm->method_holder()->is_abstract() || 2022 (fm == nullptr && uniqm != nullptr && uniqp != nullptr && !InstanceKlass::cast(uniqp)->is_linked()), 2023 "sanity"); 2024 #endif // ASSERT 2025 return fm; 2026 } 2027 2028 Klass* Dependencies::check_has_no_finalizable_subclasses(InstanceKlass* ctxk, NewKlassDepChange* changes) { 2029 InstanceKlass* search_at = ctxk; 2030 if (changes != nullptr) { 2031 search_at = changes->new_type(); // just look at the new bit 2032 } 2033 return find_finalizable_subclass(search_at); 2034 } 2035 2036 Klass* Dependencies::check_call_site_target_value(oop call_site, oop method_handle, CallSiteDepChange* changes) { 2037 assert(call_site != nullptr, "sanity"); 2038 assert(method_handle != nullptr, "sanity"); 2039 assert(call_site->is_a(vmClasses::CallSite_klass()), "sanity"); 2040 2041 if (changes == nullptr) { 2042 // Validate all CallSites 2043 if (java_lang_invoke_CallSite::target(call_site) != method_handle) 2044 return call_site->klass(); // assertion failed 2045 } else { 2046 // Validate the given CallSite 2047 if (call_site == changes->call_site() && java_lang_invoke_CallSite::target(call_site) != changes->method_handle()) { 2048 assert(method_handle != changes->method_handle(), "must be"); 2049 return call_site->klass(); // assertion failed 2050 } 2051 } 2052 return nullptr; // assertion still valid 2053 } 2054 2055 void Dependencies::DepStream::trace_and_log_witness(Klass* witness) { 2056 if (_verify_in_progress) return; // don't log 2057 if (witness != nullptr) { 2058 LogTarget(Debug, dependencies) lt; 2059 if (lt.is_enabled()) { 2060 LogStream ls(<); 2061 print_dependency(&ls, witness, /*verbose=*/ true); 2062 } 2063 // The following is a no-op unless logging is enabled: 2064 log_dependency(witness); 2065 } 2066 } 2067 2068 Klass* Dependencies::DepStream::check_new_klass_dependency(NewKlassDepChange* changes) { 2069 assert_locked_or_safepoint(Compile_lock); 2070 Dependencies::check_valid_dependency_type(type()); 2071 2072 Klass* witness = nullptr; 2073 switch (type()) { 2074 case evol_method: 2075 witness = check_evol_method(method_argument(0)); 2076 break; 2077 case leaf_type: 2078 witness = check_leaf_type(context_type()); 2079 break; 2080 case abstract_with_unique_concrete_subtype: 2081 witness = check_abstract_with_unique_concrete_subtype(context_type(), type_argument(1), changes); 2082 break; 2083 case unique_concrete_method_2: 2084 witness = check_unique_concrete_method(context_type(), method_argument(1), changes); 2085 break; 2086 case unique_concrete_method_4: 2087 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2088 break; 2089 case unique_implementor: 2090 witness = check_unique_implementor(context_type(), type_argument(1), changes); 2091 break; 2092 case no_finalizable_subclasses: 2093 witness = check_has_no_finalizable_subclasses(context_type(), changes); 2094 break; 2095 default: 2096 witness = nullptr; 2097 break; 2098 } 2099 trace_and_log_witness(witness); 2100 return witness; 2101 } 2102 2103 Klass* Dependencies::DepStream::check_klass_init_dependency(KlassInitDepChange* changes) { 2104 assert_locked_or_safepoint(Compile_lock); 2105 Dependencies::check_valid_dependency_type(type()); 2106 2107 // No new types added. Only unique_concrete_method_4 is sensitive to class initialization changes. 2108 Klass* witness = nullptr; 2109 switch (type()) { 2110 case unique_concrete_method_4: 2111 witness = check_unique_concrete_method(context_type(), method_argument(1), type_argument(2), method_argument(3), changes); 2112 break; 2113 default: 2114 witness = nullptr; 2115 break; 2116 } 2117 trace_and_log_witness(witness); 2118 return witness; 2119 } 2120 2121 Klass* Dependencies::DepStream::check_klass_dependency(KlassDepChange* changes) { 2122 assert_locked_or_safepoint(Compile_lock); 2123 Dependencies::check_valid_dependency_type(type()); 2124 2125 if (changes != nullptr) { 2126 if (changes->is_klass_init_change()) { 2127 return check_klass_init_dependency(changes->as_klass_init_change()); 2128 } else { 2129 return check_new_klass_dependency(changes->as_new_klass_change()); 2130 } 2131 } else { 2132 Klass* witness = check_new_klass_dependency(nullptr); 2133 // check_klass_init_dependency duplicates check_new_klass_dependency checks when class hierarchy change info is absent. 2134 assert(witness != nullptr || check_klass_init_dependency(nullptr) == nullptr, "missed dependency"); 2135 return witness; 2136 } 2137 } 2138 2139 Klass* Dependencies::DepStream::check_call_site_dependency(CallSiteDepChange* changes) { 2140 assert_locked_or_safepoint(Compile_lock); 2141 Dependencies::check_valid_dependency_type(type()); 2142 2143 Klass* witness = nullptr; 2144 switch (type()) { 2145 case call_site_target_value: 2146 witness = check_call_site_target_value(argument_oop(0), argument_oop(1), changes); 2147 break; 2148 default: 2149 witness = nullptr; 2150 break; 2151 } 2152 trace_and_log_witness(witness); 2153 return witness; 2154 } 2155 2156 2157 Klass* Dependencies::DepStream::spot_check_dependency_at(DepChange& changes) { 2158 // Handle klass dependency 2159 if (changes.is_klass_change() && changes.as_klass_change()->involves_context(context_type())) 2160 return check_klass_dependency(changes.as_klass_change()); 2161 2162 // Handle CallSite dependency 2163 if (changes.is_call_site_change()) 2164 return check_call_site_dependency(changes.as_call_site_change()); 2165 2166 // irrelevant dependency; skip it 2167 return nullptr; 2168 } 2169 2170 2171 void DepChange::print() { print_on(tty); } 2172 2173 void DepChange::print_on(outputStream* st) { 2174 int nsup = 0, nint = 0; 2175 for (ContextStream str(*this); str.next(); ) { 2176 InstanceKlass* k = str.klass(); 2177 switch (str.change_type()) { 2178 case Change_new_type: 2179 st->print_cr(" dependee = %s", k->external_name()); 2180 break; 2181 case Change_new_sub: 2182 if (!WizardMode) { 2183 ++nsup; 2184 } else { 2185 st->print_cr(" context super = %s", k->external_name()); 2186 } 2187 break; 2188 case Change_new_impl: 2189 if (!WizardMode) { 2190 ++nint; 2191 } else { 2192 st->print_cr(" context interface = %s", k->external_name()); 2193 } 2194 break; 2195 default: 2196 break; 2197 } 2198 } 2199 if (nsup + nint != 0) { 2200 st->print_cr(" context supers = %d, interfaces = %d", nsup, nint); 2201 } 2202 } 2203 2204 void DepChange::ContextStream::start() { 2205 InstanceKlass* type = (_changes.is_klass_change() ? _changes.as_klass_change()->type() : (InstanceKlass*) nullptr); 2206 _change_type = (type == nullptr ? NO_CHANGE : Start_Klass); 2207 _klass = type; 2208 _ti_base = nullptr; 2209 _ti_index = 0; 2210 _ti_limit = 0; 2211 } 2212 2213 bool DepChange::ContextStream::next() { 2214 switch (_change_type) { 2215 case Start_Klass: // initial state; _klass is the new type 2216 _ti_base = _klass->transitive_interfaces(); 2217 _ti_index = 0; 2218 _change_type = Change_new_type; 2219 return true; 2220 case Change_new_type: 2221 // fall through: 2222 _change_type = Change_new_sub; 2223 case Change_new_sub: 2224 // 6598190: brackets workaround Sun Studio C++ compiler bug 6629277 2225 { 2226 _klass = _klass->java_super(); 2227 if (_klass != nullptr) { 2228 return true; 2229 } 2230 } 2231 // else set up _ti_limit and fall through: 2232 _ti_limit = (_ti_base == nullptr) ? 0 : _ti_base->length(); 2233 _change_type = Change_new_impl; 2234 case Change_new_impl: 2235 if (_ti_index < _ti_limit) { 2236 _klass = _ti_base->at(_ti_index++); 2237 return true; 2238 } 2239 // fall through: 2240 _change_type = NO_CHANGE; // iterator is exhausted 2241 case NO_CHANGE: 2242 break; 2243 default: 2244 ShouldNotReachHere(); 2245 } 2246 return false; 2247 } 2248 2249 void KlassDepChange::initialize() { 2250 // entire transaction must be under this lock: 2251 assert_lock_strong(Compile_lock); 2252 2253 // Mark all dependee and all its superclasses 2254 // Mark transitive interfaces 2255 for (ContextStream str(*this); str.next(); ) { 2256 InstanceKlass* d = str.klass(); 2257 assert(!d->is_marked_dependent(), "checking"); 2258 d->set_is_marked_dependent(true); 2259 } 2260 } 2261 2262 KlassDepChange::~KlassDepChange() { 2263 // Unmark all dependee and all its superclasses 2264 // Unmark transitive interfaces 2265 for (ContextStream str(*this); str.next(); ) { 2266 InstanceKlass* d = str.klass(); 2267 d->set_is_marked_dependent(false); 2268 } 2269 } 2270 2271 bool KlassDepChange::involves_context(Klass* k) { 2272 if (k == nullptr || !k->is_instance_klass()) { 2273 return false; 2274 } 2275 InstanceKlass* ik = InstanceKlass::cast(k); 2276 bool is_contained = ik->is_marked_dependent(); 2277 assert(is_contained == type()->is_subtype_of(k), 2278 "correct marking of potential context types"); 2279 return is_contained; 2280 } 2281 2282 void Dependencies::print_statistics() { 2283 AbstractClassHierarchyWalker::print_statistics(); 2284 } 2285 2286 void AbstractClassHierarchyWalker::print_statistics() { 2287 if (UsePerfData) { 2288 jlong deps_find_witness_calls = _perf_find_witness_anywhere_calls_count->get_value(); 2289 jlong deps_find_witness_steps = _perf_find_witness_anywhere_steps_count->get_value(); 2290 jlong deps_find_witness_singles = _perf_find_witness_in_calls_count->get_value(); 2291 2292 ttyLocker ttyl; 2293 tty->print_cr("Dependency check (find_witness) " 2294 "calls=" JLONG_FORMAT ", steps=" JLONG_FORMAT " (avg=%.1f), singles=" JLONG_FORMAT, 2295 deps_find_witness_calls, 2296 deps_find_witness_steps, 2297 (double)deps_find_witness_steps / deps_find_witness_calls, 2298 deps_find_witness_singles); 2299 if (xtty != nullptr) { 2300 xtty->elem("deps_find_witness calls='" JLONG_FORMAT "' steps='" JLONG_FORMAT "' singles='" JLONG_FORMAT "'", 2301 deps_find_witness_calls, 2302 deps_find_witness_steps, 2303 deps_find_witness_singles); 2304 } 2305 } 2306 } 2307 2308 CallSiteDepChange::CallSiteDepChange(Handle call_site, Handle method_handle) : 2309 _call_site(call_site), 2310 _method_handle(method_handle) { 2311 assert(_call_site()->is_a(vmClasses::CallSite_klass()), "must be"); 2312 assert(_method_handle.is_null() || _method_handle()->is_a(vmClasses::MethodHandle_klass()), "must be"); 2313 } 2314 2315 void dependencies_init() { 2316 AbstractClassHierarchyWalker::init(); 2317 }