| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 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 | |
| 7 // real Chromium libraries (e.g. libcontentshell.so). | |
| 8 | |
| 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 | |
| 11 // renderer process. | |
| 12 | |
| 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. | |
| 15 | |
| 16 #include <android/log.h> | |
| 17 #include <crazy_linker.h> | |
| 18 #include <fcntl.h> | |
| 19 #include <jni.h> | |
| 20 #include <limits.h> | |
| 21 #include <stdlib.h> | |
| 22 #include <sys/mman.h> | |
| 23 #include <unistd.h> | |
| 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 | |
| 32 // Set this to 1 to enable debug traces to the Android log. | |
| 33 // Note that LOG() from "base/logging.h" cannot be used, since it is | |
| 34 // in base/ which hasn't been loaded yet. | |
| 35 #define DEBUG 0 | |
| 36 | |
| 37 #define TAG "chromium_android_linker" | |
| 38 | |
| 39 #if DEBUG | |
| 40 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) | |
| 41 #else | |
| 42 #define LOG_INFO(...) ((void)0) | |
| 43 #endif | |
| 44 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) | |
| 45 | |
| 46 #define UNUSED __attribute__((unused)) | |
| 47 | |
| 48 namespace { | |
| 49 | |
| 50 // A simply scoped UTF String class that can be initialized from | |
| 51 // a Java jstring handle. Modeled like std::string, which cannot | |
| 52 // be used here. | |
| 53 class String { | |
| 54 public: | |
| 55 String(JNIEnv* env, jstring str); | |
| 56 | |
| 57 ~String() { | |
| 58 if (ptr_) | |
| 59 ::free(ptr_); | |
| 60 } | |
| 61 | |
| 62 const char* c_str() const { return ptr_ ? ptr_ : ""; } | |
| 63 size_t size() const { return size_; } | |
| 64 | |
| 65 private: | |
| 66 char* ptr_; | |
| 67 size_t size_; | |
| 68 }; | |
| 69 | |
| 70 String::String(JNIEnv* env, jstring str) { | |
| 71 size_ = env->GetStringUTFLength(str); | |
| 72 ptr_ = static_cast<char*>(::malloc(size_ + 1)); | |
| 73 | |
| 74 // Note: the result contains Java "modified UTF-8" bytes. | |
| 75 // Good enough for the linker though. | |
| 76 const char* bytes = env->GetStringUTFChars(str, NULL); | |
| 77 ::memcpy(ptr_, bytes, size_); | |
| 78 ptr_[size_] = '\0'; | |
| 79 | |
| 80 env->ReleaseStringUTFChars(str, bytes); | |
| 81 } | |
| 82 | |
| 83 // Return true iff |address| is a valid address for the target CPU. | |
| 84 bool IsValidAddress(jlong address) { | |
| 85 return static_cast<jlong>(static_cast<size_t>(address)) == address; | |
| 86 } | |
| 87 | |
| 88 // Find the jclass JNI reference corresponding to a given |class_name|. | |
| 89 // |env| is the current JNI environment handle. | |
| 90 // On success, return true and set |*clazz|. | |
| 91 bool InitClassReference(JNIEnv* env, const char* class_name, jclass* clazz) { | |
| 92 *clazz = env->FindClass(class_name); | |
| 93 if (!*clazz) { | |
| 94 LOG_ERROR("Could not find class for %s", class_name); | |
| 95 return false; | |
| 96 } | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 // Initialize a jfieldID corresponding to the field of a given |clazz|, | |
| 101 // with name |field_name| and signature |field_sig|. | |
| 102 // |env| is the current JNI environment handle. | |
| 103 // On success, return true and set |*field_id|. | |
| 104 bool InitFieldId(JNIEnv* env, | |
| 105 jclass clazz, | |
| 106 const char* field_name, | |
| 107 const char* field_sig, | |
| 108 jfieldID* field_id) { | |
| 109 *field_id = env->GetFieldID(clazz, field_name, field_sig); | |
| 110 if (!*field_id) { | |
| 111 LOG_ERROR("Could not find ID for field '%s'", field_name); | |
| 112 return false; | |
| 113 } | |
| 114 LOG_INFO( | |
| 115 "%s: Found ID %p for field '%s'", __FUNCTION__, *field_id, field_name); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 // Initialize a jmethodID corresponding to the static method of a given | |
| 120 // |clazz|, with name |method_name| and signature |method_sig|. | |
| 121 // |env| is the current JNI environment handle. | |
| 122 // On success, return true and set |*method_id|. | |
| 123 bool InitStaticMethodId(JNIEnv* env, | |
| 124 jclass clazz, | |
| 125 const char* method_name, | |
| 126 const char* method_sig, | |
| 127 jmethodID* method_id) { | |
| 128 *method_id = env->GetStaticMethodID(clazz, method_name, method_sig); | |
| 129 if (!*method_id) { | |
| 130 LOG_ERROR("Could not find ID for static method '%s'", method_name); | |
| 131 return false; | |
| 132 } | |
| 133 LOG_INFO("%s: Found ID %p for static method '%s'", | |
| 134 __FUNCTION__, *method_id, method_name); | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 // Initialize a jfieldID corresponding to the static field of a given |clazz|, | |
| 139 // with name |field_name| and signature |field_sig|. | |
| 140 // |env| is the current JNI environment handle. | |
| 141 // On success, return true and set |*field_id|. | |
| 142 bool InitStaticFieldId(JNIEnv* env, | |
| 143 jclass clazz, | |
| 144 const char* field_name, | |
| 145 const char* field_sig, | |
| 146 jfieldID* field_id) { | |
| 147 *field_id = env->GetStaticFieldID(clazz, field_name, field_sig); | |
| 148 if (!*field_id) { | |
| 149 LOG_ERROR("Could not find ID for static field '%s'", field_name); | |
| 150 return false; | |
| 151 } | |
| 152 LOG_INFO( | |
| 153 "%s: Found ID %p for static field '%s'", | |
| 154 __FUNCTION__, *field_id, field_name); | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 // Initialize a jint corresponding to the static integer field of a class | |
| 159 // with class name |class_name| and field name |field_name|. | |
| 160 // |env| is the current JNI environment handle. | |
| 161 // On success, return true and set |*value|. | |
| 162 bool InitStaticInt(JNIEnv* env, | |
| 163 const char* class_name, | |
| 164 const char* field_name, | |
| 165 jint* value) { | |
| 166 jclass clazz; | |
| 167 if (!InitClassReference(env, class_name, &clazz)) | |
| 168 return false; | |
| 169 | |
| 170 jfieldID field_id; | |
| 171 if (!InitStaticFieldId(env, clazz, field_name, "I", &field_id)) | |
| 172 return false; | |
| 173 | |
| 174 *value = env->GetStaticIntField(clazz, field_id); | |
| 175 LOG_INFO( | |
| 176 "%s: Found value %d for class '%s', static field '%s'", | |
| 177 __FUNCTION__, *value, class_name, field_name); | |
| 178 | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 // A class used to model the field IDs of the org.chromium.base.Linker | |
| 183 // LibInfo inner class, used to communicate data with the Java side | |
| 184 // of the linker. | |
| 185 struct LibInfo_class { | |
| 186 jfieldID load_address_id; | |
| 187 jfieldID load_size_id; | |
| 188 jfieldID relro_start_id; | |
| 189 jfieldID relro_size_id; | |
| 190 jfieldID relro_fd_id; | |
| 191 | |
| 192 // Initialize an instance. | |
| 193 bool Init(JNIEnv* env) { | |
| 194 jclass clazz; | |
| 195 if (!InitClassReference( | |
| 196 env, "org/chromium/base/library_loader/Linker$LibInfo", &clazz)) { | |
| 197 return false; | |
| 198 } | |
| 199 | |
| 200 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && | |
| 201 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && | |
| 202 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && | |
| 203 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && | |
| 204 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); | |
| 205 } | |
| 206 | |
| 207 void SetLoadInfo(JNIEnv* env, | |
| 208 jobject library_info_obj, | |
| 209 size_t load_address, | |
| 210 size_t load_size) { | |
| 211 env->SetLongField(library_info_obj, load_address_id, load_address); | |
| 212 env->SetLongField(library_info_obj, load_size_id, load_size); | |
| 213 } | |
| 214 | |
| 215 // Use this instance to convert a RelroInfo reference into | |
| 216 // a crazy_library_info_t. | |
| 217 void GetRelroInfo(JNIEnv* env, | |
| 218 jobject library_info_obj, | |
| 219 size_t* relro_start, | |
| 220 size_t* relro_size, | |
| 221 int* relro_fd) { | |
| 222 *relro_start = static_cast<size_t>( | |
| 223 env->GetLongField(library_info_obj, relro_start_id)); | |
| 224 | |
| 225 *relro_size = | |
| 226 static_cast<size_t>(env->GetLongField(library_info_obj, relro_size_id)); | |
| 227 | |
| 228 *relro_fd = env->GetIntField(library_info_obj, relro_fd_id); | |
| 229 } | |
| 230 | |
| 231 void SetRelroInfo(JNIEnv* env, | |
| 232 jobject library_info_obj, | |
| 233 size_t relro_start, | |
| 234 size_t relro_size, | |
| 235 int relro_fd) { | |
| 236 env->SetLongField(library_info_obj, relro_start_id, relro_start); | |
| 237 env->SetLongField(library_info_obj, relro_size_id, relro_size); | |
| 238 env->SetIntField(library_info_obj, relro_fd_id, relro_fd); | |
| 239 } | |
| 240 }; | |
| 241 | |
| 242 static LibInfo_class s_lib_info_fields; | |
| 243 | |
| 244 // Retrieve the SDK build version and pass it into the crazy linker. This | |
| 245 // needs to be done early in initialization, before any other crazy linker | |
| 246 // code is run. | |
| 247 // |env| is the current JNI environment handle. | |
| 248 // On success, return true. | |
| 249 bool InitSDKVersionInfo(JNIEnv* env) { | |
| 250 jint value = 0; | |
| 251 if (!InitStaticInt(env, "android/os/Build$VERSION", "SDK_INT", &value)) | |
| 252 return false; | |
| 253 | |
| 254 crazy_set_sdk_build_version(static_cast<int>(value)); | |
| 255 LOG_INFO("%s: Set SDK build version to %d", | |
| 256 __FUNCTION__, static_cast<int>(value)); | |
| 257 | |
| 258 return true; | |
| 259 } | |
| 260 | |
| 261 // The linker uses a single crazy_context_t object created on demand. | |
| 262 // There is no need to protect this against concurrent access, locking | |
| 263 // is already handled on the Java side. | |
| 264 static crazy_context_t* s_crazy_context; | |
| 265 | |
| 266 crazy_context_t* GetCrazyContext() { | |
| 267 if (!s_crazy_context) { | |
| 268 // Create new context. | |
| 269 s_crazy_context = crazy_context_create(); | |
| 270 | |
| 271 // Ensure libraries located in the same directory as the linker | |
| 272 // can be loaded before system ones. | |
| 273 crazy_context_add_search_path_for_address( | |
| 274 s_crazy_context, reinterpret_cast<void*>(&s_crazy_context)); | |
| 275 } | |
| 276 | |
| 277 return s_crazy_context; | |
| 278 } | |
| 279 | |
| 280 // A scoped crazy_library_t that automatically closes the handle | |
| 281 // on scope exit, unless Release() has been called. | |
| 282 class ScopedLibrary { | |
| 283 public: | |
| 284 ScopedLibrary() : lib_(NULL) {} | |
| 285 | |
| 286 ~ScopedLibrary() { | |
| 287 if (lib_) | |
| 288 crazy_library_close_with_context(lib_, GetCrazyContext()); | |
| 289 } | |
| 290 | |
| 291 crazy_library_t* Get() { return lib_; } | |
| 292 | |
| 293 crazy_library_t** GetPtr() { return &lib_; } | |
| 294 | |
| 295 crazy_library_t* Release() { | |
| 296 crazy_library_t* ret = lib_; | |
| 297 lib_ = NULL; | |
| 298 return ret; | |
| 299 } | |
| 300 | |
| 301 private: | |
| 302 crazy_library_t* lib_; | |
| 303 }; | |
| 304 | |
| 305 namespace { | |
| 306 | |
| 307 template <class LibraryOpener> | |
| 308 bool GenericLoadLibrary( | |
| 309 JNIEnv* env, | |
| 310 const char* library_name, jlong load_address, jobject lib_info_obj, | |
| 311 const LibraryOpener& opener) { | |
| 312 crazy_context_t* context = GetCrazyContext(); | |
| 313 | |
| 314 if (!IsValidAddress(load_address)) { | |
| 315 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
| 316 return false; | |
| 317 } | |
| 318 | |
| 319 // Set the desired load address (0 means randomize it). | |
| 320 crazy_context_set_load_address(context, static_cast<size_t>(load_address)); | |
| 321 | |
| 322 ScopedLibrary library; | |
| 323 if (!opener.Open(library.GetPtr(), library_name, context)) { | |
| 324 return false; | |
| 325 } | |
| 326 | |
| 327 crazy_library_info_t info; | |
| 328 if (!crazy_library_get_info(library.Get(), context, &info)) { | |
| 329 LOG_ERROR("%s: Could not get library information for %s: %s", | |
| 330 __FUNCTION__, | |
| 331 library_name, | |
| 332 crazy_context_get_error(context)); | |
| 333 return false; | |
| 334 } | |
| 335 | |
| 336 // Release library object to keep it alive after the function returns. | |
| 337 library.Release(); | |
| 338 | |
| 339 s_lib_info_fields.SetLoadInfo( | |
| 340 env, lib_info_obj, info.load_address, info.load_size); | |
| 341 LOG_INFO("%s: Success loading library %s", __FUNCTION__, library_name); | |
| 342 return true; | |
| 343 } | |
| 344 | |
| 345 // Used for opening the library in a regular file. | |
| 346 class FileLibraryOpener { | |
| 347 public: | |
| 348 bool Open( | |
| 349 crazy_library_t** library, | |
| 350 const char* library_name, | |
| 351 crazy_context_t* context) const; | |
| 352 }; | |
| 353 | |
| 354 bool FileLibraryOpener::Open( | |
| 355 crazy_library_t** library, | |
| 356 const char* library_name, | |
| 357 crazy_context_t* context) const { | |
| 358 if (!crazy_library_open(library, library_name, context)) { | |
| 359 LOG_ERROR("%s: Could not open %s: %s", | |
| 360 __FUNCTION__, | |
| 361 library_name, | |
| 362 crazy_context_get_error(context)); | |
| 363 return false; | |
| 364 } | |
| 365 return true; | |
| 366 } | |
| 367 | |
| 368 // Used for opening the library in a zip file. | |
| 369 class ZipLibraryOpener { | |
| 370 public: | |
| 371 explicit ZipLibraryOpener(const char* zip_file) : zip_file_(zip_file) {} | |
| 372 bool Open( | |
| 373 crazy_library_t** library, | |
| 374 const char* library_name, | |
| 375 crazy_context_t* context) const; | |
| 376 private: | |
| 377 const char* zip_file_; | |
| 378 }; | |
| 379 | |
| 380 bool ZipLibraryOpener::Open( | |
| 381 crazy_library_t** library, | |
| 382 const char* library_name, | |
| 383 crazy_context_t* context) const { | |
| 384 if (!crazy_library_open_in_zip_file( | |
| 385 library, zip_file_, library_name, context)) { | |
| 386 LOG_ERROR("%s: Could not open %s in zip file %s: %s", | |
| 387 __FUNCTION__, library_name, zip_file_, | |
| 388 crazy_context_get_error(context)); | |
| 389 return false; | |
| 390 } | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 } // unnamed namespace | |
| 395 | |
| 396 // Load a library with the chromium linker. This will also call its | |
| 397 // JNI_OnLoad() method, which shall register its methods. Note that | |
| 398 // lazy native method resolution will _not_ work after this, because | |
| 399 // Dalvik uses the system's dlsym() which won't see the new library, | |
| 400 // so explicit registration is mandatory. | |
| 401 // |env| is the current JNI environment handle. | |
| 402 // |clazz| is the static class handle for org.chromium.base.Linker, | |
| 403 // and is ignored here. | |
| 404 // |library_name| is the library name (e.g. libfoo.so). | |
| 405 // |load_address| is an explicit load address. | |
| 406 // |library_info| is a LibInfo handle used to communicate information | |
| 407 // with the Java side. | |
| 408 // Return true on success. | |
| 409 jboolean LoadLibrary(JNIEnv* env, | |
| 410 jclass clazz, | |
| 411 jstring library_name, | |
| 412 jlong load_address, | |
| 413 jobject lib_info_obj) { | |
| 414 String lib_name(env, library_name); | |
| 415 FileLibraryOpener opener; | |
| 416 return GenericLoadLibrary( | |
| 417 env, lib_name.c_str(), | |
| 418 static_cast<size_t>(load_address), lib_info_obj, opener); | |
| 419 } | |
| 420 | |
| 421 // Load a library from a zipfile with the chromium linker. The | |
| 422 // library in the zipfile must be uncompressed and page aligned. | |
| 423 // The basename of the library is given. The library is expected | |
| 424 // to be lib/<abi_tag>/crazy.<basename>. The <abi_tag> used will be the | |
| 425 // same as the abi for this linker. The "crazy." prefix is included | |
| 426 // so that the Android Package Manager doesn't extract the library into | |
| 427 // /data/app-lib. | |
| 428 // | |
| 429 // Loading the library will also call its JNI_OnLoad() method, which | |
| 430 // shall register its methods. Note that lazy native method resolution | |
| 431 // will _not_ work after this, because Dalvik uses the system's dlsym() | |
| 432 // which won't see the new library, so explicit registration is mandatory. | |
| 433 // | |
| 434 // |env| is the current JNI environment handle. | |
| 435 // |clazz| is the static class handle for org.chromium.base.Linker, | |
| 436 // and is ignored here. | |
| 437 // |zipfile_name| is the filename of the zipfile containing the library. | |
| 438 // |library_name| is the library base name (e.g. libfoo.so). | |
| 439 // |load_address| is an explicit load address. | |
| 440 // |library_info| is a LibInfo handle used to communicate information | |
| 441 // with the Java side. | |
| 442 // Returns true on success. | |
| 443 jboolean LoadLibraryInZipFile(JNIEnv* env, | |
| 444 jclass clazz, | |
| 445 jstring zipfile_name, | |
| 446 jstring library_name, | |
| 447 jlong load_address, | |
| 448 jobject lib_info_obj) { | |
| 449 String zipfile_name_str(env, zipfile_name); | |
| 450 String lib_name(env, library_name); | |
| 451 ZipLibraryOpener opener(zipfile_name_str.c_str()); | |
| 452 return GenericLoadLibrary( | |
| 453 env, lib_name.c_str(), | |
| 454 static_cast<size_t>(load_address), lib_info_obj, opener); | |
| 455 } | |
| 456 | |
| 457 // Class holding the Java class and method ID for the Java side Linker | |
| 458 // postCallbackOnMainThread method. | |
| 459 struct JavaCallbackBindings_class { | |
| 460 jclass clazz; | |
| 461 jmethodID method_id; | |
| 462 | |
| 463 // Initialize an instance. | |
| 464 bool Init(JNIEnv* env, jclass linker_class) { | |
| 465 clazz = reinterpret_cast<jclass>(env->NewGlobalRef(linker_class)); | |
| 466 return InitStaticMethodId(env, | |
| 467 linker_class, | |
| 468 "postCallbackOnMainThread", | |
| 469 "(J)V", | |
| 470 &method_id); | |
| 471 } | |
| 472 }; | |
| 473 | |
| 474 static JavaCallbackBindings_class s_java_callback_bindings; | |
| 475 | |
| 476 // Designated receiver function for callbacks from Java. Its name is known | |
| 477 // to the Java side. | |
| 478 // |env| is the current JNI environment handle and is ignored here. | |
| 479 // |clazz| is the static class handle for org.chromium.base.Linker, | |
| 480 // and is ignored here. | |
| 481 // |arg| is a pointer to an allocated crazy_callback_t, deleted after use. | |
| 482 void RunCallbackOnUiThread(JNIEnv* env, jclass clazz, jlong arg) { | |
| 483 crazy_callback_t* callback = reinterpret_cast<crazy_callback_t*>(arg); | |
| 484 | |
| 485 LOG_INFO("%s: Called back from java with handler %p, opaque %p", | |
| 486 __FUNCTION__, callback->handler, callback->opaque); | |
| 487 | |
| 488 crazy_callback_run(callback); | |
| 489 delete callback; | |
| 490 } | |
| 491 | |
| 492 // Request a callback from Java. The supplied crazy_callback_t is valid only | |
| 493 // for the duration of this call, so we copy it to a newly allocated | |
| 494 // crazy_callback_t and then call the Java side's postCallbackOnMainThread. | |
| 495 // This will call back to to our RunCallbackOnUiThread some time | |
| 496 // later on the UI thread. | |
| 497 // |callback_request| is a crazy_callback_t. | |
| 498 // |poster_opaque| is unused. | |
| 499 // Returns true if the callback request succeeds. | |
| 500 static bool PostForLaterExecution(crazy_callback_t* callback_request, | |
| 501 void* poster_opaque UNUSED) { | |
| 502 crazy_context_t* context = GetCrazyContext(); | |
| 503 | |
| 504 JavaVM* vm; | |
| 505 int minimum_jni_version; | |
| 506 crazy_context_get_java_vm(context, | |
| 507 reinterpret_cast<void**>(&vm), | |
| 508 &minimum_jni_version); | |
| 509 | |
| 510 // Do not reuse JNIEnv from JNI_OnLoad, but retrieve our own. | |
| 511 JNIEnv* env; | |
| 512 if (JNI_OK != vm->GetEnv( | |
| 513 reinterpret_cast<void**>(&env), minimum_jni_version)) { | |
| 514 LOG_ERROR("Could not create JNIEnv"); | |
| 515 return false; | |
| 516 } | |
| 517 | |
| 518 // Copy the callback; the one passed as an argument may be temporary. | |
| 519 crazy_callback_t* callback = new crazy_callback_t(); | |
| 520 *callback = *callback_request; | |
| 521 | |
| 522 LOG_INFO("%s: Calling back to java with handler %p, opaque %p", | |
| 523 __FUNCTION__, callback->handler, callback->opaque); | |
| 524 | |
| 525 jlong arg = static_cast<jlong>(reinterpret_cast<uintptr_t>(callback)); | |
| 526 | |
| 527 env->CallStaticVoidMethod( | |
| 528 s_java_callback_bindings.clazz, s_java_callback_bindings.method_id, arg); | |
| 529 | |
| 530 // Back out and return false if we encounter a JNI exception. | |
| 531 if (env->ExceptionCheck() == JNI_TRUE) { | |
| 532 env->ExceptionDescribe(); | |
| 533 env->ExceptionClear(); | |
| 534 delete callback; | |
| 535 return false; | |
| 536 } | |
| 537 | |
| 538 return true; | |
| 539 } | |
| 540 | |
| 541 jboolean CreateSharedRelro(JNIEnv* env, | |
| 542 jclass clazz, | |
| 543 jstring library_name, | |
| 544 jlong load_address, | |
| 545 jobject lib_info_obj) { | |
| 546 String lib_name(env, library_name); | |
| 547 | |
| 548 LOG_INFO("%s: Called for %s", __FUNCTION__, lib_name.c_str()); | |
| 549 | |
| 550 if (!IsValidAddress(load_address)) { | |
| 551 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
| 552 return false; | |
| 553 } | |
| 554 | |
| 555 ScopedLibrary library; | |
| 556 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
| 557 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
| 558 return false; | |
| 559 } | |
| 560 | |
| 561 crazy_context_t* context = GetCrazyContext(); | |
| 562 size_t relro_start = 0; | |
| 563 size_t relro_size = 0; | |
| 564 int relro_fd = -1; | |
| 565 | |
| 566 if (!crazy_library_create_shared_relro(library.Get(), | |
| 567 context, | |
| 568 static_cast<size_t>(load_address), | |
| 569 &relro_start, | |
| 570 &relro_size, | |
| 571 &relro_fd)) { | |
| 572 LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n", | |
| 573 __FUNCTION__, | |
| 574 lib_name.c_str(), | |
| 575 crazy_context_get_error(context)); | |
| 576 return false; | |
| 577 } | |
| 578 | |
| 579 s_lib_info_fields.SetRelroInfo( | |
| 580 env, lib_info_obj, relro_start, relro_size, relro_fd); | |
| 581 return true; | |
| 582 } | |
| 583 | |
| 584 jboolean UseSharedRelro(JNIEnv* env, | |
| 585 jclass clazz, | |
| 586 jstring library_name, | |
| 587 jobject lib_info_obj) { | |
| 588 String lib_name(env, library_name); | |
| 589 | |
| 590 LOG_INFO("%s: called for %s, lib_info_ref=%p", | |
| 591 __FUNCTION__, | |
| 592 lib_name.c_str(), | |
| 593 lib_info_obj); | |
| 594 | |
| 595 ScopedLibrary library; | |
| 596 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
| 597 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
| 598 return false; | |
| 599 } | |
| 600 | |
| 601 crazy_context_t* context = GetCrazyContext(); | |
| 602 size_t relro_start = 0; | |
| 603 size_t relro_size = 0; | |
| 604 int relro_fd = -1; | |
| 605 s_lib_info_fields.GetRelroInfo( | |
| 606 env, lib_info_obj, &relro_start, &relro_size, &relro_fd); | |
| 607 | |
| 608 LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", | |
| 609 __FUNCTION__, | |
| 610 lib_name.c_str(), | |
| 611 (void*)relro_start, | |
| 612 (void*)relro_size, | |
| 613 relro_fd); | |
| 614 | |
| 615 if (!crazy_library_use_shared_relro( | |
| 616 library.Get(), context, relro_start, relro_size, relro_fd)) { | |
| 617 LOG_ERROR("%s: Could not use shared RELRO for %s: %s", | |
| 618 __FUNCTION__, | |
| 619 lib_name.c_str(), | |
| 620 crazy_context_get_error(context)); | |
| 621 return false; | |
| 622 } | |
| 623 | |
| 624 LOG_INFO("%s: Library %s using shared RELRO section!", | |
| 625 __FUNCTION__, | |
| 626 lib_name.c_str()); | |
| 627 | |
| 628 return true; | |
| 629 } | |
| 630 | |
| 631 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { | |
| 632 return crazy_system_can_share_relro(); | |
| 633 } | |
| 634 | |
| 635 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { | |
| 636 #if RESERVE_BREAKPAD_GUARD_REGION | |
| 637 // Add a Breakpad guard region. 16Mb should be comfortably larger than | |
| 638 // the largest relocation packer saving we expect to encounter. | |
| 639 static const size_t kBreakpadGuardRegionBytes = 16 * 1024 * 1024; | |
| 640 bytes += kBreakpadGuardRegionBytes; | |
| 641 #endif | |
| 642 | |
| 643 void* address = | |
| 644 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
| 645 if (address == MAP_FAILED) { | |
| 646 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); | |
| 647 return 0; | |
| 648 } | |
| 649 munmap(address, bytes); | |
| 650 | |
| 651 #if RESERVE_BREAKPAD_GUARD_REGION | |
| 652 // Allow for a Breakpad guard region ahead of the returned address. | |
| 653 address = reinterpret_cast<void*>( | |
| 654 reinterpret_cast<uintptr_t>(address) + kBreakpadGuardRegionBytes); | |
| 655 #endif | |
| 656 | |
| 657 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); | |
| 658 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); | |
| 659 } | |
| 660 | |
| 661 const JNINativeMethod kNativeMethods[] = { | |
| 662 {"nativeLoadLibrary", | |
| 663 "(" | |
| 664 "Ljava/lang/String;" | |
| 665 "J" | |
| 666 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
| 667 ")" | |
| 668 "Z", | |
| 669 reinterpret_cast<void*>(&LoadLibrary)}, | |
| 670 {"nativeLoadLibraryInZipFile", | |
| 671 "(" | |
| 672 "Ljava/lang/String;" | |
| 673 "Ljava/lang/String;" | |
| 674 "J" | |
| 675 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
| 676 ")" | |
| 677 "Z", | |
| 678 reinterpret_cast<void*>(&LoadLibraryInZipFile)}, | |
| 679 {"nativeRunCallbackOnUiThread", | |
| 680 "(" | |
| 681 "J" | |
| 682 ")" | |
| 683 "V", | |
| 684 reinterpret_cast<void*>(&RunCallbackOnUiThread)}, | |
| 685 {"nativeCreateSharedRelro", | |
| 686 "(" | |
| 687 "Ljava/lang/String;" | |
| 688 "J" | |
| 689 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
| 690 ")" | |
| 691 "Z", | |
| 692 reinterpret_cast<void*>(&CreateSharedRelro)}, | |
| 693 {"nativeUseSharedRelro", | |
| 694 "(" | |
| 695 "Ljava/lang/String;" | |
| 696 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
| 697 ")" | |
| 698 "Z", | |
| 699 reinterpret_cast<void*>(&UseSharedRelro)}, | |
| 700 {"nativeCanUseSharedRelro", | |
| 701 "(" | |
| 702 ")" | |
| 703 "Z", | |
| 704 reinterpret_cast<void*>(&CanUseSharedRelro)}, | |
| 705 {"nativeGetRandomBaseLoadAddress", | |
| 706 "(" | |
| 707 "J" | |
| 708 ")" | |
| 709 "J", | |
| 710 reinterpret_cast<void*>(&GetRandomBaseLoadAddress)}, | |
| 711 }; | |
| 712 | |
| 713 } // namespace | |
| 714 | |
| 715 // JNI_OnLoad() hook called when the linker library is loaded through | |
| 716 // the regular System.LoadLibrary) API. This shall save the Java VM | |
| 717 // handle and initialize LibInfo fields. | |
| 718 jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
| 719 LOG_INFO("%s: Entering", __FUNCTION__); | |
| 720 // Get new JNIEnv | |
| 721 JNIEnv* env; | |
| 722 if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { | |
| 723 LOG_ERROR("Could not create JNIEnv"); | |
| 724 return -1; | |
| 725 } | |
| 726 | |
| 727 // Initialize SDK version info. | |
| 728 LOG_INFO("%s: Retrieving SDK version info", __FUNCTION__); | |
| 729 if (!InitSDKVersionInfo(env)) | |
| 730 return -1; | |
| 731 | |
| 732 // Register native methods. | |
| 733 jclass linker_class; | |
| 734 if (!InitClassReference(env, | |
| 735 "org/chromium/base/library_loader/Linker", | |
| 736 &linker_class)) | |
| 737 return -1; | |
| 738 | |
| 739 LOG_INFO("%s: Registering native methods", __FUNCTION__); | |
| 740 env->RegisterNatives(linker_class, | |
| 741 kNativeMethods, | |
| 742 sizeof(kNativeMethods) / sizeof(kNativeMethods[0])); | |
| 743 | |
| 744 // Find LibInfo field ids. | |
| 745 LOG_INFO("%s: Caching field IDs", __FUNCTION__); | |
| 746 if (!s_lib_info_fields.Init(env)) { | |
| 747 return -1; | |
| 748 } | |
| 749 | |
| 750 // Resolve and save the Java side Linker callback class and method. | |
| 751 LOG_INFO("%s: Resolving callback bindings", __FUNCTION__); | |
| 752 if (!s_java_callback_bindings.Init(env, linker_class)) { | |
| 753 return -1; | |
| 754 } | |
| 755 | |
| 756 // Save JavaVM* handle into context. | |
| 757 crazy_context_t* context = GetCrazyContext(); | |
| 758 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); | |
| 759 | |
| 760 // Register the function that the crazy linker can call to post code | |
| 761 // for later execution. | |
| 762 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); | |
| 763 | |
| 764 LOG_INFO("%s: Done", __FUNCTION__); | |
| 765 return JNI_VERSION_1_4; | |
| 766 } | |
| OLD | NEW |