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