From 6b0ed64fb868e64684031558cbdeedcd94d40713 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Thu, 4 Sep 2014 15:43:11 -0700 Subject: [PATCH] Frameworks/base: Add native bridge post-fork initialization (cherry picked from commit d7746a8b65ee4690427ed798816f970218578fa3) Change-Id: I5a20de1cb68dd1802937b369b14c50c9c1031c67 --- core/java/android/os/Process.java | 10 +++++++++- core/java/com/android/internal/os/Zygote.java | 14 +++++++++----- .../java/com/android/internal/os/ZygoteConnection.java | 10 +++++++++- core/jni/com_android_internal_os_Zygote.cpp | 18 +++++++++++------- .../com/android/server/am/ActivityManagerService.java | 9 ++++++++- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/core/java/android/os/Process.java b/core/java/android/os/Process.java index c3ac0122ffc8..b2ebc3119ac4 100644 --- a/core/java/android/os/Process.java +++ b/core/java/android/os/Process.java @@ -468,6 +468,7 @@ public class Process { * @param targetSdkVersion The target SDK version for the app. * @param seInfo null-ok SELinux information for the new process. * @param abi non-null the ABI this app should be started with. + * @param instructionSet null-ok the instruction set to use. * @param zygoteArgs Additional arguments to supply to the zygote process. * * @return An object that describes the result of the attempt to start the process. @@ -482,11 +483,12 @@ public class Process { int targetSdkVersion, String seInfo, String abi, + String instructionSet, String[] zygoteArgs) { try { return startViaZygote(processClass, niceName, uid, gid, gids, debugFlags, mountExternal, targetSdkVersion, seInfo, - abi, zygoteArgs); + abi, instructionSet, zygoteArgs); } catch (ZygoteStartFailedEx ex) { Log.e(LOG_TAG, "Starting VM process through Zygote failed"); @@ -589,6 +591,7 @@ public class Process { * @param targetSdkVersion The target SDK version for the app. * @param seInfo null-ok SELinux information for the new process. * @param abi the ABI the process should use. + * @param instructionSet null-ok the instruction set to use. * @param extraArgs Additional arguments to supply to the zygote process. * @return An object that describes the result of the attempt to start the process. * @throws ZygoteStartFailedEx if process start failed for any reason @@ -601,6 +604,7 @@ public class Process { int targetSdkVersion, String seInfo, String abi, + String instructionSet, String[] extraArgs) throws ZygoteStartFailedEx { synchronized(Process.class) { @@ -660,6 +664,10 @@ public class Process { argsForZygote.add("--seinfo=" + seInfo); } + if (instructionSet != null) { + argsForZygote.add("--instruction-set=" + instructionSet); + } + argsForZygote.add(processClass); if (extraArgs != null) { diff --git a/core/java/com/android/internal/os/Zygote.java b/core/java/com/android/internal/os/Zygote.java index c5211bb23872..c579a15e4990 100644 --- a/core/java/com/android/internal/os/Zygote.java +++ b/core/java/com/android/internal/os/Zygote.java @@ -78,17 +78,20 @@ public final class Zygote { * file descriptor numbers that are to be closed by the child * (and replaced by /dev/null) after forking. An integer value * of -1 in any entry in the array means "ignore this one". + * @param instructionSet null-ok the instruction set to use. * * @return 0 if this is the child, pid of the child * if this is the parent, or -1 on error. */ public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags, - int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose) { + int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, + String instructionSet) { long startTime = SystemClock.elapsedRealtime(); VM_HOOKS.preFork(); checkTime(startTime, "Zygote.preFork"); int pid = nativeForkAndSpecialize( - uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose); + uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose, + instructionSet); checkTime(startTime, "Zygote.nativeForkAndSpecialize"); VM_HOOKS.postForkCommon(); checkTime(startTime, "Zygote.postForkCommon"); @@ -96,7 +99,8 @@ public final class Zygote { } native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags, - int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose); + int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, + String instructionSet); /** * Temporary hack: check time since start time and log if over a fixed threshold. @@ -145,9 +149,9 @@ public final class Zygote { native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); - private static void callPostForkChildHooks(int debugFlags) { + private static void callPostForkChildHooks(int debugFlags, String instructionSet) { long startTime = SystemClock.elapsedRealtime(); - VM_HOOKS.postForkChild(debugFlags); + VM_HOOKS.postForkChild(debugFlags, instructionSet); checkTime(startTime, "Zygote.callPostForkChildHooks"); } diff --git a/core/java/com/android/internal/os/ZygoteConnection.java b/core/java/com/android/internal/os/ZygoteConnection.java index b4c4da655b05..fb50b251f2c4 100644 --- a/core/java/com/android/internal/os/ZygoteConnection.java +++ b/core/java/com/android/internal/os/ZygoteConnection.java @@ -245,7 +245,7 @@ class ZygoteConnection { checkTime(startTime, "zygoteConnection.runOnce: preForkAndSpecialize"); pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, rlimits, parsedArgs.mountExternal, parsedArgs.seInfo, - parsedArgs.niceName, fdsToClose); + parsedArgs.niceName, fdsToClose, parsedArgs.instructionSet); checkTime(startTime, "zygoteConnection.runOnce: postForkAndSpecialize"); } catch (IOException ex) { logAndPrintError(newStderr, "Exception creating pipe", ex); @@ -335,6 +335,7 @@ class ZygoteConnection { * [--] <args for RuntimeInit > *
  • If --runtime-init is absent: * [--] <classname> [args...] + *
  • --instruction-set=instruction-set-string which instruction set to use/emulate. * */ static class Arguments { @@ -398,6 +399,11 @@ class ZygoteConnection { boolean abiListQuery; /** + * The instruction set to use, or null when not important. + */ + String instructionSet; + + /** * Constructs instance and parses args * @param args zygote command-line args * @throws IllegalArgumentException @@ -552,6 +558,8 @@ class ZygoteConnection { mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; } else if (arg.equals("--query-abi-list")) { abiListQuery = true; + } else if (arg.startsWith("--instruction-set=")) { + instructionSet = arg.substring(arg.indexOf('=') + 1); } else { break; } diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp index 3d2d47196f60..451d97aa7c7a 100644 --- a/core/jni/com_android_internal_os_Zygote.cpp +++ b/core/jni/com_android_internal_os_Zygote.cpp @@ -421,7 +421,8 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra jlong permittedCapabilities, jlong effectiveCapabilities, jint mount_external, jstring java_se_info, jstring java_se_name, - bool is_system_server, jintArray fdsToClose) { + bool is_system_server, jintArray fdsToClose, + jstring instructionSet) { uint64_t start = MsTime(); SetSigChldHandler(); ckTime(start, "ForkAndSpecializeCommon:SetSigChldHandler"); @@ -542,7 +543,8 @@ static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArra ckTime(start, "ForkAndSpecializeCommon:child process setup"); - env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags); + env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, + is_system_server ? NULL : instructionSet); ckTime(start, "ForkAndSpecializeCommon:PostForkChildHooks returns"); if (env->ExceptionCheck()) { ALOGE("Error calling post fork hooks."); @@ -561,7 +563,7 @@ static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, jint debug_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring se_name, - jintArray fdsToClose) { + jintArray fdsToClose, jstring instructionSet) { // Grant CAP_WAKE_ALARM to the Bluetooth process. jlong capabilities = 0; if (uid == AID_BLUETOOTH) { @@ -570,7 +572,7 @@ static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, capabilities, capabilities, mount_external, se_info, - se_name, false, fdsToClose); + se_name, false, fdsToClose, instructionSet); } static jint com_android_internal_os_Zygote_nativeForkSystemServer( @@ -580,7 +582,7 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer( pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, permittedCapabilities, effectiveCapabilities, - MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL); + MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL, NULL); if (pid > 0) { // The zygote process checks whether the child process has died or not. ALOGI("System server process %d has been created", pid); @@ -598,7 +600,8 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer( } static JNINativeMethod gMethods[] = { - { "nativeForkAndSpecialize", "(II[II[[IILjava/lang/String;Ljava/lang/String;[I)I", + { "nativeForkAndSpecialize", + "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;)I", (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, { "nativeForkSystemServer", "(II[II[[IJJ)I", (void *) com_android_internal_os_Zygote_nativeForkSystemServer } @@ -609,7 +612,8 @@ int register_com_android_internal_os_Zygote(JNIEnv* env) { if (gZygoteClass == NULL) { RuntimeAbort(env); } - gCallPostForkChildHooks = env->GetStaticMethodID(gZygoteClass, "callPostForkChildHooks", "(I)V"); + gCallPostForkChildHooks = env->GetStaticMethodID(gZygoteClass, "callPostForkChildHooks", + "(ILjava/lang/String;)V"); return AndroidRuntime::registerNativeMethods(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 1397ea491d23..a9a47327ead6 100755 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -196,6 +196,7 @@ import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; +import dalvik.system.VMRuntime; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -3124,6 +3125,11 @@ public final class ActivityManagerService extends ActivityManagerNative requiredAbi = Build.SUPPORTED_ABIS[0]; } + String instructionSet = null; + if (app.info.primaryCpuAbi != null) { + instructionSet = VMRuntime.getInstructionSet(app.info.primaryCpuAbi); + } + // Start the process. It will either succeed and return a result containing // the PID of the new process, or else throw a RuntimeException. boolean isActivityProcess = (entryPoint == null); @@ -3131,7 +3137,8 @@ public final class ActivityManagerService extends ActivityManagerNative checkTime(startTime, "startProcess: asking zygote to start proc"); Process.ProcessStartResult startResult = Process.start(entryPoint, app.processName, uid, uid, gids, debugFlags, mountExternal, - app.info.targetSdkVersion, app.info.seinfo, requiredAbi, entryPointArgs); + app.info.targetSdkVersion, app.info.seinfo, requiredAbi, instructionSet, + entryPointArgs); checkTime(startTime, "startProcess: returned from zygote!"); if (app.isolated) { -- 2.11.0