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