Mon, 01 Dec 2014 13:06:20 -0500
8064524: Compiler code generation improvements
Reviewed-by: jrose, acorn, vlivanov
duke@435 | 1 | /* |
hseigel@6232 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
kamg@4245 | 26 | #include "classfile/defaultMethods.hpp" |
stefank@2314 | 27 | #include "classfile/systemDictionary.hpp" |
stefank@2314 | 28 | #include "classfile/vmSymbols.hpp" |
stefank@2314 | 29 | #include "compiler/compileBroker.hpp" |
stefank@2314 | 30 | #include "gc_interface/collectedHeap.inline.hpp" |
stefank@2314 | 31 | #include "interpreter/bytecode.hpp" |
stefank@2314 | 32 | #include "interpreter/interpreterRuntime.hpp" |
stefank@2314 | 33 | #include "interpreter/linkResolver.hpp" |
stefank@2314 | 34 | #include "memory/resourceArea.hpp" |
stefank@2314 | 35 | #include "memory/universe.inline.hpp" |
stefank@2314 | 36 | #include "oops/instanceKlass.hpp" |
stefank@2314 | 37 | #include "oops/objArrayOop.hpp" |
stefank@2314 | 38 | #include "prims/methodHandles.hpp" |
stefank@2314 | 39 | #include "prims/nativeLookup.hpp" |
stefank@2314 | 40 | #include "runtime/compilationPolicy.hpp" |
stefank@2314 | 41 | #include "runtime/fieldDescriptor.hpp" |
stefank@2314 | 42 | #include "runtime/frame.inline.hpp" |
stefank@2314 | 43 | #include "runtime/handles.inline.hpp" |
stefank@2314 | 44 | #include "runtime/reflection.hpp" |
stefank@2314 | 45 | #include "runtime/signature.hpp" |
stefank@4299 | 46 | #include "runtime/thread.inline.hpp" |
stefank@2314 | 47 | #include "runtime/vmThread.hpp" |
duke@435 | 48 | |
duke@435 | 49 | |
duke@435 | 50 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 51 | // Implementation of CallInfo |
duke@435 | 52 | |
duke@435 | 53 | |
duke@435 | 54 | void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) { |
coleenp@4037 | 55 | int vtable_index = Method::nonvirtual_vtable_index; |
drchase@5732 | 56 | set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
duke@435 | 57 | } |
duke@435 | 58 | |
duke@435 | 59 | |
drchase@5732 | 60 | void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) { |
duke@435 | 61 | // This is only called for interface methods. If the resolved_method |
duke@435 | 62 | // comes from java/lang/Object, it can be the subject of a virtual call, so |
duke@435 | 63 | // we should pick the vtable index from the resolved method. |
drchase@5732 | 64 | // In that case, the caller must call set_virtual instead of set_interface. |
drchase@5732 | 65 | assert(resolved_method->method_holder()->is_interface(), ""); |
drchase@5732 | 66 | assert(itable_index == resolved_method()->itable_index(), ""); |
drchase@5732 | 67 | set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK); |
duke@435 | 68 | } |
duke@435 | 69 | |
duke@435 | 70 | void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) { |
coleenp@4037 | 71 | assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); |
drchase@5732 | 72 | assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), ""); |
drchase@5732 | 73 | CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call); |
drchase@5732 | 74 | set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK); |
twisti@3969 | 75 | assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); |
duke@435 | 76 | } |
duke@435 | 77 | |
twisti@4133 | 78 | void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) { |
twisti@3969 | 79 | if (resolved_method.is_null()) { |
twisti@3969 | 80 | THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null"); |
twisti@3969 | 81 | } |
coleenp@4037 | 82 | KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass(); |
twisti@3969 | 83 | assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || |
twisti@3969 | 84 | resolved_method->is_compiled_lambda_form(), |
twisti@3969 | 85 | "linkMethod must return one of these"); |
coleenp@4037 | 86 | int vtable_index = Method::nonvirtual_vtable_index; |
drchase@5732 | 87 | assert(!resolved_method->has_vtable_index(), ""); |
drchase@5732 | 88 | set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
twisti@4133 | 89 | _resolved_appendix = resolved_appendix; |
twisti@4133 | 90 | _resolved_method_type = resolved_method_type; |
jrose@2015 | 91 | } |
jrose@2015 | 92 | |
drchase@5732 | 93 | void CallInfo::set_common(KlassHandle resolved_klass, |
drchase@5732 | 94 | KlassHandle selected_klass, |
drchase@5732 | 95 | methodHandle resolved_method, |
drchase@5732 | 96 | methodHandle selected_method, |
drchase@5732 | 97 | CallKind kind, |
drchase@5732 | 98 | int index, |
drchase@5732 | 99 | TRAPS) { |
duke@435 | 100 | assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); |
duke@435 | 101 | _resolved_klass = resolved_klass; |
duke@435 | 102 | _selected_klass = selected_klass; |
duke@435 | 103 | _resolved_method = resolved_method; |
duke@435 | 104 | _selected_method = selected_method; |
drchase@5732 | 105 | _call_kind = kind; |
drchase@5732 | 106 | _call_index = index; |
twisti@3969 | 107 | _resolved_appendix = Handle(); |
drchase@5732 | 108 | DEBUG_ONLY(verify()); // verify before making side effects |
drchase@5732 | 109 | |
iveresov@2138 | 110 | if (CompilationPolicy::must_be_compiled(selected_method)) { |
twisti@1570 | 111 | // This path is unusual, mostly used by the '-Xcomp' stress test mode. |
twisti@1570 | 112 | |
iveresov@2138 | 113 | // Note: with several active threads, the must_be_compiled may be true |
iveresov@2138 | 114 | // while can_be_compiled is false; remove assert |
iveresov@2138 | 115 | // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile"); |
duke@435 | 116 | if (THREAD->is_Compiler_thread()) { |
duke@435 | 117 | // don't force compilation, resolve was on behalf of compiler |
duke@435 | 118 | return; |
duke@435 | 119 | } |
coleenp@4251 | 120 | if (selected_method->method_holder()->is_not_initialized()) { |
twisti@1570 | 121 | // 'is_not_initialized' means not only '!is_initialized', but also that |
twisti@1570 | 122 | // initialization has not been started yet ('!being_initialized') |
twisti@1570 | 123 | // Do not force compilation of methods in uninitialized classes. |
twisti@1570 | 124 | // Note that doing this would throw an assert later, |
twisti@1570 | 125 | // in CompileBroker::compile_method. |
twisti@1570 | 126 | // We sometimes use the link resolver to do reflective lookups |
twisti@1570 | 127 | // even before classes are initialized. |
twisti@1570 | 128 | return; |
twisti@1570 | 129 | } |
duke@435 | 130 | CompileBroker::compile_method(selected_method, InvocationEntryBci, |
iveresov@3035 | 131 | CompilationPolicy::policy()->initial_compile_level(), |
iveresov@2138 | 132 | methodHandle(), 0, "must_be_compiled", CHECK); |
duke@435 | 133 | } |
duke@435 | 134 | } |
duke@435 | 135 | |
drchase@5732 | 136 | // utility query for unreflecting a method |
drchase@5732 | 137 | CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) { |
drchase@5732 | 138 | Klass* resolved_method_holder = resolved_method->method_holder(); |
drchase@5732 | 139 | if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st |
drchase@5732 | 140 | resolved_klass = resolved_method_holder; |
drchase@5732 | 141 | } |
drchase@5732 | 142 | _resolved_klass = resolved_klass; |
drchase@5732 | 143 | _selected_klass = resolved_klass; |
drchase@5732 | 144 | _resolved_method = resolved_method; |
drchase@5732 | 145 | _selected_method = resolved_method; |
drchase@5732 | 146 | // classify: |
drchase@5732 | 147 | CallKind kind = CallInfo::unknown_kind; |
drchase@5732 | 148 | int index = resolved_method->vtable_index(); |
drchase@5732 | 149 | if (resolved_method->can_be_statically_bound()) { |
drchase@5732 | 150 | kind = CallInfo::direct_call; |
drchase@5732 | 151 | } else if (!resolved_method_holder->is_interface()) { |
drchase@5732 | 152 | // Could be an Object method inherited into an interface, but still a vtable call. |
drchase@5732 | 153 | kind = CallInfo::vtable_call; |
drchase@5732 | 154 | } else if (!resolved_klass->is_interface()) { |
acorn@6080 | 155 | // A default or miranda method. Compute the vtable index. |
drchase@5732 | 156 | ResourceMark rm; |
drchase@5732 | 157 | klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable(); |
acorn@6080 | 158 | index = LinkResolver::vtable_index_of_interface_method(resolved_klass, |
acorn@6080 | 159 | resolved_method); |
acorn@6080 | 160 | assert(index >= 0 , "we should have valid vtable index at this point"); |
acorn@6080 | 161 | |
drchase@5732 | 162 | kind = CallInfo::vtable_call; |
drchase@5924 | 163 | } else if (resolved_method->has_vtable_index()) { |
drchase@5924 | 164 | // Can occur if an interface redeclares a method of Object. |
drchase@5924 | 165 | |
drchase@5924 | 166 | #ifdef ASSERT |
drchase@5924 | 167 | // Ensure that this is really the case. |
drchase@5924 | 168 | KlassHandle object_klass = SystemDictionary::Object_klass(); |
drchase@5924 | 169 | Method * object_resolved_method = object_klass()->vtable()->method_at(index); |
drchase@5924 | 170 | assert(object_resolved_method->name() == resolved_method->name(), |
drchase@5924 | 171 | err_msg("Object and interface method names should match at vtable index %d, %s != %s", |
drchase@5924 | 172 | index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string())); |
drchase@5924 | 173 | assert(object_resolved_method->signature() == resolved_method->signature(), |
drchase@5924 | 174 | err_msg("Object and interface method signatures should match at vtable index %d, %s != %s", |
drchase@5924 | 175 | index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string())); |
drchase@5924 | 176 | #endif // ASSERT |
drchase@5924 | 177 | |
drchase@5924 | 178 | kind = CallInfo::vtable_call; |
drchase@5732 | 179 | } else { |
drchase@5732 | 180 | // A regular interface call. |
drchase@5732 | 181 | kind = CallInfo::itable_call; |
drchase@5732 | 182 | index = resolved_method->itable_index(); |
drchase@5732 | 183 | } |
drchase@5732 | 184 | assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index)); |
drchase@5732 | 185 | _call_kind = kind; |
drchase@5732 | 186 | _call_index = index; |
drchase@5732 | 187 | _resolved_appendix = Handle(); |
drchase@5732 | 188 | DEBUG_ONLY(verify()); |
drchase@5732 | 189 | } |
drchase@5732 | 190 | |
drchase@5732 | 191 | #ifdef ASSERT |
drchase@5732 | 192 | void CallInfo::verify() { |
drchase@5732 | 193 | switch (call_kind()) { // the meaning and allowed value of index depends on kind |
drchase@5732 | 194 | case CallInfo::direct_call: |
drchase@5732 | 195 | if (_call_index == Method::nonvirtual_vtable_index) break; |
drchase@5732 | 196 | // else fall through to check vtable index: |
drchase@5732 | 197 | case CallInfo::vtable_call: |
drchase@5732 | 198 | assert(resolved_klass()->verify_vtable_index(_call_index), ""); |
drchase@5732 | 199 | break; |
drchase@5732 | 200 | case CallInfo::itable_call: |
drchase@5732 | 201 | assert(resolved_method()->method_holder()->verify_itable_index(_call_index), ""); |
drchase@5732 | 202 | break; |
drchase@5732 | 203 | case CallInfo::unknown_kind: |
drchase@5732 | 204 | assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set"); |
drchase@5732 | 205 | break; |
drchase@5732 | 206 | default: |
drchase@5732 | 207 | fatal(err_msg_res("Unexpected call kind %d", call_kind())); |
drchase@5732 | 208 | } |
drchase@5732 | 209 | } |
drchase@5732 | 210 | #endif //ASSERT |
drchase@5732 | 211 | |
drchase@5732 | 212 | |
duke@435 | 213 | |
duke@435 | 214 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 215 | // Klass resolution |
duke@435 | 216 | |
duke@435 | 217 | void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) { |
coleenp@4037 | 218 | if (!Reflection::verify_class_access(ref_klass(), |
coleenp@4037 | 219 | sel_klass(), |
duke@435 | 220 | true)) { |
duke@435 | 221 | ResourceMark rm(THREAD); |
duke@435 | 222 | Exceptions::fthrow( |
duke@435 | 223 | THREAD_AND_LOCATION, |
coleenp@2497 | 224 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 225 | "tried to access class %s from class %s", |
duke@435 | 226 | sel_klass->external_name(), |
duke@435 | 227 | ref_klass->external_name() |
duke@435 | 228 | ); |
duke@435 | 229 | return; |
duke@435 | 230 | } |
duke@435 | 231 | } |
duke@435 | 232 | |
duke@435 | 233 | void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) { |
coleenp@4037 | 234 | Klass* result_oop = pool->klass_ref_at(index, CHECK); |
duke@435 | 235 | result = KlassHandle(THREAD, result_oop); |
duke@435 | 236 | } |
duke@435 | 237 | |
duke@435 | 238 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 239 | // Method resolution |
duke@435 | 240 | // |
duke@435 | 241 | // According to JVM spec. $5.4.3c & $5.4.3d |
duke@435 | 242 | |
acorn@5848 | 243 | // Look up method in klasses, including static methods |
acorn@5848 | 244 | // Then look up local default methods |
hseigel@6167 | 245 | void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, bool checkpolymorphism, bool in_imethod_resolve, TRAPS) { |
lfoltan@6632 | 246 | // Ignore overpasses so statics can be found during resolution |
lfoltan@6632 | 247 | Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::skip_overpass); |
hseigel@6167 | 248 | |
lfoltan@6818 | 249 | if (klass->oop_is_array()) { |
lfoltan@6818 | 250 | // Only consider klass and super klass for arrays |
lfoltan@6818 | 251 | result = methodHandle(THREAD, result_oop); |
lfoltan@6818 | 252 | return; |
lfoltan@6818 | 253 | } |
lfoltan@6818 | 254 | |
hseigel@6167 | 255 | // JDK 8, JVMS 5.4.3.4: Interface method resolution should |
hseigel@6167 | 256 | // ignore static and non-public methods of java.lang.Object, |
hseigel@6167 | 257 | // like clone, finalize, registerNatives. |
hseigel@6167 | 258 | if (in_imethod_resolve && |
hseigel@6167 | 259 | result_oop != NULL && |
hseigel@6167 | 260 | klass->is_interface() && |
hseigel@6167 | 261 | (result_oop->is_static() || !result_oop->is_public()) && |
hseigel@6167 | 262 | result_oop->method_holder() == SystemDictionary::Object_klass()) { |
hseigel@6167 | 263 | result_oop = NULL; |
hseigel@6167 | 264 | } |
hseigel@6167 | 265 | |
lfoltan@6632 | 266 | // Before considering default methods, check for an overpass in the |
lfoltan@6632 | 267 | // current class if a method has not been found. |
lfoltan@6632 | 268 | if (result_oop == NULL) { |
lfoltan@6632 | 269 | result_oop = InstanceKlass::cast(klass())->find_method(name, signature); |
lfoltan@6632 | 270 | } |
lfoltan@6632 | 271 | |
acorn@5848 | 272 | if (result_oop == NULL) { |
acorn@5848 | 273 | Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); |
acorn@5848 | 274 | if (default_methods != NULL) { |
acorn@5848 | 275 | result_oop = InstanceKlass::find_method(default_methods, name, signature); |
acorn@5848 | 276 | } |
acorn@5848 | 277 | } |
acorn@5848 | 278 | |
acorn@6144 | 279 | if (checkpolymorphism && EnableInvokeDynamic && result_oop != NULL) { |
twisti@3969 | 280 | vmIntrinsics::ID iid = result_oop->intrinsic_id(); |
twisti@3969 | 281 | if (MethodHandles::is_signature_polymorphic(iid)) { |
twisti@3969 | 282 | // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method. |
jrose@1862 | 283 | return; |
jrose@1862 | 284 | } |
jrose@1862 | 285 | } |
duke@435 | 286 | result = methodHandle(THREAD, result_oop); |
duke@435 | 287 | } |
duke@435 | 288 | |
duke@435 | 289 | // returns first instance method |
acorn@5848 | 290 | // Looks up method in classes, then looks up local default methods |
coleenp@2497 | 291 | void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { |
lfoltan@6632 | 292 | Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::normal); |
duke@435 | 293 | result = methodHandle(THREAD, result_oop); |
hseigel@5959 | 294 | while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) { |
acorn@6145 | 295 | KlassHandle super_klass = KlassHandle(THREAD, result->method_holder()->super()); |
lfoltan@6632 | 296 | result = methodHandle(THREAD, super_klass->uncached_lookup_method(name, signature, Klass::normal)); |
duke@435 | 297 | } |
acorn@5848 | 298 | |
lfoltan@6818 | 299 | if (klass->oop_is_array()) { |
lfoltan@6818 | 300 | // Only consider klass and super klass for arrays |
lfoltan@6818 | 301 | return; |
lfoltan@6818 | 302 | } |
lfoltan@6818 | 303 | |
acorn@5848 | 304 | if (result.is_null()) { |
acorn@5848 | 305 | Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); |
acorn@5848 | 306 | if (default_methods != NULL) { |
acorn@5848 | 307 | result = methodHandle(InstanceKlass::find_method(default_methods, name, signature)); |
acorn@5848 | 308 | assert(result.is_null() || !result->is_static(), "static defaults not allowed"); |
acorn@5848 | 309 | } |
acorn@5848 | 310 | } |
duke@435 | 311 | } |
duke@435 | 312 | |
acorn@5848 | 313 | int LinkResolver::vtable_index_of_interface_method(KlassHandle klass, |
acorn@6080 | 314 | methodHandle resolved_method) { |
duke@435 | 315 | |
acorn@5848 | 316 | int vtable_index = Method::invalid_vtable_index; |
acorn@5848 | 317 | Symbol* name = resolved_method->name(); |
acorn@5848 | 318 | Symbol* signature = resolved_method->signature(); |
acorn@5848 | 319 | |
acorn@5848 | 320 | // First check in default method array |
hseigel@6195 | 321 | if (!resolved_method->is_abstract() && |
acorn@5848 | 322 | (InstanceKlass::cast(klass())->default_methods() != NULL)) { |
drchase@7499 | 323 | int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), name, signature, false, false); |
acorn@5848 | 324 | if (index >= 0 ) { |
acorn@5848 | 325 | vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index); |
acorn@5848 | 326 | } |
acorn@5848 | 327 | } |
acorn@5848 | 328 | if (vtable_index == Method::invalid_vtable_index) { |
acorn@5848 | 329 | // get vtable_index for miranda methods |
acorn@6080 | 330 | ResourceMark rm; |
acorn@5848 | 331 | klassVtable *vt = InstanceKlass::cast(klass())->vtable(); |
acorn@5848 | 332 | vtable_index = vt->index_of_miranda(name, signature); |
acorn@5848 | 333 | } |
acorn@5848 | 334 | return vtable_index; |
duke@435 | 335 | } |
duke@435 | 336 | |
coleenp@2497 | 337 | void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { |
coleenp@4037 | 338 | InstanceKlass *ik = InstanceKlass::cast(klass()); |
hseigel@6195 | 339 | |
hseigel@6195 | 340 | // Specify 'true' in order to skip default methods when searching the |
hseigel@6195 | 341 | // interfaces. Function lookup_method_in_klasses() already looked for |
hseigel@6195 | 342 | // the method in the default methods table. |
lfoltan@6632 | 343 | result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature, Klass::skip_defaults)); |
duke@435 | 344 | } |
duke@435 | 345 | |
twisti@3969 | 346 | void LinkResolver::lookup_polymorphic_method(methodHandle& result, |
twisti@3969 | 347 | KlassHandle klass, Symbol* name, Symbol* full_signature, |
twisti@3969 | 348 | KlassHandle current_klass, |
twisti@4133 | 349 | Handle *appendix_result_or_null, |
twisti@4133 | 350 | Handle *method_type_result, |
twisti@3969 | 351 | TRAPS) { |
twisti@3969 | 352 | vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name); |
twisti@3969 | 353 | if (TraceMethodHandles) { |
kmo@4910 | 354 | ResourceMark rm(THREAD); |
twisti@3969 | 355 | tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s", |
twisti@3969 | 356 | vmIntrinsics::name_at(iid), klass->external_name(), |
twisti@3969 | 357 | name->as_C_string(), full_signature->as_C_string()); |
twisti@3969 | 358 | } |
twisti@2698 | 359 | if (EnableInvokeDynamic && |
jrose@1862 | 360 | klass() == SystemDictionary::MethodHandle_klass() && |
twisti@3969 | 361 | iid != vmIntrinsics::_none) { |
twisti@3969 | 362 | if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) { |
twisti@3969 | 363 | // Most of these do not need an up-call to Java to resolve, so can be done anywhere. |
twisti@3969 | 364 | // Do not erase last argument type (MemberName) if it is a static linkTo method. |
twisti@3969 | 365 | bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid); |
twisti@3969 | 366 | TempNewSymbol basic_signature = |
twisti@3969 | 367 | MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK); |
twisti@3969 | 368 | if (TraceMethodHandles) { |
kmo@4910 | 369 | ResourceMark rm(THREAD); |
twisti@3969 | 370 | tty->print_cr("lookup_polymorphic_method %s %s => basic %s", |
twisti@3969 | 371 | name->as_C_string(), |
twisti@3969 | 372 | full_signature->as_C_string(), |
twisti@3969 | 373 | basic_signature->as_C_string()); |
jrose@2015 | 374 | } |
twisti@3969 | 375 | result = SystemDictionary::find_method_handle_intrinsic(iid, |
twisti@3969 | 376 | basic_signature, |
twisti@3969 | 377 | CHECK); |
twisti@3969 | 378 | if (result.not_null()) { |
twisti@3969 | 379 | assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic"); |
twisti@3969 | 380 | assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this"); |
twisti@3969 | 381 | assert(basic_signature == result->signature(), "predict the result signature"); |
twisti@3969 | 382 | if (TraceMethodHandles) { |
twisti@3969 | 383 | tty->print("lookup_polymorphic_method => intrinsic "); |
twisti@3969 | 384 | result->print_on(tty); |
twisti@3969 | 385 | } |
twisti@3969 | 386 | return; |
twisti@3969 | 387 | } |
twisti@3969 | 388 | } else if (iid == vmIntrinsics::_invokeGeneric |
twisti@3969 | 389 | && !THREAD->is_Compiler_thread() |
twisti@3969 | 390 | && appendix_result_or_null != NULL) { |
twisti@3969 | 391 | // This is a method with type-checking semantics. |
twisti@3969 | 392 | // We will ask Java code to spin an adapter method for it. |
twisti@3969 | 393 | if (!MethodHandles::enabled()) { |
twisti@3969 | 394 | // Make sure the Java part of the runtime has been booted up. |
coleenp@4037 | 395 | Klass* natives = SystemDictionary::MethodHandleNatives_klass(); |
coleenp@4037 | 396 | if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) { |
twisti@3969 | 397 | SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(), |
twisti@3969 | 398 | Handle(), |
twisti@3969 | 399 | Handle(), |
twisti@3969 | 400 | true, |
twisti@3969 | 401 | CHECK); |
twisti@3969 | 402 | } |
twisti@3969 | 403 | } |
twisti@3969 | 404 | |
twisti@3969 | 405 | Handle appendix; |
twisti@4133 | 406 | Handle method_type; |
twisti@3969 | 407 | result = SystemDictionary::find_method_handle_invoker(name, |
twisti@3969 | 408 | full_signature, |
twisti@3969 | 409 | current_klass, |
twisti@3969 | 410 | &appendix, |
twisti@4133 | 411 | &method_type, |
twisti@3969 | 412 | CHECK); |
twisti@3969 | 413 | if (TraceMethodHandles) { |
twisti@3969 | 414 | tty->print("lookup_polymorphic_method => (via Java) "); |
twisti@3969 | 415 | result->print_on(tty); |
twisti@3969 | 416 | tty->print(" lookup_polymorphic_method => appendix = "); |
twisti@3969 | 417 | if (appendix.is_null()) tty->print_cr("(none)"); |
twisti@3969 | 418 | else appendix->print_on(tty); |
twisti@3969 | 419 | } |
twisti@3969 | 420 | if (result.not_null()) { |
twisti@3969 | 421 | #ifdef ASSERT |
kmo@4910 | 422 | ResourceMark rm(THREAD); |
kmo@4910 | 423 | |
twisti@3969 | 424 | TempNewSymbol basic_signature = |
twisti@3969 | 425 | MethodHandles::lookup_basic_type_signature(full_signature, CHECK); |
twisti@3969 | 426 | int actual_size_of_params = result->size_of_parameters(); |
twisti@3969 | 427 | int expected_size_of_params = ArgumentSizeComputer(basic_signature).size(); |
twisti@3969 | 428 | // +1 for MethodHandle.this, +1 for trailing MethodType |
twisti@3969 | 429 | if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1; |
twisti@3969 | 430 | if (appendix.not_null()) expected_size_of_params += 1; |
twisti@3969 | 431 | if (actual_size_of_params != expected_size_of_params) { |
twisti@3969 | 432 | tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string()); |
twisti@3969 | 433 | tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid)); |
twisti@3969 | 434 | result->print(); |
twisti@3969 | 435 | } |
twisti@3969 | 436 | assert(actual_size_of_params == expected_size_of_params, |
twisti@3969 | 437 | err_msg("%d != %d", actual_size_of_params, expected_size_of_params)); |
twisti@3969 | 438 | #endif //ASSERT |
twisti@3969 | 439 | |
twisti@3969 | 440 | assert(appendix_result_or_null != NULL, ""); |
twisti@3969 | 441 | (*appendix_result_or_null) = appendix; |
twisti@4133 | 442 | (*method_type_result) = method_type; |
twisti@3969 | 443 | return; |
twisti@3969 | 444 | } |
jrose@1145 | 445 | } |
jrose@1145 | 446 | } |
jrose@1145 | 447 | } |
jrose@1145 | 448 | |
duke@435 | 449 | void LinkResolver::check_method_accessability(KlassHandle ref_klass, |
duke@435 | 450 | KlassHandle resolved_klass, |
duke@435 | 451 | KlassHandle sel_klass, |
duke@435 | 452 | methodHandle sel_method, |
duke@435 | 453 | TRAPS) { |
duke@435 | 454 | |
duke@435 | 455 | AccessFlags flags = sel_method->access_flags(); |
duke@435 | 456 | |
hseigel@6167 | 457 | // Special case: arrays always override "clone". JVMS 2.15. |
duke@435 | 458 | // If the resolved klass is an array class, and the declaring class |
duke@435 | 459 | // is java.lang.Object and the method is "clone", set the flags |
duke@435 | 460 | // to public. |
duke@435 | 461 | // |
hseigel@6167 | 462 | // We'll check for the method name first, as that's most likely |
hseigel@6167 | 463 | // to be false (so we'll short-circuit out of these tests). |
hseigel@6167 | 464 | if (sel_method->name() == vmSymbols::clone_name() && |
hseigel@6167 | 465 | sel_klass() == SystemDictionary::Object_klass() && |
hseigel@6167 | 466 | resolved_klass->oop_is_array()) { |
duke@435 | 467 | // We need to change "protected" to "public". |
hseigel@6167 | 468 | assert(flags.is_protected(), "clone not protected?"); |
duke@435 | 469 | jint new_flags = flags.as_int(); |
duke@435 | 470 | new_flags = new_flags & (~JVM_ACC_PROTECTED); |
duke@435 | 471 | new_flags = new_flags | JVM_ACC_PUBLIC; |
duke@435 | 472 | flags.set_flags(new_flags); |
duke@435 | 473 | } |
twisti@3969 | 474 | // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument"); |
duke@435 | 475 | |
coleenp@4037 | 476 | if (!Reflection::verify_field_access(ref_klass(), |
coleenp@4037 | 477 | resolved_klass(), |
coleenp@4037 | 478 | sel_klass(), |
duke@435 | 479 | flags, |
duke@435 | 480 | true)) { |
duke@435 | 481 | ResourceMark rm(THREAD); |
duke@435 | 482 | Exceptions::fthrow( |
duke@435 | 483 | THREAD_AND_LOCATION, |
coleenp@2497 | 484 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 485 | "tried to access method %s.%s%s from class %s", |
duke@435 | 486 | sel_klass->external_name(), |
duke@435 | 487 | sel_method->name()->as_C_string(), |
duke@435 | 488 | sel_method->signature()->as_C_string(), |
duke@435 | 489 | ref_klass->external_name() |
duke@435 | 490 | ); |
duke@435 | 491 | return; |
duke@435 | 492 | } |
duke@435 | 493 | } |
duke@435 | 494 | |
twisti@3969 | 495 | void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass, |
twisti@3969 | 496 | Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) { |
drchase@5732 | 497 | // This method is used only |
drchase@5732 | 498 | // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call), |
drchase@5732 | 499 | // and |
drchase@5732 | 500 | // (2) in Bytecode_invoke::static_target |
drchase@5732 | 501 | // It appears to fail when applied to an invokeinterface call site. |
drchase@5732 | 502 | // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points. |
duke@435 | 503 | // resolve klass |
twisti@3969 | 504 | if (code == Bytecodes::_invokedynamic) { |
coleenp@4037 | 505 | resolved_klass = SystemDictionary::MethodHandle_klass(); |
twisti@3969 | 506 | Symbol* method_name = vmSymbols::invoke_name(); |
twisti@3969 | 507 | Symbol* method_signature = pool->signature_ref_at(index); |
twisti@3969 | 508 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
acorn@5897 | 509 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK); |
twisti@3969 | 510 | return; |
twisti@3969 | 511 | } |
twisti@3969 | 512 | |
duke@435 | 513 | resolve_klass(resolved_klass, pool, index, CHECK); |
duke@435 | 514 | |
coleenp@2497 | 515 | Symbol* method_name = pool->name_ref_at(index); |
coleenp@2497 | 516 | Symbol* method_signature = pool->signature_ref_at(index); |
duke@435 | 517 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
duke@435 | 518 | |
jrose@2982 | 519 | if (pool->has_preresolution() |
jrose@2982 | 520 | || (resolved_klass() == SystemDictionary::MethodHandle_klass() && |
twisti@3969 | 521 | MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) { |
coleenp@4037 | 522 | Method* result_oop = ConstantPool::method_at_if_loaded(pool, index); |
jrose@2982 | 523 | if (result_oop != NULL) { |
jrose@2982 | 524 | resolved_method = methodHandle(THREAD, result_oop); |
jrose@2982 | 525 | return; |
jrose@2982 | 526 | } |
jrose@2982 | 527 | } |
jrose@2982 | 528 | |
twisti@3969 | 529 | if (code == Bytecodes::_invokeinterface) { |
acorn@6144 | 530 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
acorn@5897 | 531 | } else if (code == Bytecodes::_invokevirtual) { |
acorn@5897 | 532 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
acorn@6144 | 533 | } else if (!resolved_klass->is_interface()) { |
acorn@6144 | 534 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK); |
twisti@3969 | 535 | } else { |
acorn@6144 | 536 | bool nostatics = (code == Bytecodes::_invokestatic) ? false : true; |
acorn@6144 | 537 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, nostatics, CHECK); |
twisti@3969 | 538 | } |
duke@435 | 539 | } |
duke@435 | 540 | |
duke@435 | 541 | void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 542 | Symbol* method_name, Symbol* method_signature, |
acorn@5897 | 543 | KlassHandle current_klass, bool check_access, |
acorn@5897 | 544 | bool require_methodref, TRAPS) { |
duke@435 | 545 | |
twisti@3969 | 546 | Handle nested_exception; |
twisti@3969 | 547 | |
acorn@5897 | 548 | // 1. check if methodref required, that resolved_klass is not interfacemethodref |
acorn@5897 | 549 | if (require_methodref && resolved_klass->is_interface()) { |
acorn@5897 | 550 | ResourceMark rm(THREAD); |
acorn@5897 | 551 | char buf[200]; |
acorn@5897 | 552 | jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", |
acorn@5897 | 553 | resolved_klass()->external_name()); |
acorn@5897 | 554 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5897 | 555 | } |
acorn@5897 | 556 | |
acorn@5897 | 557 | // 2. lookup method in resolved klass and its super klasses |
hseigel@6167 | 558 | lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, true, false, CHECK); |
duke@435 | 559 | |
lfoltan@6818 | 560 | if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy |
acorn@5897 | 561 | // 3. lookup method in all the interfaces implemented by the resolved klass |
duke@435 | 562 | lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK); |
duke@435 | 563 | |
duke@435 | 564 | if (resolved_method.is_null()) { |
twisti@3969 | 565 | // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc |
twisti@3969 | 566 | lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature, |
twisti@4133 | 567 | current_klass, (Handle*)NULL, (Handle*)NULL, THREAD); |
twisti@3969 | 568 | if (HAS_PENDING_EXCEPTION) { |
twisti@3969 | 569 | nested_exception = Handle(THREAD, PENDING_EXCEPTION); |
twisti@3969 | 570 | CLEAR_PENDING_EXCEPTION; |
twisti@3969 | 571 | } |
jrose@1145 | 572 | } |
lfoltan@6818 | 573 | } |
jrose@1145 | 574 | |
lfoltan@6818 | 575 | if (resolved_method.is_null()) { |
lfoltan@6818 | 576 | // 4. method lookup failed |
lfoltan@6818 | 577 | ResourceMark rm(THREAD); |
lfoltan@6818 | 578 | THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(), |
lfoltan@6818 | 579 | Method::name_and_sig_as_C_string(resolved_klass(), |
lfoltan@6818 | 580 | method_name, |
lfoltan@6818 | 581 | method_signature), |
lfoltan@6818 | 582 | nested_exception); |
duke@435 | 583 | } |
duke@435 | 584 | |
ccheung@6308 | 585 | // 5. access checks, access checking may be turned off when calling from within the VM. |
duke@435 | 586 | if (check_access) { |
duke@435 | 587 | assert(current_klass.not_null() , "current_klass should not be null"); |
duke@435 | 588 | |
duke@435 | 589 | // check if method can be accessed by the referring class |
duke@435 | 590 | check_method_accessability(current_klass, |
duke@435 | 591 | resolved_klass, |
duke@435 | 592 | KlassHandle(THREAD, resolved_method->method_holder()), |
duke@435 | 593 | resolved_method, |
duke@435 | 594 | CHECK); |
duke@435 | 595 | |
duke@435 | 596 | // check loader constraints |
coleenp@4037 | 597 | Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4251 | 598 | Handle class_loader (THREAD, resolved_method->method_holder()->class_loader()); |
duke@435 | 599 | { |
duke@435 | 600 | ResourceMark rm(THREAD); |
acorn@4840 | 601 | Symbol* failed_type_symbol = |
duke@435 | 602 | SystemDictionary::check_signature_loaders(method_signature, loader, |
duke@435 | 603 | class_loader, true, CHECK); |
acorn@4840 | 604 | if (failed_type_symbol != NULL) { |
duke@435 | 605 | const char* msg = "loader constraint violation: when resolving method" |
duke@435 | 606 | " \"%s\" the class loader (instance of %s) of the current class, %s," |
acorn@4840 | 607 | " and the class loader (instance of %s) for the method's defining class, %s, have" |
duke@435 | 608 | " different Class objects for the type %s used in the signature"; |
hseigel@4278 | 609 | char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature); |
duke@435 | 610 | const char* loader1 = SystemDictionary::loader_name(loader()); |
coleenp@4037 | 611 | char* current = InstanceKlass::cast(current_klass())->name()->as_C_string(); |
duke@435 | 612 | const char* loader2 = SystemDictionary::loader_name(class_loader()); |
acorn@4840 | 613 | char* target = InstanceKlass::cast(resolved_method->method_holder()) |
acorn@4840 | 614 | ->name()->as_C_string(); |
acorn@4840 | 615 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 616 | size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + |
acorn@4840 | 617 | strlen(current) + strlen(loader2) + strlen(target) + |
acorn@4840 | 618 | strlen(failed_type_name) + 1; |
duke@435 | 619 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 620 | jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, |
acorn@4840 | 621 | target, failed_type_name); |
duke@435 | 622 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 623 | } |
duke@435 | 624 | } |
duke@435 | 625 | } |
duke@435 | 626 | } |
duke@435 | 627 | |
duke@435 | 628 | void LinkResolver::resolve_interface_method(methodHandle& resolved_method, |
duke@435 | 629 | KlassHandle resolved_klass, |
coleenp@2497 | 630 | Symbol* method_name, |
coleenp@2497 | 631 | Symbol* method_signature, |
duke@435 | 632 | KlassHandle current_klass, |
acorn@6144 | 633 | bool check_access, |
acorn@6144 | 634 | bool nostatics, TRAPS) { |
duke@435 | 635 | |
hseigel@6195 | 636 | // check if klass is interface |
duke@435 | 637 | if (!resolved_klass->is_interface()) { |
jcoomes@2856 | 638 | ResourceMark rm(THREAD); |
duke@435 | 639 | char buf[200]; |
hseigel@4278 | 640 | jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name()); |
duke@435 | 641 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 642 | } |
duke@435 | 643 | |
duke@435 | 644 | // lookup method in this interface or its super, java.lang.Object |
acorn@6144 | 645 | // JDK8: also look for static methods |
hseigel@6167 | 646 | lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, false, true, CHECK); |
duke@435 | 647 | |
lfoltan@6818 | 648 | if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { |
duke@435 | 649 | // lookup method in all the super-interfaces |
duke@435 | 650 | lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK); |
lfoltan@6818 | 651 | } |
lfoltan@6818 | 652 | |
lfoltan@6818 | 653 | if (resolved_method.is_null()) { |
lfoltan@6818 | 654 | // no method found |
lfoltan@6818 | 655 | ResourceMark rm(THREAD); |
lfoltan@6818 | 656 | THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(), |
lfoltan@6818 | 657 | Method::name_and_sig_as_C_string(resolved_klass(), |
lfoltan@6818 | 658 | method_name, |
lfoltan@6818 | 659 | method_signature)); |
duke@435 | 660 | } |
duke@435 | 661 | |
duke@435 | 662 | if (check_access) { |
acorn@5786 | 663 | // JDK8 adds non-public interface methods, and accessability check requirement |
acorn@5786 | 664 | assert(current_klass.not_null() , "current_klass should not be null"); |
acorn@5786 | 665 | |
acorn@5786 | 666 | // check if method can be accessed by the referring class |
acorn@5786 | 667 | check_method_accessability(current_klass, |
acorn@5786 | 668 | resolved_klass, |
acorn@5786 | 669 | KlassHandle(THREAD, resolved_method->method_holder()), |
acorn@5786 | 670 | resolved_method, |
acorn@5786 | 671 | CHECK); |
acorn@5786 | 672 | |
duke@435 | 673 | HandleMark hm(THREAD); |
coleenp@4037 | 674 | Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4251 | 675 | Handle class_loader (THREAD, resolved_method->method_holder()->class_loader()); |
duke@435 | 676 | { |
duke@435 | 677 | ResourceMark rm(THREAD); |
acorn@4840 | 678 | Symbol* failed_type_symbol = |
duke@435 | 679 | SystemDictionary::check_signature_loaders(method_signature, loader, |
duke@435 | 680 | class_loader, true, CHECK); |
acorn@4840 | 681 | if (failed_type_symbol != NULL) { |
duke@435 | 682 | const char* msg = "loader constraint violation: when resolving " |
duke@435 | 683 | "interface method \"%s\" the class loader (instance of %s) of the " |
duke@435 | 684 | "current class, %s, and the class loader (instance of %s) for " |
acorn@4840 | 685 | "the method's defining class, %s, have different Class objects for the type %s " |
duke@435 | 686 | "used in the signature"; |
hseigel@4278 | 687 | char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature); |
duke@435 | 688 | const char* loader1 = SystemDictionary::loader_name(loader()); |
coleenp@4037 | 689 | char* current = InstanceKlass::cast(current_klass())->name()->as_C_string(); |
duke@435 | 690 | const char* loader2 = SystemDictionary::loader_name(class_loader()); |
acorn@4840 | 691 | char* target = InstanceKlass::cast(resolved_method->method_holder()) |
acorn@4840 | 692 | ->name()->as_C_string(); |
acorn@4840 | 693 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 694 | size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + |
acorn@4840 | 695 | strlen(current) + strlen(loader2) + strlen(target) + |
acorn@4840 | 696 | strlen(failed_type_name) + 1; |
duke@435 | 697 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 698 | jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, |
acorn@4840 | 699 | target, failed_type_name); |
duke@435 | 700 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 701 | } |
duke@435 | 702 | } |
duke@435 | 703 | } |
acorn@5786 | 704 | |
hseigel@6232 | 705 | if (nostatics && resolved_method->is_static()) { |
hseigel@6232 | 706 | ResourceMark rm(THREAD); |
hseigel@6232 | 707 | char buf[200]; |
hseigel@6232 | 708 | jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", |
hseigel@6232 | 709 | Method::name_and_sig_as_C_string(resolved_klass(), |
hseigel@6232 | 710 | resolved_method->name(), resolved_method->signature())); |
hseigel@6232 | 711 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
hseigel@6232 | 712 | } |
hseigel@6232 | 713 | |
acorn@5786 | 714 | if (TraceItables && Verbose) { |
acorn@5786 | 715 | ResourceMark rm(THREAD); |
acorn@5786 | 716 | tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 717 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5786 | 718 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 719 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 720 | resolved_method->name(), |
acorn@5786 | 721 | resolved_method->signature()), |
acorn@5786 | 722 | resolved_method->method_holder()->internal_name() |
acorn@5786 | 723 | ); |
acorn@5786 | 724 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 725 | if (resolved_method->is_default_method()) { |
acorn@6080 | 726 | tty->print("default "); |
acorn@5848 | 727 | } |
acorn@5848 | 728 | if (resolved_method->is_overpass()) { |
acorn@5848 | 729 | tty->print("overpass"); |
acorn@5848 | 730 | } |
acorn@5786 | 731 | tty->cr(); |
acorn@5786 | 732 | } |
duke@435 | 733 | } |
duke@435 | 734 | |
duke@435 | 735 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 736 | // Field resolution |
duke@435 | 737 | |
duke@435 | 738 | void LinkResolver::check_field_accessability(KlassHandle ref_klass, |
duke@435 | 739 | KlassHandle resolved_klass, |
duke@435 | 740 | KlassHandle sel_klass, |
duke@435 | 741 | fieldDescriptor& fd, |
duke@435 | 742 | TRAPS) { |
coleenp@4037 | 743 | if (!Reflection::verify_field_access(ref_klass(), |
coleenp@4037 | 744 | resolved_klass(), |
coleenp@4037 | 745 | sel_klass(), |
duke@435 | 746 | fd.access_flags(), |
duke@435 | 747 | true)) { |
duke@435 | 748 | ResourceMark rm(THREAD); |
duke@435 | 749 | Exceptions::fthrow( |
duke@435 | 750 | THREAD_AND_LOCATION, |
coleenp@2497 | 751 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 752 | "tried to access field %s.%s from class %s", |
duke@435 | 753 | sel_klass->external_name(), |
duke@435 | 754 | fd.name()->as_C_string(), |
duke@435 | 755 | ref_klass->external_name() |
duke@435 | 756 | ); |
duke@435 | 757 | return; |
duke@435 | 758 | } |
duke@435 | 759 | } |
duke@435 | 760 | |
drchase@5732 | 761 | void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) { |
drchase@5732 | 762 | // Load these early in case the resolve of the containing klass fails |
drchase@5732 | 763 | Symbol* field = pool->name_ref_at(index); |
drchase@5732 | 764 | Symbol* sig = pool->signature_ref_at(index); |
drchase@5732 | 765 | |
drchase@5732 | 766 | // resolve specified klass |
drchase@5732 | 767 | KlassHandle resolved_klass; |
drchase@5732 | 768 | resolve_klass(resolved_klass, pool, index, CHECK); |
drchase@5732 | 769 | |
drchase@5732 | 770 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
drchase@5732 | 771 | resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK); |
duke@435 | 772 | } |
duke@435 | 773 | |
drchase@5732 | 774 | void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig, |
drchase@5732 | 775 | KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class, |
drchase@5732 | 776 | TRAPS) { |
duke@435 | 777 | assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || |
drchase@5732 | 778 | byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || |
drchase@5732 | 779 | (byte == Bytecodes::_nop && !check_access), "bad field access bytecode"); |
duke@435 | 780 | |
duke@435 | 781 | bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); |
duke@435 | 782 | bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic); |
duke@435 | 783 | |
duke@435 | 784 | // Check if there's a resolved klass containing the field |
drchase@5732 | 785 | if (resolved_klass.is_null()) { |
duke@435 | 786 | ResourceMark rm(THREAD); |
duke@435 | 787 | THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
duke@435 | 788 | } |
duke@435 | 789 | |
duke@435 | 790 | // Resolve instance field |
lfoltan@6818 | 791 | KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd)); |
duke@435 | 792 | // check if field exists; i.e., if a klass containing the field def has been selected |
drchase@5732 | 793 | if (sel_klass.is_null()) { |
duke@435 | 794 | ResourceMark rm(THREAD); |
duke@435 | 795 | THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
duke@435 | 796 | } |
duke@435 | 797 | |
drchase@5732 | 798 | if (!check_access) |
drchase@5732 | 799 | // Access checking may be turned off when calling from within the VM. |
drchase@5732 | 800 | return; |
drchase@5732 | 801 | |
duke@435 | 802 | // check access |
drchase@5732 | 803 | check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); |
duke@435 | 804 | |
duke@435 | 805 | // check for errors |
duke@435 | 806 | if (is_static != fd.is_static()) { |
jcoomes@2856 | 807 | ResourceMark rm(THREAD); |
duke@435 | 808 | char msg[200]; |
hseigel@4278 | 809 | jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); |
duke@435 | 810 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); |
duke@435 | 811 | } |
duke@435 | 812 | |
duke@435 | 813 | // Final fields can only be accessed from its own class. |
drchase@5732 | 814 | if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) { |
duke@435 | 815 | THROW(vmSymbols::java_lang_IllegalAccessError()); |
duke@435 | 816 | } |
duke@435 | 817 | |
duke@435 | 818 | // initialize resolved_klass if necessary |
duke@435 | 819 | // note 1: the klass which declared the field must be initialized (i.e, sel_klass) |
duke@435 | 820 | // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) |
duke@435 | 821 | // |
duke@435 | 822 | // note 2: we don't want to force initialization if we are just checking |
duke@435 | 823 | // if the field access is legal; e.g., during compilation |
drchase@5732 | 824 | if (is_static && initialize_class) { |
duke@435 | 825 | sel_klass->initialize(CHECK); |
duke@435 | 826 | } |
duke@435 | 827 | |
drchase@5732 | 828 | if (sel_klass() != current_klass()) { |
duke@435 | 829 | HandleMark hm(THREAD); |
drchase@5732 | 830 | Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4037 | 831 | Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader()); |
duke@435 | 832 | { |
duke@435 | 833 | ResourceMark rm(THREAD); |
acorn@4840 | 834 | Symbol* failed_type_symbol = |
drchase@5732 | 835 | SystemDictionary::check_signature_loaders(sig, |
duke@435 | 836 | ref_loader, sel_loader, |
duke@435 | 837 | false, |
duke@435 | 838 | CHECK); |
acorn@4840 | 839 | if (failed_type_symbol != NULL) { |
duke@435 | 840 | const char* msg = "loader constraint violation: when resolving field" |
duke@435 | 841 | " \"%s\" the class loader (instance of %s) of the referring class, " |
duke@435 | 842 | "%s, and the class loader (instance of %s) for the field's resolved " |
duke@435 | 843 | "type, %s, have different Class objects for that type"; |
coleenp@2497 | 844 | char* field_name = field->as_C_string(); |
duke@435 | 845 | const char* loader1 = SystemDictionary::loader_name(ref_loader()); |
coleenp@4037 | 846 | char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string(); |
duke@435 | 847 | const char* loader2 = SystemDictionary::loader_name(sel_loader()); |
acorn@4840 | 848 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 849 | size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) + |
acorn@4840 | 850 | strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1; |
duke@435 | 851 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 852 | jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2, |
duke@435 | 853 | failed_type_name); |
duke@435 | 854 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 855 | } |
duke@435 | 856 | } |
duke@435 | 857 | } |
duke@435 | 858 | |
duke@435 | 859 | // return information. note that the klass is set to the actual klass containing the |
duke@435 | 860 | // field, otherwise access of static fields in superclasses will not work. |
duke@435 | 861 | } |
duke@435 | 862 | |
duke@435 | 863 | |
duke@435 | 864 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 865 | // Invoke resolution |
duke@435 | 866 | // |
duke@435 | 867 | // Naming conventions: |
duke@435 | 868 | // |
duke@435 | 869 | // resolved_method the specified method (i.e., static receiver specified via constant pool index) |
duke@435 | 870 | // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) |
duke@435 | 871 | // resolved_klass the specified klass (i.e., specified via constant pool index) |
duke@435 | 872 | // recv_klass the receiver klass |
duke@435 | 873 | |
duke@435 | 874 | |
coleenp@2497 | 875 | void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name, |
coleenp@2497 | 876 | Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 877 | bool check_access, bool initialize_class, TRAPS) { |
duke@435 | 878 | methodHandle resolved_method; |
duke@435 | 879 | linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
hseigel@4278 | 880 | resolved_klass = KlassHandle(THREAD, resolved_method->method_holder()); |
duke@435 | 881 | |
duke@435 | 882 | // Initialize klass (this should only happen if everything is ok) |
duke@435 | 883 | if (initialize_class && resolved_klass->should_be_initialized()) { |
duke@435 | 884 | resolved_klass->initialize(CHECK); |
duke@435 | 885 | linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 886 | } |
duke@435 | 887 | |
duke@435 | 888 | // setup result |
duke@435 | 889 | result.set_static(resolved_klass, resolved_method, CHECK); |
duke@435 | 890 | } |
duke@435 | 891 | |
duke@435 | 892 | // throws linktime exceptions |
duke@435 | 893 | void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 894 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 895 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 896 | |
acorn@6144 | 897 | if (!resolved_klass->is_interface()) { |
acorn@6144 | 898 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 899 | } else { |
acorn@6144 | 900 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 901 | } |
duke@435 | 902 | assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); |
duke@435 | 903 | |
duke@435 | 904 | // check if static |
duke@435 | 905 | if (!resolved_method->is_static()) { |
jcoomes@2856 | 906 | ResourceMark rm(THREAD); |
duke@435 | 907 | char buf[200]; |
hseigel@4278 | 908 | jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 909 | resolved_method->name(), |
duke@435 | 910 | resolved_method->signature())); |
duke@435 | 911 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 912 | } |
duke@435 | 913 | } |
duke@435 | 914 | |
duke@435 | 915 | |
coleenp@2497 | 916 | void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, Symbol* method_name, |
coleenp@2497 | 917 | Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 918 | methodHandle resolved_method; |
duke@435 | 919 | linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 920 | runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK); |
duke@435 | 921 | } |
duke@435 | 922 | |
duke@435 | 923 | // throws linktime exceptions |
duke@435 | 924 | void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 925 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 926 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 927 | |
acorn@5786 | 928 | // Invokespecial is called for multiple special reasons: |
acorn@5786 | 929 | // <init> |
acorn@5786 | 930 | // local private method invocation, for classes and interfaces |
acorn@5786 | 931 | // superclass.method, which can also resolve to a default method |
acorn@5786 | 932 | // and the selected method is recalculated relative to the direct superclass |
acorn@5786 | 933 | // superinterface.method, which explicitly does not check shadowing |
kamg@4245 | 934 | |
acorn@6144 | 935 | if (!resolved_klass->is_interface()) { |
acorn@6144 | 936 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 937 | } else { |
acorn@6144 | 938 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
acorn@6144 | 939 | } |
duke@435 | 940 | |
duke@435 | 941 | // check if method name is <init>, that it is found in same klass as static type |
duke@435 | 942 | if (resolved_method->name() == vmSymbols::object_initializer_name() && |
duke@435 | 943 | resolved_method->method_holder() != resolved_klass()) { |
duke@435 | 944 | ResourceMark rm(THREAD); |
duke@435 | 945 | Exceptions::fthrow( |
duke@435 | 946 | THREAD_AND_LOCATION, |
coleenp@2497 | 947 | vmSymbols::java_lang_NoSuchMethodError(), |
duke@435 | 948 | "%s: method %s%s not found", |
duke@435 | 949 | resolved_klass->external_name(), |
duke@435 | 950 | resolved_method->name()->as_C_string(), |
duke@435 | 951 | resolved_method->signature()->as_C_string() |
duke@435 | 952 | ); |
duke@435 | 953 | return; |
duke@435 | 954 | } |
duke@435 | 955 | |
hseigel@6132 | 956 | // check if invokespecial's interface method reference is in an indirect superinterface |
hseigel@6132 | 957 | if (!current_klass.is_null() && resolved_klass->is_interface()) { |
hseigel@6132 | 958 | Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ? |
hseigel@6132 | 959 | current_klass() : |
hseigel@6132 | 960 | InstanceKlass::cast(current_klass())->host_klass(); |
hseigel@6164 | 961 | // As of the fix for 4486457 we disable verification for all of the |
hseigel@6164 | 962 | // dynamically-generated bytecodes associated with the 1.4 |
hseigel@6164 | 963 | // reflection implementation, not just those associated with |
hseigel@6164 | 964 | // sun/reflect/SerializationConstructorAccessor. |
hseigel@6164 | 965 | bool is_reflect = JDK_Version::is_gte_jdk14x_version() && |
hseigel@6164 | 966 | UseNewReflection && |
hseigel@6164 | 967 | klass_to_check->is_subclass_of( |
hseigel@6164 | 968 | SystemDictionary::reflect_MagicAccessorImpl_klass()); |
hseigel@6132 | 969 | |
hseigel@6164 | 970 | if (!is_reflect && |
hseigel@6164 | 971 | !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) { |
hseigel@6132 | 972 | ResourceMark rm(THREAD); |
hseigel@6132 | 973 | char buf[200]; |
hseigel@6132 | 974 | jio_snprintf(buf, sizeof(buf), |
hseigel@6132 | 975 | "Interface method reference: %s, is in an indirect superinterface of %s", |
hseigel@6132 | 976 | Method::name_and_sig_as_C_string(resolved_klass(), |
hseigel@6132 | 977 | resolved_method->name(), |
hseigel@6132 | 978 | resolved_method->signature()), |
hseigel@6132 | 979 | current_klass->external_name()); |
hseigel@6132 | 980 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
hseigel@6132 | 981 | } |
hseigel@6132 | 982 | } |
hseigel@6132 | 983 | |
duke@435 | 984 | // check if not static |
duke@435 | 985 | if (resolved_method->is_static()) { |
jcoomes@2856 | 986 | ResourceMark rm(THREAD); |
duke@435 | 987 | char buf[200]; |
duke@435 | 988 | jio_snprintf(buf, sizeof(buf), |
duke@435 | 989 | "Expecting non-static method %s", |
hseigel@4278 | 990 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 991 | resolved_method->name(), |
duke@435 | 992 | resolved_method->signature())); |
duke@435 | 993 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 994 | } |
acorn@5848 | 995 | |
acorn@5786 | 996 | if (TraceItables && Verbose) { |
acorn@5786 | 997 | ResourceMark rm(THREAD); |
acorn@5786 | 998 | tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 999 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5786 | 1000 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1001 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1002 | resolved_method->name(), |
acorn@5786 | 1003 | resolved_method->signature()), |
acorn@5786 | 1004 | resolved_method->method_holder()->internal_name() |
acorn@5786 | 1005 | ); |
acorn@5786 | 1006 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 1007 | if (resolved_method->is_default_method()) { |
acorn@6080 | 1008 | tty->print("default "); |
acorn@5786 | 1009 | } |
acorn@5786 | 1010 | if (resolved_method->is_overpass()) { |
acorn@5786 | 1011 | tty->print("overpass"); |
acorn@5786 | 1012 | } |
acorn@5786 | 1013 | tty->cr(); |
acorn@5786 | 1014 | } |
duke@435 | 1015 | } |
duke@435 | 1016 | |
duke@435 | 1017 | // throws runtime exceptions |
duke@435 | 1018 | void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, |
duke@435 | 1019 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 1020 | |
duke@435 | 1021 | // resolved method is selected method unless we have an old-style lookup |
acorn@5786 | 1022 | // for a superclass method |
acorn@5786 | 1023 | // Invokespecial for a superinterface, resolved method is selected method, |
acorn@5786 | 1024 | // no checks for shadowing |
duke@435 | 1025 | methodHandle sel_method(THREAD, resolved_method()); |
duke@435 | 1026 | |
duke@435 | 1027 | // check if this is an old-style super call and do a new lookup if so |
duke@435 | 1028 | { KlassHandle method_klass = KlassHandle(THREAD, |
duke@435 | 1029 | resolved_method->method_holder()); |
duke@435 | 1030 | |
acorn@5786 | 1031 | if (check_access && |
duke@435 | 1032 | // a) check if ACC_SUPER flag is set for the current class |
kamg@4702 | 1033 | (current_klass->is_super() || !AllowNonVirtualCalls) && |
acorn@5786 | 1034 | // b) check if the class of the resolved_klass is a superclass |
acorn@5786 | 1035 | // (not supertype in order to exclude interface classes) of the current class. |
acorn@5786 | 1036 | // This check is not performed for super.invoke for interface methods |
acorn@5786 | 1037 | // in super interfaces. |
acorn@5786 | 1038 | current_klass->is_subclass_of(resolved_klass()) && |
acorn@5786 | 1039 | current_klass() != resolved_klass() && |
duke@435 | 1040 | // c) check if the method is not <init> |
duke@435 | 1041 | resolved_method->name() != vmSymbols::object_initializer_name()) { |
duke@435 | 1042 | // Lookup super method |
duke@435 | 1043 | KlassHandle super_klass(THREAD, current_klass->super()); |
duke@435 | 1044 | lookup_instance_method_in_klasses(sel_method, super_klass, |
coleenp@2497 | 1045 | resolved_method->name(), |
coleenp@2497 | 1046 | resolved_method->signature(), CHECK); |
duke@435 | 1047 | // check if found |
duke@435 | 1048 | if (sel_method.is_null()) { |
duke@435 | 1049 | ResourceMark rm(THREAD); |
duke@435 | 1050 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1051 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1052 | resolved_method->name(), |
duke@435 | 1053 | resolved_method->signature())); |
duke@435 | 1054 | } |
duke@435 | 1055 | } |
duke@435 | 1056 | } |
duke@435 | 1057 | |
duke@435 | 1058 | // check if not static |
duke@435 | 1059 | if (sel_method->is_static()) { |
jcoomes@2856 | 1060 | ResourceMark rm(THREAD); |
duke@435 | 1061 | char buf[200]; |
hseigel@4278 | 1062 | jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1063 | resolved_method->name(), |
duke@435 | 1064 | resolved_method->signature())); |
duke@435 | 1065 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1066 | } |
duke@435 | 1067 | |
duke@435 | 1068 | // check if abstract |
duke@435 | 1069 | if (sel_method->is_abstract()) { |
duke@435 | 1070 | ResourceMark rm(THREAD); |
duke@435 | 1071 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1072 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1073 | sel_method->name(), |
duke@435 | 1074 | sel_method->signature())); |
duke@435 | 1075 | } |
duke@435 | 1076 | |
acorn@5786 | 1077 | if (TraceItables && Verbose) { |
acorn@5786 | 1078 | ResourceMark rm(THREAD); |
acorn@5786 | 1079 | tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 1080 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1081 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1082 | sel_method->name(), |
acorn@5786 | 1083 | sel_method->signature()), |
acorn@5786 | 1084 | sel_method->method_holder()->internal_name() |
acorn@5786 | 1085 | ); |
acorn@5786 | 1086 | sel_method->access_flags().print_on(tty); |
acorn@5848 | 1087 | if (sel_method->is_default_method()) { |
acorn@6080 | 1088 | tty->print("default "); |
acorn@5786 | 1089 | } |
acorn@5848 | 1090 | if (sel_method->is_overpass()) { |
acorn@5848 | 1091 | tty->print("overpass"); |
acorn@5848 | 1092 | } |
acorn@5786 | 1093 | tty->cr(); |
acorn@5786 | 1094 | } |
acorn@5786 | 1095 | |
duke@435 | 1096 | // setup result |
duke@435 | 1097 | result.set_static(resolved_klass, sel_method, CHECK); |
duke@435 | 1098 | } |
duke@435 | 1099 | |
duke@435 | 1100 | void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass, |
coleenp@2497 | 1101 | Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 1102 | bool check_access, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1103 | methodHandle resolved_method; |
duke@435 | 1104 | linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 1105 | runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK); |
duke@435 | 1106 | } |
duke@435 | 1107 | |
duke@435 | 1108 | // throws linktime exceptions |
duke@435 | 1109 | void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 1110 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 1111 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 1112 | // normal method resolution |
acorn@5897 | 1113 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
duke@435 | 1114 | |
duke@435 | 1115 | assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); |
duke@435 | 1116 | assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); |
duke@435 | 1117 | |
acorn@5786 | 1118 | // check if private interface method |
acorn@5786 | 1119 | if (resolved_klass->is_interface() && resolved_method->is_private()) { |
acorn@5786 | 1120 | ResourceMark rm(THREAD); |
acorn@5786 | 1121 | char buf[200]; |
acorn@5786 | 1122 | jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", |
acorn@5786 | 1123 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1124 | resolved_method->name(), |
acorn@5786 | 1125 | resolved_method->signature()), |
acorn@5786 | 1126 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name())); |
acorn@5786 | 1127 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5786 | 1128 | } |
acorn@5786 | 1129 | |
duke@435 | 1130 | // check if not static |
duke@435 | 1131 | if (resolved_method->is_static()) { |
jcoomes@2856 | 1132 | ResourceMark rm(THREAD); |
duke@435 | 1133 | char buf[200]; |
hseigel@4278 | 1134 | jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1135 | resolved_method->name(), |
duke@435 | 1136 | resolved_method->signature())); |
duke@435 | 1137 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1138 | } |
acorn@5786 | 1139 | |
acorn@5848 | 1140 | if (PrintVtables && Verbose) { |
acorn@5848 | 1141 | ResourceMark rm(THREAD); |
acorn@5848 | 1142 | tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5848 | 1143 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5848 | 1144 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5848 | 1145 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5848 | 1146 | resolved_method->name(), |
acorn@5848 | 1147 | resolved_method->signature()), |
acorn@5848 | 1148 | resolved_method->method_holder()->internal_name() |
acorn@5848 | 1149 | ); |
acorn@5848 | 1150 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 1151 | if (resolved_method->is_default_method()) { |
acorn@6080 | 1152 | tty->print("default "); |
acorn@5848 | 1153 | } |
acorn@5848 | 1154 | if (resolved_method->is_overpass()) { |
acorn@5848 | 1155 | tty->print("overpass"); |
acorn@5848 | 1156 | } |
acorn@5848 | 1157 | tty->cr(); |
acorn@5848 | 1158 | } |
duke@435 | 1159 | } |
duke@435 | 1160 | |
duke@435 | 1161 | // throws runtime exceptions |
duke@435 | 1162 | void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, |
duke@435 | 1163 | methodHandle resolved_method, |
duke@435 | 1164 | KlassHandle resolved_klass, |
duke@435 | 1165 | Handle recv, |
duke@435 | 1166 | KlassHandle recv_klass, |
duke@435 | 1167 | bool check_null_and_abstract, |
duke@435 | 1168 | TRAPS) { |
duke@435 | 1169 | |
duke@435 | 1170 | // setup default return values |
coleenp@4037 | 1171 | int vtable_index = Method::invalid_vtable_index; |
duke@435 | 1172 | methodHandle selected_method; |
duke@435 | 1173 | |
duke@435 | 1174 | assert(recv.is_null() || recv->is_oop(), "receiver is not an oop"); |
duke@435 | 1175 | |
duke@435 | 1176 | // runtime method resolution |
duke@435 | 1177 | if (check_null_and_abstract && recv.is_null()) { // check if receiver exists |
duke@435 | 1178 | THROW(vmSymbols::java_lang_NullPointerException()); |
duke@435 | 1179 | } |
duke@435 | 1180 | |
coleenp@4037 | 1181 | // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s |
duke@435 | 1182 | // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since |
duke@435 | 1183 | // a missing receiver might result in a bogus lookup. |
coleenp@4251 | 1184 | assert(resolved_method->method_holder()->is_linked(), "must be linked"); |
duke@435 | 1185 | |
duke@435 | 1186 | // do lookup based on receiver klass using the vtable index |
coleenp@4037 | 1187 | if (resolved_method->method_holder()->is_interface()) { // miranda method |
acorn@5848 | 1188 | vtable_index = vtable_index_of_interface_method(resolved_klass, |
acorn@6080 | 1189 | resolved_method); |
duke@435 | 1190 | assert(vtable_index >= 0 , "we should have valid vtable index at this point"); |
duke@435 | 1191 | |
coleenp@4037 | 1192 | InstanceKlass* inst = InstanceKlass::cast(recv_klass()); |
duke@435 | 1193 | selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
duke@435 | 1194 | } else { |
duke@435 | 1195 | // at this point we are sure that resolved_method is virtual and not |
duke@435 | 1196 | // a miranda method; therefore, it must have a valid vtable index. |
drchase@5732 | 1197 | assert(!resolved_method->has_itable_index(), ""); |
duke@435 | 1198 | vtable_index = resolved_method->vtable_index(); |
duke@435 | 1199 | // We could get a negative vtable_index for final methods, |
duke@435 | 1200 | // because as an optimization they are they are never put in the vtable, |
duke@435 | 1201 | // unless they override an existing method. |
duke@435 | 1202 | // If we do get a negative, it means the resolved method is the the selected |
duke@435 | 1203 | // method, and it can never be changed by an override. |
coleenp@4037 | 1204 | if (vtable_index == Method::nonvirtual_vtable_index) { |
duke@435 | 1205 | assert(resolved_method->can_be_statically_bound(), "cannot override this method"); |
duke@435 | 1206 | selected_method = resolved_method; |
duke@435 | 1207 | } else { |
duke@435 | 1208 | // recv_klass might be an arrayKlassOop but all vtables start at |
duke@435 | 1209 | // the same place. The cast is to avoid virtual call and assertion. |
coleenp@4037 | 1210 | InstanceKlass* inst = (InstanceKlass*)recv_klass(); |
duke@435 | 1211 | selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
duke@435 | 1212 | } |
duke@435 | 1213 | } |
duke@435 | 1214 | |
duke@435 | 1215 | // check if method exists |
duke@435 | 1216 | if (selected_method.is_null()) { |
duke@435 | 1217 | ResourceMark rm(THREAD); |
duke@435 | 1218 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1219 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1220 | resolved_method->name(), |
duke@435 | 1221 | resolved_method->signature())); |
duke@435 | 1222 | } |
duke@435 | 1223 | |
duke@435 | 1224 | // check if abstract |
duke@435 | 1225 | if (check_null_and_abstract && selected_method->is_abstract()) { |
duke@435 | 1226 | ResourceMark rm(THREAD); |
duke@435 | 1227 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1228 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1229 | selected_method->name(), |
duke@435 | 1230 | selected_method->signature())); |
duke@435 | 1231 | } |
duke@435 | 1232 | |
acorn@5786 | 1233 | if (PrintVtables && Verbose) { |
acorn@5786 | 1234 | ResourceMark rm(THREAD); |
acorn@5786 | 1235 | tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ", |
acorn@5786 | 1236 | (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()), |
acorn@5786 | 1237 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1238 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1239 | resolved_method->name(), |
acorn@5786 | 1240 | resolved_method->signature()), |
acorn@5786 | 1241 | selected_method->method_holder()->internal_name(), |
acorn@5786 | 1242 | vtable_index |
acorn@5786 | 1243 | ); |
acorn@5786 | 1244 | selected_method->access_flags().print_on(tty); |
acorn@5848 | 1245 | if (selected_method->is_default_method()) { |
acorn@6080 | 1246 | tty->print("default "); |
acorn@5786 | 1247 | } |
acorn@5848 | 1248 | if (selected_method->is_overpass()) { |
acorn@5786 | 1249 | tty->print("overpass"); |
acorn@5786 | 1250 | } |
acorn@5786 | 1251 | tty->cr(); |
acorn@5786 | 1252 | } |
duke@435 | 1253 | // setup result |
duke@435 | 1254 | result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); |
duke@435 | 1255 | } |
duke@435 | 1256 | |
duke@435 | 1257 | void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, |
coleenp@2497 | 1258 | Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 1259 | bool check_access, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1260 | methodHandle resolved_method; |
duke@435 | 1261 | linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 1262 | runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK); |
duke@435 | 1263 | } |
duke@435 | 1264 | |
duke@435 | 1265 | // throws linktime exceptions |
coleenp@2497 | 1266 | void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, |
coleenp@2497 | 1267 | Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 1268 | // normal interface method resolution |
acorn@6144 | 1269 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
duke@435 | 1270 | |
duke@435 | 1271 | assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); |
duke@435 | 1272 | assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); |
duke@435 | 1273 | } |
duke@435 | 1274 | |
duke@435 | 1275 | // throws runtime exceptions |
duke@435 | 1276 | void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, |
duke@435 | 1277 | Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1278 | // check if receiver exists |
duke@435 | 1279 | if (check_null_and_abstract && recv.is_null()) { |
duke@435 | 1280 | THROW(vmSymbols::java_lang_NullPointerException()); |
duke@435 | 1281 | } |
duke@435 | 1282 | |
acorn@5786 | 1283 | // check if private interface method |
acorn@5786 | 1284 | if (resolved_klass->is_interface() && resolved_method->is_private()) { |
acorn@5786 | 1285 | ResourceMark rm(THREAD); |
acorn@5786 | 1286 | char buf[200]; |
acorn@5786 | 1287 | jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s", |
acorn@5786 | 1288 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1289 | resolved_method->name(), |
acorn@5786 | 1290 | resolved_method->signature())); |
acorn@5786 | 1291 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5786 | 1292 | } |
acorn@5786 | 1293 | |
duke@435 | 1294 | // check if receiver klass implements the resolved interface |
duke@435 | 1295 | if (!recv_klass->is_subtype_of(resolved_klass())) { |
jcoomes@2856 | 1296 | ResourceMark rm(THREAD); |
duke@435 | 1297 | char buf[200]; |
duke@435 | 1298 | jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", |
hseigel@4278 | 1299 | recv_klass()->external_name(), |
hseigel@4278 | 1300 | resolved_klass()->external_name()); |
duke@435 | 1301 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1302 | } |
hseigel@6195 | 1303 | |
duke@435 | 1304 | // do lookup based on receiver klass |
duke@435 | 1305 | methodHandle sel_method; |
hseigel@6195 | 1306 | // This search must match the linktime preparation search for itable initialization |
hseigel@6195 | 1307 | // to correctly enforce loader constraints for interface method inheritance |
duke@435 | 1308 | lookup_instance_method_in_klasses(sel_method, recv_klass, |
coleenp@2497 | 1309 | resolved_method->name(), |
coleenp@2497 | 1310 | resolved_method->signature(), CHECK); |
drchase@5732 | 1311 | if (sel_method.is_null() && !check_null_and_abstract) { |
drchase@5732 | 1312 | // In theory this is a harmless placeholder value, but |
drchase@5732 | 1313 | // in practice leaving in null affects the nsk default method tests. |
drchase@5732 | 1314 | // This needs further study. |
drchase@5732 | 1315 | sel_method = resolved_method; |
drchase@5732 | 1316 | } |
duke@435 | 1317 | // check if method exists |
duke@435 | 1318 | if (sel_method.is_null()) { |
duke@435 | 1319 | ResourceMark rm(THREAD); |
duke@435 | 1320 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1321 | Method::name_and_sig_as_C_string(recv_klass(), |
duke@435 | 1322 | resolved_method->name(), |
duke@435 | 1323 | resolved_method->signature())); |
duke@435 | 1324 | } |
bharadwaj@4960 | 1325 | // check access |
acorn@5786 | 1326 | // Throw Illegal Access Error if sel_method is not public. |
acorn@5786 | 1327 | if (!sel_method->is_public()) { |
acorn@5786 | 1328 | ResourceMark rm(THREAD); |
acorn@5786 | 1329 | THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), |
acorn@5786 | 1330 | Method::name_and_sig_as_C_string(recv_klass(), |
acorn@5786 | 1331 | sel_method->name(), |
acorn@5786 | 1332 | sel_method->signature())); |
bharadwaj@4960 | 1333 | } |
duke@435 | 1334 | // check if abstract |
duke@435 | 1335 | if (check_null_and_abstract && sel_method->is_abstract()) { |
duke@435 | 1336 | ResourceMark rm(THREAD); |
duke@435 | 1337 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1338 | Method::name_and_sig_as_C_string(recv_klass(), |
duke@435 | 1339 | sel_method->name(), |
duke@435 | 1340 | sel_method->signature())); |
duke@435 | 1341 | } |
acorn@5786 | 1342 | |
acorn@5786 | 1343 | if (TraceItables && Verbose) { |
acorn@5786 | 1344 | ResourceMark rm(THREAD); |
acorn@5786 | 1345 | tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 1346 | (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()), |
acorn@5786 | 1347 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1348 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1349 | resolved_method->name(), |
acorn@5786 | 1350 | resolved_method->signature()), |
acorn@5786 | 1351 | sel_method->method_holder()->internal_name() |
acorn@5786 | 1352 | ); |
acorn@5786 | 1353 | sel_method->access_flags().print_on(tty); |
acorn@5848 | 1354 | if (sel_method->is_default_method()) { |
acorn@6080 | 1355 | tty->print("default "); |
acorn@5786 | 1356 | } |
acorn@5848 | 1357 | if (sel_method->is_overpass()) { |
acorn@5786 | 1358 | tty->print("overpass"); |
acorn@5786 | 1359 | } |
acorn@5786 | 1360 | tty->cr(); |
acorn@5786 | 1361 | } |
acorn@6080 | 1362 | // setup result |
acorn@6080 | 1363 | if (!resolved_method->has_itable_index()) { |
acorn@6080 | 1364 | int vtable_index = resolved_method->vtable_index(); |
acorn@6080 | 1365 | assert(vtable_index == sel_method->vtable_index(), "sanity check"); |
acorn@6080 | 1366 | result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); |
acorn@6080 | 1367 | } else { |
acorn@6080 | 1368 | int itable_index = resolved_method()->itable_index(); |
acorn@6080 | 1369 | result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); |
acorn@6080 | 1370 | } |
duke@435 | 1371 | } |
duke@435 | 1372 | |
duke@435 | 1373 | |
duke@435 | 1374 | methodHandle LinkResolver::linktime_resolve_interface_method_or_null( |
duke@435 | 1375 | KlassHandle resolved_klass, |
coleenp@2497 | 1376 | Symbol* method_name, |
coleenp@2497 | 1377 | Symbol* method_signature, |
duke@435 | 1378 | KlassHandle current_klass, |
duke@435 | 1379 | bool check_access) { |
duke@435 | 1380 | EXCEPTION_MARK; |
duke@435 | 1381 | methodHandle method_result; |
duke@435 | 1382 | linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD); |
duke@435 | 1383 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1384 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1385 | return methodHandle(); |
duke@435 | 1386 | } else { |
duke@435 | 1387 | return method_result; |
duke@435 | 1388 | } |
duke@435 | 1389 | } |
duke@435 | 1390 | |
duke@435 | 1391 | methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( |
duke@435 | 1392 | KlassHandle resolved_klass, |
coleenp@2497 | 1393 | Symbol* method_name, |
coleenp@2497 | 1394 | Symbol* method_signature, |
duke@435 | 1395 | KlassHandle current_klass, |
duke@435 | 1396 | bool check_access) { |
duke@435 | 1397 | EXCEPTION_MARK; |
duke@435 | 1398 | methodHandle method_result; |
duke@435 | 1399 | linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD); |
duke@435 | 1400 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1401 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1402 | return methodHandle(); |
duke@435 | 1403 | } else { |
duke@435 | 1404 | return method_result; |
duke@435 | 1405 | } |
duke@435 | 1406 | } |
duke@435 | 1407 | |
duke@435 | 1408 | methodHandle LinkResolver::resolve_virtual_call_or_null( |
duke@435 | 1409 | KlassHandle receiver_klass, |
duke@435 | 1410 | KlassHandle resolved_klass, |
coleenp@2497 | 1411 | Symbol* name, |
coleenp@2497 | 1412 | Symbol* signature, |
duke@435 | 1413 | KlassHandle current_klass) { |
duke@435 | 1414 | EXCEPTION_MARK; |
duke@435 | 1415 | CallInfo info; |
duke@435 | 1416 | resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD); |
duke@435 | 1417 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1418 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1419 | return methodHandle(); |
duke@435 | 1420 | } |
duke@435 | 1421 | return info.selected_method(); |
duke@435 | 1422 | } |
duke@435 | 1423 | |
duke@435 | 1424 | methodHandle LinkResolver::resolve_interface_call_or_null( |
duke@435 | 1425 | KlassHandle receiver_klass, |
duke@435 | 1426 | KlassHandle resolved_klass, |
coleenp@2497 | 1427 | Symbol* name, |
coleenp@2497 | 1428 | Symbol* signature, |
duke@435 | 1429 | KlassHandle current_klass) { |
duke@435 | 1430 | EXCEPTION_MARK; |
duke@435 | 1431 | CallInfo info; |
duke@435 | 1432 | resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD); |
duke@435 | 1433 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1434 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1435 | return methodHandle(); |
duke@435 | 1436 | } |
duke@435 | 1437 | return info.selected_method(); |
duke@435 | 1438 | } |
duke@435 | 1439 | |
duke@435 | 1440 | int LinkResolver::resolve_virtual_vtable_index( |
duke@435 | 1441 | KlassHandle receiver_klass, |
duke@435 | 1442 | KlassHandle resolved_klass, |
coleenp@2497 | 1443 | Symbol* name, |
coleenp@2497 | 1444 | Symbol* signature, |
duke@435 | 1445 | KlassHandle current_klass) { |
duke@435 | 1446 | EXCEPTION_MARK; |
duke@435 | 1447 | CallInfo info; |
duke@435 | 1448 | resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD); |
duke@435 | 1449 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1450 | CLEAR_PENDING_EXCEPTION; |
coleenp@4037 | 1451 | return Method::invalid_vtable_index; |
duke@435 | 1452 | } |
duke@435 | 1453 | return info.vtable_index(); |
duke@435 | 1454 | } |
duke@435 | 1455 | |
duke@435 | 1456 | methodHandle LinkResolver::resolve_static_call_or_null( |
duke@435 | 1457 | KlassHandle resolved_klass, |
coleenp@2497 | 1458 | Symbol* name, |
coleenp@2497 | 1459 | Symbol* signature, |
duke@435 | 1460 | KlassHandle current_klass) { |
duke@435 | 1461 | EXCEPTION_MARK; |
duke@435 | 1462 | CallInfo info; |
duke@435 | 1463 | resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD); |
duke@435 | 1464 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1465 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1466 | return methodHandle(); |
duke@435 | 1467 | } |
duke@435 | 1468 | return info.selected_method(); |
duke@435 | 1469 | } |
duke@435 | 1470 | |
coleenp@2497 | 1471 | methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, Symbol* name, Symbol* signature, |
duke@435 | 1472 | KlassHandle current_klass) { |
duke@435 | 1473 | EXCEPTION_MARK; |
duke@435 | 1474 | CallInfo info; |
duke@435 | 1475 | resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD); |
duke@435 | 1476 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1477 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1478 | return methodHandle(); |
duke@435 | 1479 | } |
duke@435 | 1480 | return info.selected_method(); |
duke@435 | 1481 | } |
duke@435 | 1482 | |
duke@435 | 1483 | |
duke@435 | 1484 | |
duke@435 | 1485 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 1486 | // ConstantPool entries |
duke@435 | 1487 | |
duke@435 | 1488 | void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) { |
duke@435 | 1489 | switch (byte) { |
duke@435 | 1490 | case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; |
duke@435 | 1491 | case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break; |
duke@435 | 1492 | case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; |
twisti@3969 | 1493 | case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; |
jrose@1161 | 1494 | case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; |
duke@435 | 1495 | case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; |
duke@435 | 1496 | } |
duke@435 | 1497 | return; |
duke@435 | 1498 | } |
duke@435 | 1499 | |
coleenp@2497 | 1500 | void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, |
duke@435 | 1501 | KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1502 | // resolve klass |
duke@435 | 1503 | resolve_klass(resolved_klass, pool, index, CHECK); |
duke@435 | 1504 | |
duke@435 | 1505 | // Get name, signature, and static klass |
coleenp@2497 | 1506 | method_name = pool->name_ref_at(index); |
coleenp@2497 | 1507 | method_signature = pool->signature_ref_at(index); |
duke@435 | 1508 | current_klass = KlassHandle(THREAD, pool->pool_holder()); |
duke@435 | 1509 | } |
duke@435 | 1510 | |
duke@435 | 1511 | |
duke@435 | 1512 | void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1513 | KlassHandle resolved_klass; |
coleenp@2497 | 1514 | Symbol* method_name = NULL; |
coleenp@2497 | 1515 | Symbol* method_signature = NULL; |
duke@435 | 1516 | KlassHandle current_klass; |
duke@435 | 1517 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
duke@435 | 1518 | resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1519 | } |
duke@435 | 1520 | |
duke@435 | 1521 | |
duke@435 | 1522 | void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1523 | KlassHandle resolved_klass; |
coleenp@2497 | 1524 | Symbol* method_name = NULL; |
coleenp@2497 | 1525 | Symbol* method_signature = NULL; |
duke@435 | 1526 | KlassHandle current_klass; |
duke@435 | 1527 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
duke@435 | 1528 | resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK); |
duke@435 | 1529 | } |
duke@435 | 1530 | |
duke@435 | 1531 | |
duke@435 | 1532 | void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, |
duke@435 | 1533 | constantPoolHandle pool, int index, |
duke@435 | 1534 | TRAPS) { |
duke@435 | 1535 | |
duke@435 | 1536 | KlassHandle resolved_klass; |
coleenp@2497 | 1537 | Symbol* method_name = NULL; |
coleenp@2497 | 1538 | Symbol* method_signature = NULL; |
duke@435 | 1539 | KlassHandle current_klass; |
duke@435 | 1540 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
coleenp@4037 | 1541 | KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); |
duke@435 | 1542 | resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1543 | } |
duke@435 | 1544 | |
duke@435 | 1545 | |
duke@435 | 1546 | void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1547 | KlassHandle resolved_klass; |
coleenp@2497 | 1548 | Symbol* method_name = NULL; |
coleenp@2497 | 1549 | Symbol* method_signature = NULL; |
duke@435 | 1550 | KlassHandle current_klass; |
duke@435 | 1551 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
coleenp@4037 | 1552 | KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); |
duke@435 | 1553 | resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1554 | } |
duke@435 | 1555 | |
jrose@1161 | 1556 | |
twisti@3969 | 1557 | void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
jrose@1161 | 1558 | assert(EnableInvokeDynamic, ""); |
twisti@3969 | 1559 | // This guy is reached from InterpreterRuntime::resolve_invokehandle. |
twisti@3969 | 1560 | KlassHandle resolved_klass; |
twisti@3969 | 1561 | Symbol* method_name = NULL; |
twisti@3969 | 1562 | Symbol* method_signature = NULL; |
twisti@3969 | 1563 | KlassHandle current_klass; |
twisti@3969 | 1564 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
kmo@4910 | 1565 | if (TraceMethodHandles) { |
kmo@4910 | 1566 | ResourceMark rm(THREAD); |
twisti@3969 | 1567 | tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string()); |
kmo@4910 | 1568 | } |
twisti@3969 | 1569 | resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK); |
twisti@3969 | 1570 | } |
jrose@1161 | 1571 | |
twisti@3969 | 1572 | void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass, |
twisti@3969 | 1573 | Symbol* method_name, Symbol* method_signature, |
twisti@3969 | 1574 | KlassHandle current_klass, |
twisti@3969 | 1575 | TRAPS) { |
twisti@3969 | 1576 | // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar |
twisti@3969 | 1577 | assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), ""); |
twisti@3969 | 1578 | assert(MethodHandles::is_signature_polymorphic_name(method_name), ""); |
twisti@3969 | 1579 | methodHandle resolved_method; |
twisti@4133 | 1580 | Handle resolved_appendix; |
twisti@4133 | 1581 | Handle resolved_method_type; |
twisti@3969 | 1582 | lookup_polymorphic_method(resolved_method, resolved_klass, |
twisti@3969 | 1583 | method_name, method_signature, |
twisti@4133 | 1584 | current_klass, &resolved_appendix, &resolved_method_type, CHECK); |
twisti@4133 | 1585 | result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK); |
twisti@3969 | 1586 | } |
jrose@1161 | 1587 | |
jrose@1161 | 1588 | |
twisti@3969 | 1589 | void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
twisti@3969 | 1590 | assert(EnableInvokeDynamic, ""); |
twisti@3969 | 1591 | |
twisti@4133 | 1592 | //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK); |
twisti@3969 | 1593 | Symbol* method_name = pool->name_ref_at(index); |
twisti@3969 | 1594 | Symbol* method_signature = pool->signature_ref_at(index); |
twisti@3969 | 1595 | KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder()); |
twisti@3969 | 1596 | |
twisti@3969 | 1597 | // Resolve the bootstrap specifier (BSM + optional arguments). |
twisti@3969 | 1598 | Handle bootstrap_specifier; |
twisti@3969 | 1599 | // Check if CallSite has been bound already: |
coleenp@4037 | 1600 | ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); |
twisti@3969 | 1601 | if (cpce->is_f1_null()) { |
coleenp@4037 | 1602 | int pool_index = cpce->constant_pool_index(); |
twisti@3969 | 1603 | oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, CHECK); |
twisti@3969 | 1604 | assert(bsm_info != NULL, ""); |
twisti@3969 | 1605 | // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic. |
twisti@3969 | 1606 | bootstrap_specifier = Handle(THREAD, bsm_info); |
twisti@3969 | 1607 | } |
twisti@3969 | 1608 | if (!cpce->is_f1_null()) { |
twisti@4133 | 1609 | methodHandle method( THREAD, cpce->f1_as_method()); |
twisti@4133 | 1610 | Handle appendix( THREAD, cpce->appendix_if_resolved(pool)); |
twisti@4133 | 1611 | Handle method_type(THREAD, cpce->method_type_if_resolved(pool)); |
twisti@4133 | 1612 | result.set_handle(method, appendix, method_type, CHECK); |
twisti@3969 | 1613 | return; |
twisti@3969 | 1614 | } |
twisti@3969 | 1615 | |
twisti@3969 | 1616 | if (TraceMethodHandles) { |
drchase@5732 | 1617 | ResourceMark rm(THREAD); |
drchase@5732 | 1618 | tty->print_cr("resolve_invokedynamic #%d %s %s", |
coleenp@4037 | 1619 | ConstantPool::decode_invokedynamic_index(index), |
twisti@3969 | 1620 | method_name->as_C_string(), method_signature->as_C_string()); |
twisti@3969 | 1621 | tty->print(" BSM info: "); bootstrap_specifier->print(); |
twisti@3969 | 1622 | } |
twisti@3969 | 1623 | |
twisti@3969 | 1624 | resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK); |
twisti@3969 | 1625 | } |
twisti@3969 | 1626 | |
twisti@3969 | 1627 | void LinkResolver::resolve_dynamic_call(CallInfo& result, |
twisti@3969 | 1628 | Handle bootstrap_specifier, |
twisti@3969 | 1629 | Symbol* method_name, Symbol* method_signature, |
twisti@3969 | 1630 | KlassHandle current_klass, |
twisti@3969 | 1631 | TRAPS) { |
twisti@3969 | 1632 | // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...) |
twisti@3969 | 1633 | // The appendix argument is likely to be a freshly-created CallSite. |
twisti@3969 | 1634 | Handle resolved_appendix; |
twisti@4133 | 1635 | Handle resolved_method_type; |
twisti@3969 | 1636 | methodHandle resolved_method = |
twisti@3969 | 1637 | SystemDictionary::find_dynamic_call_site_invoker(current_klass, |
twisti@3969 | 1638 | bootstrap_specifier, |
twisti@3969 | 1639 | method_name, method_signature, |
twisti@3969 | 1640 | &resolved_appendix, |
twisti@4133 | 1641 | &resolved_method_type, |
twisti@4041 | 1642 | THREAD); |
jrose@2941 | 1643 | if (HAS_PENDING_EXCEPTION) { |
twisti@3969 | 1644 | if (TraceMethodHandles) { |
drchase@6680 | 1645 | tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); |
twisti@3969 | 1646 | PENDING_EXCEPTION->print(); |
twisti@3969 | 1647 | } |
jrose@2941 | 1648 | if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) { |
jrose@2941 | 1649 | // throw these guys, since they are already wrapped |
jrose@2941 | 1650 | return; |
jrose@2941 | 1651 | } |
jrose@2941 | 1652 | if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { |
jrose@2941 | 1653 | // intercept only LinkageErrors which might have failed to wrap |
jrose@2941 | 1654 | return; |
jrose@2941 | 1655 | } |
jrose@2941 | 1656 | // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS. |
twisti@3969 | 1657 | Handle nested_exception(THREAD, PENDING_EXCEPTION); |
jrose@2941 | 1658 | CLEAR_PENDING_EXCEPTION; |
twisti@4041 | 1659 | THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) |
jrose@2941 | 1660 | } |
twisti@4133 | 1661 | result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK); |
jrose@1161 | 1662 | } |
jrose@1161 | 1663 | |
duke@435 | 1664 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 1665 | #ifndef PRODUCT |
duke@435 | 1666 | |
drchase@5732 | 1667 | void CallInfo::print() { |
duke@435 | 1668 | ResourceMark rm; |
drchase@5732 | 1669 | const char* kindstr = "unknown"; |
drchase@5732 | 1670 | switch (_call_kind) { |
drchase@5732 | 1671 | case direct_call: kindstr = "direct"; break; |
drchase@5732 | 1672 | case vtable_call: kindstr = "vtable"; break; |
drchase@5732 | 1673 | case itable_call: kindstr = "itable"; break; |
drchase@5732 | 1674 | } |
drchase@5732 | 1675 | tty->print_cr("Call %s@%d %s", kindstr, _call_index, |
drchase@5732 | 1676 | _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string()); |
duke@435 | 1677 | } |
duke@435 | 1678 | |
duke@435 | 1679 | #endif |