Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: base/android/linker/legacy_linker_jni.cc

Issue 1641513004: Update //base to chromium 9659b08ea5a34f889dc4166217f438095ddc10d2 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is the Android-specific Chromium linker, a tiny shared library 5 // This is the Android-specific Chromium linker, a tiny shared library
6 // implementing a custom dynamic linker that can be used to load the 6 // implementing a custom dynamic linker that can be used to load the
7 // real Chromium libraries (e.g. libcontentshell.so). 7 // real Chromium libraries (e.g. libcontentshell.so).
8 8
9 // The main point of this linker is to be able to share the RELRO 9 // The main point of this linker is to be able to share the RELRO
10 // section of libcontentshell.so (or equivalent) between the browser and 10 // section of libcontentshell.so (or equivalent) between the browser and
11 // renderer process. 11 // renderer process.
12 12
13 // This source code *cannot* depend on anything from base/ or the C++ 13 // This source code *cannot* depend on anything from base/ or the C++
14 // STL, to keep the final library small, and avoid ugly dependency issues. 14 // STL, to keep the final library small, and avoid ugly dependency issues.
15 15
16 #include <android/log.h> 16 #include <android/log.h>
17 #include <crazy_linker.h> 17 #include <crazy_linker.h>
18 #include <fcntl.h> 18 #include <fcntl.h>
19 #include <jni.h> 19 #include <jni.h>
20 #include <limits.h> 20 #include <limits.h>
21 #include <stdlib.h> 21 #include <stdlib.h>
22 #include <sys/mman.h> 22 #include <sys/mman.h>
23 #include <unistd.h> 23 #include <unistd.h>
24 24
25 // See commentary in crazy_linker_elf_loader.cpp for the effect of setting
26 // this. If changing there, change here also.
27 //
28 // For more, see:
29 // https://crbug.com/504410
30 #define RESERVE_BREAKPAD_GUARD_REGION 1
31
25 // Set this to 1 to enable debug traces to the Android log. 32 // Set this to 1 to enable debug traces to the Android log.
26 // Note that LOG() from "base/logging.h" cannot be used, since it is 33 // Note that LOG() from "base/logging.h" cannot be used, since it is
27 // in base/ which hasn't been loaded yet. 34 // in base/ which hasn't been loaded yet.
28 #define DEBUG 0 35 #define DEBUG 0
29 36
30 #define TAG "chromium_android_linker" 37 #define TAG "chromium_android_linker"
31 38
32 #if DEBUG 39 #if DEBUG
33 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) 40 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
34 #else 41 #else
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 bool InitFieldId(JNIEnv* env, 104 bool InitFieldId(JNIEnv* env,
98 jclass clazz, 105 jclass clazz,
99 const char* field_name, 106 const char* field_name,
100 const char* field_sig, 107 const char* field_sig,
101 jfieldID* field_id) { 108 jfieldID* field_id) {
102 *field_id = env->GetFieldID(clazz, field_name, field_sig); 109 *field_id = env->GetFieldID(clazz, field_name, field_sig);
103 if (!*field_id) { 110 if (!*field_id) {
104 LOG_ERROR("Could not find ID for field '%s'", field_name); 111 LOG_ERROR("Could not find ID for field '%s'", field_name);
105 return false; 112 return false;
106 } 113 }
107 LOG_INFO( 114 LOG_INFO("%s: Found ID %p for field '%s'", __FUNCTION__, *field_id,
108 "%s: Found ID %p for field '%s'", __FUNCTION__, *field_id, field_name); 115 field_name);
109 return true; 116 return true;
110 } 117 }
111 118
112 // Initialize a jmethodID corresponding to the static method of a given 119 // Initialize a jmethodID corresponding to the static method of a given
113 // |clazz|, with name |method_name| and signature |method_sig|. 120 // |clazz|, with name |method_name| and signature |method_sig|.
114 // |env| is the current JNI environment handle. 121 // |env| is the current JNI environment handle.
115 // On success, return true and set |*method_id|. 122 // On success, return true and set |*method_id|.
116 bool InitStaticMethodId(JNIEnv* env, 123 bool InitStaticMethodId(JNIEnv* env,
117 jclass clazz, 124 jclass clazz,
118 const char* method_name, 125 const char* method_name,
119 const char* method_sig, 126 const char* method_sig,
120 jmethodID* method_id) { 127 jmethodID* method_id) {
121 *method_id = env->GetStaticMethodID(clazz, method_name, method_sig); 128 *method_id = env->GetStaticMethodID(clazz, method_name, method_sig);
122 if (!*method_id) { 129 if (!*method_id) {
123 LOG_ERROR("Could not find ID for static method '%s'", method_name); 130 LOG_ERROR("Could not find ID for static method '%s'", method_name);
124 return false; 131 return false;
125 } 132 }
126 LOG_INFO("%s: Found ID %p for static method '%s'", 133 LOG_INFO("%s: Found ID %p for static method '%s'", __FUNCTION__, *method_id,
127 __FUNCTION__, *method_id, method_name); 134 method_name);
128 return true; 135 return true;
129 } 136 }
130 137
131 // Initialize a jfieldID corresponding to the static field of a given |clazz|, 138 // Initialize a jfieldID corresponding to the static field of a given |clazz|,
132 // with name |field_name| and signature |field_sig|. 139 // with name |field_name| and signature |field_sig|.
133 // |env| is the current JNI environment handle. 140 // |env| is the current JNI environment handle.
134 // On success, return true and set |*field_id|. 141 // On success, return true and set |*field_id|.
135 bool InitStaticFieldId(JNIEnv* env, 142 bool InitStaticFieldId(JNIEnv* env,
136 jclass clazz, 143 jclass clazz,
137 const char* field_name, 144 const char* field_name,
138 const char* field_sig, 145 const char* field_sig,
139 jfieldID* field_id) { 146 jfieldID* field_id) {
140 *field_id = env->GetStaticFieldID(clazz, field_name, field_sig); 147 *field_id = env->GetStaticFieldID(clazz, field_name, field_sig);
141 if (!*field_id) { 148 if (!*field_id) {
142 LOG_ERROR("Could not find ID for static field '%s'", field_name); 149 LOG_ERROR("Could not find ID for static field '%s'", field_name);
143 return false; 150 return false;
144 } 151 }
145 LOG_INFO( 152 LOG_INFO("%s: Found ID %p for static field '%s'", __FUNCTION__, *field_id,
146 "%s: Found ID %p for static field '%s'", 153 field_name);
147 __FUNCTION__, *field_id, field_name);
148 return true; 154 return true;
149 } 155 }
150 156
151 // Initialize a jint corresponding to the static integer field of a class 157 // Initialize a jint corresponding to the static integer field of a class
152 // with class name |class_name| and field name |field_name|. 158 // with class name |class_name| and field name |field_name|.
153 // |env| is the current JNI environment handle. 159 // |env| is the current JNI environment handle.
154 // On success, return true and set |*value|. 160 // On success, return true and set |*value|.
155 bool InitStaticInt(JNIEnv* env, 161 bool InitStaticInt(JNIEnv* env,
156 const char* class_name, 162 const char* class_name,
157 const char* field_name, 163 const char* field_name,
158 jint* value) { 164 jint* value) {
159 jclass clazz; 165 jclass clazz;
160 if (!InitClassReference(env, class_name, &clazz)) 166 if (!InitClassReference(env, class_name, &clazz))
161 return false; 167 return false;
162 168
163 jfieldID field_id; 169 jfieldID field_id;
164 if (!InitStaticFieldId(env, clazz, field_name, "I", &field_id)) 170 if (!InitStaticFieldId(env, clazz, field_name, "I", &field_id))
165 return false; 171 return false;
166 172
167 *value = env->GetStaticIntField(clazz, field_id); 173 *value = env->GetStaticIntField(clazz, field_id);
168 LOG_INFO( 174 LOG_INFO("%s: Found value %d for class '%s', static field '%s'", __FUNCTION__,
169 "%s: Found value %d for class '%s', static field '%s'", 175 *value, class_name, field_name);
170 __FUNCTION__, *value, class_name, field_name);
171 176
172 return true; 177 return true;
173 } 178 }
174 179
175 // A class used to model the field IDs of the org.chromium.base.Linker 180 // A class used to model the field IDs of the org.chromium.base.Linker
176 // LibInfo inner class, used to communicate data with the Java side 181 // LibInfo inner class, used to communicate data with the Java side
177 // of the linker. 182 // of the linker.
178 struct LibInfo_class { 183 struct LibInfo_class {
179 jfieldID load_address_id; 184 jfieldID load_address_id;
180 jfieldID load_size_id; 185 jfieldID load_size_id;
181 jfieldID relro_start_id; 186 jfieldID relro_start_id;
182 jfieldID relro_size_id; 187 jfieldID relro_size_id;
183 jfieldID relro_fd_id; 188 jfieldID relro_fd_id;
184 189
185 // Initialize an instance. 190 // Initialize an instance.
186 bool Init(JNIEnv* env) { 191 bool Init(JNIEnv* env) {
187 jclass clazz; 192 jclass clazz;
188 if (!InitClassReference( 193 if (!InitClassReference(
189 env, "org/chromium/base/library_loader/Linker$LibInfo", &clazz)) { 194 env, "org/chromium/base/library_loader/Linker$LibInfo", &clazz)) {
190 return false; 195 return false;
191 } 196 }
192 197
193 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && 198 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) &&
194 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && 199 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) &&
195 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && 200 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) &&
196 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && 201 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) &&
197 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); 202 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id);
198 } 203 }
199 204
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 // needs to be done early in initialization, before any other crazy linker 243 // needs to be done early in initialization, before any other crazy linker
239 // code is run. 244 // code is run.
240 // |env| is the current JNI environment handle. 245 // |env| is the current JNI environment handle.
241 // On success, return true. 246 // On success, return true.
242 bool InitSDKVersionInfo(JNIEnv* env) { 247 bool InitSDKVersionInfo(JNIEnv* env) {
243 jint value = 0; 248 jint value = 0;
244 if (!InitStaticInt(env, "android/os/Build$VERSION", "SDK_INT", &value)) 249 if (!InitStaticInt(env, "android/os/Build$VERSION", "SDK_INT", &value))
245 return false; 250 return false;
246 251
247 crazy_set_sdk_build_version(static_cast<int>(value)); 252 crazy_set_sdk_build_version(static_cast<int>(value));
248 LOG_INFO("%s: Set SDK build version to %d", 253 LOG_INFO("%s: Set SDK build version to %d", __FUNCTION__,
249 __FUNCTION__, static_cast<int>(value)); 254 static_cast<int>(value));
250 255
251 return true; 256 return true;
252 } 257 }
253 258
254 // The linker uses a single crazy_context_t object created on demand. 259 // The linker uses a single crazy_context_t object created on demand.
255 // There is no need to protect this against concurrent access, locking 260 // There is no need to protect this against concurrent access, locking
256 // is already handled on the Java side. 261 // is already handled on the Java side.
257 static crazy_context_t* s_crazy_context; 262 static crazy_context_t* s_crazy_context;
258 263
259 crazy_context_t* GetCrazyContext() { 264 crazy_context_t* GetCrazyContext() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 return ret; 296 return ret;
292 } 297 }
293 298
294 private: 299 private:
295 crazy_library_t* lib_; 300 crazy_library_t* lib_;
296 }; 301 };
297 302
298 namespace { 303 namespace {
299 304
300 template <class LibraryOpener> 305 template <class LibraryOpener>
301 bool GenericLoadLibrary( 306 bool GenericLoadLibrary(JNIEnv* env,
302 JNIEnv* env, 307 const char* library_name,
303 const char* library_name, jlong load_address, jobject lib_info_obj, 308 jlong load_address,
304 const LibraryOpener& opener) { 309 jobject lib_info_obj,
310 const LibraryOpener& opener) {
305 crazy_context_t* context = GetCrazyContext(); 311 crazy_context_t* context = GetCrazyContext();
306 312
307 if (!IsValidAddress(load_address)) { 313 if (!IsValidAddress(load_address)) {
308 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); 314 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address);
309 return false; 315 return false;
310 } 316 }
311 317
312 // Set the desired load address (0 means randomize it). 318 // Set the desired load address (0 means randomize it).
313 crazy_context_set_load_address(context, static_cast<size_t>(load_address)); 319 crazy_context_set_load_address(context, static_cast<size_t>(load_address));
314 320
315 ScopedLibrary library; 321 ScopedLibrary library;
316 if (!opener.Open(library.GetPtr(), library_name, context)) { 322 if (!opener.Open(library.GetPtr(), library_name, context)) {
317 return false; 323 return false;
318 } 324 }
319 325
320 crazy_library_info_t info; 326 crazy_library_info_t info;
321 if (!crazy_library_get_info(library.Get(), context, &info)) { 327 if (!crazy_library_get_info(library.Get(), context, &info)) {
322 LOG_ERROR("%s: Could not get library information for %s: %s", 328 LOG_ERROR("%s: Could not get library information for %s: %s", __FUNCTION__,
323 __FUNCTION__, 329 library_name, crazy_context_get_error(context));
324 library_name,
325 crazy_context_get_error(context));
326 return false; 330 return false;
327 } 331 }
328 332
329 // Release library object to keep it alive after the function returns. 333 // Release library object to keep it alive after the function returns.
330 library.Release(); 334 library.Release();
331 335
332 s_lib_info_fields.SetLoadInfo( 336 s_lib_info_fields.SetLoadInfo(env, lib_info_obj, info.load_address,
333 env, lib_info_obj, info.load_address, info.load_size); 337 info.load_size);
334 LOG_INFO("%s: Success loading library %s", __FUNCTION__, library_name); 338 LOG_INFO("%s: Success loading library %s", __FUNCTION__, library_name);
335 return true; 339 return true;
336 } 340 }
337 341
338 // Used for opening the library in a regular file. 342 // Used for opening the library in a regular file.
339 class FileLibraryOpener { 343 class FileLibraryOpener {
340 public: 344 public:
341 bool Open( 345 bool Open(crazy_library_t** library,
342 crazy_library_t** library, 346 const char* library_name,
343 const char* library_name, 347 crazy_context_t* context) const;
344 crazy_context_t* context) const;
345 }; 348 };
346 349
347 bool FileLibraryOpener::Open( 350 bool FileLibraryOpener::Open(crazy_library_t** library,
348 crazy_library_t** library, 351 const char* library_name,
349 const char* library_name, 352 crazy_context_t* context) const {
350 crazy_context_t* context) const {
351 if (!crazy_library_open(library, library_name, context)) { 353 if (!crazy_library_open(library, library_name, context)) {
352 LOG_ERROR("%s: Could not open %s: %s", 354 LOG_ERROR("%s: Could not open %s: %s", __FUNCTION__, library_name,
353 __FUNCTION__,
354 library_name,
355 crazy_context_get_error(context)); 355 crazy_context_get_error(context));
356 return false; 356 return false;
357 } 357 }
358 return true; 358 return true;
359 } 359 }
360 360
361 // Used for opening the library in a zip file. 361 // Used for opening the library in a zip file.
362 class ZipLibraryOpener { 362 class ZipLibraryOpener {
363 public: 363 public:
364 explicit ZipLibraryOpener(const char* zip_file) : zip_file_(zip_file) {} 364 explicit ZipLibraryOpener(const char* zip_file) : zip_file_(zip_file) {}
365 bool Open( 365 bool Open(crazy_library_t** library,
366 crazy_library_t** library, 366 const char* library_name,
367 const char* library_name, 367 crazy_context_t* context) const;
368 crazy_context_t* context) const; 368
369 private: 369 private:
370 const char* zip_file_; 370 const char* zip_file_;
371 }; 371 };
372 372
373 bool ZipLibraryOpener::Open( 373 bool ZipLibraryOpener::Open(crazy_library_t** library,
374 crazy_library_t** library, 374 const char* library_name,
375 const char* library_name, 375 crazy_context_t* context) const {
376 crazy_context_t* context) const { 376 if (!crazy_library_open_in_zip_file(library, zip_file_, library_name,
377 if (!crazy_library_open_in_zip_file( 377 context)) {
378 library, zip_file_, library_name, context)) { 378 LOG_ERROR("%s: Could not open %s in zip file %s: %s", __FUNCTION__,
379 LOG_ERROR("%s: Could not open %s in zip file %s: %s", 379 library_name, zip_file_, crazy_context_get_error(context));
380 __FUNCTION__, library_name, zip_file_, 380 return false;
381 crazy_context_get_error(context));
382 return false;
383 } 381 }
384 return true; 382 return true;
385 } 383 }
386 384
387 } // unnamed namespace 385 } // unnamed namespace
388 386
389 // Load a library with the chromium linker. This will also call its 387 // Load a library with the chromium linker. This will also call its
390 // JNI_OnLoad() method, which shall register its methods. Note that 388 // JNI_OnLoad() method, which shall register its methods. Note that
391 // lazy native method resolution will _not_ work after this, because 389 // lazy native method resolution will _not_ work after this, because
392 // Dalvik uses the system's dlsym() which won't see the new library, 390 // Dalvik uses the system's dlsym() which won't see the new library,
393 // so explicit registration is mandatory. 391 // so explicit registration is mandatory.
394 // |env| is the current JNI environment handle. 392 // |env| is the current JNI environment handle.
395 // |clazz| is the static class handle for org.chromium.base.Linker, 393 // |clazz| is the static class handle for org.chromium.base.Linker,
396 // and is ignored here. 394 // and is ignored here.
397 // |library_name| is the library name (e.g. libfoo.so). 395 // |library_name| is the library name (e.g. libfoo.so).
398 // |load_address| is an explicit load address. 396 // |load_address| is an explicit load address.
399 // |library_info| is a LibInfo handle used to communicate information 397 // |library_info| is a LibInfo handle used to communicate information
400 // with the Java side. 398 // with the Java side.
401 // Return true on success. 399 // Return true on success.
402 jboolean LoadLibrary(JNIEnv* env, 400 jboolean LoadLibrary(JNIEnv* env,
403 jclass clazz, 401 jclass clazz,
404 jstring library_name, 402 jstring library_name,
405 jlong load_address, 403 jlong load_address,
406 jobject lib_info_obj) { 404 jobject lib_info_obj) {
407 String lib_name(env, library_name); 405 String lib_name(env, library_name);
408 FileLibraryOpener opener; 406 FileLibraryOpener opener;
409 return GenericLoadLibrary( 407 return GenericLoadLibrary(env, lib_name.c_str(),
410 env, lib_name.c_str(), 408 static_cast<size_t>(load_address), lib_info_obj,
411 static_cast<size_t>(load_address), lib_info_obj, opener); 409 opener);
412 } 410 }
413 411
414 // Load a library from a zipfile with the chromium linker. The 412 // Load a library from a zipfile with the chromium linker. The
415 // library in the zipfile must be uncompressed and page aligned. 413 // library in the zipfile must be uncompressed and page aligned.
416 // The basename of the library is given. The library is expected 414 // The basename of the library is given. The library is expected
417 // to be lib/<abi_tag>/crazy.<basename>. The <abi_tag> used will be the 415 // to be lib/<abi_tag>/crazy.<basename>. The <abi_tag> used will be the
418 // same as the abi for this linker. The "crazy." prefix is included 416 // same as the abi for this linker. The "crazy." prefix is included
419 // so that the Android Package Manager doesn't extract the library into 417 // so that the Android Package Manager doesn't extract the library into
420 // /data/app-lib. 418 // /data/app-lib.
421 // 419 //
(...skipping 13 matching lines...) Expand all
435 // Returns true on success. 433 // Returns true on success.
436 jboolean LoadLibraryInZipFile(JNIEnv* env, 434 jboolean LoadLibraryInZipFile(JNIEnv* env,
437 jclass clazz, 435 jclass clazz,
438 jstring zipfile_name, 436 jstring zipfile_name,
439 jstring library_name, 437 jstring library_name,
440 jlong load_address, 438 jlong load_address,
441 jobject lib_info_obj) { 439 jobject lib_info_obj) {
442 String zipfile_name_str(env, zipfile_name); 440 String zipfile_name_str(env, zipfile_name);
443 String lib_name(env, library_name); 441 String lib_name(env, library_name);
444 ZipLibraryOpener opener(zipfile_name_str.c_str()); 442 ZipLibraryOpener opener(zipfile_name_str.c_str());
445 return GenericLoadLibrary( 443 return GenericLoadLibrary(env, lib_name.c_str(),
446 env, lib_name.c_str(), 444 static_cast<size_t>(load_address), lib_info_obj,
447 static_cast<size_t>(load_address), lib_info_obj, opener); 445 opener);
448 } 446 }
449 447
450 // Class holding the Java class and method ID for the Java side Linker 448 // Class holding the Java class and method ID for the Java side Linker
451 // postCallbackOnMainThread method. 449 // postCallbackOnMainThread method.
452 struct JavaCallbackBindings_class { 450 struct JavaCallbackBindings_class {
453 jclass clazz; 451 jclass clazz;
454 jmethodID method_id; 452 jmethodID method_id;
455 453
456 // Initialize an instance. 454 // Initialize an instance.
457 bool Init(JNIEnv* env, jclass linker_class) { 455 bool Init(JNIEnv* env, jclass linker_class) {
458 clazz = reinterpret_cast<jclass>(env->NewGlobalRef(linker_class)); 456 clazz = reinterpret_cast<jclass>(env->NewGlobalRef(linker_class));
459 return InitStaticMethodId(env, 457 return InitStaticMethodId(env, linker_class, "postCallbackOnMainThread",
460 linker_class, 458 "(J)V", &method_id);
461 "postCallbackOnMainThread",
462 "(J)V",
463 &method_id);
464 } 459 }
465 }; 460 };
466 461
467 static JavaCallbackBindings_class s_java_callback_bindings; 462 static JavaCallbackBindings_class s_java_callback_bindings;
468 463
469 // Designated receiver function for callbacks from Java. Its name is known 464 // Designated receiver function for callbacks from Java. Its name is known
470 // to the Java side. 465 // to the Java side.
471 // |env| is the current JNI environment handle and is ignored here. 466 // |env| is the current JNI environment handle and is ignored here.
472 // |clazz| is the static class handle for org.chromium.base.Linker, 467 // |clazz| is the static class handle for org.chromium.base.Linker,
473 // and is ignored here. 468 // and is ignored here.
474 // |arg| is a pointer to an allocated crazy_callback_t, deleted after use. 469 // |arg| is a pointer to an allocated crazy_callback_t, deleted after use.
475 void RunCallbackOnUiThread(JNIEnv* env, jclass clazz, jlong arg) { 470 void RunCallbackOnUiThread(JNIEnv* env, jclass clazz, jlong arg) {
476 crazy_callback_t* callback = reinterpret_cast<crazy_callback_t*>(arg); 471 crazy_callback_t* callback = reinterpret_cast<crazy_callback_t*>(arg);
477 472
478 LOG_INFO("%s: Called back from java with handler %p, opaque %p", 473 LOG_INFO("%s: Called back from java with handler %p, opaque %p", __FUNCTION__,
479 __FUNCTION__, callback->handler, callback->opaque); 474 callback->handler, callback->opaque);
480 475
481 crazy_callback_run(callback); 476 crazy_callback_run(callback);
482 delete callback; 477 delete callback;
483 } 478 }
484 479
485 // Request a callback from Java. The supplied crazy_callback_t is valid only 480 // Request a callback from Java. The supplied crazy_callback_t is valid only
486 // for the duration of this call, so we copy it to a newly allocated 481 // for the duration of this call, so we copy it to a newly allocated
487 // crazy_callback_t and then call the Java side's postCallbackOnMainThread. 482 // crazy_callback_t and then call the Java side's postCallbackOnMainThread.
488 // This will call back to to our RunCallbackOnUiThread some time 483 // This will call back to to our RunCallbackOnUiThread some time
489 // later on the UI thread. 484 // later on the UI thread.
490 // |callback_request| is a crazy_callback_t. 485 // |callback_request| is a crazy_callback_t.
491 // |poster_opaque| is unused. 486 // |poster_opaque| is unused.
492 // Returns true if the callback request succeeds. 487 // Returns true if the callback request succeeds.
493 static bool PostForLaterExecution(crazy_callback_t* callback_request, 488 static bool PostForLaterExecution(crazy_callback_t* callback_request,
494 void* poster_opaque UNUSED) { 489 void* poster_opaque UNUSED) {
495 crazy_context_t* context = GetCrazyContext(); 490 crazy_context_t* context = GetCrazyContext();
496 491
497 JavaVM* vm; 492 JavaVM* vm;
498 int minimum_jni_version; 493 int minimum_jni_version;
499 crazy_context_get_java_vm(context, 494 crazy_context_get_java_vm(context, reinterpret_cast<void**>(&vm),
500 reinterpret_cast<void**>(&vm),
501 &minimum_jni_version); 495 &minimum_jni_version);
502 496
503 // Do not reuse JNIEnv from JNI_OnLoad, but retrieve our own. 497 // Do not reuse JNIEnv from JNI_OnLoad, but retrieve our own.
504 JNIEnv* env; 498 JNIEnv* env;
505 if (JNI_OK != vm->GetEnv( 499 if (JNI_OK !=
506 reinterpret_cast<void**>(&env), minimum_jni_version)) { 500 vm->GetEnv(reinterpret_cast<void**>(&env), minimum_jni_version)) {
507 LOG_ERROR("Could not create JNIEnv"); 501 LOG_ERROR("Could not create JNIEnv");
508 return false; 502 return false;
509 } 503 }
510 504
511 // Copy the callback; the one passed as an argument may be temporary. 505 // Copy the callback; the one passed as an argument may be temporary.
512 crazy_callback_t* callback = new crazy_callback_t(); 506 crazy_callback_t* callback = new crazy_callback_t();
513 *callback = *callback_request; 507 *callback = *callback_request;
514 508
515 LOG_INFO("%s: Calling back to java with handler %p, opaque %p", 509 LOG_INFO("%s: Calling back to java with handler %p, opaque %p", __FUNCTION__,
516 __FUNCTION__, callback->handler, callback->opaque); 510 callback->handler, callback->opaque);
517 511
518 jlong arg = static_cast<jlong>(reinterpret_cast<uintptr_t>(callback)); 512 jlong arg = static_cast<jlong>(reinterpret_cast<uintptr_t>(callback));
519 513
520 env->CallStaticVoidMethod( 514 env->CallStaticVoidMethod(s_java_callback_bindings.clazz,
521 s_java_callback_bindings.clazz, s_java_callback_bindings.method_id, arg); 515 s_java_callback_bindings.method_id, arg);
522 516
523 // Back out and return false if we encounter a JNI exception. 517 // Back out and return false if we encounter a JNI exception.
524 if (env->ExceptionCheck() == JNI_TRUE) { 518 if (env->ExceptionCheck() == JNI_TRUE) {
525 env->ExceptionDescribe(); 519 env->ExceptionDescribe();
526 env->ExceptionClear(); 520 env->ExceptionClear();
527 delete callback; 521 delete callback;
528 return false; 522 return false;
529 } 523 }
530 524
531 return true; 525 return true;
(...skipping 17 matching lines...) Expand all
549 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { 543 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) {
550 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); 544 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str());
551 return false; 545 return false;
552 } 546 }
553 547
554 crazy_context_t* context = GetCrazyContext(); 548 crazy_context_t* context = GetCrazyContext();
555 size_t relro_start = 0; 549 size_t relro_start = 0;
556 size_t relro_size = 0; 550 size_t relro_size = 0;
557 int relro_fd = -1; 551 int relro_fd = -1;
558 552
559 if (!crazy_library_create_shared_relro(library.Get(), 553 if (!crazy_library_create_shared_relro(
560 context, 554 library.Get(), context, static_cast<size_t>(load_address),
561 static_cast<size_t>(load_address), 555 &relro_start, &relro_size, &relro_fd)) {
562 &relro_start,
563 &relro_size,
564 &relro_fd)) {
565 LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n", 556 LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n",
566 __FUNCTION__, 557 __FUNCTION__, lib_name.c_str(), crazy_context_get_error(context));
567 lib_name.c_str(),
568 crazy_context_get_error(context));
569 return false; 558 return false;
570 } 559 }
571 560
572 s_lib_info_fields.SetRelroInfo( 561 s_lib_info_fields.SetRelroInfo(env, lib_info_obj, relro_start, relro_size,
573 env, lib_info_obj, relro_start, relro_size, relro_fd); 562 relro_fd);
574 return true; 563 return true;
575 } 564 }
576 565
577 jboolean UseSharedRelro(JNIEnv* env, 566 jboolean UseSharedRelro(JNIEnv* env,
578 jclass clazz, 567 jclass clazz,
579 jstring library_name, 568 jstring library_name,
580 jobject lib_info_obj) { 569 jobject lib_info_obj) {
581 String lib_name(env, library_name); 570 String lib_name(env, library_name);
582 571
583 LOG_INFO("%s: called for %s, lib_info_ref=%p", 572 LOG_INFO("%s: called for %s, lib_info_ref=%p", __FUNCTION__, lib_name.c_str(),
584 __FUNCTION__,
585 lib_name.c_str(),
586 lib_info_obj); 573 lib_info_obj);
587 574
588 ScopedLibrary library; 575 ScopedLibrary library;
589 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { 576 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) {
590 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); 577 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str());
591 return false; 578 return false;
592 } 579 }
593 580
594 crazy_context_t* context = GetCrazyContext(); 581 crazy_context_t* context = GetCrazyContext();
595 size_t relro_start = 0; 582 size_t relro_start = 0;
596 size_t relro_size = 0; 583 size_t relro_size = 0;
597 int relro_fd = -1; 584 int relro_fd = -1;
598 s_lib_info_fields.GetRelroInfo( 585 s_lib_info_fields.GetRelroInfo(env, lib_info_obj, &relro_start, &relro_size,
599 env, lib_info_obj, &relro_start, &relro_size, &relro_fd); 586 &relro_fd);
600 587
601 LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", 588 LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", __FUNCTION__,
602 __FUNCTION__, 589 lib_name.c_str(), (void*)relro_start, (void*)relro_size, relro_fd);
603 lib_name.c_str(),
604 (void*)relro_start,
605 (void*)relro_size,
606 relro_fd);
607 590
608 if (!crazy_library_use_shared_relro( 591 if (!crazy_library_use_shared_relro(library.Get(), context, relro_start,
609 library.Get(), context, relro_start, relro_size, relro_fd)) { 592 relro_size, relro_fd)) {
610 LOG_ERROR("%s: Could not use shared RELRO for %s: %s", 593 LOG_ERROR("%s: Could not use shared RELRO for %s: %s", __FUNCTION__,
611 __FUNCTION__, 594 lib_name.c_str(), crazy_context_get_error(context));
612 lib_name.c_str(),
613 crazy_context_get_error(context));
614 return false; 595 return false;
615 } 596 }
616 597
617 LOG_INFO("%s: Library %s using shared RELRO section!", 598 LOG_INFO("%s: Library %s using shared RELRO section!", __FUNCTION__,
618 __FUNCTION__,
619 lib_name.c_str()); 599 lib_name.c_str());
620 600
621 return true; 601 return true;
622 } 602 }
623 603
624 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { 604 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) {
625 return crazy_system_can_share_relro(); 605 return crazy_system_can_share_relro();
626 } 606 }
627 607
628 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { 608 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) {
609 #if RESERVE_BREAKPAD_GUARD_REGION
610 // Add a Breakpad guard region. 16Mb should be comfortably larger than
611 // the largest relocation packer saving we expect to encounter.
612 static const size_t kBreakpadGuardRegionBytes = 16 * 1024 * 1024;
613 bytes += kBreakpadGuardRegionBytes;
614 #endif
615
629 void* address = 616 void* address =
630 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 617 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
631 if (address == MAP_FAILED) { 618 if (address == MAP_FAILED) {
632 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); 619 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__);
633 return 0; 620 return 0;
634 } 621 }
635 munmap(address, bytes); 622 munmap(address, bytes);
623
624 #if RESERVE_BREAKPAD_GUARD_REGION
625 // Allow for a Breakpad guard region ahead of the returned address.
626 address = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) +
627 kBreakpadGuardRegionBytes);
628 #endif
629
636 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); 630 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address);
637 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); 631 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address));
638 } 632 }
639 633
640 // Get the full path of a library in the zip file
641 // (lib/<abi>/crazy.<lib_name>).
642 //
643 // |env| is the current JNI environment handle.
644 // |clazz| is the static class handle which is not used here.
645 // |lib_name| is the library base name.
646 // Returns the full path (or empty string on failure).
647 jstring GetLibraryFilePathInZipFile(JNIEnv* env,
648 jclass clazz,
649 jstring lib_name) {
650 String lib_name_str(env, lib_name);
651 const char* lib_name_c_str = lib_name_str.c_str();
652 char buffer[kMaxFilePathLengthInZip + 1];
653 if (crazy_library_file_path_in_zip_file(
654 lib_name_c_str, buffer, sizeof(buffer)) == CRAZY_STATUS_FAILURE) {
655 LOG_ERROR("%s: Failed to get full filename for library '%s'",
656 __FUNCTION__, lib_name_c_str);
657 buffer[0] = '\0';
658 }
659 return env->NewStringUTF(buffer);
660 }
661
662 // Check whether a library is page aligned and uncompressed in the APK file.
663 //
664 // |env| is the current JNI environment handle.
665 // |clazz| is the static class handle which is not used here.
666 // |apkfile_name| is the filename of the APK.
667 // |library_name| is the library base name.
668 // Returns true if page aligned and uncompressed.
669 jboolean CheckLibraryIsMappableInApk(JNIEnv* env, jclass clazz,
670 jstring apkfile_name,
671 jstring library_name) {
672 String apkfile_name_str(env, apkfile_name);
673 const char* apkfile_name_c_str = apkfile_name_str.c_str();
674 String library_name_str(env, library_name);
675 const char* library_name_c_str = library_name_str.c_str();
676
677 LOG_INFO("%s: Checking if %s is page-aligned and uncompressed in %s\n",
678 __FUNCTION__, library_name_c_str, apkfile_name_c_str);
679 jboolean mappable = crazy_linker_check_library_is_mappable_in_zip_file(
680 apkfile_name_c_str, library_name_c_str) == CRAZY_STATUS_SUCCESS;
681 LOG_INFO("%s: %s\n", __FUNCTION__, mappable ? "Mappable" : "NOT mappable");
682
683 return mappable;
684 }
685
686 const JNINativeMethod kNativeMethods[] = { 634 const JNINativeMethod kNativeMethods[] = {
687 {"nativeLoadLibrary", 635 {"nativeLoadLibrary",
688 "(" 636 "("
689 "Ljava/lang/String;" 637 "Ljava/lang/String;"
690 "J" 638 "J"
691 "Lorg/chromium/base/library_loader/Linker$LibInfo;" 639 "Lorg/chromium/base/library_loader/Linker$LibInfo;"
692 ")" 640 ")"
693 "Z", 641 "Z",
694 reinterpret_cast<void*>(&LoadLibrary)}, 642 reinterpret_cast<void*>(&LoadLibrary)},
695 {"nativeLoadLibraryInZipFile", 643 {"nativeLoadLibraryInZipFile",
(...skipping 30 matching lines...) Expand all
726 "(" 674 "("
727 ")" 675 ")"
728 "Z", 676 "Z",
729 reinterpret_cast<void*>(&CanUseSharedRelro)}, 677 reinterpret_cast<void*>(&CanUseSharedRelro)},
730 {"nativeGetRandomBaseLoadAddress", 678 {"nativeGetRandomBaseLoadAddress",
731 "(" 679 "("
732 "J" 680 "J"
733 ")" 681 ")"
734 "J", 682 "J",
735 reinterpret_cast<void*>(&GetRandomBaseLoadAddress)}, 683 reinterpret_cast<void*>(&GetRandomBaseLoadAddress)},
736 {"nativeGetLibraryFilePathInZipFile", 684 };
737 "("
738 "Ljava/lang/String;"
739 ")"
740 "Ljava/lang/String;",
741 reinterpret_cast<void*>(&GetLibraryFilePathInZipFile)},
742 {"nativeCheckLibraryIsMappableInApk",
743 "("
744 "Ljava/lang/String;"
745 "Ljava/lang/String;"
746 ")"
747 "Z",
748 reinterpret_cast<void*>(&CheckLibraryIsMappableInApk)}, };
749 685
750 } // namespace 686 } // namespace
751 687
752 // JNI_OnLoad() hook called when the linker library is loaded through 688 // JNI_OnLoad() hook called when the linker library is loaded through
753 // the regular System.LoadLibrary) API. This shall save the Java VM 689 // the regular System.LoadLibrary) API. This shall save the Java VM
754 // handle and initialize LibInfo fields. 690 // handle and initialize LibInfo fields.
755 jint JNI_OnLoad(JavaVM* vm, void* reserved) { 691 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
756 LOG_INFO("%s: Entering", __FUNCTION__); 692 LOG_INFO("%s: Entering", __FUNCTION__);
757 // Get new JNIEnv 693 // Get new JNIEnv
758 JNIEnv* env; 694 JNIEnv* env;
759 if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { 695 if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) {
760 LOG_ERROR("Could not create JNIEnv"); 696 LOG_ERROR("Could not create JNIEnv");
761 return -1; 697 return -1;
762 } 698 }
763 699
764 // Initialize SDK version info. 700 // Initialize SDK version info.
765 LOG_INFO("%s: Retrieving SDK version info", __FUNCTION__); 701 LOG_INFO("%s: Retrieving SDK version info", __FUNCTION__);
766 if (!InitSDKVersionInfo(env)) 702 if (!InitSDKVersionInfo(env))
767 return -1; 703 return -1;
768 704
769 // Register native methods. 705 // Register native methods.
770 jclass linker_class; 706 jclass linker_class;
771 if (!InitClassReference(env, 707 if (!InitClassReference(env, "org/chromium/base/library_loader/LegacyLinker",
772 "org/chromium/base/library_loader/Linker",
773 &linker_class)) 708 &linker_class))
774 return -1; 709 return -1;
775 710
776 LOG_INFO("%s: Registering native methods", __FUNCTION__); 711 LOG_INFO("%s: Registering native methods", __FUNCTION__);
777 env->RegisterNatives(linker_class, 712 env->RegisterNatives(linker_class, kNativeMethods,
778 kNativeMethods,
779 sizeof(kNativeMethods) / sizeof(kNativeMethods[0])); 713 sizeof(kNativeMethods) / sizeof(kNativeMethods[0]));
780 714
781 // Find LibInfo field ids. 715 // Find LibInfo field ids.
782 LOG_INFO("%s: Caching field IDs", __FUNCTION__); 716 LOG_INFO("%s: Caching field IDs", __FUNCTION__);
783 if (!s_lib_info_fields.Init(env)) { 717 if (!s_lib_info_fields.Init(env)) {
784 return -1; 718 return -1;
785 } 719 }
786 720
787 // Resolve and save the Java side Linker callback class and method. 721 // Resolve and save the Java side Linker callback class and method.
788 LOG_INFO("%s: Resolving callback bindings", __FUNCTION__); 722 LOG_INFO("%s: Resolving callback bindings", __FUNCTION__);
789 if (!s_java_callback_bindings.Init(env, linker_class)) { 723 if (!s_java_callback_bindings.Init(env, linker_class)) {
790 return -1; 724 return -1;
791 } 725 }
792 726
793 // Save JavaVM* handle into context. 727 // Save JavaVM* handle into context.
794 crazy_context_t* context = GetCrazyContext(); 728 crazy_context_t* context = GetCrazyContext();
795 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); 729 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4);
796 730
797 // Register the function that the crazy linker can call to post code 731 // Register the function that the crazy linker can call to post code
798 // for later execution. 732 // for later execution.
799 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); 733 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL);
800 734
801 LOG_INFO("%s: Done", __FUNCTION__); 735 LOG_INFO("%s: Done", __FUNCTION__);
802 return JNI_VERSION_1_4; 736 return JNI_VERSION_1_4;
803 } 737 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698