blob: 15eb8de5614c3bdae48b359e68a16ac42e980e69 [file] [log] [blame]
Jonathan Dixond3101b12012-04-12 20:51:51 +01001/*
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
17package android.webkit;
18
Ignacio Solla451e3382014-11-10 10:35:54 +000019import android.annotation.SystemApi;
Primiano Tucci810c0522014-07-25 18:03:16 +010020import android.app.ActivityManagerInternal;
Gustav Senntoncd8f2732016-04-14 09:58:36 +010021import android.app.ActivityManagerNative;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070022import android.app.AppGlobals;
Jeff Sharkey85844912014-11-13 16:20:38 -080023import android.app.Application;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070024import android.content.Context;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010025import android.content.pm.ApplicationInfo;
Torne (Richard Coles)0606cd52014-08-05 16:12:09 +010026import android.content.pm.PackageInfo;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070027import android.content.pm.PackageManager;
Gustav Senntoncd8f2732016-04-14 09:58:36 +010028import android.content.pm.Signature;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010029import android.os.Build;
30import android.os.Process;
31import android.os.RemoteException;
32import android.os.ServiceManager;
Ben Murdoche09e9762012-07-19 14:48:13 +010033import android.os.StrictMode;
Ben Murdoch5ced5022014-07-28 15:57:00 +010034import android.os.SystemProperties;
Torne (Richard Coles)38228822014-08-13 17:11:45 +010035import android.os.Trace;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010036import android.text.TextUtils;
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010037import android.util.AndroidRuntimeException;
Gustav Senntoncd8f2732016-04-14 09:58:36 +010038import android.util.ArraySet;
Jonathan Dixond3101b12012-04-12 20:51:51 +010039import android.util.Log;
Jeff Sharkey85844912014-11-13 16:20:38 -080040
Primiano Tucci810c0522014-07-25 18:03:16 +010041import com.android.server.LocalServices;
Jeff Sharkey85844912014-11-13 16:20:38 -080042
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010043import dalvik.system.VMRuntime;
44
45import java.io.File;
Simon Baldwinb98082dc2015-05-15 12:56:50 +010046import java.io.IOException;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010047import java.util.Arrays;
Simon Baldwinb98082dc2015-05-15 12:56:50 +010048import java.util.zip.ZipEntry;
49import java.util.zip.ZipFile;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010050
Jonathan Dixond3101b12012-04-12 20:51:51 +010051/**
52 * Top level factory, used creating all the main WebView implementation classes.
Jared Dukeb0e35842013-03-19 16:25:39 -070053 *
54 * @hide
Jonathan Dixond3101b12012-04-12 20:51:51 +010055 */
Ignacio Solla451e3382014-11-10 10:35:54 +000056@SystemApi
Jared Dukeb0e35842013-03-19 16:25:39 -070057public final class WebViewFactory {
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070058
Ben Murdoche09e9762012-07-19 14:48:13 +010059 private static final String CHROMIUM_WEBVIEW_FACTORY =
Torne (Richard Coles)a9bbd942012-10-24 11:59:22 +010060 "com.android.webview.chromium.WebViewChromiumFactoryProvider";
Jonathan Dixond3101b12012-04-12 20:51:51 +010061
Ben Murdoch0e04bcf2014-05-16 13:41:12 +010062 private static final String NULL_WEBVIEW_FACTORY =
63 "com.android.webview.nullwebview.NullWebViewFactoryProvider";
64
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010065 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 Murdoch5ced5022014-07-28 15:57:00 +010070 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 Tucci1b7977b2014-07-25 19:19:32 +010073
Jonathan Dixond3101b12012-04-12 20:51:51 +010074 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 Reck9f9d3452012-09-20 13:18:59 -070081 private static final Object sProviderLock = new Object();
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010082 private static boolean sAddressSpaceReserved = false;
Torne (Richard Coles)84392d72014-08-14 16:43:18 +010083 private static PackageInfo sPackageInfo;
Jonathan Dixond3101b12012-04-12 20:51:51 +010084
Gustav Sennton85edb6c2015-04-15 11:54:20 +010085 // 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 Sennton6258dcd2015-10-30 19:25:37 +000089
90 // error codes for waiting for WebView preparation
Gustav Sennton85edb6c2015-04-15 11:54:20 +010091 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 Sennton6258dcd2015-10-30 19:25:37 +000099 // more error codes for waiting for WebView preparation
Gustav Sennton26c82ff2016-03-11 13:06:40 +0000100 public static final int LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN = 8;
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000101
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000102 // error for namespace lookup
103 public static final int LIBLOAD_FAILED_TO_FIND_NAMESPACE = 10;
104
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000105 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 Sennton6258dcd2015-10-30 19:25:37 +0000111 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 Senntona8366e72015-04-17 11:24:07 +0100121 public MissingWebViewPackageException(String message) { super(message); }
122 public MissingWebViewPackageException(Exception e) { super(e); }
123 }
124
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100125 /**
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100126 * @hide
127 */
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000128 public static String getWebViewLibrary(ApplicationInfo ai) {
Gustav Sennton2ed6fee2015-03-03 15:12:34 +0000129 if (ai.metaData != null)
130 return ai.metaData.getString("com.android.webview.WebViewLibrary");
131 return null;
Ben Murdochdc00a842014-07-17 14:55:00 +0100132 }
133
Torne (Richard Coles)84392d72014-08-14 16:43:18 +0100134 public static PackageInfo getLoadedPackageInfo() {
135 return sPackageInfo;
136 }
137
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100138 /**
139 * Load the native library for the given package name iff that package
Gustav Senntond97301822015-06-18 16:56:26 +0100140 * name is the same as the one providing the webview.
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100141 */
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000142 public static int loadWebViewNativeLibraryFromPackage(String packageName,
143 ClassLoader clazzLoader) {
Gustav Senntonb088cb32016-06-17 14:02:52 +0100144 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 Sennton6ce92c92015-10-23 11:10:39 +0100150 }
Gustav Senntonb088cb32016-06-17 14:02:52 +0100151
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 Sennton6258dcd2015-10-30 19:25:37 +0000158 return LIBLOAD_WRONG_PACKAGE_NAME;
Gustav Senntonb088cb32016-06-17 14:02:52 +0100159 }
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 Sennton6ce92c92015-10-23 11:10:39 +0100171
Gustav Senntonfd07efa2016-05-23 13:09:03 +0100172 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 Senntonb088cb32016-06-17 14:02:52 +0100175 if (loadNativeRet == LIBLOAD_SUCCESS) return response.status;
Gustav Senntonfd07efa2016-05-23 13:09:03 +0100176 return loadNativeRet;
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100177 }
178
John Reck9f9d3452012-09-20 13:18:59 -0700179 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)d892afc2013-10-14 17:14:04 +0100182 // us honest and minimize usage of WebView internals when binding the proxy.
John Reck9f9d3452012-09-20 13:18:59 -0700183 if (sProviderInstance != null) return sProviderInstance;
Jonathan Dixond3101b12012-04-12 20:51:51 +0100184
Jeff Sharkey85844912014-11-13 16:20:38 -0800185 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 Sennton5df5e222016-02-25 18:20:12 +0000191 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100192 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()");
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100193 try {
Gustav Senntona8366e72015-04-17 11:24:07 +0100194 Class<WebViewFactoryProvider> providerClass = getProviderClass();
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100195
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100196 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "providerClass.newInstance()");
197 try {
Ignacio Solla1ea39b32014-11-10 14:08:37 +0000198 sProviderInstance = providerClass.getConstructor(WebViewDelegate.class)
199 .newInstance(new WebViewDelegate());
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100200 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)38228822014-08-13 17:11:45 +0100207 }
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100208 } finally {
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100209 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Gustav Sennton5df5e222016-02-25 18:20:12 +0000210 StrictMode.setThreadPolicy(oldPolicy);
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100211 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100212 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100213 }
214
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100215 /**
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 Shub5f554a2016-04-20 17:17:44 -0700244 + "version code is lower than expected: " + chosen.versionCode
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100245 + " 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 Senntonfd07efa2016-05-23 13:09:03 +0100268 if (response.status != LIBLOAD_SUCCESS
269 && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) {
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100270 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 Senntonb088cb32016-06-17 14:02:52 +0100312 sPackageInfo = newPackageInfo;
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100313 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 Senntona8366e72015-04-17 11:24:07 +0100322 private static Class<WebViewFactoryProvider> getProviderClass() {
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100323 Context webViewContext = null;
324 Application initialApplication = AppGlobals.getInitialApplication();
325
Gustav Senntona8366e72015-04-17 11:24:07 +0100326 try {
Gustav Senntonfc424472016-01-06 17:11:09 +0000327 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100328 "WebViewFactory.getWebViewContextAndSetProvider()");
Gustav Senntonfc424472016-01-06 17:11:09 +0000329 try {
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100330 webViewContext = getWebViewContextAndSetProvider();
Gustav Senntonfc424472016-01-06 17:11:09 +0000331 } finally {
332 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000333 }
Gustav Senntona8366e72015-04-17 11:24:07 +0100334 Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " +
Gustav Senntoncd8f2732016-04-14 09:58:36 +0100335 sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000336
Gustav Senntona8366e72015-04-17 11:24:07 +0100337 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()");
338 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000339 initialApplication.getAssets().addAssetPathAsSharedLibrary(
340 webViewContext.getApplicationInfo().sourceDir);
341 ClassLoader clazzLoader = webViewContext.getClassLoader();
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000342
343 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()");
344 loadNativeLibrary(clazzLoader);
345 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
346
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000347 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 Senntona8366e72015-04-17 11:24:07 +0100354 } 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)08cfaf62014-05-08 16:07:05 +0100375 /**
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 Murdoch5ced5022014-07-28 15:57:00 +0100382 long addressSpaceToReserve =
383 SystemProperties.getLong(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY,
384 CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES);
385 sAddressSpaceReserved = nativeReserveAddressSpace(addressSpaceToReserve);
386
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100387 if (sAddressSpaceReserved) {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100388 if (DEBUG) {
389 Log.v(LOGTAG, "address space reserved: " + addressSpaceToReserve + " bytes");
390 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100391 } else {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100392 Log.e(LOGTAG, "reserving " + addressSpaceToReserve +
393 " bytes of address space failed");
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100394 }
Primiano Tucci810c0522014-07-25 18:03:16 +0100395 } catch (Throwable t) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100396 // Log and discard errors at this stage as we must not crash the zygote.
Primiano Tucci810c0522014-07-25 18:03:16 +0100397 Log.e(LOGTAG, "error preparing native loader", t);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100398 }
399 }
400
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000401 private static int prepareWebViewInSystemServer(String[] nativeLibraryPaths) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100402 if (DEBUG) Log.v(LOGTAG, "creating relro files");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000403 int numRelros = 0;
Ben Murdoch5ced5022014-07-28 15:57:00 +0100404
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 Sennton6258dcd2015-10-30 19:25:37 +0000407 // waiting on relro creation.
Ben Murdoch5ced5022014-07-28 15:57:00 +0100408 if (Build.SUPPORTED_32_BIT_ABIS.length > 0) {
409 if (DEBUG) Log.v(LOGTAG, "Create 32 bit relro");
410 createRelroFile(false /* is64Bit */, nativeLibraryPaths);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000411 numRelros++;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100412 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100413
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 Sennton6258dcd2015-10-30 19:25:37 +0000417 numRelros++;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100418 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000419 return numRelros;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100420 }
421
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000422 /**
423 * @hide
424 */
425 public static int onWebViewProviderChanged(PackageInfo packageInfo) {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100426 String[] nativeLibs = null;
427 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000428 nativeLibs = WebViewFactory.getWebViewNativeLibraryPaths(packageInfo);
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100429 if (nativeLibs != null) {
430 long newVmSize = 0L;
Ben Murdoch5ced5022014-07-28 15:57:00 +0100431
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100432 for (String path : nativeLibs) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100433 if (path == null || TextUtils.isEmpty(path)) continue;
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100434 if (DEBUG) Log.d(LOGTAG, "Checking file size of " + path);
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100435 File f = new File(path);
436 if (f.exists()) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100437 newVmSize = Math.max(newVmSize, f.length());
438 continue;
439 }
Simon Baldwin519919b2015-06-11 17:09:49 +0100440 if (path.contains("!/")) {
441 String[] split = TextUtils.split(path, "!/");
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100442 if (split.length == 2) {
Torne (Richard Coles)c5283922015-08-10 16:40:50 +0100443 try (ZipFile z = new ZipFile(split[0])) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100444 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)27cb0d22014-08-08 18:24:12 +0100453 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100454 }
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100455 Log.e(LOGTAG, "error sizing load for " + path);
Ben Murdoch5ced5022014-07-28 15:57:00 +0100456 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100457
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100458 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 Murdoch5ced5022014-07-28 15:57:00 +0100470 }
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100471 } 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 Murdoch5ced5022014-07-28 15:57:00 +0100474 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000475 return prepareWebViewInSystemServer(nativeLibs);
Ben Murdoch5ced5022014-07-28 15:57:00 +0100476 }
477
Gustav Senntona8366e72015-04-17 11:24:07 +0100478 // throws MissingWebViewPackageException
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100479 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)c5283922015-08-10 16:40:50 +0100483 try (ZipFile z = new ZipFile(apkPath)) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100484 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 Baldwin519919b2015-06-11 17:09:49 +0100489 return apkPath + "!/" + entry;
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100490 }
491 }
492 } catch (IOException e) {
493 throw new MissingWebViewPackageException(e);
494 }
495 return "";
496 }
497
498 // throws MissingWebViewPackageException
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000499 private static String[] getWebViewNativeLibraryPaths(PackageInfo packageInfo) {
500 ApplicationInfo ai = packageInfo.applicationInfo;
Gustav Sennton2ed6fee2015-03-03 15:12:34 +0000501 final String NATIVE_LIB_FILE_NAME = getWebViewLibrary(ai);
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100502
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 Baldwinb98082dc2015-05-15 12:56:50 +0100526
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 Tucci1b7977b2014-07-25 19:19:32 +0100549 return new String[] { path32, path64 };
550 }
551
Ben Murdoch5ced5022014-07-28 15:57:00 +0100552 private static void createRelroFile(final boolean is64Bit, String[] nativeLibraryPaths) {
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100553 final String abi =
554 is64Bit ? Build.SUPPORTED_64_BIT_ABIS[0] : Build.SUPPORTED_32_BIT_ABIS[0];
Primiano Tucci810c0522014-07-25 18:03:16 +0100555
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 Tucci1b7977b2014-07-25 19:19:32 +0100561 Log.e(LOGTAG, "relro file creator for " + abi + " crashed. Proceeding without");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000562 getUpdateService().notifyRelroCreationCompleted();
Primiano Tucci810c0522014-07-25 18:03:16 +0100563 } catch (RemoteException e) {
564 Log.e(LOGTAG, "Cannot reach WebViewUpdateService. " + e.getMessage());
565 }
566 }
567 };
568
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100569 try {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100570 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 Tucci161536b2014-07-28 18:51:45 +0100575 int pid = LocalServices.getService(ActivityManagerInternal.class).startIsolatedProcess(
Ben Murdoch5ced5022014-07-28 15:57:00 +0100576 RelroFileCreator.class.getName(), nativeLibraryPaths, "WebViewLoader-" + abi, abi,
Primiano Tucci810c0522014-07-25 18:03:16 +0100577 Process.SHARED_RELRO_UID, crashHandler);
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100578 if (pid <= 0) throw new Exception("Failed to start the relro file creator process");
Primiano Tucci810c0522014-07-25 18:03:16 +0100579 } catch (Throwable t) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100580 // Log and discard errors as we must not crash the system server.
Primiano Tucci810c0522014-07-25 18:03:16 +0100581 Log.e(LOGTAG, "error starting relro file creator for abi " + abi, t);
582 crashHandler.run();
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100583 }
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 Tucci161536b2014-07-28 18:51:45 +0100589 boolean result = false;
590 boolean is64Bit = VMRuntime.getRuntime().is64Bit();
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100591 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 Tucci1b7977b2014-07-25 19:19:32 +0100596 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 Tucci161536b2014-07-28 18:51:45 +0100602 result = nativeCreateRelroFile(args[0] /* path32 */,
603 args[1] /* path64 */,
604 CHROMIUM_WEBVIEW_NATIVE_RELRO_32,
605 CHROMIUM_WEBVIEW_NATIVE_RELRO_64);
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100606 if (result && DEBUG) Log.v(LOGTAG, "created relro file");
Primiano Tucci161536b2014-07-28 18:51:45 +0100607 } finally {
608 // We must do our best to always notify the update service, even if something fails.
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100609 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000610 getUpdateService().notifyRelroCreationCompleted();
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100611 } catch (RemoteException e) {
612 Log.e(LOGTAG, "error notifying update service", e);
613 }
Primiano Tucci161536b2014-07-28 18:51:45 +0100614
615 if (!result) Log.e(LOGTAG, "failed to create relro file");
616
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100617 // Must explicitly exit or else this process will just sit around after we return.
618 System.exit(0);
619 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100620 }
621 }
622
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000623 // Assumes that we have waited for relro creation and set sPackageInfo
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000624 private static int loadNativeLibrary(ClassLoader clazzLoader) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100625 if (!sAddressSpaceReserved) {
626 Log.e(LOGTAG, "can't load with relro file; address space not reserved");
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100627 return LIBLOAD_ADDRESS_SPACE_NOT_RESERVED;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100628 }
629
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000630 String[] args = getWebViewNativeLibraryPaths(sPackageInfo);
631 int result = nativeLoadWithRelroFile(args[0] /* path32 */,
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000632 args[1] /* path64 */,
633 CHROMIUM_WEBVIEW_NATIVE_RELRO_32,
634 CHROMIUM_WEBVIEW_NATIVE_RELRO_64,
635 clazzLoader);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000636 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)08cfaf62014-05-08 16:07:05 +0100640 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000641 return result;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100642 }
643
Gustav Sennton1c177d82016-03-29 20:43:11 +0100644 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)08cfaf62014-05-08 16:07:05 +0100650 }
651
Ben Murdoch5ced5022014-07-28 15:57:00 +0100652 private static native boolean nativeReserveAddressSpace(long addressSpaceToReserve);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100653 private static native boolean nativeCreateRelroFile(String lib32, String lib64,
654 String relro32, String relro64);
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100655 private static native int nativeLoadWithRelroFile(String lib32, String lib64,
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000656 String relro32, String relro64,
657 ClassLoader clazzLoader);
Jonathan Dixond3101b12012-04-12 20:51:51 +0100658}