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