blob: d0b47542f6901bfc2d6ed5657f6a8053475a01fe [file] [log] [blame]
Gustav Senntoncd165a92017-06-21 18:04:42 +01001/*
2 * Copyright (C) 2014 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// Uncomment for verbose logging.
18// #define LOG_NDEBUG 0
19#define LOG_TAG "webviewchromiumloader"
20
21#include <dlfcn.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <sys/mman.h>
Richard Uhlerfdd7a7e2018-09-14 09:39:28 +010029#include <sys/prctl.h>
Gustav Senntoncd165a92017-06-21 18:04:42 +010030#include <sys/stat.h>
31#include <sys/types.h>
32
33#include <jni.h>
34#include <android/dlext.h>
35#include <nativeloader/native_loader.h>
36#include <utils/Log.h>
37
38#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
39
40namespace android {
41namespace {
42
43void* gReservedAddress = NULL;
44size_t gReservedSize = 0;
45
46jint LIBLOAD_SUCCESS;
47jint LIBLOAD_FAILED_TO_OPEN_RELRO_FILE;
48jint LIBLOAD_FAILED_TO_LOAD_LIBRARY;
49jint LIBLOAD_FAILED_JNI_CALL;
50jint LIBLOAD_FAILED_TO_FIND_NAMESPACE;
51
52jboolean DoReserveAddressSpace(jlong size) {
53 size_t vsize = static_cast<size_t>(size);
54
55 void* addr = mmap(NULL, vsize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56 if (addr == MAP_FAILED) {
57 ALOGE("Failed to reserve %zd bytes of address space for future load of "
58 "libwebviewchromium.so: %s",
59 vsize, strerror(errno));
60 return JNI_FALSE;
61 }
Richard Uhlerfdd7a7e2018-09-14 09:39:28 +010062 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, vsize, "libwebview reservation");
Gustav Senntoncd165a92017-06-21 18:04:42 +010063 gReservedAddress = addr;
64 gReservedSize = vsize;
65 ALOGV("Reserved %zd bytes at %p", vsize, addr);
66 return JNI_TRUE;
67}
68
69jboolean DoCreateRelroFile(const char* lib, const char* relro) {
70 // Try to unlink the old file, since if this is being called, the old one is
71 // obsolete.
72 if (unlink(relro) != 0 && errno != ENOENT) {
73 // If something went wrong other than the file not existing, log a warning
74 // but continue anyway in the hope that we can successfully overwrite the
75 // existing file with rename() later.
76 ALOGW("Failed to unlink old file %s: %s", relro, strerror(errno));
77 }
78 static const char tmpsuffix[] = ".XXXXXX";
79 char relro_tmp[strlen(relro) + sizeof(tmpsuffix)];
80 strlcpy(relro_tmp, relro, sizeof(relro_tmp));
81 strlcat(relro_tmp, tmpsuffix, sizeof(relro_tmp));
82 int tmp_fd = TEMP_FAILURE_RETRY(mkstemp(relro_tmp));
83 if (tmp_fd == -1) {
84 ALOGE("Failed to create temporary file %s: %s", relro_tmp, strerror(errno));
85 return JNI_FALSE;
86 }
87 android_dlextinfo extinfo;
88 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_WRITE_RELRO;
89 extinfo.reserved_addr = gReservedAddress;
90 extinfo.reserved_size = gReservedSize;
91 extinfo.relro_fd = tmp_fd;
92 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo);
93 int close_result = close(tmp_fd);
94 if (handle == NULL) {
95 ALOGE("Failed to load library %s: %s", lib, dlerror());
96 unlink(relro_tmp);
97 return JNI_FALSE;
98 }
99 if (close_result != 0 ||
100 chmod(relro_tmp, S_IRUSR | S_IRGRP | S_IROTH) != 0 ||
101 rename(relro_tmp, relro) != 0) {
102 ALOGE("Failed to update relro file %s: %s", relro, strerror(errno));
103 unlink(relro_tmp);
104 return JNI_FALSE;
105 }
106 ALOGV("Created relro file %s for library %s", relro, lib);
107 return JNI_TRUE;
108}
109
110jint DoLoadWithRelroFile(JNIEnv* env, const char* lib, const char* relro,
111 jobject clazzLoader) {
112 int relro_fd = TEMP_FAILURE_RETRY(open(relro, O_RDONLY));
113 if (relro_fd == -1) {
114 ALOGE("Failed to open relro file %s: %s", relro, strerror(errno));
115 return LIBLOAD_FAILED_TO_OPEN_RELRO_FILE;
116 }
117 android_namespace_t* ns =
118 android::FindNamespaceByClassLoader(env, clazzLoader);
119 if (ns == NULL) {
120 ALOGE("Failed to find classloader namespace");
121 return LIBLOAD_FAILED_TO_FIND_NAMESPACE;
122 }
123 android_dlextinfo extinfo;
124 extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_USE_RELRO |
125 ANDROID_DLEXT_USE_NAMESPACE;
126 extinfo.reserved_addr = gReservedAddress;
127 extinfo.reserved_size = gReservedSize;
128 extinfo.relro_fd = relro_fd;
129 extinfo.library_namespace = ns;
130 void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo);
131 close(relro_fd);
132 if (handle == NULL) {
133 ALOGE("Failed to load library %s: %s", lib, dlerror());
134 return LIBLOAD_FAILED_TO_LOAD_LIBRARY;
135 }
136 ALOGV("Loaded library %s with relro file %s", lib, relro);
137 return LIBLOAD_SUCCESS;
138}
139
140/******************************************************************************/
141/* JNI wrappers - handle string lifetimes and 32/64 ABI choice */
142/******************************************************************************/
143
144jboolean ReserveAddressSpace(JNIEnv*, jclass, jlong size) {
145 return DoReserveAddressSpace(size);
146}
147
Gustav Senntonae498f22017-10-05 15:34:13 +0100148jboolean CreateRelroFile(JNIEnv* env, jclass, jstring lib, jstring relro) {
Gustav Senntoncd165a92017-06-21 18:04:42 +0100149 jboolean ret = JNI_FALSE;
150 const char* lib_utf8 = env->GetStringUTFChars(lib, NULL);
151 if (lib_utf8 != NULL) {
152 const char* relro_utf8 = env->GetStringUTFChars(relro, NULL);
153 if (relro_utf8 != NULL) {
154 ret = DoCreateRelroFile(lib_utf8, relro_utf8);
155 env->ReleaseStringUTFChars(relro, relro_utf8);
156 }
157 env->ReleaseStringUTFChars(lib, lib_utf8);
158 }
159 return ret;
160}
161
Gustav Senntonae498f22017-10-05 15:34:13 +0100162jint LoadWithRelroFile(JNIEnv* env, jclass, jstring lib, jstring relro,
163 jobject clazzLoader) {
Gustav Senntoncd165a92017-06-21 18:04:42 +0100164 jint ret = LIBLOAD_FAILED_JNI_CALL;
165 const char* lib_utf8 = env->GetStringUTFChars(lib, NULL);
166 if (lib_utf8 != NULL) {
167 const char* relro_utf8 = env->GetStringUTFChars(relro, NULL);
168 if (relro_utf8 != NULL) {
169 ret = DoLoadWithRelroFile(env, lib_utf8, relro_utf8, clazzLoader);
170 env->ReleaseStringUTFChars(relro, relro_utf8);
171 }
172 env->ReleaseStringUTFChars(lib, lib_utf8);
173 }
174 return ret;
175}
176
177const char kWebViewFactoryClassName[] = "android/webkit/WebViewFactory";
178const char kWebViewLibraryLoaderClassName[] =
179 "android/webkit/WebViewLibraryLoader";
180const JNINativeMethod kJniMethods[] = {
181 { "nativeReserveAddressSpace", "(J)Z",
182 reinterpret_cast<void*>(ReserveAddressSpace) },
183 { "nativeCreateRelroFile",
Gustav Senntonae498f22017-10-05 15:34:13 +0100184 "(Ljava/lang/String;Ljava/lang/String;)Z",
Gustav Senntoncd165a92017-06-21 18:04:42 +0100185 reinterpret_cast<void*>(CreateRelroFile) },
186 { "nativeLoadWithRelroFile",
Gustav Senntonae498f22017-10-05 15:34:13 +0100187 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)I",
Gustav Senntoncd165a92017-06-21 18:04:42 +0100188 reinterpret_cast<void*>(LoadWithRelroFile) },
189};
190
191} // namespace
192
193void RegisterWebViewFactory(JNIEnv* env) {
194 // If either of these fail, it will set an exception that will be thrown on
195 // return, so no need to handle errors here.
196 jclass clazz = env->FindClass(kWebViewFactoryClassName);
197 if (clazz) {
198 LIBLOAD_SUCCESS = env->GetStaticIntField(
199 clazz,
200 env->GetStaticFieldID(clazz, "LIBLOAD_SUCCESS", "I"));
201
202 LIBLOAD_FAILED_TO_OPEN_RELRO_FILE = env->GetStaticIntField(
203 clazz,
204 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_OPEN_RELRO_FILE", "I"));
205
206 LIBLOAD_FAILED_TO_LOAD_LIBRARY = env->GetStaticIntField(
207 clazz,
208 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_LOAD_LIBRARY", "I"));
209
210 LIBLOAD_FAILED_JNI_CALL = env->GetStaticIntField(
211 clazz,
212 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_JNI_CALL", "I"));
213
214 LIBLOAD_FAILED_TO_FIND_NAMESPACE = env->GetStaticIntField(
215 clazz,
216 env->GetStaticFieldID(clazz, "LIBLOAD_FAILED_TO_FIND_NAMESPACE", "I"));
217 }
218}
219
220void RegisterWebViewLibraryLoader(JNIEnv* env) {
221 // If either of these fail, it will set an exception that will be thrown on
222 // return, so no need to handle errors here.
223 jclass clazz = env->FindClass(kWebViewLibraryLoaderClassName);
224 if (clazz) {
225 env->RegisterNatives(clazz, kJniMethods, NELEM(kJniMethods));
226 }
227}
228
229} // namespace android
230
231JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
232 JNIEnv* env = NULL;
233 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
234 ALOGE("GetEnv failed");
235 return JNI_ERR;
236 }
237 android::RegisterWebViewFactory(env);
238 // Ensure there isn't a pending Java exception before registering methods from
239 // WebViewLibraryLoader
240 if (!env->ExceptionCheck()) {
241 android::RegisterWebViewLibraryLoader(env);
242 }
243 return JNI_VERSION_1_6;
244}