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; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 30 | import com.android.internal.util.Preconditions; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 31 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 32 | import java.io.BufferedWriter; |
| 33 | import java.io.DataInputStream; |
| 34 | import java.io.IOException; |
| 35 | import java.io.OutputStreamWriter; |
| 36 | import java.nio.charset.StandardCharsets; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Arrays; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 39 | import java.util.Base64; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 40 | import java.util.Collections; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 41 | import java.util.List; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 42 | import java.util.UUID; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 43 | |
| 44 | /*package*/ class ZygoteStartFailedEx extends Exception { |
| 45 | ZygoteStartFailedEx(String s) { |
| 46 | super(s); |
| 47 | } |
| 48 | |
| 49 | ZygoteStartFailedEx(Throwable cause) { |
| 50 | super(cause); |
| 51 | } |
| 52 | |
| 53 | ZygoteStartFailedEx(String s, Throwable cause) { |
| 54 | super(s, cause); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Maintains communication state with the zygote processes. This class is responsible |
| 60 | * for the sockets opened to the zygotes and for starting processes on behalf of the |
| 61 | * {@link android.os.Process} class. |
| 62 | * |
| 63 | * {@hide} |
| 64 | */ |
| 65 | public class ZygoteProcess { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * @hide for internal use only. |
| 69 | */ |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 70 | public static final int ZYGOTE_CONNECT_TIMEOUT_MS = 20000; |
| 71 | |
| 72 | /** |
| 73 | * @hide for internal use only. |
| 74 | * |
| 75 | * Use a relatively short delay, because for app zygote, this is in the critical path of |
| 76 | * service launch. |
| 77 | */ |
| 78 | public static final int ZYGOTE_CONNECT_RETRY_DELAY_MS = 50; |
| 79 | |
| 80 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 81 | * @hide for internal use only |
| 82 | */ |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 83 | private static final String LOG_TAG = "ZygoteProcess"; |
| 84 | |
| 85 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 86 | * The default value for enabling the blastula pool. |
| 87 | */ |
| 88 | private static final String BLASTULA_POOL_ENABLED_DEFAULT = "false"; |
| 89 | |
| 90 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 91 | * The name of the socket used to communicate with the primary zygote. |
| 92 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 93 | private final LocalSocketAddress mZygoteSocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * The name of the secondary (alternate ABI) zygote socket. |
| 97 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 98 | private final LocalSocketAddress mZygoteSecondarySocketAddress; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 99 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 100 | /** |
| 101 | * The name of the socket used to communicate with the primary blastula pool. |
| 102 | */ |
| 103 | private final LocalSocketAddress mBlastulaPoolSocketAddress; |
| 104 | |
| 105 | /** |
| 106 | * The name of the socket used to communicate with the secondary (alternate ABI) blastula pool. |
| 107 | */ |
| 108 | private final LocalSocketAddress mBlastulaPoolSecondarySocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 109 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 110 | public ZygoteProcess() { |
| 111 | mZygoteSocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 112 | new LocalSocketAddress(Zygote.PRIMARY_SOCKET_NAME, |
| 113 | LocalSocketAddress.Namespace.RESERVED); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 114 | mZygoteSecondarySocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 115 | new LocalSocketAddress(Zygote.SECONDARY_SOCKET_NAME, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 116 | LocalSocketAddress.Namespace.RESERVED); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 117 | |
| 118 | mBlastulaPoolSocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 119 | new LocalSocketAddress(Zygote.BLASTULA_POOL_PRIMARY_SOCKET_NAME, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 120 | LocalSocketAddress.Namespace.RESERVED); |
| 121 | mBlastulaPoolSecondarySocketAddress = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 122 | new LocalSocketAddress(Zygote.BLASTULA_POOL_SECONDARY_SOCKET_NAME, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 123 | LocalSocketAddress.Namespace.RESERVED); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 124 | |
| 125 | // TODO (chriswailes): Uncomment when the blastula pool can be enabled. |
| 126 | // fetchBlastulaPoolEnabledProp(); |
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) { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 328 | if (fetchBlastulaPoolEnabledProp()) { |
| 329 | // TODO (chriswailes): Send the appropriate command to the zygotes |
| 330 | Log.i(LOG_TAG, "Blastula pool enabled property set to: " + mBlastulaPoolEnabled); |
| 331 | |
| 332 | // This can't be enabled yet, but we do want to test this code path. |
| 333 | mBlastulaPoolEnabled = false; |
| 334 | } |
| 335 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 336 | try { |
| 337 | return startViaZygote(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 338 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 339 | abi, instructionSet, appDataDir, invokeWith, /*startChildZygote=*/false, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame^] | 340 | packageName, packagesForUid, sandboxId, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 341 | useBlastulaPool, zygoteArgs); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 342 | } catch (ZygoteStartFailedEx ex) { |
| 343 | Log.e(LOG_TAG, |
| 344 | "Starting VM process through Zygote failed"); |
| 345 | throw new RuntimeException( |
| 346 | "Starting VM process through Zygote failed", ex); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** retry interval for opening a zygote socket */ |
| 351 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 352 | |
| 353 | /** |
| 354 | * Queries the zygote for the list of ABIS it supports. |
| 355 | * |
| 356 | * @throws ZygoteStartFailedEx if the query failed. |
| 357 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 358 | @GuardedBy("mLock") |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 359 | private static List<String> getAbiList(BufferedWriter writer, DataInputStream inputStream) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 360 | throws IOException { |
| 361 | // Each query starts with the argument count (1 in this case) |
| 362 | writer.write("1"); |
| 363 | // ... followed by a new-line. |
| 364 | writer.newLine(); |
| 365 | // ... followed by our only argument. |
| 366 | writer.write("--query-abi-list"); |
| 367 | writer.newLine(); |
| 368 | writer.flush(); |
| 369 | |
| 370 | // The response is a length prefixed stream of ASCII bytes. |
| 371 | int numBytes = inputStream.readInt(); |
| 372 | byte[] bytes = new byte[numBytes]; |
| 373 | inputStream.readFully(bytes); |
| 374 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 375 | String rawList = new String(bytes, StandardCharsets.US_ASCII); |
| 376 | |
| 377 | return Arrays.asList(rawList.split(",")); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Sends an argument list to the zygote process, which starts a new child |
| 382 | * and returns the child's pid. Please note: the present implementation |
| 383 | * replaces newlines in the argument list with spaces. |
| 384 | * |
| 385 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 386 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 387 | @GuardedBy("mLock") |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 388 | private static Process.ProcessStartResult zygoteSendArgsAndGetResult( |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 389 | ZygoteState zygoteState, boolean useBlastulaPool, ArrayList<String> args) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 390 | throws ZygoteStartFailedEx { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 391 | // Throw early if any of the arguments are malformed. This means we can |
| 392 | // avoid writing a partial response to the zygote. |
| 393 | for (String arg : args) { |
| 394 | if (arg.indexOf('\n') >= 0) { |
| 395 | throw new ZygoteStartFailedEx("embedded newlines not allowed"); |
| 396 | } |
| 397 | } |
| 398 | |
Chris Wailes | b13bfc5 | 2019-02-20 11:19:48 -0800 | [diff] [blame] | 399 | /* |
| 400 | * See com.android.internal.os.ZygoteArguments.parseArgs() |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 401 | * Presently the wire format to the zygote process is: |
| 402 | * a) a count of arguments (argc, in essence) |
| 403 | * b) a number of newline-separated argument strings equal to count |
| 404 | * |
| 405 | * After the zygote process reads these it will write the pid of |
| 406 | * the child or -1 on failure, followed by boolean to |
| 407 | * indicate whether a wrapper process was used. |
| 408 | */ |
| 409 | String msgStr = Integer.toString(args.size()) + "\n" |
| 410 | + String.join("\n", args) + "\n"; |
| 411 | |
| 412 | // Should there be a timeout on this? |
| 413 | Process.ProcessStartResult result = new Process.ProcessStartResult(); |
| 414 | |
| 415 | // TODO (chriswailes): Move branch body into separate function. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 416 | if (useBlastulaPool && isValidBlastulaCommand(args)) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 417 | LocalSocket blastulaSessionSocket = null; |
| 418 | |
| 419 | try { |
| 420 | blastulaSessionSocket = zygoteState.getBlastulaSessionSocket(); |
| 421 | |
| 422 | final BufferedWriter blastulaWriter = |
| 423 | new BufferedWriter( |
| 424 | new OutputStreamWriter(blastulaSessionSocket.getOutputStream()), |
| 425 | Zygote.SOCKET_BUFFER_SIZE); |
| 426 | final DataInputStream blastulaReader = |
| 427 | new DataInputStream(blastulaSessionSocket.getInputStream()); |
| 428 | |
| 429 | blastulaWriter.write(msgStr); |
| 430 | blastulaWriter.flush(); |
| 431 | |
| 432 | result.pid = blastulaReader.readInt(); |
| 433 | // Blastulas can't be used to spawn processes that need wrappers. |
| 434 | result.usingWrapper = false; |
| 435 | |
| 436 | if (result.pid < 0) { |
| 437 | throw new ZygoteStartFailedEx("Blastula specialization failed"); |
| 438 | } |
| 439 | |
| 440 | return result; |
| 441 | } catch (IOException ex) { |
| 442 | // If there was an IOException using the blastula pool we will log the error and |
| 443 | // attempt to start the process through the Zygote. |
| 444 | Log.e(LOG_TAG, "IO Exception while communicating with blastula pool - " |
| 445 | + ex.toString()); |
| 446 | } finally { |
| 447 | try { |
| 448 | blastulaSessionSocket.close(); |
| 449 | } catch (IOException ex) { |
| 450 | Log.e(LOG_TAG, "Failed to close blastula session socket: " + ex.getMessage()); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 451 | } |
| 452 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 453 | } |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 454 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 455 | try { |
| 456 | final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter; |
| 457 | final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 458 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 459 | zygoteWriter.write(msgStr); |
| 460 | zygoteWriter.flush(); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 461 | |
| 462 | // Always read the entire result from the input stream to avoid leaving |
| 463 | // bytes in the stream for future process starts to accidentally stumble |
| 464 | // upon. |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 465 | result.pid = zygoteInputStream.readInt(); |
| 466 | result.usingWrapper = zygoteInputStream.readBoolean(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 467 | } catch (IOException ex) { |
| 468 | zygoteState.close(); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 469 | Log.e(LOG_TAG, "IO Exception while communicating with Zygote - " |
| 470 | + ex.toString()); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 471 | throw new ZygoteStartFailedEx(ex); |
| 472 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 473 | |
| 474 | if (result.pid < 0) { |
| 475 | throw new ZygoteStartFailedEx("fork() failed"); |
| 476 | } |
| 477 | |
| 478 | return result; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Flags that may not be passed to a blastula. |
| 483 | */ |
| 484 | private static final String[] INVALID_BLASTULA_FLAGS = { |
| 485 | "--query-abi-list", |
| 486 | "--get-pid", |
| 487 | "--preload-default", |
| 488 | "--preload-package", |
| 489 | "--preload-app", |
| 490 | "--start-child-zygote", |
| 491 | "--set-api-blacklist-exemptions", |
| 492 | "--hidden-api-log-sampling-rate", |
| 493 | "--invoke-with" |
| 494 | }; |
| 495 | |
| 496 | /** |
| 497 | * Tests a command list to see if it is valid to send to a blastula. |
| 498 | * @param args Zygote/Blastula command arguments |
| 499 | * @return True if the command can be passed to a blastula; false otherwise |
| 500 | */ |
| 501 | private static boolean isValidBlastulaCommand(ArrayList<String> args) { |
| 502 | for (String flag : args) { |
| 503 | for (String badFlag : INVALID_BLASTULA_FLAGS) { |
| 504 | if (flag.startsWith(badFlag)) { |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return true; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Starts a new process via the zygote mechanism. |
| 515 | * |
| 516 | * @param processClass Class name whose static main() to run |
| 517 | * @param niceName 'nice' process name to appear in ps |
| 518 | * @param uid a POSIX uid that the new process should setuid() to |
| 519 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 520 | * @param gids null-ok; a list of supplementary group IDs that the |
| 521 | * new process should setgroup() to. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 522 | * @param runtimeFlags Additional flags for the runtime. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 523 | * @param targetSdkVersion The target SDK version for the app. |
| 524 | * @param seInfo null-ok SELinux information for the new process. |
| 525 | * @param abi the ABI the process should use. |
| 526 | * @param instructionSet null-ok the instruction set to use. |
| 527 | * @param appDataDir null-ok the data directory of the app. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 528 | * @param startChildZygote Start a sub-zygote. This creates a new zygote process |
| 529 | * that has its state cloned from this zygote process. |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 530 | * @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] | 531 | * @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] | 532 | * @param extraArgs Additional arguments to supply to the zygote process. |
| 533 | * @return An object that describes the result of the attempt to start the process. |
| 534 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 535 | */ |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 536 | private Process.ProcessStartResult startViaZygote(@NonNull final String processClass, |
| 537 | @Nullable final String niceName, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 538 | final int uid, final int gid, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 539 | @Nullable final int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 540 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 541 | int targetSdkVersion, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 542 | @Nullable String seInfo, |
| 543 | @NonNull String abi, |
| 544 | @Nullable String instructionSet, |
| 545 | @Nullable String appDataDir, |
| 546 | @Nullable String invokeWith, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 547 | boolean startChildZygote, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 548 | @Nullable String packageName, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 549 | @Nullable String[] packagesForUid, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 550 | @Nullable String sandboxId, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 551 | boolean useBlastulaPool, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 552 | @Nullable String[] extraArgs) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 553 | throws ZygoteStartFailedEx { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 554 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 555 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 556 | // --runtime-args, --setuid=, --setgid=, |
| 557 | // and --setgroups= must go first |
| 558 | argsForZygote.add("--runtime-args"); |
| 559 | argsForZygote.add("--setuid=" + uid); |
| 560 | argsForZygote.add("--setgid=" + gid); |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 561 | argsForZygote.add("--runtime-flags=" + runtimeFlags); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 562 | if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) { |
| 563 | argsForZygote.add("--mount-external-default"); |
| 564 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) { |
| 565 | argsForZygote.add("--mount-external-read"); |
| 566 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) { |
| 567 | argsForZygote.add("--mount-external-write"); |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 568 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) { |
| 569 | argsForZygote.add("--mount-external-full"); |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 570 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) { |
| 571 | argsForZygote.add("--mount-external-installer"); |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 572 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_LEGACY) { |
| 573 | argsForZygote.add("--mount-external-legacy"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 574 | } |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 575 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 576 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 577 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 578 | // --setgroups is a comma-separated list |
| 579 | if (gids != null && gids.length > 0) { |
| 580 | StringBuilder sb = new StringBuilder(); |
| 581 | sb.append("--setgroups="); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 582 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 583 | int sz = gids.length; |
| 584 | for (int i = 0; i < sz; i++) { |
| 585 | if (i != 0) { |
| 586 | sb.append(','); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 587 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 588 | sb.append(gids[i]); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 591 | argsForZygote.add(sb.toString()); |
| 592 | } |
| 593 | |
| 594 | if (niceName != null) { |
| 595 | argsForZygote.add("--nice-name=" + niceName); |
| 596 | } |
| 597 | |
| 598 | if (seInfo != null) { |
| 599 | argsForZygote.add("--seinfo=" + seInfo); |
| 600 | } |
| 601 | |
| 602 | if (instructionSet != null) { |
| 603 | argsForZygote.add("--instruction-set=" + instructionSet); |
| 604 | } |
| 605 | |
| 606 | if (appDataDir != null) { |
| 607 | argsForZygote.add("--app-data-dir=" + appDataDir); |
| 608 | } |
| 609 | |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 610 | if (invokeWith != null) { |
| 611 | argsForZygote.add("--invoke-with"); |
| 612 | argsForZygote.add(invokeWith); |
| 613 | } |
| 614 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 615 | if (startChildZygote) { |
| 616 | argsForZygote.add("--start-child-zygote"); |
| 617 | } |
| 618 | |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 619 | if (packageName != null) { |
| 620 | argsForZygote.add("--package-name=" + packageName); |
| 621 | } |
| 622 | |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 623 | if (packagesForUid != null && packagesForUid.length > 0) { |
| 624 | final StringBuilder sb = new StringBuilder(); |
| 625 | sb.append("--packages-for-uid="); |
| 626 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 627 | // TODO (chriswailes): Replace with String.join |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 628 | for (int i = 0; i < packagesForUid.length; ++i) { |
| 629 | if (i != 0) { |
| 630 | sb.append(','); |
| 631 | } |
| 632 | sb.append(packagesForUid[i]); |
| 633 | } |
| 634 | argsForZygote.add(sb.toString()); |
| 635 | } |
| 636 | |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 637 | if (sandboxId != null) { |
| 638 | argsForZygote.add("--sandbox-id=" + sandboxId); |
| 639 | } |
| 640 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 641 | argsForZygote.add(processClass); |
| 642 | |
| 643 | if (extraArgs != null) { |
| 644 | for (String arg : extraArgs) { |
| 645 | argsForZygote.add(arg); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 646 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 647 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 648 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 649 | synchronized(mLock) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 650 | return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 651 | useBlastulaPool && mBlastulaPoolEnabled, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 652 | argsForZygote); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 656 | private boolean fetchBlastulaPoolEnabledProp() { |
| 657 | boolean origVal = mBlastulaPoolEnabled; |
| 658 | |
| 659 | final String propertyString = |
| 660 | Zygote.getSystemProperty( |
| 661 | DeviceConfig.RuntimeNative.BLASTULA_POOL_ENABLED, |
| 662 | BLASTULA_POOL_ENABLED_DEFAULT); |
| 663 | |
| 664 | if (!propertyString.isEmpty()) { |
| 665 | mBlastulaPoolEnabled = |
| 666 | Zygote.getSystemPropertyBoolean( |
| 667 | DeviceConfig.RuntimeNative.BLASTULA_POOL_ENABLED, |
| 668 | Boolean.parseBoolean(BLASTULA_POOL_ENABLED_DEFAULT)); |
| 669 | } |
| 670 | |
| 671 | return origVal != mBlastulaPoolEnabled; |
| 672 | } |
| 673 | |
| 674 | private long mLastPropCheckTimestamp = 0; |
| 675 | |
| 676 | private boolean fetchBlastulaPoolEnabledPropWithMinInterval() { |
| 677 | final long currentTimestamp = SystemClock.elapsedRealtime(); |
| 678 | |
| 679 | if (currentTimestamp - mLastPropCheckTimestamp >= Zygote.PROPERTY_CHECK_INTERVAL) { |
| 680 | mLastPropCheckTimestamp = currentTimestamp; |
| 681 | return fetchBlastulaPoolEnabledProp(); |
| 682 | } |
| 683 | |
| 684 | return false; |
| 685 | } |
| 686 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 687 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 688 | * Closes the connections to the zygote, if they exist. |
| 689 | */ |
| 690 | public void close() { |
| 691 | if (primaryZygoteState != null) { |
| 692 | primaryZygoteState.close(); |
| 693 | } |
| 694 | if (secondaryZygoteState != null) { |
| 695 | secondaryZygoteState.close(); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 700 | * Tries to establish a connection to the zygote that handles a given {@code abi}. Might block |
| 701 | * and retry if the zygote is unresponsive. This method is a no-op if a connection is |
| 702 | * already open. |
| 703 | */ |
| 704 | public void establishZygoteConnectionForAbi(String abi) { |
| 705 | try { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 706 | synchronized(mLock) { |
| 707 | openZygoteSocketIfNeeded(abi); |
| 708 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 709 | } catch (ZygoteStartFailedEx ex) { |
| 710 | throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /** |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 715 | * Attempt to retrieve the PID of the zygote serving the given abi. |
| 716 | */ |
| 717 | public int getZygotePid(String abi) { |
| 718 | try { |
| 719 | synchronized (mLock) { |
| 720 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 721 | |
| 722 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 723 | state.mZygoteOutputWriter.write("1"); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 724 | // ... followed by a new-line. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 725 | state.mZygoteOutputWriter.newLine(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 726 | // ... followed by our only argument. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 727 | state.mZygoteOutputWriter.write("--get-pid"); |
| 728 | state.mZygoteOutputWriter.newLine(); |
| 729 | state.mZygoteOutputWriter.flush(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 730 | |
| 731 | // The response is a length prefixed stream of ASCII bytes. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 732 | int numBytes = state.mZygoteInputStream.readInt(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 733 | byte[] bytes = new byte[numBytes]; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 734 | state.mZygoteInputStream.readFully(bytes); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 735 | |
| 736 | return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII)); |
| 737 | } |
| 738 | } catch (Exception ex) { |
| 739 | throw new RuntimeException("Failure retrieving pid", ex); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 744 | * Push hidden API blacklisting exemptions into the zygote process(es). |
| 745 | * |
| 746 | * <p>The list of exemptions will take affect for all new processes forked from the zygote after |
| 747 | * this call. |
| 748 | * |
Mathew Inwood | 33d5138 | 2018-04-05 13:56:39 +0100 | [diff] [blame] | 749 | * @param exemptions List of hidden API exemption prefixes. Any matching members are treated as |
| 750 | * whitelisted/public APIs (i.e. allowed, no logging of usage). |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 751 | */ |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 752 | public boolean setApiBlacklistExemptions(List<String> exemptions) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 753 | synchronized (mLock) { |
| 754 | mApiBlacklistExemptions = exemptions; |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 755 | boolean ok = maybeSetApiBlacklistExemptions(primaryZygoteState, true); |
| 756 | if (ok) { |
| 757 | ok = maybeSetApiBlacklistExemptions(secondaryZygoteState, true); |
| 758 | } |
| 759 | return ok; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 763 | /** |
| 764 | * Set the precentage of detected hidden API accesses that are logged to the event log. |
| 765 | * |
| 766 | * <p>This rate will take affect for all new processes forked from the zygote after this call. |
| 767 | * |
| 768 | * @param rate An integer between 0 and 0x10000 inclusive. 0 means no event logging. |
| 769 | */ |
| 770 | public void setHiddenApiAccessLogSampleRate(int rate) { |
| 771 | synchronized (mLock) { |
| 772 | mHiddenApiAccessLogSampleRate = rate; |
| 773 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 774 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 775 | } |
| 776 | } |
| 777 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 778 | @GuardedBy("mLock") |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 779 | private boolean maybeSetApiBlacklistExemptions(ZygoteState state, boolean sendIfEmpty) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 780 | if (state == null || state.isClosed()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 781 | Slog.e(LOG_TAG, "Can't set API blacklist exemptions: no zygote connection"); |
| 782 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 783 | } |
| 784 | if (!sendIfEmpty && mApiBlacklistExemptions.isEmpty()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 785 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 786 | } |
| 787 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 788 | state.mZygoteOutputWriter.write(Integer.toString(mApiBlacklistExemptions.size() + 1)); |
| 789 | state.mZygoteOutputWriter.newLine(); |
| 790 | state.mZygoteOutputWriter.write("--set-api-blacklist-exemptions"); |
| 791 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 792 | for (int i = 0; i < mApiBlacklistExemptions.size(); ++i) { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 793 | state.mZygoteOutputWriter.write(mApiBlacklistExemptions.get(i)); |
| 794 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 795 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 796 | state.mZygoteOutputWriter.flush(); |
| 797 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 798 | if (status != 0) { |
| 799 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions; status " + status); |
| 800 | } |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 801 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 802 | } catch (IOException ioe) { |
| 803 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions", ioe); |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 804 | mApiBlacklistExemptions = Collections.emptyList(); |
| 805 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 809 | private void maybeSetHiddenApiAccessLogSampleRate(ZygoteState state) { |
| 810 | if (state == null || state.isClosed()) { |
| 811 | return; |
| 812 | } |
| 813 | if (mHiddenApiAccessLogSampleRate == -1) { |
| 814 | return; |
| 815 | } |
| 816 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 817 | state.mZygoteOutputWriter.write(Integer.toString(1)); |
| 818 | state.mZygoteOutputWriter.newLine(); |
| 819 | state.mZygoteOutputWriter.write("--hidden-api-log-sampling-rate=" |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 820 | + Integer.toString(mHiddenApiAccessLogSampleRate)); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 821 | state.mZygoteOutputWriter.newLine(); |
| 822 | state.mZygoteOutputWriter.flush(); |
| 823 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 824 | if (status != 0) { |
| 825 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate; status " + status); |
| 826 | } |
| 827 | } catch (IOException ioe) { |
| 828 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate", ioe); |
| 829 | } |
| 830 | } |
| 831 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 832 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 833 | * Tries to open a session socket to a Zygote process with a compatible ABI if one is not |
| 834 | * already open. If a compatible session socket is already open that session socket is returned. |
| 835 | * This function may block and may have to try connecting to multiple Zygotes to find the |
| 836 | * appropriate one. Requires that mLock be held. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 837 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 838 | @GuardedBy("mLock") |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 839 | private ZygoteState openZygoteSocketIfNeeded(String abi) |
| 840 | throws ZygoteStartFailedEx { |
| 841 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 842 | Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held"); |
| 843 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 844 | if (primaryZygoteState == null || primaryZygoteState.isClosed()) { |
| 845 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 846 | primaryZygoteState = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 847 | ZygoteState.connect(mZygoteSocketAddress, mBlastulaPoolSocketAddress); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 848 | } catch (IOException ioe) { |
| 849 | throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe); |
| 850 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 851 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 852 | maybeSetApiBlacklistExemptions(primaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 853 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 854 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 855 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 856 | if (primaryZygoteState.matches(abi)) { |
| 857 | return primaryZygoteState; |
| 858 | } |
| 859 | |
| 860 | // The primary zygote didn't match. Try the secondary. |
| 861 | if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { |
| 862 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 863 | secondaryZygoteState = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 864 | ZygoteState.connect(mZygoteSecondarySocketAddress, |
| 865 | mBlastulaPoolSecondarySocketAddress); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 866 | } catch (IOException ioe) { |
| 867 | throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe); |
| 868 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 869 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 870 | maybeSetApiBlacklistExemptions(secondaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 871 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (secondaryZygoteState.matches(abi)) { |
| 875 | return secondaryZygoteState; |
| 876 | } |
| 877 | |
| 878 | throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); |
| 879 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 880 | |
| 881 | /** |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 882 | * Instructs the zygote to pre-load the application code for the given Application. |
| 883 | * Only the app zygote supports this function. |
| 884 | * TODO preloadPackageForAbi() can probably be removed and the callers an use this instead. |
| 885 | */ |
| 886 | public boolean preloadApp(ApplicationInfo appInfo, String abi) throws ZygoteStartFailedEx, |
| 887 | IOException { |
| 888 | synchronized (mLock) { |
| 889 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 890 | state.mZygoteOutputWriter.write("2"); |
| 891 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 892 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 893 | state.mZygoteOutputWriter.write("--preload-app"); |
| 894 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 895 | |
| 896 | // Zygote args needs to be strings, so in order to pass ApplicationInfo, |
| 897 | // write it to a Parcel, and base64 the raw Parcel bytes to the other side. |
| 898 | Parcel parcel = Parcel.obtain(); |
| 899 | appInfo.writeToParcel(parcel, 0 /* flags */); |
| 900 | String encodedParcelData = Base64.getEncoder().encodeToString(parcel.marshall()); |
| 901 | parcel.recycle(); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 902 | state.mZygoteOutputWriter.write(encodedParcelData); |
| 903 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 904 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 905 | state.mZygoteOutputWriter.flush(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 906 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 907 | return (state.mZygoteInputStream.readInt() == 0); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 912 | * Instructs the zygote to pre-load the classes and native libraries at the given paths |
| 913 | * for the specified abi. Not all zygotes support this function. |
| 914 | */ |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 915 | public boolean preloadPackageForAbi(String packagePath, String libsPath, String libFileName, |
| 916 | String cacheKey, String abi) throws ZygoteStartFailedEx, |
| 917 | IOException { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 918 | synchronized(mLock) { |
| 919 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 920 | state.mZygoteOutputWriter.write("5"); |
| 921 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 922 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 923 | state.mZygoteOutputWriter.write("--preload-package"); |
| 924 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 925 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 926 | state.mZygoteOutputWriter.write(packagePath); |
| 927 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 928 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 929 | state.mZygoteOutputWriter.write(libsPath); |
| 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(libFileName); |
| 933 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 934 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 935 | state.mZygoteOutputWriter.write(cacheKey); |
| 936 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 937 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 938 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | bae484a | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 939 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 940 | return (state.mZygoteInputStream.readInt() == 0); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 941 | } |
| 942 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 943 | |
| 944 | /** |
| 945 | * Instructs the zygote to preload the default set of classes and resources. Returns |
| 946 | * {@code true} if a preload was performed as a result of this call, and {@code false} |
| 947 | * otherwise. The latter usually means that the zygote eagerly preloaded at startup |
| 948 | * or due to a previous call to {@code preloadDefault}. Note that this call is synchronous. |
| 949 | */ |
| 950 | public boolean preloadDefault(String abi) throws ZygoteStartFailedEx, IOException { |
| 951 | synchronized (mLock) { |
| 952 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 953 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 954 | state.mZygoteOutputWriter.write("1"); |
| 955 | state.mZygoteOutputWriter.newLine(); |
| 956 | state.mZygoteOutputWriter.write("--preload-default"); |
| 957 | state.mZygoteOutputWriter.newLine(); |
| 958 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 959 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 960 | return (state.mZygoteInputStream.readInt() == 0); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 961 | } |
| 962 | } |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 963 | |
| 964 | /** |
| 965 | * 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] | 966 | * @param zygoteSocketName The name of the socket to connect to. |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 967 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 968 | public static void waitForConnectionToZygote(String zygoteSocketName) { |
| 969 | final LocalSocketAddress zygoteSocketAddress = |
| 970 | new LocalSocketAddress(zygoteSocketName, LocalSocketAddress.Namespace.RESERVED); |
| 971 | waitForConnectionToZygote(zygoteSocketAddress); |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /** |
| 975 | * 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] | 976 | * @param zygoteSocketAddress The name of the socket to connect to. |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 977 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 978 | public static void waitForConnectionToZygote(LocalSocketAddress zygoteSocketAddress) { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 979 | int numRetries = ZYGOTE_CONNECT_TIMEOUT_MS / ZYGOTE_CONNECT_RETRY_DELAY_MS; |
| 980 | for (int n = numRetries; n >= 0; n--) { |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 981 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 982 | final ZygoteState zs = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 983 | ZygoteState.connect(zygoteSocketAddress, null); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 984 | zs.close(); |
| 985 | return; |
| 986 | } catch (IOException ioe) { |
| 987 | Log.w(LOG_TAG, |
| 988 | "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 989 | } |
| 990 | |
| 991 | try { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 992 | Thread.sleep(ZYGOTE_CONNECT_RETRY_DELAY_MS); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 993 | } catch (InterruptedException ie) { |
| 994 | } |
| 995 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 996 | Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " |
| 997 | + zygoteSocketAddress.getName()); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 998 | } |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 999 | |
| 1000 | /** |
| 1001 | * Starts a new zygote process as a child of this zygote. This is used to create |
| 1002 | * secondary zygotes that inherit data from the zygote that this object |
| 1003 | * communicates with. This returns a new ZygoteProcess representing a connection |
| 1004 | * 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] | 1005 | * |
| 1006 | * @param processClass The class to use as the child zygote's main entry |
| 1007 | * point. |
| 1008 | * @param niceName A more readable name to use for the process. |
| 1009 | * @param uid The user-id under which the child zygote will run. |
| 1010 | * @param gid The group-id under which the child zygote will run. |
| 1011 | * @param gids Additional group-ids associated with the child zygote process. |
| 1012 | * @param runtimeFlags Additional flags. |
| 1013 | * @param seInfo null-ok SELinux information for the child zygote process. |
| 1014 | * @param abi non-null the ABI of the child zygote |
| 1015 | * @param acceptedAbiList ABIs this child zygote will accept connections for; this |
| 1016 | * may be different from <code>abi</code> in case the children |
| 1017 | * spawned from this Zygote only communicate using ABI-safe methods. |
| 1018 | * @param instructionSet null-ok the instruction set to use. |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1019 | * @param uidRangeStart The first UID in the range the child zygote may setuid()/setgid() to |
| 1020 | * @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] | 1021 | */ |
| 1022 | public ChildZygoteProcess startChildZygote(final String processClass, |
| 1023 | final String niceName, |
| 1024 | int uid, int gid, int[] gids, |
| 1025 | int runtimeFlags, |
| 1026 | String seInfo, |
| 1027 | String abi, |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1028 | String acceptedAbiList, |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1029 | String instructionSet, |
| 1030 | int uidRangeStart, |
| 1031 | int uidRangeEnd) { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1032 | // Create an unguessable address in the global abstract namespace. |
| 1033 | final LocalSocketAddress serverAddress = new LocalSocketAddress( |
| 1034 | processClass + "/" + UUID.randomUUID().toString()); |
| 1035 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1036 | final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName(), |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1037 | Zygote.CHILD_ZYGOTE_ABI_LIST_ARG + acceptedAbiList, |
| 1038 | Zygote.CHILD_ZYGOTE_UID_RANGE_START + uidRangeStart, |
| 1039 | Zygote.CHILD_ZYGOTE_UID_RANGE_END + uidRangeEnd}; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1040 | |
| 1041 | Process.ProcessStartResult result; |
| 1042 | try { |
| 1043 | result = startViaZygote(processClass, niceName, uid, gid, |
| 1044 | gids, runtimeFlags, 0 /* mountExternal */, 0 /* targetSdkVersion */, seInfo, |
| 1045 | abi, instructionSet, null /* appDataDir */, null /* invokeWith */, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 1046 | true /* startChildZygote */, null /* packageName */, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame^] | 1047 | null /* packagesForUid */, null /* sandboxId */, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 1048 | false /* useBlastulaPool */, extraArgs); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1049 | } catch (ZygoteStartFailedEx ex) { |
| 1050 | throw new RuntimeException("Starting child-zygote through Zygote failed", ex); |
| 1051 | } |
| 1052 | |
| 1053 | return new ChildZygoteProcess(serverAddress, result.pid); |
| 1054 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 1055 | } |