Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 Chrome linker, a tiny shared library | |
| 6 // implementing a custom dynamic linker that can be used to load the | |
| 7 // real Chrome libraries (e.g. libchromeview.so). | |
| 8 | |
| 9 // The main point of this linker is to be able to share the RELRO | |
| 10 // section of libchromeview.so between the browser and renderer | |
| 11 // process. This saves about 1.3 MB of private RAM per 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 <jni.h> | |
| 19 #include <stdlib.h> | |
| 20 #include <unistd.h> | |
| 21 | |
| 22 // 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 // in base/ which hasn't been loaded yet. | |
| 25 #define DEBUG 0 | |
| 26 | |
| 27 #define TAG "content_android_linker" | |
| 28 | |
| 29 #if DEBUG | |
| 30 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) | |
| 31 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) | |
| 32 #else | |
| 33 #define LOG_INFO(...) ((void)0) | |
| 34 #define LOG_ERROR(...) ((void)0) | |
| 35 #endif | |
| 36 | |
| 37 #define UNUSED __attribute__((unused)) | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // A simply scoped UTF String class that can be initialized from | |
| 42 // a Java jstring handle. Modeled like std::string, which cannot | |
| 43 // be used here. | |
| 44 class String { | |
| 45 public: | |
| 46 String(JNIEnv* env, jstring str); | |
| 47 | |
| 48 ~String() { | |
| 49 if (ptr_) | |
| 50 ::free(ptr_); | |
| 51 } | |
| 52 | |
| 53 const char* c_str() const { return ptr_ ? ptr_ : ""; } | |
| 54 size_t size() const { return size_; } | |
| 55 | |
| 56 private: | |
| 57 char* ptr_; | |
| 58 size_t size_; | |
| 59 }; | |
| 60 | |
| 61 String::String(JNIEnv* env, jstring str) { | |
| 62 size_ = env->GetStringUTFLength(str); | |
| 63 ptr_ = static_cast<char*>(::malloc(size_ + 1)); | |
| 64 | |
| 65 // Note: the result contains Java "modified UTF-8" bytes. | |
| 66 // Good enough for the linker though. | |
| 67 const char* bytes = env->GetStringUTFChars(str, NULL); | |
| 68 ::memcpy(ptr_, bytes, size_); | |
| 69 ptr_[size_] = '\0'; | |
| 70 | |
| 71 env->ReleaseStringUTFChars(str, bytes); | |
| 72 } | |
| 73 | |
| 74 // A scoped crazy_library_t that automatically closes the handle | |
| 75 // on scope exit, unless Release() has been called. | |
| 76 class ScopedLibrary { | |
| 77 public: | |
| 78 ScopedLibrary() : lib_(NULL) {} | |
| 79 | |
| 80 ~ScopedLibrary() { | |
| 81 if (lib_) | |
| 82 crazy_library_close(lib_); | |
| 83 } | |
| 84 | |
| 85 crazy_library_t* Get() { return lib_; } | |
| 86 | |
| 87 crazy_library_t** GetPtr() { return &lib_; } | |
| 88 | |
| 89 crazy_library_t* Release() { | |
| 90 crazy_library_t* ret = lib_; | |
| 91 lib_ = NULL; | |
| 92 return ret; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 crazy_library_t* lib_; | |
| 97 }; | |
| 98 | |
| 99 // Return a pointer to the base name from an input |path| string. | |
| 100 const char* GetBaseNamePtr(const char* path) { | |
| 101 const char* p = strrchr(path, '/'); | |
| 102 if (p) | |
| 103 return p + 1; | |
| 104 return path; | |
| 105 } | |
| 106 | |
| 107 // Return true iff |address| is a valid address for the target CPU. | |
| 108 bool IsValidAddress(jlong address) { | |
| 109 return static_cast<jlong>(static_cast<size_t>(address)) == address; | |
| 110 } | |
| 111 | |
| 112 // Find the jclass JNI reference corresponding to a given |class_name|. | |
| 113 // |env| is the current JNI environment handle. | |
| 114 // On success, return true and set |*clazz|. | |
| 115 bool InitClassReference(JNIEnv* env, const char* class_name, jclass* clazz) { | |
| 116 *clazz = env->FindClass(class_name); | |
| 117 if (!*clazz) { | |
| 118 LOG_ERROR("Could not find class for %s", class_name); | |
| 119 return false; | |
| 120 } | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 // Initialize a jfieldID corresponding to the field of a given |clazz|, | |
| 125 // with name |field_name| and signature |field_sig|. | |
| 126 // |env| is the current JNI environment handle. | |
| 127 // On success, return true and set |*field_id|. | |
| 128 bool InitFieldId(JNIEnv* env, | |
| 129 jclass clazz, | |
| 130 const char* field_name, | |
| 131 const char* field_sig, | |
| 132 jfieldID* field_id) { | |
| 133 *field_id = env->GetFieldID(clazz, field_name, field_sig); | |
| 134 if (!*field_id) { | |
| 135 LOG_ERROR("Could not find ID for field '%s'", field_name); | |
| 136 return false; | |
| 137 } | |
| 138 LOG_INFO( | |
| 139 "%s: Found ID %p for field '%s'", __FUNCTION__, *field_id, field_name); | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 // A class used to model the field IDs of the org.chromium.base.Linker | |
| 144 // LibInfo inner class, used to communicate data with the Java side | |
| 145 // of the linker. | |
| 146 struct LibInfo_class { | |
| 147 jfieldID load_address_id; | |
| 148 jfieldID load_size_id; | |
| 149 jfieldID relro_start_id; | |
| 150 jfieldID relro_size_id; | |
| 151 jfieldID relro_fd_id; | |
| 152 | |
| 153 // Initialize an instance. | |
| 154 bool Init(JNIEnv* env) { | |
| 155 jclass clazz; | |
| 156 if (!InitClassReference( | |
| 157 env, "org/chromium/content/app/Linker$LibInfo", &clazz)) { | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && | |
| 162 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && | |
| 163 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && | |
| 164 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && | |
| 165 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); | |
| 166 } | |
| 167 | |
| 168 void SetLoadInfo(JNIEnv* env, | |
| 169 jobject library_info_obj, | |
| 170 size_t load_address, | |
| 171 size_t load_size) { | |
| 172 env->SetLongField(library_info_obj, load_address_id, load_address); | |
| 173 env->SetLongField(library_info_obj, load_size_id, load_size); | |
| 174 } | |
| 175 | |
| 176 // Use this instance to convert a RelroInfo reference into | |
| 177 // a crazy_library_info_t. | |
| 178 void GetRelroInfo(JNIEnv* env, | |
| 179 jobject library_info_obj, | |
| 180 size_t* relro_start, | |
| 181 size_t* relro_size, | |
| 182 int* relro_fd) { | |
| 183 *relro_start = static_cast<size_t>( | |
| 184 env->GetLongField(library_info_obj, relro_start_id)); | |
| 185 | |
| 186 *relro_size = | |
| 187 static_cast<size_t>(env->GetLongField(library_info_obj, relro_size_id)); | |
| 188 | |
| 189 *relro_fd = env->GetIntField(library_info_obj, relro_fd_id); | |
| 190 } | |
| 191 | |
| 192 void SetRelroInfo(JNIEnv* env, | |
| 193 jobject library_info_obj, | |
| 194 size_t relro_start, | |
| 195 size_t relro_size, | |
| 196 int relro_fd) { | |
| 197 env->SetLongField(library_info_obj, relro_start_id, relro_start); | |
| 198 env->SetLongField(library_info_obj, relro_size_id, relro_size); | |
| 199 env->SetIntField(library_info_obj, relro_fd_id, relro_fd); | |
| 200 } | |
| 201 }; | |
| 202 | |
| 203 static LibInfo_class s_lib_info_fields; | |
| 204 | |
| 205 // The linker uses a single crazy_context_t object created on demand. | |
| 206 // There is no need to protect this against concurrent access, locking | |
| 207 // is already handled on the Java side. | |
| 208 static crazy_context_t* s_crazy_context; | |
| 209 | |
| 210 crazy_context_t* GetCrazyContext() { | |
| 211 if (!s_crazy_context) { | |
| 212 // Create new context. | |
| 213 s_crazy_context = crazy_context_create(); | |
| 214 | |
| 215 // Ensure libraries located in the same directory as the linker | |
| 216 // can be loaded before system ones. | |
| 217 crazy_context_add_search_path_for_address( | |
| 218 s_crazy_context, reinterpret_cast<void*>(&s_crazy_context)); | |
| 219 } | |
| 220 | |
| 221 return s_crazy_context; | |
| 222 } | |
| 223 | |
| 224 // Load a library with the Chrome linker. This will also call its | |
| 225 // JNI_OnLoad() method, which shall register its methods. Note that | |
| 226 // lazy native method resolution will _not_ work after this, because | |
| 227 // Dalvik uses the system's dlsym() which won't see the new library, | |
| 228 // so explicit registration is mandatory. | |
| 229 // |env| is the current JNI environment handle. | |
| 230 // |clazz| is the static class handle for org.chromium.base.Linker, | |
| 231 // and is ignored here. | |
| 232 // |library_name| is the library name (e.g. libfoo.so). | |
| 233 // |load_address| is an explicit load address. | |
| 234 // |library_info| is a LibInfo handle used to communicate information | |
| 235 // with the Java side. | |
| 236 // Return true on success. | |
| 237 jboolean LoadLibrary(JNIEnv* env, | |
| 238 jclass clazz, | |
| 239 jstring library_name, | |
| 240 jlong load_address, | |
|
palmer
2013/10/01 00:11:22
Sometimes you use jlong for addresses, and other t
digit1
2013/10/01 15:40:20
jlong is only used to communicate with Java. After
| |
| 241 jobject lib_info_obj) { | |
| 242 String lib_name(env, library_name); | |
| 243 const char* UNUSED lib_basename = GetBaseNamePtr(lib_name.c_str()); | |
| 244 | |
| 245 crazy_context_t* context = GetCrazyContext(); | |
| 246 | |
| 247 if (!IsValidAddress(load_address)) { | |
| 248 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
| 249 return false; | |
| 250 } | |
| 251 | |
| 252 // Set the desired load address (0 means randomize it). | |
| 253 crazy_context_set_load_address(context, static_cast<size_t>(load_address)); | |
| 254 | |
| 255 // Open the library now. | |
| 256 LOG_INFO("%s: Opening shared library: %s", __FUNCTION__, lib_name.c_str()); | |
| 257 | |
| 258 // Not using a ScopedLibrary here is intentional, since leaking | |
| 259 // the library, even in case of error, is intentional. | |
|
palmer
2013/10/01 00:11:22
Why is it intentional?
digit1
2013/10/01 15:40:20
Because otherwise the library would be closed / un
| |
| 260 crazy_library_t* library; | |
| 261 if (!crazy_library_open(&library, lib_name.c_str(), context)) { | |
| 262 LOG_ERROR("%s: Could not open %s: %s", | |
| 263 __FUNCTION__, | |
| 264 lib_basename, | |
| 265 crazy_context_get_error(context)); | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 crazy_library_info_t info; | |
| 270 if (!crazy_library_get_info(library, context, &info)) { | |
| 271 LOG_ERROR("%s: Could not get library information for %s: %s", | |
| 272 __FUNCTION__, | |
| 273 lib_basename, | |
| 274 crazy_context_get_error(context)); | |
| 275 return false; | |
| 276 } | |
| 277 | |
| 278 s_lib_info_fields.SetLoadInfo( | |
| 279 env, lib_info_obj, info.load_address, info.load_size); | |
| 280 LOG_INFO("%s: Success loading library %s", __FUNCTION__, lib_basename); | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 jboolean CreateSharedRelro(JNIEnv* env, | |
| 285 jclass clazz, | |
| 286 jstring library_name, | |
| 287 jlong load_address, | |
| 288 jobject lib_info_obj) { | |
| 289 String lib_name(env, library_name); | |
| 290 | |
| 291 LOG_INFO("%s: Called for %s", __FUNCTION__, lib_name.c_str()); | |
| 292 | |
| 293 if (!IsValidAddress(load_address)) { | |
| 294 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
|
palmer
2013/10/01 00:11:22
Nit: check our coding standards, but "%" PRIx64 ma
digit1
2013/10/01 15:40:20
But jlong is actually defined as "long long" by <j
| |
| 295 return false; | |
| 296 } | |
| 297 | |
| 298 ScopedLibrary library; | |
| 299 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
| 300 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
| 301 return false; | |
| 302 } | |
| 303 | |
| 304 crazy_context_t* context = GetCrazyContext(); | |
| 305 size_t relro_start = 0; | |
| 306 size_t relro_size = 0; | |
| 307 int relro_fd = -1; | |
| 308 | |
| 309 if (!crazy_library_create_shared_relro(library.Get(), | |
| 310 context, | |
| 311 static_cast<size_t>(load_address), | |
| 312 &relro_start, | |
| 313 &relro_size, | |
| 314 &relro_fd)) { | |
| 315 LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n", | |
| 316 __FUNCTION__, | |
| 317 lib_name.c_str(), | |
| 318 crazy_context_get_error(context)); | |
| 319 return false; | |
| 320 } | |
| 321 | |
| 322 s_lib_info_fields.SetRelroInfo( | |
| 323 env, lib_info_obj, relro_start, relro_size, relro_fd); | |
| 324 return true; | |
| 325 } | |
| 326 | |
| 327 jboolean UseSharedRelro(JNIEnv* env, | |
| 328 jclass clazz, | |
| 329 jstring library_name, | |
| 330 jobject lib_info_obj) { | |
| 331 String lib_name(env, library_name); | |
| 332 | |
| 333 LOG_INFO("%s: called for %s, lib_info_ref=%p", | |
| 334 __FUNCTION__, | |
| 335 lib_name.c_str(), | |
| 336 lib_info_obj); | |
| 337 | |
| 338 ScopedLibrary library; | |
| 339 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
| 340 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
| 341 return false; | |
| 342 } | |
| 343 | |
| 344 crazy_context_t* context = GetCrazyContext(); | |
| 345 size_t relro_start = 0; | |
| 346 size_t relro_size = 0; | |
| 347 int relro_fd = -1; | |
| 348 s_lib_info_fields.GetRelroInfo( | |
| 349 env, lib_info_obj, &relro_start, &relro_size, &relro_fd); | |
| 350 | |
| 351 LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", | |
| 352 __FUNCTION__, | |
| 353 lib_name.c_str(), | |
| 354 (void*)relro_start, | |
| 355 (void*)relro_size, | |
| 356 relro_fd); | |
| 357 | |
| 358 if (!crazy_library_use_shared_relro( | |
| 359 library.Get(), context, relro_start, relro_size, relro_fd)) { | |
| 360 LOG_ERROR("%s: Could not use shared RELRO for %s: %s", | |
| 361 __FUNCTION__, | |
| 362 lib_name.c_str(), | |
| 363 crazy_context_get_error(context)); | |
| 364 return false; | |
| 365 } | |
| 366 | |
| 367 LOG_INFO("%s: Library %s using shared RELRO section!", | |
| 368 __FUNCTION__, | |
| 369 lib_name.c_str()); | |
| 370 | |
| 371 return true; | |
| 372 } | |
| 373 | |
| 374 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { | |
| 375 return crazy_system_can_share_relro(); | |
| 376 } | |
| 377 | |
| 378 jlong GetPageSize(JNIEnv* env, jclass clazz) { | |
| 379 jlong result = static_cast<jlong>(sysconf(_SC_PAGESIZE)); | |
| 380 LOG_INFO("%s: System page size is %lld bytes\n", __FUNCTION__, result); | |
| 381 return result; | |
| 382 } | |
| 383 | |
| 384 jboolean IsLowMemoryDevice(JNIEnv* env, jclass clazz) { | |
| 385 // This matches the implementation of org.chromium.base.SysUtils.isLowEnd(), | |
| 386 // however this Java method relies on native code from base/, which isn't | |
| 387 // available since the library hasn't been loaded yet. | |
|
palmer
2013/10/01 00:11:22
Can we #include something from base/, though, to g
digit1
2013/10/01 15:40:20
I really don't like that, but I've done it though.
| |
| 388 | |
| 389 // Threshold for low-end memory devices. | |
| 390 const size_t kMegaBytes = 1024 * 1024; | |
|
palmer
2013/10/01 00:11:22
These can be static, too? Probably doesn't matter.
digit1
2013/10/01 15:40:20
Indeed, that doesn't change anything for local int
| |
| 391 const size_t kLowMemoryDeviceThreshold = 512 * kMegaBytes; | |
| 392 | |
| 393 // Compute the amount of physical RAM on the device. | |
| 394 size_t pages = static_cast<size_t>(sysconf(_SC_PHYS_PAGES)); | |
| 395 size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE)); | |
| 396 size_t physical_size = pages * page_size; | |
| 397 | |
| 398 LOG_INFO("%s: System physical size is %d MB\n", | |
|
palmer
2013/10/01 00:11:22
Should be %zu instead of %d, since size_t
digit1
2013/10/01 15:40:20
Done.
| |
| 399 __FUNCTION__, physical_size / kMegaBytes); | |
| 400 | |
| 401 return physical_size <= kLowMemoryDeviceThreshold; | |
| 402 } | |
| 403 | |
| 404 const JNINativeMethod kNativeMethods[] = { | |
| 405 {"nativeLoadLibrary", | |
| 406 "(" | |
| 407 "Ljava/lang/String;" | |
| 408 "J" | |
| 409 "Lorg/chromium/content/app/Linker$LibInfo;" | |
| 410 ")" | |
| 411 "Z", | |
| 412 reinterpret_cast<void*>(&LoadLibrary)}, | |
| 413 {"nativeCreateSharedRelro", | |
| 414 "(" | |
| 415 "Ljava/lang/String;" | |
| 416 "J" | |
| 417 "Lorg/chromium/content/app/Linker$LibInfo;" | |
| 418 ")" | |
| 419 "Z", | |
| 420 reinterpret_cast<void*>(&CreateSharedRelro)}, | |
| 421 {"nativeUseSharedRelro", | |
| 422 "(" | |
| 423 "Ljava/lang/String;" | |
| 424 "Lorg/chromium/content/app/Linker$LibInfo;" | |
| 425 ")" | |
| 426 "Z", | |
| 427 reinterpret_cast<void*>(&UseSharedRelro)}, | |
| 428 {"nativeCanUseSharedRelro", | |
| 429 "(" | |
| 430 ")" | |
| 431 "Z", | |
| 432 reinterpret_cast<void*>(&CanUseSharedRelro)}, | |
| 433 {"nativeGetPageSize", | |
| 434 "(" | |
| 435 ")" | |
| 436 "J", | |
| 437 reinterpret_cast<void*>(&GetPageSize)}, | |
| 438 {"nativeIsLowMemoryDevice", | |
| 439 "(" | |
| 440 ")" | |
| 441 "Z", | |
| 442 reinterpret_cast<void*>(&IsLowMemoryDevice)}, }; | |
| 443 | |
| 444 } // namespace | |
| 445 | |
| 446 // JNI_OnLoad() hook called when the linker library is loaded through | |
| 447 // the regular System.LoadLibrary) API. This shall save the Java VM | |
| 448 // handle and initialize LibInfo fields. | |
| 449 jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
| 450 LOG_INFO("%s: Entering", __FUNCTION__); | |
| 451 // Get new JNIEnv | |
| 452 JNIEnv* env; | |
| 453 if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { | |
| 454 LOG_ERROR("Could not create JNIEnv"); | |
| 455 return -1; | |
| 456 } | |
| 457 | |
| 458 // Register native methods. | |
| 459 jclass linker_class; | |
| 460 if (!InitClassReference( | |
| 461 env, "org/chromium/content/app/Linker", &linker_class)) | |
| 462 return -1; | |
| 463 | |
| 464 LOG_INFO("%s: Registering native methods", __FUNCTION__); | |
| 465 env->RegisterNatives(linker_class, | |
| 466 kNativeMethods, | |
| 467 sizeof(kNativeMethods) / sizeof(kNativeMethods[0])); | |
| 468 | |
| 469 // Find LibInfo field ids. | |
| 470 LOG_INFO("%s: Caching field IDs", __FUNCTION__); | |
| 471 if (!s_lib_info_fields.Init(env)) { | |
| 472 return -1; | |
| 473 } | |
| 474 | |
| 475 // Save JavaVM* handle into context. | |
| 476 crazy_context_t* context = GetCrazyContext(); | |
| 477 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); | |
| 478 | |
| 479 LOG_INFO("%s: Done", __FUNCTION__); | |
| 480 return JNI_VERSION_1_4; | |
| 481 } | |
| OLD | NEW |