Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.webkit; |
| 18 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 20 | import android.app.ActivityManagerInternal; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 23 | import android.app.Application; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Torne (Richard Coles) | 0606cd5 | 2014-08-05 16:12:09 +0100 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 28 | import android.content.pm.Signature; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 29 | import android.os.Build; |
| 30 | import android.os.Process; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 33 | import android.os.StrictMode; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 34 | import android.os.SystemProperties; |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 35 | import android.os.Trace; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 36 | import android.text.TextUtils; |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 37 | import android.util.AndroidRuntimeException; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 38 | import android.util.ArraySet; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 39 | import android.util.Log; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 40 | |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 41 | import com.android.server.LocalServices; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 42 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 43 | import dalvik.system.VMRuntime; |
| 44 | |
| 45 | import java.io.File; |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 46 | import java.io.IOException; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 47 | import java.util.Arrays; |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 48 | import java.util.zip.ZipEntry; |
| 49 | import java.util.zip.ZipFile; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 50 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 51 | /** |
| 52 | * Top level factory, used creating all the main WebView implementation classes. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 53 | * |
| 54 | * @hide |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 55 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 56 | @SystemApi |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 57 | public final class WebViewFactory { |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 58 | |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 59 | private static final String CHROMIUM_WEBVIEW_FACTORY = |
Torne (Richard Coles) | a9bbd94 | 2012-10-24 11:59:22 +0100 | [diff] [blame] | 60 | "com.android.webview.chromium.WebViewChromiumFactoryProvider"; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 61 | |
Ben Murdoch | 0e04bcf | 2014-05-16 13:41:12 +0100 | [diff] [blame] | 62 | private static final String NULL_WEBVIEW_FACTORY = |
| 63 | "com.android.webview.nullwebview.NullWebViewFactoryProvider"; |
| 64 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 65 | private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_32 = |
| 66 | "/data/misc/shared_relro/libwebviewchromium32.relro"; |
| 67 | private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_64 = |
| 68 | "/data/misc/shared_relro/libwebviewchromium64.relro"; |
| 69 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 70 | public static final String CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY = |
| 71 | "persist.sys.webview.vmsize"; |
| 72 | private static final long CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES = 100 * 1024 * 1024; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 73 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 74 | private static final String LOGTAG = "WebViewFactory"; |
| 75 | |
| 76 | private static final boolean DEBUG = false; |
| 77 | |
| 78 | // Cache the factory both for efficiency, and ensure any one process gets all webviews from the |
| 79 | // same provider. |
| 80 | private static WebViewFactoryProvider sProviderInstance; |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 81 | private static final Object sProviderLock = new Object(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 82 | private static boolean sAddressSpaceReserved = false; |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 83 | private static PackageInfo sPackageInfo; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 84 | |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 85 | // Error codes for loadWebViewNativeLibraryFromPackage |
| 86 | public static final int LIBLOAD_SUCCESS = 0; |
| 87 | public static final int LIBLOAD_WRONG_PACKAGE_NAME = 1; |
| 88 | public static final int LIBLOAD_ADDRESS_SPACE_NOT_RESERVED = 2; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 89 | |
| 90 | // error codes for waiting for WebView preparation |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 91 | public static final int LIBLOAD_FAILED_WAITING_FOR_RELRO = 3; |
| 92 | public static final int LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES = 4; |
| 93 | |
| 94 | // native relro loading error codes |
| 95 | public static final int LIBLOAD_FAILED_TO_OPEN_RELRO_FILE = 5; |
| 96 | public static final int LIBLOAD_FAILED_TO_LOAD_LIBRARY = 6; |
| 97 | public static final int LIBLOAD_FAILED_JNI_CALL = 7; |
| 98 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 99 | // more error codes for waiting for WebView preparation |
Gustav Sennton | 26c82ff | 2016-03-11 13:06:40 +0000 | [diff] [blame] | 100 | public static final int LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN = 8; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 101 | |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 102 | // error for namespace lookup |
| 103 | public static final int LIBLOAD_FAILED_TO_FIND_NAMESPACE = 10; |
| 104 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 105 | private static String getWebViewPreparationErrorReason(int error) { |
| 106 | switch (error) { |
| 107 | case LIBLOAD_FAILED_WAITING_FOR_RELRO: |
| 108 | return "Time out waiting for Relro files being created"; |
| 109 | case LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES: |
| 110 | return "No WebView installed"; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 111 | case LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN: |
| 112 | return "Crashed for unknown reason"; |
| 113 | } |
| 114 | return "Unknown"; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @hide |
| 119 | */ |
| 120 | public static class MissingWebViewPackageException extends AndroidRuntimeException { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 121 | public MissingWebViewPackageException(String message) { super(message); } |
| 122 | public MissingWebViewPackageException(Exception e) { super(e); } |
| 123 | } |
| 124 | |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 125 | /** |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 126 | * @hide |
| 127 | */ |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 128 | public static String getWebViewLibrary(ApplicationInfo ai) { |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 129 | if (ai.metaData != null) |
| 130 | return ai.metaData.getString("com.android.webview.WebViewLibrary"); |
| 131 | return null; |
Ben Murdoch | dc00a84 | 2014-07-17 14:55:00 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 134 | public static PackageInfo getLoadedPackageInfo() { |
| 135 | return sPackageInfo; |
| 136 | } |
| 137 | |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 138 | /** |
| 139 | * Load the native library for the given package name iff that package |
Gustav Sennton | d9730182 | 2015-06-18 16:56:26 +0100 | [diff] [blame] | 140 | * name is the same as the one providing the webview. |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 141 | */ |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 142 | public static int loadWebViewNativeLibraryFromPackage(String packageName, |
| 143 | ClassLoader clazzLoader) { |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 144 | WebViewProviderResponse response = null; |
| 145 | try { |
| 146 | response = getUpdateService().waitForAndGetProvider(); |
| 147 | } catch (RemoteException e) { |
| 148 | Log.e(LOGTAG, "error waiting for relro creation", e); |
| 149 | return LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN; |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 150 | } |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 151 | |
| 152 | |
| 153 | if (response.status != LIBLOAD_SUCCESS |
| 154 | && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) { |
| 155 | return response.status; |
| 156 | } |
| 157 | if (!response.packageInfo.packageName.equals(packageName)) { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 158 | return LIBLOAD_WRONG_PACKAGE_NAME; |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | PackageManager packageManager = AppGlobals.getInitialApplication().getPackageManager(); |
| 162 | PackageInfo packageInfo; |
| 163 | try { |
| 164 | packageInfo = packageManager.getPackageInfo(packageName, |
| 165 | PackageManager.GET_META_DATA | PackageManager.MATCH_DEBUG_TRIAGED_MISSING); |
| 166 | } catch (PackageManager.NameNotFoundException e) { |
| 167 | Log.e(LOGTAG, "Couldn't find package " + packageName); |
| 168 | return LIBLOAD_WRONG_PACKAGE_NAME; |
| 169 | } |
| 170 | sPackageInfo = packageInfo; |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 171 | |
Gustav Sennton | fd07efa | 2016-05-23 13:09:03 +0100 | [diff] [blame] | 172 | int loadNativeRet = loadNativeLibrary(clazzLoader); |
| 173 | // If we failed waiting for relro we want to return that fact even if we successfully load |
| 174 | // the relro file. |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 175 | if (loadNativeRet == LIBLOAD_SUCCESS) return response.status; |
Gustav Sennton | fd07efa | 2016-05-23 13:09:03 +0100 | [diff] [blame] | 176 | return loadNativeRet; |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 177 | } |
| 178 | |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 179 | static WebViewFactoryProvider getProvider() { |
| 180 | synchronized (sProviderLock) { |
| 181 | // For now the main purpose of this function (and the factory abstraction) is to keep |
Torne (Richard Coles) | d892afc | 2013-10-14 17:14:04 +0100 | [diff] [blame] | 182 | // us honest and minimize usage of WebView internals when binding the proxy. |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 183 | if (sProviderInstance != null) return sProviderInstance; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 184 | |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 185 | final int uid = android.os.Process.myUid(); |
| 186 | if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) { |
| 187 | throw new UnsupportedOperationException( |
| 188 | "For security reasons, WebView is not allowed in privileged processes"); |
| 189 | } |
| 190 | |
Gustav Sennton | 5df5e22 | 2016-02-25 18:20:12 +0000 | [diff] [blame] | 191 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 192 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()"); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 193 | try { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 194 | Class<WebViewFactoryProvider> providerClass = getProviderClass(); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 195 | |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 196 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "providerClass.newInstance()"); |
| 197 | try { |
Ignacio Solla | 1ea39b3 | 2014-11-10 14:08:37 +0000 | [diff] [blame] | 198 | sProviderInstance = providerClass.getConstructor(WebViewDelegate.class) |
| 199 | .newInstance(new WebViewDelegate()); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 200 | if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); |
| 201 | return sProviderInstance; |
| 202 | } catch (Exception e) { |
| 203 | Log.e(LOGTAG, "error instantiating provider", e); |
| 204 | throw new AndroidRuntimeException(e); |
| 205 | } finally { |
| 206 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 207 | } |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 208 | } finally { |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 209 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Gustav Sennton | 5df5e22 | 2016-02-25 18:20:12 +0000 | [diff] [blame] | 210 | StrictMode.setThreadPolicy(oldPolicy); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 211 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 212 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 215 | /** |
| 216 | * Returns true if the signatures match, false otherwise |
| 217 | */ |
| 218 | private static boolean signaturesEquals(Signature[] s1, Signature[] s2) { |
| 219 | if (s1 == null) { |
| 220 | return s2 == null; |
| 221 | } |
| 222 | if (s2 == null) return false; |
| 223 | |
| 224 | ArraySet<Signature> set1 = new ArraySet<>(); |
| 225 | for(Signature signature : s1) { |
| 226 | set1.add(signature); |
| 227 | } |
| 228 | ArraySet<Signature> set2 = new ArraySet<>(); |
| 229 | for(Signature signature : s2) { |
| 230 | set2.add(signature); |
| 231 | } |
| 232 | return set1.equals(set2); |
| 233 | } |
| 234 | |
| 235 | // Throws MissingWebViewPackageException on failure |
| 236 | private static void verifyPackageInfo(PackageInfo chosen, PackageInfo toUse) { |
| 237 | if (!chosen.packageName.equals(toUse.packageName)) { |
| 238 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
| 239 | + "packageName mismatch, expected: " |
| 240 | + chosen.packageName + " actual: " + toUse.packageName); |
| 241 | } |
| 242 | if (chosen.versionCode > toUse.versionCode) { |
| 243 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
Hui Shu | b5f554a | 2016-04-20 17:17:44 -0700 | [diff] [blame] | 244 | + "version code is lower than expected: " + chosen.versionCode |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 245 | + " actual: " + toUse.versionCode); |
| 246 | } |
| 247 | if (getWebViewLibrary(toUse.applicationInfo) == null) { |
| 248 | throw new MissingWebViewPackageException("Tried to load an invalid WebView provider: " |
| 249 | + toUse.packageName); |
| 250 | } |
| 251 | if (!signaturesEquals(chosen.signatures, toUse.signatures)) { |
| 252 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
| 253 | + "signature mismatch"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | private static Context getWebViewContextAndSetProvider() { |
| 258 | Application initialApplication = AppGlobals.getInitialApplication(); |
| 259 | try { |
| 260 | WebViewProviderResponse response = null; |
| 261 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
| 262 | "WebViewUpdateService.waitForAndGetProvider()"); |
| 263 | try { |
| 264 | response = getUpdateService().waitForAndGetProvider(); |
| 265 | } finally { |
| 266 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 267 | } |
Gustav Sennton | fd07efa | 2016-05-23 13:09:03 +0100 | [diff] [blame] | 268 | if (response.status != LIBLOAD_SUCCESS |
| 269 | && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 270 | throw new MissingWebViewPackageException("Failed to load WebView provider: " |
| 271 | + getWebViewPreparationErrorReason(response.status)); |
| 272 | } |
| 273 | // Register to be killed before fetching package info - so that we will be |
| 274 | // killed if the package info goes out-of-date. |
| 275 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()"); |
| 276 | try { |
| 277 | ActivityManagerNative.getDefault().addPackageDependency( |
| 278 | response.packageInfo.packageName); |
| 279 | } finally { |
| 280 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 281 | } |
| 282 | // Fetch package info and verify it against the chosen package |
| 283 | PackageInfo newPackageInfo = null; |
| 284 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()"); |
| 285 | try { |
| 286 | newPackageInfo = initialApplication.getPackageManager().getPackageInfo( |
| 287 | response.packageInfo.packageName, |
| 288 | PackageManager.GET_SHARED_LIBRARY_FILES |
| 289 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING |
| 290 | // Make sure that we fetch the current provider even if its not |
| 291 | // installed for the current user |
| 292 | | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 293 | // Fetch signatures for verification |
| 294 | | PackageManager.GET_SIGNATURES |
| 295 | // Get meta-data for meta data flag verification |
| 296 | | PackageManager.GET_META_DATA); |
| 297 | } finally { |
| 298 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 299 | } |
| 300 | |
| 301 | // Validate the newly fetched package info, throws MissingWebViewPackageException on |
| 302 | // failure |
| 303 | verifyPackageInfo(response.packageInfo, newPackageInfo); |
| 304 | |
| 305 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
| 306 | "initialApplication.createApplicationContext"); |
| 307 | try { |
| 308 | // Construct an app context to load the Java code into the current app. |
| 309 | Context webViewContext = initialApplication.createApplicationContext( |
| 310 | newPackageInfo.applicationInfo, |
| 311 | Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 312 | sPackageInfo = newPackageInfo; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 313 | return webViewContext; |
| 314 | } finally { |
| 315 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 316 | } |
| 317 | } catch (RemoteException | PackageManager.NameNotFoundException e) { |
| 318 | throw new MissingWebViewPackageException("Failed to load WebView provider: " + e); |
| 319 | } |
| 320 | } |
| 321 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 322 | private static Class<WebViewFactoryProvider> getProviderClass() { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 323 | Context webViewContext = null; |
| 324 | Application initialApplication = AppGlobals.getInitialApplication(); |
| 325 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 326 | try { |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 327 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 328 | "WebViewFactory.getWebViewContextAndSetProvider()"); |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 329 | try { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 330 | webViewContext = getWebViewContextAndSetProvider(); |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 331 | } finally { |
| 332 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 333 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 334 | Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " + |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 335 | sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")"); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 336 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 337 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()"); |
| 338 | try { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 339 | initialApplication.getAssets().addAssetPathAsSharedLibrary( |
| 340 | webViewContext.getApplicationInfo().sourceDir); |
| 341 | ClassLoader clazzLoader = webViewContext.getClassLoader(); |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 342 | |
| 343 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()"); |
| 344 | loadNativeLibrary(clazzLoader); |
| 345 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 346 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 347 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()"); |
| 348 | try { |
| 349 | return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY, |
| 350 | true, clazzLoader); |
| 351 | } finally { |
| 352 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 353 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 354 | } catch (ClassNotFoundException e) { |
| 355 | Log.e(LOGTAG, "error loading provider", e); |
| 356 | throw new AndroidRuntimeException(e); |
| 357 | } finally { |
| 358 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 359 | } |
| 360 | } catch (MissingWebViewPackageException e) { |
| 361 | // If the package doesn't exist, then try loading the null WebView instead. |
| 362 | // If that succeeds, then this is a device without WebView support; if it fails then |
| 363 | // swallow the failure, complain that the real WebView is missing and rethrow the |
| 364 | // original exception. |
| 365 | try { |
| 366 | return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY); |
| 367 | } catch (ClassNotFoundException e2) { |
| 368 | // Ignore. |
| 369 | } |
| 370 | Log.e(LOGTAG, "Chromium WebView package does not exist", e); |
| 371 | throw new AndroidRuntimeException(e); |
| 372 | } |
| 373 | } |
| 374 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 375 | /** |
| 376 | * Perform any WebView loading preparations that must happen in the zygote. |
| 377 | * Currently, this means allocating address space to load the real JNI library later. |
| 378 | */ |
| 379 | public static void prepareWebViewInZygote() { |
| 380 | try { |
| 381 | System.loadLibrary("webviewchromium_loader"); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 382 | long addressSpaceToReserve = |
| 383 | SystemProperties.getLong(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY, |
| 384 | CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES); |
| 385 | sAddressSpaceReserved = nativeReserveAddressSpace(addressSpaceToReserve); |
| 386 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 387 | if (sAddressSpaceReserved) { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 388 | if (DEBUG) { |
| 389 | Log.v(LOGTAG, "address space reserved: " + addressSpaceToReserve + " bytes"); |
| 390 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 391 | } else { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 392 | Log.e(LOGTAG, "reserving " + addressSpaceToReserve + |
| 393 | " bytes of address space failed"); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 394 | } |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 395 | } catch (Throwable t) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 396 | // Log and discard errors at this stage as we must not crash the zygote. |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 397 | Log.e(LOGTAG, "error preparing native loader", t); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 401 | private static int prepareWebViewInSystemServer(String[] nativeLibraryPaths) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 402 | if (DEBUG) Log.v(LOGTAG, "creating relro files"); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 403 | int numRelros = 0; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 404 | |
| 405 | // We must always trigger createRelRo regardless of the value of nativeLibraryPaths. Any |
| 406 | // unexpected values will be handled there to ensure that we trigger notifying any process |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 407 | // waiting on relro creation. |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 408 | if (Build.SUPPORTED_32_BIT_ABIS.length > 0) { |
| 409 | if (DEBUG) Log.v(LOGTAG, "Create 32 bit relro"); |
| 410 | createRelroFile(false /* is64Bit */, nativeLibraryPaths); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 411 | numRelros++; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 412 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 413 | |
| 414 | if (Build.SUPPORTED_64_BIT_ABIS.length > 0) { |
| 415 | if (DEBUG) Log.v(LOGTAG, "Create 64 bit relro"); |
| 416 | createRelroFile(true /* is64Bit */, nativeLibraryPaths); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 417 | numRelros++; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 418 | } |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 419 | return numRelros; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 422 | /** |
| 423 | * @hide |
| 424 | */ |
| 425 | public static int onWebViewProviderChanged(PackageInfo packageInfo) { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 426 | String[] nativeLibs = null; |
| 427 | try { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 428 | nativeLibs = WebViewFactory.getWebViewNativeLibraryPaths(packageInfo); |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 429 | if (nativeLibs != null) { |
| 430 | long newVmSize = 0L; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 431 | |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 432 | for (String path : nativeLibs) { |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 433 | if (path == null || TextUtils.isEmpty(path)) continue; |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 434 | if (DEBUG) Log.d(LOGTAG, "Checking file size of " + path); |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 435 | File f = new File(path); |
| 436 | if (f.exists()) { |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 437 | newVmSize = Math.max(newVmSize, f.length()); |
| 438 | continue; |
| 439 | } |
Simon Baldwin | 519919b | 2015-06-11 17:09:49 +0100 | [diff] [blame] | 440 | if (path.contains("!/")) { |
| 441 | String[] split = TextUtils.split(path, "!/"); |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 442 | if (split.length == 2) { |
Torne (Richard Coles) | c528392 | 2015-08-10 16:40:50 +0100 | [diff] [blame] | 443 | try (ZipFile z = new ZipFile(split[0])) { |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 444 | ZipEntry e = z.getEntry(split[1]); |
| 445 | if (e != null && e.getMethod() == ZipEntry.STORED) { |
| 446 | newVmSize = Math.max(newVmSize, e.getSize()); |
| 447 | continue; |
| 448 | } |
| 449 | } |
| 450 | catch (IOException e) { |
| 451 | Log.e(LOGTAG, "error reading APK file " + split[0] + ", ", e); |
| 452 | } |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 453 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 454 | } |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 455 | Log.e(LOGTAG, "error sizing load for " + path); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 456 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 457 | |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 458 | if (DEBUG) { |
| 459 | Log.v(LOGTAG, "Based on library size, need " + newVmSize + |
| 460 | " bytes of address space."); |
| 461 | } |
| 462 | // The required memory can be larger than the file on disk (due to .bss), and an |
| 463 | // upgraded version of the library will likely be larger, so always attempt to |
| 464 | // reserve twice as much as we think to allow for the library to grow during this |
| 465 | // boot cycle. |
| 466 | newVmSize = Math.max(2 * newVmSize, CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES); |
| 467 | Log.d(LOGTAG, "Setting new address space to " + newVmSize); |
| 468 | SystemProperties.set(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY, |
| 469 | Long.toString(newVmSize)); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 470 | } |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 471 | } catch (Throwable t) { |
| 472 | // Log and discard errors at this stage as we must not crash the system server. |
| 473 | Log.e(LOGTAG, "error preparing webview native library", t); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 474 | } |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 475 | return prepareWebViewInSystemServer(nativeLibs); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 478 | // throws MissingWebViewPackageException |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 479 | private static String getLoadFromApkPath(String apkPath, |
| 480 | String[] abiList, |
| 481 | String nativeLibFileName) { |
| 482 | // Search the APK for a native library conforming to a listed ABI. |
Torne (Richard Coles) | c528392 | 2015-08-10 16:40:50 +0100 | [diff] [blame] | 483 | try (ZipFile z = new ZipFile(apkPath)) { |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 484 | for (String abi : abiList) { |
| 485 | final String entry = "lib/" + abi + "/" + nativeLibFileName; |
| 486 | ZipEntry e = z.getEntry(entry); |
| 487 | if (e != null && e.getMethod() == ZipEntry.STORED) { |
| 488 | // Return a path formatted for dlopen() load from APK. |
Simon Baldwin | 519919b | 2015-06-11 17:09:49 +0100 | [diff] [blame] | 489 | return apkPath + "!/" + entry; |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } catch (IOException e) { |
| 493 | throw new MissingWebViewPackageException(e); |
| 494 | } |
| 495 | return ""; |
| 496 | } |
| 497 | |
| 498 | // throws MissingWebViewPackageException |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 499 | private static String[] getWebViewNativeLibraryPaths(PackageInfo packageInfo) { |
| 500 | ApplicationInfo ai = packageInfo.applicationInfo; |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 501 | final String NATIVE_LIB_FILE_NAME = getWebViewLibrary(ai); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 502 | |
| 503 | String path32; |
| 504 | String path64; |
| 505 | boolean primaryArchIs64bit = VMRuntime.is64BitAbi(ai.primaryCpuAbi); |
| 506 | if (!TextUtils.isEmpty(ai.secondaryCpuAbi)) { |
| 507 | // Multi-arch case. |
| 508 | if (primaryArchIs64bit) { |
| 509 | // Primary arch: 64-bit, secondary: 32-bit. |
| 510 | path64 = ai.nativeLibraryDir; |
| 511 | path32 = ai.secondaryNativeLibraryDir; |
| 512 | } else { |
| 513 | // Primary arch: 32-bit, secondary: 64-bit. |
| 514 | path64 = ai.secondaryNativeLibraryDir; |
| 515 | path32 = ai.nativeLibraryDir; |
| 516 | } |
| 517 | } else if (primaryArchIs64bit) { |
| 518 | // Single-arch 64-bit. |
| 519 | path64 = ai.nativeLibraryDir; |
| 520 | path32 = ""; |
| 521 | } else { |
| 522 | // Single-arch 32-bit. |
| 523 | path32 = ai.nativeLibraryDir; |
| 524 | path64 = ""; |
| 525 | } |
Simon Baldwin | b98082dc | 2015-05-15 12:56:50 +0100 | [diff] [blame] | 526 | |
| 527 | // Form the full paths to the extracted native libraries. |
| 528 | // If libraries were not extracted, try load from APK paths instead. |
| 529 | if (!TextUtils.isEmpty(path32)) { |
| 530 | path32 += "/" + NATIVE_LIB_FILE_NAME; |
| 531 | File f = new File(path32); |
| 532 | if (!f.exists()) { |
| 533 | path32 = getLoadFromApkPath(ai.sourceDir, |
| 534 | Build.SUPPORTED_32_BIT_ABIS, |
| 535 | NATIVE_LIB_FILE_NAME); |
| 536 | } |
| 537 | } |
| 538 | if (!TextUtils.isEmpty(path64)) { |
| 539 | path64 += "/" + NATIVE_LIB_FILE_NAME; |
| 540 | File f = new File(path64); |
| 541 | if (!f.exists()) { |
| 542 | path64 = getLoadFromApkPath(ai.sourceDir, |
| 543 | Build.SUPPORTED_64_BIT_ABIS, |
| 544 | NATIVE_LIB_FILE_NAME); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | if (DEBUG) Log.v(LOGTAG, "Native 32-bit lib: " + path32 + ", 64-bit lib: " + path64); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 549 | return new String[] { path32, path64 }; |
| 550 | } |
| 551 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 552 | private static void createRelroFile(final boolean is64Bit, String[] nativeLibraryPaths) { |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 553 | final String abi = |
| 554 | is64Bit ? Build.SUPPORTED_64_BIT_ABIS[0] : Build.SUPPORTED_32_BIT_ABIS[0]; |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 555 | |
| 556 | // crashHandler is invoked by the ActivityManagerService when the isolated process crashes. |
| 557 | Runnable crashHandler = new Runnable() { |
| 558 | @Override |
| 559 | public void run() { |
| 560 | try { |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 561 | Log.e(LOGTAG, "relro file creator for " + abi + " crashed. Proceeding without"); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 562 | getUpdateService().notifyRelroCreationCompleted(); |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 563 | } catch (RemoteException e) { |
| 564 | Log.e(LOGTAG, "Cannot reach WebViewUpdateService. " + e.getMessage()); |
| 565 | } |
| 566 | } |
| 567 | }; |
| 568 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 569 | try { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 570 | if (nativeLibraryPaths == null |
| 571 | || nativeLibraryPaths[0] == null || nativeLibraryPaths[1] == null) { |
| 572 | throw new IllegalArgumentException( |
| 573 | "Native library paths to the WebView RelRo process must not be null!"); |
| 574 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 575 | int pid = LocalServices.getService(ActivityManagerInternal.class).startIsolatedProcess( |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 576 | RelroFileCreator.class.getName(), nativeLibraryPaths, "WebViewLoader-" + abi, abi, |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 577 | Process.SHARED_RELRO_UID, crashHandler); |
Primiano Tucci | e76e81a | 2014-07-29 16:38:33 +0100 | [diff] [blame] | 578 | if (pid <= 0) throw new Exception("Failed to start the relro file creator process"); |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 579 | } catch (Throwable t) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 580 | // Log and discard errors as we must not crash the system server. |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 581 | Log.e(LOGTAG, "error starting relro file creator for abi " + abi, t); |
| 582 | crashHandler.run(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | private static class RelroFileCreator { |
| 587 | // Called in an unprivileged child process to create the relro file. |
| 588 | public static void main(String[] args) { |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 589 | boolean result = false; |
| 590 | boolean is64Bit = VMRuntime.getRuntime().is64Bit(); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 591 | try{ |
| 592 | if (args.length != 2 || args[0] == null || args[1] == null) { |
| 593 | Log.e(LOGTAG, "Invalid RelroFileCreator args: " + Arrays.toString(args)); |
| 594 | return; |
| 595 | } |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 596 | Log.v(LOGTAG, "RelroFileCreator (64bit = " + is64Bit + "), " + |
| 597 | " 32-bit lib: " + args[0] + ", 64-bit lib: " + args[1]); |
| 598 | if (!sAddressSpaceReserved) { |
| 599 | Log.e(LOGTAG, "can't create relro file; address space not reserved"); |
| 600 | return; |
| 601 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 602 | result = nativeCreateRelroFile(args[0] /* path32 */, |
| 603 | args[1] /* path64 */, |
| 604 | CHROMIUM_WEBVIEW_NATIVE_RELRO_32, |
| 605 | CHROMIUM_WEBVIEW_NATIVE_RELRO_64); |
Primiano Tucci | e76e81a | 2014-07-29 16:38:33 +0100 | [diff] [blame] | 606 | if (result && DEBUG) Log.v(LOGTAG, "created relro file"); |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 607 | } finally { |
| 608 | // We must do our best to always notify the update service, even if something fails. |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 609 | try { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 610 | getUpdateService().notifyRelroCreationCompleted(); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 611 | } catch (RemoteException e) { |
| 612 | Log.e(LOGTAG, "error notifying update service", e); |
| 613 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 614 | |
| 615 | if (!result) Log.e(LOGTAG, "failed to create relro file"); |
| 616 | |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 617 | // Must explicitly exit or else this process will just sit around after we return. |
| 618 | System.exit(0); |
| 619 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 623 | // Assumes that we have waited for relro creation and set sPackageInfo |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 624 | private static int loadNativeLibrary(ClassLoader clazzLoader) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 625 | if (!sAddressSpaceReserved) { |
| 626 | Log.e(LOGTAG, "can't load with relro file; address space not reserved"); |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 627 | return LIBLOAD_ADDRESS_SPACE_NOT_RESERVED; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 630 | String[] args = getWebViewNativeLibraryPaths(sPackageInfo); |
| 631 | int result = nativeLoadWithRelroFile(args[0] /* path32 */, |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 632 | args[1] /* path64 */, |
| 633 | CHROMIUM_WEBVIEW_NATIVE_RELRO_32, |
| 634 | CHROMIUM_WEBVIEW_NATIVE_RELRO_64, |
| 635 | clazzLoader); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 636 | if (result != LIBLOAD_SUCCESS) { |
| 637 | Log.w(LOGTAG, "failed to load with relro file, proceeding without"); |
| 638 | } else if (DEBUG) { |
| 639 | Log.v(LOGTAG, "loaded with relro file"); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 640 | } |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 641 | return result; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Gustav Sennton | 1c177d8 | 2016-03-29 20:43:11 +0100 | [diff] [blame] | 644 | private static String WEBVIEW_UPDATE_SERVICE_NAME = "webviewupdate"; |
| 645 | |
| 646 | /** @hide */ |
| 647 | public static IWebViewUpdateService getUpdateService() { |
| 648 | return IWebViewUpdateService.Stub.asInterface( |
| 649 | ServiceManager.getService(WEBVIEW_UPDATE_SERVICE_NAME)); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 652 | private static native boolean nativeReserveAddressSpace(long addressSpaceToReserve); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 653 | private static native boolean nativeCreateRelroFile(String lib32, String lib64, |
| 654 | String relro32, String relro64); |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 655 | private static native int nativeLoadWithRelroFile(String lib32, String lib64, |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 656 | String relro32, String relro64, |
| 657 | ClassLoader clazzLoader); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 658 | } |