• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (array->IsObjectArray()) {
165     ThrowIllegalArgumentException("not a primitive array");
166     return 0;
167   }
168   if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
169     ThrowRuntimeException("Trying to get address of movable array object");
170     return 0;
171   }
172   return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize(), 0));
173 }
174 
VMRuntime_clearGrowthLimit(JNIEnv *,jobject)175 static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
176   Runtime::Current()->GetHeap()->ClearGrowthLimit();
177 }
178 
VMRuntime_clampGrowthLimit(JNIEnv *,jobject)179 static void VMRuntime_clampGrowthLimit(JNIEnv*, jobject) {
180   Runtime::Current()->GetHeap()->ClampGrowthLimit();
181 }
182 
VMRuntime_isNativeDebuggable(JNIEnv *,jobject)183 static jboolean VMRuntime_isNativeDebuggable(JNIEnv*, jobject) {
184   return Runtime::Current()->IsNativeDebuggable();
185 }
186 
VMRuntime_isJavaDebuggable(JNIEnv *,jobject)187 static jboolean VMRuntime_isJavaDebuggable(JNIEnv*, jobject) {
188   return Runtime::Current()->IsJavaDebuggable();
189 }
190 
VMRuntime_properties(JNIEnv * env,jobject)191 static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
192   DCHECK(WellKnownClasses::java_lang_String != nullptr);
193 
194   const std::vector<std::string>& properties = Runtime::Current()->GetProperties();
195   ScopedLocalRef<jobjectArray> ret(env,
196                                    env->NewObjectArray(static_cast<jsize>(properties.size()),
197                                                        WellKnownClasses::java_lang_String,
198                                                        nullptr /* initial element */));
199   if (ret == nullptr) {
200     DCHECK(env->ExceptionCheck());
201     return nullptr;
202   }
203   for (size_t i = 0; i != properties.size(); ++i) {
204     ScopedLocalRef<jstring> str(env, env->NewStringUTF(properties[i].c_str()));
205     if (str == nullptr) {
206       DCHECK(env->ExceptionCheck());
207       return nullptr;
208     }
209     env->SetObjectArrayElement(ret.get(), static_cast<jsize>(i), str.get());
210     DCHECK(!env->ExceptionCheck());
211   }
212   return ret.release();
213 }
214 
215 // This is for backward compatibility with dalvik which returned the
216 // meaningless "." when no boot classpath or classpath was
217 // specified. Unfortunately, some tests were using java.class.path to
218 // lookup relative file locations, so they are counting on this to be
219 // ".", presumably some applications or libraries could have as well.
DefaultToDot(const std::string & class_path)220 static const char* DefaultToDot(const std::string& class_path) {
221   return class_path.empty() ? "." : class_path.c_str();
222 }
223 
VMRuntime_bootClassPath(JNIEnv * env,jobject)224 static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
225   std::string boot_class_path = android::base::Join(Runtime::Current()->GetBootClassPath(), ':');
226   return env->NewStringUTF(DefaultToDot(boot_class_path));
227 }
228 
VMRuntime_classPath(JNIEnv * env,jobject)229 static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
230   return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
231 }
232 
VMRuntime_vmVersion(JNIEnv * env,jobject)233 static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
234   return env->NewStringUTF(Runtime::GetVersion());
235 }
236 
VMRuntime_vmLibrary(JNIEnv * env,jobject)237 static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) {
238   return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so");
239 }
240 
VMRuntime_vmInstructionSet(JNIEnv * env,jobject)241 static jstring VMRuntime_vmInstructionSet(JNIEnv* env, jobject) {
242   InstructionSet isa = Runtime::Current()->GetInstructionSet();
243   const char* isa_string = GetInstructionSetString(isa);
244   return env->NewStringUTF(isa_string);
245 }
246 
VMRuntime_is64Bit(JNIEnv *,jobject)247 static jboolean VMRuntime_is64Bit(JNIEnv*, jobject) {
248   bool is64BitMode = (sizeof(void*) == sizeof(uint64_t));
249   return is64BitMode ? JNI_TRUE : JNI_FALSE;
250 }
251 
VMRuntime_isCheckJniEnabled(JNIEnv * env,jobject)252 static jboolean VMRuntime_isCheckJniEnabled(JNIEnv* env, jobject) {
253   return down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled() ? JNI_TRUE : JNI_FALSE;
254 }
255 
VMRuntime_setTargetSdkVersionNative(JNIEnv *,jobject,jint target_sdk_version)256 static void VMRuntime_setTargetSdkVersionNative(JNIEnv*, jobject, jint target_sdk_version) {
257   // This is the target SDK version of the app we're about to run. It is intended that this a place
258   // where workarounds can be enabled.
259   // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
260   // Note that targetSdkVersion may be 0, meaning "current".
261   uint32_t uint_target_sdk_version =
262       target_sdk_version <= 0 ? static_cast<uint32_t>(SdkVersion::kUnset)
263                               : static_cast<uint32_t>(target_sdk_version);
264   Runtime::Current()->SetTargetSdkVersion(uint_target_sdk_version);
265 
266 #ifdef ART_TARGET_ANDROID
267   // This part is letting libc/dynamic linker know about current app's
268   // target sdk version to enable compatibility workarounds.
269   android_set_application_target_sdk_version(uint_target_sdk_version);
270 #endif
271 }
272 
VMRuntime_setDisabledCompatChangesNative(JNIEnv * env,jobject,jlongArray disabled_compat_changes)273 static void VMRuntime_setDisabledCompatChangesNative(JNIEnv* env, jobject,
274     jlongArray disabled_compat_changes) {
275   if (disabled_compat_changes == nullptr) {
276     return;
277   }
278   std::set<uint64_t> disabled_compat_changes_set;
279   int length = env->GetArrayLength(disabled_compat_changes);
280   jlong* elements = env->GetLongArrayElements(disabled_compat_changes, /*isCopy*/nullptr);
281   for (int i = 0; i < length; i++) {
282     disabled_compat_changes_set.insert(static_cast<uint64_t>(elements[i]));
283   }
284   Runtime::Current()->GetCompatFramework().SetDisabledCompatChanges(disabled_compat_changes_set);
285 }
286 
clamp_to_size_t(jlong n)287 static inline size_t clamp_to_size_t(jlong n) {
288   if (sizeof(jlong) > sizeof(size_t)
289       && UNLIKELY(n > static_cast<jlong>(std::numeric_limits<size_t>::max()))) {
290     return std::numeric_limits<size_t>::max();
291   } else {
292     return n;
293   }
294 }
295 
VMRuntime_registerNativeAllocation(JNIEnv * env,jobject,jlong bytes)296 static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jlong bytes) {
297   if (UNLIKELY(bytes < 0)) {
298     ScopedObjectAccess soa(env);
299     ThrowRuntimeException("allocation size negative %" PRId64, bytes);
300     return;
301   }
302   Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, clamp_to_size_t(bytes));
303 }
304 
VMRuntime_registerNativeFree(JNIEnv * env,jobject,jlong bytes)305 static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jlong bytes) {
306   if (UNLIKELY(bytes < 0)) {
307     ScopedObjectAccess soa(env);
308     ThrowRuntimeException("allocation size negative %" PRId64, bytes);
309     return;
310   }
311   Runtime::Current()->GetHeap()->RegisterNativeFree(env, clamp_to_size_t(bytes));
312 }
313 
VMRuntime_getNotifyNativeInterval(JNIEnv *,jclass)314 static jint VMRuntime_getNotifyNativeInterval(JNIEnv*, jclass) {
315   return Runtime::Current()->GetHeap()->GetNotifyNativeInterval();
316 }
317 
VMRuntime_notifyNativeAllocationsInternal(JNIEnv * env,jobject)318 static void VMRuntime_notifyNativeAllocationsInternal(JNIEnv* env, jobject) {
319   Runtime::Current()->GetHeap()->NotifyNativeAllocations(env);
320 }
321 
VMRuntime_getFinalizerTimeoutMs(JNIEnv *,jobject)322 static jlong VMRuntime_getFinalizerTimeoutMs(JNIEnv*, jobject) {
323   return Runtime::Current()->GetFinalizerTimeoutMs();
324 }
325 
VMRuntime_registerSensitiveThread(JNIEnv *,jobject)326 static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
327   Runtime::Current()->RegisterSensitiveThread();
328 }
329 
VMRuntime_updateProcessState(JNIEnv *,jobject,jint process_state)330 static void VMRuntime_updateProcessState(JNIEnv*, jobject, jint process_state) {
331   Runtime* runtime = Runtime::Current();
332   runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
333 }
334 
VMRuntime_notifyStartupCompleted(JNIEnv *,jobject)335 static void VMRuntime_notifyStartupCompleted(JNIEnv*, jobject) {
336   Runtime::Current()->NotifyStartupCompleted();
337 }
338 
VMRuntime_trimHeap(JNIEnv * env,jobject)339 static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
340   Runtime::Current()->GetHeap()->Trim(ThreadForEnv(env));
341 }
342 
VMRuntime_requestHeapTrim(JNIEnv * env,jobject)343 static void VMRuntime_requestHeapTrim(JNIEnv* env, jobject) {
344   Runtime::Current()->GetHeap()->RequestTrim(ThreadForEnv(env));
345 }
346 
VMRuntime_requestConcurrentGC(JNIEnv * env,jobject)347 static void VMRuntime_requestConcurrentGC(JNIEnv* env, jobject) {
348   gc::Heap *heap = Runtime::Current()->GetHeap();
349   heap->RequestConcurrentGC(ThreadForEnv(env),
350                             gc::kGcCauseBackground,
351                             true,
352                             heap->GetCurrentGcNum());
353 }
354 
VMRuntime_startHeapTaskProcessor(JNIEnv * env,jobject)355 static void VMRuntime_startHeapTaskProcessor(JNIEnv* env, jobject) {
356   Runtime::Current()->GetHeap()->GetTaskProcessor()->Start(ThreadForEnv(env));
357 }
358 
VMRuntime_stopHeapTaskProcessor(JNIEnv * env,jobject)359 static void VMRuntime_stopHeapTaskProcessor(JNIEnv* env, jobject) {
360   Runtime::Current()->GetHeap()->GetTaskProcessor()->Stop(ThreadForEnv(env));
361 }
362 
VMRuntime_runHeapTasks(JNIEnv * env,jobject)363 static void VMRuntime_runHeapTasks(JNIEnv* env, jobject) {
364   Runtime::Current()->GetHeap()->GetTaskProcessor()->RunAllTasks(ThreadForEnv(env));
365 }
366 
VMRuntime_preloadDexCaches(JNIEnv * env ATTRIBUTE_UNUSED,jobject)367 static void VMRuntime_preloadDexCaches(JNIEnv* env ATTRIBUTE_UNUSED, jobject) {
368 }
369 
370 /*
371  * This is called by the framework after it loads a code path on behalf of the app.
372  * The code_path_type indicates the type of the apk being loaded and can be used
373  * for more precise telemetry (e.g. is the split apk odex up to date?) and debugging.
374  */
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)375 static void VMRuntime_registerAppInfo(JNIEnv* env,
376                                       jclass clazz ATTRIBUTE_UNUSED,
377                                       jstring package_name,
378                                       jstring cur_profile_file,
379                                       jstring ref_profile_file,
380                                       jobjectArray code_paths,
381                                       jint code_path_type) {
382   std::vector<std::string> code_paths_vec;
383   int code_paths_length = env->GetArrayLength(code_paths);
384   for (int i = 0; i < code_paths_length; i++) {
385     jstring code_path = reinterpret_cast<jstring>(env->GetObjectArrayElement(code_paths, i));
386     const char* raw_code_path = env->GetStringUTFChars(code_path, nullptr);
387     code_paths_vec.push_back(raw_code_path);
388     env->ReleaseStringUTFChars(code_path, raw_code_path);
389   }
390 
391   const char* raw_cur_profile_file = env->GetStringUTFChars(cur_profile_file, nullptr);
392   std::string cur_profile_file_str(raw_cur_profile_file);
393   env->ReleaseStringUTFChars(cur_profile_file, raw_cur_profile_file);
394 
395   const char* raw_ref_profile_file = env->GetStringUTFChars(ref_profile_file, nullptr);
396   std::string ref_profile_file_str(raw_ref_profile_file);
397   env->ReleaseStringUTFChars(ref_profile_file, raw_ref_profile_file);
398 
399   const char* raw_package_name = env->GetStringUTFChars(package_name, nullptr);
400   std::string package_name_str(raw_package_name);
401   env->ReleaseStringUTFChars(package_name, raw_package_name);
402 
403   Runtime::Current()->RegisterAppInfo(
404       package_name_str,
405       code_paths_vec,
406       cur_profile_file_str,
407       ref_profile_file_str,
408       static_cast<int32_t>(code_path_type));
409 }
410 
VMRuntime_isBootClassPathOnDisk(JNIEnv * env,jclass,jstring java_instruction_set)411 static jboolean VMRuntime_isBootClassPathOnDisk(JNIEnv* env, jclass, jstring java_instruction_set) {
412   ScopedUtfChars instruction_set(env, java_instruction_set);
413   if (instruction_set.c_str() == nullptr) {
414     return JNI_FALSE;
415   }
416   InstructionSet isa = GetInstructionSetFromString(instruction_set.c_str());
417   if (isa == InstructionSet::kNone) {
418     ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
419     std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
420     env->ThrowNew(iae.get(), message.c_str());
421     return JNI_FALSE;
422   }
423   return gc::space::ImageSpace::IsBootClassPathOnDisk(isa);
424 }
425 
VMRuntime_getCurrentInstructionSet(JNIEnv * env,jclass)426 static jstring VMRuntime_getCurrentInstructionSet(JNIEnv* env, jclass) {
427   return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
428 }
429 
VMRuntime_setSystemDaemonThreadPriority(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED)430 static void VMRuntime_setSystemDaemonThreadPriority(JNIEnv* env ATTRIBUTE_UNUSED,
431                                                     jclass klass ATTRIBUTE_UNUSED) {
432 #ifdef ART_TARGET_ANDROID
433   Thread* self = Thread::Current();
434   DCHECK(self != nullptr);
435   pid_t tid = self->GetTid();
436   // We use a priority lower than the default for the system daemon threads (eg HeapTaskDaemon) to
437   // avoid jank due to CPU contentions between GC and other UI-related threads. b/36631902.
438   // We may use a native priority that doesn't have a corresponding java.lang.Thread-level priority.
439   static constexpr int kSystemDaemonNiceValue = 4;  // priority 124
440   if (setpriority(PRIO_PROCESS, tid, kSystemDaemonNiceValue) != 0) {
441     PLOG(INFO) << *self << " setpriority(PRIO_PROCESS, " << tid << ", "
442                << kSystemDaemonNiceValue << ") failed";
443   }
444 #endif
445 }
446 
VMRuntime_setDedupeHiddenApiWarnings(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED,jboolean dedupe)447 static void VMRuntime_setDedupeHiddenApiWarnings(JNIEnv* env ATTRIBUTE_UNUSED,
448                                                  jclass klass ATTRIBUTE_UNUSED,
449                                                  jboolean dedupe) {
450   Runtime::Current()->SetDedupeHiddenApiWarnings(dedupe);
451 }
452 
VMRuntime_setProcessPackageName(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jstring java_package_name)453 static void VMRuntime_setProcessPackageName(JNIEnv* env,
454                                             jclass klass ATTRIBUTE_UNUSED,
455                                             jstring java_package_name) {
456   ScopedUtfChars package_name(env, java_package_name);
457   Runtime::Current()->SetProcessPackageName(package_name.c_str());
458 }
459 
VMRuntime_setProcessDataDirectory(JNIEnv * env,jclass,jstring java_data_dir)460 static void VMRuntime_setProcessDataDirectory(JNIEnv* env, jclass, jstring java_data_dir) {
461   ScopedUtfChars data_dir(env, java_data_dir);
462   Runtime::Current()->SetProcessDataDirectory(data_dir.c_str());
463 }
464 
VMRuntime_bootCompleted(JNIEnv * env ATTRIBUTE_UNUSED,jclass klass ATTRIBUTE_UNUSED)465 static void VMRuntime_bootCompleted(JNIEnv* env ATTRIBUTE_UNUSED,
466                                     jclass klass ATTRIBUTE_UNUSED) {
467   jit::Jit* jit = Runtime::Current()->GetJit();
468   if (jit != nullptr) {
469     jit->BootCompleted();
470   }
471 }
472 
473 class ClearJitCountersVisitor : public ClassVisitor {
474  public:
operator ()(ObjPtr<mirror::Class> klass)475   bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
476     // Avoid some types of classes that don't need their methods visited.
477     if (klass->IsProxyClass() ||
478         klass->IsArrayClass() ||
479         klass->IsPrimitive() ||
480         !klass->IsResolved() ||
481         klass->IsErroneousResolved()) {
482       return true;
483     }
484     uint16_t threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
485     for (ArtMethod& m : klass->GetMethods(kRuntimePointerSize)) {
486       if (!m.IsAbstract()) {
487         m.ResetCounter(threshold);
488       }
489     }
490     return true;
491   }
492 };
493 
VMRuntime_resetJitCounters(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED)494 static void VMRuntime_resetJitCounters(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
495   ScopedObjectAccess soa(env);
496   ClearJitCountersVisitor visitor;
497   Runtime::Current()->GetClassLinker()->VisitClasses(&visitor);
498 }
499 
VMRuntime_isValidClassLoaderContext(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jstring jencoded_class_loader_context)500 static jboolean VMRuntime_isValidClassLoaderContext(JNIEnv* env,
501                                                     jclass klass ATTRIBUTE_UNUSED,
502                                                     jstring jencoded_class_loader_context) {
503   if (UNLIKELY(jencoded_class_loader_context == nullptr)) {
504     ScopedFastNativeObjectAccess soa(env);
505     ThrowNullPointerException("encoded_class_loader_context == null");
506     return false;
507   }
508   ScopedUtfChars encoded_class_loader_context(env, jencoded_class_loader_context);
509   return ClassLoaderContext::IsValidEncoding(encoded_class_loader_context.c_str());
510 }
511 
512 static JNINativeMethod gMethods[] = {
513   FAST_NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
514   NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
515   NATIVE_METHOD(VMRuntime, clampGrowthLimit, "()V"),
516   NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
517   NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
518   NATIVE_METHOD(VMRuntime, setHiddenApiExemptions, "([Ljava/lang/String;)V"),
519   NATIVE_METHOD(VMRuntime, setHiddenApiAccessLogSamplingRate, "(I)V"),
520   NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
521   FAST_NATIVE_METHOD(VMRuntime, isNativeDebuggable, "()Z"),
522   NATIVE_METHOD(VMRuntime, isJavaDebuggable, "()Z"),
523   NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
524   FAST_NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
525   FAST_NATIVE_METHOD(VMRuntime, newUnpaddedArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
526   NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
527   NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
528   NATIVE_METHOD(VMRuntime, setDisabledCompatChangesNative, "([J)V"),
529   NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(J)V"),
530   NATIVE_METHOD(VMRuntime, registerNativeFree, "(J)V"),
531   NATIVE_METHOD(VMRuntime, getNotifyNativeInterval, "()I"),
532   NATIVE_METHOD(VMRuntime, getFinalizerTimeoutMs, "()J"),
533   NATIVE_METHOD(VMRuntime, notifyNativeAllocationsInternal, "()V"),
534   NATIVE_METHOD(VMRuntime, notifyStartupCompleted, "()V"),
535   NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
536   NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
537   NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
538   NATIVE_METHOD(VMRuntime, runHeapTasks, "()V"),
539   NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
540   NATIVE_METHOD(VMRuntime, startHeapTaskProcessor, "()V"),
541   NATIVE_METHOD(VMRuntime, stopHeapTaskProcessor, "()V"),
542   NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
543   NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
544   NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
545   NATIVE_METHOD(VMRuntime, vmInstructionSet, "()Ljava/lang/String;"),
546   FAST_NATIVE_METHOD(VMRuntime, is64Bit, "()Z"),
547   FAST_NATIVE_METHOD(VMRuntime, isCheckJniEnabled, "()Z"),
548   NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"),
549   NATIVE_METHOD(VMRuntime, registerAppInfo,
550       "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;I)V"),
551   NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
552   NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
553   NATIVE_METHOD(VMRuntime, setSystemDaemonThreadPriority, "()V"),
554   NATIVE_METHOD(VMRuntime, setDedupeHiddenApiWarnings, "(Z)V"),
555   NATIVE_METHOD(VMRuntime, setProcessPackageName, "(Ljava/lang/String;)V"),
556   NATIVE_METHOD(VMRuntime, setProcessDataDirectory, "(Ljava/lang/String;)V"),
557   NATIVE_METHOD(VMRuntime, bootCompleted, "()V"),
558   NATIVE_METHOD(VMRuntime, resetJitCounters, "()V"),
559   NATIVE_METHOD(VMRuntime, isValidClassLoaderContext, "(Ljava/lang/String;)Z"),
560 };
561 
register_dalvik_system_VMRuntime(JNIEnv * env)562 void register_dalvik_system_VMRuntime(JNIEnv* env) {
563   REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
564 }
565 
566 }  // namespace art
567