xref: /aosp_15_r20/art/runtime/native/dalvik_system_VMRuntime.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "dalvik_system_VMRuntime.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
20*795d594fSAndroid Build Coastguard Worker #include <sys/resource.h>
21*795d594fSAndroid Build Coastguard Worker #include <sys/time.h>
22*795d594fSAndroid Build Coastguard Worker extern "C" void android_set_application_target_sdk_version(uint32_t version);
23*795d594fSAndroid Build Coastguard Worker #endif
24*795d594fSAndroid Build Coastguard Worker #include <inttypes.h>
25*795d594fSAndroid Build Coastguard Worker #include <limits>
26*795d594fSAndroid Build Coastguard Worker #include <limits.h>
27*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_utf_chars.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
30*795d594fSAndroid Build Coastguard Worker #include <android-base/strings.h>
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include "android-base/properties.h"
33*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
34*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
36*795d594fSAndroid Build Coastguard Worker #include "base/sdk_version.h"
37*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "class_loader_context.h"
39*795d594fSAndroid Build Coastguard Worker #include "common_throws.h"
40*795d594fSAndroid Build Coastguard Worker #include "debugger.h"
41*795d594fSAndroid Build Coastguard Worker #include "dex/class_accessor-inl.h"
42*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_types.h"
44*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
45*795d594fSAndroid Build Coastguard Worker #include "gc/allocator/art-dlmalloc.h"
46*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
47*795d594fSAndroid Build Coastguard Worker #include "gc/space/dlmalloc_space.h"
48*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
49*795d594fSAndroid Build Coastguard Worker #include "gc/task_processor.h"
50*795d594fSAndroid Build Coastguard Worker #include "intern_table.h"
51*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
52*795d594fSAndroid Build Coastguard Worker #include "jni/java_vm_ext.h"
53*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
54*795d594fSAndroid Build Coastguard Worker #include "mirror/array-alloc-inl.h"
55*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
56*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache-inl.h"
57*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
58*795d594fSAndroid Build Coastguard Worker #include "native_util.h"
59*795d594fSAndroid Build Coastguard Worker #include "nativehelper/jni_macros.h"
60*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
61*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
62*795d594fSAndroid Build Coastguard Worker #include "scoped_fast_native_object_access-inl.h"
63*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
64*795d594fSAndroid Build Coastguard Worker #include "startup_completed_task.h"
65*795d594fSAndroid Build Coastguard Worker #include "string_array_utils.h"
66*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
67*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
72*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getTargetHeapUtilization(JNIEnv *,jobject)73*795d594fSAndroid Build Coastguard Worker static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
74*795d594fSAndroid Build Coastguard Worker   return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
VMRuntime_nativeSetTargetHeapUtilization(JNIEnv *,jobject,jfloat target)77*795d594fSAndroid Build Coastguard Worker static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
78*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setHiddenApiExemptions(JNIEnv * env,jclass,jobjectArray exemptions)81*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setHiddenApiExemptions(JNIEnv* env,
82*795d594fSAndroid Build Coastguard Worker                                             jclass,
83*795d594fSAndroid Build Coastguard Worker                                             jobjectArray exemptions) {
84*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> exemptions_vec;
85*795d594fSAndroid Build Coastguard Worker   int exemptions_length = env->GetArrayLength(exemptions);
86*795d594fSAndroid Build Coastguard Worker   for (int i = 0; i < exemptions_length; i++) {
87*795d594fSAndroid Build Coastguard Worker     jstring exemption = reinterpret_cast<jstring>(env->GetObjectArrayElement(exemptions, i));
88*795d594fSAndroid Build Coastguard Worker     const char* raw_exemption = env->GetStringUTFChars(exemption, nullptr);
89*795d594fSAndroid Build Coastguard Worker     exemptions_vec.push_back(raw_exemption);
90*795d594fSAndroid Build Coastguard Worker     env->ReleaseStringUTFChars(exemption, raw_exemption);
91*795d594fSAndroid Build Coastguard Worker   }
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetHiddenApiExemptions(exemptions_vec);
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setHiddenApiAccessLogSamplingRate(JNIEnv *,jclass,jint rate)96*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setHiddenApiAccessLogSamplingRate(JNIEnv*, jclass, jint rate) {
97*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetHiddenApiEventLogSampleRate(rate);
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker 
VMRuntime_newNonMovableArray(JNIEnv * env,jobject,jclass javaElementClass,jint length)100*795d594fSAndroid Build Coastguard Worker static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass,
101*795d594fSAndroid Build Coastguard Worker                                             jint length) {
102*795d594fSAndroid Build Coastguard Worker   ScopedFastNativeObjectAccess soa(env);
103*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(length < 0)) {
104*795d594fSAndroid Build Coastguard Worker     ThrowNegativeArraySizeException(length);
105*795d594fSAndroid Build Coastguard Worker     return nullptr;
106*795d594fSAndroid Build Coastguard Worker   }
107*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
108*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(element_class == nullptr)) {
109*795d594fSAndroid Build Coastguard Worker     ThrowNullPointerException("element class == null");
110*795d594fSAndroid Build Coastguard Worker     return nullptr;
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
113*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> array_class =
114*795d594fSAndroid Build Coastguard Worker       runtime->GetClassLinker()->FindArrayClass(soa.Self(), element_class);
115*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(array_class == nullptr)) {
116*795d594fSAndroid Build Coastguard Worker     return nullptr;
117*795d594fSAndroid Build Coastguard Worker   }
118*795d594fSAndroid Build Coastguard Worker   gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentNonMovingAllocator();
119*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Array> result = mirror::Array::Alloc(soa.Self(),
120*795d594fSAndroid Build Coastguard Worker                                                       array_class,
121*795d594fSAndroid Build Coastguard Worker                                                       length,
122*795d594fSAndroid Build Coastguard Worker                                                       array_class->GetComponentSizeShift(),
123*795d594fSAndroid Build Coastguard Worker                                                       allocator);
124*795d594fSAndroid Build Coastguard Worker   return soa.AddLocalReference<jobject>(result);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker 
VMRuntime_newUnpaddedArray(JNIEnv * env,jobject,jclass javaElementClass,jint length)127*795d594fSAndroid Build Coastguard Worker static jobject VMRuntime_newUnpaddedArray(JNIEnv* env, jobject, jclass javaElementClass,
128*795d594fSAndroid Build Coastguard Worker                                           jint length) {
129*795d594fSAndroid Build Coastguard Worker   ScopedFastNativeObjectAccess soa(env);
130*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(length < 0)) {
131*795d594fSAndroid Build Coastguard Worker     ThrowNegativeArraySizeException(length);
132*795d594fSAndroid Build Coastguard Worker     return nullptr;
133*795d594fSAndroid Build Coastguard Worker   }
134*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
135*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(element_class == nullptr)) {
136*795d594fSAndroid Build Coastguard Worker     ThrowNullPointerException("element class == null");
137*795d594fSAndroid Build Coastguard Worker     return nullptr;
138*795d594fSAndroid Build Coastguard Worker   }
139*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
140*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> array_class = runtime->GetClassLinker()->FindArrayClass(soa.Self(),
141*795d594fSAndroid Build Coastguard Worker                                                                                 element_class);
142*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(array_class == nullptr)) {
143*795d594fSAndroid Build Coastguard Worker     return nullptr;
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker   gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
146*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Array> result =
147*795d594fSAndroid Build Coastguard Worker       mirror::Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
148*795d594fSAndroid Build Coastguard Worker           soa.Self(),
149*795d594fSAndroid Build Coastguard Worker           array_class,
150*795d594fSAndroid Build Coastguard Worker           length,
151*795d594fSAndroid Build Coastguard Worker           array_class->GetComponentSizeShift(),
152*795d594fSAndroid Build Coastguard Worker           allocator);
153*795d594fSAndroid Build Coastguard Worker   return soa.AddLocalReference<jobject>(result);
154*795d594fSAndroid Build Coastguard Worker }
155*795d594fSAndroid Build Coastguard Worker 
VMRuntime_addressOf(JNIEnv * env,jobject,jobject javaArray)156*795d594fSAndroid Build Coastguard Worker static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
157*795d594fSAndroid Build Coastguard Worker   if (javaArray == nullptr) {  // Most likely allocation failed
158*795d594fSAndroid Build Coastguard Worker     return 0;
159*795d594fSAndroid Build Coastguard Worker   }
160*795d594fSAndroid Build Coastguard Worker   ScopedFastNativeObjectAccess soa(env);
161*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(javaArray);
162*795d594fSAndroid Build Coastguard Worker   if (!array->IsArrayInstance()) {
163*795d594fSAndroid Build Coastguard Worker     ThrowIllegalArgumentException("not an array");
164*795d594fSAndroid Build Coastguard Worker     return 0;
165*795d594fSAndroid Build Coastguard Worker   }
166*795d594fSAndroid Build Coastguard Worker   if (array->IsObjectArray()) {
167*795d594fSAndroid Build Coastguard Worker     ThrowIllegalArgumentException("not a primitive array");
168*795d594fSAndroid Build Coastguard Worker     return 0;
169*795d594fSAndroid Build Coastguard Worker   }
170*795d594fSAndroid Build Coastguard Worker   if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
171*795d594fSAndroid Build Coastguard Worker     ThrowRuntimeException("Trying to get address of movable array object");
172*795d594fSAndroid Build Coastguard Worker     return 0;
173*795d594fSAndroid Build Coastguard Worker   }
174*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize(), 0));
175*795d594fSAndroid Build Coastguard Worker }
176*795d594fSAndroid Build Coastguard Worker 
VMRuntime_clearGrowthLimit(JNIEnv *,jobject)177*795d594fSAndroid Build Coastguard Worker static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
178*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->ClearGrowthLimit();
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker 
VMRuntime_clampGrowthLimit(JNIEnv *,jobject)181*795d594fSAndroid Build Coastguard Worker static void VMRuntime_clampGrowthLimit(JNIEnv*, jobject) {
182*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->ClampGrowthLimit();
183*795d594fSAndroid Build Coastguard Worker }
184*795d594fSAndroid Build Coastguard Worker 
VMRuntime_isNativeDebuggable(JNIEnv *,jobject)185*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_isNativeDebuggable(JNIEnv*, jobject) {
186*795d594fSAndroid Build Coastguard Worker   return Runtime::Current()->IsNativeDebuggable();
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker 
VMRuntime_isJavaDebuggable(JNIEnv *,jobject)189*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_isJavaDebuggable(JNIEnv*, jobject) {
190*795d594fSAndroid Build Coastguard Worker   return Runtime::Current()->IsJavaDebuggable();
191*795d594fSAndroid Build Coastguard Worker }
192*795d594fSAndroid Build Coastguard Worker 
VMRuntime_properties(JNIEnv * env,jobject)193*795d594fSAndroid Build Coastguard Worker static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
194*795d594fSAndroid Build Coastguard Worker   const std::vector<std::string>& properties = Runtime::Current()->GetProperties();
195*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::ForEnv(env));
196*795d594fSAndroid Build Coastguard Worker   return soa.AddLocalReference<jobjectArray>(CreateStringArray(soa.Self(), properties));
197*795d594fSAndroid Build Coastguard Worker }
198*795d594fSAndroid Build Coastguard Worker 
199*795d594fSAndroid Build Coastguard Worker // This is for backward compatibility with dalvik which returned the
200*795d594fSAndroid Build Coastguard Worker // meaningless "." when no boot classpath or classpath was
201*795d594fSAndroid Build Coastguard Worker // specified. Unfortunately, some tests were using java.class.path to
202*795d594fSAndroid Build Coastguard Worker // lookup relative file locations, so they are counting on this to be
203*795d594fSAndroid Build Coastguard Worker // ".", presumably some applications or libraries could have as well.
DefaultToDot(const std::string & class_path)204*795d594fSAndroid Build Coastguard Worker static const char* DefaultToDot(const std::string& class_path) {
205*795d594fSAndroid Build Coastguard Worker   return class_path.empty() ? "." : class_path.c_str();
206*795d594fSAndroid Build Coastguard Worker }
207*795d594fSAndroid Build Coastguard Worker 
VMRuntime_bootClassPath(JNIEnv * env,jobject)208*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
209*795d594fSAndroid Build Coastguard Worker   std::string boot_class_path = android::base::Join(Runtime::Current()->GetBootClassPath(), ':');
210*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(DefaultToDot(boot_class_path));
211*795d594fSAndroid Build Coastguard Worker }
212*795d594fSAndroid Build Coastguard Worker 
VMRuntime_classPath(JNIEnv * env,jobject)213*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
214*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
215*795d594fSAndroid Build Coastguard Worker }
216*795d594fSAndroid Build Coastguard Worker 
VMRuntime_vmVersion(JNIEnv * env,jobject)217*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
218*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(Runtime::GetVersion());
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker 
VMRuntime_vmLibrary(JNIEnv * env,jobject)221*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) {
222*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so");
223*795d594fSAndroid Build Coastguard Worker }
224*795d594fSAndroid Build Coastguard Worker 
VMRuntime_vmInstructionSet(JNIEnv * env,jobject)225*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_vmInstructionSet(JNIEnv* env, jobject) {
226*795d594fSAndroid Build Coastguard Worker   InstructionSet isa = Runtime::Current()->GetInstructionSet();
227*795d594fSAndroid Build Coastguard Worker   const char* isa_string = GetInstructionSetString(isa);
228*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(isa_string);
229*795d594fSAndroid Build Coastguard Worker }
230*795d594fSAndroid Build Coastguard Worker 
VMRuntime_is64Bit(JNIEnv *,jobject)231*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_is64Bit(JNIEnv*, jobject) {
232*795d594fSAndroid Build Coastguard Worker   bool is64BitMode = (sizeof(void*) == sizeof(uint64_t));
233*795d594fSAndroid Build Coastguard Worker   return is64BitMode ? JNI_TRUE : JNI_FALSE;
234*795d594fSAndroid Build Coastguard Worker }
235*795d594fSAndroid Build Coastguard Worker 
VMRuntime_isCheckJniEnabled(JNIEnv * env,jobject)236*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_isCheckJniEnabled(JNIEnv* env, jobject) {
237*795d594fSAndroid Build Coastguard Worker   return down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled() ? JNI_TRUE : JNI_FALSE;
238*795d594fSAndroid Build Coastguard Worker }
239*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getSdkVersionNative(JNIEnv * env,jclass klass,jint default_sdk_version)240*795d594fSAndroid Build Coastguard Worker static jint VMRuntime_getSdkVersionNative([[maybe_unused]] JNIEnv* env,
241*795d594fSAndroid Build Coastguard Worker                                           [[maybe_unused]] jclass klass,
242*795d594fSAndroid Build Coastguard Worker                                           jint default_sdk_version) {
243*795d594fSAndroid Build Coastguard Worker   return android::base::GetIntProperty("ro.build.version.sdk",
244*795d594fSAndroid Build Coastguard Worker                                        default_sdk_version);
245*795d594fSAndroid Build Coastguard Worker }
246*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setTargetSdkVersionNative(JNIEnv *,jobject,jint target_sdk_version)247*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setTargetSdkVersionNative(JNIEnv*, jobject, jint target_sdk_version) {
248*795d594fSAndroid Build Coastguard Worker   // This is the target SDK version of the app we're about to run. It is intended that this a place
249*795d594fSAndroid Build Coastguard Worker   // where workarounds can be enabled.
250*795d594fSAndroid Build Coastguard Worker   // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
251*795d594fSAndroid Build Coastguard Worker   // Note that targetSdkVersion may be 0, meaning "current".
252*795d594fSAndroid Build Coastguard Worker   uint32_t uint_target_sdk_version =
253*795d594fSAndroid Build Coastguard Worker       target_sdk_version <= 0 ? static_cast<uint32_t>(SdkVersion::kUnset)
254*795d594fSAndroid Build Coastguard Worker                               : static_cast<uint32_t>(target_sdk_version);
255*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetTargetSdkVersion(uint_target_sdk_version);
256*795d594fSAndroid Build Coastguard Worker 
257*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
258*795d594fSAndroid Build Coastguard Worker   // This part is letting libc/dynamic linker know about current app's
259*795d594fSAndroid Build Coastguard Worker   // target sdk version to enable compatibility workarounds.
260*795d594fSAndroid Build Coastguard Worker   android_set_application_target_sdk_version(uint_target_sdk_version);
261*795d594fSAndroid Build Coastguard Worker #endif
262*795d594fSAndroid Build Coastguard Worker }
263*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setDisabledCompatChangesNative(JNIEnv * env,jobject,jlongArray disabled_compat_changes)264*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setDisabledCompatChangesNative(JNIEnv* env, jobject,
265*795d594fSAndroid Build Coastguard Worker     jlongArray disabled_compat_changes) {
266*795d594fSAndroid Build Coastguard Worker   if (disabled_compat_changes == nullptr) {
267*795d594fSAndroid Build Coastguard Worker     return;
268*795d594fSAndroid Build Coastguard Worker   }
269*795d594fSAndroid Build Coastguard Worker   std::set<uint64_t> disabled_compat_changes_set;
270*795d594fSAndroid Build Coastguard Worker   {
271*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(env);
272*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::LongArray> array = soa.Decode<mirror::LongArray>(disabled_compat_changes);
273*795d594fSAndroid Build Coastguard Worker     int length = array->GetLength();
274*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < length; i++) {
275*795d594fSAndroid Build Coastguard Worker       disabled_compat_changes_set.insert(static_cast<uint64_t>(array->Get(i)));
276*795d594fSAndroid Build Coastguard Worker     }
277*795d594fSAndroid Build Coastguard Worker   }
278*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetCompatFramework().SetDisabledCompatChanges(disabled_compat_changes_set);
279*795d594fSAndroid Build Coastguard Worker }
280*795d594fSAndroid Build Coastguard Worker 
clamp_to_size_t(jlong n)281*795d594fSAndroid Build Coastguard Worker static inline size_t clamp_to_size_t(jlong n) {
282*795d594fSAndroid Build Coastguard Worker   if (sizeof(jlong) > sizeof(size_t)
283*795d594fSAndroid Build Coastguard Worker       && UNLIKELY(n > static_cast<jlong>(std::numeric_limits<size_t>::max()))) {
284*795d594fSAndroid Build Coastguard Worker     return std::numeric_limits<size_t>::max();
285*795d594fSAndroid Build Coastguard Worker   } else {
286*795d594fSAndroid Build Coastguard Worker     return n;
287*795d594fSAndroid Build Coastguard Worker   }
288*795d594fSAndroid Build Coastguard Worker }
289*795d594fSAndroid Build Coastguard Worker 
VMRuntime_registerNativeAllocation(JNIEnv * env,jobject,jlong bytes)290*795d594fSAndroid Build Coastguard Worker static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jlong bytes) {
291*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(bytes < 0)) {
292*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(env);
293*795d594fSAndroid Build Coastguard Worker     ThrowRuntimeException("allocation size negative %" PRId64, bytes);
294*795d594fSAndroid Build Coastguard Worker     return;
295*795d594fSAndroid Build Coastguard Worker   }
296*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, clamp_to_size_t(bytes));
297*795d594fSAndroid Build Coastguard Worker }
298*795d594fSAndroid Build Coastguard Worker 
VMRuntime_registerNativeFree(JNIEnv * env,jobject,jlong bytes)299*795d594fSAndroid Build Coastguard Worker static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jlong bytes) {
300*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(bytes < 0)) {
301*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(env);
302*795d594fSAndroid Build Coastguard Worker     ThrowRuntimeException("allocation size negative %" PRId64, bytes);
303*795d594fSAndroid Build Coastguard Worker     return;
304*795d594fSAndroid Build Coastguard Worker   }
305*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->RegisterNativeFree(env, clamp_to_size_t(bytes));
306*795d594fSAndroid Build Coastguard Worker }
307*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getNotifyNativeInterval(JNIEnv *,jclass)308*795d594fSAndroid Build Coastguard Worker static jint VMRuntime_getNotifyNativeInterval(JNIEnv*, jclass) {
309*795d594fSAndroid Build Coastguard Worker   return Runtime::Current()->GetHeap()->GetNotifyNativeInterval();
310*795d594fSAndroid Build Coastguard Worker }
311*795d594fSAndroid Build Coastguard Worker 
VMRuntime_notifyNativeAllocationsInternal(JNIEnv * env,jobject)312*795d594fSAndroid Build Coastguard Worker static void VMRuntime_notifyNativeAllocationsInternal(JNIEnv* env, jobject) {
313*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->NotifyNativeAllocations(env);
314*795d594fSAndroid Build Coastguard Worker }
315*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getFinalizerTimeoutMs(JNIEnv *,jobject)316*795d594fSAndroid Build Coastguard Worker static jlong VMRuntime_getFinalizerTimeoutMs(JNIEnv*, jobject) {
317*795d594fSAndroid Build Coastguard Worker   return Runtime::Current()->GetFinalizerTimeoutMs();
318*795d594fSAndroid Build Coastguard Worker }
319*795d594fSAndroid Build Coastguard Worker 
VMRuntime_registerSensitiveThread(JNIEnv *,jobject)320*795d594fSAndroid Build Coastguard Worker static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
321*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->RegisterSensitiveThread();
322*795d594fSAndroid Build Coastguard Worker }
323*795d594fSAndroid Build Coastguard Worker 
VMRuntime_updateProcessState(JNIEnv *,jobject,jint process_state)324*795d594fSAndroid Build Coastguard Worker static void VMRuntime_updateProcessState(JNIEnv*, jobject, jint process_state) {
325*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
326*795d594fSAndroid Build Coastguard Worker   runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
327*795d594fSAndroid Build Coastguard Worker }
328*795d594fSAndroid Build Coastguard Worker 
VMRuntime_notifyStartupCompleted(JNIEnv *,jobject)329*795d594fSAndroid Build Coastguard Worker static void VMRuntime_notifyStartupCompleted(JNIEnv*, jobject) {
330*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->AddHeapTask(new StartupCompletedTask(NanoTime()));
331*795d594fSAndroid Build Coastguard Worker }
332*795d594fSAndroid Build Coastguard Worker 
VMRuntime_trimHeap(JNIEnv * env,jobject)333*795d594fSAndroid Build Coastguard Worker static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
334*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->Trim(Thread::ForEnv(env));
335*795d594fSAndroid Build Coastguard Worker }
336*795d594fSAndroid Build Coastguard Worker 
VMRuntime_requestHeapTrim(JNIEnv * env,jobject)337*795d594fSAndroid Build Coastguard Worker static void VMRuntime_requestHeapTrim(JNIEnv* env, jobject) {
338*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->RequestTrim(Thread::ForEnv(env));
339*795d594fSAndroid Build Coastguard Worker }
340*795d594fSAndroid Build Coastguard Worker 
VMRuntime_requestConcurrentGC(JNIEnv * env,jobject)341*795d594fSAndroid Build Coastguard Worker static void VMRuntime_requestConcurrentGC(JNIEnv* env, jobject) {
342*795d594fSAndroid Build Coastguard Worker   gc::Heap *heap = Runtime::Current()->GetHeap();
343*795d594fSAndroid Build Coastguard Worker   heap->RequestConcurrentGC(Thread::ForEnv(env),
344*795d594fSAndroid Build Coastguard Worker                             gc::kGcCauseBackground,
345*795d594fSAndroid Build Coastguard Worker                             true,
346*795d594fSAndroid Build Coastguard Worker                             heap->GetCurrentGcNum());
347*795d594fSAndroid Build Coastguard Worker }
348*795d594fSAndroid Build Coastguard Worker 
VMRuntime_startHeapTaskProcessor(JNIEnv * env,jobject)349*795d594fSAndroid Build Coastguard Worker static void VMRuntime_startHeapTaskProcessor(JNIEnv* env, jobject) {
350*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->GetTaskProcessor()->Start(Thread::ForEnv(env));
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker 
VMRuntime_stopHeapTaskProcessor(JNIEnv * env,jobject)353*795d594fSAndroid Build Coastguard Worker static void VMRuntime_stopHeapTaskProcessor(JNIEnv* env, jobject) {
354*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->GetTaskProcessor()->Stop(Thread::ForEnv(env));
355*795d594fSAndroid Build Coastguard Worker }
356*795d594fSAndroid Build Coastguard Worker 
VMRuntime_runHeapTasks(JNIEnv * env,jobject)357*795d594fSAndroid Build Coastguard Worker static void VMRuntime_runHeapTasks(JNIEnv* env, jobject) {
358*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->GetTaskProcessor()->RunAllTasks(Thread::ForEnv(env));
359*795d594fSAndroid Build Coastguard Worker }
360*795d594fSAndroid Build Coastguard Worker 
VMRuntime_preloadDexCaches(JNIEnv * env,jobject)361*795d594fSAndroid Build Coastguard Worker static void VMRuntime_preloadDexCaches([[maybe_unused]] JNIEnv* env, jobject) {}
362*795d594fSAndroid Build Coastguard Worker 
363*795d594fSAndroid Build Coastguard Worker /*
364*795d594fSAndroid Build Coastguard Worker  * This is called by the framework after it loads a code path on behalf of the app.
365*795d594fSAndroid Build Coastguard Worker  * The code_path_type indicates the type of the apk being loaded and can be used
366*795d594fSAndroid Build Coastguard Worker  * for more precise telemetry (e.g. is the split apk odex up to date?) and debugging.
367*795d594fSAndroid Build Coastguard Worker  */
VMRuntime_registerAppInfo(JNIEnv * env,jclass clazz,jstring package_name,jstring cur_profile_file,jstring ref_profile_file,jobjectArray code_paths,jint code_path_type)368*795d594fSAndroid Build Coastguard Worker static void VMRuntime_registerAppInfo(JNIEnv* env,
369*795d594fSAndroid Build Coastguard Worker                                       [[maybe_unused]] jclass clazz,
370*795d594fSAndroid Build Coastguard Worker                                       jstring package_name,
371*795d594fSAndroid Build Coastguard Worker                                       jstring cur_profile_file,
372*795d594fSAndroid Build Coastguard Worker                                       jstring ref_profile_file,
373*795d594fSAndroid Build Coastguard Worker                                       jobjectArray code_paths,
374*795d594fSAndroid Build Coastguard Worker                                       jint code_path_type) {
375*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> code_paths_vec;
376*795d594fSAndroid Build Coastguard Worker   int code_paths_length = env->GetArrayLength(code_paths);
377*795d594fSAndroid Build Coastguard Worker   for (int i = 0; i < code_paths_length; i++) {
378*795d594fSAndroid Build Coastguard Worker     jstring code_path = reinterpret_cast<jstring>(env->GetObjectArrayElement(code_paths, i));
379*795d594fSAndroid Build Coastguard Worker     const char* raw_code_path = env->GetStringUTFChars(code_path, nullptr);
380*795d594fSAndroid Build Coastguard Worker     code_paths_vec.push_back(raw_code_path);
381*795d594fSAndroid Build Coastguard Worker     env->ReleaseStringUTFChars(code_path, raw_code_path);
382*795d594fSAndroid Build Coastguard Worker   }
383*795d594fSAndroid Build Coastguard Worker 
384*795d594fSAndroid Build Coastguard Worker   const char* raw_cur_profile_file = env->GetStringUTFChars(cur_profile_file, nullptr);
385*795d594fSAndroid Build Coastguard Worker   std::string cur_profile_file_str(raw_cur_profile_file);
386*795d594fSAndroid Build Coastguard Worker   env->ReleaseStringUTFChars(cur_profile_file, raw_cur_profile_file);
387*795d594fSAndroid Build Coastguard Worker 
388*795d594fSAndroid Build Coastguard Worker   const char* raw_ref_profile_file = env->GetStringUTFChars(ref_profile_file, nullptr);
389*795d594fSAndroid Build Coastguard Worker   std::string ref_profile_file_str(raw_ref_profile_file);
390*795d594fSAndroid Build Coastguard Worker   env->ReleaseStringUTFChars(ref_profile_file, raw_ref_profile_file);
391*795d594fSAndroid Build Coastguard Worker 
392*795d594fSAndroid Build Coastguard Worker   const char* raw_package_name = env->GetStringUTFChars(package_name, nullptr);
393*795d594fSAndroid Build Coastguard Worker   std::string package_name_str(raw_package_name);
394*795d594fSAndroid Build Coastguard Worker   env->ReleaseStringUTFChars(package_name, raw_package_name);
395*795d594fSAndroid Build Coastguard Worker 
396*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->RegisterAppInfo(
397*795d594fSAndroid Build Coastguard Worker       package_name_str,
398*795d594fSAndroid Build Coastguard Worker       code_paths_vec,
399*795d594fSAndroid Build Coastguard Worker       cur_profile_file_str,
400*795d594fSAndroid Build Coastguard Worker       ref_profile_file_str,
401*795d594fSAndroid Build Coastguard Worker       static_cast<int32_t>(code_path_type));
402*795d594fSAndroid Build Coastguard Worker }
403*795d594fSAndroid Build Coastguard Worker 
VMRuntime_isBootClassPathOnDisk(JNIEnv * env,jclass,jstring java_instruction_set)404*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_isBootClassPathOnDisk(JNIEnv* env, jclass, jstring java_instruction_set) {
405*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars instruction_set(env, java_instruction_set);
406*795d594fSAndroid Build Coastguard Worker   if (instruction_set.c_str() == nullptr) {
407*795d594fSAndroid Build Coastguard Worker     return JNI_FALSE;
408*795d594fSAndroid Build Coastguard Worker   }
409*795d594fSAndroid Build Coastguard Worker   InstructionSet isa = GetInstructionSetFromString(instruction_set.c_str());
410*795d594fSAndroid Build Coastguard Worker   if (isa == InstructionSet::kNone) {
411*795d594fSAndroid Build Coastguard Worker     ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
412*795d594fSAndroid Build Coastguard Worker     std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
413*795d594fSAndroid Build Coastguard Worker     env->ThrowNew(iae.get(), message.c_str());
414*795d594fSAndroid Build Coastguard Worker     return JNI_FALSE;
415*795d594fSAndroid Build Coastguard Worker   }
416*795d594fSAndroid Build Coastguard Worker   return gc::space::ImageSpace::IsBootClassPathOnDisk(isa);
417*795d594fSAndroid Build Coastguard Worker }
418*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getCurrentInstructionSet(JNIEnv * env,jclass)419*795d594fSAndroid Build Coastguard Worker static jstring VMRuntime_getCurrentInstructionSet(JNIEnv* env, jclass) {
420*795d594fSAndroid Build Coastguard Worker   return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
421*795d594fSAndroid Build Coastguard Worker }
422*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setSystemDaemonThreadPriority(JNIEnv * env,jclass klass)423*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setSystemDaemonThreadPriority([[maybe_unused]] JNIEnv* env,
424*795d594fSAndroid Build Coastguard Worker                                                     [[maybe_unused]] jclass klass) {
425*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
426*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
427*795d594fSAndroid Build Coastguard Worker   DCHECK(self != nullptr);
428*795d594fSAndroid Build Coastguard Worker   pid_t tid = self->GetTid();
429*795d594fSAndroid Build Coastguard Worker   // We use a priority lower than the default for the system daemon threads (eg HeapTaskDaemon) to
430*795d594fSAndroid Build Coastguard Worker   // avoid jank due to CPU contentions between GC and other UI-related threads. b/36631902.
431*795d594fSAndroid Build Coastguard Worker   // We may use a native priority that doesn't have a corresponding java.lang.Thread-level priority.
432*795d594fSAndroid Build Coastguard Worker   static constexpr int kSystemDaemonNiceValue = 4;  // priority 124
433*795d594fSAndroid Build Coastguard Worker   if (setpriority(PRIO_PROCESS, tid, kSystemDaemonNiceValue) != 0) {
434*795d594fSAndroid Build Coastguard Worker     PLOG(INFO) << *self << " setpriority(PRIO_PROCESS, " << tid << ", "
435*795d594fSAndroid Build Coastguard Worker                << kSystemDaemonNiceValue << ") failed";
436*795d594fSAndroid Build Coastguard Worker   }
437*795d594fSAndroid Build Coastguard Worker #endif
438*795d594fSAndroid Build Coastguard Worker }
439*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setDedupeHiddenApiWarnings(JNIEnv * env,jclass klass,jboolean dedupe)440*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setDedupeHiddenApiWarnings([[maybe_unused]] JNIEnv* env,
441*795d594fSAndroid Build Coastguard Worker                                                  [[maybe_unused]] jclass klass,
442*795d594fSAndroid Build Coastguard Worker                                                  jboolean dedupe) {
443*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetDedupeHiddenApiWarnings(dedupe);
444*795d594fSAndroid Build Coastguard Worker }
445*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setProcessPackageName(JNIEnv * env,jclass klass,jstring java_package_name)446*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setProcessPackageName(JNIEnv* env,
447*795d594fSAndroid Build Coastguard Worker                                             [[maybe_unused]] jclass klass,
448*795d594fSAndroid Build Coastguard Worker                                             jstring java_package_name) {
449*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars package_name(env, java_package_name);
450*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetProcessPackageName(package_name.c_str());
451*795d594fSAndroid Build Coastguard Worker }
452*795d594fSAndroid Build Coastguard Worker 
VMRuntime_setProcessDataDirectory(JNIEnv * env,jclass,jstring java_data_dir)453*795d594fSAndroid Build Coastguard Worker static void VMRuntime_setProcessDataDirectory(JNIEnv* env, jclass, jstring java_data_dir) {
454*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars data_dir(env, java_data_dir);
455*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetProcessDataDirectory(data_dir.c_str());
456*795d594fSAndroid Build Coastguard Worker }
457*795d594fSAndroid Build Coastguard Worker 
VMRuntime_bootCompleted(JNIEnv * env,jclass klass)458*795d594fSAndroid Build Coastguard Worker static void VMRuntime_bootCompleted([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jclass klass) {
459*795d594fSAndroid Build Coastguard Worker   jit::Jit* jit = Runtime::Current()->GetJit();
460*795d594fSAndroid Build Coastguard Worker   if (jit != nullptr) {
461*795d594fSAndroid Build Coastguard Worker     jit->BootCompleted();
462*795d594fSAndroid Build Coastguard Worker   }
463*795d594fSAndroid Build Coastguard Worker }
464*795d594fSAndroid Build Coastguard Worker 
465*795d594fSAndroid Build Coastguard Worker class ClearJitCountersVisitor : public ClassVisitor {
466*795d594fSAndroid Build Coastguard Worker  public:
operator ()(ObjPtr<mirror::Class> klass)467*795d594fSAndroid Build Coastguard Worker   bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
468*795d594fSAndroid Build Coastguard Worker     // Avoid some types of classes that don't need their methods visited.
469*795d594fSAndroid Build Coastguard Worker     if (klass->IsProxyClass() ||
470*795d594fSAndroid Build Coastguard Worker         klass->IsArrayClass() ||
471*795d594fSAndroid Build Coastguard Worker         klass->IsPrimitive() ||
472*795d594fSAndroid Build Coastguard Worker         !klass->IsResolved() ||
473*795d594fSAndroid Build Coastguard Worker         klass->IsErroneousResolved()) {
474*795d594fSAndroid Build Coastguard Worker       return true;
475*795d594fSAndroid Build Coastguard Worker     }
476*795d594fSAndroid Build Coastguard Worker     uint16_t threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
477*795d594fSAndroid Build Coastguard Worker     for (ArtMethod& m : klass->GetMethods(kRuntimePointerSize)) {
478*795d594fSAndroid Build Coastguard Worker       if (!m.IsAbstract()) {
479*795d594fSAndroid Build Coastguard Worker         m.ResetCounter(threshold);
480*795d594fSAndroid Build Coastguard Worker       }
481*795d594fSAndroid Build Coastguard Worker     }
482*795d594fSAndroid Build Coastguard Worker     return true;
483*795d594fSAndroid Build Coastguard Worker   }
484*795d594fSAndroid Build Coastguard Worker };
485*795d594fSAndroid Build Coastguard Worker 
VMRuntime_resetJitCounters(JNIEnv * env,jclass klass)486*795d594fSAndroid Build Coastguard Worker static void VMRuntime_resetJitCounters(JNIEnv* env, [[maybe_unused]] jclass klass) {
487*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(env);
488*795d594fSAndroid Build Coastguard Worker   ClearJitCountersVisitor visitor;
489*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetClassLinker()->VisitClasses(&visitor);
490*795d594fSAndroid Build Coastguard Worker }
491*795d594fSAndroid Build Coastguard Worker 
VMRuntime_isValidClassLoaderContext(JNIEnv * env,jclass klass,jstring jencoded_class_loader_context)492*795d594fSAndroid Build Coastguard Worker static jboolean VMRuntime_isValidClassLoaderContext(JNIEnv* env,
493*795d594fSAndroid Build Coastguard Worker                                                     [[maybe_unused]] jclass klass,
494*795d594fSAndroid Build Coastguard Worker                                                     jstring jencoded_class_loader_context) {
495*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(jencoded_class_loader_context == nullptr)) {
496*795d594fSAndroid Build Coastguard Worker     ScopedFastNativeObjectAccess soa(env);
497*795d594fSAndroid Build Coastguard Worker     ThrowNullPointerException("encoded_class_loader_context == null");
498*795d594fSAndroid Build Coastguard Worker     return false;
499*795d594fSAndroid Build Coastguard Worker   }
500*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars encoded_class_loader_context(env, jencoded_class_loader_context);
501*795d594fSAndroid Build Coastguard Worker   return ClassLoaderContext::IsValidEncoding(encoded_class_loader_context.c_str());
502*795d594fSAndroid Build Coastguard Worker }
503*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getBaseApkOptimizationInfo(JNIEnv * env,jclass klass)504*795d594fSAndroid Build Coastguard Worker static jobject VMRuntime_getBaseApkOptimizationInfo(JNIEnv* env, [[maybe_unused]] jclass klass) {
505*795d594fSAndroid Build Coastguard Worker   AppInfo* app_info = Runtime::Current()->GetAppInfo();
506*795d594fSAndroid Build Coastguard Worker   DCHECK(app_info != nullptr);
507*795d594fSAndroid Build Coastguard Worker 
508*795d594fSAndroid Build Coastguard Worker   std::string compiler_filter;
509*795d594fSAndroid Build Coastguard Worker   std::string compilation_reason;
510*795d594fSAndroid Build Coastguard Worker   app_info->GetPrimaryApkOptimizationStatus(&compiler_filter, &compilation_reason);
511*795d594fSAndroid Build Coastguard Worker 
512*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jclass> cls(env, env->FindClass("dalvik/system/DexFile$OptimizationInfo"));
513*795d594fSAndroid Build Coastguard Worker   if (cls == nullptr) {
514*795d594fSAndroid Build Coastguard Worker     DCHECK(env->ExceptionCheck());
515*795d594fSAndroid Build Coastguard Worker     return nullptr;
516*795d594fSAndroid Build Coastguard Worker   }
517*795d594fSAndroid Build Coastguard Worker 
518*795d594fSAndroid Build Coastguard Worker   jmethodID ctor = env->GetMethodID(cls.get(), "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
519*795d594fSAndroid Build Coastguard Worker   if (ctor == nullptr) {
520*795d594fSAndroid Build Coastguard Worker     DCHECK(env->ExceptionCheck());
521*795d594fSAndroid Build Coastguard Worker     return nullptr;
522*795d594fSAndroid Build Coastguard Worker   }
523*795d594fSAndroid Build Coastguard Worker 
524*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jstring> j_compiler_filter(env, env->NewStringUTF(compiler_filter.c_str()));
525*795d594fSAndroid Build Coastguard Worker   if (j_compiler_filter == nullptr) {
526*795d594fSAndroid Build Coastguard Worker     DCHECK(env->ExceptionCheck());
527*795d594fSAndroid Build Coastguard Worker     return nullptr;
528*795d594fSAndroid Build Coastguard Worker   }
529*795d594fSAndroid Build Coastguard Worker 
530*795d594fSAndroid Build Coastguard Worker   ScopedLocalRef<jstring> j_compilation_reason(env, env->NewStringUTF(compilation_reason.c_str()));
531*795d594fSAndroid Build Coastguard Worker   if (j_compilation_reason == nullptr) {
532*795d594fSAndroid Build Coastguard Worker     DCHECK(env->ExceptionCheck());
533*795d594fSAndroid Build Coastguard Worker     return nullptr;
534*795d594fSAndroid Build Coastguard Worker   }
535*795d594fSAndroid Build Coastguard Worker 
536*795d594fSAndroid Build Coastguard Worker   return env->NewObject(cls.get(), ctor, j_compiler_filter.get(), j_compilation_reason.get());
537*795d594fSAndroid Build Coastguard Worker }
538*795d594fSAndroid Build Coastguard Worker 
VMRuntime_getFullGcCount(JNIEnv * env,jclass klass)539*795d594fSAndroid Build Coastguard Worker static jlong VMRuntime_getFullGcCount([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jclass klass) {
540*795d594fSAndroid Build Coastguard Worker   metrics::ArtMetrics* metrics = GetMetrics();
541*795d594fSAndroid Build Coastguard Worker   return metrics->FullGcCount()->Value();
542*795d594fSAndroid Build Coastguard Worker }
543*795d594fSAndroid Build Coastguard Worker 
544*795d594fSAndroid Build Coastguard Worker static JNINativeMethod gMethods[] = {
545*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
546*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
547*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, clampGrowthLimit, "()V"),
548*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
549*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
550*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setHiddenApiExemptions, "([Ljava/lang/String;)V"),
551*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setHiddenApiAccessLogSamplingRate, "(I)V"),
552*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
553*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, isNativeDebuggable, "()Z"),
554*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, isJavaDebuggable, "()Z"),
555*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
556*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
557*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, newUnpaddedArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
558*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
559*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getSdkVersionNative, "(I)I"),
560*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
561*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setDisabledCompatChangesNative, "([J)V"),
562*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(J)V"),
563*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, registerNativeFree, "(J)V"),
564*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getNotifyNativeInterval, "()I"),
565*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getFinalizerTimeoutMs, "()J"),
566*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, notifyNativeAllocationsInternal, "()V"),
567*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, notifyStartupCompleted, "()V"),
568*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
569*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
570*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
571*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, runHeapTasks, "()V"),
572*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
573*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, startHeapTaskProcessor, "()V"),
574*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, stopHeapTaskProcessor, "()V"),
575*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
576*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
577*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
578*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, vmInstructionSet, "()Ljava/lang/String;"),
579*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, is64Bit, "()Z"),
580*795d594fSAndroid Build Coastguard Worker   FAST_NATIVE_METHOD(VMRuntime, isCheckJniEnabled, "()Z"),
581*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"),
582*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, registerAppInfo,
583*795d594fSAndroid Build Coastguard Worker       "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;I)V"),
584*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
585*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
586*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setSystemDaemonThreadPriority, "()V"),
587*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setDedupeHiddenApiWarnings, "(Z)V"),
588*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setProcessPackageName, "(Ljava/lang/String;)V"),
589*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, setProcessDataDirectory, "(Ljava/lang/String;)V"),
590*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, bootCompleted, "()V"),
591*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, resetJitCounters, "()V"),
592*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, isValidClassLoaderContext, "(Ljava/lang/String;)Z"),
593*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getBaseApkOptimizationInfo,
594*795d594fSAndroid Build Coastguard Worker       "()Ldalvik/system/DexFile$OptimizationInfo;"),
595*795d594fSAndroid Build Coastguard Worker   NATIVE_METHOD(VMRuntime, getFullGcCount, "()J"),
596*795d594fSAndroid Build Coastguard Worker };
597*795d594fSAndroid Build Coastguard Worker 
register_dalvik_system_VMRuntime(JNIEnv * env)598*795d594fSAndroid Build Coastguard Worker void register_dalvik_system_VMRuntime(JNIEnv* env) {
599*795d594fSAndroid Build Coastguard Worker   if (Runtime::Current()->GetTargetSdkVersion() <= static_cast<uint32_t>(SdkVersion::kU)) {
600*795d594fSAndroid Build Coastguard Worker     real_register_dalvik_system_VMRuntime(env);
601*795d594fSAndroid Build Coastguard Worker   } else {
602*795d594fSAndroid Build Coastguard Worker     Runtime::Current()->Abort(
603*795d594fSAndroid Build Coastguard Worker         "Call to internal function 'register_dalvik_system_VMRuntime' is not allowed");
604*795d594fSAndroid Build Coastguard Worker   }
605*795d594fSAndroid Build Coastguard Worker }
606*795d594fSAndroid Build Coastguard Worker 
real_register_dalvik_system_VMRuntime(JNIEnv * env)607*795d594fSAndroid Build Coastguard Worker void real_register_dalvik_system_VMRuntime(JNIEnv* env) {
608*795d594fSAndroid Build Coastguard Worker   REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
609*795d594fSAndroid Build Coastguard Worker }
610*795d594fSAndroid Build Coastguard Worker 
611*795d594fSAndroid Build Coastguard Worker }  // namespace art
612