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