blob: 4a1e6c21c1416876a9bad7f5d0eb9c00f63230f1 [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -07001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_Runtime.h"
18
Elliott Hughesc8fece32013-01-02 11:27:23 -080019#include <dlfcn.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070020#include <limits.h>
Elliott Hugheseac76672012-05-24 21:56:51 -070021#include <unistd.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070022
Andreas Gampe794ad762015-02-23 08:12:24 -080023#include "base/macros.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/heap.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025#include "handle_scope-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/class_loader.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070028#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070030#include "ScopedUtfChars.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "verify_object-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070032
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070033#include <sstream>
Andreas Gampec60e1b72015-07-30 08:57:50 -070034#ifdef __ANDROID__
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070035// This function is provided by android linker.
36extern "C" void android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Andreas Gampec60e1b72015-07-30 08:57:50 -070037#endif // __ANDROID__
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070038
Elliott Hughesbf86d042011-08-31 17:53:14 -070039namespace art {
40
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041static void Runtime_gc(JNIEnv*, jclass) {
Anwar Ghuloum87183592013-08-14 12:12:19 -070042 if (Runtime::Current()->IsExplicitGcDisabled()) {
43 LOG(INFO) << "Explicit GC skipped.";
44 return;
45 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080046 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070047}
48
Andreas Gampe794ad762015-02-23 08:12:24 -080049NO_RETURN static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
Dmitry Petrochenko108437e2014-07-03 17:07:45 +070050 LOG(INFO) << "System.exit called, status: " << status;
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070051 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070052 exit(status);
53}
54
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080055static void SetLdLibraryPath(JNIEnv* env, jstring javaLdLibraryPath) {
Andreas Gampec60e1b72015-07-30 08:57:50 -070056#ifdef __ANDROID__
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080057 if (javaLdLibraryPath != nullptr) {
58 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
Dmitriy Ivanov906846f2015-06-05 13:01:52 -070059 if (ldLibraryPath.c_str() != nullptr) {
60 android_update_LD_LIBRARY_PATH(ldLibraryPath.c_str());
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070061 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070062 }
63
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070064#else
65 LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080066 UNUSED(javaLdLibraryPath, env);
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070067#endif
68}
69
70static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader,
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080071 jstring javaLdLibraryPath, jstring javaIsolationPath) {
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070072 ScopedUtfChars filename(env, javaFilename);
73 if (filename.c_str() == nullptr) {
74 return nullptr;
75 }
76
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080077 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
78
79 // Starting with N nativeLoad uses classloader local
80 // linker namespace instead of global LD_LIBRARY_PATH
81 // (23 is Marshmallow)
Dimitry Ivanov7f575792015-12-14 09:57:56 -080082 if (target_sdk_version <= INT_MAX) {
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080083 SetLdLibraryPath(env, javaLdLibraryPath);
84 }
Dmitriy Ivanov785049f2014-07-18 10:08:57 -070085
Ian Rogers68d8b422014-07-17 11:09:10 -070086 std::string error_msg;
Ian Rogers1eb512d2013-10-18 15:42:20 -070087 {
Ian Rogers1eb512d2013-10-18 15:42:20 -070088 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080089 bool success = vm->LoadNativeLibrary(env, filename.c_str(), javaLoader,
90 javaLdLibraryPath, javaIsolationPath, &error_msg);
Ian Rogers1eb512d2013-10-18 15:42:20 -070091 if (success) {
92 return nullptr;
93 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070094 }
95
Brian Carlstrom75fe90c2013-06-26 22:26:16 -070096 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
97 env->ExceptionClear();
Ian Rogers68d8b422014-07-17 11:09:10 -070098 return env->NewStringUTF(error_msg.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070099}
100
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700101static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800102 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700103}
104
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700105static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800106 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700107}
108
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700109static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800110 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111}
112
113static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700114 NATIVE_METHOD(Runtime, freeMemory, "!()J"),
Elliott Hugheseac76672012-05-24 21:56:51 -0700115 NATIVE_METHOD(Runtime, gc, "()V"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700116 NATIVE_METHOD(Runtime, maxMemory, "!()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -0700117 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -0800118 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700119 NATIVE_METHOD(Runtime, totalMemory, "!()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700120};
121
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700123 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700124}
125
126} // namespace art