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