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