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
17 #include "java_lang_Runtime.h"
18
19 #include <dlfcn.h>
20 #include <limits.h>
21 #include <unistd.h>
22
23 #include "base/macros.h"
24 #include "gc/heap.h"
25 #include "handle_scope-inl.h"
26 #include "jni_internal.h"
27 #include "mirror/class_loader.h"
28 #include "runtime.h"
29 #include "scoped_thread_state_change.h"
30 #include "ScopedUtfChars.h"
31 #include "verify_object-inl.h"
32
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
39 namespace art {
40
Runtime_gc(JNIEnv *,jclass)41 static void Runtime_gc(JNIEnv*, jclass) {
42 if (Runtime::Current()->IsExplicitGcDisabled()) {
43 LOG(INFO) << "Explicit GC skipped.";
44 return;
45 }
46 Runtime::Current()->GetHeap()->CollectGarbage(false);
47 }
48
Runtime_nativeExit(JNIEnv *,jclass,jint status)49 NO_RETURN static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
50 LOG(INFO) << "System.exit called, status: " << status;
51 Runtime::Current()->CallExitHook(status);
52 exit(status);
53 }
54
SetLdLibraryPath(JNIEnv * env,jstring javaLdLibraryPathJstr)55 static void SetLdLibraryPath(JNIEnv* env, jstring javaLdLibraryPathJstr) {
56 #ifdef HAVE_ANDROID_OS
57 if (javaLdLibraryPathJstr != nullptr) {
58 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPathJstr);
59 if (ldLibraryPath.c_str() != nullptr) {
60 android_update_LD_LIBRARY_PATH(ldLibraryPath.c_str());
61 }
62 }
63
64 #else
65 LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
66 UNUSED(javaLdLibraryPathJstr, env);
67 #endif
68 }
69
Runtime_nativeLoad(JNIEnv * env,jclass,jstring javaFilename,jobject javaLoader,jstring javaLdLibraryPathJstr)70 static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader,
71 jstring javaLdLibraryPathJstr) {
72 ScopedUtfChars filename(env, javaFilename);
73 if (filename.c_str() == nullptr) {
74 return nullptr;
75 }
76
77 SetLdLibraryPath(env, javaLdLibraryPathJstr);
78
79 std::string error_msg;
80 {
81 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
82 bool success = vm->LoadNativeLibrary(env, filename.c_str(), javaLoader, &error_msg);
83 if (success) {
84 return nullptr;
85 }
86 }
87
88 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
89 env->ExceptionClear();
90 return env->NewStringUTF(error_msg.c_str());
91 }
92
Runtime_maxMemory(JNIEnv *,jclass)93 static jlong Runtime_maxMemory(JNIEnv*, jclass) {
94 return Runtime::Current()->GetHeap()->GetMaxMemory();
95 }
96
Runtime_totalMemory(JNIEnv *,jclass)97 static jlong Runtime_totalMemory(JNIEnv*, jclass) {
98 return Runtime::Current()->GetHeap()->GetTotalMemory();
99 }
100
Runtime_freeMemory(JNIEnv *,jclass)101 static jlong Runtime_freeMemory(JNIEnv*, jclass) {
102 return Runtime::Current()->GetHeap()->GetFreeMemory();
103 }
104
105 static JNINativeMethod gMethods[] = {
106 NATIVE_METHOD(Runtime, freeMemory, "!()J"),
107 NATIVE_METHOD(Runtime, gc, "()V"),
108 NATIVE_METHOD(Runtime, maxMemory, "!()J"),
109 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
110 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"),
111 NATIVE_METHOD(Runtime, totalMemory, "!()J"),
112 };
113
register_java_lang_Runtime(JNIEnv * env)114 void register_java_lang_Runtime(JNIEnv* env) {
115 REGISTER_NATIVE_METHODS("java/lang/Runtime");
116 }
117
118 } // namespace art
119