Index: android/linker/legacy_linker_jni.cc |
diff --git a/android/linker/legacy_linker_jni.cc b/android/linker/legacy_linker_jni.cc |
index bc677c4150bf4fb6c17b25db92bc12a9aa7635cf..e4c0455c67cc22707187ce323cf0f8a9287a3b42 100644 |
--- a/android/linker/legacy_linker_jni.cc |
+++ b/android/linker/legacy_linker_jni.cc |
@@ -2,254 +2,26 @@ |
// 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 Chromium linker, a tiny shared library |
-// implementing a custom dynamic linker that can be used to load the |
-// real Chromium libraries (e.g. libcontentshell.so). |
- |
-// The main point of this linker is to be able to share the RELRO |
-// section of libcontentshell.so (or equivalent) between the browser and |
-// renderer process. |
+// This is the version of the Android-specific Chromium linker that uses |
+// the crazy linker to load libraries. |
// This source code *cannot* depend on anything from base/ or the C++ |
// STL, to keep the final library small, and avoid ugly dependency issues. |
-#include <android/log.h> |
+#include "legacy_linker_jni.h" |
+ |
#include <crazy_linker.h> |
#include <fcntl.h> |
#include <jni.h> |
#include <limits.h> |
#include <stdlib.h> |
-#include <sys/mman.h> |
#include <unistd.h> |
-// See commentary in crazy_linker_elf_loader.cpp for the effect of setting |
-// this. If changing there, change here also. |
-// |
-// For more, see: |
-// https://crbug.com/504410 |
-#define RESERVE_BREAKPAD_GUARD_REGION 1 |
- |
-// Set this to 1 to enable debug traces to the Android log. |
-// Note that LOG() from "base/logging.h" cannot be used, since it is |
-// in base/ which hasn't been loaded yet. |
-#define DEBUG 0 |
- |
-#define TAG "chromium_android_linker" |
- |
-#if DEBUG |
-#define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) |
-#else |
-#define LOG_INFO(...) ((void)0) |
-#endif |
-#define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) |
- |
-#define UNUSED __attribute__((unused)) |
+#include "linker_jni.h" |
+namespace chromium_android_linker { |
namespace { |
-// A simply scoped UTF String class that can be initialized from |
-// a Java jstring handle. Modeled like std::string, which cannot |
-// be used here. |
-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_ = static_cast<char*>(::malloc(size_ + 1)); |
- |
- // Note: This runs before browser native code is loaded, and so cannot |
- // rely on anything from base/. This means that we must use |
- // GetStringUTFChars() and not base::android::ConvertJavaStringToUTF8(). |
- // |
- // GetStringUTFChars() suffices because the only strings used here are |
- // paths to APK files or names of shared libraries, all of which are |
- // plain ASCII, defined and hard-coded by the Chromium Android build. |
- // |
- // For more: see |
- // https://crbug.com/508876 |
- // |
- // Note: GetStringUTFChars() returns Java UTF-8 bytes. This is good |
- // enough for the linker though. |
- const char* bytes = env->GetStringUTFChars(str, NULL); |
- ::memcpy(ptr_, bytes, size_); |
- ptr_[size_] = '\0'; |
- |
- env->ReleaseStringUTFChars(str, bytes); |
-} |
- |
-// Return true iff |address| is a valid address for the target CPU. |
-bool IsValidAddress(jlong address) { |
- return static_cast<jlong>(static_cast<size_t>(address)) == address; |
-} |
- |
-// Find the jclass JNI reference corresponding to a given |class_name|. |
-// |env| is the current JNI environment handle. |
-// On success, return true and set |*clazz|. |
-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; |
-} |
- |
-// Initialize a jfieldID corresponding to the field of a given |clazz|, |
-// with name |field_name| and signature |field_sig|. |
-// |env| is the current JNI environment handle. |
-// On success, return true and set |*field_id|. |
-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; |
-} |
- |
-// Initialize a jmethodID corresponding to the static method of a given |
-// |clazz|, with name |method_name| and signature |method_sig|. |
-// |env| is the current JNI environment handle. |
-// On success, return true and set |*method_id|. |
-bool InitStaticMethodId(JNIEnv* env, |
- jclass clazz, |
- const char* method_name, |
- const char* method_sig, |
- jmethodID* method_id) { |
- *method_id = env->GetStaticMethodID(clazz, method_name, method_sig); |
- if (!*method_id) { |
- LOG_ERROR("Could not find ID for static method '%s'", method_name); |
- return false; |
- } |
- LOG_INFO("%s: Found ID %p for static method '%s'", __FUNCTION__, *method_id, |
- method_name); |
- return true; |
-} |
- |
-// Initialize a jfieldID corresponding to the static field of a given |clazz|, |
-// with name |field_name| and signature |field_sig|. |
-// |env| is the current JNI environment handle. |
-// On success, return true and set |*field_id|. |
-bool InitStaticFieldId(JNIEnv* env, |
- jclass clazz, |
- const char* field_name, |
- const char* field_sig, |
- jfieldID* field_id) { |
- *field_id = env->GetStaticFieldID(clazz, field_name, field_sig); |
- if (!*field_id) { |
- LOG_ERROR("Could not find ID for static field '%s'", field_name); |
- return false; |
- } |
- LOG_INFO("%s: Found ID %p for static field '%s'", __FUNCTION__, *field_id, |
- field_name); |
- return true; |
-} |
- |
-// Initialize a jint corresponding to the static integer field of a class |
-// with class name |class_name| and field name |field_name|. |
-// |env| is the current JNI environment handle. |
-// On success, return true and set |*value|. |
-bool InitStaticInt(JNIEnv* env, |
- const char* class_name, |
- const char* field_name, |
- jint* value) { |
- jclass clazz; |
- if (!InitClassReference(env, class_name, &clazz)) |
- return false; |
- |
- jfieldID field_id; |
- if (!InitStaticFieldId(env, clazz, field_name, "I", &field_id)) |
- return false; |
- |
- *value = env->GetStaticIntField(clazz, field_id); |
- LOG_INFO("%s: Found value %d for class '%s', static field '%s'", __FUNCTION__, |
- *value, class_name, field_name); |
- |
- return true; |
-} |
- |
-// A class used to model the field IDs of the org.chromium.base.Linker |
-// LibInfo inner class, used to communicate data with the Java side |
-// of the linker. |
-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/library_loader/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); |
- } |
- |
- void SetLoadInfo(JNIEnv* env, |
- jobject library_info_obj, |
- size_t load_address, |
- size_t load_size) { |
- env->SetLongField(library_info_obj, load_address_id, load_address); |
- env->SetLongField(library_info_obj, load_size_id, load_size); |
- } |
- |
- // Use this instance to convert a RelroInfo reference into |
- // a crazy_library_info_t. |
- void GetRelroInfo(JNIEnv* env, |
- jobject library_info_obj, |
- size_t* relro_start, |
- size_t* relro_size, |
- int* relro_fd) { |
- *relro_start = static_cast<size_t>( |
- env->GetLongField(library_info_obj, relro_start_id)); |
- |
- *relro_size = |
- static_cast<size_t>(env->GetLongField(library_info_obj, relro_size_id)); |
- |
- *relro_fd = env->GetIntField(library_info_obj, relro_fd_id); |
- } |
- |
- void SetRelroInfo(JNIEnv* env, |
- jobject library_info_obj, |
- size_t relro_start, |
- size_t relro_size, |
- int relro_fd) { |
- env->SetLongField(library_info_obj, relro_start_id, relro_start); |
- env->SetLongField(library_info_obj, relro_size_id, relro_size); |
- env->SetIntField(library_info_obj, relro_fd_id, relro_fd); |
- } |
-}; |
- |
-static LibInfo_class s_lib_info_fields; |
- |
// Retrieve the SDK build version and pass it into the crazy linker. This |
// needs to be done early in initialization, before any other crazy linker |
// code is run. |
@@ -261,8 +33,7 @@ bool InitSDKVersionInfo(JNIEnv* env) { |
return false; |
crazy_set_sdk_build_version(static_cast<int>(value)); |
- LOG_INFO("%s: Set SDK build version to %d", __FUNCTION__, |
- static_cast<int>(value)); |
+ LOG_INFO("Set SDK build version to %d", static_cast<int>(value)); |
return true; |
} |
@@ -270,9 +41,9 @@ bool InitSDKVersionInfo(JNIEnv* env) { |
// The linker uses a single crazy_context_t object created on demand. |
// There is no need to protect this against concurrent access, locking |
// is already handled on the Java side. |
-static crazy_context_t* s_crazy_context; |
- |
crazy_context_t* GetCrazyContext() { |
+ static crazy_context_t* s_crazy_context = nullptr; |
+ |
if (!s_crazy_context) { |
// Create new context. |
s_crazy_context = crazy_context_create(); |
@@ -290,7 +61,7 @@ crazy_context_t* GetCrazyContext() { |
// on scope exit, unless Release() has been called. |
class ScopedLibrary { |
public: |
- ScopedLibrary() : lib_(NULL) {} |
+ ScopedLibrary() : lib_(nullptr) {} |
~ScopedLibrary() { |
if (lib_) |
@@ -303,7 +74,7 @@ class ScopedLibrary { |
crazy_library_t* Release() { |
crazy_library_t* ret = lib_; |
- lib_ = NULL; |
+ lib_ = nullptr; |
return ret; |
} |
@@ -311,18 +82,17 @@ class ScopedLibrary { |
crazy_library_t* lib_; |
}; |
-namespace { |
- |
template <class LibraryOpener> |
bool GenericLoadLibrary(JNIEnv* env, |
const char* library_name, |
jlong load_address, |
jobject lib_info_obj, |
const LibraryOpener& opener) { |
+ LOG_INFO("Called for %s, at address 0x%llx", library_name, load_address); |
crazy_context_t* context = GetCrazyContext(); |
if (!IsValidAddress(load_address)) { |
- LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); |
+ LOG_ERROR("Invalid address 0x%llx", load_address); |
return false; |
} |
@@ -336,8 +106,8 @@ bool GenericLoadLibrary(JNIEnv* env, |
crazy_library_info_t info; |
if (!crazy_library_get_info(library.Get(), context, &info)) { |
- LOG_ERROR("%s: Could not get library information for %s: %s", __FUNCTION__, |
- library_name, crazy_context_get_error(context)); |
+ LOG_ERROR("Could not get library information for %s: %s", library_name, |
+ crazy_context_get_error(context)); |
return false; |
} |
@@ -346,7 +116,7 @@ bool GenericLoadLibrary(JNIEnv* env, |
s_lib_info_fields.SetLoadInfo(env, lib_info_obj, info.load_address, |
info.load_size); |
- LOG_INFO("%s: Success loading library %s", __FUNCTION__, library_name); |
+ LOG_INFO("Success loading library %s", library_name); |
return true; |
} |
@@ -362,7 +132,7 @@ bool FileLibraryOpener::Open(crazy_library_t** library, |
const char* library_name, |
crazy_context_t* context) const { |
if (!crazy_library_open(library, library_name, context)) { |
- LOG_ERROR("%s: Could not open %s: %s", __FUNCTION__, library_name, |
+ LOG_ERROR("Could not open %s: %s", library_name, |
crazy_context_get_error(context)); |
return false; |
} |
@@ -386,20 +156,19 @@ bool ZipLibraryOpener::Open(crazy_library_t** library, |
crazy_context_t* context) const { |
if (!crazy_library_open_in_zip_file(library, zip_file_, library_name, |
context)) { |
- LOG_ERROR("%s: Could not open %s in zip file %s: %s", __FUNCTION__, |
- library_name, zip_file_, crazy_context_get_error(context)); |
+ LOG_ERROR("Could not open %s in zip file %s: %s", library_name, zip_file_, |
+ crazy_context_get_error(context)); |
return false; |
} |
return true; |
} |
-} // unnamed namespace |
- |
// Load a library with the chromium 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. |
+// |
// |env| is the current JNI environment handle. |
// |clazz| is the static class handle for org.chromium.base.Linker, |
// and is ignored here. |
@@ -415,6 +184,7 @@ jboolean LoadLibrary(JNIEnv* env, |
jobject lib_info_obj) { |
String lib_name(env, library_name); |
FileLibraryOpener opener; |
+ |
return GenericLoadLibrary(env, lib_name.c_str(), |
static_cast<size_t>(load_address), lib_info_obj, |
opener); |
@@ -451,6 +221,7 @@ jboolean LoadLibraryInZipFile(JNIEnv* env, |
String zipfile_name_str(env, zipfile_name); |
String lib_name(env, library_name); |
ZipLibraryOpener opener(zipfile_name_str.c_str()); |
+ |
return GenericLoadLibrary(env, lib_name.c_str(), |
static_cast<size_t>(load_address), lib_info_obj, |
opener); |
@@ -481,7 +252,7 @@ static JavaCallbackBindings_class s_java_callback_bindings; |
void RunCallbackOnUiThread(JNIEnv* env, jclass clazz, jlong arg) { |
crazy_callback_t* callback = reinterpret_cast<crazy_callback_t*>(arg); |
- LOG_INFO("%s: Called back from java with handler %p, opaque %p", __FUNCTION__, |
+ LOG_INFO("Called back from java with handler %p, opaque %p", |
callback->handler, callback->opaque); |
crazy_callback_run(callback); |
@@ -517,8 +288,8 @@ static bool PostForLaterExecution(crazy_callback_t* callback_request, |
crazy_callback_t* callback = new crazy_callback_t(); |
*callback = *callback_request; |
- LOG_INFO("%s: Calling back to java with handler %p, opaque %p", __FUNCTION__, |
- callback->handler, callback->opaque); |
+ LOG_INFO("Calling back to java with handler %p, opaque %p", callback->handler, |
+ callback->opaque); |
jlong arg = static_cast<jlong>(reinterpret_cast<uintptr_t>(callback)); |
@@ -543,16 +314,16 @@ jboolean CreateSharedRelro(JNIEnv* env, |
jobject lib_info_obj) { |
String lib_name(env, library_name); |
- LOG_INFO("%s: Called for %s", __FUNCTION__, lib_name.c_str()); |
+ LOG_INFO("Called for %s", lib_name.c_str()); |
if (!IsValidAddress(load_address)) { |
- LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); |
+ LOG_ERROR("Invalid address 0x%llx", load_address); |
return false; |
} |
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()); |
+ LOG_ERROR("Could not find %s", lib_name.c_str()); |
return false; |
} |
@@ -564,8 +335,8 @@ jboolean CreateSharedRelro(JNIEnv* env, |
if (!crazy_library_create_shared_relro( |
library.Get(), context, static_cast<size_t>(load_address), |
&relro_start, &relro_size, &relro_fd)) { |
- LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n", |
- __FUNCTION__, lib_name.c_str(), crazy_context_get_error(context)); |
+ LOG_ERROR("Could not create shared RELRO sharing for %s: %s\n", |
+ lib_name.c_str(), crazy_context_get_error(context)); |
return false; |
} |
@@ -580,12 +351,11 @@ jboolean UseSharedRelro(JNIEnv* env, |
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); |
+ LOG_INFO("Called for %s, lib_info_ref=%p", 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()); |
+ LOG_ERROR("Could not find %s", lib_name.c_str()); |
return false; |
} |
@@ -596,48 +366,21 @@ jboolean UseSharedRelro(JNIEnv* env, |
s_lib_info_fields.GetRelroInfo(env, lib_info_obj, &relro_start, &relro_size, |
&relro_fd); |
- LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", __FUNCTION__, |
- lib_name.c_str(), (void*)relro_start, (void*)relro_size, relro_fd); |
+ LOG_INFO("library=%s relro start=%p size=%p fd=%d", lib_name.c_str(), |
+ (void*)relro_start, (void*)relro_size, relro_fd); |
if (!crazy_library_use_shared_relro(library.Get(), context, relro_start, |
relro_size, relro_fd)) { |
- LOG_ERROR("%s: Could not use shared RELRO for %s: %s", __FUNCTION__, |
- lib_name.c_str(), crazy_context_get_error(context)); |
+ LOG_ERROR("Could not use shared RELRO for %s: %s", 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()); |
+ LOG_INFO("Library %s using shared RELRO section!", lib_name.c_str()); |
return true; |
} |
-jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { |
-#if RESERVE_BREAKPAD_GUARD_REGION |
- // Add a Breakpad guard region. 16Mb should be comfortably larger than |
- // the largest relocation packer saving we expect to encounter. |
- static const size_t kBreakpadGuardRegionBytes = 16 * 1024 * 1024; |
- bytes += kBreakpadGuardRegionBytes; |
-#endif |
- |
- void* address = |
- mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
- if (address == MAP_FAILED) { |
- LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); |
- return 0; |
- } |
- munmap(address, bytes); |
- |
-#if RESERVE_BREAKPAD_GUARD_REGION |
- // Allow for a Breakpad guard region ahead of the returned address. |
- address = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) + |
- kBreakpadGuardRegionBytes); |
-#endif |
- |
- LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); |
- return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); |
-} |
- |
const JNINativeMethod kNativeMethods[] = { |
{"nativeLoadLibrary", |
"(" |
@@ -677,53 +420,32 @@ const JNINativeMethod kNativeMethods[] = { |
")" |
"Z", |
reinterpret_cast<void*>(&UseSharedRelro)}, |
- {"nativeGetRandomBaseLoadAddress", |
- "(" |
- "J" |
- ")" |
- "J", |
- reinterpret_cast<void*>(&GetRandomBaseLoadAddress)}, |
}; |
} // namespace |
-// JNI_OnLoad() hook called when the linker library is loaded through |
-// the regular System.LoadLibrary) API. This shall save the Java VM |
-// handle and initialize LibInfo fields. |
-jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
- LOG_INFO("%s: Entering", __FUNCTION__); |
- // Get new JNIEnv |
- JNIEnv* env; |
- if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { |
- LOG_ERROR("Could not create JNIEnv"); |
- return -1; |
- } |
+bool LegacyLinkerJNIInit(JavaVM* vm, JNIEnv* env) { |
+ LOG_INFO("Entering"); |
// Initialize SDK version info. |
- LOG_INFO("%s: Retrieving SDK version info", __FUNCTION__); |
+ LOG_INFO("Retrieving SDK version info"); |
if (!InitSDKVersionInfo(env)) |
- return -1; |
+ return false; |
// Register native methods. |
jclass linker_class; |
if (!InitClassReference(env, "org/chromium/base/library_loader/LegacyLinker", |
&linker_class)) |
- return -1; |
+ return false; |
- LOG_INFO("%s: Registering native methods", __FUNCTION__); |
+ LOG_INFO("Registering native methods"); |
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; |
- } |
- |
// Resolve and save the Java side Linker callback class and method. |
- LOG_INFO("%s: Resolving callback bindings", __FUNCTION__); |
+ LOG_INFO("Resolving callback bindings"); |
if (!s_java_callback_bindings.Init(env, linker_class)) { |
- return -1; |
+ return false; |
} |
// Save JavaVM* handle into context. |
@@ -732,8 +454,9 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
// Register the function that the crazy linker can call to post code |
// for later execution. |
- crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); |
+ crazy_context_set_callback_poster(context, &PostForLaterExecution, nullptr); |
- LOG_INFO("%s: Done", __FUNCTION__); |
- return JNI_VERSION_1_4; |
+ return true; |
} |
+ |
+} // namespace chromium_android_linker |