Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [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 com.android.internal.os; |
| 18 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 19 | import android.app.ApplicationLoaders; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 20 | import android.net.LocalSocket; |
| 21 | import android.os.Build; |
| 22 | import android.system.ErrnoException; |
| 23 | import android.system.Os; |
Robert Sesek | 602d132 | 2018-01-17 18:48:18 -0500 | [diff] [blame] | 24 | import android.system.OsConstants; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | import android.util.Log; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 27 | import android.webkit.WebViewFactory; |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 28 | import android.webkit.WebViewFactoryProvider; |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 29 | import android.webkit.WebViewLibraryLoader; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 30 | |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 31 | import java.io.DataOutputStream; |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 32 | import java.io.File; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 33 | import java.io.IOException; |
Narayan Kamath | 6f6e894 | 2017-07-06 12:38:55 +0100 | [diff] [blame] | 34 | import java.lang.reflect.Method; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 35 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Startup class for the WebView zygote process. |
| 38 | * |
| 39 | * See {@link ZygoteInit} for generic zygote startup documentation. |
| 40 | * |
| 41 | * @hide |
| 42 | */ |
| 43 | class WebViewZygoteInit { |
| 44 | public static final String TAG = "WebViewZygoteInit"; |
| 45 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 46 | private static ZygoteServer sServer; |
| 47 | |
| 48 | private static class WebViewZygoteServer extends ZygoteServer { |
| 49 | @Override |
| 50 | protected ZygoteConnection createNewConnection(LocalSocket socket, String abiList) |
| 51 | throws IOException { |
| 52 | return new WebViewZygoteConnection(socket, abiList); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private static class WebViewZygoteConnection extends ZygoteConnection { |
| 57 | WebViewZygoteConnection(LocalSocket socket, String abiList) throws IOException { |
| 58 | super(socket, abiList); |
| 59 | } |
| 60 | |
| 61 | @Override |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 62 | protected void preload() { |
| 63 | // Nothing to preload by default. |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected boolean isPreloadComplete() { |
| 68 | // Webview zygotes don't preload any classes or resources or defaults, all of their |
| 69 | // preloading is package specific. |
| 70 | return true; |
Torne (Richard Coles) | bb65893 | 2017-01-05 16:11:06 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 74 | protected void handlePreloadPackage(String packagePath, String libsPath, String libFileName, |
| 75 | String cacheKey) { |
Torne (Richard Coles) | deeb6ad | 2017-04-18 17:16:36 -0400 | [diff] [blame] | 76 | Log.i(TAG, "Beginning package preload"); |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 77 | // Ask ApplicationLoaders to create and cache a classloader for the WebView APK so that |
| 78 | // our children will reuse the same classloader instead of creating their own. |
| 79 | // This enables us to preload Java and native code in the webview zygote process and |
| 80 | // have the preloaded versions actually be used post-fork. |
| 81 | ClassLoader loader = ApplicationLoaders.getDefault().createAndCacheWebViewClassLoader( |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 82 | packagePath, libsPath, cacheKey); |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 83 | |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 84 | // Load the native library using WebViewLibraryLoader to share the RELRO data with other |
| 85 | // processes. |
| 86 | WebViewLibraryLoader.loadNativeLibrary(loader, libFileName); |
| 87 | |
Robert Sesek | 061ee30 | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 88 | // Add the APK to the Zygote's list of allowed files for children. |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 89 | String[] packageList = TextUtils.split(packagePath, File.pathSeparator); |
| 90 | for (String packageEntry : packageList) { |
| 91 | Zygote.nativeAllowFileAcrossFork(packageEntry); |
| 92 | } |
Robert Sesek | 061ee30 | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 93 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 94 | // Once we have the classloader, look up the WebViewFactoryProvider implementation and |
| 95 | // call preloadInZygote() on it to give it the opportunity to preload the native library |
| 96 | // and perform any other initialisation work that should be shared among the children. |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 97 | boolean preloadSucceeded = false; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 98 | try { |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 99 | Class<WebViewFactoryProvider> providerClass = |
| 100 | WebViewFactory.getWebViewProviderClass(loader); |
Narayan Kamath | 6f6e894 | 2017-07-06 12:38:55 +0100 | [diff] [blame] | 101 | Method preloadInZygote = providerClass.getMethod("preloadInZygote"); |
| 102 | preloadInZygote.setAccessible(true); |
| 103 | if (preloadInZygote.getReturnType() != Boolean.TYPE) { |
| 104 | Log.e(TAG, "Unexpected return type: preloadInZygote must return boolean"); |
| 105 | } else { |
| 106 | preloadSucceeded = (boolean) providerClass.getMethod("preloadInZygote") |
| 107 | .invoke(null); |
| 108 | if (!preloadSucceeded) { |
| 109 | Log.e(TAG, "preloadInZygote returned false"); |
| 110 | } |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 111 | } |
Narayan Kamath | 6f6e894 | 2017-07-06 12:38:55 +0100 | [diff] [blame] | 112 | } catch (ReflectiveOperationException e) { |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 113 | Log.e(TAG, "Exception while preloading package", e); |
| 114 | } |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 115 | |
| 116 | try { |
| 117 | DataOutputStream socketOut = getSocketOutputStream(); |
| 118 | socketOut.writeInt(preloadSucceeded ? 1 : 0); |
| 119 | } catch (IOException ioe) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 120 | throw new IllegalStateException("Error writing to command socket", ioe); |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Torne (Richard Coles) | deeb6ad | 2017-04-18 17:16:36 -0400 | [diff] [blame] | 123 | Log.i(TAG, "Package preload done"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 127 | public static void main(String argv[]) { |
Robert Sesek | 602d132 | 2018-01-17 18:48:18 -0500 | [diff] [blame] | 128 | Log.i(TAG, "Starting WebViewZygoteInit"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 129 | |
Robert Sesek | 602d132 | 2018-01-17 18:48:18 -0500 | [diff] [blame] | 130 | String socketName = null; |
| 131 | for (String arg : argv) { |
| 132 | Log.i(TAG, arg); |
| 133 | if (arg.startsWith(Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG)) { |
| 134 | socketName = arg.substring(Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG.length()); |
| 135 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 136 | } |
Robert Sesek | 602d132 | 2018-01-17 18:48:18 -0500 | [diff] [blame] | 137 | if (socketName == null) { |
| 138 | throw new RuntimeException("No " + Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + " specified"); |
| 139 | } |
| 140 | |
| 141 | try { |
| 142 | Os.prctl(OsConstants.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
| 143 | } catch (ErrnoException ex) { |
| 144 | throw new RuntimeException("Failed to set PR_SET_NO_NEW_PRIVS", ex); |
| 145 | } |
| 146 | |
| 147 | sServer = new WebViewZygoteServer(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 148 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 149 | final Runnable caller; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 150 | try { |
Robert Sesek | 602d132 | 2018-01-17 18:48:18 -0500 | [diff] [blame] | 151 | sServer.registerServerSocketAtAbstractName(socketName); |
| 152 | |
| 153 | // Add the abstract socket to the FD whitelist so that the native zygote code |
| 154 | // can properly detach it after forking. |
| 155 | Zygote.nativeAllowFileAcrossFork("ABSTRACT/" + socketName); |
| 156 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 157 | // The select loop returns early in the child process after a fork and |
| 158 | // loops forever in the zygote. |
| 159 | caller = sServer.runSelectLoop(TextUtils.join(",", Build.SUPPORTED_ABIS)); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 160 | } catch (RuntimeException e) { |
| 161 | Log.e(TAG, "Fatal exception:", e); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 162 | throw e; |
| 163 | } finally { |
| 164 | sServer.closeServerSocket(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 167 | // We're in the child process and have exited the select loop. Proceed to execute the |
| 168 | // command. |
| 169 | if (caller != null) { |
| 170 | caller.run(); |
| 171 | } |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 172 | } |
| 173 | } |