blob: 842590408d9290aaef6c3cbbb2541889c2053a89 [file] [log] [blame]
Elliott Hughes7ede61e2011-09-14 18:18:06 -07001/*
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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_VMRuntime.h"
18
Bilyan Borisovbb661c02016-04-04 16:27:32 +010019#ifdef ART_TARGET_ANDROID
Hiroshi Yamauchid7f03392017-03-29 14:47:49 -070020#include <sys/resource.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <sys/time.h>
Dmitriy Ivanov15efde22015-05-22 18:03:34 -070022extern "C" void android_set_application_target_sdk_version(uint32_t version);
23#endif
Hans Boehmf57f7d42019-03-14 17:49:26 -070024#include <inttypes.h>
25#include <limits>
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include <limits.h>
Andreas Gampe373a9b52017-10-18 09:01:57 -070027#include "nativehelper/scoped_utf_chars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070028
Vladimir Marko91f10322018-12-07 18:04:10 +000029#include <android-base/stringprintf.h>
30#include <android-base/strings.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080031
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000035#include "base/sdk_version.h"
Ian Rogers98379392014-02-24 16:53:16 -080036#include "class_linker-inl.h"
Dan Zimmermanc9fa7702020-01-31 13:35:12 -080037#include "class_loader_context.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080038#include "common_throws.h"
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -070039#include "debugger.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070040#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_types.h"
Brian Carlstrome8104522013-10-15 21:56:36 -070043#include "gc/accounting/card_table-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070044#include "gc/allocator/dlmalloc.h"
Mathieu Chartier82353312013-07-18 10:47:51 -070045#include "gc/heap.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/space/dlmalloc_space.h"
Brian Carlstrom31d8f522014-09-29 11:22:54 -070047#include "gc/space/image_space.h"
Mathieu Chartiera5eae692014-12-17 17:56:03 -080048#include "gc/task_processor.h"
Brian Carlstrome8104522013-10-15 21:56:36 -070049#include "intern_table.h"
David Srbeckybfcea3d2019-08-05 15:44:00 +010050#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010051#include "jni/java_vm_ext.h"
52#include "jni/jni_internal.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070053#include "mirror/array-alloc-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070054#include "mirror/class-inl.h"
Brian Carlstrome8104522013-10-15 21:56:36 -070055#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "mirror/object-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070057#include "native_util.h"
Steven Morelande431e272017-07-18 16:53:49 -070058#include "nativehelper/jni_macros.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070059#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier893263b2014-03-04 11:07:42 -080060#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070061#include "scoped_fast_native_object_access-inl.h"
62#include "scoped_thread_state_change-inl.h"
Elliott Hughes7ede61e2011-09-14 18:18:06 -070063#include "thread.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070064#include "thread_list.h"
Andreas Gamped0332e02017-09-22 19:23:38 -070065#include "well_known_classes.h"
Andreas Gampe277ccbd2014-11-03 21:36:10 -080066
Elliott Hughes7ede61e2011-09-14 18:18:06 -070067namespace art {
68
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Elliott Hughes0512f022012-03-15 22:10:52 -070071static jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080072 return Runtime::Current()->GetHeap()->GetTargetHeapUtilization();
Elliott Hughes7ede61e2011-09-14 18:18:06 -070073}
74
Elliott Hughes0512f022012-03-15 22:10:52 -070075static void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076 Runtime::Current()->GetHeap()->SetTargetHeapUtilization(target);
Elliott Hughes7ede61e2011-09-14 18:18:06 -070077}
78
Mathew Inwood3383aa52018-03-16 14:18:33 +000079static 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
Mathew Inwood73ddda42018-04-03 15:32:32 +010094static void VMRuntime_setHiddenApiAccessLogSamplingRate(JNIEnv*, jclass, jint rate) {
95 Runtime::Current()->SetHiddenApiEventLogSampleRate(rate);
96}
97
Mathieu Chartier590fee92013-09-13 13:46:47 -070098static jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass,
Brian Carlstrome8104522013-10-15 21:56:36 -070099 jint length) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700100 ScopedFastNativeObjectAccess soa(env);
Ian Rogersf9c612f2014-02-27 10:58:17 -0800101 if (UNLIKELY(length < 0)) {
102 ThrowNegativeArraySizeException(length);
103 return nullptr;
104 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700105 ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
Ian Rogers98379392014-02-24 16:53:16 -0800106 if (UNLIKELY(element_class == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000107 ThrowNullPointerException("element class == null");
Ian Rogers98379392014-02-24 16:53:16 -0800108 return nullptr;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700109 }
Ian Rogers98379392014-02-24 16:53:16 -0800110 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700111 ObjPtr<mirror::Class> array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100112 runtime->GetClassLinker()->FindArrayClass(soa.Self(), element_class);
Ian Rogers98379392014-02-24 16:53:16 -0800113 if (UNLIKELY(array_class == nullptr)) {
114 return nullptr;
115 }
116 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentNonMovingAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100117 ObjPtr<mirror::Array> result = mirror::Array::Alloc(soa.Self(),
118 array_class,
119 length,
120 array_class->GetComponentSizeShift(),
121 allocator);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 return soa.AddLocalReference<jobject>(result);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700123}
124
Ian Rogersf9c612f2014-02-27 10:58:17 -0800125static 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 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700132 ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(javaElementClass);
Ian Rogersf9c612f2014-02-27 10:58:17 -0800133 if (UNLIKELY(element_class == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000134 ThrowNullPointerException("element class == null");
Ian Rogersf9c612f2014-02-27 10:58:17 -0800135 return nullptr;
136 }
Ian Rogersf9c612f2014-02-27 10:58:17 -0800137 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700138 ObjPtr<mirror::Class> array_class = runtime->GetClassLinker()->FindArrayClass(soa.Self(),
Vladimir Markobcf17522018-06-01 13:14:32 +0100139 element_class);
Ian Rogersf9c612f2014-02-27 10:58:17 -0800140 if (UNLIKELY(array_class == nullptr)) {
141 return nullptr;
142 }
143 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100144 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);
Ian Rogersf9c612f2014-02-27 10:58:17 -0800151 return soa.AddLocalReference<jobject>(result);
152}
153
Elliott Hughes0512f022012-03-15 22:10:52 -0700154static jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 if (javaArray == nullptr) { // Most likely allocation failed
Ian Rogersa15e67d2012-02-28 13:51:55 -0800156 return 0;
157 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700158 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700159 ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(javaArray);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700160 if (!array->IsArrayInstance()) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000161 ThrowIllegalArgumentException("not an array");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700162 return 0;
163 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700164 if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700165 ThrowRuntimeException("Trying to get address of movable array object");
166 return 0;
167 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 return reinterpret_cast<uintptr_t>(array->GetRawData(array->GetClass()->GetComponentSize(), 0));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700169}
170
Elliott Hughes0512f022012-03-15 22:10:52 -0700171static void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800172 Runtime::Current()->GetHeap()->ClearGrowthLimit();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700173}
174
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800175static void VMRuntime_clampGrowthLimit(JNIEnv*, jobject) {
176 Runtime::Current()->GetHeap()->ClampGrowthLimit();
177}
178
Oleksiy Vyalov278a80b2016-03-30 20:27:00 -0700179static jboolean VMRuntime_isNativeDebuggable(JNIEnv*, jobject) {
180 return Runtime::Current()->IsNativeDebuggable();
181}
182
Mathew Inwoodeef77572018-05-18 13:32:09 +0100183static jboolean VMRuntime_isJavaDebuggable(JNIEnv*, jobject) {
184 return Runtime::Current()->IsJavaDebuggable();
185}
186
Elliott Hughes0512f022012-03-15 22:10:52 -0700187static jobjectArray VMRuntime_properties(JNIEnv* env, jobject) {
Andreas Gamped0332e02017-09-22 19:23:38 -0700188 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();
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700209}
210
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800211// 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.
Elliott Hughes0512f022012-03-15 22:10:52 -0700216static const char* DefaultToDot(const std::string& class_path) {
Brian Carlstrom7d5ffb52012-02-01 14:27:54 -0800217 return class_path.empty() ? "." : class_path.c_str();
218}
219
Elliott Hughes0512f022012-03-15 22:10:52 -0700220static jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) {
Vladimir Marko91f10322018-12-07 18:04:10 +0000221 std::string boot_class_path = android::base::Join(Runtime::Current()->GetBootClassPath(), ':');
222 return env->NewStringUTF(DefaultToDot(boot_class_path));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700223}
224
Elliott Hughes0512f022012-03-15 22:10:52 -0700225static jstring VMRuntime_classPath(JNIEnv* env, jobject) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800226 return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPathString()));
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700227}
228
Elliott Hughes0512f022012-03-15 22:10:52 -0700229static jstring VMRuntime_vmVersion(JNIEnv* env, jobject) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800230 return env->NewStringUTF(Runtime::GetVersion());
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700231}
232
Brian Carlstromcef450c2013-06-28 14:38:26 -0700233static jstring VMRuntime_vmLibrary(JNIEnv* env, jobject) {
234 return env->NewStringUTF(kIsDebugBuild ? "libartd.so" : "libart.so");
235}
236
Sebastien Hertza7053762014-05-16 16:56:45 +0200237static 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
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700243static jboolean VMRuntime_is64Bit(JNIEnv*, jobject) {
Sebastien Hertza7053762014-05-16 16:56:45 +0200244 bool is64BitMode = (sizeof(void*) == sizeof(uint64_t));
245 return is64BitMode ? JNI_TRUE : JNI_FALSE;
246}
247
248static jboolean VMRuntime_isCheckJniEnabled(JNIEnv* env, jobject) {
Ian Rogers55256cb2017-12-21 17:07:11 -0800249 return down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled() ? JNI_TRUE : JNI_FALSE;
Sebastien Hertza7053762014-05-16 16:56:45 +0200250}
251
Jeff Haof00571c2014-05-29 17:29:47 -0700252static void VMRuntime_setTargetSdkVersionNative(JNIEnv*, jobject, jint target_sdk_version) {
Ian Rogers987560f2014-04-22 11:42:59 -0700253 // 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.
Elliott Hughesc2dc62d2012-01-17 20:06:12 -0800255 // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
256 // Note that targetSdkVersion may be 0, meaning "current".
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000257 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);
Dmitriy Ivanov15efde22015-05-22 18:03:34 -0700261
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100262#ifdef ART_TARGET_ANDROID
Dmitriy Ivanov15efde22015-05-22 18:03:34 -0700263 // This part is letting libc/dynamic linker know about current app's
264 // target sdk version to enable compatibility workarounds.
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000265 android_set_application_target_sdk_version(uint_target_sdk_version);
Dmitriy Ivanov15efde22015-05-22 18:03:34 -0700266#endif
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700267}
268
atrostfab72082019-12-06 13:37:36 +0000269static void VMRuntime_setDisabledCompatChangesNative(JNIEnv* env, jobject,
270 jlongArray disabled_compat_changes) {
271 if (disabled_compat_changes == nullptr) {
272 return;
273 }
atrosta9ff45b2020-01-14 15:59:02 +0000274 std::set<uint64_t> disabled_compat_changes_set;
atrostfab72082019-12-06 13:37:36 +0000275 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++) {
atrosta9ff45b2020-01-14 15:59:02 +0000278 disabled_compat_changes_set.insert(static_cast<uint64_t>(elements[i]));
atrostfab72082019-12-06 13:37:36 +0000279 }
Andrei Onea037d2822020-11-19 00:20:04 +0000280 Runtime::Current()->GetCompatFramework().SetDisabledCompatChanges(disabled_compat_changes_set);
atrostfab72082019-12-06 13:37:36 +0000281}
282
Hans Boehmf57f7d42019-03-14 17:49:26 -0700283static 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;
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700289 }
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700290}
291
Hans Boehmf57f7d42019-03-14 17:49:26 -0700292static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jlong bytes) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700293 if (UNLIKELY(bytes < 0)) {
294 ScopedObjectAccess soa(env);
Hans Boehmf57f7d42019-03-14 17:49:26 -0700295 ThrowRuntimeException("allocation size negative %" PRId64, bytes);
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700296 return;
297 }
Hans Boehmf57f7d42019-03-14 17:49:26 -0700298 Runtime::Current()->GetHeap()->RegisterNativeAllocation(env, clamp_to_size_t(bytes));
299}
300
301static 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));
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700308}
309
Hans Boehmc220f982018-10-12 16:15:45 -0700310static jint VMRuntime_getNotifyNativeInterval(JNIEnv*, jclass) {
311 return Runtime::Current()->GetHeap()->GetNotifyNativeInterval();
312}
313
314static void VMRuntime_notifyNativeAllocationsInternal(JNIEnv* env, jobject) {
315 Runtime::Current()->GetHeap()->NotifyNativeAllocations(env);
316}
317
Hans Boehmb2155572019-03-27 14:25:53 -0700318static jlong VMRuntime_getFinalizerTimeoutMs(JNIEnv*, jobject) {
319 return Runtime::Current()->GetFinalizerTimeoutMs();
320}
321
Hans Boehmc220f982018-10-12 16:15:45 -0700322static void VMRuntime_registerSensitiveThread(JNIEnv*, jobject) {
323 Runtime::Current()->RegisterSensitiveThread();
324}
325
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700326static void VMRuntime_updateProcessState(JNIEnv*, jobject, jint process_state) {
327 Runtime* runtime = Runtime::Current();
Mathieu Chartierf8cb1782016-03-18 18:45:41 -0700328 runtime->UpdateProcessState(static_cast<ProcessState>(process_state));
Mathieu Chartierca2a24d2013-11-25 15:12:12 -0800329}
330
Mathieu Chartier175ce3d2019-03-06 16:54:24 -0800331static void VMRuntime_notifyStartupCompleted(JNIEnv*, jobject) {
332 Runtime::Current()->NotifyStartupCompleted();
333}
334
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800335static void VMRuntime_trimHeap(JNIEnv* env, jobject) {
336 Runtime::Current()->GetHeap()->Trim(ThreadForEnv(env));
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800337}
338
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800339static void VMRuntime_requestHeapTrim(JNIEnv* env, jobject) {
340 Runtime::Current()->GetHeap()->RequestTrim(ThreadForEnv(env));
Mathieu Chartier079101a2014-12-15 14:23:10 -0800341}
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800342
343static void VMRuntime_requestConcurrentGC(JNIEnv* env, jobject) {
Hans Boehm5c4d0df2021-04-29 16:16:39 +0000344 gc::Heap *heap = Runtime::Current()->GetHeap();
345 heap->RequestConcurrentGC(ThreadForEnv(env),
346 gc::kGcCauseBackground,
347 true,
348 heap->GetCurrentGcNum());
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800349}
350
351static void VMRuntime_startHeapTaskProcessor(JNIEnv* env, jobject) {
352 Runtime::Current()->GetHeap()->GetTaskProcessor()->Start(ThreadForEnv(env));
353}
354
355static void VMRuntime_stopHeapTaskProcessor(JNIEnv* env, jobject) {
356 Runtime::Current()->GetHeap()->GetTaskProcessor()->Stop(ThreadForEnv(env));
357}
358
359static void VMRuntime_runHeapTasks(JNIEnv* env, jobject) {
360 Runtime::Current()->GetHeap()->GetTaskProcessor()->RunAllTasks(ThreadForEnv(env));
Mathieu Chartier079101a2014-12-15 14:23:10 -0800361}
362
David Srbeckyd259ffb2020-12-08 16:59:18 +0000363static void VMRuntime_preloadDexCaches(JNIEnv* env ATTRIBUTE_UNUSED, jobject) {
Brian Carlstrome8104522013-10-15 21:56:36 -0700364}
365
Dave Allison0aded082013-11-07 13:15:11 -0800366/*
Calin Juravle3ddf4d42021-05-11 10:19:28 -0700367 * 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.
Dave Allison0aded082013-11-07 13:15:11 -0800370 */
Calin Juravle31f2c152015-10-23 17:56:15 +0100371static void VMRuntime_registerAppInfo(JNIEnv* env,
372 jclass clazz ATTRIBUTE_UNUSED,
Calin Juravlec2753e62021-06-25 15:34:09 -0700373 jstring package_name,
Calin Juravle3ddf4d42021-05-11 10:19:28 -0700374 jstring cur_profile_file,
Calin Juravlebeb9f202021-06-02 14:42:03 -0700375 jstring ref_profile_file,
Calin Juravle3ddf4d42021-05-11 10:19:28 -0700376 jobjectArray code_paths,
Calin Juravlec2753e62021-06-25 15:34:09 -0700377 jint code_path_type) {
Calin Juravle66f55232015-12-08 15:09:10 +0000378 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 }
Calin Juravlec1b643c2014-05-30 23:44:11 +0100386
Calin Juravle3ddf4d42021-05-11 10:19:28 -0700387 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);
Calin Juravlec1b643c2014-05-30 23:44:11 +0100390
Calin Juravlebeb9f202021-06-02 14:42:03 -0700391 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
Calin Juravlec2753e62021-06-25 15:34:09 -0700395 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));
Dave Allison0aded082013-11-07 13:15:11 -0800405}
406
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700407static 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());
Vladimir Marko33bff252017-11-01 14:35:42 +0000413 if (isa == InstructionSet::kNone) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700414 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 }
Vladimir Markof9dfd772020-01-30 13:56:13 +0000419 return gc::space::ImageSpace::IsBootClassPathOnDisk(isa);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700420}
421
422static jstring VMRuntime_getCurrentInstructionSet(JNIEnv* env, jclass) {
423 return env->NewStringUTF(GetInstructionSetString(kRuntimeISA));
424}
425
Hiroshi Yamauchid7f03392017-03-29 14:47:49 -0700426static 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
Mathew Inwood80900a32018-04-19 14:40:40 +0100443static void VMRuntime_setDedupeHiddenApiWarnings(JNIEnv* env ATTRIBUTE_UNUSED,
444 jclass klass ATTRIBUTE_UNUSED,
445 jboolean dedupe) {
446 Runtime::Current()->SetDedupeHiddenApiWarnings(dedupe);
447}
448
Mathew Inwood5bcef172018-05-01 14:40:12 +0100449static 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
David Brazdilc2f43572019-04-03 12:56:14 +0100456static 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
David Srbeckybfcea3d2019-08-05 15:44:00 +0100461static 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
Mathieu Chartier0aa02062019-11-21 16:24:09 -0800469class ClearJitCountersVisitor : public ClassVisitor {
470 public:
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
491static void VMRuntime_resetJitCounters(JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
492 ScopedObjectAccess soa(env);
493 ClearJitCountersVisitor visitor;
494 Runtime::Current()->GetClassLinker()->VisitClasses(&visitor);
495}
496
Dan Zimmermanc9fa7702020-01-31 13:35:12 -0800497static 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
Elliott Hughes0512f022012-03-15 22:10:52 -0700509static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800510 FAST_NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700511 NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"),
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800512 NATIVE_METHOD(VMRuntime, clampGrowthLimit, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700513 NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"),
514 NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"),
Mathew Inwood3383aa52018-03-16 14:18:33 +0000515 NATIVE_METHOD(VMRuntime, setHiddenApiExemptions, "([Ljava/lang/String;)V"),
Mathew Inwood73ddda42018-04-03 15:32:32 +0100516 NATIVE_METHOD(VMRuntime, setHiddenApiAccessLogSamplingRate, "(I)V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700517 NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800518 FAST_NATIVE_METHOD(VMRuntime, isNativeDebuggable, "()Z"),
Mathew Inwoodeef77572018-05-18 13:32:09 +0100519 NATIVE_METHOD(VMRuntime, isJavaDebuggable, "()Z"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700520 NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800521 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;"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700523 NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
Alex Klyubin881392f2013-10-24 16:31:56 -0700524 NATIVE_METHOD(VMRuntime, setTargetSdkVersionNative, "(I)V"),
atrostfab72082019-12-06 13:37:36 +0000525 NATIVE_METHOD(VMRuntime, setDisabledCompatChangesNative, "([J)V"),
Hans Boehmf57f7d42019-03-14 17:49:26 -0700526 NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(J)V"),
527 NATIVE_METHOD(VMRuntime, registerNativeFree, "(J)V"),
Hans Boehmc220f982018-10-12 16:15:45 -0700528 NATIVE_METHOD(VMRuntime, getNotifyNativeInterval, "()I"),
Hans Boehmb2155572019-03-27 14:25:53 -0700529 NATIVE_METHOD(VMRuntime, getFinalizerTimeoutMs, "()J"),
Hans Boehmc220f982018-10-12 16:15:45 -0700530 NATIVE_METHOD(VMRuntime, notifyNativeAllocationsInternal, "()V"),
Mathieu Chartier175ce3d2019-03-06 16:54:24 -0800531 NATIVE_METHOD(VMRuntime, notifyStartupCompleted, "()V"),
Calin Juravle97cbc922016-04-15 16:16:35 +0100532 NATIVE_METHOD(VMRuntime, registerSensitiveThread, "()V"),
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800533 NATIVE_METHOD(VMRuntime, requestConcurrentGC, "()V"),
534 NATIVE_METHOD(VMRuntime, requestHeapTrim, "()V"),
535 NATIVE_METHOD(VMRuntime, runHeapTasks, "()V"),
Mathieu Chartierca2a24d2013-11-25 15:12:12 -0800536 NATIVE_METHOD(VMRuntime, updateProcessState, "(I)V"),
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800537 NATIVE_METHOD(VMRuntime, startHeapTaskProcessor, "()V"),
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800538 NATIVE_METHOD(VMRuntime, stopHeapTaskProcessor, "()V"),
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800539 NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700540 NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
Brian Carlstromcef450c2013-06-28 14:38:26 -0700541 NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
Sebastien Hertza7053762014-05-16 16:56:45 +0200542 NATIVE_METHOD(VMRuntime, vmInstructionSet, "()Ljava/lang/String;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800543 FAST_NATIVE_METHOD(VMRuntime, is64Bit, "()Z"),
544 FAST_NATIVE_METHOD(VMRuntime, isCheckJniEnabled, "()Z"),
Brian Carlstrome8104522013-10-15 21:56:36 -0700545 NATIVE_METHOD(VMRuntime, preloadDexCaches, "()V"),
Calin Juravle3ddf4d42021-05-11 10:19:28 -0700546 NATIVE_METHOD(VMRuntime, registerAppInfo,
547 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;I)V"),
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700548 NATIVE_METHOD(VMRuntime, isBootClassPathOnDisk, "(Ljava/lang/String;)Z"),
549 NATIVE_METHOD(VMRuntime, getCurrentInstructionSet, "()Ljava/lang/String;"),
Hiroshi Yamauchid7f03392017-03-29 14:47:49 -0700550 NATIVE_METHOD(VMRuntime, setSystemDaemonThreadPriority, "()V"),
Mathew Inwood80900a32018-04-19 14:40:40 +0100551 NATIVE_METHOD(VMRuntime, setDedupeHiddenApiWarnings, "(Z)V"),
Mathew Inwood5bcef172018-05-01 14:40:12 +0100552 NATIVE_METHOD(VMRuntime, setProcessPackageName, "(Ljava/lang/String;)V"),
David Brazdilc2f43572019-04-03 12:56:14 +0100553 NATIVE_METHOD(VMRuntime, setProcessDataDirectory, "(Ljava/lang/String;)V"),
David Srbeckybfcea3d2019-08-05 15:44:00 +0100554 NATIVE_METHOD(VMRuntime, bootCompleted, "()V"),
Mathieu Chartier0aa02062019-11-21 16:24:09 -0800555 NATIVE_METHOD(VMRuntime, resetJitCounters, "()V"),
Dan Zimmermanc9fa7702020-01-31 13:35:12 -0800556 NATIVE_METHOD(VMRuntime, isValidClassLoaderContext, "(Ljava/lang/String;)Z"),
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700557};
558
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700559void register_dalvik_system_VMRuntime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700560 REGISTER_NATIVE_METHODS("dalvik/system/VMRuntime");
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700561}
562
563} // namespace art