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/deoptimization.hpp" 33 #include "runtime/interfaceSupport.inline.hpp" 34 #include "runtime/vframe.hpp" 35 #include "aot/aotLoader.hpp" 36 37 // Resolve and allocate String 38 JRT_BLOCK_ENTRY(void, CompilerRuntime::resolve_string_by_symbol(JavaThread *thread, void* string_result, const char* name)) 39 JRT_BLOCK 40 oop str = *(oop*)string_result; // Is it resolved already? 41 if (str == NULL) { // Do resolution 42 // First 2 bytes of name contains length (number of bytes). 43 int len = build_u2_from((address)name); 44 name += 2; 45 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK); 46 str = StringTable::intern(sym, CHECK); 47 assert(java_lang_String::is_instance(str), "must be string"); 48 *(oop*)string_result = str; // Store result 49 } 50 assert(str != NULL, "Should be allocated!"); 51 thread->set_vm_result(str); 52 JRT_BLOCK_END 53 JRT_END 54 55 56 57 Klass* CompilerRuntime::resolve_klass_helper(JavaThread *thread, const char* name, int len, TRAPS) { 58 ResourceMark rm(THREAD); 59 // last java frame on stack (which includes native call frames) 60 RegisterMap cbl_map(thread, false); 61 // Skip stub 62 frame caller_frame = thread->last_frame().sender(&cbl_map); 63 CodeBlob* caller_cb = caller_frame.cb(); 64 guarantee(caller_cb != NULL && caller_cb->is_compiled(), "must be called from compiled method"); 65 CompiledMethod* caller_nm = caller_cb->as_compiled_method_or_null(); 66 methodHandle caller(THREAD, caller_nm->method()); 67 68 // Use class loader of aot method. 69 Handle loader(THREAD, caller->method_holder()->class_loader()); 70 Handle protection_domain(THREAD, caller->method_holder()->protection_domain()); 71 72 // Ignore wrapping L and ; 73 if (name[0] == 'L') { 74 assert(len > 2, "small name %s", name); 75 name++; 76 len -= 2; 77 } 78 TempNewSymbol sym = SymbolTable::new_symbol(name, len, CHECK_NULL); 79 if (sym == NULL) { 80 return NULL; 81 } 82 Klass* k = SystemDictionary::resolve_or_fail(sym, loader, protection_domain, true, CHECK_NULL); 83 84 return k; 85 } 86 87 // Resolve Klass 88 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::resolve_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 89 Klass* k = NULL; 90 JRT_BLOCK 91 k = *klass_result; // Is it resolved already? 92 if (k == NULL) { // Do resolution 93 // First 2 bytes of name contains length (number of bytes). 94 int len = build_u2_from((address)name); 95 name += 2; 96 k = CompilerRuntime::resolve_klass_helper(thread, name, len, CHECK_NULL); 97 *klass_result = k; // Store result 98 } 99 JRT_BLOCK_END 100 assert(k != NULL, " Should be loaded!"); 101 return k; 102 JRT_END 103 104 105 Method* CompilerRuntime::resolve_method_helper(Klass* klass, const char* method_name, int method_name_len, 106 const char* signature_name, int signature_name_len) { 107 Method* m = NULL; 108 TempNewSymbol name_symbol = SymbolTable::probe(method_name, method_name_len); 109 TempNewSymbol signature_symbol = SymbolTable::probe(signature_name, signature_name_len); 110 if (name_symbol != NULL && signature_symbol != NULL) { 111 if (name_symbol == vmSymbols::object_initializer_name() || 112 name_symbol == vmSymbols::class_initializer_name()) { 113 // Never search superclasses for constructors 114 if (klass->is_instance_klass()) { 115 m = InstanceKlass::cast(klass)->find_method(name_symbol, signature_symbol); 116 } 117 } else { 118 m = klass->lookup_method(name_symbol, signature_symbol); 119 if (m == NULL && klass->is_instance_klass()) { 120 m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name_symbol, signature_symbol); 121 } 122 } 123 } 124 return m; 125 } 126 127 JRT_BLOCK_ENTRY(void, CompilerRuntime::resolve_dynamic_invoke(JavaThread *thread, oop* appendix_result)) 128 JRT_BLOCK 129 { 130 ResourceMark rm(THREAD); 131 vframeStream vfst(thread, true); // Do not skip and javaCalls 132 assert(!vfst.at_end(), "Java frame must exist"); 133 methodHandle caller(THREAD, vfst.method()); 134 InstanceKlass* holder = caller->method_holder(); 135 int bci = vfst.bci(); 136 Bytecode_invoke bytecode(caller, bci); 137 int index = bytecode.index(); 138 139 // Make sure it's resolved first 140 CallInfo callInfo; 141 constantPoolHandle cp(holder->constants()); 142 ConstantPoolCacheEntry* cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index, true)); 143 Bytecodes::Code invoke_code = bytecode.invoke_code(); 144 if (!cp_cache_entry->is_resolved(invoke_code)) { 145 LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, invoke_code, CHECK); 146 if (bytecode.is_invokedynamic()) { 147 cp_cache_entry->set_dynamic_call(cp, callInfo); 148 } else { 149 cp_cache_entry->set_method_handle(cp, callInfo); 150 } 151 vmassert(cp_cache_entry->is_resolved(invoke_code), "sanity"); 152 } 153 154 Handle appendix(THREAD, cp_cache_entry->appendix_if_resolved(cp)); 155 Klass *appendix_klass = appendix.is_null() ? NULL : appendix->klass(); 156 157 methodHandle adapter_method(cp_cache_entry->f1_as_method()); 158 InstanceKlass *adapter_klass = adapter_method->method_holder(); 159 160 if (appendix_klass != NULL && appendix_klass->is_instance_klass()) { 161 vmassert(InstanceKlass::cast(appendix_klass)->is_initialized(), "sanity"); 162 } 163 if (!adapter_klass->is_initialized()) { 164 // Force initialization of adapter class 165 adapter_klass->initialize(CHECK); 166 // Double-check that it was really initialized, 167 // because we could be doing a recursive call 168 // from inside <clinit>. 169 } 170 171 int cpi = cp_cache_entry->constant_pool_index(); 172 if (!AOTLoader::reconcile_dynamic_invoke(holder, cpi, adapter_method(), 173 appendix_klass)) { 174 return; 175 } 176 177 *appendix_result = appendix(); 178 thread->set_vm_result(appendix()); 179 } 180 JRT_BLOCK_END 181 JRT_END 182 183 JRT_BLOCK_ENTRY(MethodCounters*, CompilerRuntime::resolve_method_by_symbol_and_load_counters(JavaThread *thread, MethodCounters** counters_result, Klass* klass, const char* data)) 184 MethodCounters* c = *counters_result; // Is it resolved already? 185 JRT_BLOCK 186 if (c == NULL) { // Do resolution 187 // Get method name and its length 188 int method_name_len = build_u2_from((address)data); 189 data += sizeof(u2); 190 const char* method_name = data; 191 data += method_name_len; 192 193 // Get signature and its length 194 int signature_name_len = build_u2_from((address)data); 195 data += sizeof(u2); 196 const char* signature_name = data; 197 198 assert(klass != NULL, "Klass parameter must not be null"); 199 Method* m = resolve_method_helper(klass, method_name, method_name_len, signature_name, signature_name_len); 200 assert(m != NULL, "Method must resolve successfully"); 201 202 // Create method counters immediately to avoid check at runtime. 203 c = m->get_method_counters(thread); 204 if (c == NULL) { 205 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Cannot allocate method counters"); 206 } 207 208 *counters_result = c; 209 } 210 JRT_BLOCK_END 211 return c; 212 JRT_END 213 214 // Resolve and initialize Klass 215 JRT_BLOCK_ENTRY(Klass*, CompilerRuntime::initialize_klass_by_symbol(JavaThread *thread, Klass** klass_result, const char* name)) 216 Klass* k = NULL; 217 JRT_BLOCK 218 k = klass_result[0]; // Is it initialized already? 219 if (k == NULL) { // Do initialized 220 k = klass_result[1]; // Is it resolved already? 221 if (k == NULL) { // Do resolution 222 // First 2 bytes of name contains length (number of bytes). 223 int len = build_u2_from((address)name); 224 const char *cname = name + 2; 225 k = CompilerRuntime::resolve_klass_helper(thread, cname, len, CHECK_NULL); 226 klass_result[1] = k; // Store resolved result 227 } 228 Klass* k0 = klass_result[0]; // Is it initialized already? 229 if (k0 == NULL && k != NULL && k->is_instance_klass()) { 230 // Force initialization of instance class 231 InstanceKlass::cast(k)->initialize(CHECK_NULL); 232 // Double-check that it was really initialized, 233 // because we could be doing a recursive call 234 // from inside <clinit>. 235 if (InstanceKlass::cast(k)->is_initialized()) { 236 klass_result[0] = k; // Store initialized result 237 } 238 } 239 } 240 JRT_BLOCK_END 241 assert(k != NULL, " Should be loaded!"); 242 return k; 243 JRT_END 244 245 246 JRT_BLOCK_ENTRY(void, CompilerRuntime::invocation_event(JavaThread *thread, MethodCounters* counters)) 247 if (!TieredCompilation) { 248 // Ignore the event if tiered is off 249 return; 250 } 251 JRT_BLOCK 252 methodHandle mh(THREAD, counters->method()); 253 RegisterMap map(thread, false); 254 255 // Compute the enclosing method 256 frame fr = thread->last_frame().sender(&map); 257 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 258 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 259 methodHandle emh(THREAD, cm->method()); 260 261 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 262 CompilationPolicy::policy()->event(emh, mh, InvocationEntryBci, InvocationEntryBci, CompLevel_aot, cm, thread); 263 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 264 JRT_BLOCK_END 265 JRT_END 266 267 JRT_BLOCK_ENTRY(void, CompilerRuntime::backedge_event(JavaThread *thread, MethodCounters* counters, int branch_bci, int target_bci)) 268 if (!TieredCompilation) { 269 // Ignore the event if tiered is off 270 return; 271 } 272 assert(branch_bci != InvocationEntryBci && target_bci != InvocationEntryBci, "Wrong bci"); 273 assert(target_bci <= branch_bci, "Expected a back edge"); 274 JRT_BLOCK 275 methodHandle mh(THREAD, counters->method()); 276 RegisterMap map(thread, false); 277 278 // Compute the enclosing method 279 frame fr = thread->last_frame().sender(&map); 280 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null(); 281 assert(cm != NULL && cm->is_compiled(), "Sanity check"); 282 methodHandle emh(THREAD, cm->method()); 283 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 284 nmethod* osr_nm = CompilationPolicy::policy()->event(emh, mh, branch_bci, target_bci, CompLevel_aot, cm, thread); 285 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 286 if (osr_nm != NULL) { 287 Deoptimization::deoptimize_frame(thread, fr.id()); 288 } 289 JRT_BLOCK_END 290 JRT_END