1 /*
2 * Copyright (C) 2006 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 #define ATRACE_TAG ATRACE_TAG_DALVIK
18 #define LOG_TAG "AndroidRuntime"
19 #define LOG_NDEBUG 1
20
21 #include <android-base/macros.h>
22 #include <android-base/properties.h>
23 #include <android/graphics/jni_runtime.h>
24 #include <android_runtime/AndroidRuntime.h>
25 #include <android_runtime/vm.h>
26 #include <assert.h>
27 #include <binder/IBinder.h>
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/Parcel.h>
31 #include <bionic/malloc.h>
32 #include <cutils/properties.h>
33 #include <dirent.h>
34 #include <dlfcn.h>
35 #include <nativehelper/JNIHelp.h>
36 #include <nativehelper/JniInvocation.h>
37 #include <server_configurable_flags/get_flags.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <utils/Log.h>
43 #include <utils/Trace.h>
44 #include <utils/misc.h>
45 #include <utils/threads.h>
46
47 #include <string>
48 #include <vector>
49
50 #include "android_util_Binder.h"
51 #include "jni.h"
52
53 using namespace android;
54 using android::base::GetBoolProperty;
55 using android::base::GetProperty;
56
57 extern int register_android_os_Binder(JNIEnv* env);
58 extern int register_android_os_Process(JNIEnv* env);
59 extern int register_android_graphics_GraphicBuffer(JNIEnv* env);
60
61 extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
62 extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
63 extern int register_android_opengl_jni_EGL14(JNIEnv* env);
64 extern int register_android_opengl_jni_EGL15(JNIEnv* env);
65 extern int register_android_opengl_jni_EGLExt(JNIEnv* env);
66 extern int register_android_opengl_jni_GLES10(JNIEnv* env);
67 extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
68 extern int register_android_opengl_jni_GLES11(JNIEnv* env);
69 extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
70 extern int register_android_opengl_jni_GLES20(JNIEnv* env);
71 extern int register_android_opengl_jni_GLES30(JNIEnv* env);
72 extern int register_android_opengl_jni_GLES31(JNIEnv* env);
73 extern int register_android_opengl_jni_GLES31Ext(JNIEnv* env);
74 extern int register_android_opengl_jni_GLES32(JNIEnv* env);
75
76 extern int register_android_hardware_Camera(JNIEnv *env);
77 extern int register_android_hardware_camera2_CameraMetadata(JNIEnv *env);
78 extern int register_android_hardware_camera2_DngCreator(JNIEnv *env);
79 extern int register_android_hardware_camera2_impl_CameraExtensionJpegProcessor(JNIEnv* env);
80 extern int register_android_hardware_camera2_utils_SurfaceUtils(JNIEnv* env);
81 extern int register_android_hardware_display_DisplayManagerGlobal(JNIEnv* env);
82 extern int register_android_hardware_HardwareBuffer(JNIEnv *env);
83 extern int register_android_hardware_SensorManager(JNIEnv *env);
84 extern int register_android_hardware_SerialPort(JNIEnv *env);
85 extern int register_android_hardware_SyncFence(JNIEnv* env);
86 extern int register_android_hardware_UsbDevice(JNIEnv *env);
87 extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
88 extern int register_android_hardware_UsbRequest(JNIEnv *env);
89 extern int register_android_hardware_location_ActivityRecognitionHardware(JNIEnv* env);
90
91 extern int register_android_media_AudioDeviceAttributes(JNIEnv* env);
92 extern int register_android_media_AudioEffectDescriptor(JNIEnv *env);
93 extern int register_android_media_AudioRecord(JNIEnv *env);
94 extern int register_android_media_AudioSystem(JNIEnv *env);
95 extern int register_android_media_AudioTrack(JNIEnv *env);
96 extern int register_android_media_AudioAttributes(JNIEnv *env);
97 extern int register_android_media_AudioProductStrategies(JNIEnv *env);
98 extern int register_android_media_AudioVolumeGroups(JNIEnv *env);
99 extern int register_android_media_AudioVolumeGroupChangeHandler(JNIEnv *env);
100 extern int register_android_media_MicrophoneInfo(JNIEnv *env);
101 extern int register_android_media_ToneGenerator(JNIEnv *env);
102 extern int register_android_media_audio_common_AidlConversion(JNIEnv* env);
103 extern int register_android_media_midi(JNIEnv *env);
104
105 namespace android {
106
107 /*
108 * JNI-based registration functions. Note these are properly contained in
109 * namespace android.
110 */
111 extern int register_android_app_admin_SecurityLog(JNIEnv* env);
112 extern int register_android_content_AssetManager(JNIEnv* env);
113 extern int register_android_util_CharsetUtils(JNIEnv* env);
114 extern int register_android_util_EventLog(JNIEnv* env);
115 extern int register_android_util_Log(JNIEnv* env);
116 extern int register_android_util_MemoryIntArray(JNIEnv* env);
117 extern int register_android_content_StringBlock(JNIEnv* env);
118 extern int register_android_content_XmlBlock(JNIEnv* env);
119 extern int register_android_content_res_ApkAssets(JNIEnv* env);
120 extern int register_android_graphics_BLASTBufferQueue(JNIEnv* env);
121 extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
122 extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
123 extern int register_android_view_InputApplicationHandle(JNIEnv* env);
124 extern int register_android_view_InputWindowHandle(JNIEnv* env);
125 extern int register_android_view_Surface(JNIEnv* env);
126 extern int register_android_view_SurfaceControl(JNIEnv* env);
127 extern int register_android_view_SurfaceControlHdrLayerInfoListener(JNIEnv* env);
128 extern int register_android_view_SurfaceSession(JNIEnv* env);
129 extern int register_android_view_CompositionSamplingListener(JNIEnv* env);
130 extern int register_android_view_TextureView(JNIEnv* env);
131 extern int register_android_view_TunnelModeEnabledListener(JNIEnv* env);
132 extern int register_android_database_CursorWindow(JNIEnv* env);
133 extern int register_android_database_SQLiteConnection(JNIEnv* env);
134 extern int register_android_database_SQLiteGlobal(JNIEnv* env);
135 extern int register_android_database_SQLiteDebug(JNIEnv* env);
136 extern int register_android_media_MediaMetrics(JNIEnv *env);
137 extern int register_android_os_Debug(JNIEnv* env);
138 extern int register_android_os_GraphicsEnvironment(JNIEnv* env);
139 extern int register_android_os_HidlSupport(JNIEnv* env);
140 extern int register_android_os_HwBinder(JNIEnv *env);
141 extern int register_android_os_HwBlob(JNIEnv *env);
142 extern int register_android_os_HwParcel(JNIEnv *env);
143 extern int register_android_os_HwRemoteBinder(JNIEnv *env);
144 extern int register_android_os_NativeHandle(JNIEnv *env);
145 extern int register_android_os_ServiceManager(JNIEnv *env);
146 extern int register_android_os_MessageQueue(JNIEnv* env);
147 extern int register_android_os_Parcel(JNIEnv* env);
148 extern int register_android_os_PerformanceHintManager(JNIEnv* env);
149 extern int register_android_os_SELinux(JNIEnv* env);
150 extern int register_android_os_VintfObject(JNIEnv *env);
151 extern int register_android_os_VintfRuntimeInfo(JNIEnv *env);
152 extern int register_android_os_storage_StorageManager(JNIEnv* env);
153 extern int register_android_os_SystemProperties(JNIEnv *env);
154 extern int register_android_os_SystemClock(JNIEnv* env);
155 extern int register_android_os_Trace(JNIEnv* env);
156 extern int register_android_os_FileObserver(JNIEnv *env);
157 extern int register_android_os_UEventObserver(JNIEnv* env);
158 extern int register_android_os_HidlMemory(JNIEnv* env);
159 extern int register_android_os_MemoryFile(JNIEnv* env);
160 extern int register_android_os_SharedMemory(JNIEnv* env);
161 extern int register_android_service_DataLoaderService(JNIEnv* env);
162 extern int register_android_os_incremental_IncrementalManager(JNIEnv* env);
163 extern int register_android_net_LocalSocketImpl(JNIEnv* env);
164 extern int register_android_text_AndroidCharacter(JNIEnv *env);
165 extern int register_android_text_Hyphenator(JNIEnv *env);
166 extern int register_android_opengl_classes(JNIEnv *env);
167 extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
168 extern int register_android_backup_BackupDataInput(JNIEnv *env);
169 extern int register_android_backup_BackupDataOutput(JNIEnv *env);
170 extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
171 extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
172 extern int register_android_app_backup_FullBackup(JNIEnv *env);
173 extern int register_android_app_Activity(JNIEnv *env);
174 extern int register_android_app_ActivityThread(JNIEnv *env);
175 extern int register_android_app_NativeActivity(JNIEnv *env);
176 extern int register_android_media_RemoteDisplay(JNIEnv *env);
177 extern int register_android_util_jar_StrictJarFile(JNIEnv* env);
178 extern int register_android_view_InputChannel(JNIEnv* env);
179 extern int register_android_view_InputDevice(JNIEnv* env);
180 extern int register_android_view_InputEventReceiver(JNIEnv* env);
181 extern int register_android_view_InputEventSender(JNIEnv* env);
182 extern int register_android_view_InputQueue(JNIEnv* env);
183 extern int register_android_view_KeyCharacterMap(JNIEnv *env);
184 extern int register_android_view_KeyEvent(JNIEnv* env);
185 extern int register_android_view_MotionEvent(JNIEnv* env);
186 extern int register_android_view_PointerIcon(JNIEnv* env);
187 extern int register_android_view_VelocityTracker(JNIEnv* env);
188 extern int register_android_view_VerifiedKeyEvent(JNIEnv* env);
189 extern int register_android_view_VerifiedMotionEvent(JNIEnv* env);
190 extern int register_android_content_res_ObbScanner(JNIEnv* env);
191 extern int register_android_content_res_Configuration(JNIEnv* env);
192 extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
193 extern int register_android_security_Scrypt(JNIEnv *env);
194 extern int register_com_android_internal_content_F2fsUtils(JNIEnv* env);
195 extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
196 extern int register_com_android_internal_content_om_OverlayConfig(JNIEnv *env);
197 extern int register_com_android_internal_net_NetworkUtilsInternal(JNIEnv* env);
198 extern int register_com_android_internal_os_ClassLoaderFactory(JNIEnv* env);
199 extern int register_com_android_internal_os_FuseAppLoop(JNIEnv* env);
200 extern int register_com_android_internal_os_KernelAllocationStats(JNIEnv* env);
201 extern int register_com_android_internal_os_KernelCpuBpfTracking(JNIEnv* env);
202 extern int register_com_android_internal_os_KernelCpuTotalBpfMapReader(JNIEnv* env);
203 extern int register_com_android_internal_os_KernelCpuUidBpfMapReader(JNIEnv *env);
204 extern int register_com_android_internal_os_KernelSingleProcessCpuThreadReader(JNIEnv* env);
205 extern int register_com_android_internal_os_KernelSingleUidTimeReader(JNIEnv *env);
206 extern int register_com_android_internal_os_LongArrayMultiStateCounter(JNIEnv* env);
207 extern int register_com_android_internal_os_LongMultiStateCounter(JNIEnv* env);
208 extern int register_com_android_internal_os_Zygote(JNIEnv *env);
209 extern int register_com_android_internal_os_ZygoteCommandBuffer(JNIEnv *env);
210 extern int register_com_android_internal_os_ZygoteInit(JNIEnv *env);
211 extern int register_com_android_internal_security_VerityUtils(JNIEnv* env);
212 extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
213 extern int register_android_window_WindowInfosListener(JNIEnv* env);
214
215 // Namespace for Android Runtime flags applied during boot time.
216 static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot";
217 // Feature flag name to enable/disable generational garbage collection in ART's
218 // Concurrent Copying (CC) garbage collector.
219 static const char* ENABLE_GENERATIONAL_CC = "enable_generational_cc";
220 // Runtime option enabling generational garbage collection in ART's Concurrent
221 // Copying (CC) garbage collector.
222 static const char* kGenerationalCCRuntimeOption = "-Xgc:generational_cc";
223 // Runtime option disabling generational garbage collection in ART's Concurrent
224 // Copying (CC) garbage collector.
225 static const char* kNoGenerationalCCRuntimeOption = "-Xgc:nogenerational_cc";
226
227 // Phenotype property name for enabling profiling the boot class path.
228 static const char* PROFILE_BOOT_CLASS_PATH = "profilebootclasspath";
229
230 // Feature flag name for running the JIT in Zygote experiment, b/119800099.
231 // TODO: Rename the server-level flag or remove.
232 static const char* ENABLE_JITZYGOTE_IMAGE = "enable_apex_image";
233 // Flag to pass to the runtime when using the JIT Zygote image.
234 static const char* kJitZygoteImageOption = "-Xforcejitzygote";
235
236 // Feature flag name for disabling lock profiling.
237 static const char* DISABLE_LOCK_PROFILING = "disable_lock_profiling";
238 // Runtime option disabling lock profiling.
239 static const char* kLockProfThresholdRuntimeOption = "-Xlockprofthreshold:0";
240
241 static AndroidRuntime* gCurRuntime = NULL;
242
243 /*
244 * Code written in the Java Programming Language calls here from main().
245 */
com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv * env,jobject clazz)246 static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
247 {
248 gCurRuntime->onStarted();
249 }
250
com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv * env,jobject clazz)251 static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
252 {
253 gCurRuntime->onZygoteInit();
254 }
255
com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv * env,jobject clazz,jboolean exitWithoutCleanup)256 static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
257 jobject clazz, jboolean exitWithoutCleanup)
258 {
259 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
260 }
261
262 /*
263 * JNI registration.
264 */
265
register_com_android_internal_os_RuntimeInit(JNIEnv * env)266 int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
267 {
268 const JNINativeMethod methods[] = {
269 {"nativeFinishInit", "()V",
270 (void*)com_android_internal_os_RuntimeInit_nativeFinishInit},
271 {"nativeSetExitWithoutCleanup", "(Z)V",
272 (void*)com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup},
273 };
274 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
275 methods, NELEM(methods));
276 }
277
register_com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv * env)278 int register_com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env)
279 {
280 const JNINativeMethod methods[] = {
281 { "nativeZygoteInit", "()V",
282 (void*) com_android_internal_os_ZygoteInit_nativeZygoteInit },
283 };
284 return jniRegisterNativeMethods(env, "com/android/internal/os/ZygoteInit",
285 methods, NELEM(methods));
286 }
287
288 // ----------------------------------------------------------------------
289
290 /*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
291
AndroidRuntime(char * argBlockStart,const size_t argBlockLength)292 AndroidRuntime::AndroidRuntime(char* argBlockStart, const size_t argBlockLength) :
293 mExitWithoutCleanup(false),
294 mArgBlockStart(argBlockStart),
295 mArgBlockLength(argBlockLength)
296 {
297 init_android_graphics();
298
299 // Pre-allocate enough space to hold a fair number of options.
300 mOptions.setCapacity(20);
301
302 assert(gCurRuntime == NULL); // one per process
303 gCurRuntime = this;
304 }
305
~AndroidRuntime()306 AndroidRuntime::~AndroidRuntime()
307 {
308 }
309
310 /*
311 * Register native methods using JNI.
312 */
registerNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)313 /*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
314 const char* className, const JNINativeMethod* gMethods, int numMethods)
315 {
316 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
317 }
318
setArgv0(const char * argv0,bool setProcName)319 void AndroidRuntime::setArgv0(const char* argv0, bool setProcName) {
320 // Set the kernel's task name, for as much of the name as we can fit.
321 // The kernel's TASK_COMM_LEN minus one for the terminating NUL == 15.
322 if (setProcName) {
323 int len = strlen(argv0);
324 if (len < 15) {
325 pthread_setname_np(pthread_self(), argv0);
326 } else {
327 pthread_setname_np(pthread_self(), argv0 + len - 15);
328 }
329 }
330
331 // Directly change the memory pointed to by argv[0].
332 memset(mArgBlockStart, 0, mArgBlockLength);
333 strlcpy(mArgBlockStart, argv0, mArgBlockLength);
334
335 // Let bionic know that we just did that, because __progname points
336 // into argv[0] (https://issuetracker.google.com/152893281).
337 setprogname(mArgBlockStart);
338 }
339
callMain(const String8 & className,jclass clazz,const Vector<String8> & args)340 status_t AndroidRuntime::callMain(const String8& className, jclass clazz,
341 const Vector<String8>& args)
342 {
343 JNIEnv* env;
344 jmethodID methodId;
345
346 ALOGD("Calling main entry %s", className.string());
347
348 env = getJNIEnv();
349 if (clazz == NULL || env == NULL) {
350 return UNKNOWN_ERROR;
351 }
352
353 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
354 if (methodId == NULL) {
355 ALOGE("ERROR: could not find method %s.main(String[])\n", className.string());
356 return UNKNOWN_ERROR;
357 }
358
359 /*
360 * We want to call main() with a String array with our arguments in it.
361 * Create an array and populate it.
362 */
363 jclass stringClass;
364 jobjectArray strArray;
365
366 const size_t numArgs = args.size();
367 stringClass = env->FindClass("java/lang/String");
368 strArray = env->NewObjectArray(numArgs, stringClass, NULL);
369
370 for (size_t i = 0; i < numArgs; i++) {
371 jstring argStr = env->NewStringUTF(args[i].string());
372 env->SetObjectArrayElement(strArray, i, argStr);
373 }
374
375 env->CallStaticVoidMethod(clazz, methodId, strArray);
376 return NO_ERROR;
377 }
378
379 /*
380 * The VM calls this through the "exit" hook.
381 */
runtime_exit(int code)382 static void runtime_exit(int code)
383 {
384 gCurRuntime->exit(code);
385 }
386
387 /*
388 * The VM calls this through the "vfprintf" hook.
389 *
390 * We ignore "fp" and just write the results to the log file.
391 */
runtime_vfprintf(FILE * fp,const char * format,va_list ap)392 static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
393 {
394 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
395 }
396
397 /**
398 * The VM calls this when mutex contention debugging is enabled to
399 * determine whether or not the blocked thread was a "sensitive thread"
400 * for user responsiveness/smoothess.
401 *
402 * Our policy for this is whether or not we're tracing any StrictMode
403 * events on this thread (which we might've inherited via Binder calls
404 * into us)
405 */
runtime_isSensitiveThread()406 static bool runtime_isSensitiveThread() {
407 IPCThreadState* state = IPCThreadState::selfOrNull();
408 return state && state->getStrictModePolicy() != 0;
409 }
410
hasDir(const char * dir)411 static int hasDir(const char* dir)
412 {
413 struct stat s;
414 int res = stat(dir, &s);
415 if (res == 0) {
416 return S_ISDIR(s.st_mode);
417 }
418 return 0;
419 }
420
hasFile(const char * file)421 static bool hasFile(const char* file) {
422 struct stat s;
423 int res = stat(file, &s);
424 if (res == 0) {
425 return S_ISREG(s.st_mode);
426 }
427 return false;
428 }
429
430 /*
431 * Read the persistent locale. Inspects the following system properties
432 * (in order) and returns the first non-empty property in the list :
433 *
434 * (1) persist.sys.locale
435 * (2) persist.sys.language/country/localevar (country and localevar are
436 * inspected iff. language is non-empty.
437 * (3) ro.product.locale
438 * (4) ro.product.locale.language/region
439 *
440 * Note that we need to inspect persist.sys.language/country/localevar to
441 * preserve language settings for devices that are upgrading from Lollipop
442 * to M. The same goes for ro.product.locale.language/region as well.
443 */
readLocale()444 const std::string readLocale()
445 {
446 const std::string locale = GetProperty("persist.sys.locale", "");
447 if (!locale.empty()) {
448 return locale;
449 }
450
451 const std::string language = GetProperty("persist.sys.language", "");
452 if (!language.empty()) {
453 const std::string country = GetProperty("persist.sys.country", "");
454 const std::string variant = GetProperty("persist.sys.localevar", "");
455
456 std::string out = language;
457 if (!country.empty()) {
458 out = out + "-" + country;
459 }
460
461 if (!variant.empty()) {
462 out = out + "-" + variant;
463 }
464
465 return out;
466 }
467
468 const std::string productLocale = GetProperty("ro.product.locale", "");
469 if (!productLocale.empty()) {
470 return productLocale;
471 }
472
473 // If persist.sys.locale and ro.product.locale are missing,
474 // construct a locale value from the individual locale components.
475 const std::string productLanguage = GetProperty("ro.product.locale.language", "en");
476 const std::string productRegion = GetProperty("ro.product.locale.region", "US");
477
478 return productLanguage + "-" + productRegion;
479 }
480
addOption(const char * optionString,void * extraInfo)481 void AndroidRuntime::addOption(const char* optionString, void* extraInfo)
482 {
483 JavaVMOption opt;
484 opt.optionString = optionString;
485 opt.extraInfo = extraInfo;
486 mOptions.add(opt);
487 }
488
489 /*
490 * Parse a property containing space-separated options that should be
491 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
492 *
493 * This will cut up "extraOptsBuf" as we chop it into individual options.
494 *
495 * If "quotingArg" is non-null, it is passed before each extra option in mOptions.
496 *
497 * Adds the strings, if any, to mOptions.
498 */
parseExtraOpts(char * extraOptsBuf,const char * quotingArg)499 void AndroidRuntime::parseExtraOpts(char* extraOptsBuf, const char* quotingArg)
500 {
501 char* start = extraOptsBuf;
502 char* end = NULL;
503 while (*start != '\0') {
504 while (*start == ' ') /* skip leading whitespace */
505 start++;
506 if (*start == '\0') /* was trailing ws, bail */
507 break;
508
509 end = start+1;
510 while (*end != ' ' && *end != '\0') /* find end of token */
511 end++;
512 if (*end == ' ')
513 *end++ = '\0'; /* mark end, advance to indicate more */
514
515 if (quotingArg != NULL) {
516 addOption(quotingArg);
517 }
518 addOption(start);
519 start = end;
520 }
521 }
522
523 /*
524 * Reads a "property" into "buffer" with a default of "defaultArg". If
525 * the property is non-empty, it is treated as a runtime option such
526 * as "-Xmx32m".
527 *
528 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
529 *
530 * If an argument is found, it is added to mOptions.
531 *
532 * If an option is found, it is added to mOptions and true is
533 * returned. Otherwise false is returned.
534 */
parseRuntimeOption(const char * property,char * buffer,const char * runtimeArg,const char * defaultArg)535 bool AndroidRuntime::parseRuntimeOption(const char* property,
536 char* buffer,
537 const char* runtimeArg,
538 const char* defaultArg)
539 {
540 strcpy(buffer, runtimeArg);
541 size_t runtimeArgLen = strlen(runtimeArg);
542 property_get(property, buffer+runtimeArgLen, defaultArg);
543 if (buffer[runtimeArgLen] == '\0') {
544 return false;
545 }
546 addOption(buffer);
547 return true;
548 }
549
550 /*
551 * Reads a "property" into "buffer". If the property is non-empty, it
552 * is treated as a dex2oat compiler option that should be
553 * passed as a quoted option, e.g. "-Ximage-compiler-option --compiler-filter=assume-verified".
554 *
555 * The "compilerArg" is a prefix for the option such as "--compiler-filter=".
556 *
557 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
558 *
559 * If an option is found, it is added to mOptions and true is
560 * returned. Otherwise false is returned.
561 */
parseCompilerOption(const char * property,char * buffer,const char * compilerArg,const char * quotingArg)562 bool AndroidRuntime::parseCompilerOption(const char* property,
563 char* buffer,
564 const char* compilerArg,
565 const char* quotingArg)
566 {
567 strcpy(buffer, compilerArg);
568 size_t compilerArgLen = strlen(compilerArg);
569 property_get(property, buffer+compilerArgLen, "");
570 if (buffer[compilerArgLen] == '\0') {
571 return false;
572 }
573 addOption(quotingArg);
574 addOption(buffer);
575 return true;
576 }
577
578 /*
579 * Reads a "property" into "buffer". If the property is non-empty, it
580 * is treated as a dex2oat compiler runtime option that should be
581 * passed as a quoted option, e.g. "-Ximage-compiler-option
582 * --runtime-arg -Ximage-compiler-option -Xmx32m".
583 *
584 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
585 *
586 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
587 *
588 * If an option is found, it is added to mOptions and true is
589 * returned. Otherwise false is returned.
590 */
parseCompilerRuntimeOption(const char * property,char * buffer,const char * runtimeArg,const char * quotingArg)591 bool AndroidRuntime::parseCompilerRuntimeOption(const char* property,
592 char* buffer,
593 const char* runtimeArg,
594 const char* quotingArg)
595 {
596 strcpy(buffer, runtimeArg);
597 size_t runtimeArgLen = strlen(runtimeArg);
598 property_get(property, buffer+runtimeArgLen, "");
599 if (buffer[runtimeArgLen] == '\0') {
600 return false;
601 }
602 addOption(quotingArg);
603 addOption("--runtime-arg");
604 addOption(quotingArg);
605 addOption(buffer);
606 return true;
607 }
608
609 /*
610 * Start the Dalvik Virtual Machine.
611 *
612 * Various arguments, most determined by system properties, are passed in.
613 * The "mOptions" vector is updated.
614 *
615 * CAUTION: when adding options in here, be careful not to put the
616 * char buffer inside a nested scope. Adding the buffer to the
617 * options using mOptions.add() does not copy the buffer, so if the
618 * buffer goes out of scope the option may be overwritten. It's best
619 * to put the buffer at the top of the function so that it is more
620 * unlikely that someone will surround it in a scope at a later time
621 * and thus introduce a bug.
622 *
623 * Returns 0 on success.
624 */
startVm(JavaVM ** pJavaVM,JNIEnv ** pEnv,bool zygote,bool primary_zygote)625 int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote, bool primary_zygote)
626 {
627 JavaVMInitArgs initArgs;
628 char propBuf[PROPERTY_VALUE_MAX];
629 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
630 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
631 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
632 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
633 char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
634 char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
635 char usejitOptsBuf[sizeof("-Xusejit:")-1 + PROPERTY_VALUE_MAX];
636 char jitpthreadpriorityOptsBuf[sizeof("-Xjitpthreadpriority:")-1 + PROPERTY_VALUE_MAX];
637 char jitmaxsizeOptsBuf[sizeof("-Xjitmaxsize:")-1 + PROPERTY_VALUE_MAX];
638 char jitinitialsizeOptsBuf[sizeof("-Xjitinitialsize:")-1 + PROPERTY_VALUE_MAX];
639 char jitthresholdOptsBuf[sizeof("-Xjitthreshold:")-1 + PROPERTY_VALUE_MAX];
640 char useJitProfilesOptsBuf[sizeof("-Xjitsaveprofilinginfo:")-1 + PROPERTY_VALUE_MAX];
641 char jitprithreadweightOptBuf[sizeof("-Xjitprithreadweight:")-1 + PROPERTY_VALUE_MAX];
642 char jittransitionweightOptBuf[sizeof("-Xjittransitionweight:")-1 + PROPERTY_VALUE_MAX];
643 char hotstartupsamplesOptsBuf[sizeof("-Xps-hot-startup-method-samples:")-1 + PROPERTY_VALUE_MAX];
644 char saveResolvedClassesDelayMsOptsBuf[
645 sizeof("-Xps-save-resolved-classes-delay-ms:")-1 + PROPERTY_VALUE_MAX];
646 char profileMinSavePeriodOptsBuf[sizeof("-Xps-min-save-period-ms:")-1 + PROPERTY_VALUE_MAX];
647 char profileMinFirstSaveOptsBuf[
648 sizeof("-Xps-min-first-save-ms:")-1 + PROPERTY_VALUE_MAX];
649 char madviseRandomOptsBuf[sizeof("-XX:MadviseRandomAccess:")-1 + PROPERTY_VALUE_MAX];
650 char madviseWillNeedFileSizeVdex[
651 sizeof("-XMadviseWillNeedVdexFileSize:")-1 + PROPERTY_VALUE_MAX];
652 char madviseWillNeedFileSizeOdex[
653 sizeof("-XMadviseWillNeedOdexFileSize:")-1 + PROPERTY_VALUE_MAX];
654 char madviseWillNeedFileSizeArt[
655 sizeof("-XMadviseWillNeedArtFileSize:")-1 + PROPERTY_VALUE_MAX];
656 char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
657 char backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1 + PROPERTY_VALUE_MAX];
658 char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
659 char foregroundHeapGrowthMultiplierOptsBuf[
660 sizeof("-XX:ForegroundHeapGrowthMultiplier=")-1 + PROPERTY_VALUE_MAX];
661 char finalizerTimeoutMsOptsBuf[sizeof("-XX:FinalizerTimeoutMs=")-1 + PROPERTY_VALUE_MAX];
662 char threadSuspendTimeoutOptsBuf[sizeof("-XX:ThreadSuspendTimeout=")-1 + PROPERTY_VALUE_MAX];
663 char cachePruneBuf[sizeof("-Xzygote-max-boot-retry=")-1 + PROPERTY_VALUE_MAX];
664 char dex2oatXmsImageFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
665 char dex2oatXmxImageFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
666 char dex2oatCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
667 char dex2oatImageCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
668 char dex2oatThreadsBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
669 char dex2oatThreadsImageBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
670 char dex2oatCpuSetBuf[sizeof("--cpu-set=")-1 + PROPERTY_VALUE_MAX];
671 char dex2oatCpuSetImageBuf[sizeof("--cpu-set=")-1 + PROPERTY_VALUE_MAX];
672 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
673 char dex2oat_isa_variant[sizeof("--instruction-set-variant=") -1 + PROPERTY_VALUE_MAX];
674 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
675 char dex2oat_isa_features[sizeof("--instruction-set-features=") -1 + PROPERTY_VALUE_MAX];
676 char dex2oatFlagsBuf[PROPERTY_VALUE_MAX];
677 char dex2oatImageFlagsBuf[PROPERTY_VALUE_MAX];
678 char extraOptsBuf[PROPERTY_VALUE_MAX];
679 char voldDecryptBuf[PROPERTY_VALUE_MAX];
680 char perfettoHprofOptBuf[sizeof("-XX:PerfettoHprof=") + PROPERTY_VALUE_MAX];
681 char perfettoJavaHeapStackOptBuf[
682 sizeof("-XX:PerfettoJavaHeapStackProf=") + PROPERTY_VALUE_MAX];
683 enum {
684 kEMDefault,
685 kEMIntPortable,
686 kEMIntFast,
687 kEMJitCompiler,
688 } executionMode = kEMDefault;
689 char localeOption[sizeof("-Duser.locale=") + PROPERTY_VALUE_MAX];
690 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:")-1 + PROPERTY_VALUE_MAX];
691 char nativeBridgeLibrary[sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX];
692 char cpuAbiListBuf[sizeof("--cpu-abilist=") + PROPERTY_VALUE_MAX];
693 char corePlatformApiPolicyBuf[sizeof("-Xcore-platform-api-policy:") + PROPERTY_VALUE_MAX];
694 char methodTraceFileBuf[sizeof("-Xmethod-trace-file:") + PROPERTY_VALUE_MAX];
695 char methodTraceFileSizeBuf[sizeof("-Xmethod-trace-file-size:") + PROPERTY_VALUE_MAX];
696 std::string fingerprintBuf;
697 char javaZygoteForkLoopBuf[sizeof("-XX:ForceJavaZygoteForkLoop=") + PROPERTY_VALUE_MAX];
698 char jdwpProviderBuf[sizeof("-XjdwpProvider:") - 1 + PROPERTY_VALUE_MAX];
699 char opaqueJniIds[sizeof("-Xopaque-jni-ids:") - 1 + PROPERTY_VALUE_MAX];
700 char bootImageBuf[sizeof("-Ximage:") - 1 + PROPERTY_VALUE_MAX];
701
702 // Read if we are using the profile configuration, do this at the start since the last ART args
703 // take precedence.
704 property_get("dalvik.vm.profilebootclasspath", propBuf, "");
705 std::string profile_boot_class_path_flag = propBuf;
706 // Empty means the property is unset and we should default to the phenotype property.
707 // The possible values are {"true", "false", ""}
708 if (profile_boot_class_path_flag.empty()) {
709 profile_boot_class_path_flag = server_configurable_flags::GetServerConfigurableFlag(
710 RUNTIME_NATIVE_BOOT_NAMESPACE,
711 PROFILE_BOOT_CLASS_PATH,
712 /*default_value=*/ "");
713 }
714 const bool profile_boot_class_path = (profile_boot_class_path_flag == "true");
715 if (profile_boot_class_path) {
716 addOption("-Xcompiler-option");
717 addOption("--count-hotness-in-compiled-code");
718 addOption("-Xps-profile-boot-class-path");
719 addOption("-Xps-profile-aot-code");
720 addOption("-Xjitsaveprofilinginfo");
721 }
722
723 std::string use_jitzygote_image_flag =
724 server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
725 ENABLE_JITZYGOTE_IMAGE,
726 /*default_value=*/"");
727 // Use the APEX boot image for boot class path profiling to get JIT samples on BCP methods.
728 // Also use the APEX boot image if it's explicitly enabled via configuration flag.
729 const bool use_apex_image = profile_boot_class_path || (use_jitzygote_image_flag == "true");
730 if (use_apex_image) {
731 ALOGI("Using JIT Zygote image: '%s'\n", kJitZygoteImageOption);
732 addOption(kJitZygoteImageOption);
733 } else if (parseRuntimeOption("dalvik.vm.boot-image", bootImageBuf, "-Ximage:")) {
734 ALOGI("Using dalvik.vm.boot-image: '%s'\n", bootImageBuf);
735 } else {
736 ALOGI("Using default boot image");
737 }
738
739 std::string disable_lock_profiling =
740 server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
741 DISABLE_LOCK_PROFILING,
742 /*default_value=*/ "");
743 if (disable_lock_profiling == "true") {
744 addOption(kLockProfThresholdRuntimeOption);
745 ALOGI("Disabling lock profiling: '%s'\n", kLockProfThresholdRuntimeOption);
746 } else {
747 ALOGI("Leaving lock profiling enabled");
748 }
749
750 const bool checkJni = GetBoolProperty("dalvik.vm.checkjni", false);
751 if (checkJni) {
752 ALOGD("CheckJNI is ON");
753
754 /* extended JNI checking */
755 addOption("-Xcheck:jni");
756
757 /* with -Xcheck:jni, this provides a JNI function call trace */
758 //addOption("-verbose:jni");
759 }
760
761 const bool odsignVerificationSuccess = GetBoolProperty("odsign.verification.success", false);
762 if (!odsignVerificationSuccess) {
763 addOption("-Xdeny-art-apex-data-files");
764 }
765
766 property_get("dalvik.vm.execution-mode", propBuf, "");
767 if (strcmp(propBuf, "int:portable") == 0) {
768 executionMode = kEMIntPortable;
769 } else if (strcmp(propBuf, "int:fast") == 0) {
770 executionMode = kEMIntFast;
771 } else if (strcmp(propBuf, "int:jit") == 0) {
772 executionMode = kEMJitCompiler;
773 }
774
775 strcpy(jniOptsBuf, "-Xjniopts:");
776 if (parseRuntimeOption("dalvik.vm.jniopts", jniOptsBuf, "-Xjniopts:")) {
777 ALOGI("JNI options: '%s'\n", jniOptsBuf);
778 }
779
780 /* route exit() to our handler */
781 addOption("exit", (void*) runtime_exit);
782
783 /* route fprintf() to our handler */
784 addOption("vfprintf", (void*) runtime_vfprintf);
785
786 /* register the framework-specific "is sensitive thread" hook */
787 addOption("sensitiveThread", (void*) runtime_isSensitiveThread);
788
789 /* enable verbose; standard options are { jni, gc, class } */
790 //addOption("-verbose:jni");
791 addOption("-verbose:gc");
792 //addOption("-verbose:class");
793
794 // On Android, we always want to allow loading the PerfettoHprof plugin.
795 // Even with this option set, we will still only actually load the plugin
796 // if we are on a userdebug build or the app is debuggable or profileable.
797 // This is enforced in art/runtime/runtime.cc.
798 //
799 // We want to be able to disable this, because this does not work on host,
800 // and we do not want to enable it in tests.
801 parseRuntimeOption("dalvik.vm.perfetto_hprof", perfettoHprofOptBuf, "-XX:PerfettoHprof=",
802 "true");
803
804 // Enable PerfettoJavaHeapStackProf in the zygote
805 parseRuntimeOption("dalvik.vm.perfetto_javaheap", perfettoJavaHeapStackOptBuf,
806 "-XX:PerfettoJavaHeapStackProf=", "true");
807
808 if (primary_zygote) {
809 addOption("-Xprimaryzygote");
810 }
811
812 /*
813 * The default starting and maximum size of the heap. Larger
814 * values should be specified in a product property override.
815 */
816 parseRuntimeOption("dalvik.vm.heapstartsize", heapstartsizeOptsBuf, "-Xms", "4m");
817 parseRuntimeOption("dalvik.vm.heapsize", heapsizeOptsBuf, "-Xmx", "16m");
818
819 parseRuntimeOption("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
820 parseRuntimeOption("dalvik.vm.heapminfree", heapminfreeOptsBuf, "-XX:HeapMinFree=");
821 parseRuntimeOption("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
822 parseRuntimeOption("dalvik.vm.heaptargetutilization",
823 heaptargetutilizationOptsBuf,
824 "-XX:HeapTargetUtilization=");
825
826 /* Foreground heap growth multiplier option */
827 parseRuntimeOption("dalvik.vm.foreground-heap-growth-multiplier",
828 foregroundHeapGrowthMultiplierOptsBuf,
829 "-XX:ForegroundHeapGrowthMultiplier=");
830 /*
831 * Finalizer and thread suspend timeouts.
832 */
833 parseRuntimeOption("dalvik.vm.finalizer-timeout-ms",
834 finalizerTimeoutMsOptsBuf,
835 "-XX:FinalizerTimeoutMs=");
836 parseRuntimeOption("dalvik.vm.thread-suspend-timeout-ms",
837 threadSuspendTimeoutOptsBuf,
838 "-XX:ThreadSuspendTimeout=");
839 /*
840 * JIT related options.
841 */
842 parseRuntimeOption("dalvik.vm.usejit", usejitOptsBuf, "-Xusejit:");
843 parseRuntimeOption("dalvik.vm.jitmaxsize", jitmaxsizeOptsBuf, "-Xjitmaxsize:");
844 parseRuntimeOption("dalvik.vm.jitinitialsize", jitinitialsizeOptsBuf, "-Xjitinitialsize:");
845 parseRuntimeOption("dalvik.vm.jitthreshold", jitthresholdOptsBuf, "-Xjitthreshold:");
846 parseRuntimeOption("dalvik.vm.jitpthreadpriority",
847 jitpthreadpriorityOptsBuf,
848 "-Xjitpthreadpriority:");
849 property_get("dalvik.vm.usejitprofiles", useJitProfilesOptsBuf, "");
850 if (strcmp(useJitProfilesOptsBuf, "true") == 0) {
851 addOption("-Xjitsaveprofilinginfo");
852 }
853
854 parseRuntimeOption("dalvik.vm.jitprithreadweight",
855 jitprithreadweightOptBuf,
856 "-Xjitprithreadweight:");
857
858 parseRuntimeOption("dalvik.vm.jittransitionweight",
859 jittransitionweightOptBuf,
860 "-Xjittransitionweight:");
861 /*
862 * Madvise related options.
863 */
864 parseRuntimeOption("dalvik.vm.madvise-random", madviseRandomOptsBuf, "-XX:MadviseRandomAccess:");
865
866 /*
867 * Use default platform configuration as limits for madvising,
868 * when no properties are specified.
869 */
870 parseRuntimeOption("dalvik.vm.madvise.vdexfile.size",
871 madviseWillNeedFileSizeVdex,
872 "-XMadviseWillNeedVdexFileSize:");
873
874 parseRuntimeOption("dalvik.vm.madvise.odexfile.size",
875 madviseWillNeedFileSizeOdex,
876 "-XMadviseWillNeedOdexFileSize:");
877
878 parseRuntimeOption("dalvik.vm.madvise.artfile.size",
879 madviseWillNeedFileSizeArt,
880 "-XMadviseWillNeedArtFileSize:");
881
882 /*
883 * Profile related options.
884 */
885 parseRuntimeOption("dalvik.vm.hot-startup-method-samples", hotstartupsamplesOptsBuf,
886 "-Xps-hot-startup-method-samples:");
887
888 parseRuntimeOption("dalvik.vm.ps-resolved-classes-delay-ms", saveResolvedClassesDelayMsOptsBuf,
889 "-Xps-save-resolved-classes-delay-ms:");
890
891 parseRuntimeOption("dalvik.vm.ps-min-save-period-ms", profileMinSavePeriodOptsBuf,
892 "-Xps-min-save-period-ms:");
893
894 parseRuntimeOption("dalvik.vm.ps-min-first-save-ms", profileMinFirstSaveOptsBuf,
895 "-Xps-min-first-save-ms:");
896
897 property_get("ro.config.low_ram", propBuf, "");
898 if (strcmp(propBuf, "true") == 0) {
899 addOption("-XX:LowMemoryMode");
900 }
901
902 /*
903 * Garbage-collection related options.
904 */
905 parseRuntimeOption("dalvik.vm.gctype", gctypeOptsBuf, "-Xgc:");
906
907 // If it set, honor the "enable_generational_cc" device configuration;
908 // otherwise, let the runtime use its default behavior.
909 std::string enable_generational_cc =
910 server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
911 ENABLE_GENERATIONAL_CC,
912 /*default_value=*/ "");
913 if (enable_generational_cc == "true") {
914 addOption(kGenerationalCCRuntimeOption);
915 } else if (enable_generational_cc == "false") {
916 addOption(kNoGenerationalCCRuntimeOption);
917 }
918
919 parseRuntimeOption("dalvik.vm.backgroundgctype", backgroundgcOptsBuf, "-XX:BackgroundGC=");
920
921 /*
922 * Enable/disable zygote native fork loop.
923 */
924 parseRuntimeOption("dalvik.vm.force-java-zygote-fork-loop",
925 javaZygoteForkLoopBuf,
926 "-XX:ForceJavaZygoteForkLoop=");
927
928 /*
929 * Enable debugging only for apps forked from zygote.
930 */
931 if (zygote) {
932 // Set the JDWP provider and required arguments. By default let the runtime choose how JDWP is
933 // implemented. When this is not set the runtime defaults to not allowing JDWP.
934 addOption("-XjdwpOptions:suspend=n,server=y");
935 parseRuntimeOption("dalvik.vm.jdwp-provider",
936 jdwpProviderBuf,
937 "-XjdwpProvider:",
938 "default");
939 }
940
941 // Only pass an explicit opaque-jni-ids to apps forked from zygote
942 if (zygote) {
943 parseRuntimeOption("dalvik.vm.opaque-jni-ids",
944 opaqueJniIds,
945 "-Xopaque-jni-ids:",
946 "swapable");
947 }
948
949 parseRuntimeOption("dalvik.vm.lockprof.threshold",
950 lockProfThresholdBuf,
951 "-Xlockprofthreshold:");
952
953 if (executionMode == kEMIntPortable) {
954 addOption("-Xint:portable");
955 } else if (executionMode == kEMIntFast) {
956 addOption("-Xint:fast");
957 } else if (executionMode == kEMJitCompiler) {
958 addOption("-Xint:jit");
959 }
960
961 // If we are booting without the real /data, don't spend time compiling.
962 property_get("vold.decrypt", voldDecryptBuf, "");
963 bool skip_compilation = ((strcmp(voldDecryptBuf, "trigger_restart_min_framework") == 0) ||
964 (strcmp(voldDecryptBuf, "1") == 0));
965
966 // Extra options for JIT.
967 if (skip_compilation) {
968 addOption("-Xcompiler-option");
969 addOption("--compiler-filter=assume-verified");
970 } else {
971 parseCompilerOption("dalvik.vm.dex2oat-filter", dex2oatCompilerFilterBuf,
972 "--compiler-filter=", "-Xcompiler-option");
973 }
974 parseCompilerOption("dalvik.vm.dex2oat-threads", dex2oatThreadsBuf, "-j", "-Xcompiler-option");
975 parseCompilerOption("dalvik.vm.dex2oat-cpu-set", dex2oatCpuSetBuf, "--cpu-set=",
976 "-Xcompiler-option");
977
978 // Copy the variant.
979 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", ABI_STRING);
980 parseCompilerOption(dex2oat_isa_variant_key, dex2oat_isa_variant,
981 "--instruction-set-variant=", "-Xcompiler-option");
982 // Copy the features.
983 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", ABI_STRING);
984 parseCompilerOption(dex2oat_isa_features_key, dex2oat_isa_features,
985 "--instruction-set-features=", "-Xcompiler-option");
986
987 /*
988 * When running with debug.generate-debug-info, add --generate-debug-info to the compiler
989 * options so that both JITted code and the boot image, if it is compiled on device, will
990 * include native debugging information.
991 */
992 property_get("debug.generate-debug-info", propBuf, "");
993 bool generate_debug_info = (strcmp(propBuf, "true") == 0);
994 if (generate_debug_info) {
995 addOption("-Xcompiler-option");
996 addOption("--generate-debug-info");
997 }
998
999 // The mini-debug-info makes it possible to backtrace through compiled code.
1000 bool generate_mini_debug_info = property_get_bool("dalvik.vm.minidebuginfo", 0);
1001 if (generate_mini_debug_info) {
1002 addOption("-Xcompiler-option");
1003 addOption("--generate-mini-debug-info");
1004 }
1005
1006 property_get("dalvik.vm.dex2oat-flags", dex2oatFlagsBuf, "");
1007 parseExtraOpts(dex2oatFlagsBuf, "-Xcompiler-option");
1008
1009 /* extra options; parse this late so it overrides others */
1010 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
1011 parseExtraOpts(extraOptsBuf, NULL);
1012
1013 // Extra options for boot image generation.
1014 if (skip_compilation) {
1015 addOption("-Xnoimage-dex2oat");
1016 } else {
1017 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xms", dex2oatXmsImageFlagsBuf,
1018 "-Xms", "-Ximage-compiler-option");
1019 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xmx", dex2oatXmxImageFlagsBuf,
1020 "-Xmx", "-Ximage-compiler-option");
1021
1022 parseCompilerOption("dalvik.vm.image-dex2oat-filter", dex2oatImageCompilerFilterBuf,
1023 "--compiler-filter=", "-Ximage-compiler-option");
1024
1025 // If there is a dirty-image-objects file, push it.
1026 if (hasFile("/system/etc/dirty-image-objects")) {
1027 addOption("-Ximage-compiler-option");
1028 addOption("--dirty-image-objects=/system/etc/dirty-image-objects");
1029 }
1030
1031 parseCompilerOption("dalvik.vm.image-dex2oat-threads", dex2oatThreadsImageBuf, "-j",
1032 "-Ximage-compiler-option");
1033 parseCompilerOption("dalvik.vm.image-dex2oat-cpu-set", dex2oatCpuSetImageBuf, "--cpu-set=",
1034 "-Ximage-compiler-option");
1035
1036 // The runtime may compile a boot image, when necessary, not using installd. Thus, we need
1037 // to pass the instruction-set-features/variant as an image-compiler-option.
1038 // Note: it is OK to reuse the buffer, as the values are exactly the same between
1039 // * compiler-option, used for runtime compilation (DexClassLoader)
1040 // * image-compiler-option, used for boot-image compilation on device
1041 parseCompilerOption(dex2oat_isa_variant_key, dex2oat_isa_variant,
1042 "--instruction-set-variant=", "-Ximage-compiler-option");
1043 parseCompilerOption(dex2oat_isa_features_key, dex2oat_isa_features,
1044 "--instruction-set-features=", "-Ximage-compiler-option");
1045
1046 if (generate_debug_info) {
1047 addOption("-Ximage-compiler-option");
1048 addOption("--generate-debug-info");
1049 }
1050
1051 if (generate_mini_debug_info) {
1052 addOption("-Ximage-compiler-option");
1053 addOption("--generate-mini-debug-info");
1054 }
1055
1056 property_get("dalvik.vm.image-dex2oat-flags", dex2oatImageFlagsBuf, "");
1057 parseExtraOpts(dex2oatImageFlagsBuf, "-Ximage-compiler-option");
1058 }
1059
1060 /* Set the properties for locale */
1061 {
1062 strcpy(localeOption, "-Duser.locale=");
1063 const std::string locale = readLocale();
1064 strncat(localeOption, locale.c_str(), PROPERTY_VALUE_MAX);
1065 addOption(localeOption);
1066 }
1067
1068 // Trace files are stored in /data/misc/trace which is writable only in debug mode.
1069 property_get("ro.debuggable", propBuf, "0");
1070 if (strcmp(propBuf, "1") == 0) {
1071 property_get("dalvik.vm.method-trace", propBuf, "false");
1072 if (strcmp(propBuf, "true") == 0) {
1073 addOption("-Xmethod-trace");
1074 parseRuntimeOption("dalvik.vm.method-trace-file",
1075 methodTraceFileBuf,
1076 "-Xmethod-trace-file:");
1077 parseRuntimeOption("dalvik.vm.method-trace-file-siz",
1078 methodTraceFileSizeBuf,
1079 "-Xmethod-trace-file-size:");
1080 property_get("dalvik.vm.method-trace-stream", propBuf, "false");
1081 if (strcmp(propBuf, "true") == 0) {
1082 addOption("-Xmethod-trace-stream");
1083 }
1084 }
1085 }
1086
1087 // Native bridge library. "0" means that native bridge is disabled.
1088 //
1089 // Note: bridging is only enabled for the zygote. Other runs of
1090 // app_process may not have the permissions to mount etc.
1091 property_get("ro.dalvik.vm.native.bridge", propBuf, "");
1092 if (propBuf[0] == '\0') {
1093 ALOGW("ro.dalvik.vm.native.bridge is not expected to be empty");
1094 } else if (zygote && strcmp(propBuf, "0") != 0) {
1095 snprintf(nativeBridgeLibrary, sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX,
1096 "-XX:NativeBridge=%s", propBuf);
1097 addOption(nativeBridgeLibrary);
1098 }
1099
1100 #if defined(__LP64__)
1101 const char* cpu_abilist_property_name = "ro.product.cpu.abilist64";
1102 #else
1103 const char* cpu_abilist_property_name = "ro.product.cpu.abilist32";
1104 #endif // defined(__LP64__)
1105 property_get(cpu_abilist_property_name, propBuf, "");
1106 if (propBuf[0] == '\0') {
1107 ALOGE("%s is not expected to be empty", cpu_abilist_property_name);
1108 return -1;
1109 }
1110 snprintf(cpuAbiListBuf, sizeof(cpuAbiListBuf), "--cpu-abilist=%s", propBuf);
1111 addOption(cpuAbiListBuf);
1112
1113 // Dalvik-cache pruning counter.
1114 parseRuntimeOption("dalvik.vm.zygote.max-boot-retry", cachePruneBuf,
1115 "-Xzygote-max-boot-retry=");
1116
1117 // If set, the property below can be used to enable core platform API violation reporting.
1118 property_get("persist.debug.dalvik.vm.core_platform_api_policy", propBuf, "");
1119 if (propBuf[0] != '\0') {
1120 snprintf(corePlatformApiPolicyBuf,
1121 sizeof(corePlatformApiPolicyBuf),
1122 "-Xcore-platform-api-policy:%s",
1123 propBuf);
1124 addOption(corePlatformApiPolicyBuf);
1125 }
1126
1127 /*
1128 * Retrieve the build fingerprint and provide it to the runtime. That way, ANR dumps will
1129 * contain the fingerprint and can be parsed.
1130 * Fingerprints are potentially longer than PROPERTY_VALUE_MAX, so parseRuntimeOption() cannot
1131 * be used here.
1132 * Do not ever re-assign fingerprintBuf as its c_str() value is stored in mOptions.
1133 */
1134 std::string fingerprint = GetProperty("ro.build.fingerprint", "");
1135 if (!fingerprint.empty()) {
1136 fingerprintBuf = "-Xfingerprint:" + fingerprint;
1137 addOption(fingerprintBuf.c_str());
1138 }
1139
1140 initArgs.version = JNI_VERSION_1_4;
1141 initArgs.options = mOptions.editArray();
1142 initArgs.nOptions = mOptions.size();
1143 initArgs.ignoreUnrecognized = JNI_FALSE;
1144
1145 /*
1146 * Initialize the VM.
1147 *
1148 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
1149 * If this call succeeds, the VM is ready, and we can start issuing
1150 * JNI calls.
1151 */
1152 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
1153 ALOGE("JNI_CreateJavaVM failed\n");
1154 return -1;
1155 }
1156
1157 return 0;
1158 }
1159
toSlashClassName(const char * className)1160 char* AndroidRuntime::toSlashClassName(const char* className)
1161 {
1162 char* result = strdup(className);
1163 for (char* cp = result; *cp != '\0'; cp++) {
1164 if (*cp == '.') {
1165 *cp = '/';
1166 }
1167 }
1168 return result;
1169 }
1170
1171 /** Create a Java string from an ASCII or Latin-1 string */
NewStringLatin1(JNIEnv * env,const char * bytes)1172 jstring AndroidRuntime::NewStringLatin1(JNIEnv* env, const char* bytes) {
1173 if (!bytes) return NULL;
1174 int length = strlen(bytes);
1175 jchar* buffer = (jchar *)alloca(length * sizeof(jchar));
1176 if (!buffer) return NULL;
1177 jchar* chp = buffer;
1178 for (int i = 0; i < length; i++) {
1179 *chp++ = *bytes++;
1180 }
1181 return env->NewString(buffer, length);
1182 }
1183
1184
1185 /*
1186 * Start the Android runtime. This involves starting the virtual machine
1187 * and calling the "static void main(String[] args)" method in the class
1188 * named by "className".
1189 *
1190 * Passes the main function two arguments, the class name and the specified
1191 * options string.
1192 */
start(const char * className,const Vector<String8> & options,bool zygote)1193 void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
1194 {
1195 ALOGD(">>>>>> START %s uid %d <<<<<<\n",
1196 className != NULL ? className : "(unknown)", getuid());
1197
1198 static const String8 startSystemServer("start-system-server");
1199 // Whether this is the primary zygote, meaning the zygote which will fork system server.
1200 bool primary_zygote = false;
1201
1202 /*
1203 * 'startSystemServer == true' means runtime is obsolete and not run from
1204 * init.rc anymore, so we print out the boot start event here.
1205 */
1206 for (size_t i = 0; i < options.size(); ++i) {
1207 if (options[i] == startSystemServer) {
1208 primary_zygote = true;
1209 /* track our progress through the boot sequence */
1210 const int LOG_BOOT_PROGRESS_START = 3000;
1211 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
1212 }
1213 }
1214
1215 const char* rootDir = getenv("ANDROID_ROOT");
1216 if (rootDir == NULL) {
1217 rootDir = "/system";
1218 if (!hasDir("/system")) {
1219 LOG_FATAL("No root directory specified, and /system does not exist.");
1220 return;
1221 }
1222 setenv("ANDROID_ROOT", rootDir, 1);
1223 }
1224
1225 const char* artRootDir = getenv("ANDROID_ART_ROOT");
1226 if (artRootDir == NULL) {
1227 LOG_FATAL("No ART directory specified with ANDROID_ART_ROOT environment variable.");
1228 return;
1229 }
1230
1231 const char* i18nRootDir = getenv("ANDROID_I18N_ROOT");
1232 if (i18nRootDir == NULL) {
1233 LOG_FATAL("No runtime directory specified with ANDROID_I18N_ROOT environment variable.");
1234 return;
1235 }
1236
1237 const char* tzdataRootDir = getenv("ANDROID_TZDATA_ROOT");
1238 if (tzdataRootDir == NULL) {
1239 LOG_FATAL("No tz data directory specified with ANDROID_TZDATA_ROOT environment variable.");
1240 return;
1241 }
1242
1243 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
1244 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
1245
1246 /* start the virtual machine */
1247 JniInvocation jni_invocation;
1248 jni_invocation.Init(NULL);
1249 JNIEnv* env;
1250 if (startVm(&mJavaVM, &env, zygote, primary_zygote) != 0) {
1251 return;
1252 }
1253 onVmCreated(env);
1254
1255 /*
1256 * Register android functions.
1257 */
1258 if (startReg(env) < 0) {
1259 ALOGE("Unable to register all android natives\n");
1260 return;
1261 }
1262
1263 /*
1264 * We want to call main() with a String array with arguments in it.
1265 * At present we have two arguments, the class name and an option string.
1266 * Create an array to hold them.
1267 */
1268 jclass stringClass;
1269 jobjectArray strArray;
1270 jstring classNameStr;
1271
1272 stringClass = env->FindClass("java/lang/String");
1273 assert(stringClass != NULL);
1274 strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
1275 assert(strArray != NULL);
1276 classNameStr = env->NewStringUTF(className);
1277 assert(classNameStr != NULL);
1278 env->SetObjectArrayElement(strArray, 0, classNameStr);
1279
1280 for (size_t i = 0; i < options.size(); ++i) {
1281 jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
1282 assert(optionsStr != NULL);
1283 env->SetObjectArrayElement(strArray, i + 1, optionsStr);
1284 }
1285
1286 /*
1287 * Start VM. This thread becomes the main thread of the VM, and will
1288 * not return until the VM exits.
1289 */
1290 char* slashClassName = toSlashClassName(className != NULL ? className : "");
1291 jclass startClass = env->FindClass(slashClassName);
1292 if (startClass == NULL) {
1293 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
1294 /* keep going */
1295 } else {
1296 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
1297 "([Ljava/lang/String;)V");
1298 if (startMeth == NULL) {
1299 ALOGE("JavaVM unable to find main() in '%s'\n", className);
1300 /* keep going */
1301 } else {
1302 env->CallStaticVoidMethod(startClass, startMeth, strArray);
1303
1304 #if 0
1305 if (env->ExceptionCheck())
1306 threadExitUncaughtException(env);
1307 #endif
1308 }
1309 }
1310 free(slashClassName);
1311
1312 ALOGD("Shutting down VM\n");
1313 if (mJavaVM->DetachCurrentThread() != JNI_OK)
1314 ALOGW("Warning: unable to detach main thread\n");
1315 if (mJavaVM->DestroyJavaVM() != 0)
1316 ALOGW("Warning: VM did not shut down cleanly\n");
1317 }
1318
exit(int code)1319 void AndroidRuntime::exit(int code)
1320 {
1321 if (mExitWithoutCleanup) {
1322 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
1323 } else {
1324 ALOGI("VM exiting with result code %d.", code);
1325 onExit(code);
1326 }
1327 ::_exit(code);
1328 }
1329
onVmCreated(JNIEnv * env)1330 void AndroidRuntime::onVmCreated(JNIEnv* env)
1331 {
1332 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
1333 }
1334
getJavaVM()1335 /*static*/ JavaVM* AndroidRuntime::getJavaVM() {
1336 return AndroidRuntime::mJavaVM;
1337 }
1338
AndroidRuntimeGetJavaVM()1339 extern "C" JavaVM* AndroidRuntimeGetJavaVM() {
1340 return AndroidRuntime::getJavaVM();
1341 }
1342
1343 /*
1344 * Get the JNIEnv pointer for this thread.
1345 *
1346 * Returns NULL if the slot wasn't allocated or populated.
1347 */
getJNIEnv()1348 /*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1349 {
1350 JNIEnv* env;
1351 JavaVM* vm = AndroidRuntime::getJavaVM();
1352 assert(vm != NULL);
1353
1354 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1355 return NULL;
1356 return env;
1357 }
1358
1359 /*
1360 * Makes the current thread visible to the VM.
1361 *
1362 * The JNIEnv pointer returned is only valid for the current thread, and
1363 * thus must be tucked into thread-local storage.
1364 */
javaAttachThread(const char * threadName,JNIEnv ** pEnv)1365 static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1366 {
1367 JavaVMAttachArgs args;
1368 JavaVM* vm;
1369 jint result;
1370
1371 vm = AndroidRuntime::getJavaVM();
1372 assert(vm != NULL);
1373
1374 args.version = JNI_VERSION_1_4;
1375 args.name = (char*) threadName;
1376 args.group = NULL;
1377
1378 result = vm->AttachCurrentThread(pEnv, (void*) &args);
1379 if (result != JNI_OK)
1380 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
1381
1382 return result;
1383 }
1384
1385 /*
1386 * Detach the current thread from the set visible to the VM.
1387 */
javaDetachThread(void)1388 static int javaDetachThread(void)
1389 {
1390 JavaVM* vm;
1391 jint result;
1392
1393 vm = AndroidRuntime::getJavaVM();
1394 assert(vm != NULL);
1395
1396 result = vm->DetachCurrentThread();
1397 if (result != JNI_OK)
1398 ALOGE("ERROR: thread detach failed\n");
1399 return result;
1400 }
1401
1402 /*
1403 * When starting a native thread that will be visible from the VM, we
1404 * bounce through this to get the right attach/detach action.
1405 * Note that this function calls free(args)
1406 */
javaThreadShell(void * args)1407 /*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1408 void* start = ((void**)args)[0];
1409 void* userData = ((void **)args)[1];
1410 char* name = (char*) ((void **)args)[2]; // we own this storage
1411 free(args);
1412 JNIEnv* env;
1413 int result;
1414
1415 /* hook us into the VM */
1416 if (javaAttachThread(name, &env) != JNI_OK)
1417 return -1;
1418
1419 /* start the thread running */
1420 result = (*(android_thread_func_t)start)(userData);
1421
1422 /* unhook us */
1423 javaDetachThread();
1424 free(name);
1425
1426 return result;
1427 }
1428
1429 /*
1430 * This is invoked from androidCreateThreadEtc() via the callback
1431 * set with androidSetCreateThreadFunc().
1432 *
1433 * We need to create the new thread in such a way that it gets hooked
1434 * into the VM before it really starts executing.
1435 */
javaCreateThreadEtc(android_thread_func_t entryFunction,void * userData,const char * threadName,int32_t threadPriority,size_t threadStackSize,android_thread_id_t * threadId)1436 /*static*/ int AndroidRuntime::javaCreateThreadEtc(
1437 android_thread_func_t entryFunction,
1438 void* userData,
1439 const char* threadName,
1440 int32_t threadPriority,
1441 size_t threadStackSize,
1442 android_thread_id_t* threadId)
1443 {
1444 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1445 int result;
1446
1447 LOG_ALWAYS_FATAL_IF(threadName == nullptr, "threadName not provided to javaCreateThreadEtc");
1448
1449 args[0] = (void*) entryFunction;
1450 args[1] = userData;
1451 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1452
1453 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1454 threadName, threadPriority, threadStackSize, threadId);
1455 return result;
1456 }
1457
1458 /*
1459 * Create a thread that is visible from the VM.
1460 *
1461 * This is called from elsewhere in the library.
1462 */
createJavaThread(const char * name,void (* start)(void *),void * arg)1463 /*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
1464 void (*start)(void *), void* arg)
1465 {
1466 android_thread_id_t threadId = 0;
1467 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
1468 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1469 return threadId;
1470 }
1471
1472 #if 0
1473 static void quickTest(void* arg)
1474 {
1475 const char* str = (const char*) arg;
1476
1477 printf("In quickTest: %s\n", str);
1478 }
1479 #endif
1480
1481 #ifdef NDEBUG
1482 #define REG_JNI(name) { name }
1483 struct RegJNIRec {
1484 int (*mProc)(JNIEnv*);
1485 };
1486 #else
1487 #define REG_JNI(name) { name, #name }
1488 struct RegJNIRec {
1489 int (*mProc)(JNIEnv*);
1490 const char* mName;
1491 };
1492 #endif
1493
1494 typedef void (*RegJAMProc)();
1495
register_jni_procs(const RegJNIRec array[],size_t count,JNIEnv * env)1496 static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1497 {
1498 for (size_t i = 0; i < count; i++) {
1499 if (array[i].mProc(env) < 0) {
1500 #ifndef NDEBUG
1501 ALOGD("----------!!! %s failed to load\n", array[i].mName);
1502 #endif
1503 return -1;
1504 }
1505 }
1506 return 0;
1507 }
1508
1509 static const RegJNIRec gRegJNI[] = {
1510 REG_JNI(register_com_android_internal_os_RuntimeInit),
1511 REG_JNI(register_com_android_internal_os_ZygoteInit_nativeZygoteInit),
1512 REG_JNI(register_android_os_SystemClock),
1513 REG_JNI(register_android_util_CharsetUtils),
1514 REG_JNI(register_android_util_EventLog),
1515 REG_JNI(register_android_util_Log),
1516 REG_JNI(register_android_util_MemoryIntArray),
1517 REG_JNI(register_android_app_admin_SecurityLog),
1518 REG_JNI(register_android_content_AssetManager),
1519 REG_JNI(register_android_content_StringBlock),
1520 REG_JNI(register_android_content_XmlBlock),
1521 REG_JNI(register_android_content_res_ApkAssets),
1522 REG_JNI(register_android_text_AndroidCharacter),
1523 REG_JNI(register_android_text_Hyphenator),
1524 REG_JNI(register_android_view_InputDevice),
1525 REG_JNI(register_android_view_KeyCharacterMap),
1526 REG_JNI(register_android_os_Process),
1527 REG_JNI(register_android_os_SystemProperties),
1528 REG_JNI(register_android_os_Binder),
1529 REG_JNI(register_android_os_Parcel),
1530 REG_JNI(register_android_os_PerformanceHintManager),
1531 REG_JNI(register_android_os_HidlMemory),
1532 REG_JNI(register_android_os_HidlSupport),
1533 REG_JNI(register_android_os_HwBinder),
1534 REG_JNI(register_android_os_HwBlob),
1535 REG_JNI(register_android_os_HwParcel),
1536 REG_JNI(register_android_os_HwRemoteBinder),
1537 REG_JNI(register_android_os_NativeHandle),
1538 REG_JNI(register_android_os_ServiceManager),
1539 REG_JNI(register_android_os_storage_StorageManager),
1540 REG_JNI(register_android_os_VintfObject),
1541 REG_JNI(register_android_os_VintfRuntimeInfo),
1542 REG_JNI(register_android_service_DataLoaderService),
1543 REG_JNI(register_android_view_DisplayEventReceiver),
1544 REG_JNI(register_android_view_InputApplicationHandle),
1545 REG_JNI(register_android_view_InputWindowHandle),
1546 REG_JNI(register_android_view_Surface),
1547 REG_JNI(register_android_view_SurfaceControl),
1548 REG_JNI(register_android_view_SurfaceControlHdrLayerInfoListener),
1549 REG_JNI(register_android_view_SurfaceSession),
1550 REG_JNI(register_android_view_CompositionSamplingListener),
1551 REG_JNI(register_android_view_TextureView),
1552 REG_JNI(register_android_view_TunnelModeEnabledListener),
1553 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1554 REG_JNI(register_com_google_android_gles_jni_GLImpl),
1555 REG_JNI(register_android_opengl_jni_EGL14),
1556 REG_JNI(register_android_opengl_jni_EGL15),
1557 REG_JNI(register_android_opengl_jni_EGLExt),
1558 REG_JNI(register_android_opengl_jni_GLES10),
1559 REG_JNI(register_android_opengl_jni_GLES10Ext),
1560 REG_JNI(register_android_opengl_jni_GLES11),
1561 REG_JNI(register_android_opengl_jni_GLES11Ext),
1562 REG_JNI(register_android_opengl_jni_GLES20),
1563 REG_JNI(register_android_opengl_jni_GLES30),
1564 REG_JNI(register_android_opengl_jni_GLES31),
1565 REG_JNI(register_android_opengl_jni_GLES31Ext),
1566 REG_JNI(register_android_opengl_jni_GLES32),
1567 REG_JNI(register_android_graphics_classes),
1568 REG_JNI(register_android_graphics_BLASTBufferQueue),
1569 REG_JNI(register_android_graphics_GraphicBuffer),
1570 REG_JNI(register_android_graphics_GraphicsStatsService),
1571 REG_JNI(register_android_graphics_SurfaceTexture),
1572 REG_JNI(register_android_database_CursorWindow),
1573 REG_JNI(register_android_database_SQLiteConnection),
1574 REG_JNI(register_android_database_SQLiteGlobal),
1575 REG_JNI(register_android_database_SQLiteDebug),
1576 REG_JNI(register_android_os_Debug),
1577 REG_JNI(register_android_os_FileObserver),
1578 REG_JNI(register_android_os_GraphicsEnvironment),
1579 REG_JNI(register_android_os_MessageQueue),
1580 REG_JNI(register_android_os_SELinux),
1581 REG_JNI(register_android_os_Trace),
1582 REG_JNI(register_android_os_UEventObserver),
1583 REG_JNI(register_android_net_LocalSocketImpl),
1584 REG_JNI(register_android_os_MemoryFile),
1585 REG_JNI(register_android_os_SharedMemory),
1586 REG_JNI(register_android_os_incremental_IncrementalManager),
1587 REG_JNI(register_com_android_internal_content_om_OverlayConfig),
1588 REG_JNI(register_com_android_internal_net_NetworkUtilsInternal),
1589 REG_JNI(register_com_android_internal_os_ClassLoaderFactory),
1590 REG_JNI(register_com_android_internal_os_LongArrayMultiStateCounter),
1591 REG_JNI(register_com_android_internal_os_LongMultiStateCounter),
1592 REG_JNI(register_com_android_internal_os_Zygote),
1593 REG_JNI(register_com_android_internal_os_ZygoteCommandBuffer),
1594 REG_JNI(register_com_android_internal_os_ZygoteInit),
1595 REG_JNI(register_com_android_internal_security_VerityUtils),
1596 REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
1597 REG_JNI(register_android_hardware_Camera),
1598 REG_JNI(register_android_hardware_camera2_CameraMetadata),
1599 REG_JNI(register_android_hardware_camera2_DngCreator),
1600 REG_JNI(register_android_hardware_camera2_impl_CameraExtensionJpegProcessor),
1601 REG_JNI(register_android_hardware_camera2_utils_SurfaceUtils),
1602 REG_JNI(register_android_hardware_display_DisplayManagerGlobal),
1603 REG_JNI(register_android_hardware_HardwareBuffer),
1604 REG_JNI(register_android_hardware_SensorManager),
1605 REG_JNI(register_android_hardware_SerialPort),
1606 REG_JNI(register_android_hardware_SyncFence),
1607 REG_JNI(register_android_hardware_UsbDevice),
1608 REG_JNI(register_android_hardware_UsbDeviceConnection),
1609 REG_JNI(register_android_hardware_UsbRequest),
1610 REG_JNI(register_android_hardware_location_ActivityRecognitionHardware),
1611 REG_JNI(register_android_media_AudioDeviceAttributes),
1612 REG_JNI(register_android_media_AudioEffectDescriptor),
1613 REG_JNI(register_android_media_AudioSystem),
1614 REG_JNI(register_android_media_AudioRecord),
1615 REG_JNI(register_android_media_AudioTrack),
1616 REG_JNI(register_android_media_AudioAttributes),
1617 REG_JNI(register_android_media_AudioProductStrategies),
1618 REG_JNI(register_android_media_AudioVolumeGroups),
1619 REG_JNI(register_android_media_AudioVolumeGroupChangeHandler),
1620 REG_JNI(register_android_media_MediaMetrics),
1621 REG_JNI(register_android_media_MicrophoneInfo),
1622 REG_JNI(register_android_media_RemoteDisplay),
1623 REG_JNI(register_android_media_ToneGenerator),
1624 REG_JNI(register_android_media_audio_common_AidlConversion),
1625 REG_JNI(register_android_media_midi),
1626
1627 REG_JNI(register_android_opengl_classes),
1628 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
1629 REG_JNI(register_android_backup_BackupDataInput),
1630 REG_JNI(register_android_backup_BackupDataOutput),
1631 REG_JNI(register_android_backup_FileBackupHelperBase),
1632 REG_JNI(register_android_backup_BackupHelperDispatcher),
1633 REG_JNI(register_android_app_backup_FullBackup),
1634 REG_JNI(register_android_app_Activity),
1635 REG_JNI(register_android_app_ActivityThread),
1636 REG_JNI(register_android_app_NativeActivity),
1637 REG_JNI(register_android_util_jar_StrictJarFile),
1638 REG_JNI(register_android_view_InputChannel),
1639 REG_JNI(register_android_view_InputEventReceiver),
1640 REG_JNI(register_android_view_InputEventSender),
1641 REG_JNI(register_android_view_InputQueue),
1642 REG_JNI(register_android_view_KeyEvent),
1643 REG_JNI(register_android_view_MotionEvent),
1644 REG_JNI(register_android_view_PointerIcon),
1645 REG_JNI(register_android_view_VelocityTracker),
1646 REG_JNI(register_android_view_VerifiedKeyEvent),
1647 REG_JNI(register_android_view_VerifiedMotionEvent),
1648
1649 REG_JNI(register_android_content_res_ObbScanner),
1650 REG_JNI(register_android_content_res_Configuration),
1651
1652 REG_JNI(register_android_animation_PropertyValuesHolder),
1653 REG_JNI(register_android_security_Scrypt),
1654 REG_JNI(register_com_android_internal_content_F2fsUtils),
1655 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
1656 REG_JNI(register_com_android_internal_os_FuseAppLoop),
1657 REG_JNI(register_com_android_internal_os_KernelAllocationStats),
1658 REG_JNI(register_com_android_internal_os_KernelCpuBpfTracking),
1659 REG_JNI(register_com_android_internal_os_KernelCpuTotalBpfMapReader),
1660 REG_JNI(register_com_android_internal_os_KernelCpuUidBpfMapReader),
1661 REG_JNI(register_com_android_internal_os_KernelSingleProcessCpuThreadReader),
1662 REG_JNI(register_com_android_internal_os_KernelSingleUidTimeReader),
1663
1664 REG_JNI(register_android_window_WindowInfosListener),
1665 };
1666
1667 /*
1668 * Register android native functions with the VM.
1669 */
startReg(JNIEnv * env)1670 /*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1671 {
1672 ATRACE_NAME("RegisterAndroidNatives");
1673 /*
1674 * This hook causes all future threads created in this process to be
1675 * attached to the JavaVM. (This needs to go away in favor of JNI
1676 * Attach calls.)
1677 */
1678 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1679
1680 ALOGV("--- registering native functions ---\n");
1681
1682 /*
1683 * Every "register" function calls one or more things that return
1684 * a local reference (e.g. FindClass). Because we haven't really
1685 * started the VM yet, they're all getting stored in the base frame
1686 * and never released. Use Push/Pop to manage the storage.
1687 */
1688 env->PushLocalFrame(200);
1689
1690 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1691 env->PopLocalFrame(NULL);
1692 return -1;
1693 }
1694 env->PopLocalFrame(NULL);
1695
1696 //createJavaThread("fubar", quickTest, (void*) "hello");
1697
1698 return 0;
1699 }
1700
getRuntime()1701 AndroidRuntime* AndroidRuntime::getRuntime()
1702 {
1703 return gCurRuntime;
1704 }
1705
1706 /**
1707 * Used by surface flinger's DdmConnection to register native methods from
1708 * the framework.
1709 */
registerFrameworkNatives(JNIEnv * env)1710 extern "C" jint registerFrameworkNatives(JNIEnv* env) {
1711 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1712 }
1713 } // namespace android
1714