1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "classfile/stringTable.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "interpreter/linkResolver.hpp" 28 #include "jvmci/compilerRuntime.hpp" 29 #include "oops/cpCache.inline.hpp" 30 #include "oops/oop.inline.hpp" 31 #include "runtime/compilationPolicy.hpp" 32 #include "runtime/frame.inline.hpp" 33 #include "runtime/deoptimization.hpp" 34 #include "runtime/interfaceSupport.inline.hpp" 35 #include "runtime/vframe.inline.hpp" 36 #include "aot/aotLoader.hpp" 37 38 // Resolve and allocate String 39 JRT_BLOCK_ENTRY(void, CompilerRuntime::resolve_string_by_symbol(JavaThread *thread, void* string_result, const char* name)) 40 JRT_BLOCK 41 oop str = *(oop*)string_result; // Is it resolved already? 42 if (str == NULL) { // Do resolution 43 // First 2 bytes of name contains length (number of bytes). 44 int len = build_u2_from((address)name); 45 name += 2; 46 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK); 47 str = StringTable::intern(sym, CHECK); 48 assert(java_lang_String::is_instance(str), "must be string"); 49 *(oop*)string_result = str; // Store result 50 } 51 assert(str != NULL, "Should be allocated!"); 52 thread->set_vm_result(str); 53 JRT_BLOCK_END 54 JRT_END 55 56 57 58 Klass* CompilerRuntime::resolve_klass_helper(JavaThread *thread, const char* name, int len, TRAPS) { 59 ResourceMark rm(THREAD); 60 // last java frame on stack (which includes native call frames) 61 RegisterMap cbl_map(thread, false); 62 // Skip stub 63 frame caller_frame = thread->last_frame().sender(&cbl_map); 64 CodeBlob* caller_cb = caller_frame.cb(); 65 guarantee(caller_cb != NULL && caller_cb->is_compiled(), "must be called from compiled method"); 66 CompiledMethod* caller_nm = caller_cb->as_compiled_method_or_null(); 67 methodHandle caller(THREAD, caller_nm->method()); 68 69 // Use class loader of aot method. 70 Handle loader(THREAD, caller->method_holder()->class_loader()); 71 Handle protection_domain(THREAD, caller->method_holder()->protection_domain()); 72 73 // Ignore wrapping L and ; 74 if (name[0] == 'L') { 75 assert(len > 2, "small name %s", name); 76 name++; 77 len -= 2; 78 } 79 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK_NULL); 80 if (sym == NULL) { 81 return NULL; 82 } 83 Klass* k = SystemDictionary::resolve_or_fail(sym, loader, protection_domain, true, CHECK_NULL); 84 85 return k; 86 } 87 88 // Resolve Klass 89 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::resolve_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 90 Klass* k = NULL; 91 JRT_BLOCK 92 k = *klass_result; // Is it resolved already? 93 if (k == NULL) { // Do resolution 94 // First 2 bytes of name contains length (number of bytes). 95 int len = build_u2_from((address)name); 96 name += 2; 97 k = CompilerRuntime::resolve_klass_helper(thread, name, len, CHECK_NULL); 98 *klass_result = k; // Store result 99 } 100 JRT_BLOCK_END 101 assert(k != NULL, " Should be loaded!"); 102 return k; 103 JRT_END 104 105 106 Method* CompilerRuntime::resolve_method_helper(Klass* klass, const char* method_name, int method_name_len, 107 const char* signature_name, int signature_name_len) { 108 Method* m = NULL; 109 TempNewSymbol name_symbol = SymbolTable::probe(method_name, method_name_len); 110 TempNewSymbol signature_symbol = SymbolTable::probe(signature_name, signature_name_len); 111 if (name_symbol != NULL && signature_symbol != NULL) { 112 if (name_symbol == vmSymbols::object_initializer_name() || 113 name_symbol == vmSymbols::class_initializer_name()) { 114 // Never search superclasses for constructors 115 if (klass->is_instance_klass()) { 116 m = InstanceKlass::cast(klass)->find_method(name_symbol, signature_symbol); 117 } 118 } else { 119 m = klass->lookup_method(name_symbol, signature_symbol); 120 if (m == NULL && klass->is_instance_klass()) { 121 m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name_symbol, signature_symbol); 122 } 123 } 124 } 125 return m; 126 } 127 128 JRT_BLOCK_ENTRY(void, CompilerRuntime::resolve_dynamic_invoke(JavaThread *thread, oop* appendix_result)) 129 JRT_BLOCK 130 { 131 ResourceMark rm(THREAD); 132 vframeStream vfst(thread, true); // Do not skip and javaCalls 133 assert(!vfst.at_end(), "Java frame must exist"); 134 methodHandle caller(THREAD, vfst.method()); 135 InstanceKlass* holder = caller->method_holder(); 136 int bci = vfst.bci(); 137 Bytecode_invoke bytecode(caller, bci); 138 int index = bytecode.index(); 139 140 // Make sure it's resolved first 141 CallInfo callInfo; 142 constantPoolHandle cp(holder->constants()); 143 ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index, true)); 144 Bytecodes::Code invoke_code = bytecode.invoke_code(); 145 if (!cp_cache_entry->is_resolved(invoke_code)) { 146 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, invoke_code, CHECK); 147 if (bytecode.is_invokedynamic()) { 148 cp_cache_entry->set_dynamic_call(cp, callInfo); 149 } else { 150 cp_cache_entry->set_method_handle(cp, callInfo); 151 } 152 vmassert(cp_cache_entry->is_resolved(invoke_code), "sanity"); 153 } 154 155 Handle appendix(THREAD, cp_cache_entry->appendix_if_resolved(cp)); 156 Klass *appendix_klass = appendix.is_null() ? NULL : appendix->klass(); 157 158 methodHandle adapter_method(cp_cache_entry->f1_as_method()); 159 InstanceKlass *adapter_klass = adapter_method->method_holder(); 160 161 if (appendix_klass != NULL && appendix_klass->is_instance_klass()) { 162 vmassert(InstanceKlass::cast(appendix_klass)->is_initialized(), "sanity"); 163 } 164 if (!adapter_klass->is_initialized()) { 165 // Force initialization of adapter class 166 adapter_klass->initialize(CHECK); 167 // Double-check that it was really initialized, 168 // because we could be doing a recursive call 169 // from inside <clinit>. 170 } 171 172 int cpi = cp_cache_entry->constant_pool_index(); 173 if (!AOTLoader::reconcile_dynamic_invoke(holder, cpi, adapter_method(), 174 appendix_klass)) { 175 return; 176 } 177 178 *appendix_result = appendix(); 179 thread->set_vm_result(appendix()); 180 } 181 JRT_BLOCK_END 182 JRT_END 183 184 JRT_BLOCK_ENTRY(MethodCounters*, CompilerRuntime::resolve_method_by_symbol_and_load_counters(JavaThread *thread, MethodCounters** counters_result, Klass* klass, const char* data)) 185 MethodCounters* c = *counters_result; // Is it resolved already? 186 JRT_BLOCK 187 if (c == NULL) { // Do resolution 188 // Get method name and its length 189 int method_name_len = build_u2_from((address)data); 190 data += sizeof(u2); 191 const char* method_name = data; 192 data += method_name_len; 193 194 // Get signature and its length 195 int signature_name_len = build_u2_from((address)data); 196 data += sizeof(u2); 197 const char* signature_name = data; 198 199 assert(klass != NULL, "Klass parameter must not be null"); 200 Method* m = resolve_method_helper(klass, method_name, method_name_len, signature_name, signature_name_len); 201 assert(m != NULL, "Method must resolve successfully"); 202 203 // Create method counters immediately to avoid check at runtime. 204 c = m->get_method_counters(thread); 205 if (c == NULL) { 206 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Cannot allocate method counters"); 207 } 208 209 *counters_result = c; 210 } 211 JRT_BLOCK_END 212 return c; 213 JRT_END 214 215 // Resolve and initialize Klass 216 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::initialize_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 217 Klass* k = NULL; 218 JRT_BLOCK 219 k = klass_result[0]; // Is it initialized already? 220 if (k == NULL) { // Do initialized 221 k = klass_result[1]; // Is it resolved already? 222 if (k == NULL) { // Do resolution 223 // First 2 bytes of name contains length (number of bytes). 224 int len = build_u2_from((address)name); 225 const char *cname = name + 2; 226 k = CompilerRuntime::resolve_klass_helper(thread, cname, len, CHECK_NULL); 227 klass_result[1] = k; // Store resolved result 228 } 229 Klass* k0 = klass_result[0]; // Is it initialized already? 230 if (k0 == NULL && k != NULL && k->is_instance_klass()) { 231 // Force initialization of instance class 232 InstanceKlass::cast(k)->initialize(CHECK_NULL); 233 // Double-check that it was really initialized, 234 // because we could be doing a recursive call 235 // from inside <clinit>. 236 if (InstanceKlass::cast(k)->is_initialized()) { 237 klass_result[0] = k; // Store initialized result 238 } 239 } 240 } 241 JRT_BLOCK_END 242 assert(k != NULL, " Should be loaded!"); 243 return k; 244 JRT_END 245 246 247 JRT_BLOCK_ENTRY(void, CompilerRuntime::invocation_event(JavaThread *thread, MethodCounters* counters)) 248 if (!TieredCompilation) { 249 // Ignore the event if tiered is off 250 return; 251 } 252 JRT_BLOCK 253 methodHandle mh(THREAD, counters->method()); 254 RegisterMap map(thread, false); 255 256 // Compute the enclosing method 257 frame fr = thread->last_frame().sender(&map); 258 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 259 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 260 methodHandle emh(THREAD, cm->method()); 261 262 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 263 CompilationPolicy::policy()->event(emh, mh, InvocationEntryBci, InvocationEntryBci, CompLevel_aot, cm, thread); 264 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 265 JRT_BLOCK_END 266 JRT_END 267 268 JRT_BLOCK_ENTRY(void, CompilerRuntime::backedge_event(JavaThread *thread, MethodCounters* counters, int branch_bci, int target_bci)) 269 if (!TieredCompilation) { 270 // Ignore the event if tiered is off 271 return; 272 } 273 assert(branch_bci != InvocationEntryBci && target_bci != InvocationEntryBci, "Wrong bci"); 274 assert(target_bci <= branch_bci, "Expected a back edge"); 275 JRT_BLOCK 276 methodHandle mh(THREAD, counters->method()); 277 RegisterMap map(thread, false); 278 279 // Compute the enclosing method 280 frame fr = thread->last_frame().sender(&map); 281 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 282 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 283 methodHandle emh(THREAD, cm->method()); 284 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 285 nmethod* osr_nm = CompilationPolicy::policy()->event(emh, mh, branch_bci, target_bci, CompLevel_aot, cm, thread); 286 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 287 if (osr_nm != NULL) { 288 Deoptimization::deoptimize_frame(thread, fr.id()); 289 } 290 JRT_BLOCK_END 291 JRT_END