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