comparison src/share/vm/interpreter/linkResolver.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children e5b0439ef4ae
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 1997-2007 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 */
24
25 #include "incls/_precompiled.incl"
26 #include "incls/_linkResolver.cpp.incl"
27
28 //------------------------------------------------------------------------------------------------------------------------
29 // Implementation of FieldAccessInfo
30
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 }
40
41
42 //------------------------------------------------------------------------------------------------------------------------
43 // Implementation of CallInfo
44
45
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 }
50
51
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 }
64
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 }
69
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 }
89
90
91 //------------------------------------------------------------------------------------------------------------------------
92 // Klass resolution
93
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 }
109
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 }
114
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 }
120
121
122 //------------------------------------------------------------------------------------------------------------------------
123 // Method resolution
124 //
125 // According to JVM spec. $5.4.3c & $5.4.3d
126
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 }
131
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 }
141
142
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 }
148
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 }
153
154 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
155 KlassHandle resolved_klass,
156 KlassHandle sel_klass,
157 methodHandle sel_method,
158 TRAPS) {
159
160 AccessFlags flags = sel_method->access_flags();
161
162 // Special case: arrays always override "clone". JVMS 2.15.
163 // If the resolved klass is an array class, and the declaring class
164 // is java.lang.Object and the method is "clone", set the flags
165 // to public.
166 //
167 // We'll check for the method name first, as that's most likely
168 // to be false (so we'll short-circuit out of these tests).
169 if (sel_method->name() == vmSymbols::clone_name() &&
170 sel_klass() == SystemDictionary::object_klass() &&
171 resolved_klass->oop_is_array()) {
172 // We need to change "protected" to "public".
173 assert(flags.is_protected(), "clone not protected?");
174 jint new_flags = flags.as_int();
175 new_flags = new_flags & (~JVM_ACC_PROTECTED);
176 new_flags = new_flags | JVM_ACC_PUBLIC;
177 flags.set_flags(new_flags);
178 }
179
180 if (!Reflection::verify_field_access(ref_klass->as_klassOop(),
181 resolved_klass->as_klassOop(),
182 sel_klass->as_klassOop(),
183 flags,
184 true)) {
185 ResourceMark rm(THREAD);
186 Exceptions::fthrow(
187 THREAD_AND_LOCATION,
188 vmSymbolHandles::java_lang_IllegalAccessError(),
189 "tried to access method %s.%s%s from class %s",
190 sel_klass->external_name(),
191 sel_method->name()->as_C_string(),
192 sel_method->signature()->as_C_string(),
193 ref_klass->external_name()
194 );
195 return;
196 }
197 }
198
199 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle& resolved_klass,
200 constantPoolHandle pool, int index, TRAPS) {
201
202 // resolve klass
203 resolve_klass(resolved_klass, pool, index, CHECK);
204
205 symbolHandle method_name (THREAD, pool->name_ref_at(index));
206 symbolHandle method_signature (THREAD, pool->signature_ref_at(index));
207 KlassHandle current_klass(THREAD, pool->pool_holder());
208
209 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
210 }
211
212 void LinkResolver::resolve_interface_method(methodHandle& resolved_method, KlassHandle& resolved_klass, constantPoolHandle pool, int index, TRAPS) {
213
214 // resolve klass
215 resolve_klass(resolved_klass, pool, index, CHECK);
216 symbolHandle method_name (THREAD, pool->name_ref_at(index));
217 symbolHandle method_signature (THREAD, pool->signature_ref_at(index));
218 KlassHandle current_klass(THREAD, pool->pool_holder());
219
220 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
221 }
222
223
224 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
225 symbolHandle method_name, symbolHandle method_signature,
226 KlassHandle current_klass, bool check_access, TRAPS) {
227
228 // 1. check if klass is not interface
229 if (resolved_klass->is_interface()) {
230 char buf[200];
231 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", Klass::cast(resolved_klass())->external_name());
232 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
233 }
234
235 // 2. lookup method in resolved klass and its super klasses
236 lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
237
238 if (resolved_method.is_null()) { // not found in the class hierarchy
239 // 3. lookup method in all the interfaces implemented by the resolved klass
240 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
241
242 if (resolved_method.is_null()) {
243 // 4. method lookup failed
244 ResourceMark rm(THREAD);
245 THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
246 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
247 method_name(),
248 method_signature()));
249 }
250 }
251
252 // 5. check if method is concrete
253 if (resolved_method->is_abstract() && !resolved_klass->is_abstract()) {
254 ResourceMark rm(THREAD);
255 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
256 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
257 method_name(),
258 method_signature()));
259 }
260
261 // 6. access checks, access checking may be turned off when calling from within the VM.
262 if (check_access) {
263 assert(current_klass.not_null() , "current_klass should not be null");
264
265 // check if method can be accessed by the referring class
266 check_method_accessability(current_klass,
267 resolved_klass,
268 KlassHandle(THREAD, resolved_method->method_holder()),
269 resolved_method,
270 CHECK);
271
272 // check loader constraints
273 Handle loader (THREAD, instanceKlass::cast(current_klass())->class_loader());
274 Handle class_loader (THREAD, instanceKlass::cast(resolved_method->method_holder())->class_loader());
275 {
276 ResourceMark rm(THREAD);
277 char* failed_type_name =
278 SystemDictionary::check_signature_loaders(method_signature, loader,
279 class_loader, true, CHECK);
280 if (failed_type_name != NULL) {
281 const char* msg = "loader constraint violation: when resolving method"
282 " \"%s\" the class loader (instance of %s) of the current class, %s,"
283 " and the class loader (instance of %s) for resolved class, %s, have"
284 " different Class objects for the type %s used in the signature";
285 char* sig = methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),method_name(),method_signature());
286 const char* loader1 = SystemDictionary::loader_name(loader());
287 char* current = instanceKlass::cast(current_klass())->name()->as_C_string();
288 const char* loader2 = SystemDictionary::loader_name(class_loader());
289 char* resolved = instanceKlass::cast(resolved_klass())->name()->as_C_string();
290 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
291 strlen(current) + strlen(loader2) + strlen(resolved) +
292 strlen(failed_type_name);
293 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
294 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
295 resolved, failed_type_name);
296 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
297 }
298 }
299 }
300 }
301
302 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
303 KlassHandle resolved_klass,
304 symbolHandle method_name,
305 symbolHandle method_signature,
306 KlassHandle current_klass,
307 bool check_access, TRAPS) {
308
309 // check if klass is interface
310 if (!resolved_klass->is_interface()) {
311 char buf[200];
312 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", Klass::cast(resolved_klass())->external_name());
313 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
314 }
315
316 // lookup method in this interface or its super, java.lang.Object
317 lookup_instance_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
318
319 if (resolved_method.is_null()) {
320 // lookup method in all the super-interfaces
321 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
322 if (resolved_method.is_null()) {
323 // no method found
324 ResourceMark rm(THREAD);
325 THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
326 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
327 method_name(),
328 method_signature()));
329 }
330 }
331
332 if (check_access) {
333 HandleMark hm(THREAD);
334 Handle loader (THREAD, instanceKlass::cast(current_klass())->class_loader());
335 Handle class_loader (THREAD, instanceKlass::cast(resolved_method->method_holder())->class_loader());
336 {
337 ResourceMark rm(THREAD);
338 char* failed_type_name =
339 SystemDictionary::check_signature_loaders(method_signature, loader,
340 class_loader, true, CHECK);
341 if (failed_type_name != NULL) {
342 const char* msg = "loader constraint violation: when resolving "
343 "interface method \"%s\" the class loader (instance of %s) of the "
344 "current class, %s, and the class loader (instance of %s) for "
345 "resolved class, %s, have different Class objects for the type %s "
346 "used in the signature";
347 char* sig = methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),method_name(),method_signature());
348 const char* loader1 = SystemDictionary::loader_name(loader());
349 char* current = instanceKlass::cast(current_klass())->name()->as_C_string();
350 const char* loader2 = SystemDictionary::loader_name(class_loader());
351 char* resolved = instanceKlass::cast(resolved_klass())->name()->as_C_string();
352 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
353 strlen(current) + strlen(loader2) + strlen(resolved) +
354 strlen(failed_type_name);
355 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
356 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
357 resolved, failed_type_name);
358 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
359 }
360 }
361 }
362 }
363
364 //------------------------------------------------------------------------------------------------------------------------
365 // Field resolution
366
367 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
368 KlassHandle resolved_klass,
369 KlassHandle sel_klass,
370 fieldDescriptor& fd,
371 TRAPS) {
372 if (!Reflection::verify_field_access(ref_klass->as_klassOop(),
373 resolved_klass->as_klassOop(),
374 sel_klass->as_klassOop(),
375 fd.access_flags(),
376 true)) {
377 ResourceMark rm(THREAD);
378 Exceptions::fthrow(
379 THREAD_AND_LOCATION,
380 vmSymbolHandles::java_lang_IllegalAccessError(),
381 "tried to access field %s.%s from class %s",
382 sel_klass->external_name(),
383 fd.name()->as_C_string(),
384 ref_klass->external_name()
385 );
386 return;
387 }
388 }
389
390 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, TRAPS) {
391 resolve_field(result, pool, index, byte, check_only, true, CHECK);
392 }
393
394 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, bool update_pool, TRAPS) {
395 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
396 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield, "bad bytecode");
397
398 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
399 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic);
400
401 // resolve specified klass
402 KlassHandle resolved_klass;
403 if (update_pool) {
404 resolve_klass(resolved_klass, pool, index, CHECK);
405 } else {
406 resolve_klass_no_update(resolved_klass, pool, index, CHECK);
407 }
408 // Load these early in case the resolve of the containing klass fails
409 symbolOop field = pool->name_ref_at(index);
410 symbolHandle field_h (THREAD, field); // preserve in case we need the name
411 symbolOop sig = pool->signature_ref_at(index);
412 // Check if there's a resolved klass containing the field
413 if( resolved_klass.is_null() ) {
414 ResourceMark rm(THREAD);
415 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
416 }
417
418 // Resolve instance field
419 fieldDescriptor fd; // find_field initializes fd if found
420 KlassHandle sel_klass(THREAD, instanceKlass::cast(resolved_klass())->find_field(field, sig, &fd));
421 // check if field exists; i.e., if a klass containing the field def has been selected
422 if (sel_klass.is_null()){
423 ResourceMark rm(THREAD);
424 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
425 }
426
427 // check access
428 KlassHandle ref_klass(THREAD, pool->pool_holder());
429 check_field_accessability(ref_klass, resolved_klass, sel_klass, fd, CHECK);
430
431 // check for errors
432 if (is_static != fd.is_static()) {
433 char msg[200];
434 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());
435 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
436 }
437
438 // Final fields can only be accessed from its own class.
439 if (is_put && fd.access_flags().is_final() && sel_klass() != pool->pool_holder()) {
440 THROW(vmSymbols::java_lang_IllegalAccessError());
441 }
442
443 // initialize resolved_klass if necessary
444 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
445 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
446 //
447 // note 2: we don't want to force initialization if we are just checking
448 // if the field access is legal; e.g., during compilation
449 if (is_static && !check_only) {
450 sel_klass->initialize(CHECK);
451 }
452
453 {
454 HandleMark hm(THREAD);
455 Handle ref_loader (THREAD, instanceKlass::cast(ref_klass())->class_loader());
456 Handle sel_loader (THREAD, instanceKlass::cast(sel_klass())->class_loader());
457 symbolHandle signature_ref (THREAD, pool->signature_ref_at(index));
458 {
459 ResourceMark rm(THREAD);
460 char* failed_type_name =
461 SystemDictionary::check_signature_loaders(signature_ref,
462 ref_loader, sel_loader,
463 false,
464 CHECK);
465 if (failed_type_name != NULL) {
466 const char* msg = "loader constraint violation: when resolving field"
467 " \"%s\" the class loader (instance of %s) of the referring class, "
468 "%s, and the class loader (instance of %s) for the field's resolved "
469 "type, %s, have different Class objects for that type";
470 char* field_name = field_h()->as_C_string();
471 const char* loader1 = SystemDictionary::loader_name(ref_loader());
472 char* sel = instanceKlass::cast(sel_klass())->name()->as_C_string();
473 const char* loader2 = SystemDictionary::loader_name(sel_loader());
474 size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
475 strlen(sel) + strlen(loader2) + strlen(failed_type_name);
476 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
477 jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
478 failed_type_name);
479 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
480 }
481 }
482 }
483
484 // return information. note that the klass is set to the actual klass containing the
485 // field, otherwise access of static fields in superclasses will not work.
486 KlassHandle holder (THREAD, fd.field_holder());
487 symbolHandle name (THREAD, fd.name());
488 result.set(holder, name, fd.index(), fd.offset(), fd.field_type(), fd.access_flags());
489 }
490
491
492 //------------------------------------------------------------------------------------------------------------------------
493 // Invoke resolution
494 //
495 // Naming conventions:
496 //
497 // resolved_method the specified method (i.e., static receiver specified via constant pool index)
498 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
499 // resolved_klass the specified klass (i.e., specified via constant pool index)
500 // recv_klass the receiver klass
501
502
503 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, symbolHandle method_name,
504 symbolHandle method_signature, KlassHandle current_klass,
505 bool check_access, bool initialize_class, TRAPS) {
506 methodHandle resolved_method;
507 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
508 resolved_klass = KlassHandle(THREAD, Klass::cast(resolved_method->method_holder()));
509
510 // Initialize klass (this should only happen if everything is ok)
511 if (initialize_class && resolved_klass->should_be_initialized()) {
512 resolved_klass->initialize(CHECK);
513 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
514 }
515
516 // setup result
517 result.set_static(resolved_klass, resolved_method, CHECK);
518 }
519
520 // throws linktime exceptions
521 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
522 symbolHandle method_name, symbolHandle method_signature,
523 KlassHandle current_klass, bool check_access, TRAPS) {
524
525 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
526 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
527
528 // check if static
529 if (!resolved_method->is_static()) {
530 char buf[200];
531 jio_snprintf(buf, sizeof(buf), "Expected static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
532 resolved_method->name(),
533 resolved_method->signature()));
534 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
535 }
536 }
537
538
539 void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, symbolHandle method_name,
540 symbolHandle method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
541 methodHandle resolved_method;
542 linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
543 runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK);
544 }
545
546 // throws linktime exceptions
547 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
548 symbolHandle method_name, symbolHandle method_signature,
549 KlassHandle current_klass, bool check_access, TRAPS) {
550
551 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
552
553 // check if method name is <init>, that it is found in same klass as static type
554 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
555 resolved_method->method_holder() != resolved_klass()) {
556 ResourceMark rm(THREAD);
557 Exceptions::fthrow(
558 THREAD_AND_LOCATION,
559 vmSymbolHandles::java_lang_NoSuchMethodError(),
560 "%s: method %s%s not found",
561 resolved_klass->external_name(),
562 resolved_method->name()->as_C_string(),
563 resolved_method->signature()->as_C_string()
564 );
565 return;
566 }
567
568 // check if not static
569 if (resolved_method->is_static()) {
570 char buf[200];
571 jio_snprintf(buf, sizeof(buf),
572 "Expecting non-static method %s",
573 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
574 resolved_method->name(),
575 resolved_method->signature()));
576 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
577 }
578 }
579
580 // throws runtime exceptions
581 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
582 KlassHandle current_klass, bool check_access, TRAPS) {
583
584 // resolved method is selected method unless we have an old-style lookup
585 methodHandle sel_method(THREAD, resolved_method());
586
587 // check if this is an old-style super call and do a new lookup if so
588 { KlassHandle method_klass = KlassHandle(THREAD,
589 resolved_method->method_holder());
590
591 if (check_access &&
592 // a) check if ACC_SUPER flag is set for the current class
593 current_klass->is_super() &&
594 // b) check if the method class is a superclass of the current class (superclass relation is not reflexive!)
595 current_klass->is_subtype_of(method_klass()) && current_klass() != method_klass() &&
596 // c) check if the method is not <init>
597 resolved_method->name() != vmSymbols::object_initializer_name()) {
598 // Lookup super method
599 KlassHandle super_klass(THREAD, current_klass->super());
600 lookup_instance_method_in_klasses(sel_method, super_klass,
601 symbolHandle(THREAD, resolved_method->name()),
602 symbolHandle(THREAD, resolved_method->signature()), CHECK);
603 // check if found
604 if (sel_method.is_null()) {
605 ResourceMark rm(THREAD);
606 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
607 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
608 resolved_method->name(),
609 resolved_method->signature()));
610 }
611 }
612 }
613
614 // check if not static
615 if (sel_method->is_static()) {
616 char buf[200];
617 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
618 resolved_method->name(),
619 resolved_method->signature()));
620 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
621 }
622
623 // check if abstract
624 if (sel_method->is_abstract()) {
625 ResourceMark rm(THREAD);
626 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
627 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
628 sel_method->name(),
629 sel_method->signature()));
630 }
631
632 // setup result
633 result.set_static(resolved_klass, sel_method, CHECK);
634 }
635
636 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
637 symbolHandle method_name, symbolHandle method_signature, KlassHandle current_klass,
638 bool check_access, bool check_null_and_abstract, TRAPS) {
639 methodHandle resolved_method;
640 linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
641 runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
642 }
643
644 // throws linktime exceptions
645 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
646 symbolHandle method_name, symbolHandle method_signature,
647 KlassHandle current_klass, bool check_access, TRAPS) {
648 // normal method resolution
649 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
650
651 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
652 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
653
654 // check if not static
655 if (resolved_method->is_static()) {
656 char buf[200];
657 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
658 resolved_method->name(),
659 resolved_method->signature()));
660 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
661 }
662 }
663
664 // throws runtime exceptions
665 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
666 methodHandle resolved_method,
667 KlassHandle resolved_klass,
668 Handle recv,
669 KlassHandle recv_klass,
670 bool check_null_and_abstract,
671 TRAPS) {
672
673 // setup default return values
674 int vtable_index = methodOopDesc::invalid_vtable_index;
675 methodHandle selected_method;
676
677 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
678
679 // runtime method resolution
680 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
681 THROW(vmSymbols::java_lang_NullPointerException());
682 }
683
684 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the methodOop's
685 // has not been rewritten, and the vtable initialized.
686 assert(instanceKlass::cast(resolved_method->method_holder())->is_linked(), "must be linked");
687
688 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the methodOop's
689 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
690 // a missing receiver might result in a bogus lookup.
691 assert(instanceKlass::cast(resolved_method->method_holder())->is_linked(), "must be linked");
692
693 // do lookup based on receiver klass using the vtable index
694 if (resolved_method->method_holder()->klass_part()->is_interface()) { // miranda method
695 vtable_index = vtable_index_of_miranda_method(resolved_klass,
696 symbolHandle(THREAD, resolved_method->name()),
697 symbolHandle(THREAD, resolved_method->signature()), CHECK);
698 assert(vtable_index >= 0 , "we should have valid vtable index at this point");
699
700 instanceKlass* inst = instanceKlass::cast(recv_klass());
701 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
702 } else {
703 // at this point we are sure that resolved_method is virtual and not
704 // a miranda method; therefore, it must have a valid vtable index.
705 vtable_index = resolved_method->vtable_index();
706 // We could get a negative vtable_index for final methods,
707 // because as an optimization they are they are never put in the vtable,
708 // unless they override an existing method.
709 // If we do get a negative, it means the resolved method is the the selected
710 // method, and it can never be changed by an override.
711 if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
712 assert(resolved_method->can_be_statically_bound(), "cannot override this method");
713 selected_method = resolved_method;
714 } else {
715 // recv_klass might be an arrayKlassOop but all vtables start at
716 // the same place. The cast is to avoid virtual call and assertion.
717 instanceKlass* inst = (instanceKlass*)recv_klass()->klass_part();
718 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
719 }
720 }
721
722 // check if method exists
723 if (selected_method.is_null()) {
724 ResourceMark rm(THREAD);
725 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
726 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
727 resolved_method->name(),
728 resolved_method->signature()));
729 }
730
731 // check if abstract
732 if (check_null_and_abstract && selected_method->is_abstract()) {
733 ResourceMark rm(THREAD);
734 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
735 methodOopDesc::name_and_sig_as_C_string(Klass::cast(resolved_klass()),
736 selected_method->name(),
737 selected_method->signature()));
738 }
739
740 // setup result
741 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
742 }
743
744 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
745 symbolHandle method_name, symbolHandle method_signature, KlassHandle current_klass,
746 bool check_access, bool check_null_and_abstract, TRAPS) {
747 methodHandle resolved_method;
748 linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
749 runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
750 }
751
752 // throws linktime exceptions
753 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, symbolHandle method_name,
754 symbolHandle method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
755 // normal interface method resolution
756 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
757
758 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
759 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
760 }
761
762 // throws runtime exceptions
763 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
764 Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
765 // check if receiver exists
766 if (check_null_and_abstract && recv.is_null()) {
767 THROW(vmSymbols::java_lang_NullPointerException());
768 }
769
770 // check if receiver klass implements the resolved interface
771 if (!recv_klass->is_subtype_of(resolved_klass())) {
772 char buf[200];
773 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
774 (Klass::cast(recv_klass()))->external_name(),
775 (Klass::cast(resolved_klass()))->external_name());
776 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
777 }
778 // do lookup based on receiver klass
779 methodHandle sel_method;
780 lookup_instance_method_in_klasses(sel_method, recv_klass,
781 symbolHandle(THREAD, resolved_method->name()),
782 symbolHandle(THREAD, resolved_method->signature()), CHECK);
783 // check if method exists
784 if (sel_method.is_null()) {
785 ResourceMark rm(THREAD);
786 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
787 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
788 resolved_method->name(),
789 resolved_method->signature()));
790 }
791 // check if public
792 if (!sel_method->is_public()) {
793 ResourceMark rm(THREAD);
794 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
795 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
796 sel_method->name(),
797 sel_method->signature()));
798 }
799 // check if abstract
800 if (check_null_and_abstract && sel_method->is_abstract()) {
801 ResourceMark rm(THREAD);
802 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
803 methodOopDesc::name_and_sig_as_C_string(Klass::cast(recv_klass()),
804 sel_method->name(),
805 sel_method->signature()));
806 }
807 // setup result
808 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, CHECK);
809 }
810
811
812 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
813 KlassHandle resolved_klass,
814 symbolHandle method_name,
815 symbolHandle method_signature,
816 KlassHandle current_klass,
817 bool check_access) {
818 EXCEPTION_MARK;
819 methodHandle method_result;
820 linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
821 if (HAS_PENDING_EXCEPTION) {
822 CLEAR_PENDING_EXCEPTION;
823 return methodHandle();
824 } else {
825 return method_result;
826 }
827 }
828
829 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
830 KlassHandle resolved_klass,
831 symbolHandle method_name,
832 symbolHandle method_signature,
833 KlassHandle current_klass,
834 bool check_access) {
835 EXCEPTION_MARK;
836 methodHandle method_result;
837 linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
838 if (HAS_PENDING_EXCEPTION) {
839 CLEAR_PENDING_EXCEPTION;
840 return methodHandle();
841 } else {
842 return method_result;
843 }
844 }
845
846 methodHandle LinkResolver::resolve_virtual_call_or_null(
847 KlassHandle receiver_klass,
848 KlassHandle resolved_klass,
849 symbolHandle name,
850 symbolHandle signature,
851 KlassHandle current_klass) {
852 EXCEPTION_MARK;
853 CallInfo info;
854 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
855 if (HAS_PENDING_EXCEPTION) {
856 CLEAR_PENDING_EXCEPTION;
857 return methodHandle();
858 }
859 return info.selected_method();
860 }
861
862 methodHandle LinkResolver::resolve_interface_call_or_null(
863 KlassHandle receiver_klass,
864 KlassHandle resolved_klass,
865 symbolHandle name,
866 symbolHandle signature,
867 KlassHandle current_klass) {
868 EXCEPTION_MARK;
869 CallInfo info;
870 resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
871 if (HAS_PENDING_EXCEPTION) {
872 CLEAR_PENDING_EXCEPTION;
873 return methodHandle();
874 }
875 return info.selected_method();
876 }
877
878 int LinkResolver::resolve_virtual_vtable_index(
879 KlassHandle receiver_klass,
880 KlassHandle resolved_klass,
881 symbolHandle name,
882 symbolHandle signature,
883 KlassHandle current_klass) {
884 EXCEPTION_MARK;
885 CallInfo info;
886 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
887 if (HAS_PENDING_EXCEPTION) {
888 CLEAR_PENDING_EXCEPTION;
889 return methodOopDesc::invalid_vtable_index;
890 }
891 return info.vtable_index();
892 }
893
894 methodHandle LinkResolver::resolve_static_call_or_null(
895 KlassHandle resolved_klass,
896 symbolHandle name,
897 symbolHandle signature,
898 KlassHandle current_klass) {
899 EXCEPTION_MARK;
900 CallInfo info;
901 resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD);
902 if (HAS_PENDING_EXCEPTION) {
903 CLEAR_PENDING_EXCEPTION;
904 return methodHandle();
905 }
906 return info.selected_method();
907 }
908
909 methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, symbolHandle name, symbolHandle signature,
910 KlassHandle current_klass) {
911 EXCEPTION_MARK;
912 CallInfo info;
913 resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD);
914 if (HAS_PENDING_EXCEPTION) {
915 CLEAR_PENDING_EXCEPTION;
916 return methodHandle();
917 }
918 return info.selected_method();
919 }
920
921
922
923 //------------------------------------------------------------------------------------------------------------------------
924 // ConstantPool entries
925
926 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
927 switch (byte) {
928 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
929 case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break;
930 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
931 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
932 }
933 return;
934 }
935
936 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, symbolHandle& method_name, symbolHandle& method_signature,
937 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
938 // resolve klass
939 resolve_klass(resolved_klass, pool, index, CHECK);
940
941 // Get name, signature, and static klass
942 method_name = symbolHandle(THREAD, pool->name_ref_at(index));
943 method_signature = symbolHandle(THREAD, pool->signature_ref_at(index));
944 current_klass = KlassHandle(THREAD, pool->pool_holder());
945 }
946
947
948 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
949 KlassHandle resolved_klass;
950 symbolHandle method_name;
951 symbolHandle method_signature;
952 KlassHandle current_klass;
953 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
954 resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
955 }
956
957
958 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
959 KlassHandle resolved_klass;
960 symbolHandle method_name;
961 symbolHandle method_signature;
962 KlassHandle current_klass;
963 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
964 resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
965 }
966
967
968 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
969 constantPoolHandle pool, int index,
970 TRAPS) {
971
972 KlassHandle resolved_klass;
973 symbolHandle method_name;
974 symbolHandle method_signature;
975 KlassHandle current_klass;
976 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
977 KlassHandle recvrKlass (THREAD, recv.is_null() ? (klassOop)NULL : recv->klass());
978 resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
979 }
980
981
982 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
983 KlassHandle resolved_klass;
984 symbolHandle method_name;
985 symbolHandle method_signature;
986 KlassHandle current_klass;
987 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
988 KlassHandle recvrKlass (THREAD, recv.is_null() ? (klassOop)NULL : recv->klass());
989 resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
990 }
991
992 //------------------------------------------------------------------------------------------------------------------------
993 #ifndef PRODUCT
994
995 void FieldAccessInfo::print() {
996 ResourceMark rm;
997 tty->print_cr("Field %s@%d", name()->as_C_string(), field_offset());
998 }
999
1000 #endif