Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -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 | |
| 17 | #include "class_linker.h" |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 18 | #include "debugger.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
| 20 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "object_utils.h" |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | |
| 24 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 25 | #include "toStringArray.h" |
| 26 | |
| 27 | #include <limits.h> |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | jfloat VMRuntime_getTargetHeapUtilization(JNIEnv*, jobject) { |
| 34 | return Heap::GetTargetHeapUtilization(); |
| 35 | } |
| 36 | |
| 37 | void VMRuntime_nativeSetTargetHeapUtilization(JNIEnv*, jobject, jfloat target) { |
| 38 | Heap::SetTargetHeapUtilization(target); |
| 39 | } |
| 40 | |
| 41 | void VMRuntime_startJitCompilation(JNIEnv*, jobject) { |
| 42 | } |
| 43 | |
| 44 | void VMRuntime_disableJitCompilation(JNIEnv*, jobject) { |
| 45 | } |
| 46 | |
| 47 | jobject VMRuntime_newNonMovableArray(JNIEnv* env, jobject, jclass javaElementClass, jint length) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 48 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 49 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 50 | // TODO: right now, we don't have a copying collector, so there's no need |
| 51 | // to do anything special here, but we ought to pass the non-movability |
| 52 | // through to the allocator. |
| 53 | UNIMPLEMENTED(FATAL); |
| 54 | #endif |
| 55 | |
| 56 | Class* element_class = Decode<Class*>(env, javaElementClass); |
| 57 | if (element_class == NULL) { |
| 58 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null"); |
| 59 | return NULL; |
| 60 | } |
| 61 | if (length < 0) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 62 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 67 | std::string descriptor; |
| 68 | descriptor += "["; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 69 | descriptor += ClassHelper(element_class).GetDescriptor(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 70 | Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 71 | Array* result = Array::Alloc(array_class, length); |
| 72 | if (result == NULL) { |
| 73 | return NULL; |
| 74 | } |
| 75 | return AddLocalReference<jobject>(env, result); |
| 76 | } |
| 77 | |
| 78 | jlong VMRuntime_addressOf(JNIEnv* env, jobject, jobject javaArray) { |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 79 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 80 | Array* array = Decode<Array*>(env, javaArray); |
| 81 | if (!array->IsArrayInstance()) { |
| 82 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array"); |
| 83 | return 0; |
| 84 | } |
| 85 | // TODO: we should also check that this is a non-movable array. |
| 86 | return reinterpret_cast<uintptr_t>(array->GetRawData()); |
| 87 | } |
| 88 | |
| 89 | void VMRuntime_clearGrowthLimit(JNIEnv*, jobject) { |
| 90 | Heap::ClearGrowthLimit(); |
| 91 | } |
| 92 | |
| 93 | jboolean VMRuntime_isDebuggerActive(JNIEnv*, jobject) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 94 | return Dbg::IsDebuggerConnected(); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | jobjectArray VMRuntime_properties(JNIEnv* env, jobject) { |
| 98 | return toStringArray(env, Runtime::Current()->GetProperties()); |
| 99 | } |
| 100 | |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame^] | 101 | // This is for backward compatibility with dalvik which returned the |
| 102 | // meaningless "." when no boot classpath or classpath was |
| 103 | // specified. Unfortunately, some tests were using java.class.path to |
| 104 | // lookup relative file locations, so they are counting on this to be |
| 105 | // ".", presumably some applications or libraries could have as well. |
| 106 | const char* DefaultToDot(const std::string& class_path) { |
| 107 | return class_path.empty() ? "." : class_path.c_str(); |
| 108 | } |
| 109 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 110 | jstring VMRuntime_bootClassPath(JNIEnv* env, jobject) { |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame^] | 111 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetBootClassPath())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | jstring VMRuntime_classPath(JNIEnv* env, jobject) { |
Brian Carlstrom | 7d5ffb5 | 2012-02-01 14:27:54 -0800 | [diff] [blame^] | 115 | return env->NewStringUTF(DefaultToDot(Runtime::Current()->GetClassPath())); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | jstring VMRuntime_vmVersion(JNIEnv* env, jobject) { |
| 119 | return env->NewStringUTF(Runtime::Current()->GetVersion()); |
| 120 | } |
| 121 | |
| 122 | void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) { |
| 123 | // This is the target SDK version of the app we're about to run. |
Elliott Hughes | c2dc62d | 2012-01-17 20:06:12 -0800 | [diff] [blame] | 124 | // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000). |
| 125 | // Note that targetSdkVersion may be 0, meaning "current". |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 126 | if (targetSdkVersion > 0 && targetSdkVersion <= 13 /* honeycomb-mr2 */) { |
| 127 | // TODO: running with CheckJNI should override this and force you to obey the strictest rules. |
| 128 | LOG(INFO) << "Turning on JNI app bug workarounds for target SDK version " << targetSdkVersion << "..."; |
Brian Carlstrom | 49b4f07 | 2012-01-18 14:23:48 -0800 | [diff] [blame] | 129 | // Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs = true; |
| 130 | UNIMPLEMENTED(WARNING) << "Support work arounds for app JNI bugs"; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | JNINativeMethod gMethods[] = { |
| 135 | NATIVE_METHOD(VMRuntime, addressOf, "(Ljava/lang/Object;)J"), |
| 136 | NATIVE_METHOD(VMRuntime, bootClassPath, "()Ljava/lang/String;"), |
| 137 | NATIVE_METHOD(VMRuntime, classPath, "()Ljava/lang/String;"), |
| 138 | NATIVE_METHOD(VMRuntime, clearGrowthLimit, "()V"), |
| 139 | NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"), |
| 140 | NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"), |
| 141 | NATIVE_METHOD(VMRuntime, isDebuggerActive, "()Z"), |
| 142 | NATIVE_METHOD(VMRuntime, nativeSetTargetHeapUtilization, "(F)V"), |
| 143 | NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"), |
| 144 | NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"), |
| 145 | NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"), |
| 146 | NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"), |
| 147 | NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"), |
| 148 | }; |
| 149 | |
| 150 | } // namespace |
| 151 | |
| 152 | void register_dalvik_system_VMRuntime(JNIEnv* env) { |
| 153 | jniRegisterNativeMethods(env, "dalvik/system/VMRuntime", gMethods, NELEM(gMethods)); |
| 154 | } |
| 155 | |
| 156 | } // namespace art |