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