Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package android.os; |
| 18 | |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 22 | import android.net.LocalSocket; |
| 23 | import android.net.LocalSocketAddress; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 24 | import android.provider.DeviceConfig; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 25 | import android.util.Log; |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 26 | import android.util.Slog; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 27 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 29 | import com.android.internal.os.Zygote; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 30 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 31 | import java.io.BufferedWriter; |
| 32 | import java.io.DataInputStream; |
| 33 | import java.io.IOException; |
| 34 | import java.io.OutputStreamWriter; |
| 35 | import java.nio.charset.StandardCharsets; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Arrays; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 38 | import java.util.Base64; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 39 | import java.util.Collections; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 40 | import java.util.List; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 41 | import java.util.UUID; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 42 | |
| 43 | /*package*/ class ZygoteStartFailedEx extends Exception { |
| 44 | ZygoteStartFailedEx(String s) { |
| 45 | super(s); |
| 46 | } |
| 47 | |
| 48 | ZygoteStartFailedEx(Throwable cause) { |
| 49 | super(cause); |
| 50 | } |
| 51 | |
| 52 | ZygoteStartFailedEx(String s, Throwable cause) { |
| 53 | super(s, cause); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Maintains communication state with the zygote processes. This class is responsible |
| 59 | * for the sockets opened to the zygotes and for starting processes on behalf of the |
| 60 | * {@link android.os.Process} class. |
| 61 | * |
| 62 | * {@hide} |
| 63 | */ |
| 64 | public class ZygoteProcess { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @hide for internal use only. |
| 68 | */ |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 69 | public static final int ZYGOTE_CONNECT_TIMEOUT_MS = 20000; |
| 70 | |
| 71 | /** |
| 72 | * @hide for internal use only. |
| 73 | * |
| 74 | * Use a relatively short delay, because for app zygote, this is in the critical path of |
| 75 | * service launch. |
| 76 | */ |
| 77 | public static final int ZYGOTE_CONNECT_RETRY_DELAY_MS = 50; |
| 78 | |
| 79 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 80 | * @hide for internal use only |
| 81 | */ |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 82 | private static final String LOG_TAG = "ZygoteProcess"; |
| 83 | |
| 84 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 85 | * The default value for enabling the blastula pool. |
| 86 | */ |
| 87 | private static final String BLASTULA_POOL_ENABLED_DEFAULT = "false"; |
| 88 | |
| 89 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 90 | * The name of the socket used to communicate with the primary zygote. |
| 91 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 92 | private final LocalSocketAddress mZygoteSocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * The name of the secondary (alternate ABI) zygote socket. |
| 96 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 97 | private final LocalSocketAddress mZygoteSecondarySocketAddress; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 98 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 99 | /** |
| 100 | * The name of the socket used to communicate with the primary blastula pool. |
| 101 | */ |
| 102 | private final LocalSocketAddress mBlastulaPoolSocketAddress; |
| 103 | |
| 104 | /** |
| 105 | * The name of the socket used to communicate with the secondary (alternate ABI) blastula pool. |
| 106 | */ |
| 107 | private final LocalSocketAddress mBlastulaPoolSecondarySocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 108 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 109 | public ZygoteProcess() { |
| 110 | mZygoteSocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 111 | new LocalSocketAddress(Zygote.PRIMARY_SOCKET_NAME, |
| 112 | LocalSocketAddress.Namespace.RESERVED); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 113 | mZygoteSecondarySocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 114 | new LocalSocketAddress(Zygote.SECONDARY_SOCKET_NAME, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 115 | LocalSocketAddress.Namespace.RESERVED); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 116 | |
| 117 | mBlastulaPoolSocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 118 | new LocalSocketAddress(Zygote.BLASTULA_POOL_PRIMARY_SOCKET_NAME, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 119 | LocalSocketAddress.Namespace.RESERVED); |
| 120 | mBlastulaPoolSecondarySocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 121 | new LocalSocketAddress(Zygote.BLASTULA_POOL_SECONDARY_SOCKET_NAME, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 122 | LocalSocketAddress.Namespace.RESERVED); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 123 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 124 | if (fetchBlastulaPoolEnabledProp()) { |
| 125 | informZygotesOfBlastulaPoolStatus(); |
| 126 | } |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 129 | public ZygoteProcess(LocalSocketAddress primarySocketAddress, |
| 130 | LocalSocketAddress secondarySocketAddress) { |
| 131 | mZygoteSocketAddress = primarySocketAddress; |
| 132 | mZygoteSecondarySocketAddress = secondarySocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 133 | |
| 134 | mBlastulaPoolSocketAddress = null; |
| 135 | mBlastulaPoolSecondarySocketAddress = null; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 138 | public LocalSocketAddress getPrimarySocketAddress() { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 139 | return mZygoteSocketAddress; |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 142 | /** |
| 143 | * State for communicating with the zygote process. |
| 144 | */ |
| 145 | public static class ZygoteState { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 146 | final LocalSocketAddress mZygoteSocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 147 | final LocalSocketAddress mBlastulaSocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 148 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 149 | private final LocalSocket mZygoteSessionSocket; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 150 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 151 | final DataInputStream mZygoteInputStream; |
| 152 | final BufferedWriter mZygoteOutputWriter; |
| 153 | |
| 154 | private final List<String> mABIList; |
| 155 | |
| 156 | private boolean mClosed; |
| 157 | |
| 158 | private ZygoteState(LocalSocketAddress zygoteSocketAddress, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 159 | LocalSocketAddress blastulaSocketAddress, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 160 | LocalSocket zygoteSessionSocket, |
| 161 | DataInputStream zygoteInputStream, |
| 162 | BufferedWriter zygoteOutputWriter, |
| 163 | List<String> abiList) { |
| 164 | this.mZygoteSocketAddress = zygoteSocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 165 | this.mBlastulaSocketAddress = blastulaSocketAddress; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 166 | this.mZygoteSessionSocket = zygoteSessionSocket; |
| 167 | this.mZygoteInputStream = zygoteInputStream; |
| 168 | this.mZygoteOutputWriter = zygoteOutputWriter; |
| 169 | this.mABIList = abiList; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 172 | /** |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 173 | * Create a new ZygoteState object by connecting to the given Zygote socket and saving the |
| 174 | * given blastula socket address. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 175 | * |
| 176 | * @param zygoteSocketAddress Zygote socket to connect to |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 177 | * @param blastulaSocketAddress Blastula socket address to save for later |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 178 | * @return A new ZygoteState object containing a session socket for the given Zygote socket |
| 179 | * address |
| 180 | * @throws IOException |
| 181 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 182 | public static ZygoteState connect(LocalSocketAddress zygoteSocketAddress, |
| 183 | LocalSocketAddress blastulaSocketAddress) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 184 | throws IOException { |
| 185 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 186 | DataInputStream zygoteInputStream = null; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 187 | BufferedWriter zygoteOutputWriter = null; |
| 188 | final LocalSocket zygoteSessionSocket = new LocalSocket(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 189 | |
| 190 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 191 | zygoteSessionSocket.connect(zygoteSocketAddress); |
| 192 | zygoteInputStream = new DataInputStream(zygoteSessionSocket.getInputStream()); |
| 193 | zygoteOutputWriter = |
| 194 | new BufferedWriter( |
| 195 | new OutputStreamWriter(zygoteSessionSocket.getOutputStream()), |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 196 | Zygote.SOCKET_BUFFER_SIZE); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 197 | } catch (IOException ex) { |
| 198 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 199 | zygoteSessionSocket.close(); |
| 200 | } catch (IOException ignore) { } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 201 | |
| 202 | throw ex; |
| 203 | } |
| 204 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 205 | return new ZygoteState(zygoteSocketAddress, blastulaSocketAddress, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 206 | zygoteSessionSocket, zygoteInputStream, zygoteOutputWriter, |
| 207 | getAbiList(zygoteOutputWriter, zygoteInputStream)); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 210 | LocalSocket getBlastulaSessionSocket() throws IOException { |
| 211 | final LocalSocket blastulaSessionSocket = new LocalSocket(); |
| 212 | blastulaSessionSocket.connect(this.mBlastulaSocketAddress); |
| 213 | |
| 214 | return blastulaSessionSocket; |
| 215 | } |
| 216 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 217 | boolean matches(String abi) { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 218 | return mABIList.contains(abi); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public void close() { |
| 222 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 223 | mZygoteSessionSocket.close(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 224 | } catch (IOException ex) { |
| 225 | Log.e(LOG_TAG,"I/O exception on routine close", ex); |
| 226 | } |
| 227 | |
| 228 | mClosed = true; |
| 229 | } |
| 230 | |
| 231 | boolean isClosed() { |
| 232 | return mClosed; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 237 | * Lock object to protect access to the two ZygoteStates below. This lock must be |
| 238 | * acquired while communicating over the ZygoteState's socket, to prevent |
| 239 | * interleaved access. |
| 240 | */ |
| 241 | private final Object mLock = new Object(); |
| 242 | |
| 243 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 244 | * List of exemptions to the API blacklist. These are prefix matches on the runtime format |
| 245 | * symbol signature. Any matching symbol is treated by the runtime as being on the light grey |
| 246 | * list. |
| 247 | */ |
| 248 | private List<String> mApiBlacklistExemptions = Collections.emptyList(); |
| 249 | |
| 250 | /** |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 251 | * Proportion of hidden API accesses that should be logged to the event log; 0 - 0x10000. |
| 252 | */ |
| 253 | private int mHiddenApiAccessLogSampleRate; |
| 254 | |
| 255 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 256 | * The state of the connection to the primary zygote. |
| 257 | */ |
| 258 | private ZygoteState primaryZygoteState; |
| 259 | |
| 260 | /** |
| 261 | * The state of the connection to the secondary zygote. |
| 262 | */ |
| 263 | private ZygoteState secondaryZygoteState; |
| 264 | |
| 265 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 266 | * If the blastula pool should be created and used to start applications. |
| 267 | * |
| 268 | * Setting this value to false will disable the creation, maintenance, and use of the blastula |
| 269 | * pool. When the blastula pool is disabled the application lifecycle will be identical to |
| 270 | * previous versions of Android. |
| 271 | */ |
| 272 | private boolean mBlastulaPoolEnabled = false; |
| 273 | |
| 274 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 275 | * Start a new process. |
| 276 | * |
| 277 | * <p>If processes are enabled, a new process is created and the |
| 278 | * static main() function of a <var>processClass</var> is executed there. |
| 279 | * The process will continue running after this function returns. |
| 280 | * |
| 281 | * <p>If processes are not enabled, a new thread in the caller's |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 282 | * process is created and main() of <var>processclass</var> called there. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 283 | * |
| 284 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 285 | * give to the process instead of using processClass. This allows you to |
| 286 | * make easily identifyable processes even if you are using the same base |
| 287 | * <var>processClass</var> to start them. |
| 288 | * |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 289 | * When invokeWith is not null, the process will be started as a fresh app |
Tamas Berghammer | 0ca16fa | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 290 | * and not a zygote fork. Note that this is only allowed for uid 0 or when |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 291 | * runtimeFlags contains DEBUG_ENABLE_DEBUGGER. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 292 | * |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 293 | * @param processClass The class to use as the process's main entry |
| 294 | * point. |
| 295 | * @param niceName A more readable name to use for the process. |
| 296 | * @param uid The user-id under which the process will run. |
| 297 | * @param gid The group-id under which the process will run. |
| 298 | * @param gids Additional group-ids associated with the process. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 299 | * @param runtimeFlags Additional flags. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 300 | * @param targetSdkVersion The target SDK version for the app. |
| 301 | * @param seInfo null-ok SELinux information for the new process. |
| 302 | * @param abi non-null the ABI this app should be started with. |
| 303 | * @param instructionSet null-ok the instruction set to use. |
| 304 | * @param appDataDir null-ok the data directory of the app. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 305 | * @param invokeWith null-ok the command to invoke with. |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 306 | * @param packageName null-ok the name of the package this process belongs to. |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 307 | * @param packagesForUid null-ok all the packages with the same uid as this process. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 308 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 309 | * |
| 310 | * @return An object that describes the result of the attempt to start the process. |
| 311 | * @throws RuntimeException on fatal start failure |
| 312 | */ |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 313 | public final Process.ProcessStartResult start(@NonNull final String processClass, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 314 | final String niceName, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 315 | int uid, int gid, @Nullable int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 316 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 317 | int targetSdkVersion, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 318 | @Nullable String seInfo, |
| 319 | @NonNull String abi, |
| 320 | @Nullable String instructionSet, |
| 321 | @Nullable String appDataDir, |
| 322 | @Nullable String invokeWith, |
| 323 | @Nullable String packageName, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 324 | @Nullable String[] packagesForUid, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 325 | @Nullable String sandboxId, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 326 | boolean useBlastulaPool, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 327 | @Nullable String[] zygoteArgs) { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 328 | // TODO (chriswailes): Is there a better place to check this value? |
| 329 | if (fetchBlastulaPoolEnabledPropWithMinInterval()) { |
| 330 | informZygotesOfBlastulaPoolStatus(); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 333 | try { |
| 334 | return startViaZygote(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 335 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 336 | abi, instructionSet, appDataDir, invokeWith, /*startChildZygote=*/false, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 337 | packageName, packagesForUid, sandboxId, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 338 | useBlastulaPool, zygoteArgs); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 339 | } catch (ZygoteStartFailedEx ex) { |
| 340 | Log.e(LOG_TAG, |
| 341 | "Starting VM process through Zygote failed"); |
| 342 | throw new RuntimeException( |
| 343 | "Starting VM process through Zygote failed", ex); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** retry interval for opening a zygote socket */ |
| 348 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 349 | |
| 350 | /** |
| 351 | * Queries the zygote for the list of ABIS it supports. |
| 352 | * |
| 353 | * @throws ZygoteStartFailedEx if the query failed. |
| 354 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 355 | @GuardedBy("mLock") |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 356 | private static List<String> getAbiList(BufferedWriter writer, DataInputStream inputStream) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 357 | throws IOException { |
| 358 | // Each query starts with the argument count (1 in this case) |
| 359 | writer.write("1"); |
| 360 | // ... followed by a new-line. |
| 361 | writer.newLine(); |
| 362 | // ... followed by our only argument. |
| 363 | writer.write("--query-abi-list"); |
| 364 | writer.newLine(); |
| 365 | writer.flush(); |
| 366 | |
| 367 | // The response is a length prefixed stream of ASCII bytes. |
| 368 | int numBytes = inputStream.readInt(); |
| 369 | byte[] bytes = new byte[numBytes]; |
| 370 | inputStream.readFully(bytes); |
| 371 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 372 | String rawList = new String(bytes, StandardCharsets.US_ASCII); |
| 373 | |
| 374 | return Arrays.asList(rawList.split(",")); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Sends an argument list to the zygote process, which starts a new child |
| 379 | * and returns the child's pid. Please note: the present implementation |
| 380 | * replaces newlines in the argument list with spaces. |
| 381 | * |
| 382 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 383 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 384 | @GuardedBy("mLock") |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 385 | private Process.ProcessStartResult zygoteSendArgsAndGetResult( |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 386 | ZygoteState zygoteState, boolean useBlastulaPool, ArrayList<String> args) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 387 | throws ZygoteStartFailedEx { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 388 | // Throw early if any of the arguments are malformed. This means we can |
| 389 | // avoid writing a partial response to the zygote. |
| 390 | for (String arg : args) { |
| 391 | if (arg.indexOf('\n') >= 0) { |
| 392 | throw new ZygoteStartFailedEx("embedded newlines not allowed"); |
| 393 | } |
| 394 | } |
| 395 | |
Chris Wailes | b13bfc5 | 2019-02-20 11:19:48 -0800 | [diff] [blame] | 396 | /* |
| 397 | * See com.android.internal.os.ZygoteArguments.parseArgs() |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 398 | * Presently the wire format to the zygote process is: |
| 399 | * a) a count of arguments (argc, in essence) |
| 400 | * b) a number of newline-separated argument strings equal to count |
| 401 | * |
| 402 | * After the zygote process reads these it will write the pid of |
| 403 | * the child or -1 on failure, followed by boolean to |
| 404 | * indicate whether a wrapper process was used. |
| 405 | */ |
| 406 | String msgStr = Integer.toString(args.size()) + "\n" |
| 407 | + String.join("\n", args) + "\n"; |
| 408 | |
| 409 | // Should there be a timeout on this? |
| 410 | Process.ProcessStartResult result = new Process.ProcessStartResult(); |
| 411 | |
| 412 | // TODO (chriswailes): Move branch body into separate function. |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 413 | if (useBlastulaPool && mBlastulaPoolEnabled && isValidBlastulaCommand(args)) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 414 | LocalSocket blastulaSessionSocket = null; |
| 415 | |
| 416 | try { |
| 417 | blastulaSessionSocket = zygoteState.getBlastulaSessionSocket(); |
| 418 | |
| 419 | final BufferedWriter blastulaWriter = |
| 420 | new BufferedWriter( |
| 421 | new OutputStreamWriter(blastulaSessionSocket.getOutputStream()), |
| 422 | Zygote.SOCKET_BUFFER_SIZE); |
| 423 | final DataInputStream blastulaReader = |
| 424 | new DataInputStream(blastulaSessionSocket.getInputStream()); |
| 425 | |
| 426 | blastulaWriter.write(msgStr); |
| 427 | blastulaWriter.flush(); |
| 428 | |
| 429 | result.pid = blastulaReader.readInt(); |
| 430 | // Blastulas can't be used to spawn processes that need wrappers. |
| 431 | result.usingWrapper = false; |
| 432 | |
| 433 | if (result.pid < 0) { |
| 434 | throw new ZygoteStartFailedEx("Blastula specialization failed"); |
| 435 | } |
| 436 | |
| 437 | return result; |
| 438 | } catch (IOException ex) { |
| 439 | // If there was an IOException using the blastula pool we will log the error and |
| 440 | // attempt to start the process through the Zygote. |
| 441 | Log.e(LOG_TAG, "IO Exception while communicating with blastula pool - " |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 442 | + ex.getMessage()); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 443 | } finally { |
| 444 | try { |
| 445 | blastulaSessionSocket.close(); |
| 446 | } catch (IOException ex) { |
| 447 | Log.e(LOG_TAG, "Failed to close blastula session socket: " + ex.getMessage()); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 448 | } |
| 449 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 450 | } |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 451 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 452 | try { |
| 453 | final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter; |
| 454 | final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 455 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 456 | zygoteWriter.write(msgStr); |
| 457 | zygoteWriter.flush(); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 458 | |
| 459 | // Always read the entire result from the input stream to avoid leaving |
| 460 | // bytes in the stream for future process starts to accidentally stumble |
| 461 | // upon. |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 462 | result.pid = zygoteInputStream.readInt(); |
| 463 | result.usingWrapper = zygoteInputStream.readBoolean(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 464 | } catch (IOException ex) { |
| 465 | zygoteState.close(); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 466 | Log.e(LOG_TAG, "IO Exception while communicating with Zygote - " |
| 467 | + ex.toString()); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 468 | throw new ZygoteStartFailedEx(ex); |
| 469 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 470 | |
| 471 | if (result.pid < 0) { |
| 472 | throw new ZygoteStartFailedEx("fork() failed"); |
| 473 | } |
| 474 | |
| 475 | return result; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Flags that may not be passed to a blastula. |
| 480 | */ |
| 481 | private static final String[] INVALID_BLASTULA_FLAGS = { |
| 482 | "--query-abi-list", |
| 483 | "--get-pid", |
| 484 | "--preload-default", |
| 485 | "--preload-package", |
| 486 | "--preload-app", |
| 487 | "--start-child-zygote", |
| 488 | "--set-api-blacklist-exemptions", |
| 489 | "--hidden-api-log-sampling-rate", |
| 490 | "--invoke-with" |
| 491 | }; |
| 492 | |
| 493 | /** |
| 494 | * Tests a command list to see if it is valid to send to a blastula. |
| 495 | * @param args Zygote/Blastula command arguments |
| 496 | * @return True if the command can be passed to a blastula; false otherwise |
| 497 | */ |
| 498 | private static boolean isValidBlastulaCommand(ArrayList<String> args) { |
| 499 | for (String flag : args) { |
| 500 | for (String badFlag : INVALID_BLASTULA_FLAGS) { |
| 501 | if (flag.startsWith(badFlag)) { |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return true; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Starts a new process via the zygote mechanism. |
| 512 | * |
| 513 | * @param processClass Class name whose static main() to run |
| 514 | * @param niceName 'nice' process name to appear in ps |
| 515 | * @param uid a POSIX uid that the new process should setuid() to |
| 516 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 517 | * @param gids null-ok; a list of supplementary group IDs that the |
| 518 | * new process should setgroup() to. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 519 | * @param runtimeFlags Additional flags for the runtime. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 520 | * @param targetSdkVersion The target SDK version for the app. |
| 521 | * @param seInfo null-ok SELinux information for the new process. |
| 522 | * @param abi the ABI the process should use. |
| 523 | * @param instructionSet null-ok the instruction set to use. |
| 524 | * @param appDataDir null-ok the data directory of the app. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 525 | * @param startChildZygote Start a sub-zygote. This creates a new zygote process |
| 526 | * that has its state cloned from this zygote process. |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 527 | * @param packageName null-ok the name of the package this process belongs to. |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 528 | * @param packagesForUid null-ok all the packages with the same uid as this process. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 529 | * @param extraArgs Additional arguments to supply to the zygote process. |
| 530 | * @return An object that describes the result of the attempt to start the process. |
| 531 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 532 | */ |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 533 | private Process.ProcessStartResult startViaZygote(@NonNull final String processClass, |
| 534 | @Nullable final String niceName, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 535 | final int uid, final int gid, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 536 | @Nullable final int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 537 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 538 | int targetSdkVersion, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 539 | @Nullable String seInfo, |
| 540 | @NonNull String abi, |
| 541 | @Nullable String instructionSet, |
| 542 | @Nullable String appDataDir, |
| 543 | @Nullable String invokeWith, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 544 | boolean startChildZygote, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 545 | @Nullable String packageName, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 546 | @Nullable String[] packagesForUid, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 547 | @Nullable String sandboxId, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 548 | boolean useBlastulaPool, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 549 | @Nullable String[] extraArgs) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 550 | throws ZygoteStartFailedEx { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 551 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 552 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 553 | // --runtime-args, --setuid=, --setgid=, |
| 554 | // and --setgroups= must go first |
| 555 | argsForZygote.add("--runtime-args"); |
| 556 | argsForZygote.add("--setuid=" + uid); |
| 557 | argsForZygote.add("--setgid=" + gid); |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 558 | argsForZygote.add("--runtime-flags=" + runtimeFlags); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 559 | if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) { |
| 560 | argsForZygote.add("--mount-external-default"); |
| 561 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) { |
| 562 | argsForZygote.add("--mount-external-read"); |
| 563 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) { |
| 564 | argsForZygote.add("--mount-external-write"); |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 565 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) { |
| 566 | argsForZygote.add("--mount-external-full"); |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 567 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) { |
| 568 | argsForZygote.add("--mount-external-installer"); |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 569 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_LEGACY) { |
| 570 | argsForZygote.add("--mount-external-legacy"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 571 | } |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 572 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 573 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 574 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 575 | // --setgroups is a comma-separated list |
| 576 | if (gids != null && gids.length > 0) { |
| 577 | StringBuilder sb = new StringBuilder(); |
| 578 | sb.append("--setgroups="); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 579 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 580 | int sz = gids.length; |
| 581 | for (int i = 0; i < sz; i++) { |
| 582 | if (i != 0) { |
| 583 | sb.append(','); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 584 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 585 | sb.append(gids[i]); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 588 | argsForZygote.add(sb.toString()); |
| 589 | } |
| 590 | |
| 591 | if (niceName != null) { |
| 592 | argsForZygote.add("--nice-name=" + niceName); |
| 593 | } |
| 594 | |
| 595 | if (seInfo != null) { |
| 596 | argsForZygote.add("--seinfo=" + seInfo); |
| 597 | } |
| 598 | |
| 599 | if (instructionSet != null) { |
| 600 | argsForZygote.add("--instruction-set=" + instructionSet); |
| 601 | } |
| 602 | |
| 603 | if (appDataDir != null) { |
| 604 | argsForZygote.add("--app-data-dir=" + appDataDir); |
| 605 | } |
| 606 | |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 607 | if (invokeWith != null) { |
| 608 | argsForZygote.add("--invoke-with"); |
| 609 | argsForZygote.add(invokeWith); |
| 610 | } |
| 611 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 612 | if (startChildZygote) { |
| 613 | argsForZygote.add("--start-child-zygote"); |
| 614 | } |
| 615 | |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 616 | if (packageName != null) { |
| 617 | argsForZygote.add("--package-name=" + packageName); |
| 618 | } |
| 619 | |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 620 | if (packagesForUid != null && packagesForUid.length > 0) { |
| 621 | final StringBuilder sb = new StringBuilder(); |
| 622 | sb.append("--packages-for-uid="); |
| 623 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 624 | // TODO (chriswailes): Replace with String.join |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 625 | for (int i = 0; i < packagesForUid.length; ++i) { |
| 626 | if (i != 0) { |
| 627 | sb.append(','); |
| 628 | } |
| 629 | sb.append(packagesForUid[i]); |
| 630 | } |
| 631 | argsForZygote.add(sb.toString()); |
| 632 | } |
| 633 | |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 634 | if (sandboxId != null) { |
| 635 | argsForZygote.add("--sandbox-id=" + sandboxId); |
| 636 | } |
| 637 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 638 | argsForZygote.add(processClass); |
| 639 | |
| 640 | if (extraArgs != null) { |
| 641 | for (String arg : extraArgs) { |
| 642 | argsForZygote.add(arg); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 643 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 644 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 645 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 646 | synchronized(mLock) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 647 | return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 648 | useBlastulaPool, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 649 | argsForZygote); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 653 | private boolean fetchBlastulaPoolEnabledProp() { |
| 654 | boolean origVal = mBlastulaPoolEnabled; |
| 655 | |
| 656 | final String propertyString = |
| 657 | Zygote.getSystemProperty( |
| 658 | DeviceConfig.RuntimeNative.BLASTULA_POOL_ENABLED, |
| 659 | BLASTULA_POOL_ENABLED_DEFAULT); |
| 660 | |
| 661 | if (!propertyString.isEmpty()) { |
| 662 | mBlastulaPoolEnabled = |
| 663 | Zygote.getSystemPropertyBoolean( |
| 664 | DeviceConfig.RuntimeNative.BLASTULA_POOL_ENABLED, |
| 665 | Boolean.parseBoolean(BLASTULA_POOL_ENABLED_DEFAULT)); |
| 666 | } |
| 667 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 668 | if (origVal != mBlastulaPoolEnabled) { |
| 669 | Log.i(LOG_TAG, "blastulaPoolEnabled = " + mBlastulaPoolEnabled); |
| 670 | } |
| 671 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 672 | return origVal != mBlastulaPoolEnabled; |
| 673 | } |
| 674 | |
| 675 | private long mLastPropCheckTimestamp = 0; |
| 676 | |
| 677 | private boolean fetchBlastulaPoolEnabledPropWithMinInterval() { |
| 678 | final long currentTimestamp = SystemClock.elapsedRealtime(); |
| 679 | |
| 680 | if (currentTimestamp - mLastPropCheckTimestamp >= Zygote.PROPERTY_CHECK_INTERVAL) { |
| 681 | mLastPropCheckTimestamp = currentTimestamp; |
| 682 | return fetchBlastulaPoolEnabledProp(); |
| 683 | } |
| 684 | |
| 685 | return false; |
| 686 | } |
| 687 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 688 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 689 | * Closes the connections to the zygote, if they exist. |
| 690 | */ |
| 691 | public void close() { |
| 692 | if (primaryZygoteState != null) { |
| 693 | primaryZygoteState.close(); |
| 694 | } |
| 695 | if (secondaryZygoteState != null) { |
| 696 | secondaryZygoteState.close(); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 701 | * Tries to establish a connection to the zygote that handles a given {@code abi}. Might block |
| 702 | * and retry if the zygote is unresponsive. This method is a no-op if a connection is |
| 703 | * already open. |
| 704 | */ |
| 705 | public void establishZygoteConnectionForAbi(String abi) { |
| 706 | try { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 707 | synchronized(mLock) { |
| 708 | openZygoteSocketIfNeeded(abi); |
| 709 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 710 | } catch (ZygoteStartFailedEx ex) { |
| 711 | throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /** |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 716 | * Attempt to retrieve the PID of the zygote serving the given abi. |
| 717 | */ |
| 718 | public int getZygotePid(String abi) { |
| 719 | try { |
| 720 | synchronized (mLock) { |
| 721 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 722 | |
| 723 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 724 | state.mZygoteOutputWriter.write("1"); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 725 | // ... followed by a new-line. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 726 | state.mZygoteOutputWriter.newLine(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 727 | // ... followed by our only argument. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 728 | state.mZygoteOutputWriter.write("--get-pid"); |
| 729 | state.mZygoteOutputWriter.newLine(); |
| 730 | state.mZygoteOutputWriter.flush(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 731 | |
| 732 | // The response is a length prefixed stream of ASCII bytes. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 733 | int numBytes = state.mZygoteInputStream.readInt(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 734 | byte[] bytes = new byte[numBytes]; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 735 | state.mZygoteInputStream.readFully(bytes); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 736 | |
| 737 | return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII)); |
| 738 | } |
| 739 | } catch (Exception ex) { |
| 740 | throw new RuntimeException("Failure retrieving pid", ex); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 745 | * Push hidden API blacklisting exemptions into the zygote process(es). |
| 746 | * |
| 747 | * <p>The list of exemptions will take affect for all new processes forked from the zygote after |
| 748 | * this call. |
| 749 | * |
Mathew Inwood | 33d5138 | 2018-04-05 13:56:39 +0100 | [diff] [blame] | 750 | * @param exemptions List of hidden API exemption prefixes. Any matching members are treated as |
| 751 | * whitelisted/public APIs (i.e. allowed, no logging of usage). |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 752 | */ |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 753 | public boolean setApiBlacklistExemptions(List<String> exemptions) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 754 | synchronized (mLock) { |
| 755 | mApiBlacklistExemptions = exemptions; |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 756 | boolean ok = maybeSetApiBlacklistExemptions(primaryZygoteState, true); |
| 757 | if (ok) { |
| 758 | ok = maybeSetApiBlacklistExemptions(secondaryZygoteState, true); |
| 759 | } |
| 760 | return ok; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 764 | /** |
| 765 | * Set the precentage of detected hidden API accesses that are logged to the event log. |
| 766 | * |
| 767 | * <p>This rate will take affect for all new processes forked from the zygote after this call. |
| 768 | * |
| 769 | * @param rate An integer between 0 and 0x10000 inclusive. 0 means no event logging. |
| 770 | */ |
| 771 | public void setHiddenApiAccessLogSampleRate(int rate) { |
| 772 | synchronized (mLock) { |
| 773 | mHiddenApiAccessLogSampleRate = rate; |
| 774 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 775 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 776 | } |
| 777 | } |
| 778 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 779 | @GuardedBy("mLock") |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 780 | private boolean maybeSetApiBlacklistExemptions(ZygoteState state, boolean sendIfEmpty) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 781 | if (state == null || state.isClosed()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 782 | Slog.e(LOG_TAG, "Can't set API blacklist exemptions: no zygote connection"); |
| 783 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 784 | } |
| 785 | if (!sendIfEmpty && mApiBlacklistExemptions.isEmpty()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 786 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 787 | } |
| 788 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 789 | state.mZygoteOutputWriter.write(Integer.toString(mApiBlacklistExemptions.size() + 1)); |
| 790 | state.mZygoteOutputWriter.newLine(); |
| 791 | state.mZygoteOutputWriter.write("--set-api-blacklist-exemptions"); |
| 792 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 793 | for (int i = 0; i < mApiBlacklistExemptions.size(); ++i) { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 794 | state.mZygoteOutputWriter.write(mApiBlacklistExemptions.get(i)); |
| 795 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 796 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 797 | state.mZygoteOutputWriter.flush(); |
| 798 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 799 | if (status != 0) { |
| 800 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions; status " + status); |
| 801 | } |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 802 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 803 | } catch (IOException ioe) { |
| 804 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions", ioe); |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 805 | mApiBlacklistExemptions = Collections.emptyList(); |
| 806 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 810 | private void maybeSetHiddenApiAccessLogSampleRate(ZygoteState state) { |
| 811 | if (state == null || state.isClosed()) { |
| 812 | return; |
| 813 | } |
| 814 | if (mHiddenApiAccessLogSampleRate == -1) { |
| 815 | return; |
| 816 | } |
| 817 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 818 | state.mZygoteOutputWriter.write(Integer.toString(1)); |
| 819 | state.mZygoteOutputWriter.newLine(); |
| 820 | state.mZygoteOutputWriter.write("--hidden-api-log-sampling-rate=" |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 821 | + Integer.toString(mHiddenApiAccessLogSampleRate)); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 822 | state.mZygoteOutputWriter.newLine(); |
| 823 | state.mZygoteOutputWriter.flush(); |
| 824 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 825 | if (status != 0) { |
| 826 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate; status " + status); |
| 827 | } |
| 828 | } catch (IOException ioe) { |
| 829 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate", ioe); |
| 830 | } |
| 831 | } |
| 832 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 833 | /** |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 834 | * Creates a ZygoteState for the primary zygote if it doesn't exist or has been disconnected. |
| 835 | */ |
| 836 | @GuardedBy("mLock") |
| 837 | private void attemptConnectionToPrimaryZygote() throws IOException { |
| 838 | if (primaryZygoteState == null || primaryZygoteState.isClosed()) { |
| 839 | primaryZygoteState = |
| 840 | ZygoteState.connect(mZygoteSocketAddress, mBlastulaPoolSocketAddress); |
| 841 | |
| 842 | maybeSetApiBlacklistExemptions(primaryZygoteState, false); |
| 843 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Creates a ZygoteState for the secondary zygote if it doesn't exist or has been disconnected. |
| 849 | */ |
| 850 | @GuardedBy("mLock") |
| 851 | private void attemptConnectionToSecondaryZygote() throws IOException { |
| 852 | if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { |
| 853 | secondaryZygoteState = |
| 854 | ZygoteState.connect(mZygoteSecondarySocketAddress, |
| 855 | mBlastulaPoolSecondarySocketAddress); |
| 856 | |
| 857 | maybeSetApiBlacklistExemptions(secondaryZygoteState, false); |
| 858 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 863 | * Tries to open a session socket to a Zygote process with a compatible ABI if one is not |
| 864 | * already open. If a compatible session socket is already open that session socket is returned. |
| 865 | * This function may block and may have to try connecting to multiple Zygotes to find the |
| 866 | * appropriate one. Requires that mLock be held. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 867 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 868 | @GuardedBy("mLock") |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 869 | private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx { |
| 870 | try { |
| 871 | attemptConnectionToPrimaryZygote(); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 872 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 873 | if (primaryZygoteState.matches(abi)) { |
| 874 | return primaryZygoteState; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 875 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 876 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 877 | // The primary zygote didn't match. Try the secondary. |
| 878 | attemptConnectionToSecondaryZygote(); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 879 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 880 | if (secondaryZygoteState.matches(abi)) { |
| 881 | return secondaryZygoteState; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 882 | } |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 883 | } catch (IOException ioe) { |
| 884 | throw new ZygoteStartFailedEx("Error connecting to zygote", ioe); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); |
| 888 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 889 | |
| 890 | /** |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 891 | * Instructs the zygote to pre-load the application code for the given Application. |
| 892 | * Only the app zygote supports this function. |
| 893 | * TODO preloadPackageForAbi() can probably be removed and the callers an use this instead. |
| 894 | */ |
| 895 | public boolean preloadApp(ApplicationInfo appInfo, String abi) throws ZygoteStartFailedEx, |
| 896 | IOException { |
| 897 | synchronized (mLock) { |
| 898 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 899 | state.mZygoteOutputWriter.write("2"); |
| 900 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 901 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 902 | state.mZygoteOutputWriter.write("--preload-app"); |
| 903 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 904 | |
| 905 | // Zygote args needs to be strings, so in order to pass ApplicationInfo, |
| 906 | // write it to a Parcel, and base64 the raw Parcel bytes to the other side. |
| 907 | Parcel parcel = Parcel.obtain(); |
| 908 | appInfo.writeToParcel(parcel, 0 /* flags */); |
| 909 | String encodedParcelData = Base64.getEncoder().encodeToString(parcel.marshall()); |
| 910 | parcel.recycle(); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 911 | state.mZygoteOutputWriter.write(encodedParcelData); |
| 912 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 913 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 914 | state.mZygoteOutputWriter.flush(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 915 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 916 | return (state.mZygoteInputStream.readInt() == 0); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
| 920 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 921 | * Instructs the zygote to pre-load the classes and native libraries at the given paths |
| 922 | * for the specified abi. Not all zygotes support this function. |
| 923 | */ |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 924 | public boolean preloadPackageForAbi(String packagePath, String libsPath, String libFileName, |
| 925 | String cacheKey, String abi) throws ZygoteStartFailedEx, |
| 926 | IOException { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 927 | synchronized(mLock) { |
| 928 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 929 | state.mZygoteOutputWriter.write("5"); |
| 930 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 931 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 932 | state.mZygoteOutputWriter.write("--preload-package"); |
| 933 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 934 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 935 | state.mZygoteOutputWriter.write(packagePath); |
| 936 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 937 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 938 | state.mZygoteOutputWriter.write(libsPath); |
| 939 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 940 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 941 | state.mZygoteOutputWriter.write(libFileName); |
| 942 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 943 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 944 | state.mZygoteOutputWriter.write(cacheKey); |
| 945 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 946 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 947 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | bae484a | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 948 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 949 | return (state.mZygoteInputStream.readInt() == 0); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 950 | } |
| 951 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 952 | |
| 953 | /** |
| 954 | * Instructs the zygote to preload the default set of classes and resources. Returns |
| 955 | * {@code true} if a preload was performed as a result of this call, and {@code false} |
| 956 | * otherwise. The latter usually means that the zygote eagerly preloaded at startup |
| 957 | * or due to a previous call to {@code preloadDefault}. Note that this call is synchronous. |
| 958 | */ |
| 959 | public boolean preloadDefault(String abi) throws ZygoteStartFailedEx, IOException { |
| 960 | synchronized (mLock) { |
| 961 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 962 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 963 | state.mZygoteOutputWriter.write("1"); |
| 964 | state.mZygoteOutputWriter.newLine(); |
| 965 | state.mZygoteOutputWriter.write("--preload-default"); |
| 966 | state.mZygoteOutputWriter.newLine(); |
| 967 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 968 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 969 | return (state.mZygoteInputStream.readInt() == 0); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 972 | |
| 973 | /** |
| 974 | * Try connecting to the Zygote over and over again until we hit a time-out. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 975 | * @param zygoteSocketName The name of the socket to connect to. |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 976 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 977 | public static void waitForConnectionToZygote(String zygoteSocketName) { |
| 978 | final LocalSocketAddress zygoteSocketAddress = |
| 979 | new LocalSocketAddress(zygoteSocketName, LocalSocketAddress.Namespace.RESERVED); |
| 980 | waitForConnectionToZygote(zygoteSocketAddress); |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Try connecting to the Zygote over and over again until we hit a time-out. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 985 | * @param zygoteSocketAddress The name of the socket to connect to. |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 986 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 987 | public static void waitForConnectionToZygote(LocalSocketAddress zygoteSocketAddress) { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 988 | int numRetries = ZYGOTE_CONNECT_TIMEOUT_MS / ZYGOTE_CONNECT_RETRY_DELAY_MS; |
| 989 | for (int n = numRetries; n >= 0; n--) { |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 990 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 991 | final ZygoteState zs = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 992 | ZygoteState.connect(zygoteSocketAddress, null); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 993 | zs.close(); |
| 994 | return; |
| 995 | } catch (IOException ioe) { |
| 996 | Log.w(LOG_TAG, |
| 997 | "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 998 | } |
| 999 | |
| 1000 | try { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 1001 | Thread.sleep(ZYGOTE_CONNECT_RETRY_DELAY_MS); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 1002 | } catch (InterruptedException ie) { |
| 1003 | } |
| 1004 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 1005 | Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " |
| 1006 | + zygoteSocketAddress.getName()); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 1007 | } |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1008 | |
| 1009 | /** |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 1010 | * Sends messages to the zygotes telling them to change the status of their blastula pools. If |
| 1011 | * this notification fails the ZygoteProcess will fall back to the previous behavior. |
| 1012 | */ |
| 1013 | private void informZygotesOfBlastulaPoolStatus() { |
| 1014 | final String command = "1\n--blastula-pool-enabled=" + mBlastulaPoolEnabled + "\n"; |
| 1015 | |
| 1016 | synchronized (mLock) { |
| 1017 | try { |
| 1018 | attemptConnectionToPrimaryZygote(); |
| 1019 | |
| 1020 | primaryZygoteState.mZygoteOutputWriter.write(command); |
| 1021 | primaryZygoteState.mZygoteOutputWriter.flush(); |
| 1022 | } catch (IOException ioe) { |
| 1023 | mBlastulaPoolEnabled = !mBlastulaPoolEnabled; |
| 1024 | Log.w(LOG_TAG, "Failed to inform zygotes of blastula pool status: " |
| 1025 | + ioe.getMessage()); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | try { |
| 1030 | attemptConnectionToSecondaryZygote(); |
| 1031 | |
| 1032 | try { |
| 1033 | secondaryZygoteState.mZygoteOutputWriter.write(command); |
| 1034 | secondaryZygoteState.mZygoteOutputWriter.flush(); |
| 1035 | |
| 1036 | // Wait for the secondary Zygote to finish its work. |
| 1037 | secondaryZygoteState.mZygoteInputStream.readInt(); |
| 1038 | } catch (IOException ioe) { |
| 1039 | throw new IllegalStateException( |
| 1040 | "Blastula pool state change cause an irrecoverable error", |
| 1041 | ioe); |
| 1042 | } |
| 1043 | } catch (IOException ioe) { |
| 1044 | // No secondary zygote present. This is expected on some devices. |
| 1045 | } |
| 1046 | |
| 1047 | // Wait for the response from the primary zygote here so the primary/secondary zygotes |
| 1048 | // can work concurrently. |
| 1049 | try { |
| 1050 | // Wait for the primary zygote to finish its work. |
| 1051 | primaryZygoteState.mZygoteInputStream.readInt(); |
| 1052 | } catch (IOException ioe) { |
| 1053 | throw new IllegalStateException( |
| 1054 | "Blastula pool state change cause an irrecoverable error", |
| 1055 | ioe); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1061 | * Starts a new zygote process as a child of this zygote. This is used to create |
| 1062 | * secondary zygotes that inherit data from the zygote that this object |
| 1063 | * communicates with. This returns a new ZygoteProcess representing a connection |
| 1064 | * to the newly created zygote. Throws an exception if the zygote cannot be started. |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1065 | * |
| 1066 | * @param processClass The class to use as the child zygote's main entry |
| 1067 | * point. |
| 1068 | * @param niceName A more readable name to use for the process. |
| 1069 | * @param uid The user-id under which the child zygote will run. |
| 1070 | * @param gid The group-id under which the child zygote will run. |
| 1071 | * @param gids Additional group-ids associated with the child zygote process. |
| 1072 | * @param runtimeFlags Additional flags. |
| 1073 | * @param seInfo null-ok SELinux information for the child zygote process. |
| 1074 | * @param abi non-null the ABI of the child zygote |
| 1075 | * @param acceptedAbiList ABIs this child zygote will accept connections for; this |
| 1076 | * may be different from <code>abi</code> in case the children |
| 1077 | * spawned from this Zygote only communicate using ABI-safe methods. |
| 1078 | * @param instructionSet null-ok the instruction set to use. |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1079 | * @param uidRangeStart The first UID in the range the child zygote may setuid()/setgid() to |
| 1080 | * @param uidRangeEnd The last UID in the range the child zygote may setuid()/setgid() to |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1081 | */ |
| 1082 | public ChildZygoteProcess startChildZygote(final String processClass, |
| 1083 | final String niceName, |
| 1084 | int uid, int gid, int[] gids, |
| 1085 | int runtimeFlags, |
| 1086 | String seInfo, |
| 1087 | String abi, |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1088 | String acceptedAbiList, |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1089 | String instructionSet, |
| 1090 | int uidRangeStart, |
| 1091 | int uidRangeEnd) { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1092 | // Create an unguessable address in the global abstract namespace. |
| 1093 | final LocalSocketAddress serverAddress = new LocalSocketAddress( |
| 1094 | processClass + "/" + UUID.randomUUID().toString()); |
| 1095 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1096 | final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName(), |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1097 | Zygote.CHILD_ZYGOTE_ABI_LIST_ARG + acceptedAbiList, |
| 1098 | Zygote.CHILD_ZYGOTE_UID_RANGE_START + uidRangeStart, |
| 1099 | Zygote.CHILD_ZYGOTE_UID_RANGE_END + uidRangeEnd}; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1100 | |
| 1101 | Process.ProcessStartResult result; |
| 1102 | try { |
| 1103 | result = startViaZygote(processClass, niceName, uid, gid, |
| 1104 | gids, runtimeFlags, 0 /* mountExternal */, 0 /* targetSdkVersion */, seInfo, |
| 1105 | abi, instructionSet, null /* appDataDir */, null /* invokeWith */, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 1106 | true /* startChildZygote */, null /* packageName */, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 1107 | null /* packagesForUid */, null /* sandboxId */, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 1108 | false /* useBlastulaPool */, extraArgs); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1109 | } catch (ZygoteStartFailedEx ex) { |
| 1110 | throw new RuntimeException("Starting child-zygote through Zygote failed", ex); |
| 1111 | } |
| 1112 | |
| 1113 | return new ChildZygoteProcess(serverAddress, result.pid); |
| 1114 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 1115 | } |