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/pointer_size.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 HIDDEN {
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,jclass klass,jint default_sdk_version)240 static jint VMRuntime_getSdkVersionNative([[maybe_unused]] JNIEnv* env,
241 [[maybe_unused]] jclass klass,
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 {
271 ScopedObjectAccess soa(env);
272 ObjPtr<mirror::LongArray> array = soa.Decode<mirror::LongArray>(disabled_compat_changes);
273 int length = array->GetLength();
274 for (int i = 0; i < length; i++) {
275 disabled_compat_changes_set.insert(static_cast<uint64_t>(array->Get(i)));
276 }
277 }
278 Runtime::Current()->GetCompatFramework().SetDisabledCompatChanges(disabled_compat_changes_set);
279 }
280
clamp_to_size_t(jlong n)281 static inline size_t clamp_to_size_t(jlong n) {
282 if (sizeof(jlong) > sizeof(size_t)
283 && UNLIKELY(n > static_cast<jlong>(std::numeric_limits<size_t>::max()))) {
284 return std::numeric_limits<size_t>::max();
285 } else {
286 return n;
287 }
288 }
289
VMRuntime_registerNativeAllocation(JNIEnv * env,jobject,jlong bytes)290 static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jlong bytes) {
291 if (UNLIKELY(bytes < 0)) {
292 ScopedObjectAccess soa(env);
293 ThrowRuntimeException("allocation size negative %" PRId64, bytes);
294 return;
295 }
296 Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, clamp_to_size_t(bytes));
297 }
298
VMRuntime_registerNativeFree(JNIEnv * env,jobject,jlong bytes)299 static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jlong bytes) {
300 if (UNLIKELY(bytes < 0)) {
301 ScopedObjectAccess soa(env);
302 ThrowRuntimeException("allocation size negative %" PRId64, bytes);
303 return;
304 }
305 Runtime::Current()->GetHeap()->RegisterNativeFree(env, clamp_to_size_t(bytes));
306 }
307
VMRuntime_getNotifyNativeInterval(JNIEnv *,jclass)308 static jint VMRuntime_getNotifyNativeInterval(JNIEnv*, jclass) {
309 return Runtime::Current()->GetHeap()->GetNotifyNativeInterval();
310 }
311
VMRuntime_notifyNativeAllocationsInternal(JNIEnv * env,jobject)312 static void VMRuntime_notifyNativeAllocationsInternal(JNIEnv* env, jobject) {
313 Runtime::Current()->GetHeap()->NotifyNativeAllocations(env);
314 }
315
VMRuntime_getFinalizerTimeoutMs(JNIEnv *,jobject)316 static jlong VMRuntime_getFinalizerTimeoutMs(JNIEnv*, jobject) {
317 return Runtime::Current()->GetFinalizerTimeoutMs();
318 }
319
VMRuntime_registerSensitiveThread(JNIEnv *,jobject)320 static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
321 Runtime::Current()->RegisterSensitiveThread();
322 }
323
VMRuntime_updateProcessState(JNIEnv *,jobject,jint process_state)324 static void VMRuntime_updateProcessState(JNIEnv*, jobject, jint process_state) {
325 Runtime* runtime = Runtime::Current();
326 runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
327 }
328
VMRuntime_notifyStartupCompleted(JNIEnv *,jobject)329 static void VMRuntime_notifyStartupCompleted(JNIEnv*, jobject) {
330 Runtime::Current()->GetHeap()->AddHeapTask(new StartupCompletedTask(NanoTime()));
331 }
332
VMRuntime_trimHeap(JNIEnv * env,jobject)333 static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
334 Runtime::Current()->GetHeap()->Trim(Thread::ForEnv(env));
335 }
336
VMRuntime_requestHeapTrim(JNIEnv * env,jobject)337 static void VMRuntime_requestHeapTrim(JNIEnv* env, jobject) {
338 Runtime::Current()->GetHeap()->RequestTrim(Thread::ForEnv(env));
339 }
340
VMRuntime_requestConcurrentGC(JNIEnv * env,jobject)341 static void VMRuntime_requestConcurrentGC(JNIEnv* env, jobject) {
342 gc::Heap *heap = Runtime::Current()->GetHeap();
343 heap->RequestConcurrentGC(Thread::ForEnv(env),
344 gc::kGcCauseBackground,
345 true,
346 heap->GetCurrentGcNum());
347 }
348
VMRuntime_startHeapTaskProcessor(JNIEnv * env,jobject)349 static void VMRuntime_startHeapTaskProcessor(JNIEnv* env, jobject) {
350 Runtime::Current()->GetHeap()->GetTaskProcessor()->Start(Thread::ForEnv(env));
351 }
352
VMRuntime_stopHeapTaskProcessor(JNIEnv * env,jobject)353 static void VMRuntime_stopHeapTaskProcessor(JNIEnv* env, jobject) {
354 Runtime::Current()->GetHeap()->GetTaskProcessor()->Stop(Thread::ForEnv(env));
355 }
356
VMRuntime_runHeapTasks(JNIEnv * env,jobject)357 static void VMRuntime_runHeapTasks(JNIEnv* env, jobject) {
358 Runtime::Current()->GetHeap()->GetTaskProcessor()->RunAllTasks(Thread::ForEnv(env));
359 }
360
VMRuntime_preloadDexCaches(JNIEnv * env,jobject)361 static void VMRuntime_preloadDexCaches([[maybe_unused]] JNIEnv* env, jobject) {}
362
363 /*
364 * This is called by the framework after it loads a code path on behalf of the app.
365 * The code_path_type indicates the type of the apk being loaded and can be used
366 * for more precise telemetry (e.g. is the split apk odex up to date?) and debugging.
367 */
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 static void VMRuntime_registerAppInfo(JNIEnv* env,
369 [[maybe_unused]] jclass clazz,
370 jstring package_name,
371 jstring cur_profile_file,
372 jstring ref_profile_file,
373 jobjectArray code_paths,
374 jint code_path_type) {
375 std::vector<std::string> code_paths_vec;
376 int code_paths_length = env->GetArrayLength(code_paths);
377 for (int i = 0; i < code_paths_length; i++) {
378 jstring code_path = reinterpret_cast<jstring>(env->GetObjectArrayElement(code_paths, i));
379 const char* raw_code_path = env->GetStringUTFChars(code_path, nullptr);
380 code_paths_vec.push_back(raw_code_path);
381 env->ReleaseStringUTFChars(code_path, raw_code_path);
382 }
383
384 const char* raw_cur_profile_file = env->GetStringUTFChars(cur_profile_file, nullptr);
385 std::string cur_profile_file_str(raw_cur_profile_file);
386 env->ReleaseStringUTFChars(cur_profile_file, raw_cur_profile_file);
387
388 const char* raw_ref_profile_file = env->GetStringUTFChars(ref_profile_file, nullptr);
389 std::string ref_profile_file_str(raw_ref_profile_file);
390 env->ReleaseStringUTFChars(ref_profile_file, raw_ref_profile_file);
391
392 const char* raw_package_name = env->GetStringUTFChars(package_name, nullptr);
393 std::string package_name_str(raw_package_name);
394 env->ReleaseStringUTFChars(package_name, raw_package_name);
395
396 Runtime::Current()->RegisterAppInfo(
397 package_name_str,
398 code_paths_vec,
399 cur_profile_file_str,
400 ref_profile_file_str,
401 static_cast<int32_t>(code_path_type));
402 }
403
VMRuntime_isBootClassPathOnDisk(JNIEnv * env,jclass,jstring java_instruction_set)404 static jboolean VMRuntime_isBootClassPathOnDisk(JNIEnv* env, jclass, jstring java_instruction_set) {
405 ScopedUtfChars instruction_set(env, java_instruction_set);
406 if (instruction_set.c_str() == nullptr) {
407 return JNI_FALSE;
408 }
409 InstructionSet isa = GetInstructionSetFromString(instruction_set.c_str());
410 if (isa == InstructionSet::kNone) {
411 ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
412 std::string message(StringPrintf("Instruction set %s is invalid.", instruction_set.c_str()));
413 env->ThrowNew(iae.get(), message.c_str());
414 return JNI_FALSE;
415 }
416 return gc::space::ImageSpace::IsBootClassPathOnDisk(isa);
417 }
418
VMRuntime_getCurrentInstructionSet(JNIEnv * env,jclass)419 static jstring VMRuntime_getCurrentInstructionSet(JNIEnv* env, jclass) {
420 return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
421 }
422
VMRuntime_setSystemDaemonThreadPriority(JNIEnv * env,jclass klass)423 static void VMRuntime_setSystemDaemonThreadPriority([[maybe_unused]] JNIEnv* env,
424 [[maybe_unused]] jclass klass) {
425 #ifdef ART_TARGET_ANDROID
426 Thread* self = Thread::Current();
427 DCHECK(self != nullptr);
428 pid_t tid = self->GetTid();
429 // We use a priority lower than the default for the system daemon threads (eg HeapTaskDaemon) to
430 // avoid jank due to CPU contentions between GC and other UI-related threads. b/36631902.
431 // We may use a native priority that doesn't have a corresponding java.lang.Thread-level priority.
432 static constexpr int kSystemDaemonNiceValue = 4; // priority 124
433 if (setpriority(PRIO_PROCESS, tid, kSystemDaemonNiceValue) != 0) {
434 PLOG(INFO) << *self << " setpriority(PRIO_PROCESS, " << tid << ", "
435 << kSystemDaemonNiceValue << ") failed";
436 }
437 #endif
438 }
439
VMRuntime_setDedupeHiddenApiWarnings(JNIEnv * env,jclass klass,jboolean dedupe)440 static void VMRuntime_setDedupeHiddenApiWarnings([[maybe_unused]] JNIEnv* env,
441 [[maybe_unused]] jclass klass,
442 jboolean dedupe) {
443 Runtime::Current()->SetDedupeHiddenApiWarnings(dedupe);
444 }
445
VMRuntime_setProcessPackageName(JNIEnv * env,jclass klass,jstring java_package_name)446 static void VMRuntime_setProcessPackageName(JNIEnv* env,
447 [[maybe_unused]] jclass klass,
448 jstring java_package_name) {
449 ScopedUtfChars package_name(env, java_package_name);
450 Runtime::Current()->SetProcessPackageName(package_name.c_str());
451 }
452
VMRuntime_setProcessDataDirectory(JNIEnv * env,jclass,jstring java_data_dir)453 static void VMRuntime_setProcessDataDirectory(JNIEnv* env, jclass, jstring java_data_dir) {
454 ScopedUtfChars data_dir(env, java_data_dir);
455 Runtime::Current()->SetProcessDataDirectory(data_dir.c_str());
456 }
457
VMRuntime_bootCompleted(JNIEnv * env,jclass klass)458 static void VMRuntime_bootCompleted([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jclass klass) {
459 jit::Jit* jit = Runtime::Current()->GetJit();
460 if (jit != nullptr) {
461 jit->BootCompleted();
462 }
463 }
464
465 class ClearJitCountersVisitor : public ClassVisitor {
466 public:
operator ()(ObjPtr<mirror::Class> klass)467 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
468 // Avoid some types of classes that don't need their methods visited.
469 if (klass->IsProxyClass() ||
470 klass->IsArrayClass() ||
471 klass->IsPrimitive() ||
472 !klass->IsResolved() ||
473 klass->IsErroneousResolved()) {
474 return true;
475 }
476 uint16_t threshold = Runtime::Current()->GetJITOptions()->GetWarmupThreshold();
477 for (ArtMethod& m : klass->GetMethods(kRuntimePointerSize)) {
478 if (!m.IsAbstract()) {
479 m.ResetCounter(threshold);
480 }
481 }
482 return true;
483 }
484 };
485
VMRuntime_resetJitCounters(JNIEnv * env,jclass klass)486 static void VMRuntime_resetJitCounters(JNIEnv* env, [[maybe_unused]] jclass klass) {
487 ScopedObjectAccess soa(env);
488 ClearJitCountersVisitor visitor;
489 Runtime::Current()->GetClassLinker()->VisitClasses(&visitor);
490 }
491
VMRuntime_isValidClassLoaderContext(JNIEnv * env,jclass klass,jstring jencoded_class_loader_context)492 static jboolean VMRuntime_isValidClassLoaderContext(JNIEnv* env,
493 [[maybe_unused]] jclass klass,
494 jstring jencoded_class_loader_context) {
495 if (UNLIKELY(jencoded_class_loader_context == nullptr)) {
496 ScopedFastNativeObjectAccess soa(env);
497 ThrowNullPointerException("encoded_class_loader_context == null");
498 return false;
499 }
500 ScopedUtfChars encoded_class_loader_context(env, jencoded_class_loader_context);
501 return ClassLoaderContext::IsValidEncoding(encoded_class_loader_context.c_str());
502 }
503
VMRuntime_getBaseApkOptimizationInfo(JNIEnv * env,jclass klass)504 static jobject VMRuntime_getBaseApkOptimizationInfo(JNIEnv* env, [[maybe_unused]] jclass klass) {
505 AppInfo* app_info = Runtime::Current()->GetAppInfo();
506 DCHECK(app_info != nullptr);
507
508 std::string compiler_filter;
509 std::string compilation_reason;
510 app_info->GetPrimaryApkOptimizationStatus(&compiler_filter, &compilation_reason);
511
512 ScopedLocalRef<jclass> cls(env, env->FindClass("dalvik/system/DexFile$OptimizationInfo"));
513 if (cls == nullptr) {
514 DCHECK(env->ExceptionCheck());
515 return nullptr;
516 }
517
518 jmethodID ctor = env->GetMethodID(cls.get(), "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
519 if (ctor == nullptr) {
520 DCHECK(env->ExceptionCheck());
521 return nullptr;
522 }
523
524 ScopedLocalRef<jstring> j_compiler_filter(env, env->NewStringUTF(compiler_filter.c_str()));
525 if (j_compiler_filter == nullptr) {
526 DCHECK(env->ExceptionCheck());
527 return nullptr;
528 }
529
530 ScopedLocalRef<jstring> j_compilation_reason(env, env->NewStringUTF(compilation_reason.c_str()));
531 if (j_compilation_reason == nullptr) {
532 DCHECK(env->ExceptionCheck());
533 return nullptr;
534 }
535
536 return env->NewObject(cls.get(), ctor, j_compiler_filter.get(), j_compilation_reason.get());
537 }
538
539 static JNINativeMethod gMethods[] = {
540 FAST_NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
541 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
542 NATIVE_METHOD(VMRuntime, clampGrowthLimit, "()V"),
543 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
544 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
545 NATIVE_METHOD(VMRuntime, setHiddenApiExemptions, "([Ljava/lang/String;)V"),
546 NATIVE_METHOD(VMRuntime, setHiddenApiAccessLogSamplingRate, "(I)V"),
547 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
548 FAST_NATIVE_METHOD(VMRuntime, isNativeDebuggable, "()Z"),
549 NATIVE_METHOD(VMRuntime, isJavaDebuggable, "()Z"),
550 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
551 FAST_NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
552 FAST_NATIVE_METHOD(VMRuntime, newUnpaddedArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
553 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
554 NATIVE_METHOD(VMRuntime, getSdkVersionNative, "(I)I"),
555 NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
556 NATIVE_METHOD(VMRuntime, setDisabledCompatChangesNative, "([J)V"),
557 NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(J)V"),
558 NATIVE_METHOD(VMRuntime, registerNativeFree, "(J)V"),
559 NATIVE_METHOD(VMRuntime, getNotifyNativeInterval, "()I"),
560 NATIVE_METHOD(VMRuntime, getFinalizerTimeoutMs, "()J"),
561 NATIVE_METHOD(VMRuntime, notifyNativeAllocationsInternal, "()V"),
562 NATIVE_METHOD(VMRuntime, notifyStartupCompleted, "()V"),
563 NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
564 NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
565 NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
566 NATIVE_METHOD(VMRuntime, runHeapTasks, "()V"),
567 NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
568 NATIVE_METHOD(VMRuntime, startHeapTaskProcessor, "()V"),
569 NATIVE_METHOD(VMRuntime, stopHeapTaskProcessor, "()V"),
570 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
571 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
572 NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
573 NATIVE_METHOD(VMRuntime, vmInstructionSet, "()Ljava/lang/String;"),
574 FAST_NATIVE_METHOD(VMRuntime, is64Bit, "()Z"),
575 FAST_NATIVE_METHOD(VMRuntime, isCheckJniEnabled, "()Z"),
576 NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"),
577 NATIVE_METHOD(VMRuntime, registerAppInfo,
578 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;I)V"),
579 NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
580 NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
581 NATIVE_METHOD(VMRuntime, setSystemDaemonThreadPriority, "()V"),
582 NATIVE_METHOD(VMRuntime, setDedupeHiddenApiWarnings, "(Z)V"),
583 NATIVE_METHOD(VMRuntime, setProcessPackageName, "(Ljava/lang/String;)V"),
584 NATIVE_METHOD(VMRuntime, setProcessDataDirectory, "(Ljava/lang/String;)V"),
585 NATIVE_METHOD(VMRuntime, bootCompleted, "()V"),
586 NATIVE_METHOD(VMRuntime, resetJitCounters, "()V"),
587 NATIVE_METHOD(VMRuntime, isValidClassLoaderContext, "(Ljava/lang/String;)Z"),
588 NATIVE_METHOD(VMRuntime, getBaseApkOptimizationInfo,
589 "()Ldalvik/system/DexFile$OptimizationInfo;"),
590 };
591
register_dalvik_system_VMRuntime(JNIEnv * env)592 void register_dalvik_system_VMRuntime(JNIEnv* env) {
593 if (Runtime::Current()->GetTargetSdkVersion() <= static_cast<uint32_t>(SdkVersion::kU)) {
594 real_register_dalvik_system_VMRuntime(env);
595 } else {
596 Runtime::Current()->Abort(
597 "Call to internal function 'register_dalvik_system_VMRuntime' is not allowed");
598 }
599 }
600
real_register_dalvik_system_VMRuntime(JNIEnv * env)601 void real_register_dalvik_system_VMRuntime(JNIEnv* env) {
602 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
603 }
604
605 } // namespace art
606