| OLD | NEW |
| 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 <jni.h> | 18 #include <jni.h> |
| 19 #include <stdlib.h> | 19 #include <stdlib.h> |
| 20 #include <sys/mman.h> |
| 20 #include <unistd.h> | 21 #include <unistd.h> |
| 21 | 22 |
| 22 // Set this to 1 to enable debug traces to the Android log. | 23 // Set this to 1 to enable debug traces to the Android log. |
| 23 // Note that LOG() from "base/logging.h" cannot be used, since it is | 24 // Note that LOG() from "base/logging.h" cannot be used, since it is |
| 24 // in base/ which hasn't been loaded yet. | 25 // in base/ which hasn't been loaded yet. |
| 25 #define DEBUG 0 | 26 #define DEBUG 0 |
| 26 | 27 |
| 27 #define TAG "chromium_android_linker" | 28 #define TAG "chromium_android_linker" |
| 28 | 29 |
| 29 #if DEBUG | 30 #if DEBUG |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 __FUNCTION__, | 554 __FUNCTION__, |
| 554 lib_name.c_str()); | 555 lib_name.c_str()); |
| 555 | 556 |
| 556 return true; | 557 return true; |
| 557 } | 558 } |
| 558 | 559 |
| 559 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { | 560 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { |
| 560 return crazy_system_can_share_relro(); | 561 return crazy_system_can_share_relro(); |
| 561 } | 562 } |
| 562 | 563 |
| 563 jlong GetPageSize(JNIEnv* env, jclass clazz) { | 564 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { |
| 564 jlong result = static_cast<jlong>(sysconf(_SC_PAGESIZE)); | 565 void* address = |
| 565 LOG_INFO("%s: System page size is %lld bytes\n", __FUNCTION__, result); | 566 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 566 return result; | 567 if (address == MAP_FAILED) { |
| 568 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); |
| 569 return 0; |
| 570 } |
| 571 munmap(address, bytes); |
| 572 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); |
| 573 return static_cast<jlong>(reinterpret_cast<intptr_t>(address)); |
| 567 } | 574 } |
| 568 | 575 |
| 569 const JNINativeMethod kNativeMethods[] = { | 576 const JNINativeMethod kNativeMethods[] = { |
| 570 {"nativeLoadLibrary", | 577 {"nativeLoadLibrary", |
| 571 "(" | 578 "(" |
| 572 "Ljava/lang/String;" | 579 "Ljava/lang/String;" |
| 573 "J" | 580 "J" |
| 574 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | 581 "Lorg/chromium/base/library_loader/Linker$LibInfo;" |
| 575 ")" | 582 ")" |
| 576 "Z", | 583 "Z", |
| (...skipping 26 matching lines...) Expand all Loading... |
| 603 "Ljava/lang/String;" | 610 "Ljava/lang/String;" |
| 604 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | 611 "Lorg/chromium/base/library_loader/Linker$LibInfo;" |
| 605 ")" | 612 ")" |
| 606 "Z", | 613 "Z", |
| 607 reinterpret_cast<void*>(&UseSharedRelro)}, | 614 reinterpret_cast<void*>(&UseSharedRelro)}, |
| 608 {"nativeCanUseSharedRelro", | 615 {"nativeCanUseSharedRelro", |
| 609 "(" | 616 "(" |
| 610 ")" | 617 ")" |
| 611 "Z", | 618 "Z", |
| 612 reinterpret_cast<void*>(&CanUseSharedRelro)}, | 619 reinterpret_cast<void*>(&CanUseSharedRelro)}, |
| 613 {"nativeGetPageSize", | 620 {"nativeGetRandomBaseLoadAddress", |
| 614 "(" | 621 "(" |
| 622 "J" |
| 615 ")" | 623 ")" |
| 616 "J", | 624 "J", |
| 617 reinterpret_cast<void*>(&GetPageSize)}, }; | 625 reinterpret_cast<void*>(&GetRandomBaseLoadAddress)}, }; |
| 618 | 626 |
| 619 } // namespace | 627 } // namespace |
| 620 | 628 |
| 621 // JNI_OnLoad() hook called when the linker library is loaded through | 629 // JNI_OnLoad() hook called when the linker library is loaded through |
| 622 // the regular System.LoadLibrary) API. This shall save the Java VM | 630 // the regular System.LoadLibrary) API. This shall save the Java VM |
| 623 // handle and initialize LibInfo fields. | 631 // handle and initialize LibInfo fields. |
| 624 jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 632 jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 625 LOG_INFO("%s: Entering", __FUNCTION__); | 633 LOG_INFO("%s: Entering", __FUNCTION__); |
| 626 // Get new JNIEnv | 634 // Get new JNIEnv |
| 627 JNIEnv* env; | 635 JNIEnv* env; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 658 crazy_context_t* context = GetCrazyContext(); | 666 crazy_context_t* context = GetCrazyContext(); |
| 659 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); | 667 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); |
| 660 | 668 |
| 661 // Register the function that the crazy linker can call to post code | 669 // Register the function that the crazy linker can call to post code |
| 662 // for later execution. | 670 // for later execution. |
| 663 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); | 671 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); |
| 664 | 672 |
| 665 LOG_INFO("%s: Done", __FUNCTION__); | 673 LOG_INFO("%s: Done", __FUNCTION__); |
| 666 return JNI_VERSION_1_4; | 674 return JNI_VERSION_1_4; |
| 667 } | 675 } |
| OLD | NEW |