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