1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "jni.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/macros.h>
21
22 #include "art_field.h"
23 #include "art_method-inl.h"
24 #include "base/enums.h"
25 #include "common_throws.h"
26 #include "dex/dex_file-inl.h"
27 #include "instrumentation.h"
28 #include "jit/jit.h"
29 #include "jit/jit_code_cache.h"
30 #include "jit/profiling_info.h"
31 #include "jni/jni_internal.h"
32 #include "mirror/class-inl.h"
33 #include "mirror/class.h"
34 #include "nativehelper/ScopedUtfChars.h"
35 #include "oat.h"
36 #include "oat_file.h"
37 #include "oat_quick_method_header.h"
38 #include "profile/profile_compilation_info.h"
39 #include "runtime.h"
40 #include "scoped_thread_state_change-inl.h"
41 #include "scoped_thread_state_change.h"
42 #include "thread-current-inl.h"
43
44 namespace art {
45
46 // public static native boolean hasJit();
47
GetJitIfEnabled()48 static jit::Jit* GetJitIfEnabled() {
49 Runtime* runtime = Runtime::Current();
50 bool can_jit =
51 runtime != nullptr
52 && runtime->GetJit() != nullptr
53 && runtime->GetInstrumentation()->GetCurrentInstrumentationLevel() !=
54 instrumentation::Instrumentation::InstrumentationLevel::kInstrumentWithInterpreter;
55 return can_jit ? runtime->GetJit() : nullptr;
56 }
57
Java_Main_hasJit(JNIEnv *,jclass)58 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJit(JNIEnv*, jclass) {
59 return GetJitIfEnabled() != nullptr;
60 }
61
62 // public static native boolean hasOatFile();
63
Java_Main_hasOatFile(JNIEnv * env,jclass cls)64 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasOatFile(JNIEnv* env, jclass cls) {
65 ScopedObjectAccess soa(env);
66
67 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
68 const DexFile& dex_file = klass->GetDexFile();
69 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
70 return (oat_dex_file != nullptr) ? JNI_TRUE : JNI_FALSE;
71 }
72
Java_Main_getCompilerFilter(JNIEnv * env,jclass caller ATTRIBUTE_UNUSED,jclass cls)73 extern "C" JNIEXPORT jobject JNICALL Java_Main_getCompilerFilter(JNIEnv* env,
74 jclass caller ATTRIBUTE_UNUSED,
75 jclass cls) {
76 ScopedObjectAccess soa(env);
77
78 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
79 const DexFile& dex_file = klass->GetDexFile();
80 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
81 if (oat_dex_file == nullptr) {
82 return nullptr;
83 }
84
85 std::string filter =
86 CompilerFilter::NameOfFilter(oat_dex_file->GetOatFile()->GetCompilerFilter());
87 return soa.AddLocalReference<jobject>(
88 mirror::String::AllocFromModifiedUtf8(soa.Self(), filter.c_str()));
89 }
90
91 // public static native boolean runtimeIsSoftFail();
92
Java_Main_runtimeIsSoftFail(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)93 extern "C" JNIEXPORT jboolean JNICALL Java_Main_runtimeIsSoftFail(JNIEnv* env ATTRIBUTE_UNUSED,
94 jclass cls ATTRIBUTE_UNUSED) {
95 return Runtime::Current()->IsVerificationSoftFail() ? JNI_TRUE : JNI_FALSE;
96 }
97
98 // public static native boolean hasImage();
99
Java_Main_hasImage(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)100 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasImage(JNIEnv* env ATTRIBUTE_UNUSED,
101 jclass cls ATTRIBUTE_UNUSED) {
102 return Runtime::Current()->GetHeap()->HasBootImageSpace();
103 }
104
105 // public static native boolean isImageDex2OatEnabled();
106
Java_Main_isImageDex2OatEnabled(JNIEnv * env ATTRIBUTE_UNUSED,jclass cls ATTRIBUTE_UNUSED)107 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isImageDex2OatEnabled(JNIEnv* env ATTRIBUTE_UNUSED,
108 jclass cls ATTRIBUTE_UNUSED) {
109 return Runtime::Current()->IsImageDex2OatEnabled();
110 }
111
112 // public static native boolean compiledWithOptimizing();
113 // Did we use the optimizing compiler to compile this?
114
Java_Main_compiledWithOptimizing(JNIEnv * env,jclass cls)115 extern "C" JNIEXPORT jboolean JNICALL Java_Main_compiledWithOptimizing(JNIEnv* env, jclass cls) {
116 ScopedObjectAccess soa(env);
117
118 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
119 const DexFile& dex_file = klass->GetDexFile();
120 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
121 if (oat_dex_file == nullptr) {
122 // Could be JIT, which also uses optimizing, but conservatively say no.
123 return JNI_FALSE;
124 }
125 const OatFile* oat_file = oat_dex_file->GetOatFile();
126 CHECK(oat_file != nullptr);
127
128 const char* cmd_line = oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kDex2OatCmdLineKey);
129 if (cmd_line == nullptr) {
130 // Vdex-only execution, conservatively say no.
131 return JNI_FALSE;
132 }
133
134 // Check the backend.
135 constexpr const char* kCompilerBackend = "--compiler-backend=";
136 const char* backend = strstr(cmd_line, kCompilerBackend);
137 if (backend != nullptr) {
138 // If it's set, make sure it's optimizing.
139 backend += strlen(kCompilerBackend);
140 if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
141 return JNI_FALSE;
142 }
143 }
144
145 // Check the filter.
146 constexpr const char* kCompilerFilter = "--compiler-filter=";
147 const char* filter = strstr(cmd_line, kCompilerFilter);
148 if (filter != nullptr) {
149 filter += strlen(kCompilerFilter);
150 const char* end = strchr(filter, ' ');
151 std::string string_filter(filter, (end == nullptr) ? strlen(filter) : end - filter);
152 CompilerFilter::Filter compiler_filter;
153 bool success = CompilerFilter::ParseCompilerFilter(string_filter.c_str(), &compiler_filter);
154 CHECK(success);
155 return CompilerFilter::IsAotCompilationEnabled(compiler_filter) ? JNI_TRUE : JNI_FALSE;
156 }
157
158 // No filter passed, assume default has AOT.
159 return JNI_TRUE;
160 }
161
Java_Main_isAotCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)162 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
163 jclass,
164 jclass cls,
165 jstring method_name) {
166 Thread* self = Thread::Current();
167 ScopedObjectAccess soa(self);
168 ScopedUtfChars chars(env, method_name);
169 CHECK(chars.c_str() != nullptr);
170 ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
171 chars.c_str(), kRuntimePointerSize);
172 const void* oat_code = method->GetOatMethodQuickCode(kRuntimePointerSize);
173 if (oat_code == nullptr) {
174 return false;
175 }
176 const void* actual_code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
177 bool interpreter =
178 Runtime::Current()->GetClassLinker()->ShouldUseInterpreterEntrypoint(method, actual_code) ||
179 (actual_code == interpreter::GetNterpEntryPoint());
180 return !interpreter;
181 }
182
GetMethod(ScopedObjectAccess & soa,jclass cls,const ScopedUtfChars & chars)183 static ArtMethod* GetMethod(ScopedObjectAccess& soa, jclass cls, const ScopedUtfChars& chars)
184 REQUIRES_SHARED(Locks::mutator_lock_) {
185 CHECK(chars.c_str() != nullptr);
186 ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
187 chars.c_str(), kRuntimePointerSize);
188 if (method == nullptr) {
189 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
190 chars.c_str(), kRuntimePointerSize);
191 }
192 DCHECK(method != nullptr) << "Unable to find method called " << chars.c_str();
193 return method;
194 }
195
Java_Main_hasJitCompiledEntrypoint(JNIEnv * env,jclass,jclass cls,jstring method_name)196 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledEntrypoint(JNIEnv* env,
197 jclass,
198 jclass cls,
199 jstring method_name) {
200 jit::Jit* jit = GetJitIfEnabled();
201 if (jit == nullptr) {
202 return false;
203 }
204 Thread* self = Thread::Current();
205 ScopedObjectAccess soa(self);
206 ScopedUtfChars chars(env, method_name);
207 ArtMethod* method = GetMethod(soa, cls, chars);
208 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
209 return jit->GetCodeCache()->ContainsPc(
210 Runtime::Current()->GetInstrumentation()->GetCodeForInvoke(method));
211 }
212
Java_Main_hasJitCompiledCode(JNIEnv * env,jclass,jclass cls,jstring method_name)213 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledCode(JNIEnv* env,
214 jclass,
215 jclass cls,
216 jstring method_name) {
217 jit::Jit* jit = GetJitIfEnabled();
218 if (jit == nullptr) {
219 return false;
220 }
221 Thread* self = Thread::Current();
222 ScopedObjectAccess soa(self);
223 ScopedUtfChars chars(env, method_name);
224 ArtMethod* method = GetMethod(soa, cls, chars);
225 return jit->GetCodeCache()->ContainsMethod(method);
226 }
227
ForceJitCompiled(Thread * self,ArtMethod * method,CompilationKind kind)228 static void ForceJitCompiled(Thread* self,
229 ArtMethod* method,
230 CompilationKind kind) REQUIRES(!Locks::mutator_lock_) {
231 {
232 ScopedObjectAccess soa(self);
233 if (!Runtime::Current()->GetRuntimeCallbacks()->IsMethodSafeToJit(method)) {
234 std::string msg(method->PrettyMethod());
235 msg += ": is not safe to jit!";
236 ThrowIllegalStateException(msg.c_str());
237 return;
238 }
239 // We force visible initialization of the declaring class to make sure the method
240 // doesn't keep the resolution stub as entrypoint.
241 StackHandleScope<1> hs(self);
242 Handle<mirror::Class> h_klass(hs.NewHandle(method->GetDeclaringClass()));
243 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
244 if (!class_linker->EnsureInitialized(self, h_klass, true, true)) {
245 self->AssertPendingException();
246 return;
247 }
248 if (UNLIKELY(!h_klass->IsInitialized())) {
249 // Must be initializing in this thread.
250 CHECK_EQ(h_klass->GetStatus(), ClassStatus::kInitializing);
251 CHECK_EQ(h_klass->GetClinitThreadId(), self->GetTid());
252 std::string msg(method->PrettyMethod());
253 msg += ": is not safe to jit because the class is being initialized in this thread!";
254 ThrowIllegalStateException(msg.c_str());
255 return;
256 }
257 if (!h_klass->IsVisiblyInitialized()) {
258 ScopedThreadSuspension sts(self, ThreadState::kNative);
259 class_linker->MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ true);
260 }
261 }
262 jit::Jit* jit = GetJitIfEnabled();
263 jit::JitCodeCache* code_cache = jit->GetCodeCache();
264 // Update the code cache to make sure the JIT code does not get deleted.
265 // Note: this will apply to all JIT compilations.
266 code_cache->SetGarbageCollectCode(false);
267 do {
268 // Sleep to yield to the compiler thread.
269 usleep(1000);
270 ScopedObjectAccess soa(self);
271 // Will either ensure it's compiled or do the compilation itself. We do
272 // this before checking if we will execute JIT code in case the request
273 // is for an 'optimized' compilation.
274 jit->CompileMethod(method, self, kind, /*prejit=*/ false);
275 } while (!code_cache->ContainsPc(method->GetEntryPointFromQuickCompiledCode()));
276 }
277
Java_Main_ensureMethodJitCompiled(JNIEnv *,jclass,jobject meth)278 extern "C" JNIEXPORT void JNICALL Java_Main_ensureMethodJitCompiled(JNIEnv*, jclass, jobject meth) {
279 jit::Jit* jit = GetJitIfEnabled();
280 if (jit == nullptr) {
281 return;
282 }
283
284 Thread* self = Thread::Current();
285 ArtMethod* method;
286 {
287 ScopedObjectAccess soa(self);
288 method = ArtMethod::FromReflectedMethod(soa, meth);
289 }
290 ForceJitCompiled(self, method, CompilationKind::kOptimized);
291 }
292
Java_Main_ensureJitCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)293 extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
294 jclass,
295 jclass cls,
296 jstring method_name) {
297 jit::Jit* jit = GetJitIfEnabled();
298 if (jit == nullptr) {
299 return;
300 }
301
302 Thread* self = Thread::Current();
303 ArtMethod* method = nullptr;
304 {
305 ScopedObjectAccess soa(self);
306
307 ScopedUtfChars chars(env, method_name);
308 method = GetMethod(soa, cls, chars);
309 }
310 ForceJitCompiled(self, method, CompilationKind::kOptimized);
311 }
312
Java_Main_ensureJitBaselineCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)313 extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitBaselineCompiled(JNIEnv* env,
314 jclass,
315 jclass cls,
316 jstring method_name) {
317 jit::Jit* jit = GetJitIfEnabled();
318 if (jit == nullptr) {
319 return;
320 }
321
322 Thread* self = Thread::Current();
323 ArtMethod* method = nullptr;
324 {
325 ScopedObjectAccess soa(self);
326
327 ScopedUtfChars chars(env, method_name);
328 method = GetMethod(soa, cls, chars);
329 }
330 ForceJitCompiled(self, method, CompilationKind::kBaseline);
331 }
332
Java_Main_hasSingleImplementation(JNIEnv * env,jclass,jclass cls,jstring method_name)333 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
334 jclass,
335 jclass cls,
336 jstring method_name) {
337 ArtMethod* method = nullptr;
338 ScopedObjectAccess soa(Thread::Current());
339 ScopedUtfChars chars(env, method_name);
340 CHECK(chars.c_str() != nullptr);
341 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
342 chars.c_str(), kRuntimePointerSize);
343 return method->HasSingleImplementation();
344 }
345
Java_Main_getHotnessCounter(JNIEnv * env,jclass,jclass cls,jstring method_name)346 extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
347 jclass,
348 jclass cls,
349 jstring method_name) {
350 ScopedObjectAccess soa(Thread::Current());
351 ScopedUtfChars chars(env, method_name);
352 CHECK(chars.c_str() != nullptr);
353 ArtMethod* method =
354 soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(chars.c_str(),
355 kRuntimePointerSize);
356 if (method != nullptr) {
357 return method->GetCounter();
358 }
359
360 method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(chars.c_str(),
361 kRuntimePointerSize);
362 if (method != nullptr) {
363 return method->GetCounter();
364 }
365
366 return std::numeric_limits<int32_t>::min();
367 }
368
Java_Main_numberOfDeoptimizations(JNIEnv *,jclass)369 extern "C" JNIEXPORT int JNICALL Java_Main_numberOfDeoptimizations(JNIEnv*, jclass) {
370 return Runtime::Current()->GetNumberOfDeoptimizations();
371 }
372
Java_Main_fetchProfiles(JNIEnv *,jclass)373 extern "C" JNIEXPORT void JNICALL Java_Main_fetchProfiles(JNIEnv*, jclass) {
374 jit::Jit* jit = GetJitIfEnabled();
375 if (jit == nullptr) {
376 return;
377 }
378 jit::JitCodeCache* code_cache = jit->GetCodeCache();
379 std::vector<ProfileMethodInfo> unused_vector;
380 std::set<std::string> unused_locations;
381 unused_locations.insert("fake_location");
382 ScopedObjectAccess soa(Thread::Current());
383 code_cache->GetProfiledMethods(unused_locations, unused_vector);
384 }
385
Java_Main_waitForCompilation(JNIEnv *,jclass)386 extern "C" JNIEXPORT void JNICALL Java_Main_waitForCompilation(JNIEnv*, jclass) {
387 jit::Jit* jit = Runtime::Current()->GetJit();
388 if (jit != nullptr) {
389 jit->WaitForCompilationToFinish(Thread::Current());
390 }
391 }
392
Java_Main_stopJit(JNIEnv *,jclass)393 extern "C" JNIEXPORT void JNICALL Java_Main_stopJit(JNIEnv*, jclass) {
394 jit::Jit* jit = Runtime::Current()->GetJit();
395 if (jit != nullptr) {
396 jit->Stop();
397 }
398 }
399
Java_Main_startJit(JNIEnv *,jclass)400 extern "C" JNIEXPORT void JNICALL Java_Main_startJit(JNIEnv*, jclass) {
401 jit::Jit* jit = Runtime::Current()->GetJit();
402 if (jit != nullptr) {
403 jit->Start();
404 }
405 }
406
Java_Main_getJitThreshold(JNIEnv *,jclass)407 extern "C" JNIEXPORT jint JNICALL Java_Main_getJitThreshold(JNIEnv*, jclass) {
408 jit::Jit* jit = Runtime::Current()->GetJit();
409 return (jit != nullptr) ? jit->HotMethodThreshold() : 0;
410 }
411
Java_Main_deoptimizeBootImage(JNIEnv *,jclass)412 extern "C" JNIEXPORT void JNICALL Java_Main_deoptimizeBootImage(JNIEnv*, jclass) {
413 ScopedSuspendAll ssa(__FUNCTION__);
414 Runtime::Current()->DeoptimizeBootImage();
415 }
416
Java_Main_isDebuggable(JNIEnv *,jclass)417 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDebuggable(JNIEnv*, jclass) {
418 return Runtime::Current()->IsJavaDebuggable() ? JNI_TRUE : JNI_FALSE;
419 }
420
Java_Main_setTargetSdkVersion(JNIEnv *,jclass,jint version)421 extern "C" JNIEXPORT void JNICALL Java_Main_setTargetSdkVersion(JNIEnv*, jclass, jint version) {
422 Runtime::Current()->SetTargetSdkVersion(static_cast<uint32_t>(version));
423 }
424
Java_Main_genericFieldOffset(JNIEnv * env,jclass,jobject fld)425 extern "C" JNIEXPORT jlong JNICALL Java_Main_genericFieldOffset(JNIEnv* env, jclass, jobject fld) {
426 jfieldID fid = env->FromReflectedField(fld);
427 ScopedObjectAccess soa(env);
428 ArtField* af = jni::DecodeArtField(fid);
429 return af->GetOffset().Int32Value();
430 }
431
Java_Main_isObsoleteObject(JNIEnv * env,jclass,jclass c)432 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isObsoleteObject(JNIEnv* env, jclass, jclass c) {
433 ScopedObjectAccess soa(env);
434 return soa.Decode<mirror::Class>(c)->IsObsoleteObject();
435 }
436
437 } // namespace art
438