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