blob: 84b18ab382b74d96d3d0421186696cff0c2794da [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
33namespace art {
34
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035static void Runtime_gc(JNIEnv*, jclass) {
Anwar Ghuloum87183592013-08-14 12:12:19 -070036 if (Runtime::Current()->IsExplicitGcDisabled()) {
37 LOG(INFO) << "Explicit GC skipped.";
38 return;
39 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080040 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070041}
42
Andreas Gampe794ad762015-02-23 08:12:24 -080043NO_RETURN static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
Dmitry Petrochenko108437e2014-07-03 17:07:45 +070044 LOG(INFO) << "System.exit called, status: " << status;
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070045 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070046 exit(status);
47}
48
Ian Rogers68d8b422014-07-17 11:09:10 -070049static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader,
50 jstring javaLdLibraryPath) {
51 // TODO: returns NULL on success or an error message describing the failure on failure. This
52 // should be refactored in terms of suppressed exceptions.
Elliott Hughesbf86d042011-08-31 17:53:14 -070053 ScopedUtfChars filename(env, javaFilename);
54 if (filename.c_str() == NULL) {
55 return NULL;
56 }
57
Elliott Hughesc8fece32013-01-02 11:27:23 -080058 if (javaLdLibraryPath != NULL) {
59 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
60 if (ldLibraryPath.c_str() == NULL) {
61 return NULL;
62 }
63 void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH");
64 if (sym != NULL) {
65 typedef void (*Fn)(const char*);
66 Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym);
67 (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str());
68 } else {
Andreas Gampee4301ff2015-02-17 19:25:29 -080069 LOG(WARNING) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
Elliott Hughesc8fece32013-01-02 11:27:23 -080070 }
71 }
72
Ian Rogers68d8b422014-07-17 11:09:10 -070073 std::string error_msg;
Ian Rogers1eb512d2013-10-18 15:42:20 -070074 {
Ian Rogers1eb512d2013-10-18 15:42:20 -070075 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070076 bool success = vm->LoadNativeLibrary(env, filename.c_str(), javaLoader, &error_msg);
Ian Rogers1eb512d2013-10-18 15:42:20 -070077 if (success) {
78 return nullptr;
79 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070080 }
81
Brian Carlstrom75fe90c2013-06-26 22:26:16 -070082 // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
83 env->ExceptionClear();
Ian Rogers68d8b422014-07-17 11:09:10 -070084 return env->NewStringUTF(error_msg.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070085}
86
Elliott Hughes1bac54f2012-03-16 12:48:31 -070087static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080088 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070089}
90
Elliott Hughes1bac54f2012-03-16 12:48:31 -070091static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070093}
94
Elliott Hughes1bac54f2012-03-16 12:48:31 -070095static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070097}
98
99static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700100 NATIVE_METHOD(Runtime, freeMemory, "!()J"),
Elliott Hugheseac76672012-05-24 21:56:51 -0700101 NATIVE_METHOD(Runtime, gc, "()V"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700102 NATIVE_METHOD(Runtime, maxMemory, "!()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -0700103 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hughesc8fece32013-01-02 11:27:23 -0800104 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"),
Ian Rogers53b8b092014-03-13 23:45:53 -0700105 NATIVE_METHOD(Runtime, totalMemory, "!()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106};
107
Elliott Hughesbf86d042011-08-31 17:53:14 -0700108void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700109 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700110}
111
112} // namespace art