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   CHECK(cmd_line != nullptr);  // Huh? This should not happen.
130 
131   // Check the backend.
132   constexpr const char* kCompilerBackend = "--compiler-backend=";
133   const char* backend = strstr(cmd_line, kCompilerBackend);
134   if (backend != nullptr) {
135     // If it's set, make sure it's optimizing.
136     backend += strlen(kCompilerBackend);
137     if (strncmp(backend, "Optimizing", strlen("Optimizing")) != 0) {
138       return JNI_FALSE;
139     }
140   }
141 
142   // Check the filter.
143   constexpr const char* kCompilerFilter = "--compiler-filter=";
144   const char* filter = strstr(cmd_line, kCompilerFilter);
145   if (filter != nullptr) {
146     // If it's set, make sure it's not interpret-only|verify-none|verify-at-runtime.
147     // Note: The space filter might have an impact on the test, but ignore that for now.
148     filter += strlen(kCompilerFilter);
149     constexpr const char* kInterpretOnly = "interpret-only";
150     constexpr const char* kVerifyNone = "verify-none";
151     constexpr const char* kVerifyAtRuntime = "verify-at-runtime";
152     constexpr const char* kQuicken = "quicken";
153     constexpr const char* kExtract = "extract";
154     if (strncmp(filter, kInterpretOnly, strlen(kInterpretOnly)) == 0 ||
155         strncmp(filter, kVerifyNone, strlen(kVerifyNone)) == 0 ||
156         strncmp(filter, kVerifyAtRuntime, strlen(kVerifyAtRuntime)) == 0 ||
157         strncmp(filter, kExtract, strlen(kExtract)) == 0 ||
158         strncmp(filter, kQuicken, strlen(kQuicken)) == 0) {
159       return JNI_FALSE;
160     }
161   }
162 
163   return JNI_TRUE;
164 }
165 
Java_Main_isAotCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)166 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isAotCompiled(JNIEnv* env,
167                                                               jclass,
168                                                               jclass cls,
169                                                               jstring method_name) {
170   Thread* self = Thread::Current();
171   ScopedObjectAccess soa(self);
172   ScopedUtfChars chars(env, method_name);
173   CHECK(chars.c_str() != nullptr);
174   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
175         chars.c_str(), kRuntimePointerSize);
176   const void* oat_code = method->GetOatMethodQuickCode(kRuntimePointerSize);
177   if (oat_code == nullptr) {
178     return false;
179   }
180   const void* actual_code = method->GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
181   bool interpreter =
182       Runtime::Current()->GetClassLinker()->ShouldUseInterpreterEntrypoint(method, actual_code) ||
183       (actual_code == interpreter::GetNterpEntryPoint());
184   return !interpreter;
185 }
186 
GetMethod(ScopedObjectAccess & soa,jclass cls,const ScopedUtfChars & chars)187 static ArtMethod* GetMethod(ScopedObjectAccess& soa, jclass cls, const ScopedUtfChars& chars)
188     REQUIRES_SHARED(Locks::mutator_lock_) {
189   CHECK(chars.c_str() != nullptr);
190   ArtMethod* method = soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(
191         chars.c_str(), kRuntimePointerSize);
192   if (method == nullptr) {
193     method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
194         chars.c_str(), kRuntimePointerSize);
195   }
196   DCHECK(method != nullptr) << "Unable to find method called " << chars.c_str();
197   return method;
198 }
199 
Java_Main_hasJitCompiledEntrypoint(JNIEnv * env,jclass,jclass cls,jstring method_name)200 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledEntrypoint(JNIEnv* env,
201                                                                          jclass,
202                                                                          jclass cls,
203                                                                          jstring method_name) {
204   jit::Jit* jit = GetJitIfEnabled();
205   if (jit == nullptr) {
206     return false;
207   }
208   Thread* self = Thread::Current();
209   ScopedObjectAccess soa(self);
210   ScopedUtfChars chars(env, method_name);
211   ArtMethod* method = GetMethod(soa, cls, chars);
212   ScopedAssertNoThreadSuspension sants(__FUNCTION__);
213   return jit->GetCodeCache()->ContainsPc(
214       Runtime::Current()->GetInstrumentation()->GetCodeForInvoke(method));
215 }
216 
Java_Main_hasJitCompiledCode(JNIEnv * env,jclass,jclass cls,jstring method_name)217 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasJitCompiledCode(JNIEnv* env,
218                                                                    jclass,
219                                                                    jclass cls,
220                                                                    jstring method_name) {
221   jit::Jit* jit = GetJitIfEnabled();
222   if (jit == nullptr) {
223     return false;
224   }
225   Thread* self = Thread::Current();
226   ScopedObjectAccess soa(self);
227   ScopedUtfChars chars(env, method_name);
228   ArtMethod* method = GetMethod(soa, cls, chars);
229   return jit->GetCodeCache()->ContainsMethod(method);
230 }
231 
ForceJitCompiled(Thread * self,ArtMethod * method)232 static void ForceJitCompiled(Thread* self, ArtMethod* method) REQUIRES(!Locks::mutator_lock_) {
233   bool native = false;
234   {
235     ScopedObjectAccess soa(self);
236     if (method->IsNative()) {
237       native = true;
238     } else if (!Runtime::Current()->GetRuntimeCallbacks()->IsMethodSafeToJit(method)) {
239       std::string msg(method->PrettyMethod());
240       msg += ": is not safe to jit!";
241       ThrowIllegalStateException(msg.c_str());
242       return;
243     }
244     // We force visible initialization of the declaring class to make sure the method
245     // doesn't keep the resolution stub as entrypoint.
246     StackHandleScope<1> hs(self);
247     Handle<mirror::Class> h_klass(hs.NewHandle(method->GetDeclaringClass()));
248     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
249     if (!class_linker->EnsureInitialized(self, h_klass, true, true)) {
250       self->AssertPendingException();
251       return;
252     }
253     if (UNLIKELY(!h_klass->IsInitialized())) {
254       // Must be initializing in this thread.
255       CHECK_EQ(h_klass->GetStatus(), ClassStatus::kInitializing);
256       CHECK_EQ(h_klass->GetClinitThreadId(), self->GetTid());
257       std::string msg(method->PrettyMethod());
258       msg += ": is not safe to jit because the class is being initialized in this thread!";
259       ThrowIllegalStateException(msg.c_str());
260       return;
261     }
262     if (!h_klass->IsVisiblyInitialized()) {
263       ScopedThreadSuspension sts(self, ThreadState::kNative);
264       class_linker->MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ true);
265     }
266   }
267   jit::Jit* jit = GetJitIfEnabled();
268   jit::JitCodeCache* code_cache = jit->GetCodeCache();
269   // Update the code cache to make sure the JIT code does not get deleted.
270   // Note: this will apply to all JIT compilations.
271   code_cache->SetGarbageCollectCode(false);
272   while (true) {
273     if (native && code_cache->ContainsMethod(method)) {
274       break;
275     } else {
276       // Sleep to yield to the compiler thread.
277       usleep(1000);
278       ScopedObjectAccess soa(self);
279       if (!native && jit->GetCodeCache()->CanAllocateProfilingInfo()) {
280         // Make sure there is a profiling info, required by the compiler.
281         ProfilingInfo::Create(self, method, /* retry_allocation */ true);
282       }
283       // Will either ensure it's compiled or do the compilation itself. We do
284       // this before checking if we will execute JIT code to make sure the
285       // method is compiled 'optimized' and not baseline (tests expect optimized
286       // compilation).
287       jit->CompileMethod(method, self, CompilationKind::kOptimized, /*prejit=*/ false);
288       if (code_cache->WillExecuteJitCode(method)) {
289         break;
290       }
291     }
292   }
293 }
294 
Java_Main_ensureMethodJitCompiled(JNIEnv *,jclass,jobject meth)295 extern "C" JNIEXPORT void JNICALL Java_Main_ensureMethodJitCompiled(JNIEnv*, jclass, jobject meth) {
296   jit::Jit* jit = GetJitIfEnabled();
297   if (jit == nullptr) {
298     return;
299   }
300 
301   Thread* self = Thread::Current();
302   ArtMethod* method;
303   {
304     ScopedObjectAccess soa(self);
305     method = ArtMethod::FromReflectedMethod(soa, meth);
306   }
307   ForceJitCompiled(self, method);
308 }
309 
Java_Main_ensureJitCompiled(JNIEnv * env,jclass,jclass cls,jstring method_name)310 extern "C" JNIEXPORT void JNICALL Java_Main_ensureJitCompiled(JNIEnv* env,
311                                                              jclass,
312                                                              jclass cls,
313                                                              jstring method_name) {
314   jit::Jit* jit = GetJitIfEnabled();
315   if (jit == nullptr) {
316     return;
317   }
318 
319   Thread* self = Thread::Current();
320   ArtMethod* method = nullptr;
321   {
322     ScopedObjectAccess soa(self);
323 
324     ScopedUtfChars chars(env, method_name);
325     method = GetMethod(soa, cls, chars);
326   }
327   ForceJitCompiled(self, method);
328 }
329 
Java_Main_hasSingleImplementation(JNIEnv * env,jclass,jclass cls,jstring method_name)330 extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasSingleImplementation(JNIEnv* env,
331                                                                         jclass,
332                                                                         jclass cls,
333                                                                         jstring method_name) {
334   ArtMethod* method = nullptr;
335   ScopedObjectAccess soa(Thread::Current());
336   ScopedUtfChars chars(env, method_name);
337   CHECK(chars.c_str() != nullptr);
338   method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(
339       chars.c_str(), kRuntimePointerSize);
340   return method->HasSingleImplementation();
341 }
342 
Java_Main_getHotnessCounter(JNIEnv * env,jclass,jclass cls,jstring method_name)343 extern "C" JNIEXPORT int JNICALL Java_Main_getHotnessCounter(JNIEnv* env,
344                                                              jclass,
345                                                              jclass cls,
346                                                              jstring method_name) {
347   ScopedObjectAccess soa(Thread::Current());
348   ScopedUtfChars chars(env, method_name);
349   CHECK(chars.c_str() != nullptr);
350   ArtMethod* method =
351       soa.Decode<mirror::Class>(cls)->FindDeclaredDirectMethodByName(chars.c_str(),
352                                                                      kRuntimePointerSize);
353   if (method != nullptr) {
354     return method->GetCounter();
355   }
356 
357   method = soa.Decode<mirror::Class>(cls)->FindDeclaredVirtualMethodByName(chars.c_str(),
358                                                                            kRuntimePointerSize);
359   if (method != nullptr) {
360     return method->GetCounter();
361   }
362 
363   return std::numeric_limits<int32_t>::min();
364 }
365 
Java_Main_numberOfDeoptimizations(JNIEnv *,jclass)366 extern "C" JNIEXPORT int JNICALL Java_Main_numberOfDeoptimizations(JNIEnv*, jclass) {
367   return Runtime::Current()->GetNumberOfDeoptimizations();
368 }
369 
Java_Main_fetchProfiles(JNIEnv *,jclass)370 extern "C" JNIEXPORT void JNICALL Java_Main_fetchProfiles(JNIEnv*, jclass) {
371   jit::Jit* jit = GetJitIfEnabled();
372   if (jit == nullptr) {
373     return;
374   }
375   jit::JitCodeCache* code_cache = jit->GetCodeCache();
376   std::vector<ProfileMethodInfo> unused_vector;
377   std::set<std::string> unused_locations;
378   unused_locations.insert("fake_location");
379   ScopedObjectAccess soa(Thread::Current());
380   code_cache->GetProfiledMethods(unused_locations, unused_vector);
381 }
382 
Java_Main_waitForCompilation(JNIEnv *,jclass)383 extern "C" JNIEXPORT void JNICALL Java_Main_waitForCompilation(JNIEnv*, jclass) {
384   jit::Jit* jit = Runtime::Current()->GetJit();
385   if (jit != nullptr) {
386     jit->WaitForCompilationToFinish(Thread::Current());
387   }
388 }
389 
Java_Main_stopJit(JNIEnv *,jclass)390 extern "C" JNIEXPORT void JNICALL Java_Main_stopJit(JNIEnv*, jclass) {
391   jit::Jit* jit = Runtime::Current()->GetJit();
392   if (jit != nullptr) {
393     jit->Stop();
394   }
395 }
396 
Java_Main_startJit(JNIEnv *,jclass)397 extern "C" JNIEXPORT void JNICALL Java_Main_startJit(JNIEnv*, jclass) {
398   jit::Jit* jit = Runtime::Current()->GetJit();
399   if (jit != nullptr) {
400     jit->Start();
401   }
402 }
403 
Java_Main_getJitThreshold(JNIEnv *,jclass)404 extern "C" JNIEXPORT jint JNICALL Java_Main_getJitThreshold(JNIEnv*, jclass) {
405   jit::Jit* jit = Runtime::Current()->GetJit();
406   return (jit != nullptr) ? jit->HotMethodThreshold() : 0;
407 }
408 
Java_Main_deoptimizeBootImage(JNIEnv *,jclass)409 extern "C" JNIEXPORT void JNICALL Java_Main_deoptimizeBootImage(JNIEnv*, jclass) {
410   ScopedSuspendAll ssa(__FUNCTION__);
411   Runtime::Current()->DeoptimizeBootImage();
412 }
413 
Java_Main_isDebuggable(JNIEnv *,jclass)414 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isDebuggable(JNIEnv*, jclass) {
415   return Runtime::Current()->IsJavaDebuggable() ? JNI_TRUE : JNI_FALSE;
416 }
417 
Java_Main_setTargetSdkVersion(JNIEnv *,jclass,jint version)418 extern "C" JNIEXPORT void JNICALL Java_Main_setTargetSdkVersion(JNIEnv*, jclass, jint version) {
419   Runtime::Current()->SetTargetSdkVersion(static_cast<uint32_t>(version));
420 }
421 
Java_Main_genericFieldOffset(JNIEnv * env,jclass,jobject fld)422 extern "C" JNIEXPORT jlong JNICALL Java_Main_genericFieldOffset(JNIEnv* env, jclass, jobject fld) {
423   jfieldID fid = env->FromReflectedField(fld);
424   ScopedObjectAccess soa(env);
425   ArtField* af = jni::DecodeArtField(fid);
426   return af->GetOffset().Int32Value();
427 }
428 
Java_Main_isObsoleteObject(JNIEnv * env,jclass,jclass c)429 extern "C" JNIEXPORT jboolean JNICALL Java_Main_isObsoleteObject(JNIEnv* env, jclass, jclass c) {
430   ScopedObjectAccess soa(env);
431   return soa.Decode<mirror::Class>(c)->IsObsoleteObject();
432 }
433 
434 }  // namespace art
435