1 /*
2 * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classPrelinker.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/resolutionErrors.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "classfile/vmClasses.hpp"
34 #include "code/codeCache.hpp"
35 #include "interpreter/bytecodeStream.hpp"
36 #include "interpreter/bytecodes.hpp"
37 #include "interpreter/interpreter.hpp"
38 #include "interpreter/linkResolver.hpp"
39 #include "interpreter/rewriter.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "memory/metadataFactory.hpp"
43 #include "memory/metaspaceClosure.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "oops/access.inline.hpp"
46 #include "oops/compressedOops.hpp"
47 #include "oops/constantPool.inline.hpp"
48 #include "oops/cpCache.inline.hpp"
49 #include "oops/method.inline.hpp"
50 #include "oops/objArrayOop.inline.hpp"
51 #include "oops/oop.inline.hpp"
52 #include "oops/resolvedFieldEntry.hpp"
53 #include "oops/resolvedIndyEntry.hpp"
54 #include "oops/resolvedMethodEntry.hpp"
55 #include "prims/methodHandles.hpp"
56 #include "runtime/arguments.hpp"
57 #include "runtime/atomic.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/mutexLocker.hpp"
60 #include "runtime/synchronizer.hpp"
61 #include "runtime/vm_version.hpp"
62 #include "utilities/macros.hpp"
63
64 // Implementation of ConstantPoolCache
65
66 template <class T>
67 static Array<T>* initialize_resolved_entries_array(ClassLoaderData* loader_data, GrowableArray<T> entries, TRAPS) {
68 Array<T>* resolved_entries;
69 if (entries.length() != 0) {
70 resolved_entries = MetadataFactory::new_array<T>(loader_data, entries.length(), CHECK_NULL);
71 for (int i = 0; i < entries.length(); i++) {
72 resolved_entries->at_put(i, entries.at(i));
73 }
74 return resolved_entries;
75 }
76 return nullptr;
77 }
78
79 void ConstantPoolCache::set_direct_or_vtable_call(Bytecodes::Code invoke_code,
80 int method_index,
81 const methodHandle& method,
82 int vtable_index,
83 bool sender_is_interface) {
84 bool is_vtable_call = (vtable_index >= 0); // FIXME: split this method on this boolean
85 assert(method->interpreter_entry() != nullptr, "should have been set at this point");
86 assert(!method->is_obsolete(), "attempt to write obsolete method to cpCache");
87
88 int byte_no = -1;
89 bool change_to_virtual = false;
90 InstanceKlass* holder = nullptr; // have to declare this outside the switch
91 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
92 switch (invoke_code) {
93 case Bytecodes::_invokeinterface:
94 holder = method->method_holder();
95 // check for private interface method invocations
96 if (vtable_index == Method::nonvirtual_vtable_index && holder->is_interface() ) {
97 assert(method->is_private(), "unexpected non-private method");
98 assert(method->can_be_statically_bound(), "unexpected non-statically-bound method");
99
100 method_entry->set_flags(( 1 << ResolvedMethodEntry::is_vfinal_shift) |
101 ((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift));
102 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
103 assert(method_entry->is_vfinal(), "flags must be set");
104 method_entry->set_method(method());
105 byte_no = 2;
106 method_entry->set_klass(holder);
107 break;
108 }
109 else {
110 // We get here from InterpreterRuntime::resolve_invoke when an invokeinterface
111 // instruction links to a non-interface method (in Object). This can happen when
112 // an interface redeclares an Object method (like CharSequence declaring toString())
113 // or when invokeinterface is used explicitly.
114 // In that case, the method has no itable index and must be invoked as a virtual.
115 // Set a flag to keep track of this corner case.
116 assert(holder->is_interface() || holder == vmClasses::Object_klass(), "unexpected holder class");
117 assert(method->is_public(), "Calling non-public method in Object with invokeinterface");
118 change_to_virtual = true;
119
120 // ...and fall through as if we were handling invokevirtual:
121 }
122 case Bytecodes::_invokevirtual:
123 {
124 if (!is_vtable_call) {
125 assert(method->can_be_statically_bound(), "");
126 method_entry->set_flags(( 1 << ResolvedMethodEntry::is_vfinal_shift) |
127 ((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift) |
128 ((change_to_virtual ? 1 : 0) << ResolvedMethodEntry::is_forced_virtual_shift));
129 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
130 assert(method_entry->is_vfinal(), "flags must be set");
131 method_entry->set_method(method());
132 } else {
133 assert(!method->can_be_statically_bound(), "");
134 assert(vtable_index >= 0, "valid index");
135 assert(!method->is_final_method(), "sanity");
136 method_entry->set_flags((change_to_virtual ? 1 : 0) << ResolvedMethodEntry::is_forced_virtual_shift);
137 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
138 assert(!method_entry->is_vfinal(), "flags must not be set");
139 method_entry->set_table_index(vtable_index);
140 }
141 byte_no = 2;
142 break;
143 }
144
145 case Bytecodes::_invokespecial:
146 case Bytecodes::_invokestatic: {
147 assert(!is_vtable_call, "");
148 // Note: Read and preserve the value of the is_vfinal flag on any
149 // invokevirtual bytecode shared with this constant pool cache entry.
150 // It is cheap and safe to consult is_vfinal() at all times.
151 // Once is_vfinal is set, it must stay that way, lest we get a dangling oop.
152 bool vfinal = method_entry->is_vfinal();
153 method_entry->set_flags(((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift));
154 assert(vfinal == method_entry->is_vfinal(), "Vfinal flag must be preserved");
155 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
156 method_entry->set_method(method());
157 byte_no = 1;
158 break;
159 }
160 default:
161 ShouldNotReachHere();
162 break;
163 }
164
165 // Note: byte_no also appears in TemplateTable::resolve.
166 if (byte_no == 1) {
167 assert(invoke_code != Bytecodes::_invokevirtual &&
168 invoke_code != Bytecodes::_invokeinterface, "");
169 bool do_resolve = true;
170 // Don't mark invokespecial to method as resolved if sender is an interface. The receiver
171 // has to be checked that it is a subclass of the current class every time this bytecode
172 // is executed.
173 if (invoke_code == Bytecodes::_invokespecial && sender_is_interface &&
174 method->name() != vmSymbols::object_initializer_name()) {
175 do_resolve = false;
176 }
177 if (invoke_code == Bytecodes::_invokestatic) {
178 assert(method->method_holder()->is_initialized() ||
179 method->method_holder()->is_reentrant_initialization(JavaThread::current()),
180 "invalid class initialization state for invoke_static");
181
182 if (!VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) {
183 // Don't mark invokestatic to method as resolved if the holder class has not yet completed
184 // initialization. An invokestatic must only proceed if the class is initialized, but if
185 // we resolve it before then that class initialization check is skipped.
186 //
187 // When fast class initialization checks are supported (VM_Version::supports_fast_class_init_checks() == true),
188 // template interpreter supports fast class initialization check for
189 // invokestatic which doesn't require call site re-resolution to
190 // enforce class initialization barrier.
191 do_resolve = false;
192 }
193 }
194 if (do_resolve) {
195 method_entry->set_bytecode1(invoke_code);
196 }
197 } else if (byte_no == 2) {
198 if (change_to_virtual) {
199 assert(invoke_code == Bytecodes::_invokeinterface, "");
200 // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
201 //
202 // Workaround for the case where we encounter an invokeinterface, but we
203 // should really have an _invokevirtual since the resolved method is a
204 // virtual method in java.lang.Object. This is a corner case in the spec
205 // but is presumably legal. javac does not generate this code.
206 //
207 // We do not set bytecode_1() to _invokeinterface, because that is the
208 // bytecode # used by the interpreter to see if it is resolved. In this
209 // case, the method gets reresolved with caller for each interface call
210 // because the actual selected method may not be public.
211 //
212 // We set bytecode_2() to _invokevirtual.
213 // See also interpreterRuntime.cpp. (8/25/2000)
214 } else {
215 assert(invoke_code == Bytecodes::_invokevirtual ||
216 (invoke_code == Bytecodes::_invokeinterface &&
217 ((method->is_private() ||
218 (method->is_final() && method->method_holder() == vmClasses::Object_klass())))),
219 "unexpected invocation mode");
220 if (invoke_code == Bytecodes::_invokeinterface &&
221 (method->is_private() || method->is_final())) {
222 // We set bytecode_1() to _invokeinterface, because that is the
223 // bytecode # used by the interpreter to see if it is resolved.
224 // We set bytecode_2() to _invokevirtual.
225 method_entry->set_bytecode1(invoke_code);
226 }
227 }
228 // set up for invokevirtual, even if linking for invokeinterface also:
229 method_entry->set_bytecode2(Bytecodes::_invokevirtual);
230 } else {
231 ShouldNotReachHere();
232 }
233 }
234
235 void ConstantPoolCache::set_direct_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method,
236 bool sender_is_interface) {
237 int index = Method::nonvirtual_vtable_index;
238 // index < 0; FIXME: inline and customize set_direct_or_vtable_call
239 set_direct_or_vtable_call(invoke_code, method_index, method, index, sender_is_interface);
240 }
241
242 void ConstantPoolCache::set_vtable_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method, int index) {
243 // either the method is a miranda or its holder should accept the given index
244 assert(method->method_holder()->is_interface() || method->method_holder()->verify_vtable_index(index), "");
245 // index >= 0; FIXME: inline and customize set_direct_or_vtable_call
246 set_direct_or_vtable_call(invoke_code, method_index, method, index, false);
247 }
248
249 void ConstantPoolCache::set_itable_call(Bytecodes::Code invoke_code,
250 int method_index,
251 Klass* referenced_klass,
252 const methodHandle& method, int index) {
253 assert(method->method_holder()->verify_itable_index(index), "");
254 assert(invoke_code == Bytecodes::_invokeinterface, "");
255 InstanceKlass* interf = method->method_holder();
256 assert(interf->is_interface(), "must be an interface");
257 assert(!method->is_final_method(), "interfaces do not have final methods; cannot link to one here");
258 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
259 method_entry->set_klass(static_cast<InstanceKlass*>(referenced_klass));
260 method_entry->set_method(method());
261 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
262 method_entry->set_bytecode1(Bytecodes::_invokeinterface);
263 }
264
265 ResolvedMethodEntry* ConstantPoolCache::set_method_handle(int method_index, const CallInfo &call_info) {
266 // NOTE: This method entry can be the subject of data races.
267 // There are three words to update: flags, refs[appendix_index], method (in that order).
268 // Writers must store all other values before method.
269 // Readers must test the method first for non-null before reading other fields.
270 // Competing writers must acquire exclusive access via a lock.
271 // A losing writer waits on the lock until the winner writes the method and leaves
272 // the lock, so that when the losing writer returns, he can use the linked
273 // cache entry.
274
275 // Lock fields to write
276 Bytecodes::Code invoke_code = Bytecodes::_invokehandle;
277
278 JavaThread* current = JavaThread::current();
279 objArrayHandle resolved_references(current, constant_pool()->resolved_references());
280 // Use the resolved_references() lock for this cpCache entry.
281 // resolved_references are created for all classes with Invokedynamic, MethodHandle
282 // or MethodType constant pool cache entries.
283 assert(resolved_references() != nullptr,
284 "a resolved_references array should have been created for this class");
285 ObjectLocker ol(resolved_references, current);
286
287 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
288 if (method_entry->is_resolved(invoke_code)) {
289 return method_entry;
290 }
291
292 Method* adapter = call_info.resolved_method();
293 const Handle appendix = call_info.resolved_appendix();
294 const bool has_appendix = appendix.not_null();
295
296 // Write the flags.
297 // MHs are always sig-poly and have a local signature.
298 method_entry->fill_in((u1)as_TosState(adapter->result_type()), (u2)adapter->size_of_parameters());
299 method_entry->set_flags(((has_appendix ? 1 : 0) << ResolvedMethodEntry::has_appendix_shift ) |
300 ( 1 << ResolvedMethodEntry::has_local_signature_shift ) |
301 ( 1 << ResolvedMethodEntry::is_final_shift ));
302
303 // Method handle invokes use both a method and a resolved references index.
304 // refs[appendix_index], if not null, contains a value passed as a trailing argument to the adapter.
305 // In the general case, this could be the call site's MethodType,
306 // for use with java.lang.Invokers.checkExactType, or else a CallSite object.
307 // method_entry->method() contains the adapter method which manages the actual call.
308 // In the general case, this is a compiled LambdaForm.
309 // (The Java code is free to optimize these calls by binding other
310 // sorts of methods and appendices to call sites.)
311 // JVM-level linking is via the method, as if for invokespecial, and signatures are erased.
312 // The appendix argument (if any) is added to the signature, and is counted in the parameter_size bits.
313 // Even with the appendix, the method will never take more than 255 parameter slots.
314 //
315 // This means that given a call site like (List)mh.invoke("foo"),
316 // the method has signature '(Ljl/Object;Ljl/invoke/MethodType;)Ljl/Object;',
317 // not '(Ljava/lang/String;)Ljava/util/List;'.
318 // The fact that String and List are involved is encoded in the MethodType in refs[appendix_index].
319 // This allows us to create fewer Methods, while keeping type safety.
320 //
321
322 // Store appendix, if any.
323 if (has_appendix) {
324 const int appendix_index = method_entry->resolved_references_index();
325 assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob");
326 assert(resolved_references->obj_at(appendix_index) == nullptr, "init just once");
327 resolved_references->obj_at_put(appendix_index, appendix());
328 }
329
330 method_entry->set_method(adapter); // This must be the last one to set (see NOTE above)!
331
332 // The interpreter assembly code does not check byte_2,
333 // but it is used by is_resolved, method_if_resolved, etc.
334 method_entry->set_bytecode1(invoke_code);
335
336 assert(has_appendix == method_entry->has_appendix(), "proper storage of appendix flag");
337 assert(method_entry->has_local_signature(), "proper storage of signature flag");
338 return method_entry;
339 }
340
341 Method* ConstantPoolCache::method_if_resolved(int method_index) const {
342 // Decode the action of set_method and set_interface_call
343 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
344
345 Bytecodes::Code invoke_code = (Bytecodes::Code)method_entry->bytecode1();
346 switch (invoke_code) {
347 case Bytecodes::_invokeinterface:
348 case Bytecodes::_invokestatic:
349 case Bytecodes::_invokespecial:
350 assert(!method_entry->has_appendix(), "");
351 // fall through
352 case Bytecodes::_invokehandle:
353 return method_entry->method();
354 case Bytecodes::_invokedynamic:
355 ShouldNotReachHere();
356 default:
357 assert(invoke_code == (Bytecodes::Code)0, "unexpected bytecode");
358 break;
359 }
360
361 invoke_code = (Bytecodes::Code)method_entry->bytecode2();
362 if (invoke_code == Bytecodes::_invokevirtual) {
363 if (method_entry->is_vfinal()) {
364 return method_entry->method();
365 } else {
366 int holder_index = constant_pool()->uncached_klass_ref_index_at(method_entry->constant_pool_index());
367 if (constant_pool()->tag_at(holder_index).is_klass()) {
368 Klass* klass = constant_pool()->resolved_klass_at(holder_index);
369 return klass->method_at_vtable(method_entry->table_index());
370 }
371 }
372 }
373 return nullptr;
374 }
375
376 ConstantPoolCache* ConstantPoolCache::allocate(ClassLoaderData* loader_data,
377 const intStack& invokedynamic_map,
378 const GrowableArray<ResolvedIndyEntry> indy_entries,
379 const GrowableArray<ResolvedFieldEntry> field_entries,
380 const GrowableArray<ResolvedMethodEntry> method_entries,
381 TRAPS) {
382
383 int size = ConstantPoolCache::size();
384
385 // Initialize resolved entry arrays with available data
386 Array<ResolvedFieldEntry>* resolved_field_entries = initialize_resolved_entries_array(loader_data, field_entries, CHECK_NULL);
387 Array<ResolvedIndyEntry>* resolved_indy_entries = initialize_resolved_entries_array(loader_data, indy_entries, CHECK_NULL);
388 Array<ResolvedMethodEntry>* resolved_method_entries = initialize_resolved_entries_array(loader_data, method_entries, CHECK_NULL);
389
390 return new (loader_data, size, MetaspaceObj::ConstantPoolCacheType, THREAD)
391 ConstantPoolCache(invokedynamic_map, resolved_indy_entries, resolved_field_entries, resolved_method_entries);
392 }
393
394 // Record the GC marking cycle when redefined vs. when found in the loom stack chunks.
395 void ConstantPoolCache::record_gc_epoch() {
396 _gc_epoch = CodeCache::gc_epoch();
397 }
398
399 #if INCLUDE_CDS
400 void ConstantPoolCache::remove_unshareable_info() {
401 assert(CDSConfig::is_dumping_archive(), "sanity");
402
403 if (_resolved_indy_entries != nullptr) {
404 for (int i = 0; i < _resolved_indy_entries->length(); i++) {
405 resolved_indy_entry_at(i)->remove_unshareable_info();
406 }
407 }
408 if (_resolved_field_entries != nullptr) {
409 remove_resolved_field_entries_if_non_deterministic();
410 }
411 if (_resolved_method_entries != nullptr) {
412 remove_resolved_method_entries_if_non_deterministic();
413 }
414 }
415
416 void ConstantPoolCache::remove_resolved_field_entries_if_non_deterministic() {
417 ConstantPool* cp = constant_pool();
418 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(cp);
419 for (int i = 0; i < _resolved_field_entries->length(); i++) {
420 ResolvedFieldEntry* rfi = _resolved_field_entries->adr_at(i);
421 int cp_index = rfi->constant_pool_index();
422 bool archived = false;
423 bool resolved = rfi->is_resolved(Bytecodes::_getfield) ||
424 rfi->is_resolved(Bytecodes::_putfield);
425 if (resolved && ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
426 rfi->mark_and_relocate();
427 archived = true;
428 } else {
429 rfi->remove_unshareable_info();
430 }
431 if (resolved) {
432 LogStreamHandle(Trace, cds, resolve) log;
433 if (log.is_enabled()) {
434 ResourceMark rm;
435 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
436 Symbol* klass_name = cp->klass_name_at(klass_cp_index);
437 Symbol* name = cp->uncached_name_ref_at(cp_index);
438 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
439 log.print("%s field CP entry [%3d]: %s %s %s.%s:%s",
440 (archived ? "archived" : "reverted"),
441 cp_index,
442 cp->pool_holder()->name()->as_C_string(),
443 (archived ? "=>" : " "),
444 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string());
445 }
446 }
447 ArchiveBuilder::alloc_stats()->record_field_cp_entry(archived, resolved && !archived);
448 }
449 }
450
451 void ConstantPoolCache::remove_resolved_method_entries_if_non_deterministic() {
452 ConstantPool* cp = constant_pool();
453 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(cp);
454 for (int i = 0; i < _resolved_method_entries->length(); i++) {
455 ResolvedMethodEntry* rme = _resolved_method_entries->adr_at(i);
456 int cp_index = rme->constant_pool_index();
457 bool archived = false;
458 bool resolved = rme->is_resolved(Bytecodes::_invokevirtual) ||
459 rme->is_resolved(Bytecodes::_invokespecial) ||
460 rme->is_resolved(Bytecodes::_invokeinterface);
461
462 // Just for safety -- this should not happen, but do not archive if we ever see this.
463 resolved &= !(rme->is_resolved(Bytecodes::_invokehandle) ||
464 rme->is_resolved(Bytecodes::_invokestatic));
465
466 if (resolved && can_archive_resolved_method(rme)) {
467 rme->mark_and_relocate(src_cp);
468 archived = true;
469 } else {
470 rme->remove_unshareable_info();
471 }
472 if (resolved) {
473 LogStreamHandle(Trace, cds, resolve) log;
474 if (log.is_enabled()) {
475 ResourceMark rm;
476 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index);
477 Symbol* klass_name = cp->klass_name_at(klass_cp_index);
478 Symbol* name = cp->uncached_name_ref_at(cp_index);
479 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
480 log.print("%s%s method CP entry [%3d]: %s %s.%s:%s",
481 (archived ? "archived" : "reverted"),
482 (rme->is_resolved(Bytecodes::_invokeinterface) ? " interface" : ""),
483 cp_index,
484 cp->pool_holder()->name()->as_C_string(),
485 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string());
486 if (archived) {
487 Klass* resolved_klass = cp->resolved_klass_at(klass_cp_index);
488 log.print(" => %s%s",
489 resolved_klass->name()->as_C_string(),
490 (rme->is_resolved(Bytecodes::_invokestatic) ? " *** static" : ""));
491 }
492 }
493 ArchiveBuilder::alloc_stats()->record_method_cp_entry(archived, resolved && !archived);
494 }
495 }
496 }
497
498 bool ConstantPoolCache::can_archive_resolved_method(ResolvedMethodEntry* method_entry) {
499 InstanceKlass* pool_holder = constant_pool()->pool_holder();
500 if (!(pool_holder->is_shared_boot_class() || pool_holder->is_shared_platform_class() ||
501 pool_holder->is_shared_app_class())) {
502 // Archiving resolved cp entries for classes from non-builtin loaders
503 // is not yet supported.
504 return false;
505 }
506
507 if (CDSConfig::is_dumping_dynamic_archive()) {
508 // InstanceKlass::methods() has been resorted. We need to
509 // update the vtable_index in method_entry (not implemented)
510 return false;
511 }
512
513 if (!method_entry->is_resolved(Bytecodes::_invokevirtual)) {
514 if (method_entry->method() == nullptr) {
515 return false;
516 }
517 if (method_entry->method()->is_continuation_native_intrinsic()) {
518 return false; // FIXME: corresponding stub is generated on demand during method resolution (see LinkResolver::resolve_static_call).
519 }
520 }
521
522 int cp_index = method_entry->constant_pool_index();
523 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(constant_pool());
524 assert(src_cp->tag_at(cp_index).is_method() || src_cp->tag_at(cp_index).is_interface_method(), "sanity");
525
526 if (!ClassPrelinker::is_resolution_deterministic(src_cp, cp_index)) {
527 return false;
528 }
529
530 if (method_entry->is_resolved(Bytecodes::_invokeinterface) ||
531 method_entry->is_resolved(Bytecodes::_invokevirtual) ||
532 method_entry->is_resolved(Bytecodes::_invokespecial)) {
533 return true;
534 } else {
535 // invokestatic and invokehandle are not supported yet.
536 return false;
537 }
538
539 }
540 #endif // INCLUDE_CDS
541
542 void ConstantPoolCache::deallocate_contents(ClassLoaderData* data) {
543 assert(!is_shared(), "shared caches are not deallocated");
544 data->remove_handle(_resolved_references);
545 set_resolved_references(OopHandle());
546 MetadataFactory::free_array<u2>(data, _reference_map);
547 set_reference_map(nullptr);
548 #if INCLUDE_CDS
549 if (_resolved_indy_entries != nullptr) {
550 MetadataFactory::free_array<ResolvedIndyEntry>(data, _resolved_indy_entries);
551 _resolved_indy_entries = nullptr;
552 }
553 if (_resolved_field_entries != nullptr) {
554 MetadataFactory::free_array<ResolvedFieldEntry>(data, _resolved_field_entries);
555 _resolved_field_entries = nullptr;
556 }
557 if (_resolved_method_entries != nullptr) {
558 MetadataFactory::free_array<ResolvedMethodEntry>(data, _resolved_method_entries);
559 _resolved_method_entries = nullptr;
560 }
561 #endif
562 }
563
564 #if INCLUDE_CDS_JAVA_HEAP
565 oop ConstantPoolCache::archived_references() {
566 if (_archived_references_index < 0) {
567 return nullptr;
568 }
569 return HeapShared::get_root(_archived_references_index);
570 }
571
572 void ConstantPoolCache::clear_archived_references() {
573 if (_archived_references_index >= 0) {
574 HeapShared::clear_root(_archived_references_index);
575 _archived_references_index = -1;
576 }
577 }
578
579 void ConstantPoolCache::set_archived_references(int root_index) {
580 assert(CDSConfig::is_dumping_heap(), "sanity");
581 _archived_references_index = root_index;
582 }
583 #endif
584
585 #if INCLUDE_JVMTI
586 static void log_adjust(const char* entry_type, Method* old_method, Method* new_method, bool* trace_name_printed) {
587 ResourceMark rm;
588
589 if (!(*trace_name_printed)) {
590 log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name());
591 *trace_name_printed = true;
592 }
593 log_trace(redefine, class, update, constantpool)
594 ("cpc %s entry update: %s", entry_type, new_method->external_name());
595 }
596
597 // RedefineClasses() API support:
598 // If any entry of this ConstantPoolCache points to any of
599 // old_methods, replace it with the corresponding new_method.
600 void ConstantPoolCache::adjust_method_entries(bool * trace_name_printed) {
601 if (_resolved_indy_entries != nullptr) {
602 for (int j = 0; j < _resolved_indy_entries->length(); j++) {
603 Method* old_method = resolved_indy_entry_at(j)->method();
604 if (old_method == nullptr || !old_method->is_old()) {
605 continue;
606 }
607 assert(!old_method->is_deleted(), "cannot delete these methods");
608 Method* new_method = old_method->get_new_method();
609 resolved_indy_entry_at(j)->adjust_method_entry(new_method);
610 log_adjust("indy", old_method, new_method, trace_name_printed);
611 }
612 }
613 if (_resolved_method_entries != nullptr) {
614 for (int i = 0; i < _resolved_method_entries->length(); i++) {
615 ResolvedMethodEntry* method_entry = resolved_method_entry_at(i);
616 // get interesting method entry
617 Method* old_method = method_entry->method();
618 if (old_method == nullptr || !old_method->is_old()) {
619 continue; // skip uninteresting entries
620 }
621 if (old_method->is_deleted()) {
622 // clean up entries with deleted methods
623 method_entry->reset_entry();
624 continue;
625 }
626 Method* new_method = old_method->get_new_method();
627 method_entry->adjust_method_entry(new_method);
628 log_adjust("non-indy", old_method, new_method, trace_name_printed);
629 }
630 }
631 }
632
633 // the constant pool cache should never contain old or obsolete methods
634 bool ConstantPoolCache::check_no_old_or_obsolete_entries() {
635 ResourceMark rm;
636 if (_resolved_indy_entries != nullptr) {
637 for (int i = 0; i < _resolved_indy_entries->length(); i++) {
638 Method* m = resolved_indy_entry_at(i)->method();
639 if (m != nullptr && !resolved_indy_entry_at(i)->check_no_old_or_obsolete_entry()) {
640 log_trace(redefine, class, update, constantpool)
641 ("cpcache check found old method entry: class: %s, old: %d, obsolete: %d, method: %s",
642 constant_pool()->pool_holder()->external_name(), m->is_old(), m->is_obsolete(), m->external_name());
643 return false;
644 }
645 }
646 }
647 if (_resolved_method_entries != nullptr) {
648 for (int i = 0; i < _resolved_method_entries->length(); i++) {
649 ResolvedMethodEntry* method_entry = resolved_method_entry_at(i);
650 Method* m = method_entry->method();
651 if (m != nullptr && !method_entry->check_no_old_or_obsolete_entry()) {
652 log_trace(redefine, class, update, constantpool)
653 ("cpcache check found old method entry: class: %s, old: %d, obsolete: %d, method: %s",
654 constant_pool()->pool_holder()->external_name(), m->is_old(), m->is_obsolete(), m->external_name());
655 return false;
656 }
657 }
658 }
659 return true;
660 }
661
662 void ConstantPoolCache::dump_cache() {
663 print_on(tty);
664 }
665 #endif // INCLUDE_JVMTI
666
667 void ConstantPoolCache::metaspace_pointers_do(MetaspaceClosure* it) {
668 log_trace(cds)("Iter(ConstantPoolCache): %p", this);
669 it->push(&_constant_pool);
670 it->push(&_reference_map);
671 if (_resolved_indy_entries != nullptr) {
672 it->push(&_resolved_indy_entries, MetaspaceClosure::_writable);
673 }
674 if (_resolved_field_entries != nullptr) {
675 it->push(&_resolved_field_entries, MetaspaceClosure::_writable);
676 }
677 if (_resolved_method_entries != nullptr) {
678 it->push(&_resolved_method_entries, MetaspaceClosure::_writable);
679 }
680 }
681
682 bool ConstantPoolCache::save_and_throw_indy_exc(
683 const constantPoolHandle& cpool, int cpool_index, int index, constantTag tag, TRAPS) {
684
685 assert(HAS_PENDING_EXCEPTION, "No exception got thrown!");
686 assert(PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass()),
687 "No LinkageError exception");
688
689 // Use the resolved_references() lock for this cpCache entry.
690 // resolved_references are created for all classes with Invokedynamic, MethodHandle
691 // or MethodType constant pool cache entries.
692 JavaThread* current = THREAD;
693 objArrayHandle resolved_references(current, cpool->resolved_references());
694 assert(resolved_references() != nullptr,
695 "a resolved_references array should have been created for this class");
696 ObjectLocker ol(resolved_references, current);
697
698 // if the indy_info is resolved or the indy_resolution_failed flag is set then another
699 // thread either succeeded in resolving the method or got a LinkageError
700 // exception, before this thread was able to record its failure. So, clear
701 // this thread's exception and return false so caller can use the earlier
702 // thread's result.
703 if (resolved_indy_entry_at(index)->is_resolved() || resolved_indy_entry_at(index)->resolution_failed()) {
704 CLEAR_PENDING_EXCEPTION;
705 return false;
706 }
707 ResourceMark rm(THREAD);
708 Symbol* error = PENDING_EXCEPTION->klass()->name();
709 const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION);
710
711 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
712 SystemDictionary::add_resolution_error(cpool, encoded_index, error, message);
713 resolved_indy_entry_at(index)->set_resolution_failed();
714 return true;
715 }
716
717 oop ConstantPoolCache::set_dynamic_call(const CallInfo &call_info, int index) {
718 ResourceMark rm;
719
720 // Use the resolved_references() lock for this cpCache entry.
721 // resolved_references are created for all classes with Invokedynamic, MethodHandle
722 // or MethodType constant pool cache entries.
723 JavaThread* current = JavaThread::current();
724 constantPoolHandle cp(current, constant_pool());
725
726 objArrayHandle resolved_references(current, cp->resolved_references());
727 assert(resolved_references() != nullptr,
728 "a resolved_references array should have been created for this class");
729 ObjectLocker ol(resolved_references, current);
730 assert(index >= 0, "Indy index must be positive at this point");
731
732 if (resolved_indy_entry_at(index)->method() != nullptr) {
733 return cp->resolved_reference_from_indy(index);
734 }
735
736 if (resolved_indy_entry_at(index)->resolution_failed()) {
737 // Before we got here, another thread got a LinkageError exception during
738 // resolution. Ignore our success and throw their exception.
739 guarantee(index >= 0, "Invalid indy index");
740 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
741 ConstantPool::throw_resolution_error(cp, encoded_index, current);
742 return nullptr;
743 }
744
745 Method* adapter = call_info.resolved_method();
746 const Handle appendix = call_info.resolved_appendix();
747 const bool has_appendix = appendix.not_null();
748
749 LogStream* log_stream = nullptr;
750 LogStreamHandle(Debug, methodhandles, indy) lsh_indy;
751 if (lsh_indy.is_enabled()) {
752 ResourceMark rm;
753 log_stream = &lsh_indy;
754 log_stream->print_cr("set_method_handle bc=%d appendix=" PTR_FORMAT "%s method=" PTR_FORMAT " (local signature) ",
755 0xba,
756 p2i(appendix()),
757 (has_appendix ? "" : " (unused)"),
758 p2i(adapter));
759 adapter->print_on(log_stream);
760 if (has_appendix) appendix()->print_on(log_stream);
761 }
762
763 if (has_appendix) {
764 const int appendix_index = resolved_indy_entry_at(index)->resolved_references_index();
765 assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob");
766 assert(resolved_references->obj_at(appendix_index) == nullptr, "init just once");
767 resolved_references->obj_at_put(appendix_index, appendix());
768 }
769
770 // Populate entry with resolved information
771 assert(resolved_indy_entries() != nullptr, "Invokedynamic array is empty, cannot fill with resolved information");
772 resolved_indy_entry_at(index)->fill_in(adapter, adapter->size_of_parameters(), as_TosState(adapter->result_type()), has_appendix);
773
774 if (log_stream != nullptr) {
775 resolved_indy_entry_at(index)->print_on(log_stream);
776 }
777 return appendix();
778 }
779
780 oop ConstantPoolCache::appendix_if_resolved(int method_index) const {
781 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
782 return appendix_if_resolved(method_entry);
783 }
784
785 oop ConstantPoolCache::appendix_if_resolved(ResolvedMethodEntry* method_entry) const {
786 if (!method_entry->has_appendix())
787 return nullptr;
788 const int ref_index = method_entry->resolved_references_index();
789 return constant_pool()->resolved_reference_at(ref_index);
790 }
791
792 // Printing
793
794 void ConstantPoolCache::print_on(outputStream* st) const {
795 st->print_cr("%s", internal_name());
796 // print constant pool cache entries
797 print_resolved_field_entries(st);
798 print_resolved_method_entries(st);
799 print_resolved_indy_entries(st);
800 }
801
802 void ConstantPoolCache::print_resolved_field_entries(outputStream* st) const {
803 for (int field_index = 0; field_index < resolved_field_entries_length(); field_index++) {
804 resolved_field_entry_at(field_index)->print_on(st);
805 }
806 }
807
808 void ConstantPoolCache::print_resolved_method_entries(outputStream* st) const {
809 for (int method_index = 0; method_index < resolved_method_entries_length(); method_index++) {
810 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
811 method_entry->print_on(st);
812 if (method_entry->has_appendix()) {
813 st->print(" appendix: ");
814 constant_pool()->resolved_reference_from_method(method_index)->print_on(st);
815 }
816 }
817 }
818
819 void ConstantPoolCache::print_resolved_indy_entries(outputStream* st) const {
820 for (int indy_index = 0; indy_index < resolved_indy_entries_length(); indy_index++) {
821 ResolvedIndyEntry* indy_entry = resolved_indy_entry_at(indy_index);
822 indy_entry->print_on(st);
823 if (indy_entry->has_appendix()) {
824 st->print(" appendix: ");
825 constant_pool()->resolved_reference_from_indy(indy_index)->print_on(st);
826 }
827 }
828 }
--- EOF ---