OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 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 #ifndef BASE_ANDROID_LINKER_LINKER_JNI_H_ |
| 17 #define BASE_ANDROID_LINKER_LINKER_JNI_H_ |
| 18 |
| 19 #include <android/log.h> |
| 20 #include <jni.h> |
| 21 #include <stdlib.h> |
| 22 |
| 23 // Set this to 1 to enable debug traces to the Android log. |
| 24 // Note that LOG() from "base/logging.h" cannot be used, since it is |
| 25 // in base/ which hasn't been loaded yet. |
| 26 #define DEBUG 0 |
| 27 |
| 28 #define TAG "cr.chromium_android_linker" |
| 29 |
| 30 #if DEBUG |
| 31 #define LOG_INFO(FORMAT, ...) \ |
| 32 __android_log_print(ANDROID_LOG_INFO, TAG, "%s: " FORMAT, __FUNCTION__, \ |
| 33 ##__VA_ARGS__) |
| 34 #else |
| 35 #define LOG_INFO(FORMAT, ...) ((void)0) |
| 36 #endif |
| 37 #define LOG_ERROR(FORMAT, ...) \ |
| 38 __android_log_print(ANDROID_LOG_ERROR, TAG, "%s: " FORMAT, __FUNCTION__, \ |
| 39 ##__VA_ARGS__) |
| 40 |
| 41 #define UNUSED __attribute__((unused)) |
| 42 |
| 43 // See commentary in crazy_linker_elf_loader.cpp for the effect of setting |
| 44 // this. If changing there, change here also. |
| 45 // |
| 46 // For more, see: |
| 47 // https://crbug.com/504410 |
| 48 #define RESERVE_BREAKPAD_GUARD_REGION 1 |
| 49 |
| 50 namespace chromium_android_linker { |
| 51 |
| 52 // Larger than the largest library we might attempt to load. |
| 53 static const size_t kAddressSpaceReservationSize = 192 * 1024 * 1024; |
| 54 |
| 55 // Size of any Breakpad guard region. 16MB is comfortably larger than the |
| 56 // ~6MB relocation packing of the current 64-bit libchrome.so, the largest we |
| 57 // expect to encounter. |
| 58 #if RESERVE_BREAKPAD_GUARD_REGION |
| 59 static const size_t kBreakpadGuardRegionBytes = 16 * 1024 * 1024; |
| 60 #endif |
| 61 |
| 62 // A simple scoped UTF String class that can be initialized from |
| 63 // a Java jstring handle. Modeled like std::string, which cannot |
| 64 // be used here. |
| 65 class String { |
| 66 public: |
| 67 String(JNIEnv* env, jstring str); |
| 68 |
| 69 inline ~String() { ::free(ptr_); } |
| 70 |
| 71 inline const char* c_str() const { return ptr_ ? ptr_ : ""; } |
| 72 inline size_t size() const { return size_; } |
| 73 |
| 74 private: |
| 75 char* ptr_; |
| 76 size_t size_; |
| 77 }; |
| 78 |
| 79 // Return true iff |address| is a valid address for the target CPU. |
| 80 inline bool IsValidAddress(jlong address) { |
| 81 return static_cast<jlong>(static_cast<size_t>(address)) == address; |
| 82 } |
| 83 |
| 84 // Find the jclass JNI reference corresponding to a given |class_name|. |
| 85 // |env| is the current JNI environment handle. |
| 86 // On success, return true and set |*clazz|. |
| 87 extern bool InitClassReference(JNIEnv* env, |
| 88 const char* class_name, |
| 89 jclass* clazz); |
| 90 |
| 91 // Initialize a jfieldID corresponding to the field of a given |clazz|, |
| 92 // with name |field_name| and signature |field_sig|. |
| 93 // |env| is the current JNI environment handle. |
| 94 // On success, return true and set |*field_id|. |
| 95 extern bool InitFieldId(JNIEnv* env, |
| 96 jclass clazz, |
| 97 const char* field_name, |
| 98 const char* field_sig, |
| 99 jfieldID* field_id); |
| 100 |
| 101 // Initialize a jmethodID corresponding to the static method of a given |
| 102 // |clazz|, with name |method_name| and signature |method_sig|. |
| 103 // |env| is the current JNI environment handle. |
| 104 // On success, return true and set |*method_id|. |
| 105 extern bool InitStaticMethodId(JNIEnv* env, |
| 106 jclass clazz, |
| 107 const char* method_name, |
| 108 const char* method_sig, |
| 109 jmethodID* method_id); |
| 110 |
| 111 // Initialize a jfieldID corresponding to the static field of a given |clazz|, |
| 112 // with name |field_name| and signature |field_sig|. |
| 113 // |env| is the current JNI environment handle. |
| 114 // On success, return true and set |*field_id|. |
| 115 extern bool InitStaticFieldId(JNIEnv* env, |
| 116 jclass clazz, |
| 117 const char* field_name, |
| 118 const char* field_sig, |
| 119 jfieldID* field_id); |
| 120 |
| 121 // Initialize a jint corresponding to the static integer field of a class |
| 122 // with class name |class_name| and field name |field_name|. |
| 123 // |env| is the current JNI environment handle. |
| 124 // On success, return true and set |*value|. |
| 125 extern bool InitStaticInt(JNIEnv* env, |
| 126 const char* class_name, |
| 127 const char* field_name, |
| 128 jint* value); |
| 129 |
| 130 // Use Android ASLR to create a random library load address. |
| 131 // |env| is the current JNI environment handle, and |clazz| a class. |
| 132 // Returns the address selected by ASLR. |
| 133 extern jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz); |
| 134 |
| 135 // A class used to model the field IDs of the org.chromium.base.Linker |
| 136 // LibInfo inner class, used to communicate data with the Java side |
| 137 // of the linker. |
| 138 struct LibInfo_class { |
| 139 jfieldID load_address_id; |
| 140 jfieldID load_size_id; |
| 141 jfieldID relro_start_id; |
| 142 jfieldID relro_size_id; |
| 143 jfieldID relro_fd_id; |
| 144 |
| 145 // Initialize an instance. |
| 146 bool Init(JNIEnv* env) { |
| 147 jclass clazz; |
| 148 if (!InitClassReference( |
| 149 env, "org/chromium/base/library_loader/Linker$LibInfo", &clazz)) { |
| 150 return false; |
| 151 } |
| 152 |
| 153 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && |
| 154 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && |
| 155 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && |
| 156 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && |
| 157 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); |
| 158 } |
| 159 |
| 160 void SetLoadInfo(JNIEnv* env, |
| 161 jobject library_info_obj, |
| 162 size_t load_address, |
| 163 size_t load_size) { |
| 164 env->SetLongField(library_info_obj, load_address_id, load_address); |
| 165 env->SetLongField(library_info_obj, load_size_id, load_size); |
| 166 } |
| 167 |
| 168 void SetRelroInfo(JNIEnv* env, |
| 169 jobject library_info_obj, |
| 170 size_t relro_start, |
| 171 size_t relro_size, |
| 172 int relro_fd) { |
| 173 env->SetLongField(library_info_obj, relro_start_id, relro_start); |
| 174 env->SetLongField(library_info_obj, relro_size_id, relro_size); |
| 175 env->SetIntField(library_info_obj, relro_fd_id, relro_fd); |
| 176 } |
| 177 |
| 178 // Use this instance to convert a RelroInfo reference into |
| 179 // a crazy_library_info_t. |
| 180 void GetRelroInfo(JNIEnv* env, |
| 181 jobject library_info_obj, |
| 182 size_t* relro_start, |
| 183 size_t* relro_size, |
| 184 int* relro_fd) { |
| 185 if (relro_start) { |
| 186 *relro_start = static_cast<size_t>( |
| 187 env->GetLongField(library_info_obj, relro_start_id)); |
| 188 } |
| 189 |
| 190 if (relro_size) { |
| 191 *relro_size = static_cast<size_t>( |
| 192 env->GetLongField(library_info_obj, relro_size_id)); |
| 193 } |
| 194 |
| 195 if (relro_fd) { |
| 196 *relro_fd = env->GetIntField(library_info_obj, relro_fd_id); |
| 197 } |
| 198 } |
| 199 }; |
| 200 |
| 201 // Variable containing LibInfo for the loaded library. |
| 202 extern LibInfo_class s_lib_info_fields; |
| 203 |
| 204 } // namespace chromium_android_linker |
| 205 |
| 206 #endif // BASE_ANDROID_LINKER_LINKER_JNI_H_ |
OLD | NEW |