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