Index: base/android/linker/crazy_linker.cpp |
diff --git a/base/android/linker/crazy_linker.cpp b/base/android/linker/crazy_linker.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..55b39a0d5939304d978076abf8ecb1b7ef8165c3 |
--- /dev/null |
+++ b/base/android/linker/crazy_linker.cpp |
@@ -0,0 +1,414 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// This is the Android-specific Chrome linker, a tiny shared library |
+// implementing a custom dynamic linker that can be used to load the |
+// real Chrome libraries (e.g. libchromeview.so). |
+ |
+// The main point of this linker is to be able to share the RELRO |
+// section of libchromeview.so between the browser and renderer |
+// process. This saves about 1.3 MB of private RAM per renderer process. |
+ |
+#include <stdlib.h> |
+ |
+#include <android/log.h> |
+#include <jni.h> |
+#include <crazy_linker.h> |
digit1
2013/09/03 14:31:54
Can we rename this source file to crazy_linker_chr
|
+ |
+// Set this to 1 to enable debug traces to the Android log. |
+#define DEBUG 0 |
+ |
+#if DEBUG |
+#define LOG_INFO(...) \ |
+ __android_log_print(ANDROID_LOG_INFO, "chrome_linker", __VA_ARGS__) |
+#define LOG_ERROR(...) \ |
+ __android_log_print(ANDROID_LOG_ERROR, "chrome_linker", __VA_ARGS__) |
+#else |
+#define LOG_INFO(...) ((void)0) |
+#define LOG_ERROR(...) ((void)0) |
+#endif |
+ |
+#define UNUSED __attribute__((unused)) |
+ |
+namespace { |
+ |
+// A simply scoped UTF String class that can be initialized from |
+// a Java jstring handle. |
+class String { |
+public: |
+ String(JNIEnv* env, jstring str); |
+ |
+ ~String() { |
+ if (ptr_) |
+ ::free(ptr_); |
+ } |
+ |
+ const char* c_str() const { return ptr_ ? ptr_ : ""; } |
+ size_t size() const { return size_; } |
+ |
+private: |
+ char* ptr_; |
+ size_t size_; |
+}; |
+ |
+String::String(JNIEnv* env, jstring str) { |
+ size_ = env->GetStringUTFLength(str); |
+ ptr_ = reinterpret_cast<char*>(::malloc(size_ + 1)); |
+ |
+ // Note: the result contains Java "modified UTF-8" bytes. |
+ // Good enough for the linker though. |
+ const char* bytes = env->GetStringUTFChars(str, NULL); |
+ ::memcpy(ptr_, bytes, size_); |
+ ptr_[size_] = '\0'; |
+ |
+ env->ReleaseStringUTFChars(str, bytes); |
+} |
+ |
+// A scoped crazy_library_t that automatically closes the handle |
+// on scope exit, unless Release() has been called. |
+class ScopedLibrary { |
+public: |
+ ScopedLibrary() : lib_(NULL) {} |
+ |
+ ~ScopedLibrary() { |
+ if (lib_) |
+ crazy_library_close(lib_); |
+ } |
+ |
+ crazy_library_t* Get() { return lib_; } |
+ |
+ crazy_library_t** GetPtr() { return &lib_; } |
+ |
+ crazy_library_t* Release() { |
+ crazy_library_t* ret = lib_; |
+ lib_ = NULL; |
+ return ret; |
+ } |
+ |
+private: |
+ crazy_library_t* lib_; |
+}; |
+ |
+static const char* GetBaseNamePtr(const char* path) { |
+ const char* p = strchr(path, '/'); |
+ if (p) |
+ return p + 1; |
+ else |
+ return path; |
+} |
+ |
+static bool InitClassReference(JNIEnv* env, |
+ const char* class_name, |
+ jclass* clazz) { |
+ *clazz = env->FindClass(class_name); |
+ if (!*clazz) { |
+ LOG_ERROR("Could not find class for %s", class_name); |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+static bool InitFieldId(JNIEnv* env, |
+ jclass clazz, |
+ const char* field_name, |
+ const char* field_sig, |
+ jfieldID* field_id) { |
+ *field_id = env->GetFieldID(clazz, field_name, field_sig); |
+ if (!*field_id) { |
+ LOG_ERROR("Could not find ID for field '%s'", field_name); |
+ return false; |
+ } |
+ LOG_INFO( |
+ "%s: Found ID %p for field '%s", __FUNCTION__, *field_id, field_name); |
+ return true; |
+} |
+ |
+ |
+static JavaVM* s_java_vm; |
+ |
+struct LibInfo_class { |
+ jfieldID load_address_id; |
+ jfieldID load_size_id; |
+ jfieldID relro_start_id; |
+ jfieldID relro_size_id; |
+ jfieldID relro_fd_id; |
+ |
+ // Initialize an instance. |
+ bool Init(JNIEnv* env) { |
+ jclass clazz; |
+ if (!InitClassReference(env, |
+ "org/chromium/base/Linker$LibInfo", |
+ &clazz)) { |
+ return false; |
+ } |
+ |
+ return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && |
+ InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && |
+ InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && |
+ InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && |
+ InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); |
+ } |
+ |
+ // Use this instance to convert a RelroInfo reference into |
+ // a crazy_library_info_t. |
+ void GetLibraryInfo(JNIEnv* env, |
+ jobject library_info_obj, |
+ crazy_library_info_t* info) { |
+ info->load_address = static_cast<size_t>( |
+ env->GetLongField(library_info_obj, load_address_id)); |
+ |
+ info->load_size = static_cast<size_t>( |
+ env->GetLongField(library_info_obj, load_size_id)); |
+ |
+ info->relro_start = static_cast<size_t>( |
+ env->GetLongField(library_info_obj, relro_start_id)); |
+ |
+ info->relro_size = static_cast<size_t>( |
+ env->GetLongField(library_info_obj, relro_size_id)); |
+ |
+ info->relro_fd = env->GetIntField(library_info_obj, relro_fd_id); |
+ } |
+ |
+ void SetLibraryInfo(JNIEnv* env, |
+ jobject library_info_obj, |
+ crazy_library_info_t* info) { |
+ env->SetLongField(library_info_obj, load_address_id, info->load_address); |
+ env->SetLongField(library_info_obj, load_size_id, info->load_size); |
+ env->SetLongField(library_info_obj, relro_start_id, info->relro_start); |
+ env->SetLongField(library_info_obj, relro_size_id, info->relro_size); |
+ env->SetIntField(library_info_obj, relro_fd_id, info->relro_fd); |
+ } |
+}; |
+ |
+ |
+static LibInfo_class s_lib_info_fields; |
+ |
+static crazy_context_t* s_crazy_context; |
+ |
+crazy_context_t* GetCrazyContext() { |
+ if (!s_crazy_context) { |
+ // Create new context. |
+ s_crazy_context = crazy_context_create(); |
+ |
+ // Ensure libraries located in the same directory than the linker |
+ // can be loaded. |
+ crazy_context_add_search_path_for_address( |
+ s_crazy_context, reinterpret_cast<void*>(&s_crazy_context)); |
+ } |
+ |
+ return s_crazy_context; |
+} |
+ |
+// Load a library with the Chrome linker. This will also call its |
+// JNI_OnLoad() method, which shall register its methods. Note that |
+// lazy native method resolution will _not_ work after this, because |
+// Dalvik uses the system's dlsym() which won't see the new library, |
+// so explicit registration is mandatory. |
+jboolean nativeLoadLibrary(JNIEnv* env, |
+ jclass clazz, |
+ jstring library_name, |
+ jlong load_address, |
+ jobject library_info) { |
+ String lib_name(env, library_name); |
+ const char* UNUSED lib_basename = GetBaseNamePtr(lib_name.c_str()); |
+ |
+ crazy_context_t* context = GetCrazyContext(); |
+ |
+ // Set the desired load address (0 means randomize it). |
+ crazy_context_set_load_address(context, |
+ static_cast<size_t>(load_address)); |
+ |
+ // Open the library now. |
+ LOG_INFO("%s: Opening shared library: %s", __FUNCTION__, lib_name.c_str()); |
+ |
+ // Not using a ScopedLibrary here is intentional, since leaking |
+ // the library, even in case of error, is intentional. |
+ crazy_library_t* library; |
+ if (!crazy_library_open(&library, lib_name.c_str(), context)) { |
+ LOG_ERROR("%s: Could not open %s: %s", __FUNCTION__, lib_basename, |
+ crazy_context_get_error(context)); |
+ return false; |
+ } |
+ |
+ if (library_info) { |
+ crazy_library_info_t info; |
+ |
+ memset(&info, 0, sizeof(info)); |
+ info.relro_fd = -1; |
+ |
+ if (!crazy_library_get_info(library, context, &info)) { |
+ LOG_INFO("%s: Could not get library info for %s: %s", |
+ __FUNCTION__, lib_name.c_str(), |
+ crazy_context_get_error(context)); |
+ } else { |
+ s_lib_info_fields.SetLibraryInfo(env, library_info, &info); |
+ } |
+ } |
+ |
+ LOG_INFO("%s: Looking for JNI_OnLoad() in %s", __FUNCTION__, lib_basename); |
+ |
+ int (*jni_on_load_ptr)(JavaVM*, void*); |
+ if (crazy_library_find_symbol( |
+ library, |
+ "JNI_OnLoad", |
+ reinterpret_cast<void**>(&jni_on_load_ptr))) { |
+ |
+ LOG_INFO("%s: Calling JNI_OnLoad() in %s", __FUNCTION__, lib_basename); |
+ |
+ // Call it. |
+ int ret = (*jni_on_load_ptr)(s_java_vm, NULL); |
+ if (ret < JNI_VERSION_1_4) { |
+ LOG_ERROR("%s: JNI_OnLoad() of %s returned %d, at least %d expected", |
+ __FUNCTION__, lib_basename, ret, JNI_VERSION_1_4); |
+ return false; |
+ } |
+ } |
+ |
+ LOG_INFO("%s: Success loading library %s", __FUNCTION__, lib_basename); |
+ return true; |
+} |
+ |
+jboolean nativeEnableSharedRelro(JNIEnv* env, |
+ jclass clazz, |
+ jstring library_name, |
+ jobject lib_info_obj) { |
+ String lib_name(env, library_name); |
+ |
+ LOG_INFO("%s: Called for %s", __FUNCTION__, lib_name.c_str()); |
+ |
+ ScopedLibrary library; |
+ if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { |
+ LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); |
+ return false; |
+ } |
+ |
+ crazy_context_t* context = GetCrazyContext(); |
+ |
+ if (!crazy_library_enable_relro_sharing(library.Get(), context)) { |
+ LOG_ERROR("%s: Could not enable RELRO sharing for %s: %s", |
+ __FUNCTION__, lib_name.c_str(), |
+ crazy_context_get_error(context)); |
+ return false; |
+ } |
+ |
+ crazy_library_info_t info; |
+ if (!crazy_library_get_info(library.Get(), context, &info)) { |
+ LOG_ERROR("%s: Could not retrieve %s information: %s", |
+ __FUNCTION__, lib_name.c_str(), |
+ crazy_context_get_error(context)); |
+ return false; |
+ } |
+ |
+ s_lib_info_fields.SetLibraryInfo(env, lib_info_obj, &info); |
+ return true; |
+} |
+ |
+jboolean nativeUseSharedRelro(JNIEnv* env, |
+ jclass clazz, |
+ jstring library_name, |
+ jobject lib_info_obj) { |
+ String lib_name(env, library_name); |
+ |
+ LOG_INFO("%s: called for %s, lib_info_ref=%p", |
+ __FUNCTION__, lib_name.c_str(), lib_info_obj); |
+ |
+ ScopedLibrary library; |
+ if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { |
+ LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); |
+ return false; |
+ } |
+ |
+ crazy_context_t* context = GetCrazyContext(); |
+ crazy_library_info_t info; |
+ s_lib_info_fields.GetLibraryInfo(env, lib_info_obj, &info); |
+ |
+ LOG_INFO( |
+ "%s: library=%s load_addr=%p load_size=%p relro_start=%p relro_size=%p " |
+ "relro_fd=%d", |
+ __FUNCTION__, |
+ lib_name.c_str(), |
+ (void*)info.load_address, |
+ (void*)info.load_size, |
+ (void*)info.relro_start, |
+ (void*)info.relro_size, |
+ info.relro_fd); |
+ |
+ if (!crazy_library_use_relro_sharing(library.Get(), |
+ info.relro_start, |
+ info.relro_size, |
+ info.relro_fd, |
+ context)) { |
+ LOG_ERROR("%s: Could not enable RELRO sharing for %s: %s", |
+ __FUNCTION__, lib_name.c_str(), |
+ crazy_context_get_error(context)); |
+ return false; |
+ } |
+ |
+ LOG_INFO("%s: Library %s using shared RELRO section!", |
+ __FUNCTION__, lib_name.c_str()); |
+ |
+ return true; |
+} |
+ |
+static const JNINativeMethod kNativeMethods[] = { |
+ { "nativeLoadLibrary", |
+ "(" |
+ "Ljava/lang/String;" |
+ "J" |
+ "Lorg/chromium/base/Linker$LibInfo;" |
+ ")" |
+ "Z", |
+ reinterpret_cast<void*>(&nativeLoadLibrary) }, |
+ |
+ { "nativeEnableSharedRelro", |
+ "(" |
+ "Ljava/lang/String;" |
+ "Lorg/chromium/base/Linker$LibInfo;" |
+ ")" |
+ "Z", |
+ reinterpret_cast<void*>(&nativeEnableSharedRelro) }, |
+ |
+ { "nativeUseSharedRelro", |
+ "(" |
+ "Ljava/lang/String;" |
+ "Lorg/chromium/base/Linker$LibInfo;" |
+ ")" |
+ "Z", |
+ reinterpret_cast<void*>(&nativeUseSharedRelro) }, |
+}; |
+ |
+} // namespace |
+ |
+jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
+ s_java_vm = vm; |
+ |
+ LOG_INFO("%s: Entering", __FUNCTION__); |
+ // Get new JNIEnv |
+ JNIEnv* env; |
+ if (vm->GetEnv( |
+ reinterpret_cast<void**>(&env), JNI_VERSION_1_4) != JNI_OK) { |
+ LOG_ERROR("Could not create JNIEnv"); |
+ return -1; |
+ } |
+ |
+ // Register native methods. |
+ jclass linker_class; |
+ if (!InitClassReference(env, "org/chromium/base/Linker", &linker_class)) |
+ return -1; |
+ |
+ LOG_INFO("%s: Registering native methods", __FUNCTION__); |
+ env->RegisterNatives(linker_class, |
+ kNativeMethods, |
+ sizeof(kNativeMethods)/sizeof(kNativeMethods[0])); |
+ |
+ |
+ // Find LibInfo field ids. |
+ LOG_INFO("%s: Caching field IDs", __FUNCTION__); |
+ if (!s_lib_info_fields.Init(env)) { |
+ return -1; |
+ } |
+ |
+ LOG_INFO("%s: Done", __FUNCTION__); |
+ return JNI_VERSION_1_4; |
+} |