Elliott Hughes | a250199 | 2011-08-26 19:39:54 -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 "jni_internal.h" |
| 18 | |
| 19 | #include <sys/mman.h> |
| 20 | #include <zlib.h> |
| 21 | |
| 22 | #include "class_linker.h" |
| 23 | #include "logging.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 24 | #include "object_utils.h" |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 25 | #include "scoped_jni_thread_state.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 26 | #include "space.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 27 | #include "thread.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 28 | #include "runtime.h" |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | e608763 | 2011-09-26 12:18:25 -0700 | [diff] [blame] | 30 | #define LIBCORE_CPP_JNI_HELPERS |
| 31 | #include <JNIHelp.h> // from libcore |
| 32 | #undef LIBCORE_CPP_JNI_HELPERS |
| 33 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 36 | static void JniAbort(const char* jni_function_name, const char* msg) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 37 | Thread* self = Thread::Current(); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 38 | Method* current_method = self->GetCurrentMethod(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 40 | std::ostringstream os; |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 41 | os << "JNI DETECTED ERROR IN APPLICATION: " << msg; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 42 | |
| 43 | if (jni_function_name != NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 44 | os << "\n in call to " << jni_function_name; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 45 | } |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 46 | // TODO: is this useful given that we're about to dump the calling thread's stack? |
| 47 | if (current_method != NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 48 | os << "\n from " << PrettyMethod(current_method); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 49 | } |
| 50 | os << "\n"; |
| 51 | self->Dump(os); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 52 | |
| 53 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 54 | if (vm->check_jni_abort_hook != NULL) { |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 55 | vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 56 | } else { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 57 | self->SetState(kNative); // Ensure that we get a native stack trace for this thread. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 58 | LOG(FATAL) << os.str(); |
| 59 | } |
| 60 | } |
| 61 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 62 | static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) { |
| 63 | std::string msg; |
| 64 | StringAppendV(&msg, fmt, ap); |
| 65 | JniAbort(jni_function_name, msg.c_str()); |
| 66 | } |
| 67 | |
| 68 | void JniAbortF(const char* jni_function_name, const char* fmt, ...) { |
| 69 | va_list args; |
| 70 | va_start(args, fmt); |
| 71 | JniAbortV(jni_function_name, fmt, args); |
| 72 | va_end(args); |
| 73 | } |
| 74 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 75 | /* |
| 76 | * =========================================================================== |
| 77 | * JNI function helpers |
| 78 | * =========================================================================== |
| 79 | */ |
| 80 | |
Ian Rogers | 959f8ed | 2012-02-07 16:33:37 -0800 | [diff] [blame] | 81 | static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) { |
| 82 | return GetIndirectRefKind(localRef) == kSirtOrInvalid && |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 83 | reinterpret_cast<JNIEnvExt*>(env)->self->StackReferencesContain(localRef); |
Ian Rogers | 959f8ed | 2012-02-07 16:33:37 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 86 | template<typename T> |
| 87 | T Decode(ScopedJniThreadState& ts, jobject obj) { |
| 88 | return reinterpret_cast<T>(ts.Self()->DecodeJObject(obj)); |
| 89 | } |
| 90 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 91 | // Hack to allow forcecopy to work with jniGetNonMovableArrayElements. |
| 92 | // The code deliberately uses an invalid sequence of operations, so we |
| 93 | // need to pass it through unmodified. Review that code before making |
| 94 | // any changes here. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 95 | #define kNoCopyMagic 0xd5aab57f |
| 96 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 97 | // Flags passed into ScopedCheck. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 98 | #define kFlag_Default 0x0000 |
| 99 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 100 | #define kFlag_CritBad 0x0000 // Calling while in critical is not allowed. |
| 101 | #define kFlag_CritOkay 0x0001 // Calling while in critical is allowed. |
| 102 | #define kFlag_CritGet 0x0002 // This is a critical "get". |
| 103 | #define kFlag_CritRelease 0x0003 // This is a critical "release". |
| 104 | #define kFlag_CritMask 0x0003 // Bit mask to get "crit" value. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 105 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 106 | #define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed. |
| 107 | #define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 108 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 109 | #define kFlag_Release 0x0010 // Are we in a non-critical release function? |
| 110 | #define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable? |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 111 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 112 | #define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*). |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 113 | |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 114 | #define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call. |
| 115 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 116 | static const char* gBuiltInPrefixes[] = { |
| 117 | "Landroid/", |
| 118 | "Lcom/android/", |
| 119 | "Lcom/google/android/", |
| 120 | "Ldalvik/", |
| 121 | "Ljava/", |
| 122 | "Ljavax/", |
| 123 | "Llibcore/", |
| 124 | "Lorg/apache/harmony/", |
| 125 | NULL |
| 126 | }; |
| 127 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 128 | static bool ShouldTrace(JavaVMExt* vm, const Method* method) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 129 | // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages |
| 130 | // when a native method that matches the -Xjnitrace argument calls a JNI function |
| 131 | // such as NewByteArray. |
| 132 | // If -verbose:third-party-jni is on, we want to log any JNI function calls |
| 133 | // made by a third-party native method. |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 134 | std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor()); |
| 135 | if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 136 | return true; |
| 137 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 138 | if (VLOG_IS_ON(third_party_jni)) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 139 | // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look |
| 140 | // like part of Android. |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 141 | for (size_t i = 0; gBuiltInPrefixes[i] != NULL; ++i) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 142 | if (StartsWith(class_name, gBuiltInPrefixes[i])) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 151 | class ScopedCheck { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 152 | public: |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 153 | // For JNIEnv* functions. |
| 154 | explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 155 | Init(env, reinterpret_cast<JNIEnvExt*>(env)->vm, flags, functionName, true); |
| 156 | CheckThread(flags); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // For JavaVM* functions. |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 160 | explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName) { |
| 161 | Init(NULL, vm, kFlag_Invocation, functionName, has_method); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 164 | bool ForceCopy() { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 165 | return Runtime::Current()->GetJavaVM()->force_copy; |
| 166 | } |
| 167 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 168 | // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread" |
| 169 | // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of |
| 170 | // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some |
| 171 | // circumstances, but this is incorrect. |
| 172 | void CheckClassName(const char* class_name) { |
| 173 | if (!IsValidJniClassName(class_name)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 174 | JniAbortF(function_name_, |
| 175 | "illegal class name '%s'\n" |
| 176 | " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')", |
| 177 | class_name); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Verify that the field is of the appropriate type. If the field has an |
| 183 | * object type, "java_object" is the object we're trying to assign into it. |
| 184 | * |
| 185 | * Works for both static and instance fields. |
| 186 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 187 | void CheckFieldType(jobject java_object, jfieldID fid, char prim, bool isStatic) { |
| 188 | ScopedJniThreadState ts(env_); |
| 189 | Field* f = CheckFieldID(fid); |
| 190 | if (f == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 193 | Class* field_type = FieldHelper(f).GetType(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 194 | if (!field_type->IsPrimitive()) { |
| 195 | if (java_object != NULL) { |
| 196 | Object* obj = Decode<Object*>(ts, java_object); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 197 | // If java_object is a weak global ref whose referent has been cleared, |
| 198 | // obj will be NULL. Otherwise, obj should always be non-NULL |
| 199 | // and valid. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 200 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 201 | JniAbortF(function_name_, "field operation on invalid %s: %p", |
| 202 | ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 203 | return; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 204 | } else { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 205 | if (!obj->InstanceOf(field_type)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 206 | JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s", |
| 207 | PrettyField(f).c_str(), PrettyTypeOf(obj).c_str()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 210 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 211 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 212 | } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 213 | JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c", |
| 214 | PrettyField(f).c_str(), prim); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 215 | return; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 218 | if (isStatic != f->IsStatic()) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 219 | if (isStatic) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 220 | JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 221 | } else { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 222 | JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 223 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Verify that this instance field ID is valid for this object. |
| 230 | * |
| 231 | * Assumes "jobj" has already been validated. |
| 232 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 233 | void CheckInstanceFieldID(jobject java_object, jfieldID fid) { |
| 234 | ScopedJniThreadState ts(env_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 235 | |
| 236 | Object* o = Decode<Object*>(ts, java_object); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 237 | if (o == NULL || !Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 238 | JniAbortF(function_name_, "field operation on invalid %s: %p", |
| 239 | ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 243 | Field* f = CheckFieldID(fid); |
| 244 | if (f == NULL) { |
| 245 | return; |
| 246 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 247 | Class* c = o->GetClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 248 | FieldHelper fh(f); |
| 249 | if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 250 | JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s", |
| 251 | PrettyField(f).c_str(), PrettyTypeOf(o).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Verify that the pointer value is non-NULL. |
| 257 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 258 | void CheckNonNull(const void* ptr) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 259 | if (ptr == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 260 | JniAbortF(function_name_, "non-nullable argument was NULL"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Verify that the method's return type matches the type of call. |
| 266 | * 'expectedType' will be "L" for all objects, including arrays. |
| 267 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 268 | void CheckSig(jmethodID mid, const char* expectedType, bool isStatic) { |
| 269 | ScopedJniThreadState ts(env_); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 270 | Method* m = CheckMethodID(mid); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 271 | if (m == NULL) { |
| 272 | return; |
| 273 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 274 | if (*expectedType != MethodHelper(m).GetShorty()[0]) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 275 | JniAbortF(function_name_, "the return type of %s does not match %s", |
| 276 | function_name_, PrettyMethod(m).c_str()); |
| 277 | } |
| 278 | if (isStatic != m->IsStatic()) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 279 | if (isStatic) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 280 | JniAbortF(function_name_, "calling non-static method %s with %s", |
| 281 | PrettyMethod(m).c_str(), function_name_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 282 | } else { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 283 | JniAbortF(function_name_, "calling static method %s with %s", |
| 284 | PrettyMethod(m).c_str(), function_name_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 285 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Verify that this static field ID is valid for this class. |
| 291 | * |
| 292 | * Assumes "java_class" has already been validated. |
| 293 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 294 | void CheckStaticFieldID(jclass java_class, jfieldID fid) { |
| 295 | ScopedJniThreadState ts(env_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 296 | Class* c = Decode<Class*>(ts, java_class); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 297 | const Field* f = CheckFieldID(fid); |
| 298 | if (f == NULL) { |
| 299 | return; |
| 300 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 301 | if (f->GetDeclaringClass() != c) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 302 | JniAbortF(function_name_, "static jfieldID %p not valid for class %s", |
| 303 | fid, PrettyClass(c).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 308 | * Verify that "mid" is appropriate for "java_class". |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 309 | * |
| 310 | * A mismatch isn't dangerous, because the jmethodID defines the class. In |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 311 | * fact, java_class is unused in the implementation. It's best if we don't |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 312 | * allow bad code in the system though. |
| 313 | * |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 314 | * Instances of "java_class" must be instances of the method's declaring class. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 315 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 316 | void CheckStaticMethod(jclass java_class, jmethodID mid) { |
| 317 | ScopedJniThreadState ts(env_); |
| 318 | const Method* m = CheckMethodID(mid); |
| 319 | if (m == NULL) { |
| 320 | return; |
| 321 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 322 | Class* c = Decode<Class*>(ts, java_class); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 323 | if (!c->IsAssignableFrom(m->GetDeclaringClass())) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 324 | JniAbortF(function_name_, "can't call static %s on class %s", |
| 325 | PrettyMethod(m).c_str(), PrettyClass(c).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Verify that "mid" is appropriate for "jobj". |
| 331 | * |
| 332 | * Make sure the object is an instance of the method's declaring class. |
| 333 | * (Note the mid might point to a declaration in an interface; this |
| 334 | * will be handled automatically by the instanceof check.) |
| 335 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 336 | void CheckVirtualMethod(jobject java_object, jmethodID mid) { |
| 337 | ScopedJniThreadState ts(env_); |
| 338 | const Method* m = CheckMethodID(mid); |
| 339 | if (m == NULL) { |
| 340 | return; |
| 341 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 342 | Object* o = Decode<Object*>(ts, java_object); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 343 | if (!o->InstanceOf(m->GetDeclaringClass())) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 344 | JniAbortF(function_name_, "can't call %s on instance of %s", |
| 345 | PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * The format string is a sequence of the following characters, |
| 351 | * and must be followed by arguments of the corresponding types |
| 352 | * in the same order. |
| 353 | * |
| 354 | * Java primitive types: |
| 355 | * B - jbyte |
| 356 | * C - jchar |
| 357 | * D - jdouble |
| 358 | * F - jfloat |
| 359 | * I - jint |
| 360 | * J - jlong |
| 361 | * S - jshort |
| 362 | * Z - jboolean (shown as true and false) |
| 363 | * V - void |
| 364 | * |
| 365 | * Java reference types: |
| 366 | * L - jobject |
| 367 | * a - jarray |
| 368 | * c - jclass |
| 369 | * s - jstring |
| 370 | * |
| 371 | * JNI types: |
| 372 | * b - jboolean (shown as JNI_TRUE and JNI_FALSE) |
| 373 | * f - jfieldID |
| 374 | * m - jmethodID |
| 375 | * p - void* |
| 376 | * r - jint (for release mode arguments) |
Elliott Hughes | 78090d1 | 2011-10-07 14:31:47 -0700 | [diff] [blame] | 377 | * u - const char* (Modified UTF-8) |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 378 | * z - jsize (for lengths; use i if negative values are okay) |
| 379 | * v - JavaVM* |
| 380 | * E - JNIEnv* |
| 381 | * . - no argument; just print "..." (used for varargs JNI calls) |
| 382 | * |
| 383 | * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable. |
| 384 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 385 | void Check(bool entry, const char* fmt0, ...) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 386 | va_list ap; |
| 387 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 388 | const Method* traceMethod = NULL; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 389 | if ((!vm_->trace.empty() || VLOG_IS_ON(third_party_jni)) && has_method_) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 390 | // We need to guard some of the invocation interface's calls: a bad caller might |
| 391 | // use DetachCurrentThread or GetEnv on a thread that's not yet attached. |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 392 | Thread* self = Thread::Current(); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 393 | if ((flags_ & kFlag_Invocation) == 0 || self != NULL) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 394 | traceMethod = self->GetCurrentMethod(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 397 | |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 398 | if (((flags_ & kFlag_ForceTrace) != 0) || (traceMethod != NULL && ShouldTrace(vm_, traceMethod))) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 399 | va_start(ap, fmt0); |
| 400 | std::string msg; |
| 401 | for (const char* fmt = fmt0; *fmt;) { |
| 402 | char ch = *fmt++; |
| 403 | if (ch == 'B') { // jbyte |
| 404 | jbyte b = va_arg(ap, int); |
| 405 | if (b >= 0 && b < 10) { |
| 406 | StringAppendF(&msg, "%d", b); |
| 407 | } else { |
| 408 | StringAppendF(&msg, "%#x (%d)", b, b); |
| 409 | } |
| 410 | } else if (ch == 'C') { // jchar |
| 411 | jchar c = va_arg(ap, int); |
| 412 | if (c < 0x7f && c >= ' ') { |
| 413 | StringAppendF(&msg, "U+%x ('%c')", c, c); |
| 414 | } else { |
| 415 | StringAppendF(&msg, "U+%x", c); |
| 416 | } |
| 417 | } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble |
| 418 | StringAppendF(&msg, "%g", va_arg(ap, double)); |
| 419 | } else if (ch == 'I' || ch == 'S') { // jint, jshort |
| 420 | StringAppendF(&msg, "%d", va_arg(ap, int)); |
| 421 | } else if (ch == 'J') { // jlong |
| 422 | StringAppendF(&msg, "%lld", va_arg(ap, jlong)); |
| 423 | } else if (ch == 'Z') { // jboolean |
| 424 | StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false"); |
| 425 | } else if (ch == 'V') { // void |
| 426 | msg += "void"; |
| 427 | } else if (ch == 'v') { // JavaVM* |
| 428 | JavaVM* vm = va_arg(ap, JavaVM*); |
| 429 | StringAppendF(&msg, "(JavaVM*)%p", vm); |
| 430 | } else if (ch == 'E') { // JNIEnv* |
| 431 | JNIEnv* env = va_arg(ap, JNIEnv*); |
| 432 | StringAppendF(&msg, "(JNIEnv*)%p", env); |
| 433 | } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring |
| 434 | // For logging purposes, these are identical. |
| 435 | jobject o = va_arg(ap, jobject); |
| 436 | if (o == NULL) { |
| 437 | msg += "NULL"; |
| 438 | } else { |
| 439 | StringAppendF(&msg, "%p", o); |
| 440 | } |
| 441 | } else if (ch == 'b') { // jboolean (JNI-style) |
| 442 | jboolean b = va_arg(ap, int); |
| 443 | msg += (b ? "JNI_TRUE" : "JNI_FALSE"); |
| 444 | } else if (ch == 'c') { // jclass |
| 445 | jclass jc = va_arg(ap, jclass); |
| 446 | Class* c = reinterpret_cast<Class*>(Thread::Current()->DecodeJObject(jc)); |
| 447 | if (c == NULL) { |
| 448 | msg += "NULL"; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 449 | } else if (c == kInvalidIndirectRefObject || !Runtime::Current()->GetHeap()->IsHeapAddress(c)) { |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 450 | StringAppendF(&msg, "INVALID POINTER:%p", jc); |
| 451 | } else if (!c->IsClass()) { |
| 452 | msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 453 | } else { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 454 | msg += PrettyClass(c); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 455 | if (!entry) { |
| 456 | StringAppendF(&msg, " (%p)", jc); |
| 457 | } |
| 458 | } |
| 459 | } else if (ch == 'f') { // jfieldID |
| 460 | jfieldID fid = va_arg(ap, jfieldID); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 461 | Field* f = reinterpret_cast<Field*>(fid); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 462 | msg += PrettyField(f); |
| 463 | if (!entry) { |
| 464 | StringAppendF(&msg, " (%p)", fid); |
| 465 | } |
| 466 | } else if (ch == 'z') { // non-negative jsize |
| 467 | // You might expect jsize to be size_t, but it's not; it's the same as jint. |
| 468 | // We only treat this specially so we can do the non-negative check. |
| 469 | // TODO: maybe this wasn't worth it? |
| 470 | jint i = va_arg(ap, jint); |
| 471 | StringAppendF(&msg, "%d", i); |
| 472 | } else if (ch == 'm') { // jmethodID |
| 473 | jmethodID mid = va_arg(ap, jmethodID); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 474 | Method* m = reinterpret_cast<Method*>(mid); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 475 | msg += PrettyMethod(m); |
| 476 | if (!entry) { |
| 477 | StringAppendF(&msg, " (%p)", mid); |
| 478 | } |
| 479 | } else if (ch == 'p') { // void* ("pointer") |
| 480 | void* p = va_arg(ap, void*); |
| 481 | if (p == NULL) { |
| 482 | msg += "NULL"; |
| 483 | } else { |
| 484 | StringAppendF(&msg, "(void*) %p", p); |
| 485 | } |
| 486 | } else if (ch == 'r') { // jint (release mode) |
| 487 | jint releaseMode = va_arg(ap, jint); |
| 488 | if (releaseMode == 0) { |
| 489 | msg += "0"; |
| 490 | } else if (releaseMode == JNI_ABORT) { |
| 491 | msg += "JNI_ABORT"; |
| 492 | } else if (releaseMode == JNI_COMMIT) { |
| 493 | msg += "JNI_COMMIT"; |
| 494 | } else { |
| 495 | StringAppendF(&msg, "invalid release mode %d", releaseMode); |
| 496 | } |
Elliott Hughes | 78090d1 | 2011-10-07 14:31:47 -0700 | [diff] [blame] | 497 | } else if (ch == 'u') { // const char* (Modified UTF-8) |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 498 | const char* utf = va_arg(ap, const char*); |
| 499 | if (utf == NULL) { |
| 500 | msg += "NULL"; |
| 501 | } else { |
| 502 | StringAppendF(&msg, "\"%s\"", utf); |
| 503 | } |
| 504 | } else if (ch == '.') { |
| 505 | msg += "..."; |
| 506 | } else { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 507 | JniAbortF(function_name_, "unknown trace format specifier: %c", ch); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | if (*fmt) { |
| 511 | StringAppendF(&msg, ", "); |
| 512 | } |
| 513 | } |
| 514 | va_end(ap); |
| 515 | |
Elliott Hughes | 485cac4 | 2011-12-09 17:49:35 -0800 | [diff] [blame] | 516 | if ((flags_ & kFlag_ForceTrace) != 0) { |
| 517 | LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")"; |
| 518 | } else if (entry) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 519 | if (has_method_) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 520 | std::string methodName(PrettyMethod(traceMethod, false)); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 521 | LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")"; |
| 522 | indent_ = methodName.size() + 1; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 523 | } else { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 524 | LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")"; |
| 525 | indent_ = 0; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 526 | } |
| 527 | } else { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 528 | LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | // We always do the thorough checks on entry, and never on exit... |
| 533 | if (entry) { |
| 534 | va_start(ap, fmt0); |
| 535 | for (const char* fmt = fmt0; *fmt; ++fmt) { |
| 536 | char ch = *fmt; |
| 537 | if (ch == 'a') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 538 | CheckArray(va_arg(ap, jarray)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 539 | } else if (ch == 'c') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 540 | CheckInstance(kClass, va_arg(ap, jclass)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 541 | } else if (ch == 'L') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 542 | CheckObject(va_arg(ap, jobject)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 543 | } else if (ch == 'r') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 544 | CheckReleaseMode(va_arg(ap, jint)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 545 | } else if (ch == 's') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 546 | CheckInstance(kString, va_arg(ap, jstring)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 547 | } else if (ch == 'u') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 548 | if ((flags_ & kFlag_Release) != 0) { |
| 549 | CheckNonNull(va_arg(ap, const char*)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 550 | } else { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 551 | bool nullable = ((flags_ & kFlag_NullableUtf) != 0); |
| 552 | CheckUtfString(va_arg(ap, const char*), nullable); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 553 | } |
| 554 | } else if (ch == 'z') { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 555 | CheckLengthPositive(va_arg(ap, jsize)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 556 | } else if (strchr("BCISZbfmpEv", ch) != NULL) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 557 | va_arg(ap, uint32_t); // Skip this argument. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 558 | } else if (ch == 'D' || ch == 'F') { |
| 559 | va_arg(ap, double); // Skip this argument. |
| 560 | } else if (ch == 'J') { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 561 | va_arg(ap, uint64_t); // Skip this argument. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 562 | } else if (ch == '.') { |
| 563 | } else { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 564 | LOG(FATAL) << "Unknown check format specifier: " << ch; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | va_end(ap); |
| 568 | } |
| 569 | } |
| 570 | |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 571 | enum InstanceKind { |
| 572 | kClass, |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 573 | kDirectByteBuffer, |
| 574 | kObject, |
| 575 | kString, |
| 576 | kThrowable, |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | /* |
| 580 | * Verify that "jobj" is a valid non-NULL object reference, and points to |
| 581 | * an instance of expectedClass. |
| 582 | * |
| 583 | * Because we're looking at an object on the GC heap, we have to switch |
| 584 | * to "running" mode before doing the checks. |
| 585 | */ |
| 586 | bool CheckInstance(InstanceKind kind, jobject java_object) { |
| 587 | const char* what = NULL; |
| 588 | switch (kind) { |
| 589 | case kClass: |
| 590 | what = "jclass"; |
| 591 | break; |
| 592 | case kDirectByteBuffer: |
| 593 | what = "direct ByteBuffer"; |
| 594 | break; |
| 595 | case kObject: |
| 596 | what = "jobject"; |
| 597 | break; |
| 598 | case kString: |
| 599 | what = "jstring"; |
| 600 | break; |
| 601 | case kThrowable: |
| 602 | what = "jthrowable"; |
| 603 | break; |
| 604 | default: |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 605 | LOG(FATAL) << "Unknown kind " << static_cast<int>(kind); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | if (java_object == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 609 | JniAbortF(function_name_, "%s received null %s", function_name_, what); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | |
| 613 | ScopedJniThreadState ts(env_); |
| 614 | Object* obj = Decode<Object*>(ts, java_object); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 615 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(obj)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 616 | JniAbortF(function_name_, "%s is an invalid %s: %p (%p)", |
| 617 | what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | |
| 621 | bool okay = true; |
| 622 | switch (kind) { |
| 623 | case kClass: |
| 624 | okay = obj->IsClass(); |
| 625 | break; |
| 626 | case kDirectByteBuffer: |
| 627 | UNIMPLEMENTED(FATAL); |
| 628 | break; |
| 629 | case kString: |
| 630 | okay = obj->GetClass()->IsStringClass(); |
| 631 | break; |
| 632 | case kThrowable: |
| 633 | okay = obj->GetClass()->IsThrowableClass(); |
| 634 | break; |
| 635 | case kObject: |
| 636 | break; |
| 637 | } |
| 638 | if (!okay) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 639 | JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str()); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | |
| 643 | return true; |
| 644 | } |
| 645 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 646 | private: |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 647 | // Set "has_method" to true if we have a valid thread with a method pointer. |
| 648 | // We won't have one before attaching a thread, after detaching a thread, or |
| 649 | // when shutting down the runtime. |
| 650 | void Init(JNIEnv* env, JavaVM* vm, int flags, const char* functionName, bool has_method) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 651 | env_ = reinterpret_cast<JNIEnvExt*>(env); |
| 652 | vm_ = reinterpret_cast<JavaVMExt*>(vm); |
| 653 | flags_ = flags; |
| 654 | function_name_ = functionName; |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 655 | has_method_ = has_method; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Verify that "array" is non-NULL and points to an Array object. |
| 660 | * |
| 661 | * Since we're dealing with objects, switch to "running" mode. |
| 662 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 663 | void CheckArray(jarray java_array) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 664 | if (java_array == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 665 | JniAbortF(function_name_, "jarray was NULL"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 669 | ScopedJniThreadState ts(env_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 670 | Array* a = Decode<Array*>(ts, java_array); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 671 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(a)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 672 | JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)", |
| 673 | ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 674 | } else if (!a->IsArrayInstance()) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 675 | JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 679 | void CheckLengthPositive(jsize length) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 680 | if (length < 0) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 681 | JniAbortF(function_name_, "negative jsize: %d", length); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 685 | Field* CheckFieldID(jfieldID fid) { |
| 686 | if (fid == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 687 | JniAbortF(function_name_, "jfieldID was NULL"); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 688 | return NULL; |
| 689 | } |
| 690 | Field* f = DecodeField(fid); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 691 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(f)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 692 | JniAbortF(function_name_, "invalid jfieldID: %p", fid); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 693 | return NULL; |
| 694 | } |
| 695 | return f; |
| 696 | } |
| 697 | |
| 698 | Method* CheckMethodID(jmethodID mid) { |
| 699 | if (mid == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 700 | JniAbortF(function_name_, "jmethodID was NULL"); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 701 | return NULL; |
| 702 | } |
| 703 | Method* m = DecodeMethod(mid); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 704 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(m)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 705 | JniAbortF(function_name_, "invalid jmethodID: %p", mid); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 706 | return NULL; |
| 707 | } |
| 708 | return m; |
| 709 | } |
| 710 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 711 | /* |
| 712 | * Verify that "jobj" is a valid object, and that it's an object that JNI |
| 713 | * is allowed to know about. We allow NULL references. |
| 714 | * |
| 715 | * Switches to "running" mode before performing checks. |
| 716 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 717 | void CheckObject(jobject java_object) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 718 | if (java_object == NULL) { |
| 719 | return; |
| 720 | } |
| 721 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 722 | ScopedJniThreadState ts(env_); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 723 | |
| 724 | Object* o = Decode<Object*>(ts, java_object); |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 725 | if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) { |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 726 | // TODO: when we remove work_around_app_jni_bugs, this should be impossible. |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 727 | JniAbortF(function_name_, "native code passing in reference to invalid %s: %p", |
| 728 | ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Verify that the "mode" argument passed to a primitive array Release |
| 734 | * function is one of the valid values. |
| 735 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 736 | void CheckReleaseMode(jint mode) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 737 | if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 738 | JniAbortF(function_name_, "bad value for release mode: %d", mode); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 742 | void CheckThread(int flags) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 743 | Thread* self = Thread::Current(); |
| 744 | if (self == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 745 | JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
| 749 | // Get the *correct* JNIEnv by going through our TLS pointer. |
| 750 | JNIEnvExt* threadEnv = self->GetJniEnv(); |
| 751 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 752 | // Verify that the current thread is (a) attached and (b) associated with |
| 753 | // this particular instance of JNIEnv. |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 754 | if (env_ != threadEnv) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 755 | if (vm_->work_around_app_jni_bugs) { |
| 756 | // If we're keeping broken code limping along, we need to suppress the abort... |
| 757 | LOG(ERROR) << "APP BUG DETECTED: thread " << *self << " using JNIEnv* from thread " << *env_->self; |
| 758 | } else { |
| 759 | JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s", |
| 760 | ToStr<Thread>(*self).c_str(), ToStr<Thread>(*env_->self).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 761 | return; |
| 762 | } |
| 763 | } |
| 764 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 765 | // Verify that, if this thread previously made a critical "get" call, we |
| 766 | // do the corresponding "release" call before we try anything else. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 767 | switch (flags & kFlag_CritMask) { |
| 768 | case kFlag_CritOkay: // okay to call this method |
| 769 | break; |
| 770 | case kFlag_CritBad: // not okay to call |
| 771 | if (threadEnv->critical) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 772 | JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 773 | return; |
| 774 | } |
| 775 | break; |
| 776 | case kFlag_CritGet: // this is a "get" call |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 777 | // Don't check here; we allow nested gets. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 778 | threadEnv->critical++; |
| 779 | break; |
| 780 | case kFlag_CritRelease: // this is a "release" call |
| 781 | threadEnv->critical--; |
| 782 | if (threadEnv->critical < 0) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 783 | JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | break; |
| 787 | default: |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 788 | LOG(FATAL) << "Bad flags (internal error): " << flags; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 791 | // Verify that, if an exception has been raised, the native code doesn't |
| 792 | // make any JNI calls other than the Exception* methods. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 793 | if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 794 | std::string type(PrettyTypeOf(self->GetException())); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 795 | // TODO: write native code that doesn't require allocation for dumping an exception. |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 796 | // TODO: do we care any more? art always dumps pending exceptions on aborting threads. |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 797 | if (type != "java.lang.OutOfMemoryError") { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 798 | JniAbortF(function_name_, "JNI %s called with pending exception: %s", |
| 799 | function_name_, type.c_str(), jniGetStackTrace(env_).c_str()); |
| 800 | } else { |
| 801 | JniAbortF(function_name_, "JNI %s called with %s pending", function_name_, type.c_str()); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 802 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | } |
| 806 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 807 | // Verifies that "bytes" points to valid Modified UTF-8 data. |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 808 | void CheckUtfString(const char* bytes, bool nullable) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 809 | if (bytes == NULL) { |
| 810 | if (!nullable) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 811 | JniAbortF(function_name_, "non-nullable const char* was NULL"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | const char* errorKind = NULL; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 818 | uint8_t utf8 = CheckUtfBytes(bytes, &errorKind); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 819 | if (errorKind != NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 820 | JniAbortF(function_name_, |
| 821 | "input is not valid Modified UTF-8: illegal %s byte %#x\n" |
| 822 | " string: '%s'", errorKind, utf8, bytes); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 823 | return; |
| 824 | } |
| 825 | } |
| 826 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 827 | static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 828 | while (*bytes != '\0') { |
| 829 | uint8_t utf8 = *(bytes++); |
| 830 | // Switch on the high four bits. |
| 831 | switch (utf8 >> 4) { |
| 832 | case 0x00: |
| 833 | case 0x01: |
| 834 | case 0x02: |
| 835 | case 0x03: |
| 836 | case 0x04: |
| 837 | case 0x05: |
| 838 | case 0x06: |
| 839 | case 0x07: |
| 840 | // Bit pattern 0xxx. No need for any extra bytes. |
| 841 | break; |
| 842 | case 0x08: |
| 843 | case 0x09: |
| 844 | case 0x0a: |
| 845 | case 0x0b: |
| 846 | case 0x0f: |
| 847 | /* |
| 848 | * Bit pattern 10xx or 1111, which are illegal start bytes. |
| 849 | * Note: 1111 is valid for normal UTF-8, but not the |
Elliott Hughes | 78090d1 | 2011-10-07 14:31:47 -0700 | [diff] [blame] | 850 | * Modified UTF-8 used here. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 851 | */ |
| 852 | *errorKind = "start"; |
| 853 | return utf8; |
| 854 | case 0x0e: |
| 855 | // Bit pattern 1110, so there are two additional bytes. |
| 856 | utf8 = *(bytes++); |
| 857 | if ((utf8 & 0xc0) != 0x80) { |
| 858 | *errorKind = "continuation"; |
| 859 | return utf8; |
| 860 | } |
| 861 | // Fall through to take care of the final byte. |
| 862 | case 0x0c: |
| 863 | case 0x0d: |
| 864 | // Bit pattern 110x, so there is one additional byte. |
| 865 | utf8 = *(bytes++); |
| 866 | if ((utf8 & 0xc0) != 0x80) { |
| 867 | *errorKind = "continuation"; |
| 868 | return utf8; |
| 869 | } |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | return 0; |
| 874 | } |
| 875 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 876 | JNIEnvExt* env_; |
| 877 | JavaVMExt* vm_; |
| 878 | const char* function_name_; |
| 879 | int flags_; |
| 880 | bool has_method_; |
Elliott Hughes | 92cb498 | 2011-12-16 16:57:28 -0800 | [diff] [blame] | 881 | int indent_; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 882 | |
| 883 | DISALLOW_COPY_AND_ASSIGN(ScopedCheck); |
| 884 | }; |
| 885 | |
| 886 | #define CHECK_JNI_ENTRY(flags, types, args...) \ |
| 887 | ScopedCheck sc(env, flags, __FUNCTION__); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 888 | sc.Check(true, types, ##args) |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 889 | |
| 890 | #define CHECK_JNI_EXIT(type, exp) ({ \ |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 891 | typeof(exp) _rc = (exp); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 892 | sc.Check(false, type, _rc); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 893 | _rc; }) |
| 894 | #define CHECK_JNI_EXIT_VOID() \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 895 | sc.Check(false, "V") |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * =========================================================================== |
| 899 | * Guarded arrays |
| 900 | * =========================================================================== |
| 901 | */ |
| 902 | |
| 903 | #define kGuardLen 512 /* must be multiple of 2 */ |
| 904 | #define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */ |
| 905 | #define kGuardMagic 0xffd5aa96 |
| 906 | |
| 907 | /* this gets tucked in at the start of the buffer; struct size must be even */ |
| 908 | struct GuardedCopy { |
| 909 | uint32_t magic; |
| 910 | uLong adler; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 911 | size_t original_length; |
| 912 | const void* original_ptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 913 | |
| 914 | /* find the GuardedCopy given the pointer into the "live" data */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 915 | static inline const GuardedCopy* FromData(const void* dataBuf) { |
| 916 | return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Create an over-sized buffer to hold the contents of "buf". Copy it in, |
| 921 | * filling in the area around it with guard data. |
| 922 | * |
| 923 | * We use a 16-bit pattern to make a rogue memset less likely to elude us. |
| 924 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 925 | static void* Create(const void* buf, size_t len, bool modOkay) { |
| 926 | size_t newLen = ActualLength(len); |
| 927 | uint8_t* newBuf = DebugAlloc(newLen); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 928 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 929 | // Fill it in with a pattern. |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 930 | uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 931 | for (size_t i = 0; i < newLen / 2; i++) { |
| 932 | *pat++ = kGuardPattern; |
| 933 | } |
| 934 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 935 | // Copy the data in; note "len" could be zero. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 936 | memcpy(newBuf + kGuardLen / 2, buf, len); |
| 937 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 938 | // If modification is not expected, grab a checksum. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 939 | uLong adler = 0; |
| 940 | if (!modOkay) { |
| 941 | adler = adler32(0L, Z_NULL, 0); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 942 | adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len); |
| 943 | *reinterpret_cast<uLong*>(newBuf) = adler; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf); |
| 947 | pExtra->magic = kGuardMagic; |
| 948 | pExtra->adler = adler; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 949 | pExtra->original_ptr = buf; |
| 950 | pExtra->original_length = len; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 951 | |
| 952 | return newBuf + kGuardLen / 2; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Free up the guard buffer, scrub it, and return the original pointer. |
| 957 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 958 | static void* Destroy(void* dataBuf) { |
| 959 | const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 960 | void* original_ptr = const_cast<void*>(pExtra->original_ptr); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 961 | size_t len = pExtra->original_length; |
| 962 | DebugFree(dataBuf, len); |
| 963 | return original_ptr; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Verify the guard area and, if "modOkay" is false, that the data itself |
| 968 | * has not been altered. |
| 969 | * |
| 970 | * The caller has already checked that "dataBuf" is non-NULL. |
| 971 | */ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 972 | static void Check(const char* functionName, const void* dataBuf, bool modOkay) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 973 | static const uint32_t kMagicCmp = kGuardMagic; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 974 | const uint8_t* fullBuf = ActualBuffer(dataBuf); |
| 975 | const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 976 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 977 | // Before we do anything with "pExtra", check the magic number. We |
| 978 | // do the check with memcmp rather than "==" in case the pointer is |
| 979 | // unaligned. If it points to completely bogus memory we're going |
| 980 | // to crash, but there's no easy way around that. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 981 | if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) { |
| 982 | uint8_t buf[4]; |
| 983 | memcpy(buf, &pExtra->magic, 4); |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 984 | JniAbortF(functionName, |
| 985 | "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?", |
| 986 | buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 989 | size_t len = pExtra->original_length; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 990 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 991 | // Check bottom half of guard; skip over optional checksum storage. |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 992 | const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 993 | for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) { |
| 994 | if (pat[i] != kGuardPattern) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 995 | JniAbortF(functionName, "guard pattern(1) disturbed at %p +%d", fullBuf, i*2); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
| 999 | int offset = kGuardLen / 2 + len; |
| 1000 | if (offset & 0x01) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1001 | // Odd byte; expected value depends on endian. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1002 | const uint16_t patSample = kGuardPattern; |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1003 | uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1]; |
| 1004 | if (fullBuf[offset] != expected_byte) { |
| 1005 | JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x", |
| 1006 | fullBuf, offset, fullBuf[offset], expected_byte); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1007 | } |
| 1008 | offset++; |
| 1009 | } |
| 1010 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1011 | // Check top half of guard. |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1012 | pat = reinterpret_cast<const uint16_t*>(fullBuf + offset); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1013 | for (size_t i = 0; i < kGuardLen / 4; i++) { |
| 1014 | if (pat[i] != kGuardPattern) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1015 | JniAbortF(functionName, "guard pattern(2) disturbed at %p +%d", fullBuf, offset + i*2); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1019 | // If modification is not expected, verify checksum. Strictly speaking |
| 1020 | // this is wrong: if we told the client that we made a copy, there's no |
| 1021 | // reason they can't alter the buffer. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1022 | if (!modOkay) { |
| 1023 | uLong adler = adler32(0L, Z_NULL, 0); |
| 1024 | adler = adler32(adler, (const Bytef*)dataBuf, len); |
| 1025 | if (pExtra->adler != adler) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1026 | JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p", |
| 1027 | pExtra->adler, adler, dataBuf); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | private: |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1033 | static uint8_t* DebugAlloc(size_t len) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1034 | void* result = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
| 1035 | if (result == MAP_FAILED) { |
| 1036 | PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed"; |
| 1037 | } |
| 1038 | return reinterpret_cast<uint8_t*>(result); |
| 1039 | } |
| 1040 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1041 | static void DebugFree(void* dataBuf, size_t len) { |
| 1042 | uint8_t* fullBuf = ActualBuffer(dataBuf); |
| 1043 | size_t totalByteCount = ActualLength(len); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1044 | // TODO: we could mprotect instead, and keep the allocation around for a while. |
| 1045 | // This would be even more expensive, but it might catch more errors. |
| 1046 | // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 1047 | // PLOG(WARNING) << "mprotect(PROT_NONE) failed"; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1048 | // } |
| 1049 | if (munmap(fullBuf, totalByteCount) != 0) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1050 | PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed"; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1054 | static const uint8_t* ActualBuffer(const void* dataBuf) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1055 | return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2; |
| 1056 | } |
| 1057 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1058 | static uint8_t* ActualBuffer(void* dataBuf) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1059 | return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2; |
| 1060 | } |
| 1061 | |
| 1062 | // Underlying length of a user allocation of 'length' bytes. |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1063 | static size_t ActualLength(size_t length) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1064 | return (length + kGuardLen + 1) & ~0x01; |
| 1065 | } |
| 1066 | }; |
| 1067 | |
| 1068 | /* |
| 1069 | * Create a guarded copy of a primitive array. Modifications to the copied |
| 1070 | * data are allowed. Returns a pointer to the copied data. |
| 1071 | */ |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1072 | static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1073 | ScopedJniThreadState ts(env); |
| 1074 | |
| 1075 | Array* a = Decode<Array*>(ts, java_array); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1076 | size_t component_size = a->GetClass()->GetComponentSize(); |
| 1077 | size_t byte_count = a->GetLength() * component_size; |
| 1078 | void* result = GuardedCopy::Create(a->GetRawData(component_size), byte_count, true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1079 | if (isCopy != NULL) { |
| 1080 | *isCopy = JNI_TRUE; |
| 1081 | } |
| 1082 | return result; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * Perform the array "release" operation, which may or may not copy data |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1087 | * back into the managed heap, and may or may not release the underlying storage. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1088 | */ |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1089 | static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1090 | if (reinterpret_cast<uintptr_t>(dataBuf) == kNoCopyMagic) { |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | ScopedJniThreadState ts(env); |
| 1095 | Array* a = Decode<Array*>(ts, java_array); |
| 1096 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1097 | GuardedCopy::Check(__FUNCTION__, dataBuf, true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1098 | |
| 1099 | if (mode != JNI_ABORT) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1100 | size_t len = GuardedCopy::FromData(dataBuf)->original_length; |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1101 | memcpy(a->GetRawData(a->GetClass()->GetComponentSize()), dataBuf, len); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1102 | } |
| 1103 | if (mode != JNI_COMMIT) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1104 | GuardedCopy::Destroy(dataBuf); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * =========================================================================== |
| 1110 | * JNI functions |
| 1111 | * =========================================================================== |
| 1112 | */ |
| 1113 | |
| 1114 | class CheckJNI { |
| 1115 | public: |
| 1116 | static jint GetVersion(JNIEnv* env) { |
| 1117 | CHECK_JNI_ENTRY(kFlag_Default, "E", env); |
| 1118 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env)); |
| 1119 | } |
| 1120 | |
| 1121 | static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) { |
| 1122 | CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1123 | sc.CheckClassName(name); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1124 | return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen)); |
| 1125 | } |
| 1126 | |
| 1127 | static jclass FindClass(JNIEnv* env, const char* name) { |
| 1128 | CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1129 | sc.CheckClassName(name); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1130 | return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name)); |
| 1131 | } |
| 1132 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1133 | static jclass GetSuperclass(JNIEnv* env, jclass c) { |
| 1134 | CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c); |
| 1135 | return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1138 | static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) { |
| 1139 | CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2); |
| 1140 | return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) { |
| 1144 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method); |
| 1145 | // TODO: check that 'field' is a java.lang.reflect.Method. |
| 1146 | return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method)); |
| 1147 | } |
| 1148 | |
| 1149 | static jfieldID FromReflectedField(JNIEnv* env, jobject field) { |
| 1150 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field); |
| 1151 | // TODO: check that 'field' is a java.lang.reflect.Field. |
| 1152 | return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field)); |
| 1153 | } |
| 1154 | |
| 1155 | static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) { |
| 1156 | CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic); |
| 1157 | return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic)); |
| 1158 | } |
| 1159 | |
| 1160 | static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) { |
| 1161 | CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic); |
| 1162 | return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic)); |
| 1163 | } |
| 1164 | |
| 1165 | static jint Throw(JNIEnv* env, jthrowable obj) { |
| 1166 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj); |
| 1167 | // TODO: check that 'obj' is a java.lang.Throwable. |
| 1168 | return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj)); |
| 1169 | } |
| 1170 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1171 | static jint ThrowNew(JNIEnv* env, jclass c, const char* message) { |
| 1172 | CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message); |
| 1173 | return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static jthrowable ExceptionOccurred(JNIEnv* env) { |
| 1177 | CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env); |
| 1178 | return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env)); |
| 1179 | } |
| 1180 | |
| 1181 | static void ExceptionDescribe(JNIEnv* env) { |
| 1182 | CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env); |
| 1183 | baseEnv(env)->ExceptionDescribe(env); |
| 1184 | CHECK_JNI_EXIT_VOID(); |
| 1185 | } |
| 1186 | |
| 1187 | static void ExceptionClear(JNIEnv* env) { |
| 1188 | CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env); |
| 1189 | baseEnv(env)->ExceptionClear(env); |
| 1190 | CHECK_JNI_EXIT_VOID(); |
| 1191 | } |
| 1192 | |
| 1193 | static void FatalError(JNIEnv* env, const char* msg) { |
| 1194 | CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, msg); |
| 1195 | baseEnv(env)->FatalError(env, msg); |
| 1196 | CHECK_JNI_EXIT_VOID(); |
| 1197 | } |
| 1198 | |
| 1199 | static jint PushLocalFrame(JNIEnv* env, jint capacity) { |
| 1200 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity); |
| 1201 | return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity)); |
| 1202 | } |
| 1203 | |
| 1204 | static jobject PopLocalFrame(JNIEnv* env, jobject res) { |
| 1205 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res); |
| 1206 | return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res)); |
| 1207 | } |
| 1208 | |
| 1209 | static jobject NewGlobalRef(JNIEnv* env, jobject obj) { |
| 1210 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj); |
| 1211 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj)); |
| 1212 | } |
| 1213 | |
| 1214 | static jobject NewLocalRef(JNIEnv* env, jobject ref) { |
| 1215 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref); |
| 1216 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref)); |
| 1217 | } |
| 1218 | |
| 1219 | static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) { |
| 1220 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef); |
| 1221 | if (globalRef != NULL && GetIndirectRefKind(globalRef) != kGlobal) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1222 | JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p", |
| 1223 | ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1224 | } else { |
| 1225 | baseEnv(env)->DeleteGlobalRef(env, globalRef); |
| 1226 | CHECK_JNI_EXIT_VOID(); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) { |
| 1231 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef); |
| 1232 | if (weakGlobalRef != NULL && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1233 | JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p", |
| 1234 | ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1235 | } else { |
| 1236 | baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef); |
| 1237 | CHECK_JNI_EXIT_VOID(); |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | static void DeleteLocalRef(JNIEnv* env, jobject localRef) { |
| 1242 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef); |
Ian Rogers | 959f8ed | 2012-02-07 16:33:37 -0800 | [diff] [blame] | 1243 | if (localRef != NULL && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1244 | JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p", |
| 1245 | ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1246 | } else { |
| 1247 | baseEnv(env)->DeleteLocalRef(env, localRef); |
| 1248 | CHECK_JNI_EXIT_VOID(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) { |
| 1253 | CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity); |
| 1254 | return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity)); |
| 1255 | } |
| 1256 | |
| 1257 | static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) { |
| 1258 | CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2); |
| 1259 | return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2)); |
| 1260 | } |
| 1261 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1262 | static jobject AllocObject(JNIEnv* env, jclass c) { |
| 1263 | CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c); |
| 1264 | return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1267 | static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) { |
| 1268 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1269 | va_list args; |
| 1270 | va_start(args, mid); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1271 | jobject result = baseEnv(env)->NewObjectV(env, c, mid, args); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1272 | va_end(args); |
| 1273 | return CHECK_JNI_EXIT("L", result); |
| 1274 | } |
| 1275 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1276 | static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) { |
| 1277 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); |
| 1278 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1281 | static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) { |
| 1282 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); |
| 1283 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static jclass GetObjectClass(JNIEnv* env, jobject obj) { |
| 1287 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj); |
| 1288 | return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj)); |
| 1289 | } |
| 1290 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1291 | static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) { |
| 1292 | CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c); |
| 1293 | return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1296 | static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 1297 | CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig); |
| 1298 | return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1301 | static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 1302 | CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig); |
| 1303 | return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1306 | static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 1307 | CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig); |
| 1308 | return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1311 | static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) { |
| 1312 | CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig); |
| 1313 | return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | #define FIELD_ACCESSORS(_ctype, _jname, _type) \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1317 | static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \ |
| 1318 | CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \ |
| 1319 | sc.CheckStaticFieldID(c, fid); \ |
| 1320 | return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1321 | } \ |
| 1322 | static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \ |
| 1323 | CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1324 | sc.CheckInstanceFieldID(obj, fid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1325 | return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \ |
| 1326 | } \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1327 | static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \ |
| 1328 | CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \ |
| 1329 | sc.CheckStaticFieldID(c, fid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1330 | /* "value" arg only used when type == ref */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1331 | sc.CheckFieldType((jobject)(uint32_t)value, fid, _type[0], true); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1332 | baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1333 | CHECK_JNI_EXIT_VOID(); \ |
| 1334 | } \ |
| 1335 | static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \ |
| 1336 | CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1337 | sc.CheckInstanceFieldID(obj, fid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1338 | /* "value" arg only used when type == ref */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1339 | sc.CheckFieldType((jobject)(uint32_t) value, fid, _type[0], false); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1340 | baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \ |
| 1341 | CHECK_JNI_EXIT_VOID(); \ |
| 1342 | } |
| 1343 | |
| 1344 | FIELD_ACCESSORS(jobject, Object, "L"); |
| 1345 | FIELD_ACCESSORS(jboolean, Boolean, "Z"); |
| 1346 | FIELD_ACCESSORS(jbyte, Byte, "B"); |
| 1347 | FIELD_ACCESSORS(jchar, Char, "C"); |
| 1348 | FIELD_ACCESSORS(jshort, Short, "S"); |
| 1349 | FIELD_ACCESSORS(jint, Int, "I"); |
| 1350 | FIELD_ACCESSORS(jlong, Long, "J"); |
| 1351 | FIELD_ACCESSORS(jfloat, Float, "F"); |
| 1352 | FIELD_ACCESSORS(jdouble, Double, "D"); |
| 1353 | |
| 1354 | #define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \ |
| 1355 | /* Virtual... */ \ |
| 1356 | static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \ |
| 1357 | jmethodID mid, ...) \ |
| 1358 | { \ |
| 1359 | CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1360 | sc.CheckSig(mid, _retsig, false); \ |
| 1361 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1362 | _retdecl; \ |
| 1363 | va_list args; \ |
| 1364 | va_start(args, mid); \ |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1365 | _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1366 | va_end(args); \ |
| 1367 | _retok; \ |
| 1368 | } \ |
| 1369 | static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \ |
| 1370 | jmethodID mid, va_list args) \ |
| 1371 | { \ |
| 1372 | CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1373 | sc.CheckSig(mid, _retsig, false); \ |
| 1374 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1375 | _retdecl; \ |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1376 | _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1377 | _retok; \ |
| 1378 | } \ |
| 1379 | static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \ |
| 1380 | jmethodID mid, jvalue* args) \ |
| 1381 | { \ |
| 1382 | CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1383 | sc.CheckSig(mid, _retsig, false); \ |
| 1384 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1385 | _retdecl; \ |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1386 | _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1387 | _retok; \ |
| 1388 | } \ |
| 1389 | /* Non-virtual... */ \ |
| 1390 | static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1391 | jobject obj, jclass c, jmethodID mid, ...) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1392 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1393 | CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1394 | sc.CheckSig(mid, _retsig, false); \ |
| 1395 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1396 | _retdecl; \ |
| 1397 | va_list args; \ |
| 1398 | va_start(args, mid); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1399 | _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1400 | va_end(args); \ |
| 1401 | _retok; \ |
| 1402 | } \ |
| 1403 | static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1404 | jobject obj, jclass c, jmethodID mid, va_list args) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1405 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1406 | CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1407 | sc.CheckSig(mid, _retsig, false); \ |
| 1408 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1409 | _retdecl; \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1410 | _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1411 | _retok; \ |
| 1412 | } \ |
| 1413 | static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1414 | jobject obj, jclass c, jmethodID mid, jvalue* args) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1415 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1416 | CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1417 | sc.CheckSig(mid, _retsig, false); \ |
| 1418 | sc.CheckVirtualMethod(obj, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1419 | _retdecl; \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1420 | _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1421 | _retok; \ |
| 1422 | } \ |
| 1423 | /* Static... */ \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1424 | static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1425 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1426 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1427 | sc.CheckSig(mid, _retsig, true); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1428 | sc.CheckStaticMethod(c, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1429 | _retdecl; \ |
| 1430 | va_list args; \ |
| 1431 | va_start(args, mid); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1432 | _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1433 | va_end(args); \ |
| 1434 | _retok; \ |
| 1435 | } \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1436 | static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1437 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1438 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1439 | sc.CheckSig(mid, _retsig, true); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1440 | sc.CheckStaticMethod(c, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1441 | _retdecl; \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1442 | _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1443 | _retok; \ |
| 1444 | } \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1445 | static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1446 | { \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1447 | CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1448 | sc.CheckSig(mid, _retsig, true); \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1449 | sc.CheckStaticMethod(c, mid); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1450 | _retdecl; \ |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1451 | _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1452 | _retok; \ |
| 1453 | } |
| 1454 | |
| 1455 | #define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result) |
| 1456 | #define VOID_RETURN CHECK_JNI_EXIT_VOID() |
| 1457 | |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1458 | CALL(jobject, Object, Object* result, result = reinterpret_cast<Object*>, NON_VOID_RETURN("L", jobject), "L"); |
| 1459 | CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z"); |
| 1460 | CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B"); |
| 1461 | CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C"); |
| 1462 | CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S"); |
| 1463 | CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I"); |
| 1464 | CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J"); |
| 1465 | CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F"); |
| 1466 | CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1467 | CALL(void, Void, , , VOID_RETURN, "V"); |
| 1468 | |
| 1469 | static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) { |
| 1470 | CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len); |
| 1471 | return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len)); |
| 1472 | } |
| 1473 | |
| 1474 | static jsize GetStringLength(JNIEnv* env, jstring string) { |
| 1475 | CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string); |
| 1476 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string)); |
| 1477 | } |
| 1478 | |
| 1479 | static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) { |
| 1480 | CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy); |
| 1481 | const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1482 | if (sc.ForceCopy() && result != NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1483 | ScopedJniThreadState ts(env); |
| 1484 | String* s = Decode<String*>(ts, java_string); |
| 1485 | int byteCount = s->GetLength() * 2; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1486 | result = (const jchar*) GuardedCopy::Create(result, byteCount, false); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1487 | if (isCopy != NULL) { |
| 1488 | *isCopy = JNI_TRUE; |
| 1489 | } |
| 1490 | } |
| 1491 | return CHECK_JNI_EXIT("p", result); |
| 1492 | } |
| 1493 | |
| 1494 | static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) { |
| 1495 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1496 | sc.CheckNonNull(chars); |
| 1497 | if (sc.ForceCopy()) { |
| 1498 | GuardedCopy::Check(__FUNCTION__, chars, false); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1499 | chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars))); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1500 | } |
| 1501 | baseEnv(env)->ReleaseStringChars(env, string, chars); |
| 1502 | CHECK_JNI_EXIT_VOID(); |
| 1503 | } |
| 1504 | |
| 1505 | static jstring NewStringUTF(JNIEnv* env, const char* bytes) { |
| 1506 | CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string. |
| 1507 | return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes)); |
| 1508 | } |
| 1509 | |
| 1510 | static jsize GetStringUTFLength(JNIEnv* env, jstring string) { |
| 1511 | CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string); |
| 1512 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string)); |
| 1513 | } |
| 1514 | |
| 1515 | static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) { |
| 1516 | CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy); |
| 1517 | const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1518 | if (sc.ForceCopy() && result != NULL) { |
| 1519 | result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1520 | if (isCopy != NULL) { |
| 1521 | *isCopy = JNI_TRUE; |
| 1522 | } |
| 1523 | } |
| 1524 | return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string. |
| 1525 | } |
| 1526 | |
| 1527 | static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) { |
| 1528 | CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string. |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1529 | if (sc.ForceCopy()) { |
| 1530 | GuardedCopy::Check(__FUNCTION__, utf, false); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1531 | utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf))); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1532 | } |
| 1533 | baseEnv(env)->ReleaseStringUTFChars(env, string, utf); |
| 1534 | CHECK_JNI_EXIT_VOID(); |
| 1535 | } |
| 1536 | |
| 1537 | static jsize GetArrayLength(JNIEnv* env, jarray array) { |
| 1538 | CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array); |
| 1539 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array)); |
| 1540 | } |
| 1541 | |
| 1542 | static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) { |
| 1543 | CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement); |
| 1544 | return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement)); |
| 1545 | } |
| 1546 | |
| 1547 | static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) { |
| 1548 | CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index); |
| 1549 | return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index)); |
| 1550 | } |
| 1551 | |
| 1552 | static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) { |
| 1553 | CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value); |
| 1554 | baseEnv(env)->SetObjectArrayElement(env, array, index, value); |
| 1555 | CHECK_JNI_EXIT_VOID(); |
| 1556 | } |
| 1557 | |
| 1558 | #define NEW_PRIMITIVE_ARRAY(_artype, _jname) \ |
| 1559 | static _artype New##_jname##Array(JNIEnv* env, jsize length) { \ |
| 1560 | CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \ |
| 1561 | return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \ |
| 1562 | } |
| 1563 | NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean); |
| 1564 | NEW_PRIMITIVE_ARRAY(jbyteArray, Byte); |
| 1565 | NEW_PRIMITIVE_ARRAY(jcharArray, Char); |
| 1566 | NEW_PRIMITIVE_ARRAY(jshortArray, Short); |
| 1567 | NEW_PRIMITIVE_ARRAY(jintArray, Int); |
| 1568 | NEW_PRIMITIVE_ARRAY(jlongArray, Long); |
| 1569 | NEW_PRIMITIVE_ARRAY(jfloatArray, Float); |
| 1570 | NEW_PRIMITIVE_ARRAY(jdoubleArray, Double); |
| 1571 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1572 | struct ForceCopyGetChecker { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1573 | public: |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1574 | ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) { |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1575 | force_copy = sc.ForceCopy(); |
| 1576 | no_copy = 0; |
| 1577 | if (force_copy && isCopy != NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1578 | // Capture this before the base call tramples on it. |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1579 | no_copy = *reinterpret_cast<uint32_t*>(isCopy); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | template<typename ResultT> |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1584 | ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) { |
| 1585 | if (force_copy && result != NULL) { |
| 1586 | if (no_copy != kNoCopyMagic) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1587 | result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy)); |
| 1588 | } |
| 1589 | } |
| 1590 | return result; |
| 1591 | } |
| 1592 | |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1593 | uint32_t no_copy; |
| 1594 | bool force_copy; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1595 | }; |
| 1596 | |
| 1597 | #define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \ |
| 1598 | static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \ |
| 1599 | CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1600 | _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1601 | return CHECK_JNI_EXIT("p", result); \ |
| 1602 | } |
| 1603 | |
| 1604 | #define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \ |
| 1605 | static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \ |
| 1606 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \ |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1607 | sc.CheckNonNull(elems); \ |
| 1608 | if (sc.ForceCopy()) { \ |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1609 | ReleaseGuardedPACopy(env, array, elems, mode); \ |
| 1610 | } \ |
| 1611 | baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \ |
| 1612 | CHECK_JNI_EXIT_VOID(); \ |
| 1613 | } |
| 1614 | |
| 1615 | #define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \ |
| 1616 | static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \ |
| 1617 | CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \ |
| 1618 | baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \ |
| 1619 | CHECK_JNI_EXIT_VOID(); \ |
| 1620 | } |
| 1621 | |
| 1622 | #define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \ |
| 1623 | static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \ |
| 1624 | CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \ |
| 1625 | baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \ |
| 1626 | CHECK_JNI_EXIT_VOID(); \ |
| 1627 | } |
| 1628 | |
| 1629 | #define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \ |
| 1630 | GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \ |
| 1631 | RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \ |
| 1632 | GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \ |
| 1633 | SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); |
| 1634 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1635 | // TODO: verify primitive array type matches call type. |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1636 | PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z'); |
| 1637 | PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B'); |
| 1638 | PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C'); |
| 1639 | PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S'); |
| 1640 | PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I'); |
| 1641 | PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J'); |
| 1642 | PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F'); |
| 1643 | PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D'); |
| 1644 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1645 | static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) { |
| 1646 | CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods); |
| 1647 | return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1650 | static jint UnregisterNatives(JNIEnv* env, jclass c) { |
| 1651 | CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c); |
| 1652 | return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static jint MonitorEnter(JNIEnv* env, jobject obj) { |
| 1656 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1657 | if (!sc.CheckInstance(ScopedCheck::kObject, obj)) { |
| 1658 | return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already. |
| 1659 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1660 | return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj)); |
| 1661 | } |
| 1662 | |
| 1663 | static jint MonitorExit(JNIEnv* env, jobject obj) { |
| 1664 | CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj); |
Elliott Hughes | a92853e | 2012-02-07 16:09:27 -0800 | [diff] [blame] | 1665 | if (!sc.CheckInstance(ScopedCheck::kObject, obj)) { |
| 1666 | return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already. |
| 1667 | } |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1668 | return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj)); |
| 1669 | } |
| 1670 | |
| 1671 | static jint GetJavaVM(JNIEnv *env, JavaVM **vm) { |
| 1672 | CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm); |
| 1673 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm)); |
| 1674 | } |
| 1675 | |
| 1676 | static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) { |
| 1677 | CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf); |
| 1678 | baseEnv(env)->GetStringRegion(env, str, start, len, buf); |
| 1679 | CHECK_JNI_EXIT_VOID(); |
| 1680 | } |
| 1681 | |
| 1682 | static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) { |
| 1683 | CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf); |
| 1684 | baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf); |
| 1685 | CHECK_JNI_EXIT_VOID(); |
| 1686 | } |
| 1687 | |
| 1688 | static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) { |
| 1689 | CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy); |
| 1690 | void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1691 | if (sc.ForceCopy() && result != NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1692 | result = CreateGuardedPACopy(env, array, isCopy); |
| 1693 | } |
| 1694 | return CHECK_JNI_EXIT("p", result); |
| 1695 | } |
| 1696 | |
| 1697 | static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) { |
| 1698 | CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1699 | sc.CheckNonNull(carray); |
| 1700 | if (sc.ForceCopy()) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1701 | ReleaseGuardedPACopy(env, array, carray, mode); |
| 1702 | } |
| 1703 | baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode); |
| 1704 | CHECK_JNI_EXIT_VOID(); |
| 1705 | } |
| 1706 | |
| 1707 | static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) { |
| 1708 | CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy); |
| 1709 | const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1710 | if (sc.ForceCopy() && result != NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1711 | ScopedJniThreadState ts(env); |
| 1712 | String* s = Decode<String*>(ts, java_string); |
| 1713 | int byteCount = s->GetLength() * 2; |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1714 | result = (const jchar*) GuardedCopy::Create(result, byteCount, false); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1715 | if (isCopy != NULL) { |
| 1716 | *isCopy = JNI_TRUE; |
| 1717 | } |
| 1718 | } |
| 1719 | return CHECK_JNI_EXIT("p", result); |
| 1720 | } |
| 1721 | |
| 1722 | static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) { |
| 1723 | CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 1724 | sc.CheckNonNull(carray); |
| 1725 | if (sc.ForceCopy()) { |
| 1726 | GuardedCopy::Check(__FUNCTION__, carray, false); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1727 | carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray))); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1728 | } |
| 1729 | baseEnv(env)->ReleaseStringCritical(env, string, carray); |
| 1730 | CHECK_JNI_EXIT_VOID(); |
| 1731 | } |
| 1732 | |
| 1733 | static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) { |
| 1734 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj); |
| 1735 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj)); |
| 1736 | } |
| 1737 | |
| 1738 | static jboolean ExceptionCheck(JNIEnv* env) { |
| 1739 | CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env); |
| 1740 | return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env)); |
| 1741 | } |
| 1742 | |
| 1743 | static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) { |
| 1744 | // Note: we use "Ep" rather than "EL" because this is the one JNI function |
| 1745 | // that it's okay to pass an invalid reference to. |
| 1746 | CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj); |
| 1747 | // TODO: proper decoding of jobjectRefType! |
| 1748 | return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj)); |
| 1749 | } |
| 1750 | |
| 1751 | static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) { |
| 1752 | CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity); |
| 1753 | if (address == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1754 | JniAbortF(__FUNCTION__, "non-nullable address is NULL"); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1755 | } |
| 1756 | if (capacity <= 0) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame^] | 1757 | JniAbortF(__FUNCTION__, "capacity must be greater than 0: %d", capacity); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1758 | } |
| 1759 | return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity)); |
| 1760 | } |
| 1761 | |
| 1762 | static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) { |
| 1763 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf); |
| 1764 | // TODO: check that 'buf' is a java.nio.Buffer. |
| 1765 | return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf)); |
| 1766 | } |
| 1767 | |
| 1768 | static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) { |
| 1769 | CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf); |
| 1770 | // TODO: check that 'buf' is a java.nio.Buffer. |
| 1771 | return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf)); |
| 1772 | } |
| 1773 | |
| 1774 | private: |
| 1775 | static inline const JNINativeInterface* baseEnv(JNIEnv* env) { |
| 1776 | return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions; |
| 1777 | } |
| 1778 | }; |
| 1779 | |
| 1780 | const JNINativeInterface gCheckNativeInterface = { |
| 1781 | NULL, // reserved0. |
| 1782 | NULL, // reserved1. |
| 1783 | NULL, // reserved2. |
| 1784 | NULL, // reserved3. |
| 1785 | CheckJNI::GetVersion, |
| 1786 | CheckJNI::DefineClass, |
| 1787 | CheckJNI::FindClass, |
| 1788 | CheckJNI::FromReflectedMethod, |
| 1789 | CheckJNI::FromReflectedField, |
| 1790 | CheckJNI::ToReflectedMethod, |
| 1791 | CheckJNI::GetSuperclass, |
| 1792 | CheckJNI::IsAssignableFrom, |
| 1793 | CheckJNI::ToReflectedField, |
| 1794 | CheckJNI::Throw, |
| 1795 | CheckJNI::ThrowNew, |
| 1796 | CheckJNI::ExceptionOccurred, |
| 1797 | CheckJNI::ExceptionDescribe, |
| 1798 | CheckJNI::ExceptionClear, |
| 1799 | CheckJNI::FatalError, |
| 1800 | CheckJNI::PushLocalFrame, |
| 1801 | CheckJNI::PopLocalFrame, |
| 1802 | CheckJNI::NewGlobalRef, |
| 1803 | CheckJNI::DeleteGlobalRef, |
| 1804 | CheckJNI::DeleteLocalRef, |
| 1805 | CheckJNI::IsSameObject, |
| 1806 | CheckJNI::NewLocalRef, |
| 1807 | CheckJNI::EnsureLocalCapacity, |
| 1808 | CheckJNI::AllocObject, |
| 1809 | CheckJNI::NewObject, |
| 1810 | CheckJNI::NewObjectV, |
| 1811 | CheckJNI::NewObjectA, |
| 1812 | CheckJNI::GetObjectClass, |
| 1813 | CheckJNI::IsInstanceOf, |
| 1814 | CheckJNI::GetMethodID, |
| 1815 | CheckJNI::CallObjectMethod, |
| 1816 | CheckJNI::CallObjectMethodV, |
| 1817 | CheckJNI::CallObjectMethodA, |
| 1818 | CheckJNI::CallBooleanMethod, |
| 1819 | CheckJNI::CallBooleanMethodV, |
| 1820 | CheckJNI::CallBooleanMethodA, |
| 1821 | CheckJNI::CallByteMethod, |
| 1822 | CheckJNI::CallByteMethodV, |
| 1823 | CheckJNI::CallByteMethodA, |
| 1824 | CheckJNI::CallCharMethod, |
| 1825 | CheckJNI::CallCharMethodV, |
| 1826 | CheckJNI::CallCharMethodA, |
| 1827 | CheckJNI::CallShortMethod, |
| 1828 | CheckJNI::CallShortMethodV, |
| 1829 | CheckJNI::CallShortMethodA, |
| 1830 | CheckJNI::CallIntMethod, |
| 1831 | CheckJNI::CallIntMethodV, |
| 1832 | CheckJNI::CallIntMethodA, |
| 1833 | CheckJNI::CallLongMethod, |
| 1834 | CheckJNI::CallLongMethodV, |
| 1835 | CheckJNI::CallLongMethodA, |
| 1836 | CheckJNI::CallFloatMethod, |
| 1837 | CheckJNI::CallFloatMethodV, |
| 1838 | CheckJNI::CallFloatMethodA, |
| 1839 | CheckJNI::CallDoubleMethod, |
| 1840 | CheckJNI::CallDoubleMethodV, |
| 1841 | CheckJNI::CallDoubleMethodA, |
| 1842 | CheckJNI::CallVoidMethod, |
| 1843 | CheckJNI::CallVoidMethodV, |
| 1844 | CheckJNI::CallVoidMethodA, |
| 1845 | CheckJNI::CallNonvirtualObjectMethod, |
| 1846 | CheckJNI::CallNonvirtualObjectMethodV, |
| 1847 | CheckJNI::CallNonvirtualObjectMethodA, |
| 1848 | CheckJNI::CallNonvirtualBooleanMethod, |
| 1849 | CheckJNI::CallNonvirtualBooleanMethodV, |
| 1850 | CheckJNI::CallNonvirtualBooleanMethodA, |
| 1851 | CheckJNI::CallNonvirtualByteMethod, |
| 1852 | CheckJNI::CallNonvirtualByteMethodV, |
| 1853 | CheckJNI::CallNonvirtualByteMethodA, |
| 1854 | CheckJNI::CallNonvirtualCharMethod, |
| 1855 | CheckJNI::CallNonvirtualCharMethodV, |
| 1856 | CheckJNI::CallNonvirtualCharMethodA, |
| 1857 | CheckJNI::CallNonvirtualShortMethod, |
| 1858 | CheckJNI::CallNonvirtualShortMethodV, |
| 1859 | CheckJNI::CallNonvirtualShortMethodA, |
| 1860 | CheckJNI::CallNonvirtualIntMethod, |
| 1861 | CheckJNI::CallNonvirtualIntMethodV, |
| 1862 | CheckJNI::CallNonvirtualIntMethodA, |
| 1863 | CheckJNI::CallNonvirtualLongMethod, |
| 1864 | CheckJNI::CallNonvirtualLongMethodV, |
| 1865 | CheckJNI::CallNonvirtualLongMethodA, |
| 1866 | CheckJNI::CallNonvirtualFloatMethod, |
| 1867 | CheckJNI::CallNonvirtualFloatMethodV, |
| 1868 | CheckJNI::CallNonvirtualFloatMethodA, |
| 1869 | CheckJNI::CallNonvirtualDoubleMethod, |
| 1870 | CheckJNI::CallNonvirtualDoubleMethodV, |
| 1871 | CheckJNI::CallNonvirtualDoubleMethodA, |
| 1872 | CheckJNI::CallNonvirtualVoidMethod, |
| 1873 | CheckJNI::CallNonvirtualVoidMethodV, |
| 1874 | CheckJNI::CallNonvirtualVoidMethodA, |
| 1875 | CheckJNI::GetFieldID, |
| 1876 | CheckJNI::GetObjectField, |
| 1877 | CheckJNI::GetBooleanField, |
| 1878 | CheckJNI::GetByteField, |
| 1879 | CheckJNI::GetCharField, |
| 1880 | CheckJNI::GetShortField, |
| 1881 | CheckJNI::GetIntField, |
| 1882 | CheckJNI::GetLongField, |
| 1883 | CheckJNI::GetFloatField, |
| 1884 | CheckJNI::GetDoubleField, |
| 1885 | CheckJNI::SetObjectField, |
| 1886 | CheckJNI::SetBooleanField, |
| 1887 | CheckJNI::SetByteField, |
| 1888 | CheckJNI::SetCharField, |
| 1889 | CheckJNI::SetShortField, |
| 1890 | CheckJNI::SetIntField, |
| 1891 | CheckJNI::SetLongField, |
| 1892 | CheckJNI::SetFloatField, |
| 1893 | CheckJNI::SetDoubleField, |
| 1894 | CheckJNI::GetStaticMethodID, |
| 1895 | CheckJNI::CallStaticObjectMethod, |
| 1896 | CheckJNI::CallStaticObjectMethodV, |
| 1897 | CheckJNI::CallStaticObjectMethodA, |
| 1898 | CheckJNI::CallStaticBooleanMethod, |
| 1899 | CheckJNI::CallStaticBooleanMethodV, |
| 1900 | CheckJNI::CallStaticBooleanMethodA, |
| 1901 | CheckJNI::CallStaticByteMethod, |
| 1902 | CheckJNI::CallStaticByteMethodV, |
| 1903 | CheckJNI::CallStaticByteMethodA, |
| 1904 | CheckJNI::CallStaticCharMethod, |
| 1905 | CheckJNI::CallStaticCharMethodV, |
| 1906 | CheckJNI::CallStaticCharMethodA, |
| 1907 | CheckJNI::CallStaticShortMethod, |
| 1908 | CheckJNI::CallStaticShortMethodV, |
| 1909 | CheckJNI::CallStaticShortMethodA, |
| 1910 | CheckJNI::CallStaticIntMethod, |
| 1911 | CheckJNI::CallStaticIntMethodV, |
| 1912 | CheckJNI::CallStaticIntMethodA, |
| 1913 | CheckJNI::CallStaticLongMethod, |
| 1914 | CheckJNI::CallStaticLongMethodV, |
| 1915 | CheckJNI::CallStaticLongMethodA, |
| 1916 | CheckJNI::CallStaticFloatMethod, |
| 1917 | CheckJNI::CallStaticFloatMethodV, |
| 1918 | CheckJNI::CallStaticFloatMethodA, |
| 1919 | CheckJNI::CallStaticDoubleMethod, |
| 1920 | CheckJNI::CallStaticDoubleMethodV, |
| 1921 | CheckJNI::CallStaticDoubleMethodA, |
| 1922 | CheckJNI::CallStaticVoidMethod, |
| 1923 | CheckJNI::CallStaticVoidMethodV, |
| 1924 | CheckJNI::CallStaticVoidMethodA, |
| 1925 | CheckJNI::GetStaticFieldID, |
| 1926 | CheckJNI::GetStaticObjectField, |
| 1927 | CheckJNI::GetStaticBooleanField, |
| 1928 | CheckJNI::GetStaticByteField, |
| 1929 | CheckJNI::GetStaticCharField, |
| 1930 | CheckJNI::GetStaticShortField, |
| 1931 | CheckJNI::GetStaticIntField, |
| 1932 | CheckJNI::GetStaticLongField, |
| 1933 | CheckJNI::GetStaticFloatField, |
| 1934 | CheckJNI::GetStaticDoubleField, |
| 1935 | CheckJNI::SetStaticObjectField, |
| 1936 | CheckJNI::SetStaticBooleanField, |
| 1937 | CheckJNI::SetStaticByteField, |
| 1938 | CheckJNI::SetStaticCharField, |
| 1939 | CheckJNI::SetStaticShortField, |
| 1940 | CheckJNI::SetStaticIntField, |
| 1941 | CheckJNI::SetStaticLongField, |
| 1942 | CheckJNI::SetStaticFloatField, |
| 1943 | CheckJNI::SetStaticDoubleField, |
| 1944 | CheckJNI::NewString, |
| 1945 | CheckJNI::GetStringLength, |
| 1946 | CheckJNI::GetStringChars, |
| 1947 | CheckJNI::ReleaseStringChars, |
| 1948 | CheckJNI::NewStringUTF, |
| 1949 | CheckJNI::GetStringUTFLength, |
| 1950 | CheckJNI::GetStringUTFChars, |
| 1951 | CheckJNI::ReleaseStringUTFChars, |
| 1952 | CheckJNI::GetArrayLength, |
| 1953 | CheckJNI::NewObjectArray, |
| 1954 | CheckJNI::GetObjectArrayElement, |
| 1955 | CheckJNI::SetObjectArrayElement, |
| 1956 | CheckJNI::NewBooleanArray, |
| 1957 | CheckJNI::NewByteArray, |
| 1958 | CheckJNI::NewCharArray, |
| 1959 | CheckJNI::NewShortArray, |
| 1960 | CheckJNI::NewIntArray, |
| 1961 | CheckJNI::NewLongArray, |
| 1962 | CheckJNI::NewFloatArray, |
| 1963 | CheckJNI::NewDoubleArray, |
| 1964 | CheckJNI::GetBooleanArrayElements, |
| 1965 | CheckJNI::GetByteArrayElements, |
| 1966 | CheckJNI::GetCharArrayElements, |
| 1967 | CheckJNI::GetShortArrayElements, |
| 1968 | CheckJNI::GetIntArrayElements, |
| 1969 | CheckJNI::GetLongArrayElements, |
| 1970 | CheckJNI::GetFloatArrayElements, |
| 1971 | CheckJNI::GetDoubleArrayElements, |
| 1972 | CheckJNI::ReleaseBooleanArrayElements, |
| 1973 | CheckJNI::ReleaseByteArrayElements, |
| 1974 | CheckJNI::ReleaseCharArrayElements, |
| 1975 | CheckJNI::ReleaseShortArrayElements, |
| 1976 | CheckJNI::ReleaseIntArrayElements, |
| 1977 | CheckJNI::ReleaseLongArrayElements, |
| 1978 | CheckJNI::ReleaseFloatArrayElements, |
| 1979 | CheckJNI::ReleaseDoubleArrayElements, |
| 1980 | CheckJNI::GetBooleanArrayRegion, |
| 1981 | CheckJNI::GetByteArrayRegion, |
| 1982 | CheckJNI::GetCharArrayRegion, |
| 1983 | CheckJNI::GetShortArrayRegion, |
| 1984 | CheckJNI::GetIntArrayRegion, |
| 1985 | CheckJNI::GetLongArrayRegion, |
| 1986 | CheckJNI::GetFloatArrayRegion, |
| 1987 | CheckJNI::GetDoubleArrayRegion, |
| 1988 | CheckJNI::SetBooleanArrayRegion, |
| 1989 | CheckJNI::SetByteArrayRegion, |
| 1990 | CheckJNI::SetCharArrayRegion, |
| 1991 | CheckJNI::SetShortArrayRegion, |
| 1992 | CheckJNI::SetIntArrayRegion, |
| 1993 | CheckJNI::SetLongArrayRegion, |
| 1994 | CheckJNI::SetFloatArrayRegion, |
| 1995 | CheckJNI::SetDoubleArrayRegion, |
| 1996 | CheckJNI::RegisterNatives, |
| 1997 | CheckJNI::UnregisterNatives, |
| 1998 | CheckJNI::MonitorEnter, |
| 1999 | CheckJNI::MonitorExit, |
| 2000 | CheckJNI::GetJavaVM, |
| 2001 | CheckJNI::GetStringRegion, |
| 2002 | CheckJNI::GetStringUTFRegion, |
| 2003 | CheckJNI::GetPrimitiveArrayCritical, |
| 2004 | CheckJNI::ReleasePrimitiveArrayCritical, |
| 2005 | CheckJNI::GetStringCritical, |
| 2006 | CheckJNI::ReleaseStringCritical, |
| 2007 | CheckJNI::NewWeakGlobalRef, |
| 2008 | CheckJNI::DeleteWeakGlobalRef, |
| 2009 | CheckJNI::ExceptionCheck, |
| 2010 | CheckJNI::NewDirectByteBuffer, |
| 2011 | CheckJNI::GetDirectBufferAddress, |
| 2012 | CheckJNI::GetDirectBufferCapacity, |
| 2013 | CheckJNI::GetObjectRefType, |
| 2014 | }; |
| 2015 | |
| 2016 | const JNINativeInterface* GetCheckJniNativeInterface() { |
| 2017 | return &gCheckNativeInterface; |
| 2018 | } |
| 2019 | |
| 2020 | class CheckJII { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2021 | public: |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2022 | static jint DestroyJavaVM(JavaVM* vm) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2023 | ScopedCheck sc(vm, false, __FUNCTION__); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2024 | sc.Check(true, "v", vm); |
| 2025 | return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2029 | ScopedCheck sc(vm, false, __FUNCTION__); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2030 | sc.Check(true, "vpp", vm, p_env, thr_args); |
| 2031 | return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2035 | ScopedCheck sc(vm, false, __FUNCTION__); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2036 | sc.Check(true, "vpp", vm, p_env, thr_args); |
| 2037 | return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | static jint DetachCurrentThread(JavaVM* vm) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2041 | ScopedCheck sc(vm, true, __FUNCTION__); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2042 | sc.Check(true, "v", vm); |
| 2043 | return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | static jint GetEnv(JavaVM* vm, void** env, jint version) { |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 2047 | ScopedCheck sc(vm, true, __FUNCTION__); |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2048 | sc.Check(true, "v", vm); |
| 2049 | return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | private: |
Elliott Hughes | 32ae6e3 | 2011-09-27 10:46:50 -0700 | [diff] [blame] | 2053 | static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 2054 | return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions; |
| 2055 | } |
| 2056 | }; |
| 2057 | |
| 2058 | const JNIInvokeInterface gCheckInvokeInterface = { |
| 2059 | NULL, // reserved0 |
| 2060 | NULL, // reserved1 |
| 2061 | NULL, // reserved2 |
| 2062 | CheckJII::DestroyJavaVM, |
| 2063 | CheckJII::AttachCurrentThread, |
| 2064 | CheckJII::DetachCurrentThread, |
| 2065 | CheckJII::GetEnv, |
| 2066 | CheckJII::AttachCurrentThreadAsDaemon |
| 2067 | }; |
| 2068 | |
| 2069 | const JNIInvokeInterface* GetCheckJniInvokeInterface() { |
| 2070 | return &gCheckInvokeInterface; |
| 2071 | } |
| 2072 | |
| 2073 | } // namespace art |