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