Fri, 30 Oct 2009 16:22:59 -0700
6858164: invokedynamic code needs some cleanup (post-6655638)
Summary: Fix several crashers, remove needless paths for boxed-style bootstrap method call, refactor & simplify APIs for rewriter constantPoolOop, remove sun.dyn.CallSiteImpl
Reviewed-by: kvn
1 /*
2 * Copyright 1997-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_linkResolver.cpp.incl"
28 //------------------------------------------------------------------------------------------------------------------------
29 // Implementation of FieldAccessInfo
31 void FieldAccessInfo::set(KlassHandle klass, symbolHandle name, int field_index, int field_offset,
32 BasicType field_type, AccessFlags access_flags) {
33 _klass = klass;
34 _name = name;
35 _field_index = field_index;
36 _field_offset = field_offset;
37 _field_type = field_type;
38 _access_flags = access_flags;
39 }
42 //------------------------------------------------------------------------------------------------------------------------
43 // Implementation of CallInfo
46 void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) {
47 int vtable_index = methodOopDesc::nonvirtual_vtable_index;
48 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, vtable_index, CHECK);
49 }
52 void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, TRAPS) {
53 // This is only called for interface methods. If the resolved_method
54 // comes from java/lang/Object, it can be the subject of a virtual call, so
55 // we should pick the vtable index from the resolved method.
56 // Other than that case, there is no valid vtable index to specify.
57 int vtable_index = methodOopDesc::invalid_vtable_index;
58 if (resolved_method->method_holder() == SystemDictionary::object_klass()) {
59 assert(resolved_method->vtable_index() == selected_method->vtable_index(), "sanity check");
60 vtable_index = resolved_method->vtable_index();
61 }
62 set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK);
63 }
65 void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
66 assert(vtable_index >= 0 || vtable_index == methodOopDesc::nonvirtual_vtable_index, "valid index");
67 set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK);
68 }
70 void CallInfo::set_common(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
71 assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
72 _resolved_klass = resolved_klass;
73 _selected_klass = selected_klass;
74 _resolved_method = resolved_method;
75 _selected_method = selected_method;
76 _vtable_index = vtable_index;
77 if (CompilationPolicy::mustBeCompiled(selected_method)) {
78 // Note: with several active threads, the mustBeCompiled may be true
79 // while canBeCompiled is false; remove assert
80 // assert(CompilationPolicy::canBeCompiled(selected_method), "cannot compile");
81 if (THREAD->is_Compiler_thread()) {
82 // don't force compilation, resolve was on behalf of compiler
83 return;
84 }
85 CompileBroker::compile_method(selected_method, InvocationEntryBci,
86 methodHandle(), 0, "mustBeCompiled", CHECK);
87 }
88 }
91 //------------------------------------------------------------------------------------------------------------------------
92 // Klass resolution
94 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
95 if (!Reflection::verify_class_access(ref_klass->as_klassOop(),
96 sel_klass->as_klassOop(),
97 true)) {
98 ResourceMark rm(THREAD);
99 Exceptions::fthrow(
100 THREAD_AND_LOCATION,
101 vmSymbolHandles::java_lang_IllegalAccessError(),
102 "tried to access class %s from class %s",
103 sel_klass->external_name(),
104 ref_klass->external_name()
105 );
106 return;
107 }
108 }
110 void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
111 klassOop result_oop = pool->klass_ref_at(index, CHECK);
112 result = KlassHandle(THREAD, result_oop);
113 }
115 void LinkResolver::resolve_klass_no_update(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
116 klassOop result_oop =
117 constantPoolOopDesc::klass_ref_at_if_loaded_check(pool, index, CHECK);
118 result = KlassHandle(THREAD, result_oop);
119 }
122 //------------------------------------------------------------------------------------------------------------------------
123 // Method resolution
124 //
125 // According to JVM spec. $5.4.3c & $5.4.3d
127 void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) {
128 methodOop result_oop = klass->uncached_lookup_method(name(), signature());
129 result = methodHandle(THREAD, result_oop);
130 }
132 // returns first instance method
133 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) {
134 methodOop result_oop = klass->uncached_lookup_method(name(), signature());
135 result = methodHandle(THREAD, result_oop);
136 while (!result.is_null() && result->is_static()) {
137 klass = KlassHandle(THREAD, Klass::cast(result->method_holder())->super());
138 result = methodHandle(THREAD, klass->uncached_lookup_method(name(), signature()));
139 }
140 }
143 int LinkResolver::vtable_index_of_miranda_method(KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) {
144 ResourceMark rm(THREAD);
145 klassVtable *vt = instanceKlass::cast(klass())->vtable();
146 return vt->index_of_miranda(name(), signature());
147 }
149 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) {
150 instanceKlass *ik = instanceKlass::cast(klass());
151 result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name(), signature()));
152 }
154 void LinkResolver::lookup_implicit_method(methodHandle& result, KlassHandle klass, symbolHandle name, symbolHandle signature, TRAPS) {
155 if (EnableMethodHandles && MethodHandles::enabled() &&
156 name == vmSymbolHandles::invoke_name() && klass() == SystemDictionary::MethodHandle_klass()) {
157 methodOop result_oop = SystemDictionary::find_method_handle_invoke(signature,
158 Handle(),
159 Handle(),
160 CHECK);
161 if (result_oop != NULL) {
162 assert(result_oop->is_method_handle_invoke() && result_oop->signature() == signature(), "consistent");
163 result = methodHandle(THREAD, result_oop);
164 }
165 }
166 }
168 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
169 KlassHandle resolved_klass,
170 KlassHandle sel_klass,
171 methodHandle sel_method,
172 TRAPS) {
174 AccessFlags flags = sel_method->access_flags();
176 // Special case: arrays always override "clone". JVMS 2.15.
177 // If the resolved klass is an array class, and the declaring class
178 // is java.lang.Object and the method is "clone", set the flags
179 // to public.
180 //
181 // We'll check for the method name first, as that's most likely
182 // to be false (so we'll short-circuit out of these tests).
183 if (sel_method->name() == vmSymbols::clone_name() &&
184 sel_klass() == SystemDictionary::object_klass() &&
185 resolved_klass->oop_is_array()) {
186 // We need to change "protected" to "public".
187 assert(flags.is_protected(), "clone not protected?");
188 jint new_flags = flags.as_int();
189 new_flags = new_flags & (~JVM_ACC_PROTECTED);
190 new_flags = new_flags | JVM_ACC_PUBLIC;
191 flags.set_flags(new_flags);
192 }
194 if (!Reflection::verify_field_access(ref_klass->as_klassOop(),
195 resolved_klass->as_klassOop(),
196 sel_klass->as_klassOop(),
197 flags,
198 true)) {
199 ResourceMark rm(THREAD);
200 Exceptions::fthrow(
201 THREAD_AND_LOCATION,
202 vmSymbolHandles::java_lang_IllegalAccessError(),
203 "tried to access method %s.%s%s from class %s",
204 sel_klass->external_name(),
205 sel_method->name()->as_C_string(),
206 sel_method->signature()->as_C_string(),
207 ref_klass->external_name()
208 );
209 return;
210 }
211 }
213 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle& resolved_klass,
214 constantPoolHandle pool, int index, TRAPS) {
216 // resolve klass
217 resolve_klass(resolved_klass, pool, index, CHECK);
219 symbolHandle method_name (THREAD, pool->name_ref_at(index));
220 symbolHandle method_signature (THREAD, pool->signature_ref_at(index));
221 KlassHandle current_klass(THREAD, pool->pool_holder());
223 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
224 }
226 void LinkResolver::resolve_interface_method(methodHandle& resolved_method, KlassHandle& resolved_klass, constantPoolHandle pool, int index, TRAPS) {
228 // resolve klass
229 resolve_klass(resolved_klass, pool, index, CHECK);
230 symbolHandle method_name (THREAD, pool->name_ref_at(index));
231 symbolHandle method_signature (THREAD, pool->signature_ref_at(index));
232 KlassHandle current_klass(THREAD, pool->pool_holder());
234 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
235 }
238 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
239 symbolHandle method_name, symbolHandle method_signature,
240 KlassHandle current_klass, bool check_access, TRAPS) {
242 // 1. check if klass is not interface
243 if (resolved_klass->is_interface()) {
244 char buf[200];
245 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", Klass::cast(resolved_klass())->external_name());
246 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
247 }
249 // 2. lookup method in resolved klass and its super klasses
250 lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
252 if (resolved_method.is_null()) { // not found in the class hierarchy
253 // 3. lookup method in all the interfaces implemented by the resolved klass
254 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
256 if (resolved_method.is_null()) {
257 // JSR 292: see if this is an implicitly generated method MethodHandle.invoke(*...)
258 lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, CHECK);
259 }
261 if (resolved_method.is_null()) {
262 // 4. method lookup failed
263 ResourceMark rm(THREAD);
264 THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
265 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
266 method_name(),
267 method_signature()));
268 }
269 }
271 // 5. check if method is concrete
272 if (resolved_method->is_abstract() && !resolved_klass->is_abstract()) {
273 ResourceMark rm(THREAD);
274 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
275 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
276 method_name(),
277 method_signature()));
278 }
280 // 6. access checks, access checking may be turned off when calling from within the VM.
281 if (check_access) {
282 assert(current_klass.not_null() , "current_klass should not be null");
284 // check if method can be accessed by the referring class
285 check_method_accessability(current_klass,
286 resolved_klass,
287 KlassHandle(THREAD, resolved_method->method_holder()),
288 resolved_method,
289 CHECK);
291 // check loader constraints
292 Handle loader (THREAD, instanceKlass::cast(current_klass())->class_loader());
293 Handle class_loader (THREAD, instanceKlass::cast(resolved_method->method_holder())->class_loader());
294 {
295 ResourceMark rm(THREAD);
296 char* failed_type_name =
297 SystemDictionary::check_signature_loaders(method_signature, loader,
298 class_loader, true, CHECK);
299 if (failed_type_name != NULL) {
300 const char* msg = "loader constraint violation: when resolving method"
301 " \"%s\" the class loader (instance of %s) of the current class, %s,"
302 " and the class loader (instance of %s) for resolved class, %s, have"
303 " different Class objects for the type %s used in the signature";
304 char* sig = methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),method_name(),method_signature());
305 const char* loader1 = SystemDictionary::loader_name(loader());
306 char* current = instanceKlass::cast(current_klass())->name()->as_C_string();
307 const char* loader2 = SystemDictionary::loader_name(class_loader());
308 char* resolved = instanceKlass::cast(resolved_klass())->name()->as_C_string();
309 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
310 strlen(current) + strlen(loader2) + strlen(resolved) +
311 strlen(failed_type_name);
312 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
313 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
314 resolved, failed_type_name);
315 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
316 }
317 }
318 }
319 }
321 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
322 KlassHandle resolved_klass,
323 symbolHandle method_name,
324 symbolHandle method_signature,
325 KlassHandle current_klass,
326 bool check_access, TRAPS) {
328 // check if klass is interface
329 if (!resolved_klass->is_interface()) {
330 char buf[200];
331 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", Klass::cast(resolved_klass())->external_name());
332 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
333 }
335 // lookup method in this interface or its super, java.lang.Object
336 lookup_instance_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
338 if (resolved_method.is_null()) {
339 // lookup method in all the super-interfaces
340 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
341 if (resolved_method.is_null()) {
342 // no method found
343 ResourceMark rm(THREAD);
344 THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
345 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
346 method_name(),
347 method_signature()));
348 }
349 }
351 if (check_access) {
352 HandleMark hm(THREAD);
353 Handle loader (THREAD, instanceKlass::cast(current_klass())->class_loader());
354 Handle class_loader (THREAD, instanceKlass::cast(resolved_method->method_holder())->class_loader());
355 {
356 ResourceMark rm(THREAD);
357 char* failed_type_name =
358 SystemDictionary::check_signature_loaders(method_signature, loader,
359 class_loader, true, CHECK);
360 if (failed_type_name != NULL) {
361 const char* msg = "loader constraint violation: when resolving "
362 "interface method \"%s\" the class loader (instance of %s) of the "
363 "current class, %s, and the class loader (instance of %s) for "
364 "resolved class, %s, have different Class objects for the type %s "
365 "used in the signature";
366 char* sig = methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),method_name(),method_signature());
367 const char* loader1 = SystemDictionary::loader_name(loader());
368 char* current = instanceKlass::cast(current_klass())->name()->as_C_string();
369 const char* loader2 = SystemDictionary::loader_name(class_loader());
370 char* resolved = instanceKlass::cast(resolved_klass())->name()->as_C_string();
371 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
372 strlen(current) + strlen(loader2) + strlen(resolved) +
373 strlen(failed_type_name);
374 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
375 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
376 resolved, failed_type_name);
377 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
378 }
379 }
380 }
381 }
383 //------------------------------------------------------------------------------------------------------------------------
384 // Field resolution
386 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
387 KlassHandle resolved_klass,
388 KlassHandle sel_klass,
389 fieldDescriptor& fd,
390 TRAPS) {
391 if (!Reflection::verify_field_access(ref_klass->as_klassOop(),
392 resolved_klass->as_klassOop(),
393 sel_klass->as_klassOop(),
394 fd.access_flags(),
395 true)) {
396 ResourceMark rm(THREAD);
397 Exceptions::fthrow(
398 THREAD_AND_LOCATION,
399 vmSymbolHandles::java_lang_IllegalAccessError(),
400 "tried to access field %s.%s from class %s",
401 sel_klass->external_name(),
402 fd.name()->as_C_string(),
403 ref_klass->external_name()
404 );
405 return;
406 }
407 }
409 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, TRAPS) {
410 resolve_field(result, pool, index, byte, check_only, true, CHECK);
411 }
413 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, bool update_pool, TRAPS) {
414 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
415 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield, "bad bytecode");
417 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
418 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic);
420 // resolve specified klass
421 KlassHandle resolved_klass;
422 if (update_pool) {
423 resolve_klass(resolved_klass, pool, index, CHECK);
424 } else {
425 resolve_klass_no_update(resolved_klass, pool, index, CHECK);
426 }
427 // Load these early in case the resolve of the containing klass fails
428 symbolOop field = pool->name_ref_at(index);
429 symbolHandle field_h (THREAD, field); // preserve in case we need the name
430 symbolOop sig = pool->signature_ref_at(index);
431 // Check if there's a resolved klass containing the field
432 if( resolved_klass.is_null() ) {
433 ResourceMark rm(THREAD);
434 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
435 }
437 // Resolve instance field
438 fieldDescriptor fd; // find_field initializes fd if found
439 KlassHandle sel_klass(THREAD, instanceKlass::cast(resolved_klass())->find_field(field, sig, &fd));
440 // check if field exists; i.e., if a klass containing the field def has been selected
441 if (sel_klass.is_null()){
442 ResourceMark rm(THREAD);
443 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
444 }
446 // check access
447 KlassHandle ref_klass(THREAD, pool->pool_holder());
448 check_field_accessability(ref_klass, resolved_klass, sel_klass, fd, CHECK);
450 // check for errors
451 if (is_static != fd.is_static()) {
452 char msg[200];
453 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", Klass::cast(resolved_klass())->external_name(), fd.name()->as_C_string());
454 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
455 }
457 // Final fields can only be accessed from its own class.
458 if (is_put && fd.access_flags().is_final() && sel_klass() != pool->pool_holder()) {
459 THROW(vmSymbols::java_lang_IllegalAccessError());
460 }
462 // initialize resolved_klass if necessary
463 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
464 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
465 //
466 // note 2: we don't want to force initialization if we are just checking
467 // if the field access is legal; e.g., during compilation
468 if (is_static && !check_only) {
469 sel_klass->initialize(CHECK);
470 }
472 {
473 HandleMark hm(THREAD);
474 Handle ref_loader (THREAD, instanceKlass::cast(ref_klass())->class_loader());
475 Handle sel_loader (THREAD, instanceKlass::cast(sel_klass())->class_loader());
476 symbolHandle signature_ref (THREAD, pool->signature_ref_at(index));
477 {
478 ResourceMark rm(THREAD);
479 char* failed_type_name =
480 SystemDictionary::check_signature_loaders(signature_ref,
481 ref_loader, sel_loader,
482 false,
483 CHECK);
484 if (failed_type_name != NULL) {
485 const char* msg = "loader constraint violation: when resolving field"
486 " \"%s\" the class loader (instance of %s) of the referring class, "
487 "%s, and the class loader (instance of %s) for the field's resolved "
488 "type, %s, have different Class objects for that type";
489 char* field_name = field_h()->as_C_string();
490 const char* loader1 = SystemDictionary::loader_name(ref_loader());
491 char* sel = instanceKlass::cast(sel_klass())->name()->as_C_string();
492 const char* loader2 = SystemDictionary::loader_name(sel_loader());
493 size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
494 strlen(sel) + strlen(loader2) + strlen(failed_type_name);
495 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
496 jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
497 failed_type_name);
498 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
499 }
500 }
501 }
503 // return information. note that the klass is set to the actual klass containing the
504 // field, otherwise access of static fields in superclasses will not work.
505 KlassHandle holder (THREAD, fd.field_holder());
506 symbolHandle name (THREAD, fd.name());
507 result.set(holder, name, fd.index(), fd.offset(), fd.field_type(), fd.access_flags());
508 }
511 //------------------------------------------------------------------------------------------------------------------------
512 // Invoke resolution
513 //
514 // Naming conventions:
515 //
516 // resolved_method the specified method (i.e., static receiver specified via constant pool index)
517 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
518 // resolved_klass the specified klass (i.e., specified via constant pool index)
519 // recv_klass the receiver klass
522 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, symbolHandle method_name,
523 symbolHandle method_signature, KlassHandle current_klass,
524 bool check_access, bool initialize_class, TRAPS) {
525 methodHandle resolved_method;
526 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
527 resolved_klass = KlassHandle(THREAD, Klass::cast(resolved_method->method_holder()));
529 // Initialize klass (this should only happen if everything is ok)
530 if (initialize_class && resolved_klass->should_be_initialized()) {
531 resolved_klass->initialize(CHECK);
532 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
533 }
535 // setup result
536 result.set_static(resolved_klass, resolved_method, CHECK);
537 }
539 // throws linktime exceptions
540 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
541 symbolHandle method_name, symbolHandle method_signature,
542 KlassHandle current_klass, bool check_access, TRAPS) {
544 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
545 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
547 // check if static
548 if (!resolved_method->is_static()) {
549 char buf[200];
550 jio_snprintf(buf, sizeof(buf), "Expected static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
551 resolved_method->name(),
552 resolved_method->signature()));
553 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
554 }
555 }
558 void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, symbolHandle method_name,
559 symbolHandle method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
560 methodHandle resolved_method;
561 linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
562 runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK);
563 }
565 // throws linktime exceptions
566 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
567 symbolHandle method_name, symbolHandle method_signature,
568 KlassHandle current_klass, bool check_access, TRAPS) {
570 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
572 // check if method name is <init>, that it is found in same klass as static type
573 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
574 resolved_method->method_holder() != resolved_klass()) {
575 ResourceMark rm(THREAD);
576 Exceptions::fthrow(
577 THREAD_AND_LOCATION,
578 vmSymbolHandles::java_lang_NoSuchMethodError(),
579 "%s: method %s%s not found",
580 resolved_klass->external_name(),
581 resolved_method->name()->as_C_string(),
582 resolved_method->signature()->as_C_string()
583 );
584 return;
585 }
587 // check if not static
588 if (resolved_method->is_static()) {
589 char buf[200];
590 jio_snprintf(buf, sizeof(buf),
591 "Expecting non-static method %s",
592 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
593 resolved_method->name(),
594 resolved_method->signature()));
595 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
596 }
597 }
599 // throws runtime exceptions
600 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
601 KlassHandle current_klass, bool check_access, TRAPS) {
603 // resolved method is selected method unless we have an old-style lookup
604 methodHandle sel_method(THREAD, resolved_method());
606 // check if this is an old-style super call and do a new lookup if so
607 { KlassHandle method_klass = KlassHandle(THREAD,
608 resolved_method->method_holder());
610 if (check_access &&
611 // a) check if ACC_SUPER flag is set for the current class
612 current_klass->is_super() &&
613 // b) check if the method class is a superclass of the current class (superclass relation is not reflexive!)
614 current_klass->is_subtype_of(method_klass()) && current_klass() != method_klass() &&
615 // c) check if the method is not <init>
616 resolved_method->name() != vmSymbols::object_initializer_name()) {
617 // Lookup super method
618 KlassHandle super_klass(THREAD, current_klass->super());
619 lookup_instance_method_in_klasses(sel_method, super_klass,
620 symbolHandle(THREAD, resolved_method->name()),
621 symbolHandle(THREAD, resolved_method->signature()), CHECK);
622 // check if found
623 if (sel_method.is_null()) {
624 ResourceMark rm(THREAD);
625 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
626 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
627 resolved_method->name(),
628 resolved_method->signature()));
629 }
630 }
631 }
633 // check if not static
634 if (sel_method->is_static()) {
635 char buf[200];
636 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
637 resolved_method->name(),
638 resolved_method->signature()));
639 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
640 }
642 // check if abstract
643 if (sel_method->is_abstract()) {
644 ResourceMark rm(THREAD);
645 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
646 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
647 sel_method->name(),
648 sel_method->signature()));
649 }
651 // setup result
652 result.set_static(resolved_klass, sel_method, CHECK);
653 }
655 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
656 symbolHandle method_name, symbolHandle method_signature, KlassHandle current_klass,
657 bool check_access, bool check_null_and_abstract, TRAPS) {
658 methodHandle resolved_method;
659 linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
660 runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
661 }
663 // throws linktime exceptions
664 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
665 symbolHandle method_name, symbolHandle method_signature,
666 KlassHandle current_klass, bool check_access, TRAPS) {
667 // normal method resolution
668 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
670 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
671 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
673 // check if not static
674 if (resolved_method->is_static()) {
675 char buf[200];
676 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
677 resolved_method->name(),
678 resolved_method->signature()));
679 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
680 }
681 }
683 // throws runtime exceptions
684 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
685 methodHandle resolved_method,
686 KlassHandle resolved_klass,
687 Handle recv,
688 KlassHandle recv_klass,
689 bool check_null_and_abstract,
690 TRAPS) {
692 // setup default return values
693 int vtable_index = methodOopDesc::invalid_vtable_index;
694 methodHandle selected_method;
696 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
698 // runtime method resolution
699 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
700 THROW(vmSymbols::java_lang_NullPointerException());
701 }
703 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the methodOop's
704 // has not been rewritten, and the vtable initialized.
705 assert(instanceKlass::cast(resolved_method->method_holder())->is_linked(), "must be linked");
707 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the methodOop's
708 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
709 // a missing receiver might result in a bogus lookup.
710 assert(instanceKlass::cast(resolved_method->method_holder())->is_linked(), "must be linked");
712 // do lookup based on receiver klass using the vtable index
713 if (resolved_method->method_holder()->klass_part()->is_interface()) { // miranda method
714 vtable_index = vtable_index_of_miranda_method(resolved_klass,
715 symbolHandle(THREAD, resolved_method->name()),
716 symbolHandle(THREAD, resolved_method->signature()), CHECK);
717 assert(vtable_index >= 0 , "we should have valid vtable index at this point");
719 instanceKlass* inst = instanceKlass::cast(recv_klass());
720 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
721 } else {
722 // at this point we are sure that resolved_method is virtual and not
723 // a miranda method; therefore, it must have a valid vtable index.
724 vtable_index = resolved_method->vtable_index();
725 // We could get a negative vtable_index for final methods,
726 // because as an optimization they are they are never put in the vtable,
727 // unless they override an existing method.
728 // If we do get a negative, it means the resolved method is the the selected
729 // method, and it can never be changed by an override.
730 if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
731 assert(resolved_method->can_be_statically_bound(), "cannot override this method");
732 selected_method = resolved_method;
733 } else {
734 // recv_klass might be an arrayKlassOop but all vtables start at
735 // the same place. The cast is to avoid virtual call and assertion.
736 instanceKlass* inst = (instanceKlass*)recv_klass()->klass_part();
737 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
738 }
739 }
741 // check if method exists
742 if (selected_method.is_null()) {
743 ResourceMark rm(THREAD);
744 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
745 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
746 resolved_method->name(),
747 resolved_method->signature()));
748 }
750 // check if abstract
751 if (check_null_and_abstract && selected_method->is_abstract()) {
752 ResourceMark rm(THREAD);
753 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
754 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
755 selected_method->name(),
756 selected_method->signature()));
757 }
759 // setup result
760 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
761 }
763 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
764 symbolHandle method_name, symbolHandle method_signature, KlassHandle current_klass,
765 bool check_access, bool check_null_and_abstract, TRAPS) {
766 methodHandle resolved_method;
767 linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
768 runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
769 }
771 // throws linktime exceptions
772 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, symbolHandle method_name,
773 symbolHandle method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
774 // normal interface method resolution
775 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
777 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
778 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
779 }
781 // throws runtime exceptions
782 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
783 Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
784 // check if receiver exists
785 if (check_null_and_abstract && recv.is_null()) {
786 THROW(vmSymbols::java_lang_NullPointerException());
787 }
789 // check if receiver klass implements the resolved interface
790 if (!recv_klass->is_subtype_of(resolved_klass())) {
791 char buf[200];
792 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
793 (Klass::cast(recv_klass()))->external_name(),
794 (Klass::cast(resolved_klass()))->external_name());
795 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
796 }
797 // do lookup based on receiver klass
798 methodHandle sel_method;
799 lookup_instance_method_in_klasses(sel_method, recv_klass,
800 symbolHandle(THREAD, resolved_method->name()),
801 symbolHandle(THREAD, resolved_method->signature()), CHECK);
802 // check if method exists
803 if (sel_method.is_null()) {
804 ResourceMark rm(THREAD);
805 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
806 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
807 resolved_method->name(),
808 resolved_method->signature()));
809 }
810 // check if public
811 if (!sel_method->is_public()) {
812 ResourceMark rm(THREAD);
813 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
814 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
815 sel_method->name(),
816 sel_method->signature()));
817 }
818 // check if abstract
819 if (check_null_and_abstract && sel_method->is_abstract()) {
820 ResourceMark rm(THREAD);
821 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
822 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
823 sel_method->name(),
824 sel_method->signature()));
825 }
826 // setup result
827 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, CHECK);
828 }
831 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
832 KlassHandle resolved_klass,
833 symbolHandle method_name,
834 symbolHandle method_signature,
835 KlassHandle current_klass,
836 bool check_access) {
837 EXCEPTION_MARK;
838 methodHandle method_result;
839 linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
840 if (HAS_PENDING_EXCEPTION) {
841 CLEAR_PENDING_EXCEPTION;
842 return methodHandle();
843 } else {
844 return method_result;
845 }
846 }
848 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
849 KlassHandle resolved_klass,
850 symbolHandle method_name,
851 symbolHandle method_signature,
852 KlassHandle current_klass,
853 bool check_access) {
854 EXCEPTION_MARK;
855 methodHandle method_result;
856 linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
857 if (HAS_PENDING_EXCEPTION) {
858 CLEAR_PENDING_EXCEPTION;
859 return methodHandle();
860 } else {
861 return method_result;
862 }
863 }
865 methodHandle LinkResolver::resolve_virtual_call_or_null(
866 KlassHandle receiver_klass,
867 KlassHandle resolved_klass,
868 symbolHandle name,
869 symbolHandle signature,
870 KlassHandle current_klass) {
871 EXCEPTION_MARK;
872 CallInfo info;
873 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
874 if (HAS_PENDING_EXCEPTION) {
875 CLEAR_PENDING_EXCEPTION;
876 return methodHandle();
877 }
878 return info.selected_method();
879 }
881 methodHandle LinkResolver::resolve_interface_call_or_null(
882 KlassHandle receiver_klass,
883 KlassHandle resolved_klass,
884 symbolHandle name,
885 symbolHandle signature,
886 KlassHandle current_klass) {
887 EXCEPTION_MARK;
888 CallInfo info;
889 resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
890 if (HAS_PENDING_EXCEPTION) {
891 CLEAR_PENDING_EXCEPTION;
892 return methodHandle();
893 }
894 return info.selected_method();
895 }
897 int LinkResolver::resolve_virtual_vtable_index(
898 KlassHandle receiver_klass,
899 KlassHandle resolved_klass,
900 symbolHandle name,
901 symbolHandle signature,
902 KlassHandle current_klass) {
903 EXCEPTION_MARK;
904 CallInfo info;
905 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
906 if (HAS_PENDING_EXCEPTION) {
907 CLEAR_PENDING_EXCEPTION;
908 return methodOopDesc::invalid_vtable_index;
909 }
910 return info.vtable_index();
911 }
913 methodHandle LinkResolver::resolve_static_call_or_null(
914 KlassHandle resolved_klass,
915 symbolHandle name,
916 symbolHandle signature,
917 KlassHandle current_klass) {
918 EXCEPTION_MARK;
919 CallInfo info;
920 resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD);
921 if (HAS_PENDING_EXCEPTION) {
922 CLEAR_PENDING_EXCEPTION;
923 return methodHandle();
924 }
925 return info.selected_method();
926 }
928 methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, symbolHandle name, symbolHandle signature,
929 KlassHandle current_klass) {
930 EXCEPTION_MARK;
931 CallInfo info;
932 resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD);
933 if (HAS_PENDING_EXCEPTION) {
934 CLEAR_PENDING_EXCEPTION;
935 return methodHandle();
936 }
937 return info.selected_method();
938 }
942 //------------------------------------------------------------------------------------------------------------------------
943 // ConstantPool entries
945 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
946 switch (byte) {
947 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
948 case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break;
949 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
950 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
951 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
952 }
953 return;
954 }
956 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, symbolHandle& method_name, symbolHandle& method_signature,
957 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
958 // resolve klass
959 resolve_klass(resolved_klass, pool, index, CHECK);
961 // Get name, signature, and static klass
962 method_name = symbolHandle(THREAD, pool->name_ref_at(index));
963 method_signature = symbolHandle(THREAD, pool->signature_ref_at(index));
964 current_klass = KlassHandle(THREAD, pool->pool_holder());
965 }
968 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
969 KlassHandle resolved_klass;
970 symbolHandle method_name;
971 symbolHandle method_signature;
972 KlassHandle current_klass;
973 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
974 resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
975 }
978 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
979 KlassHandle resolved_klass;
980 symbolHandle method_name;
981 symbolHandle method_signature;
982 KlassHandle current_klass;
983 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
984 resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
985 }
988 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
989 constantPoolHandle pool, int index,
990 TRAPS) {
992 KlassHandle resolved_klass;
993 symbolHandle method_name;
994 symbolHandle method_signature;
995 KlassHandle current_klass;
996 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
997 KlassHandle recvrKlass (THREAD, recv.is_null() ? (klassOop)NULL : recv->klass());
998 resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
999 }
1002 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
1003 KlassHandle resolved_klass;
1004 symbolHandle method_name;
1005 symbolHandle method_signature;
1006 KlassHandle current_klass;
1007 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1008 KlassHandle recvrKlass (THREAD, recv.is_null() ? (klassOop)NULL : recv->klass());
1009 resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1010 }
1013 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int raw_index, TRAPS) {
1014 assert(EnableInvokeDynamic, "");
1016 // This guy is reached from InterpreterRuntime::resolve_invokedynamic.
1018 // At this point, we only need the signature, and can ignore the name.
1019 symbolHandle method_signature(THREAD, pool->signature_ref_at(raw_index)); // raw_index works directly
1020 symbolHandle method_name = vmSymbolHandles::invoke_name();
1021 KlassHandle resolved_klass = SystemDictionaryHandles::MethodHandle_klass();
1023 // JSR 292: this must be an implicitly generated method MethodHandle.invoke(*...)
1024 // The extra MH receiver will be inserted into the stack on every call.
1025 methodHandle resolved_method;
1026 lookup_implicit_method(resolved_method, resolved_klass, method_name, method_signature, CHECK);
1027 if (resolved_method.is_null()) {
1028 THROW(vmSymbols::java_lang_InternalError());
1029 }
1030 result.set_virtual(resolved_klass, KlassHandle(), resolved_method, resolved_method, resolved_method->vtable_index(), CHECK);
1031 }
1033 //------------------------------------------------------------------------------------------------------------------------
1034 #ifndef PRODUCT
1036 void FieldAccessInfo::print() {
1037 ResourceMark rm;
1038 tty->print_cr("Field %s@%d", name()->as_C_string(), field_offset());
1039 }
1041 #endif