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