Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_Runtime.h" |
| 18 | |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 19 | #include <dlfcn.h> |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 23 | #include "base/macros.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "gc/heap.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 25 | #include "handle_scope-inl.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 26 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 28 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 29 | #include "scoped_thread_state_change.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 30 | #include "ScopedUtfChars.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 31 | #include "verify_object-inl.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 32 | |
Dmitriy Ivanov | 785049f | 2014-07-18 10:08:57 -0700 | [diff] [blame] | 33 | #include <sstream> |
| 34 | #ifdef HAVE_ANDROID_OS |
| 35 | // This function is provided by android linker. |
| 36 | extern "C" void android_update_LD_LIBRARY_PATH(const char* ld_library_path); |
| 37 | #endif // HAVE_ANDROID_OS |
| 38 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | static void Runtime_gc(JNIEnv*, jclass) { |
Anwar Ghuloum | 8718359 | 2013-08-14 12:12:19 -0700 | [diff] [blame] | 42 | if (Runtime::Current()->IsExplicitGcDisabled()) { |
| 43 | LOG(INFO) << "Explicit GC skipped."; |
| 44 | return; |
| 45 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 46 | Runtime::Current()->GetHeap()->CollectGarbage(false); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 49 | NO_RETURN static void Runtime_nativeExit(JNIEnv*, jclass, jint status) { |
Dmitry Petrochenko | 108437e | 2014-07-03 17:07:45 +0700 | [diff] [blame] | 50 | LOG(INFO) << "System.exit called, status: " << status; |
Elliott Hughes | 28c7bfd | 2012-06-01 12:45:40 -0700 | [diff] [blame] | 51 | Runtime::Current()->CallExitHook(status); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 52 | exit(status); |
| 53 | } |
| 54 | |
Dmitriy Ivanov | 785049f | 2014-07-18 10:08:57 -0700 | [diff] [blame] | 55 | static void SetLdLibraryPath(JNIEnv* env, jstring javaLdLibraryPathJstr, jstring javaDexPathJstr) { |
| 56 | #ifdef HAVE_ANDROID_OS |
| 57 | std::stringstream ss; |
| 58 | if (javaLdLibraryPathJstr != nullptr) { |
| 59 | ScopedUtfChars javaLdLibraryPath(env, javaLdLibraryPathJstr); |
| 60 | if (javaLdLibraryPath.c_str() != nullptr) { |
| 61 | ss << javaLdLibraryPath.c_str(); |
| 62 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Dmitriy Ivanov | 785049f | 2014-07-18 10:08:57 -0700 | [diff] [blame] | 65 | if (javaDexPathJstr != nullptr) { |
| 66 | ScopedUtfChars javaDexPath(env, javaDexPathJstr); |
| 67 | if (javaDexPath.c_str() != nullptr) { |
| 68 | std::vector<std::string> dexPathVector; |
| 69 | Split(javaDexPath.c_str(), ':', &dexPathVector); |
| 70 | |
| 71 | for (auto abi : art::Runtime::Current()->GetCpuAbilist()) { |
| 72 | for (auto zip_path : dexPathVector) { |
| 73 | // Native libraries live under lib/<abi>/ inside .apk file. |
| 74 | ss << ":" << zip_path << "!" << "lib/" << abi; |
| 75 | } |
| 76 | } |
Elliott Hughes | c8fece3 | 2013-01-02 11:27:23 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Dmitriy Ivanov | 785049f | 2014-07-18 10:08:57 -0700 | [diff] [blame] | 80 | std::string ldLibraryPathStr = ss.str(); |
| 81 | const char* ldLibraryPath = ldLibraryPathStr.c_str(); |
| 82 | if (*ldLibraryPath == ':') { |
| 83 | ++ldLibraryPath; |
| 84 | } |
| 85 | |
| 86 | android_update_LD_LIBRARY_PATH(ldLibraryPath); |
| 87 | #else |
| 88 | LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!"; |
| 89 | UNUSED(javaLdLibraryPathJstr, javaDexPathJstr, env); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader, |
| 94 | jstring javaLdLibraryPathJstr, jstring javaDexPathJstr) { |
| 95 | ScopedUtfChars filename(env, javaFilename); |
| 96 | if (filename.c_str() == nullptr) { |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | SetLdLibraryPath(env, javaLdLibraryPathJstr, javaDexPathJstr); |
| 101 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 102 | std::string error_msg; |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 103 | { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 104 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 105 | bool success = vm->LoadNativeLibrary(env, filename.c_str(), javaLoader, &error_msg); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 106 | if (success) { |
| 107 | return nullptr; |
| 108 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Brian Carlstrom | 75fe90c | 2013-06-26 22:26:16 -0700 | [diff] [blame] | 111 | // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF. |
| 112 | env->ExceptionClear(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 113 | return env->NewStringUTF(error_msg.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 116 | static jlong Runtime_maxMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 117 | return Runtime::Current()->GetHeap()->GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 120 | static jlong Runtime_totalMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 121 | return Runtime::Current()->GetHeap()->GetTotalMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 124 | static jlong Runtime_freeMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 125 | return Runtime::Current()->GetHeap()->GetFreeMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 129 | NATIVE_METHOD(Runtime, freeMemory, "!()J"), |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 130 | NATIVE_METHOD(Runtime, gc, "()V"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 131 | NATIVE_METHOD(Runtime, maxMemory, "!()J"), |
Elliott Hughes | 28c7bfd | 2012-06-01 12:45:40 -0700 | [diff] [blame] | 132 | NATIVE_METHOD(Runtime, nativeExit, "(I)V"), |
Dmitriy Ivanov | 785049f | 2014-07-18 10:08:57 -0700 | [diff] [blame] | 133 | NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 134 | NATIVE_METHOD(Runtime, totalMemory, "!()J"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 137 | void register_java_lang_Runtime(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 138 | REGISTER_NATIVE_METHODS("java/lang/Runtime"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace art |