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