OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 (e.g. libcontentshell.so). | |
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 #include <android/log.h> | |
17 #include <crazy_linker.h> | |
18 #include <fcntl.h> | |
19 #include <jni.h> | |
20 #include <limits.h> | |
21 #include <stdlib.h> | |
22 #include <sys/mman.h> | |
23 #include <unistd.h> | |
24 | |
25 // Set this to 1 to enable debug traces to the Android log. | |
26 // Note that LOG() from "base/logging.h" cannot be used, since it is | |
27 // in base/ which hasn't been loaded yet. | |
28 #define DEBUG 0 | |
29 | |
30 #define TAG "chromium_android_linker" | |
31 | |
32 #if DEBUG | |
33 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) | |
34 #else | |
35 #define LOG_INFO(...) ((void)0) | |
36 #endif | |
37 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) | |
38 | |
39 #define UNUSED __attribute__((unused)) | |
40 | |
41 namespace { | |
42 | |
43 // A simply scoped UTF String class that can be initialized from | |
44 // a Java jstring handle. Modeled like std::string, which cannot | |
45 // be used here. | |
46 class String { | |
47 public: | |
48 String(JNIEnv* env, jstring str); | |
49 | |
50 ~String() { | |
51 if (ptr_) | |
52 ::free(ptr_); | |
53 } | |
54 | |
55 const char* c_str() const { return ptr_ ? ptr_ : ""; } | |
56 size_t size() const { return size_; } | |
57 | |
58 private: | |
59 char* ptr_; | |
60 size_t size_; | |
61 }; | |
62 | |
63 String::String(JNIEnv* env, jstring str) { | |
64 size_ = env->GetStringUTFLength(str); | |
65 ptr_ = static_cast<char*>(::malloc(size_ + 1)); | |
66 | |
67 // Note: the result contains Java "modified UTF-8" bytes. | |
68 // Good enough for the linker though. | |
69 const char* bytes = env->GetStringUTFChars(str, NULL); | |
70 ::memcpy(ptr_, bytes, size_); | |
71 ptr_[size_] = '\0'; | |
72 | |
73 env->ReleaseStringUTFChars(str, bytes); | |
74 } | |
75 | |
76 // Return true iff |address| is a valid address for the target CPU. | |
77 bool IsValidAddress(jlong address) { | |
78 return static_cast<jlong>(static_cast<size_t>(address)) == address; | |
79 } | |
80 | |
81 // Find the jclass JNI reference corresponding to a given |class_name|. | |
82 // |env| is the current JNI environment handle. | |
83 // On success, return true and set |*clazz|. | |
84 bool InitClassReference(JNIEnv* env, const char* class_name, jclass* clazz) { | |
85 *clazz = env->FindClass(class_name); | |
86 if (!*clazz) { | |
87 LOG_ERROR("Could not find class for %s", class_name); | |
88 return false; | |
89 } | |
90 return true; | |
91 } | |
92 | |
93 // Initialize a jfieldID corresponding to the field of a given |clazz|, | |
94 // with name |field_name| and signature |field_sig|. | |
95 // |env| is the current JNI environment handle. | |
96 // On success, return true and set |*field_id|. | |
97 bool InitFieldId(JNIEnv* env, | |
98 jclass clazz, | |
99 const char* field_name, | |
100 const char* field_sig, | |
101 jfieldID* field_id) { | |
102 *field_id = env->GetFieldID(clazz, field_name, field_sig); | |
103 if (!*field_id) { | |
104 LOG_ERROR("Could not find ID for field '%s'", field_name); | |
105 return false; | |
106 } | |
107 LOG_INFO( | |
108 "%s: Found ID %p for field '%s'", __FUNCTION__, *field_id, field_name); | |
109 return true; | |
110 } | |
111 | |
112 // Initialize a jmethodID corresponding to the static method of a given | |
113 // |clazz|, with name |method_name| and signature |method_sig|. | |
114 // |env| is the current JNI environment handle. | |
115 // On success, return true and set |*method_id|. | |
116 bool InitStaticMethodId(JNIEnv* env, | |
117 jclass clazz, | |
118 const char* method_name, | |
119 const char* method_sig, | |
120 jmethodID* method_id) { | |
121 *method_id = env->GetStaticMethodID(clazz, method_name, method_sig); | |
122 if (!*method_id) { | |
123 LOG_ERROR("Could not find ID for static method '%s'", method_name); | |
124 return false; | |
125 } | |
126 LOG_INFO("%s: Found ID %p for static method '%s'", | |
127 __FUNCTION__, *method_id, method_name); | |
128 return true; | |
129 } | |
130 | |
131 // Initialize a jfieldID corresponding to the static field of a given |clazz|, | |
132 // with name |field_name| and signature |field_sig|. | |
133 // |env| is the current JNI environment handle. | |
134 // On success, return true and set |*field_id|. | |
135 bool InitStaticFieldId(JNIEnv* env, | |
136 jclass clazz, | |
137 const char* field_name, | |
138 const char* field_sig, | |
139 jfieldID* field_id) { | |
140 *field_id = env->GetStaticFieldID(clazz, field_name, field_sig); | |
141 if (!*field_id) { | |
142 LOG_ERROR("Could not find ID for static field '%s'", field_name); | |
143 return false; | |
144 } | |
145 LOG_INFO( | |
146 "%s: Found ID %p for static field '%s'", | |
147 __FUNCTION__, *field_id, field_name); | |
148 return true; | |
149 } | |
150 | |
151 // Initialize a jint corresponding to the static integer field of a class | |
152 // with class name |class_name| and field name |field_name|. | |
153 // |env| is the current JNI environment handle. | |
154 // On success, return true and set |*value|. | |
155 bool InitStaticInt(JNIEnv* env, | |
156 const char* class_name, | |
157 const char* field_name, | |
158 jint* value) { | |
159 jclass clazz; | |
160 if (!InitClassReference(env, class_name, &clazz)) | |
161 return false; | |
162 | |
163 jfieldID field_id; | |
164 if (!InitStaticFieldId(env, clazz, field_name, "I", &field_id)) | |
165 return false; | |
166 | |
167 *value = env->GetStaticIntField(clazz, field_id); | |
168 LOG_INFO( | |
169 "%s: Found value %d for class '%s', static field '%s'", | |
170 __FUNCTION__, *value, class_name, field_name); | |
171 | |
172 return true; | |
173 } | |
174 | |
175 // A class used to model the field IDs of the org.chromium.base.Linker | |
176 // LibInfo inner class, used to communicate data with the Java side | |
177 // of the linker. | |
178 struct LibInfo_class { | |
179 jfieldID load_address_id; | |
180 jfieldID load_size_id; | |
181 jfieldID relro_start_id; | |
182 jfieldID relro_size_id; | |
183 jfieldID relro_fd_id; | |
184 | |
185 // Initialize an instance. | |
186 bool Init(JNIEnv* env) { | |
187 jclass clazz; | |
188 if (!InitClassReference( | |
189 env, "org/chromium/base/library_loader/Linker$LibInfo", &clazz)) { | |
190 return false; | |
191 } | |
192 | |
193 return InitFieldId(env, clazz, "mLoadAddress", "J", &load_address_id) && | |
194 InitFieldId(env, clazz, "mLoadSize", "J", &load_size_id) && | |
195 InitFieldId(env, clazz, "mRelroStart", "J", &relro_start_id) && | |
196 InitFieldId(env, clazz, "mRelroSize", "J", &relro_size_id) && | |
197 InitFieldId(env, clazz, "mRelroFd", "I", &relro_fd_id); | |
198 } | |
199 | |
200 void SetLoadInfo(JNIEnv* env, | |
201 jobject library_info_obj, | |
202 size_t load_address, | |
203 size_t load_size) { | |
204 env->SetLongField(library_info_obj, load_address_id, load_address); | |
205 env->SetLongField(library_info_obj, load_size_id, load_size); | |
206 } | |
207 | |
208 // Use this instance to convert a RelroInfo reference into | |
209 // a crazy_library_info_t. | |
210 void GetRelroInfo(JNIEnv* env, | |
211 jobject library_info_obj, | |
212 size_t* relro_start, | |
213 size_t* relro_size, | |
214 int* relro_fd) { | |
215 *relro_start = static_cast<size_t>( | |
216 env->GetLongField(library_info_obj, relro_start_id)); | |
217 | |
218 *relro_size = | |
219 static_cast<size_t>(env->GetLongField(library_info_obj, relro_size_id)); | |
220 | |
221 *relro_fd = env->GetIntField(library_info_obj, relro_fd_id); | |
222 } | |
223 | |
224 void SetRelroInfo(JNIEnv* env, | |
225 jobject library_info_obj, | |
226 size_t relro_start, | |
227 size_t relro_size, | |
228 int relro_fd) { | |
229 env->SetLongField(library_info_obj, relro_start_id, relro_start); | |
230 env->SetLongField(library_info_obj, relro_size_id, relro_size); | |
231 env->SetIntField(library_info_obj, relro_fd_id, relro_fd); | |
232 } | |
233 }; | |
234 | |
235 static LibInfo_class s_lib_info_fields; | |
236 | |
237 // Retrieve the SDK build version and pass it into the crazy linker. This | |
238 // needs to be done early in initialization, before any other crazy linker | |
239 // code is run. | |
240 // |env| is the current JNI environment handle. | |
241 // On success, return true. | |
242 bool InitSDKVersionInfo(JNIEnv* env) { | |
243 jint value = 0; | |
244 if (!InitStaticInt(env, "android/os/Build$VERSION", "SDK_INT", &value)) | |
245 return false; | |
246 | |
247 crazy_set_sdk_build_version(static_cast<int>(value)); | |
248 LOG_INFO("%s: Set SDK build version to %d", | |
249 __FUNCTION__, static_cast<int>(value)); | |
250 | |
251 return true; | |
252 } | |
253 | |
254 // The linker uses a single crazy_context_t object created on demand. | |
255 // There is no need to protect this against concurrent access, locking | |
256 // is already handled on the Java side. | |
257 static crazy_context_t* s_crazy_context; | |
258 | |
259 crazy_context_t* GetCrazyContext() { | |
260 if (!s_crazy_context) { | |
261 // Create new context. | |
262 s_crazy_context = crazy_context_create(); | |
263 | |
264 // Ensure libraries located in the same directory as the linker | |
265 // can be loaded before system ones. | |
266 crazy_context_add_search_path_for_address( | |
267 s_crazy_context, reinterpret_cast<void*>(&s_crazy_context)); | |
268 } | |
269 | |
270 return s_crazy_context; | |
271 } | |
272 | |
273 // A scoped crazy_library_t that automatically closes the handle | |
274 // on scope exit, unless Release() has been called. | |
275 class ScopedLibrary { | |
276 public: | |
277 ScopedLibrary() : lib_(NULL) {} | |
278 | |
279 ~ScopedLibrary() { | |
280 if (lib_) | |
281 crazy_library_close_with_context(lib_, GetCrazyContext()); | |
282 } | |
283 | |
284 crazy_library_t* Get() { return lib_; } | |
285 | |
286 crazy_library_t** GetPtr() { return &lib_; } | |
287 | |
288 crazy_library_t* Release() { | |
289 crazy_library_t* ret = lib_; | |
290 lib_ = NULL; | |
291 return ret; | |
292 } | |
293 | |
294 private: | |
295 crazy_library_t* lib_; | |
296 }; | |
297 | |
298 namespace { | |
299 | |
300 template <class LibraryOpener> | |
301 bool GenericLoadLibrary( | |
302 JNIEnv* env, | |
303 const char* library_name, jlong load_address, jobject lib_info_obj, | |
304 const LibraryOpener& opener) { | |
305 crazy_context_t* context = GetCrazyContext(); | |
306 | |
307 if (!IsValidAddress(load_address)) { | |
308 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
309 return false; | |
310 } | |
311 | |
312 // Set the desired load address (0 means randomize it). | |
313 crazy_context_set_load_address(context, static_cast<size_t>(load_address)); | |
314 | |
315 ScopedLibrary library; | |
316 if (!opener.Open(library.GetPtr(), library_name, context)) { | |
317 return false; | |
318 } | |
319 | |
320 crazy_library_info_t info; | |
321 if (!crazy_library_get_info(library.Get(), context, &info)) { | |
322 LOG_ERROR("%s: Could not get library information for %s: %s", | |
323 __FUNCTION__, | |
324 library_name, | |
325 crazy_context_get_error(context)); | |
326 return false; | |
327 } | |
328 | |
329 // Release library object to keep it alive after the function returns. | |
330 library.Release(); | |
331 | |
332 s_lib_info_fields.SetLoadInfo( | |
333 env, lib_info_obj, info.load_address, info.load_size); | |
334 LOG_INFO("%s: Success loading library %s", __FUNCTION__, library_name); | |
335 return true; | |
336 } | |
337 | |
338 // Used for opening the library in a regular file. | |
339 class FileLibraryOpener { | |
340 public: | |
341 bool Open( | |
342 crazy_library_t** library, | |
343 const char* library_name, | |
344 crazy_context_t* context) const; | |
345 }; | |
346 | |
347 bool FileLibraryOpener::Open( | |
348 crazy_library_t** library, | |
349 const char* library_name, | |
350 crazy_context_t* context) const { | |
351 if (!crazy_library_open(library, library_name, context)) { | |
352 LOG_ERROR("%s: Could not open %s: %s", | |
353 __FUNCTION__, | |
354 library_name, | |
355 crazy_context_get_error(context)); | |
356 return false; | |
357 } | |
358 return true; | |
359 } | |
360 | |
361 // Used for opening the library in a zip file. | |
362 class ZipLibraryOpener { | |
363 public: | |
364 explicit ZipLibraryOpener(const char* zip_file) : zip_file_(zip_file) {} | |
365 bool Open( | |
366 crazy_library_t** library, | |
367 const char* library_name, | |
368 crazy_context_t* context) const; | |
369 private: | |
370 const char* zip_file_; | |
371 }; | |
372 | |
373 bool ZipLibraryOpener::Open( | |
374 crazy_library_t** library, | |
375 const char* library_name, | |
376 crazy_context_t* context) const { | |
377 if (!crazy_library_open_in_zip_file( | |
378 library, zip_file_, library_name, context)) { | |
379 LOG_ERROR("%s: Could not open %s in zip file %s: %s", | |
380 __FUNCTION__, library_name, zip_file_, | |
381 crazy_context_get_error(context)); | |
382 return false; | |
383 } | |
384 return true; | |
385 } | |
386 | |
387 } // unnamed namespace | |
388 | |
389 // Load a library with the chromium linker. This will also call its | |
390 // JNI_OnLoad() method, which shall register its methods. Note that | |
391 // lazy native method resolution will _not_ work after this, because | |
392 // Dalvik uses the system's dlsym() which won't see the new library, | |
393 // so explicit registration is mandatory. | |
394 // |env| is the current JNI environment handle. | |
395 // |clazz| is the static class handle for org.chromium.base.Linker, | |
396 // and is ignored here. | |
397 // |library_name| is the library name (e.g. libfoo.so). | |
398 // |load_address| is an explicit load address. | |
399 // |library_info| is a LibInfo handle used to communicate information | |
400 // with the Java side. | |
401 // Return true on success. | |
402 jboolean LoadLibrary(JNIEnv* env, | |
403 jclass clazz, | |
404 jstring library_name, | |
405 jlong load_address, | |
406 jobject lib_info_obj) { | |
407 String lib_name(env, library_name); | |
408 FileLibraryOpener opener; | |
409 return GenericLoadLibrary( | |
410 env, lib_name.c_str(), | |
411 static_cast<size_t>(load_address), lib_info_obj, opener); | |
412 } | |
413 | |
414 // Load a library from a zipfile with the chromium linker. The | |
415 // library in the zipfile must be uncompressed and page aligned. | |
416 // The basename of the library is given. The library is expected | |
417 // to be lib/<abi_tag>/crazy.<basename>. The <abi_tag> used will be the | |
418 // same as the abi for this linker. The "crazy." prefix is included | |
419 // so that the Android Package Manager doesn't extract the library into | |
420 // /data/app-lib. | |
421 // | |
422 // Loading the library will also call its JNI_OnLoad() method, which | |
423 // shall register its methods. Note that lazy native method resolution | |
424 // will _not_ work after this, because Dalvik uses the system's dlsym() | |
425 // which won't see the new library, so explicit registration is mandatory. | |
426 // | |
427 // |env| is the current JNI environment handle. | |
428 // |clazz| is the static class handle for org.chromium.base.Linker, | |
429 // and is ignored here. | |
430 // |zipfile_name| is the filename of the zipfile containing the library. | |
431 // |library_name| is the library base name (e.g. libfoo.so). | |
432 // |load_address| is an explicit load address. | |
433 // |library_info| is a LibInfo handle used to communicate information | |
434 // with the Java side. | |
435 // Returns true on success. | |
436 jboolean LoadLibraryInZipFile(JNIEnv* env, | |
437 jclass clazz, | |
438 jstring zipfile_name, | |
439 jstring library_name, | |
440 jlong load_address, | |
441 jobject lib_info_obj) { | |
442 String zipfile_name_str(env, zipfile_name); | |
443 String lib_name(env, library_name); | |
444 ZipLibraryOpener opener(zipfile_name_str.c_str()); | |
445 return GenericLoadLibrary( | |
446 env, lib_name.c_str(), | |
447 static_cast<size_t>(load_address), lib_info_obj, opener); | |
448 } | |
449 | |
450 // Class holding the Java class and method ID for the Java side Linker | |
451 // postCallbackOnMainThread method. | |
452 struct JavaCallbackBindings_class { | |
453 jclass clazz; | |
454 jmethodID method_id; | |
455 | |
456 // Initialize an instance. | |
457 bool Init(JNIEnv* env, jclass linker_class) { | |
458 clazz = reinterpret_cast<jclass>(env->NewGlobalRef(linker_class)); | |
459 return InitStaticMethodId(env, | |
460 linker_class, | |
461 "postCallbackOnMainThread", | |
462 "(J)V", | |
463 &method_id); | |
464 } | |
465 }; | |
466 | |
467 static JavaCallbackBindings_class s_java_callback_bindings; | |
468 | |
469 // Designated receiver function for callbacks from Java. Its name is known | |
470 // to the Java side. | |
471 // |env| is the current JNI environment handle and is ignored here. | |
472 // |clazz| is the static class handle for org.chromium.base.Linker, | |
473 // and is ignored here. | |
474 // |arg| is a pointer to an allocated crazy_callback_t, deleted after use. | |
475 void RunCallbackOnUiThread(JNIEnv* env, jclass clazz, jlong arg) { | |
476 crazy_callback_t* callback = reinterpret_cast<crazy_callback_t*>(arg); | |
477 | |
478 LOG_INFO("%s: Called back from java with handler %p, opaque %p", | |
479 __FUNCTION__, callback->handler, callback->opaque); | |
480 | |
481 crazy_callback_run(callback); | |
482 delete callback; | |
483 } | |
484 | |
485 // Request a callback from Java. The supplied crazy_callback_t is valid only | |
486 // for the duration of this call, so we copy it to a newly allocated | |
487 // crazy_callback_t and then call the Java side's postCallbackOnMainThread. | |
488 // This will call back to to our RunCallbackOnUiThread some time | |
489 // later on the UI thread. | |
490 // |callback_request| is a crazy_callback_t. | |
491 // |poster_opaque| is unused. | |
492 // Returns true if the callback request succeeds. | |
493 static bool PostForLaterExecution(crazy_callback_t* callback_request, | |
494 void* poster_opaque UNUSED) { | |
495 crazy_context_t* context = GetCrazyContext(); | |
496 | |
497 JavaVM* vm; | |
498 int minimum_jni_version; | |
499 crazy_context_get_java_vm(context, | |
500 reinterpret_cast<void**>(&vm), | |
501 &minimum_jni_version); | |
502 | |
503 // Do not reuse JNIEnv from JNI_OnLoad, but retrieve our own. | |
504 JNIEnv* env; | |
505 if (JNI_OK != vm->GetEnv( | |
506 reinterpret_cast<void**>(&env), minimum_jni_version)) { | |
507 LOG_ERROR("Could not create JNIEnv"); | |
508 return false; | |
509 } | |
510 | |
511 // Copy the callback; the one passed as an argument may be temporary. | |
512 crazy_callback_t* callback = new crazy_callback_t(); | |
513 *callback = *callback_request; | |
514 | |
515 LOG_INFO("%s: Calling back to java with handler %p, opaque %p", | |
516 __FUNCTION__, callback->handler, callback->opaque); | |
517 | |
518 jlong arg = static_cast<jlong>(reinterpret_cast<uintptr_t>(callback)); | |
519 | |
520 env->CallStaticVoidMethod( | |
521 s_java_callback_bindings.clazz, s_java_callback_bindings.method_id, arg); | |
522 | |
523 // Back out and return false if we encounter a JNI exception. | |
524 if (env->ExceptionCheck() == JNI_TRUE) { | |
525 env->ExceptionDescribe(); | |
526 env->ExceptionClear(); | |
527 delete callback; | |
528 return false; | |
529 } | |
530 | |
531 return true; | |
532 } | |
533 | |
534 jboolean CreateSharedRelro(JNIEnv* env, | |
535 jclass clazz, | |
536 jstring library_name, | |
537 jlong load_address, | |
538 jobject lib_info_obj) { | |
539 String lib_name(env, library_name); | |
540 | |
541 LOG_INFO("%s: Called for %s", __FUNCTION__, lib_name.c_str()); | |
542 | |
543 if (!IsValidAddress(load_address)) { | |
544 LOG_ERROR("%s: Invalid address 0x%llx", __FUNCTION__, load_address); | |
545 return false; | |
546 } | |
547 | |
548 ScopedLibrary library; | |
549 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
550 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
551 return false; | |
552 } | |
553 | |
554 crazy_context_t* context = GetCrazyContext(); | |
555 size_t relro_start = 0; | |
556 size_t relro_size = 0; | |
557 int relro_fd = -1; | |
558 | |
559 if (!crazy_library_create_shared_relro(library.Get(), | |
560 context, | |
561 static_cast<size_t>(load_address), | |
562 &relro_start, | |
563 &relro_size, | |
564 &relro_fd)) { | |
565 LOG_ERROR("%s: Could not create shared RELRO sharing for %s: %s\n", | |
566 __FUNCTION__, | |
567 lib_name.c_str(), | |
568 crazy_context_get_error(context)); | |
569 return false; | |
570 } | |
571 | |
572 s_lib_info_fields.SetRelroInfo( | |
573 env, lib_info_obj, relro_start, relro_size, relro_fd); | |
574 return true; | |
575 } | |
576 | |
577 jboolean UseSharedRelro(JNIEnv* env, | |
578 jclass clazz, | |
579 jstring library_name, | |
580 jobject lib_info_obj) { | |
581 String lib_name(env, library_name); | |
582 | |
583 LOG_INFO("%s: called for %s, lib_info_ref=%p", | |
584 __FUNCTION__, | |
585 lib_name.c_str(), | |
586 lib_info_obj); | |
587 | |
588 ScopedLibrary library; | |
589 if (!crazy_library_find_by_name(lib_name.c_str(), library.GetPtr())) { | |
590 LOG_ERROR("%s: Could not find %s", __FUNCTION__, lib_name.c_str()); | |
591 return false; | |
592 } | |
593 | |
594 crazy_context_t* context = GetCrazyContext(); | |
595 size_t relro_start = 0; | |
596 size_t relro_size = 0; | |
597 int relro_fd = -1; | |
598 s_lib_info_fields.GetRelroInfo( | |
599 env, lib_info_obj, &relro_start, &relro_size, &relro_fd); | |
600 | |
601 LOG_INFO("%s: library=%s relro start=%p size=%p fd=%d", | |
602 __FUNCTION__, | |
603 lib_name.c_str(), | |
604 (void*)relro_start, | |
605 (void*)relro_size, | |
606 relro_fd); | |
607 | |
608 if (!crazy_library_use_shared_relro( | |
609 library.Get(), context, relro_start, relro_size, relro_fd)) { | |
610 LOG_ERROR("%s: Could not use shared RELRO for %s: %s", | |
611 __FUNCTION__, | |
612 lib_name.c_str(), | |
613 crazy_context_get_error(context)); | |
614 return false; | |
615 } | |
616 | |
617 LOG_INFO("%s: Library %s using shared RELRO section!", | |
618 __FUNCTION__, | |
619 lib_name.c_str()); | |
620 | |
621 return true; | |
622 } | |
623 | |
624 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { | |
625 return crazy_system_can_share_relro(); | |
626 } | |
627 | |
628 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { | |
629 void* address = | |
630 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
631 if (address == MAP_FAILED) { | |
632 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); | |
633 return 0; | |
634 } | |
635 munmap(address, bytes); | |
636 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); | |
637 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); | |
638 } | |
639 | |
640 // Get the full path of a library in the zip file | |
641 // (lib/<abi>/crazy.<lib_name>). | |
642 // | |
643 // |env| is the current JNI environment handle. | |
644 // |clazz| is the static class handle which is not used here. | |
645 // |lib_name| is the library base name. | |
646 // Returns the full path (or empty string on failure). | |
647 jstring GetLibraryFilePathInZipFile(JNIEnv* env, | |
648 jclass clazz, | |
649 jstring lib_name) { | |
650 String lib_name_str(env, lib_name); | |
651 const char* lib_name_c_str = lib_name_str.c_str(); | |
652 char buffer[kMaxFilePathLengthInZip + 1]; | |
653 if (crazy_library_file_path_in_zip_file( | |
654 lib_name_c_str, buffer, sizeof(buffer)) == CRAZY_STATUS_FAILURE) { | |
655 LOG_ERROR("%s: Failed to get full filename for library '%s'", | |
656 __FUNCTION__, lib_name_c_str); | |
657 buffer[0] = '\0'; | |
658 } | |
659 return env->NewStringUTF(buffer); | |
660 } | |
661 | |
662 // Check whether a library is page aligned and uncompressed in the APK file. | |
663 // | |
664 // |env| is the current JNI environment handle. | |
665 // |clazz| is the static class handle which is not used here. | |
666 // |apkfile_name| is the filename of the APK. | |
667 // |library_name| is the library base name. | |
668 // Returns true if page aligned and uncompressed. | |
669 jboolean CheckLibraryIsMappableInApk(JNIEnv* env, jclass clazz, | |
670 jstring apkfile_name, | |
671 jstring library_name) { | |
672 String apkfile_name_str(env, apkfile_name); | |
673 const char* apkfile_name_c_str = apkfile_name_str.c_str(); | |
674 String library_name_str(env, library_name); | |
675 const char* library_name_c_str = library_name_str.c_str(); | |
676 | |
677 LOG_INFO("%s: Checking if %s is page-aligned and uncompressed in %s\n", | |
678 __FUNCTION__, library_name_c_str, apkfile_name_c_str); | |
679 jboolean mappable = crazy_linker_check_library_is_mappable_in_zip_file( | |
680 apkfile_name_c_str, library_name_c_str) == CRAZY_STATUS_SUCCESS; | |
681 LOG_INFO("%s: %s\n", __FUNCTION__, mappable ? "Mappable" : "NOT mappable"); | |
682 | |
683 return mappable; | |
684 } | |
685 | |
686 const JNINativeMethod kNativeMethods[] = { | |
687 {"nativeLoadLibrary", | |
688 "(" | |
689 "Ljava/lang/String;" | |
690 "J" | |
691 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
692 ")" | |
693 "Z", | |
694 reinterpret_cast<void*>(&LoadLibrary)}, | |
695 {"nativeLoadLibraryInZipFile", | |
696 "(" | |
697 "Ljava/lang/String;" | |
698 "Ljava/lang/String;" | |
699 "J" | |
700 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
701 ")" | |
702 "Z", | |
703 reinterpret_cast<void*>(&LoadLibraryInZipFile)}, | |
704 {"nativeRunCallbackOnUiThread", | |
705 "(" | |
706 "J" | |
707 ")" | |
708 "V", | |
709 reinterpret_cast<void*>(&RunCallbackOnUiThread)}, | |
710 {"nativeCreateSharedRelro", | |
711 "(" | |
712 "Ljava/lang/String;" | |
713 "J" | |
714 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
715 ")" | |
716 "Z", | |
717 reinterpret_cast<void*>(&CreateSharedRelro)}, | |
718 {"nativeUseSharedRelro", | |
719 "(" | |
720 "Ljava/lang/String;" | |
721 "Lorg/chromium/base/library_loader/Linker$LibInfo;" | |
722 ")" | |
723 "Z", | |
724 reinterpret_cast<void*>(&UseSharedRelro)}, | |
725 {"nativeCanUseSharedRelro", | |
726 "(" | |
727 ")" | |
728 "Z", | |
729 reinterpret_cast<void*>(&CanUseSharedRelro)}, | |
730 {"nativeGetRandomBaseLoadAddress", | |
731 "(" | |
732 "J" | |
733 ")" | |
734 "J", | |
735 reinterpret_cast<void*>(&GetRandomBaseLoadAddress)}, | |
736 {"nativeGetLibraryFilePathInZipFile", | |
737 "(" | |
738 "Ljava/lang/String;" | |
739 ")" | |
740 "Ljava/lang/String;", | |
741 reinterpret_cast<void*>(&GetLibraryFilePathInZipFile)}, | |
742 {"nativeCheckLibraryIsMappableInApk", | |
743 "(" | |
744 "Ljava/lang/String;" | |
745 "Ljava/lang/String;" | |
746 ")" | |
747 "Z", | |
748 reinterpret_cast<void*>(&CheckLibraryIsMappableInApk)}, }; | |
749 | |
750 } // namespace | |
751 | |
752 // JNI_OnLoad() hook called when the linker library is loaded through | |
753 // the regular System.LoadLibrary) API. This shall save the Java VM | |
754 // handle and initialize LibInfo fields. | |
755 jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
756 LOG_INFO("%s: Entering", __FUNCTION__); | |
757 // Get new JNIEnv | |
758 JNIEnv* env; | |
759 if (JNI_OK != vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { | |
760 LOG_ERROR("Could not create JNIEnv"); | |
761 return -1; | |
762 } | |
763 | |
764 // Initialize SDK version info. | |
765 LOG_INFO("%s: Retrieving SDK version info", __FUNCTION__); | |
766 if (!InitSDKVersionInfo(env)) | |
767 return -1; | |
768 | |
769 // Register native methods. | |
770 jclass linker_class; | |
771 if (!InitClassReference(env, | |
772 "org/chromium/base/library_loader/Linker", | |
773 &linker_class)) | |
774 return -1; | |
775 | |
776 LOG_INFO("%s: Registering native methods", __FUNCTION__); | |
777 env->RegisterNatives(linker_class, | |
778 kNativeMethods, | |
779 sizeof(kNativeMethods) / sizeof(kNativeMethods[0])); | |
780 | |
781 // Find LibInfo field ids. | |
782 LOG_INFO("%s: Caching field IDs", __FUNCTION__); | |
783 if (!s_lib_info_fields.Init(env)) { | |
784 return -1; | |
785 } | |
786 | |
787 // Resolve and save the Java side Linker callback class and method. | |
788 LOG_INFO("%s: Resolving callback bindings", __FUNCTION__); | |
789 if (!s_java_callback_bindings.Init(env, linker_class)) { | |
790 return -1; | |
791 } | |
792 | |
793 // Save JavaVM* handle into context. | |
794 crazy_context_t* context = GetCrazyContext(); | |
795 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); | |
796 | |
797 // Register the function that the crazy linker can call to post code | |
798 // for later execution. | |
799 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); | |
800 | |
801 LOG_INFO("%s: Done", __FUNCTION__); | |
802 return JNI_VERSION_1_4; | |
803 } | |
OLD | NEW |