blob: b52941b258064de6934e86fd6f7ddc6a85342d39 [file] [log] [blame]
Elliott Hughesa2501992011-08-26 19:39:54 -07001/*
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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070023#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/space/space.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_field-inl.h"
28#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
32#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Jeff Hao58df3272013-04-22 15:28:53 -070034#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035#include "scoped_thread_state_change.h"
Elliott Hughesa2501992011-08-26 19:39:54 -070036#include "thread.h"
37
38namespace art {
39
Elliott Hughes3f6635a2012-06-19 13:37:49 -070040static void JniAbort(const char* jni_function_name, const char* msg) {
Elliott Hughesa0957642011-09-02 14:27:33 -070041 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 ScopedObjectAccess soa(self);
Ian Rogersef7d42f2014-01-06 12:55:46 -080043 mirror::ArtMethod* current_method = self->GetCurrentMethod(nullptr);
Elliott Hughesa2501992011-08-26 19:39:54 -070044
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070045 std::ostringstream os;
Elliott Hughes3f6635a2012-06-19 13:37:49 -070046 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
Elliott Hughesa2501992011-08-26 19:39:54 -070047
Ian Rogersef7d42f2014-01-06 12:55:46 -080048 if (jni_function_name != nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070049 os << "\n in call to " << jni_function_name;
Elliott Hughesa2501992011-08-26 19:39:54 -070050 }
Elliott Hughesa0957642011-09-02 14:27:33 -070051 // TODO: is this useful given that we're about to dump the calling thread's stack?
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 if (current_method != nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -070053 os << "\n from " << PrettyMethod(current_method);
Elliott Hughesa0957642011-09-02 14:27:33 -070054 }
55 os << "\n";
56 self->Dump(os);
Elliott Hughesa2501992011-08-26 19:39:54 -070057
58 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 if (vm->check_jni_abort_hook != nullptr) {
Elliott Hughesb264f082012-04-06 17:10:10 -070060 vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
Elliott Hughesa2501992011-08-26 19:39:54 -070061 } else {
Ian Rogers9da7f592012-08-20 17:14:28 -070062 // Ensure that we get a native stack trace for this thread.
63 self->TransitionFromRunnableToSuspended(kNative);
Elliott Hughesa2501992011-08-26 19:39:54 -070064 LOG(FATAL) << os.str();
Ian Rogers9da7f592012-08-20 17:14:28 -070065 self->TransitionFromSuspendedToRunnable(); // Unreachable, keep annotalysis happy.
Elliott Hughesa2501992011-08-26 19:39:54 -070066 }
67}
68
Elliott Hughes3f6635a2012-06-19 13:37:49 -070069static void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
70 std::string msg;
71 StringAppendV(&msg, fmt, ap);
72 JniAbort(jni_function_name, msg.c_str());
73}
74
75void JniAbortF(const char* jni_function_name, const char* fmt, ...) {
76 va_list args;
77 va_start(args, fmt);
78 JniAbortV(jni_function_name, fmt, args);
79 va_end(args);
80}
81
Elliott Hughesa2501992011-08-26 19:39:54 -070082/*
83 * ===========================================================================
84 * JNI function helpers
85 * ===========================================================================
86 */
87
Ian Rogers959f8ed2012-02-07 16:33:37 -080088static bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
89 return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
Ian Rogers0399dde2012-06-06 17:09:28 -070090 reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
Ian Rogers959f8ed2012-02-07 16:33:37 -080091}
92
Elliott Hughes3f6635a2012-06-19 13:37:49 -070093// Flags passed into ScopedCheck.
Elliott Hughesa2501992011-08-26 19:39:54 -070094#define kFlag_Default 0x0000
95
Elliott Hughes3f6635a2012-06-19 13:37:49 -070096#define kFlag_CritBad 0x0000 // Calling while in critical is not allowed.
97#define kFlag_CritOkay 0x0001 // Calling while in critical is allowed.
98#define kFlag_CritGet 0x0002 // This is a critical "get".
99#define kFlag_CritRelease 0x0003 // This is a critical "release".
100#define kFlag_CritMask 0x0003 // Bit mask to get "crit" value.
Elliott Hughesa2501992011-08-26 19:39:54 -0700101
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700102#define kFlag_ExcepBad 0x0000 // Raised exceptions are not allowed.
103#define kFlag_ExcepOkay 0x0004 // Raised exceptions are allowed.
Elliott Hughesa2501992011-08-26 19:39:54 -0700104
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700105#define kFlag_Release 0x0010 // Are we in a non-critical release function?
106#define kFlag_NullableUtf 0x0020 // Are our UTF parameters nullable?
Elliott Hughesa2501992011-08-26 19:39:54 -0700107
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700108#define kFlag_Invocation 0x8000 // Part of the invocation interface (JavaVM*).
Elliott Hughesa2501992011-08-26 19:39:54 -0700109
Elliott Hughes485cac42011-12-09 17:49:35 -0800110#define kFlag_ForceTrace 0x80000000 // Add this to a JNI function's flags if you want to trace every call.
111
Elliott Hughesa0957642011-09-02 14:27:33 -0700112static const char* gBuiltInPrefixes[] = {
113 "Landroid/",
114 "Lcom/android/",
115 "Lcom/google/android/",
116 "Ldalvik/",
117 "Ljava/",
118 "Ljavax/",
119 "Llibcore/",
120 "Lorg/apache/harmony/",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 nullptr
Elliott Hughesa0957642011-09-02 14:27:33 -0700122};
123
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124static bool ShouldTrace(JavaVMExt* vm, mirror::ArtMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700126 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
127 // when a native method that matches the -Xjnitrace argument calls a JNI function
128 // such as NewByteArray.
129 // If -verbose:third-party-jni is on, we want to log any JNI function calls
130 // made by a third-party native method.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700131 std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
132 if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700133 return true;
134 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800135 if (VLOG_IS_ON(third_party_jni)) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700136 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
137 // like part of Android.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 for (size_t i = 0; gBuiltInPrefixes[i] != nullptr; ++i) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700139 if (StartsWith(class_name, gBuiltInPrefixes[i])) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700140 return false;
141 }
142 }
143 return true;
144 }
145 return false;
146}
147
Elliott Hughesa2501992011-08-26 19:39:54 -0700148class ScopedCheck {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800149 public:
Elliott Hughesa2501992011-08-26 19:39:54 -0700150 // For JNIEnv* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 : soa_(env) {
Ian Rogers365c1022012-06-22 15:05:28 -0700154 Init(flags, functionName, true);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700155 CheckThread(flags);
Elliott Hughesa2501992011-08-26 19:39:54 -0700156 }
157
158 // For JavaVM* functions.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
160 explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 : soa_(vm) {
Ian Rogers365c1022012-06-22 15:05:28 -0700163 Init(kFlag_Invocation, functionName, has_method);
Elliott Hughesa2501992011-08-26 19:39:54 -0700164 }
165
Ian Rogersb726dcb2012-09-05 08:57:23 -0700166 ~ScopedCheck() UNLOCK_FUNCTION(Locks::mutator_lock_) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167
168 const ScopedObjectAccess& soa() {
169 return soa_;
170 }
171
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700172 bool ForceCopy() {
Elliott Hughesa2501992011-08-26 19:39:54 -0700173 return Runtime::Current()->GetJavaVM()->force_copy;
174 }
175
Elliott Hughes81ff3182012-03-23 20:35:56 -0700176 // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
177 // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
178 // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
179 // circumstances, but this is incorrect.
180 void CheckClassName(const char* class_name) {
181 if (!IsValidJniClassName(class_name)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700182 JniAbortF(function_name_,
183 "illegal class name '%s'\n"
184 " (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
185 class_name);
Elliott Hughesa2501992011-08-26 19:39:54 -0700186 }
187 }
188
189 /*
190 * Verify that the field is of the appropriate type. If the field has an
191 * object type, "java_object" is the object we're trying to assign into it.
192 *
193 * Works for both static and instance fields.
194 */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 void CheckFieldType(jvalue value, jfieldID fid, char prim, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700197 mirror::ArtField* f = CheckFieldID(fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800198 if (f == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700199 return;
200 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::Class* field_type = FieldHelper(f).GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 if (!field_type->IsPrimitive()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800203 jobject java_object = value.l;
204 if (java_object != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700206 // If java_object is a weak global ref whose referent has been cleared,
207 // obj will be NULL. Otherwise, obj should always be non-NULL
208 // and valid.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700209 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700210 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700211 JniAbortF(function_name_, "field operation on invalid %s: %p",
212 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700213 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700214 } else {
Brian Carlstrom16192862011-09-12 17:50:06 -0700215 if (!obj->InstanceOf(field_type)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700216 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
217 PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 return;
219 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700221 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700223 JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
224 PrettyField(f).c_str(), prim);
Elliott Hughesa2501992011-08-26 19:39:54 -0700225 return;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 }
227
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700228 if (isStatic != f->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700229 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700230 JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700231 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700232 JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700233 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700234 return;
235 }
236 }
237
238 /*
239 * Verify that this instance field ID is valid for this object.
240 *
241 * Assumes "jobj" has already been validated.
242 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 void CheckInstanceFieldID(jobject java_object, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 if (o == nullptr || !Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700247 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700248 JniAbortF(function_name_, "field operation on invalid %s: %p",
249 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700250 return;
251 }
252
Brian Carlstromea46f952013-07-30 01:26:50 -0700253 mirror::ArtField* f = CheckFieldID(fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800254 if (f == nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700255 return;
256 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 mirror::Class* c = o->GetClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 FieldHelper fh(f);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800259 if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700260 JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
261 PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700262 }
263 }
264
265 /*
266 * Verify that the pointer value is non-NULL.
267 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700268 void CheckNonNull(const void* ptr) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 if (ptr == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700270 JniAbortF(function_name_, "non-nullable argument was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700271 }
272 }
273
274 /*
275 * Verify that the method's return type matches the type of call.
276 * 'expectedType' will be "L" for all objects, including arrays.
277 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700280 mirror::ArtMethod* m = CheckMethodID(mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 if (m == nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700282 return;
283 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 if (*expectedType != MethodHelper(m).GetShorty()[0]) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700285 JniAbortF(function_name_, "the return type of %s does not match %s",
286 function_name_, PrettyMethod(m).c_str());
287 }
288 if (isStatic != m->IsStatic()) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700289 if (isStatic) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700290 JniAbortF(function_name_, "calling non-static method %s with %s",
291 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700292 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700293 JniAbortF(function_name_, "calling static method %s with %s",
294 PrettyMethod(m).c_str(), function_name_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700295 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700296 }
297 }
298
299 /*
300 * Verify that this static field ID is valid for this class.
301 *
302 * Assumes "java_class" has already been validated.
303 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 void CheckStaticFieldID(jclass java_class, jfieldID fid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307 mirror::ArtField* f = CheckFieldID(fid);
308 if (f == nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700309 return;
310 }
Elliott Hughesa2501992011-08-26 19:39:54 -0700311 if (f->GetDeclaringClass() != c) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700312 JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
313 fid, PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700314 }
315 }
316
317 /*
Elliott Hughese84278b2012-03-22 10:06:53 -0700318 * Verify that "mid" is appropriate for "java_class".
Elliott Hughesa2501992011-08-26 19:39:54 -0700319 *
320 * A mismatch isn't dangerous, because the jmethodID defines the class. In
Elliott Hughese84278b2012-03-22 10:06:53 -0700321 * fact, java_class is unused in the implementation. It's best if we don't
Elliott Hughesa2501992011-08-26 19:39:54 -0700322 * allow bad code in the system though.
323 *
Elliott Hughese84278b2012-03-22 10:06:53 -0700324 * Instances of "java_class" must be instances of the method's declaring class.
Elliott Hughesa2501992011-08-26 19:39:54 -0700325 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 void CheckStaticMethod(jclass java_class, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 mirror::ArtMethod* m = CheckMethodID(mid);
329 if (m == nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700330 return;
331 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
Brian Carlstrom67fe2b42013-10-15 18:51:42 -0700333 if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700334 JniAbortF(function_name_, "can't call static %s on class %s",
335 PrettyMethod(m).c_str(), PrettyClass(c).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700336 }
337 }
338
339 /*
340 * Verify that "mid" is appropriate for "jobj".
341 *
342 * Make sure the object is an instance of the method's declaring class.
343 * (Note the mid might point to a declaration in an interface; this
344 * will be handled automatically by the instanceof check.)
345 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700346 void CheckVirtualMethod(jobject java_object, jmethodID mid)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800348 mirror::ArtMethod* m = CheckMethodID(mid);
349 if (m == nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700350 return;
351 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700353 if (!o->InstanceOf(m->GetDeclaringClass())) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700354 JniAbortF(function_name_, "can't call %s on instance of %s",
355 PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700356 }
357 }
358
359 /**
360 * The format string is a sequence of the following characters,
361 * and must be followed by arguments of the corresponding types
362 * in the same order.
363 *
364 * Java primitive types:
365 * B - jbyte
366 * C - jchar
367 * D - jdouble
368 * F - jfloat
369 * I - jint
370 * J - jlong
371 * S - jshort
372 * Z - jboolean (shown as true and false)
373 * V - void
374 *
375 * Java reference types:
376 * L - jobject
377 * a - jarray
378 * c - jclass
379 * s - jstring
380 *
381 * JNI types:
382 * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
383 * f - jfieldID
384 * m - jmethodID
385 * p - void*
386 * r - jint (for release mode arguments)
Elliott Hughes78090d12011-10-07 14:31:47 -0700387 * u - const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700388 * z - jsize (for lengths; use i if negative values are okay)
389 * v - JavaVM*
390 * E - JNIEnv*
391 * . - no argument; just print "..." (used for varargs JNI calls)
392 *
393 * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
394 */
Brian Carlstromdf629502013-07-17 22:39:56 -0700395 void Check(bool entry, const char* fmt0, ...) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700396 va_list ap;
397
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 mirror::ArtMethod* traceMethod = nullptr;
Ian Rogers9365f582013-04-19 09:55:42 -0700399 if (has_method_ && (!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700400 // We need to guard some of the invocation interface's calls: a bad caller might
401 // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
Elliott Hughesa0957642011-09-02 14:27:33 -0700402 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800403 if ((flags_ & kFlag_Invocation) == 0 || self != nullptr) {
404 traceMethod = self->GetCurrentMethod(nullptr);
Elliott Hughesa2501992011-08-26 19:39:54 -0700405 }
406 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700407
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408 if (((flags_ & kFlag_ForceTrace) != 0) ||
409 (traceMethod != nullptr && ShouldTrace(soa_.Vm(), traceMethod))) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700410 va_start(ap, fmt0);
411 std::string msg;
412 for (const char* fmt = fmt0; *fmt;) {
413 char ch = *fmt++;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700414 if (ch == 'B') { // jbyte
Elliott Hughesa2501992011-08-26 19:39:54 -0700415 jbyte b = va_arg(ap, int);
416 if (b >= 0 && b < 10) {
417 StringAppendF(&msg, "%d", b);
418 } else {
419 StringAppendF(&msg, "%#x (%d)", b, b);
420 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700421 } else if (ch == 'C') { // jchar
Elliott Hughesa2501992011-08-26 19:39:54 -0700422 jchar c = va_arg(ap, int);
423 if (c < 0x7f && c >= ' ') {
424 StringAppendF(&msg, "U+%x ('%c')", c, c);
425 } else {
426 StringAppendF(&msg, "U+%x", c);
427 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700428 } else if (ch == 'F' || ch == 'D') { // jfloat, jdouble
Elliott Hughesa2501992011-08-26 19:39:54 -0700429 StringAppendF(&msg, "%g", va_arg(ap, double));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700430 } else if (ch == 'I' || ch == 'S') { // jint, jshort
Elliott Hughesa2501992011-08-26 19:39:54 -0700431 StringAppendF(&msg, "%d", va_arg(ap, int));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700432 } else if (ch == 'J') { // jlong
Ian Rogersef7d42f2014-01-06 12:55:46 -0800433 StringAppendF(&msg, "%" PRId64, va_arg(ap, jlong));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700434 } else if (ch == 'Z') { // jboolean
Elliott Hughesa2501992011-08-26 19:39:54 -0700435 StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700436 } else if (ch == 'V') { // void
Elliott Hughesa2501992011-08-26 19:39:54 -0700437 msg += "void";
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700438 } else if (ch == 'v') { // JavaVM*
Elliott Hughesa2501992011-08-26 19:39:54 -0700439 JavaVM* vm = va_arg(ap, JavaVM*);
440 StringAppendF(&msg, "(JavaVM*)%p", vm);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700441 } else if (ch == 'E') { // JNIEnv*
Elliott Hughesa2501992011-08-26 19:39:54 -0700442 JNIEnv* env = va_arg(ap, JNIEnv*);
443 StringAppendF(&msg, "(JNIEnv*)%p", env);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700444 } else if (ch == 'L' || ch == 'a' || ch == 's') { // jobject, jarray, jstring
Elliott Hughesa2501992011-08-26 19:39:54 -0700445 // For logging purposes, these are identical.
446 jobject o = va_arg(ap, jobject);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800447 if (o == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700448 msg += "NULL";
449 } else {
450 StringAppendF(&msg, "%p", o);
451 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700452 } else if (ch == 'b') { // jboolean (JNI-style)
Elliott Hughesa2501992011-08-26 19:39:54 -0700453 jboolean b = va_arg(ap, int);
454 msg += (b ? "JNI_TRUE" : "JNI_FALSE");
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700455 } else if (ch == 'c') { // jclass
Elliott Hughesa2501992011-08-26 19:39:54 -0700456 jclass jc = va_arg(ap, jclass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457 mirror::Class* c = reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(jc));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800458 if (c == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700459 msg += "NULL";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700460 } else if (c == kInvalidIndirectRefObject ||
461 !Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
Elliott Hughes485cac42011-12-09 17:49:35 -0800462 StringAppendF(&msg, "INVALID POINTER:%p", jc);
463 } else if (!c->IsClass()) {
464 msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700465 } else {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700466 msg += PrettyClass(c);
Elliott Hughesa2501992011-08-26 19:39:54 -0700467 if (!entry) {
468 StringAppendF(&msg, " (%p)", jc);
469 }
470 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700471 } else if (ch == 'f') { // jfieldID
Elliott Hughesa2501992011-08-26 19:39:54 -0700472 jfieldID fid = va_arg(ap, jfieldID);
Brian Carlstromea46f952013-07-30 01:26:50 -0700473 mirror::ArtField* f = reinterpret_cast<mirror::ArtField*>(fid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700474 msg += PrettyField(f);
475 if (!entry) {
476 StringAppendF(&msg, " (%p)", fid);
477 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700478 } else if (ch == 'z') { // non-negative jsize
Elliott Hughesa2501992011-08-26 19:39:54 -0700479 // You might expect jsize to be size_t, but it's not; it's the same as jint.
480 // We only treat this specially so we can do the non-negative check.
481 // TODO: maybe this wasn't worth it?
482 jint i = va_arg(ap, jint);
483 StringAppendF(&msg, "%d", i);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700484 } else if (ch == 'm') { // jmethodID
Elliott Hughesa2501992011-08-26 19:39:54 -0700485 jmethodID mid = va_arg(ap, jmethodID);
Brian Carlstromea46f952013-07-30 01:26:50 -0700486 mirror::ArtMethod* m = reinterpret_cast<mirror::ArtMethod*>(mid);
Elliott Hughesa2501992011-08-26 19:39:54 -0700487 msg += PrettyMethod(m);
488 if (!entry) {
489 StringAppendF(&msg, " (%p)", mid);
490 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700491 } else if (ch == 'p') { // void* ("pointer")
Elliott Hughesa2501992011-08-26 19:39:54 -0700492 void* p = va_arg(ap, void*);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800493 if (p == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700494 msg += "NULL";
495 } else {
496 StringAppendF(&msg, "(void*) %p", p);
497 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700498 } else if (ch == 'r') { // jint (release mode)
Elliott Hughesa2501992011-08-26 19:39:54 -0700499 jint releaseMode = va_arg(ap, jint);
500 if (releaseMode == 0) {
501 msg += "0";
502 } else if (releaseMode == JNI_ABORT) {
503 msg += "JNI_ABORT";
504 } else if (releaseMode == JNI_COMMIT) {
505 msg += "JNI_COMMIT";
506 } else {
507 StringAppendF(&msg, "invalid release mode %d", releaseMode);
508 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700509 } else if (ch == 'u') { // const char* (Modified UTF-8)
Elliott Hughesa2501992011-08-26 19:39:54 -0700510 const char* utf = va_arg(ap, const char*);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800511 if (utf == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700512 msg += "NULL";
513 } else {
514 StringAppendF(&msg, "\"%s\"", utf);
515 }
516 } else if (ch == '.') {
517 msg += "...";
518 } else {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700519 JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
Elliott Hughesa2501992011-08-26 19:39:54 -0700520 return;
521 }
522 if (*fmt) {
523 StringAppendF(&msg, ", ");
524 }
525 }
526 va_end(ap);
527
Elliott Hughes485cac42011-12-09 17:49:35 -0800528 if ((flags_ & kFlag_ForceTrace) != 0) {
529 LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
530 } else if (entry) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700531 if (has_method_) {
Elliott Hughesa0957642011-09-02 14:27:33 -0700532 std::string methodName(PrettyMethod(traceMethod, false));
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700533 LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
534 indent_ = methodName.size() + 1;
Elliott Hughesa2501992011-08-26 19:39:54 -0700535 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700536 LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
537 indent_ = 0;
Elliott Hughesa2501992011-08-26 19:39:54 -0700538 }
539 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700540 LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700541 }
542 }
543
544 // We always do the thorough checks on entry, and never on exit...
545 if (entry) {
546 va_start(ap, fmt0);
547 for (const char* fmt = fmt0; *fmt; ++fmt) {
548 char ch = *fmt;
549 if (ch == 'a') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700550 CheckArray(va_arg(ap, jarray));
Elliott Hughesa2501992011-08-26 19:39:54 -0700551 } else if (ch == 'c') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700552 CheckInstance(kClass, va_arg(ap, jclass));
Elliott Hughesa2501992011-08-26 19:39:54 -0700553 } else if (ch == 'L') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700554 CheckObject(va_arg(ap, jobject));
Elliott Hughesa2501992011-08-26 19:39:54 -0700555 } else if (ch == 'r') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700556 CheckReleaseMode(va_arg(ap, jint));
Elliott Hughesa2501992011-08-26 19:39:54 -0700557 } else if (ch == 's') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700558 CheckInstance(kString, va_arg(ap, jstring));
Elliott Hughesa2501992011-08-26 19:39:54 -0700559 } else if (ch == 'u') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700560 if ((flags_ & kFlag_Release) != 0) {
561 CheckNonNull(va_arg(ap, const char*));
Elliott Hughesa2501992011-08-26 19:39:54 -0700562 } else {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700563 bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
564 CheckUtfString(va_arg(ap, const char*), nullable);
Elliott Hughesa2501992011-08-26 19:39:54 -0700565 }
566 } else if (ch == 'z') {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700567 CheckLengthPositive(va_arg(ap, jsize));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800568 } else if (strchr("BCISZbfmpEv", ch) != nullptr) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700569 va_arg(ap, uint32_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700570 } else if (ch == 'D' || ch == 'F') {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700571 va_arg(ap, double); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700572 } else if (ch == 'J') {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700573 va_arg(ap, uint64_t); // Skip this argument.
Elliott Hughesa2501992011-08-26 19:39:54 -0700574 } else if (ch == '.') {
575 } else {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800576 LOG(FATAL) << "Unknown check format specifier: " << ch;
Elliott Hughesa2501992011-08-26 19:39:54 -0700577 }
578 }
579 va_end(ap);
580 }
581 }
582
Elliott Hughesa92853e2012-02-07 16:09:27 -0800583 enum InstanceKind {
584 kClass,
Elliott Hughes0f3c5532012-03-30 14:51:51 -0700585 kDirectByteBuffer,
586 kObject,
587 kString,
588 kThrowable,
Elliott Hughesa92853e2012-02-07 16:09:27 -0800589 };
590
591 /*
592 * Verify that "jobj" is a valid non-NULL object reference, and points to
593 * an instance of expectedClass.
594 *
595 * Because we're looking at an object on the GC heap, we have to switch
596 * to "running" mode before doing the checks.
597 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 bool CheckInstance(InstanceKind kind, jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800600 const char* what = nullptr;
Elliott Hughesa92853e2012-02-07 16:09:27 -0800601 switch (kind) {
602 case kClass:
603 what = "jclass";
604 break;
605 case kDirectByteBuffer:
606 what = "direct ByteBuffer";
607 break;
608 case kObject:
609 what = "jobject";
610 break;
611 case kString:
612 what = "jstring";
613 break;
614 case kThrowable:
615 what = "jthrowable";
616 break;
617 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700618 LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800619 }
620
Ian Rogersef7d42f2014-01-06 12:55:46 -0800621 if (java_object == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700622 JniAbortF(function_name_, "%s received null %s", function_name_, what);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800623 return false;
624 }
625
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800626 mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700627 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700628 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700629 JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
630 what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -0800631 return false;
632 }
633
634 bool okay = true;
635 switch (kind) {
636 case kClass:
637 okay = obj->IsClass();
638 break;
639 case kDirectByteBuffer:
640 UNIMPLEMENTED(FATAL);
641 break;
642 case kString:
643 okay = obj->GetClass()->IsStringClass();
644 break;
645 case kThrowable:
646 okay = obj->GetClass()->IsThrowableClass();
647 break;
648 case kObject:
649 break;
650 }
651 if (!okay) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700652 JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
Elliott Hughesa92853e2012-02-07 16:09:27 -0800653 return false;
654 }
655
656 return true;
657 }
658
Elliott Hughesba8eee12012-01-24 20:25:24 -0800659 private:
Elliott Hughes81ff3182012-03-23 20:35:56 -0700660 // Set "has_method" to true if we have a valid thread with a method pointer.
661 // We won't have one before attaching a thread, after detaching a thread, or
662 // when shutting down the runtime.
Ian Rogers365c1022012-06-22 15:05:28 -0700663 void Init(int flags, const char* functionName, bool has_method) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700664 flags_ = flags;
665 function_name_ = functionName;
Elliott Hughes81ff3182012-03-23 20:35:56 -0700666 has_method_ = has_method;
Elliott Hughesa2501992011-08-26 19:39:54 -0700667 }
668
669 /*
670 * Verify that "array" is non-NULL and points to an Array object.
671 *
672 * Since we're dealing with objects, switch to "running" mode.
673 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700674 void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800675 if (java_array == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700676 JniAbortF(function_name_, "jarray was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700677 return;
678 }
679
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680 mirror::Array* a = soa_.Decode<mirror::Array*>(java_array);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700681 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(a)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700682 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700683 JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
684 ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
Elliott Hughesa2501992011-08-26 19:39:54 -0700685 } else if (!a->IsArrayInstance()) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700686 JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700687 }
688 }
689
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700690 void CheckLengthPositive(jsize length) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700691 if (length < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700692 JniAbortF(function_name_, "negative jsize: %d", length);
Elliott Hughesa2501992011-08-26 19:39:54 -0700693 }
694 }
695
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 mirror::ArtField* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800697 if (fid == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700698 JniAbortF(function_name_, "jfieldID was NULL");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800699 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700700 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700701 mirror::ArtField* f = soa_.DecodeField(fid);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700702 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(f) || !f->IsArtField()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700703 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700704 JniAbortF(function_name_, "invalid jfieldID: %p", fid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800705 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700706 }
707 return f;
708 }
709
Brian Carlstromea46f952013-07-30 01:26:50 -0700710 mirror::ArtMethod* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711 if (mid == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700712 JniAbortF(function_name_, "jmethodID was NULL");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800713 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700714 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700715 mirror::ArtMethod* m = soa_.DecodeMethod(mid);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700716 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(m) || !m->IsArtMethod()) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700717 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700718 JniAbortF(function_name_, "invalid jmethodID: %p", mid);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800719 return nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700720 }
721 return m;
722 }
723
Elliott Hughesa2501992011-08-26 19:39:54 -0700724 /*
725 * Verify that "jobj" is a valid object, and that it's an object that JNI
726 * is allowed to know about. We allow NULL references.
727 *
728 * Switches to "running" mode before performing checks.
729 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700730 void CheckObject(jobject java_object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700731 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800732 if (java_object == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700733 return;
734 }
735
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800736 mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700737 if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
Mathieu Chartier128c52c2012-10-16 14:12:41 -0700738 Runtime::Current()->GetHeap()->DumpSpaces();
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -0700739 // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700740 JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
741 ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
Elliott Hughesa2501992011-08-26 19:39:54 -0700742 }
743 }
744
745 /*
746 * Verify that the "mode" argument passed to a primitive array Release
747 * function is one of the valid values.
748 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700749 void CheckReleaseMode(jint mode) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700750 if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
Elliott Hughes96a98872012-12-19 14:21:15 -0800751 JniAbortF(function_name_, "unknown value for release mode: %d", mode);
Elliott Hughesa2501992011-08-26 19:39:54 -0700752 }
753 }
754
Ian Rogersb726dcb2012-09-05 08:57:23 -0700755 void CheckThread(int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700756 Thread* self = Thread::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800757 if (self == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700758 JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
Elliott Hughesa2501992011-08-26 19:39:54 -0700759 return;
760 }
761
762 // Get the *correct* JNIEnv by going through our TLS pointer.
763 JNIEnvExt* threadEnv = self->GetJniEnv();
764
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700765 // Verify that the current thread is (a) attached and (b) associated with
766 // this particular instance of JNIEnv.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 if (soa_.Env() != threadEnv) {
Ian Rogers987560f2014-04-22 11:42:59 -0700768 JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
769 ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
770 return;
Elliott Hughesa2501992011-08-26 19:39:54 -0700771 }
772
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700773 // Verify that, if this thread previously made a critical "get" call, we
774 // do the corresponding "release" call before we try anything else.
Elliott Hughesa2501992011-08-26 19:39:54 -0700775 switch (flags & kFlag_CritMask) {
776 case kFlag_CritOkay: // okay to call this method
777 break;
778 case kFlag_CritBad: // not okay to call
779 if (threadEnv->critical) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700780 JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700781 return;
782 }
783 break;
784 case kFlag_CritGet: // this is a "get" call
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700785 // Don't check here; we allow nested gets.
Elliott Hughesa2501992011-08-26 19:39:54 -0700786 threadEnv->critical++;
787 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700788 case kFlag_CritRelease: // this is a "release" call
Elliott Hughesa2501992011-08-26 19:39:54 -0700789 threadEnv->critical--;
790 if (threadEnv->critical < 0) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700791 JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700792 return;
793 }
794 break;
795 default:
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800796 LOG(FATAL) << "Bad flags (internal error): " << flags;
Elliott Hughesa2501992011-08-26 19:39:54 -0700797 }
798
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700799 // Verify that, if an exception has been raised, the native code doesn't
800 // make any JNI calls other than the Exception* methods.
Elliott Hughesa2501992011-08-26 19:39:54 -0700801 if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800802 ThrowLocation throw_location;
803 mirror::Throwable* exception = self->GetException(&throw_location);
804 std::string type(PrettyTypeOf(exception));
Elliott Hughes8e4d3ed2013-09-03 17:41:50 -0700805 JniAbortF(function_name_, "JNI %s called with pending exception '%s' thrown in %s",
806 function_name_, type.c_str(), throw_location.Dump().c_str());
Elliott Hughesa2501992011-08-26 19:39:54 -0700807 return;
808 }
809 }
810
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700811 // Verifies that "bytes" points to valid Modified UTF-8 data.
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700812 void CheckUtfString(const char* bytes, bool nullable) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800813 if (bytes == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700814 if (!nullable) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700815 JniAbortF(function_name_, "non-nullable const char* was NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -0700816 return;
817 }
818 return;
819 }
820
Ian Rogersef7d42f2014-01-06 12:55:46 -0800821 const char* errorKind = nullptr;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700822 uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800823 if (errorKind != nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700824 JniAbortF(function_name_,
825 "input is not valid Modified UTF-8: illegal %s byte %#x\n"
826 " string: '%s'", errorKind, utf8, bytes);
Elliott Hughesa2501992011-08-26 19:39:54 -0700827 return;
828 }
829 }
830
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700831 static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700832 while (*bytes != '\0') {
833 uint8_t utf8 = *(bytes++);
834 // Switch on the high four bits.
835 switch (utf8 >> 4) {
836 case 0x00:
837 case 0x01:
838 case 0x02:
839 case 0x03:
840 case 0x04:
841 case 0x05:
842 case 0x06:
843 case 0x07:
844 // Bit pattern 0xxx. No need for any extra bytes.
845 break;
846 case 0x08:
847 case 0x09:
848 case 0x0a:
849 case 0x0b:
850 case 0x0f:
851 /*
852 * Bit pattern 10xx or 1111, which are illegal start bytes.
853 * Note: 1111 is valid for normal UTF-8, but not the
Elliott Hughes78090d12011-10-07 14:31:47 -0700854 * Modified UTF-8 used here.
Elliott Hughesa2501992011-08-26 19:39:54 -0700855 */
856 *errorKind = "start";
857 return utf8;
858 case 0x0e:
859 // Bit pattern 1110, so there are two additional bytes.
860 utf8 = *(bytes++);
861 if ((utf8 & 0xc0) != 0x80) {
862 *errorKind = "continuation";
863 return utf8;
864 }
865 // Fall through to take care of the final byte.
866 case 0x0c:
867 case 0x0d:
868 // Bit pattern 110x, so there is one additional byte.
869 utf8 = *(bytes++);
870 if ((utf8 & 0xc0) != 0x80) {
871 *errorKind = "continuation";
872 return utf8;
873 }
874 break;
875 }
876 }
877 return 0;
878 }
879
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700880 const ScopedObjectAccess soa_;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700881 const char* function_name_;
882 int flags_;
883 bool has_method_;
Elliott Hughes92cb4982011-12-16 16:57:28 -0800884 int indent_;
Elliott Hughesa2501992011-08-26 19:39:54 -0700885
886 DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
887};
888
889#define CHECK_JNI_ENTRY(flags, types, args...) \
890 ScopedCheck sc(env, flags, __FUNCTION__); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700891 sc.Check(true, types, ##args)
Elliott Hughesa2501992011-08-26 19:39:54 -0700892
893#define CHECK_JNI_EXIT(type, exp) ({ \
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -0700894 auto _rc = (exp); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700895 sc.Check(false, type, _rc); \
Elliott Hughesa2501992011-08-26 19:39:54 -0700896 _rc; })
897#define CHECK_JNI_EXIT_VOID() \
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700898 sc.Check(false, "V")
Elliott Hughesa2501992011-08-26 19:39:54 -0700899
900/*
901 * ===========================================================================
902 * Guarded arrays
903 * ===========================================================================
904 */
905
906#define kGuardLen 512 /* must be multiple of 2 */
907#define kGuardPattern 0xd5e3 /* uncommon values; d5e3d5e3 invalid addr */
908#define kGuardMagic 0xffd5aa96
909
910/* this gets tucked in at the start of the buffer; struct size must be even */
911struct GuardedCopy {
912 uint32_t magic;
913 uLong adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700914 size_t original_length;
915 const void* original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700916
917 /* find the GuardedCopy given the pointer into the "live" data */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700918 static inline const GuardedCopy* FromData(const void* dataBuf) {
919 return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
Elliott Hughesa2501992011-08-26 19:39:54 -0700920 }
921
922 /*
923 * Create an over-sized buffer to hold the contents of "buf". Copy it in,
924 * filling in the area around it with guard data.
925 *
926 * We use a 16-bit pattern to make a rogue memset less likely to elude us.
927 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700928 static void* Create(const void* buf, size_t len, bool modOkay) {
929 size_t newLen = ActualLength(len);
930 uint8_t* newBuf = DebugAlloc(newLen);
Elliott Hughesa2501992011-08-26 19:39:54 -0700931
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700932 // Fill it in with a pattern.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800933 uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700934 for (size_t i = 0; i < newLen / 2; i++) {
935 *pat++ = kGuardPattern;
936 }
937
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700938 // Copy the data in; note "len" could be zero.
Elliott Hughesa2501992011-08-26 19:39:54 -0700939 memcpy(newBuf + kGuardLen / 2, buf, len);
940
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700941 // If modification is not expected, grab a checksum.
Elliott Hughesa2501992011-08-26 19:39:54 -0700942 uLong adler = 0;
943 if (!modOkay) {
944 adler = adler32(0L, Z_NULL, 0);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800945 adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
946 *reinterpret_cast<uLong*>(newBuf) = adler;
Elliott Hughesa2501992011-08-26 19:39:54 -0700947 }
948
949 GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
950 pExtra->magic = kGuardMagic;
951 pExtra->adler = adler;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700952 pExtra->original_ptr = buf;
953 pExtra->original_length = len;
Elliott Hughesa2501992011-08-26 19:39:54 -0700954
955 return newBuf + kGuardLen / 2;
956 }
957
958 /*
959 * Free up the guard buffer, scrub it, and return the original pointer.
960 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700961 static void* Destroy(void* dataBuf) {
962 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesba8eee12012-01-24 20:25:24 -0800963 void* original_ptr = const_cast<void*>(pExtra->original_ptr);
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700964 size_t len = pExtra->original_length;
965 DebugFree(dataBuf, len);
966 return original_ptr;
Elliott Hughesa2501992011-08-26 19:39:54 -0700967 }
968
969 /*
970 * Verify the guard area and, if "modOkay" is false, that the data itself
971 * has not been altered.
972 *
973 * The caller has already checked that "dataBuf" is non-NULL.
974 */
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700975 static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700976 static const uint32_t kMagicCmp = kGuardMagic;
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700977 const uint8_t* fullBuf = ActualBuffer(dataBuf);
978 const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700979
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700980 // Before we do anything with "pExtra", check the magic number. We
981 // do the check with memcmp rather than "==" in case the pointer is
982 // unaligned. If it points to completely bogus memory we're going
983 // to crash, but there's no easy way around that.
Elliott Hughesa2501992011-08-26 19:39:54 -0700984 if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
985 uint8_t buf[4];
986 memcpy(buf, &pExtra->magic, 4);
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700987 JniAbortF(functionName,
988 "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700989 buf[3], buf[2], buf[1], buf[0], dataBuf); // Assumes little-endian.
Elliott Hughesa2501992011-08-26 19:39:54 -0700990 }
991
Elliott Hughes32ae6e32011-09-27 10:46:50 -0700992 size_t len = pExtra->original_length;
Elliott Hughesa2501992011-08-26 19:39:54 -0700993
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700994 // Check bottom half of guard; skip over optional checksum storage.
Elliott Hughesba8eee12012-01-24 20:25:24 -0800995 const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -0700996 for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
997 if (pat[i] != kGuardPattern) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800998 JniAbortF(functionName, "guard pattern(1) disturbed at %p +%zd", fullBuf, i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -0700999 }
1000 }
1001
1002 int offset = kGuardLen / 2 + len;
1003 if (offset & 0x01) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001004 // Odd byte; expected value depends on endian.
Elliott Hughesa2501992011-08-26 19:39:54 -07001005 const uint16_t patSample = kGuardPattern;
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001006 uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
1007 if (fullBuf[offset] != expected_byte) {
1008 JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
1009 fullBuf, offset, fullBuf[offset], expected_byte);
Elliott Hughesa2501992011-08-26 19:39:54 -07001010 }
1011 offset++;
1012 }
1013
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001014 // Check top half of guard.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001015 pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
Elliott Hughesa2501992011-08-26 19:39:54 -07001016 for (size_t i = 0; i < kGuardLen / 4; i++) {
1017 if (pat[i] != kGuardPattern) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001018 JniAbortF(functionName, "guard pattern(2) disturbed at %p +%zd", fullBuf, offset + i*2);
Elliott Hughesa2501992011-08-26 19:39:54 -07001019 }
1020 }
1021
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001022 // If modification is not expected, verify checksum. Strictly speaking
1023 // this is wrong: if we told the client that we made a copy, there's no
1024 // reason they can't alter the buffer.
Elliott Hughesa2501992011-08-26 19:39:54 -07001025 if (!modOkay) {
1026 uLong adler = adler32(0L, Z_NULL, 0);
1027 adler = adler32(adler, (const Bytef*)dataBuf, len);
1028 if (pExtra->adler != adler) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001029 JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
1030 pExtra->adler, adler, dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001031 }
1032 }
1033 }
1034
1035 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001036 static uint8_t* DebugAlloc(size_t len) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001037 void* result = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
Elliott Hughesa2501992011-08-26 19:39:54 -07001038 if (result == MAP_FAILED) {
1039 PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1040 }
1041 return reinterpret_cast<uint8_t*>(result);
1042 }
1043
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001044 static void DebugFree(void* dataBuf, size_t len) {
1045 uint8_t* fullBuf = ActualBuffer(dataBuf);
1046 size_t totalByteCount = ActualLength(len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001047 // TODO: we could mprotect instead, and keep the allocation around for a while.
1048 // This would be even more expensive, but it might catch more errors.
1049 // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -07001050 // PLOG(WARNING) << "mprotect(PROT_NONE) failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001051 // }
1052 if (munmap(fullBuf, totalByteCount) != 0) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001053 PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
Elliott Hughesa2501992011-08-26 19:39:54 -07001054 }
1055 }
1056
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001057 static const uint8_t* ActualBuffer(const void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001058 return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1059 }
1060
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001061 static uint8_t* ActualBuffer(void* dataBuf) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001062 return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1063 }
1064
1065 // Underlying length of a user allocation of 'length' bytes.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001066 static size_t ActualLength(size_t length) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001067 return (length + kGuardLen + 1) & ~0x01;
1068 }
1069};
1070
1071/*
1072 * Create a guarded copy of a primitive array. Modifications to the copied
1073 * data are allowed. Returns a pointer to the copied data.
1074 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001075static void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
Elliott Hughesa2501992011-08-26 19:39:54 -07001077
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001078 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001079 size_t component_size = a->GetClass()->GetComponentSize();
1080 size_t byte_count = a->GetLength() * component_size;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001081 void* result = GuardedCopy::Create(a->GetRawData(component_size, 0), byte_count, true);
1082 if (isCopy != nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001083 *isCopy = JNI_TRUE;
1084 }
1085 return result;
1086}
1087
1088/*
1089 * Perform the array "release" operation, which may or may not copy data
Elliott Hughes81ff3182012-03-23 20:35:56 -07001090 * back into the managed heap, and may or may not release the underlying storage.
Elliott Hughesa2501992011-08-26 19:39:54 -07001091 */
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001092static void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001094 mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
Elliott Hughesa2501992011-08-26 19:39:54 -07001095
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001096 GuardedCopy::Check(__FUNCTION__, dataBuf, true);
Elliott Hughesa2501992011-08-26 19:39:54 -07001097
1098 if (mode != JNI_ABORT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001099 size_t len = GuardedCopy::FromData(dataBuf)->original_length;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001100 memcpy(a->GetRawData(a->GetClass()->GetComponentSize(), 0), dataBuf, len);
Elliott Hughesa2501992011-08-26 19:39:54 -07001101 }
1102 if (mode != JNI_COMMIT) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001103 GuardedCopy::Destroy(dataBuf);
Elliott Hughesa2501992011-08-26 19:39:54 -07001104 }
1105}
1106
1107/*
1108 * ===========================================================================
1109 * JNI functions
1110 * ===========================================================================
1111 */
1112
1113class CheckJNI {
1114 public:
1115 static jint GetVersion(JNIEnv* env) {
1116 CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1117 return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1118 }
1119
1120 static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1121 CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001122 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001123 return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1124 }
1125
1126 static jclass FindClass(JNIEnv* env, const char* name) {
1127 CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001128 sc.CheckClassName(name);
Elliott Hughesa2501992011-08-26 19:39:54 -07001129 return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1130 }
1131
Elliott Hughese84278b2012-03-22 10:06:53 -07001132 static jclass GetSuperclass(JNIEnv* env, jclass c) {
1133 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1134 return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001135 }
1136
Elliott Hughese84278b2012-03-22 10:06:53 -07001137 static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1138 CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1139 return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
Elliott Hughesa2501992011-08-26 19:39:54 -07001140 }
1141
1142 static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1143 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1144 // TODO: check that 'field' is a java.lang.reflect.Method.
1145 return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1146 }
1147
1148 static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1149 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1150 // TODO: check that 'field' is a java.lang.reflect.Field.
1151 return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1152 }
1153
1154 static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1155 CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1156 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1157 }
1158
1159 static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1160 CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1161 return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1162 }
1163
1164 static jint Throw(JNIEnv* env, jthrowable obj) {
1165 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1166 // TODO: check that 'obj' is a java.lang.Throwable.
1167 return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1168 }
1169
Elliott Hughese84278b2012-03-22 10:06:53 -07001170 static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1171 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1172 return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
Elliott Hughesa2501992011-08-26 19:39:54 -07001173 }
1174
1175 static jthrowable ExceptionOccurred(JNIEnv* env) {
1176 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1177 return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1178 }
1179
1180 static void ExceptionDescribe(JNIEnv* env) {
1181 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1182 baseEnv(env)->ExceptionDescribe(env);
1183 CHECK_JNI_EXIT_VOID();
1184 }
1185
1186 static void ExceptionClear(JNIEnv* env) {
1187 CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1188 baseEnv(env)->ExceptionClear(env);
1189 CHECK_JNI_EXIT_VOID();
1190 }
1191
1192 static void FatalError(JNIEnv* env, const char* msg) {
Elliott Hughesc4378df2013-06-14 17:05:13 -07001193 // The JNI specification doesn't say it's okay to call FatalError with a pending exception,
1194 // but you're about to abort anyway, and it's quite likely that you have a pending exception,
1195 // and it's not unimaginable that you don't know that you do. So we allow it.
1196 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_NullableUtf, "Eu", env, msg);
Elliott Hughesa2501992011-08-26 19:39:54 -07001197 baseEnv(env)->FatalError(env, msg);
1198 CHECK_JNI_EXIT_VOID();
1199 }
1200
1201 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1202 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1203 return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1204 }
1205
1206 static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1207 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1208 return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1209 }
1210
1211 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1212 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1213 return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1214 }
1215
1216 static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1217 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1218 return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1219 }
1220
1221 static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1222 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001223 if (globalRef != nullptr && GetIndirectRefKind(globalRef) != kGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001224 JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
1225 ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001226 } else {
1227 baseEnv(env)->DeleteGlobalRef(env, globalRef);
1228 CHECK_JNI_EXIT_VOID();
1229 }
1230 }
1231
1232 static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1233 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001234 if (weakGlobalRef != nullptr && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001235 JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
1236 ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001237 } else {
1238 baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1239 CHECK_JNI_EXIT_VOID();
1240 }
1241 }
1242
1243 static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1244 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001245 if (localRef != nullptr && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001246 JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
1247 ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
Elliott Hughesa2501992011-08-26 19:39:54 -07001248 } else {
1249 baseEnv(env)->DeleteLocalRef(env, localRef);
1250 CHECK_JNI_EXIT_VOID();
1251 }
1252 }
1253
1254 static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1255 CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1256 return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1257 }
1258
1259 static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1260 CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1261 return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1262 }
1263
Elliott Hughese84278b2012-03-22 10:06:53 -07001264 static jobject AllocObject(JNIEnv* env, jclass c) {
1265 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1266 return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001267 }
1268
Elliott Hughese84278b2012-03-22 10:06:53 -07001269 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1270 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
Elliott Hughesa2501992011-08-26 19:39:54 -07001271 va_list args;
1272 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -07001273 jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
Elliott Hughesa2501992011-08-26 19:39:54 -07001274 va_end(args);
1275 return CHECK_JNI_EXIT("L", result);
1276 }
1277
Elliott Hughese84278b2012-03-22 10:06:53 -07001278 static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1279 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1280 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001281 }
1282
Elliott Hughese84278b2012-03-22 10:06:53 -07001283 static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1284 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1285 return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
Elliott Hughesa2501992011-08-26 19:39:54 -07001286 }
1287
1288 static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1289 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1290 return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1291 }
1292
Elliott Hughese84278b2012-03-22 10:06:53 -07001293 static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1294 CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1295 return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001296 }
1297
Elliott Hughese84278b2012-03-22 10:06:53 -07001298 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1299 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1300 return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001301 }
1302
Elliott Hughese84278b2012-03-22 10:06:53 -07001303 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1304 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1305 return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001306 }
1307
Elliott Hughese84278b2012-03-22 10:06:53 -07001308 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1309 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1310 return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001311 }
1312
Elliott Hughese84278b2012-03-22 10:06:53 -07001313 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1314 CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1315 return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
Elliott Hughesa2501992011-08-26 19:39:54 -07001316 }
1317
Ian Rogersef7d42f2014-01-06 12:55:46 -08001318#define FIELD_ACCESSORS(_ctype, _jname, _jvalue_type, _type) \
Elliott Hughese84278b2012-03-22 10:06:53 -07001319 static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1320 CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1321 sc.CheckStaticFieldID(c, fid); \
1322 return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001323 } \
1324 static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1325 CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001326 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001327 return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1328 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001329 static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1330 CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1331 sc.CheckStaticFieldID(c, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001332 /* "value" arg only used when type == ref */ \
Ian Rogersef7d42f2014-01-06 12:55:46 -08001333 jvalue java_type_value; \
1334 java_type_value._jvalue_type = value; \
1335 sc.CheckFieldType(java_type_value, fid, _type[0], true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001336 baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001337 CHECK_JNI_EXIT_VOID(); \
1338 } \
1339 static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1340 CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001341 sc.CheckInstanceFieldID(obj, fid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001342 /* "value" arg only used when type == ref */ \
Ian Rogersef7d42f2014-01-06 12:55:46 -08001343 jvalue java_type_value; \
1344 java_type_value._jvalue_type = value; \
1345 sc.CheckFieldType(java_type_value, fid, _type[0], false); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001346 baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1347 CHECK_JNI_EXIT_VOID(); \
1348 }
1349
Ian Rogersef7d42f2014-01-06 12:55:46 -08001350FIELD_ACCESSORS(jobject, Object, l, "L");
1351FIELD_ACCESSORS(jboolean, Boolean, z, "Z");
1352FIELD_ACCESSORS(jbyte, Byte, b, "B");
1353FIELD_ACCESSORS(jchar, Char, c, "C");
1354FIELD_ACCESSORS(jshort, Short, s, "S");
1355FIELD_ACCESSORS(jint, Int, i, "I");
1356FIELD_ACCESSORS(jlong, Long, j, "J");
1357FIELD_ACCESSORS(jfloat, Float, f, "F");
1358FIELD_ACCESSORS(jdouble, Double, d, "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001359
1360#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1361 /* Virtual... */ \
1362 static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1363 jmethodID mid, ...) \
1364 { \
1365 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001366 sc.CheckSig(mid, _retsig, false); \
1367 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001368 _retdecl; \
1369 va_list args; \
1370 va_start(args, mid); \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001371 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001372 va_end(args); \
1373 _retok; \
1374 } \
1375 static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1376 jmethodID mid, va_list args) \
1377 { \
1378 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001379 sc.CheckSig(mid, _retsig, false); \
1380 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001381 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001382 _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001383 _retok; \
1384 } \
1385 static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1386 jmethodID mid, jvalue* args) \
1387 { \
1388 CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001389 sc.CheckSig(mid, _retsig, false); \
1390 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001391 _retdecl; \
Elliott Hughesba8eee12012-01-24 20:25:24 -08001392 _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001393 _retok; \
1394 } \
1395 /* Non-virtual... */ \
1396 static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001397 jobject obj, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001398 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001399 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001400 sc.CheckSig(mid, _retsig, false); \
1401 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001402 _retdecl; \
1403 va_list args; \
1404 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001405 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001406 va_end(args); \
1407 _retok; \
1408 } \
1409 static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001410 jobject obj, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001411 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001412 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001413 sc.CheckSig(mid, _retsig, false); \
1414 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001415 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001416 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001417 _retok; \
1418 } \
1419 static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
Elliott Hughese84278b2012-03-22 10:06:53 -07001420 jobject obj, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001421 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001422 CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001423 sc.CheckSig(mid, _retsig, false); \
1424 sc.CheckVirtualMethod(obj, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001425 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001426 _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001427 _retok; \
1428 } \
1429 /* Static... */ \
Elliott Hughese84278b2012-03-22 10:06:53 -07001430 static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001431 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001432 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001433 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001434 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001435 _retdecl; \
1436 va_list args; \
1437 va_start(args, mid); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001438 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001439 va_end(args); \
1440 _retok; \
1441 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001442 static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001443 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001444 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001445 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001446 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001447 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001448 _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001449 _retok; \
1450 } \
Elliott Hughese84278b2012-03-22 10:06:53 -07001451 static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
Elliott Hughesa2501992011-08-26 19:39:54 -07001452 { \
Elliott Hughese84278b2012-03-22 10:06:53 -07001453 CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001454 sc.CheckSig(mid, _retsig, true); \
Elliott Hughese84278b2012-03-22 10:06:53 -07001455 sc.CheckStaticMethod(c, mid); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001456 _retdecl; \
Elliott Hughese84278b2012-03-22 10:06:53 -07001457 _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001458 _retok; \
1459 }
1460
1461#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1462#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1463
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001464CALL(jobject, Object, mirror::Object* result, result = reinterpret_cast<mirror::Object*>, NON_VOID_RETURN("L", jobject), "L");
Elliott Hughesba8eee12012-01-24 20:25:24 -08001465CALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1466CALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1467CALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1468CALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1469CALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1470CALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1471CALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1472CALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
Elliott Hughesa2501992011-08-26 19:39:54 -07001473CALL(void, Void, , , VOID_RETURN, "V");
1474
1475 static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1476 CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1477 return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1478 }
1479
1480 static jsize GetStringLength(JNIEnv* env, jstring string) {
1481 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1482 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1483 }
1484
1485 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1486 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1487 const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001488 if (sc.ForceCopy() && result != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001489 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001490 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001491 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001492 if (isCopy != nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001493 *isCopy = JNI_TRUE;
1494 }
1495 }
1496 return CHECK_JNI_EXIT("p", result);
1497 }
1498
1499 static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1500 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001501 sc.CheckNonNull(chars);
1502 if (sc.ForceCopy()) {
1503 GuardedCopy::Check(__FUNCTION__, chars, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001504 chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001505 }
1506 baseEnv(env)->ReleaseStringChars(env, string, chars);
1507 CHECK_JNI_EXIT_VOID();
1508 }
1509
1510 static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001511 CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes); // TODO: show pointer and truncate string.
Elliott Hughesa2501992011-08-26 19:39:54 -07001512 return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1513 }
1514
1515 static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1516 CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1517 return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1518 }
1519
1520 static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1521 CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1522 const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001523 if (sc.ForceCopy() && result != nullptr) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001524 result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001525 if (isCopy != nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001526 *isCopy = JNI_TRUE;
1527 }
1528 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001529 return CHECK_JNI_EXIT("u", result); // TODO: show pointer and truncate string.
Elliott Hughesa2501992011-08-26 19:39:54 -07001530 }
1531
1532 static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001533 CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf); // TODO: show pointer and truncate string.
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001534 if (sc.ForceCopy()) {
1535 GuardedCopy::Check(__FUNCTION__, utf, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001536 utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001537 }
1538 baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1539 CHECK_JNI_EXIT_VOID();
1540 }
1541
1542 static jsize GetArrayLength(JNIEnv* env, jarray array) {
1543 CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1544 return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1545 }
1546
1547 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1548 CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1549 return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1550 }
1551
1552 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1553 CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1554 return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1555 }
1556
1557 static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1558 CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1559 baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1560 CHECK_JNI_EXIT_VOID();
1561 }
1562
1563#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1564 static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1565 CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1566 return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1567 }
1568NEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1569NEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1570NEW_PRIMITIVE_ARRAY(jcharArray, Char);
1571NEW_PRIMITIVE_ARRAY(jshortArray, Short);
1572NEW_PRIMITIVE_ARRAY(jintArray, Int);
1573NEW_PRIMITIVE_ARRAY(jlongArray, Long);
1574NEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1575NEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1576
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001577struct ForceCopyGetChecker {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001578 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07001579 ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001580 force_copy = sc.ForceCopy();
1581 no_copy = 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001582 if (force_copy && isCopy != nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001583 // Capture this before the base call tramples on it.
Elliott Hughesba8eee12012-01-24 20:25:24 -08001584 no_copy = *reinterpret_cast<uint32_t*>(isCopy);
Elliott Hughesa2501992011-08-26 19:39:54 -07001585 }
1586 }
1587
1588 template<typename ResultT>
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001589 ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001590 if (force_copy && result != nullptr) {
Mathieu Chartier77129ff2013-10-18 11:36:46 -07001591 result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
Elliott Hughesa2501992011-08-26 19:39:54 -07001592 }
1593 return result;
1594 }
1595
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001596 uint32_t no_copy;
1597 bool force_copy;
Elliott Hughesa2501992011-08-26 19:39:54 -07001598};
1599
1600#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1601 static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1602 CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001603 _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
Elliott Hughesa2501992011-08-26 19:39:54 -07001604 return CHECK_JNI_EXIT("p", result); \
1605 }
1606
1607#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1608 static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1609 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001610 sc.CheckNonNull(elems); \
1611 if (sc.ForceCopy()) { \
Elliott Hughesa2501992011-08-26 19:39:54 -07001612 ReleaseGuardedPACopy(env, array, elems, mode); \
1613 } \
1614 baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1615 CHECK_JNI_EXIT_VOID(); \
1616 }
1617
1618#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1619 static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1620 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1621 baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1622 CHECK_JNI_EXIT_VOID(); \
1623 }
1624
1625#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1626 static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1627 CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1628 baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1629 CHECK_JNI_EXIT_VOID(); \
1630 }
1631
1632#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1633 GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1634 RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1635 GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1636 SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1637
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001638// TODO: verify primitive array type matches call type.
Elliott Hughesa2501992011-08-26 19:39:54 -07001639PRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1640PRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1641PRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1642PRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1643PRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1644PRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1645PRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1646PRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1647
Elliott Hughese84278b2012-03-22 10:06:53 -07001648 static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1649 CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1650 return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
Elliott Hughesa2501992011-08-26 19:39:54 -07001651 }
1652
Elliott Hughese84278b2012-03-22 10:06:53 -07001653 static jint UnregisterNatives(JNIEnv* env, jclass c) {
1654 CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1655 return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
Elliott Hughesa2501992011-08-26 19:39:54 -07001656 }
1657
1658 static jint MonitorEnter(JNIEnv* env, jobject obj) {
1659 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001660 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001661 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
Elliott Hughesa92853e2012-02-07 16:09:27 -08001662 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001663 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1664 }
1665
1666 static jint MonitorExit(JNIEnv* env, jobject obj) {
1667 CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
Elliott Hughesa92853e2012-02-07 16:09:27 -08001668 if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001669 return JNI_ERR; // Only for jni_internal_test. Real code will have aborted already.
Elliott Hughesa92853e2012-02-07 16:09:27 -08001670 }
Elliott Hughesa2501992011-08-26 19:39:54 -07001671 return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1672 }
1673
1674 static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1675 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1676 return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1677 }
1678
1679 static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1680 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1681 baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1682 CHECK_JNI_EXIT_VOID();
1683 }
1684
1685 static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1686 CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1687 baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1688 CHECK_JNI_EXIT_VOID();
1689 }
1690
1691 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1692 CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1693 void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001694 if (sc.ForceCopy() && result != nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001695 result = CreateGuardedPACopy(env, array, isCopy);
1696 }
1697 return CHECK_JNI_EXIT("p", result);
1698 }
1699
1700 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1701 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001702 sc.CheckNonNull(carray);
1703 if (sc.ForceCopy()) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001704 ReleaseGuardedPACopy(env, array, carray, mode);
1705 }
1706 baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1707 CHECK_JNI_EXIT_VOID();
1708 }
1709
1710 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1711 CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1712 const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001713 if (sc.ForceCopy() && result != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001714 mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
Elliott Hughesa2501992011-08-26 19:39:54 -07001715 int byteCount = s->GetLength() * 2;
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001716 result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001717 if (isCopy != nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001718 *isCopy = JNI_TRUE;
1719 }
1720 }
1721 return CHECK_JNI_EXIT("p", result);
1722 }
1723
1724 static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1725 CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07001726 sc.CheckNonNull(carray);
1727 if (sc.ForceCopy()) {
1728 GuardedCopy::Check(__FUNCTION__, carray, false);
Elliott Hughesba8eee12012-01-24 20:25:24 -08001729 carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
Elliott Hughesa2501992011-08-26 19:39:54 -07001730 }
1731 baseEnv(env)->ReleaseStringCritical(env, string, carray);
1732 CHECK_JNI_EXIT_VOID();
1733 }
1734
1735 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1736 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1737 return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1738 }
1739
1740 static jboolean ExceptionCheck(JNIEnv* env) {
1741 CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1742 return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1743 }
1744
1745 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1746 // Note: we use "Ep" rather than "EL" because this is the one JNI function
1747 // that it's okay to pass an invalid reference to.
1748 CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1749 // TODO: proper decoding of jobjectRefType!
1750 return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1751 }
1752
1753 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1754 CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001755 if (address == nullptr) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -07001756 JniAbortF(__FUNCTION__, "non-nullable address is NULL");
Elliott Hughesa2501992011-08-26 19:39:54 -07001757 }
Narayan Kamathef809d02013-12-19 17:52:47 +00001758 if (capacity < 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001759 JniAbortF(__FUNCTION__, "capacity must be non-negative: %" PRId64, capacity);
Elliott Hughesa2501992011-08-26 19:39:54 -07001760 }
1761 return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1762 }
1763
1764 static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1765 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1766 // TODO: check that 'buf' is a java.nio.Buffer.
1767 return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1768 }
1769
1770 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1771 CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1772 // TODO: check that 'buf' is a java.nio.Buffer.
1773 return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1774 }
1775
1776 private:
1777 static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1778 return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1779 }
1780};
1781
1782const JNINativeInterface gCheckNativeInterface = {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001783 nullptr, // reserved0.
1784 nullptr, // reserved1.
1785 nullptr, // reserved2.
1786 nullptr, // reserved3.
Elliott Hughesa2501992011-08-26 19:39:54 -07001787 CheckJNI::GetVersion,
1788 CheckJNI::DefineClass,
1789 CheckJNI::FindClass,
1790 CheckJNI::FromReflectedMethod,
1791 CheckJNI::FromReflectedField,
1792 CheckJNI::ToReflectedMethod,
1793 CheckJNI::GetSuperclass,
1794 CheckJNI::IsAssignableFrom,
1795 CheckJNI::ToReflectedField,
1796 CheckJNI::Throw,
1797 CheckJNI::ThrowNew,
1798 CheckJNI::ExceptionOccurred,
1799 CheckJNI::ExceptionDescribe,
1800 CheckJNI::ExceptionClear,
1801 CheckJNI::FatalError,
1802 CheckJNI::PushLocalFrame,
1803 CheckJNI::PopLocalFrame,
1804 CheckJNI::NewGlobalRef,
1805 CheckJNI::DeleteGlobalRef,
1806 CheckJNI::DeleteLocalRef,
1807 CheckJNI::IsSameObject,
1808 CheckJNI::NewLocalRef,
1809 CheckJNI::EnsureLocalCapacity,
1810 CheckJNI::AllocObject,
1811 CheckJNI::NewObject,
1812 CheckJNI::NewObjectV,
1813 CheckJNI::NewObjectA,
1814 CheckJNI::GetObjectClass,
1815 CheckJNI::IsInstanceOf,
1816 CheckJNI::GetMethodID,
1817 CheckJNI::CallObjectMethod,
1818 CheckJNI::CallObjectMethodV,
1819 CheckJNI::CallObjectMethodA,
1820 CheckJNI::CallBooleanMethod,
1821 CheckJNI::CallBooleanMethodV,
1822 CheckJNI::CallBooleanMethodA,
1823 CheckJNI::CallByteMethod,
1824 CheckJNI::CallByteMethodV,
1825 CheckJNI::CallByteMethodA,
1826 CheckJNI::CallCharMethod,
1827 CheckJNI::CallCharMethodV,
1828 CheckJNI::CallCharMethodA,
1829 CheckJNI::CallShortMethod,
1830 CheckJNI::CallShortMethodV,
1831 CheckJNI::CallShortMethodA,
1832 CheckJNI::CallIntMethod,
1833 CheckJNI::CallIntMethodV,
1834 CheckJNI::CallIntMethodA,
1835 CheckJNI::CallLongMethod,
1836 CheckJNI::CallLongMethodV,
1837 CheckJNI::CallLongMethodA,
1838 CheckJNI::CallFloatMethod,
1839 CheckJNI::CallFloatMethodV,
1840 CheckJNI::CallFloatMethodA,
1841 CheckJNI::CallDoubleMethod,
1842 CheckJNI::CallDoubleMethodV,
1843 CheckJNI::CallDoubleMethodA,
1844 CheckJNI::CallVoidMethod,
1845 CheckJNI::CallVoidMethodV,
1846 CheckJNI::CallVoidMethodA,
1847 CheckJNI::CallNonvirtualObjectMethod,
1848 CheckJNI::CallNonvirtualObjectMethodV,
1849 CheckJNI::CallNonvirtualObjectMethodA,
1850 CheckJNI::CallNonvirtualBooleanMethod,
1851 CheckJNI::CallNonvirtualBooleanMethodV,
1852 CheckJNI::CallNonvirtualBooleanMethodA,
1853 CheckJNI::CallNonvirtualByteMethod,
1854 CheckJNI::CallNonvirtualByteMethodV,
1855 CheckJNI::CallNonvirtualByteMethodA,
1856 CheckJNI::CallNonvirtualCharMethod,
1857 CheckJNI::CallNonvirtualCharMethodV,
1858 CheckJNI::CallNonvirtualCharMethodA,
1859 CheckJNI::CallNonvirtualShortMethod,
1860 CheckJNI::CallNonvirtualShortMethodV,
1861 CheckJNI::CallNonvirtualShortMethodA,
1862 CheckJNI::CallNonvirtualIntMethod,
1863 CheckJNI::CallNonvirtualIntMethodV,
1864 CheckJNI::CallNonvirtualIntMethodA,
1865 CheckJNI::CallNonvirtualLongMethod,
1866 CheckJNI::CallNonvirtualLongMethodV,
1867 CheckJNI::CallNonvirtualLongMethodA,
1868 CheckJNI::CallNonvirtualFloatMethod,
1869 CheckJNI::CallNonvirtualFloatMethodV,
1870 CheckJNI::CallNonvirtualFloatMethodA,
1871 CheckJNI::CallNonvirtualDoubleMethod,
1872 CheckJNI::CallNonvirtualDoubleMethodV,
1873 CheckJNI::CallNonvirtualDoubleMethodA,
1874 CheckJNI::CallNonvirtualVoidMethod,
1875 CheckJNI::CallNonvirtualVoidMethodV,
1876 CheckJNI::CallNonvirtualVoidMethodA,
1877 CheckJNI::GetFieldID,
1878 CheckJNI::GetObjectField,
1879 CheckJNI::GetBooleanField,
1880 CheckJNI::GetByteField,
1881 CheckJNI::GetCharField,
1882 CheckJNI::GetShortField,
1883 CheckJNI::GetIntField,
1884 CheckJNI::GetLongField,
1885 CheckJNI::GetFloatField,
1886 CheckJNI::GetDoubleField,
1887 CheckJNI::SetObjectField,
1888 CheckJNI::SetBooleanField,
1889 CheckJNI::SetByteField,
1890 CheckJNI::SetCharField,
1891 CheckJNI::SetShortField,
1892 CheckJNI::SetIntField,
1893 CheckJNI::SetLongField,
1894 CheckJNI::SetFloatField,
1895 CheckJNI::SetDoubleField,
1896 CheckJNI::GetStaticMethodID,
1897 CheckJNI::CallStaticObjectMethod,
1898 CheckJNI::CallStaticObjectMethodV,
1899 CheckJNI::CallStaticObjectMethodA,
1900 CheckJNI::CallStaticBooleanMethod,
1901 CheckJNI::CallStaticBooleanMethodV,
1902 CheckJNI::CallStaticBooleanMethodA,
1903 CheckJNI::CallStaticByteMethod,
1904 CheckJNI::CallStaticByteMethodV,
1905 CheckJNI::CallStaticByteMethodA,
1906 CheckJNI::CallStaticCharMethod,
1907 CheckJNI::CallStaticCharMethodV,
1908 CheckJNI::CallStaticCharMethodA,
1909 CheckJNI::CallStaticShortMethod,
1910 CheckJNI::CallStaticShortMethodV,
1911 CheckJNI::CallStaticShortMethodA,
1912 CheckJNI::CallStaticIntMethod,
1913 CheckJNI::CallStaticIntMethodV,
1914 CheckJNI::CallStaticIntMethodA,
1915 CheckJNI::CallStaticLongMethod,
1916 CheckJNI::CallStaticLongMethodV,
1917 CheckJNI::CallStaticLongMethodA,
1918 CheckJNI::CallStaticFloatMethod,
1919 CheckJNI::CallStaticFloatMethodV,
1920 CheckJNI::CallStaticFloatMethodA,
1921 CheckJNI::CallStaticDoubleMethod,
1922 CheckJNI::CallStaticDoubleMethodV,
1923 CheckJNI::CallStaticDoubleMethodA,
1924 CheckJNI::CallStaticVoidMethod,
1925 CheckJNI::CallStaticVoidMethodV,
1926 CheckJNI::CallStaticVoidMethodA,
1927 CheckJNI::GetStaticFieldID,
1928 CheckJNI::GetStaticObjectField,
1929 CheckJNI::GetStaticBooleanField,
1930 CheckJNI::GetStaticByteField,
1931 CheckJNI::GetStaticCharField,
1932 CheckJNI::GetStaticShortField,
1933 CheckJNI::GetStaticIntField,
1934 CheckJNI::GetStaticLongField,
1935 CheckJNI::GetStaticFloatField,
1936 CheckJNI::GetStaticDoubleField,
1937 CheckJNI::SetStaticObjectField,
1938 CheckJNI::SetStaticBooleanField,
1939 CheckJNI::SetStaticByteField,
1940 CheckJNI::SetStaticCharField,
1941 CheckJNI::SetStaticShortField,
1942 CheckJNI::SetStaticIntField,
1943 CheckJNI::SetStaticLongField,
1944 CheckJNI::SetStaticFloatField,
1945 CheckJNI::SetStaticDoubleField,
1946 CheckJNI::NewString,
1947 CheckJNI::GetStringLength,
1948 CheckJNI::GetStringChars,
1949 CheckJNI::ReleaseStringChars,
1950 CheckJNI::NewStringUTF,
1951 CheckJNI::GetStringUTFLength,
1952 CheckJNI::GetStringUTFChars,
1953 CheckJNI::ReleaseStringUTFChars,
1954 CheckJNI::GetArrayLength,
1955 CheckJNI::NewObjectArray,
1956 CheckJNI::GetObjectArrayElement,
1957 CheckJNI::SetObjectArrayElement,
1958 CheckJNI::NewBooleanArray,
1959 CheckJNI::NewByteArray,
1960 CheckJNI::NewCharArray,
1961 CheckJNI::NewShortArray,
1962 CheckJNI::NewIntArray,
1963 CheckJNI::NewLongArray,
1964 CheckJNI::NewFloatArray,
1965 CheckJNI::NewDoubleArray,
1966 CheckJNI::GetBooleanArrayElements,
1967 CheckJNI::GetByteArrayElements,
1968 CheckJNI::GetCharArrayElements,
1969 CheckJNI::GetShortArrayElements,
1970 CheckJNI::GetIntArrayElements,
1971 CheckJNI::GetLongArrayElements,
1972 CheckJNI::GetFloatArrayElements,
1973 CheckJNI::GetDoubleArrayElements,
1974 CheckJNI::ReleaseBooleanArrayElements,
1975 CheckJNI::ReleaseByteArrayElements,
1976 CheckJNI::ReleaseCharArrayElements,
1977 CheckJNI::ReleaseShortArrayElements,
1978 CheckJNI::ReleaseIntArrayElements,
1979 CheckJNI::ReleaseLongArrayElements,
1980 CheckJNI::ReleaseFloatArrayElements,
1981 CheckJNI::ReleaseDoubleArrayElements,
1982 CheckJNI::GetBooleanArrayRegion,
1983 CheckJNI::GetByteArrayRegion,
1984 CheckJNI::GetCharArrayRegion,
1985 CheckJNI::GetShortArrayRegion,
1986 CheckJNI::GetIntArrayRegion,
1987 CheckJNI::GetLongArrayRegion,
1988 CheckJNI::GetFloatArrayRegion,
1989 CheckJNI::GetDoubleArrayRegion,
1990 CheckJNI::SetBooleanArrayRegion,
1991 CheckJNI::SetByteArrayRegion,
1992 CheckJNI::SetCharArrayRegion,
1993 CheckJNI::SetShortArrayRegion,
1994 CheckJNI::SetIntArrayRegion,
1995 CheckJNI::SetLongArrayRegion,
1996 CheckJNI::SetFloatArrayRegion,
1997 CheckJNI::SetDoubleArrayRegion,
1998 CheckJNI::RegisterNatives,
1999 CheckJNI::UnregisterNatives,
2000 CheckJNI::MonitorEnter,
2001 CheckJNI::MonitorExit,
2002 CheckJNI::GetJavaVM,
2003 CheckJNI::GetStringRegion,
2004 CheckJNI::GetStringUTFRegion,
2005 CheckJNI::GetPrimitiveArrayCritical,
2006 CheckJNI::ReleasePrimitiveArrayCritical,
2007 CheckJNI::GetStringCritical,
2008 CheckJNI::ReleaseStringCritical,
2009 CheckJNI::NewWeakGlobalRef,
2010 CheckJNI::DeleteWeakGlobalRef,
2011 CheckJNI::ExceptionCheck,
2012 CheckJNI::NewDirectByteBuffer,
2013 CheckJNI::GetDirectBufferAddress,
2014 CheckJNI::GetDirectBufferCapacity,
2015 CheckJNI::GetObjectRefType,
2016};
2017
2018const JNINativeInterface* GetCheckJniNativeInterface() {
2019 return &gCheckNativeInterface;
2020}
2021
2022class CheckJII {
Elliott Hughesba8eee12012-01-24 20:25:24 -08002023 public:
Elliott Hughesa2501992011-08-26 19:39:54 -07002024 static jint DestroyJavaVM(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002025 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002026 sc.Check(true, "v", vm);
2027 return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002028 }
2029
2030 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002031 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002032 sc.Check(true, "vpp", vm, p_env, thr_args);
2033 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002034 }
2035
2036 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002037 ScopedCheck sc(vm, false, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002038 sc.Check(true, "vpp", vm, p_env, thr_args);
2039 return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
Elliott Hughesa2501992011-08-26 19:39:54 -07002040 }
2041
2042 static jint DetachCurrentThread(JavaVM* vm) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002043 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002044 sc.Check(true, "v", vm);
2045 return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
Elliott Hughesa2501992011-08-26 19:39:54 -07002046 }
2047
2048 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughesa0957642011-09-02 14:27:33 -07002049 ScopedCheck sc(vm, true, __FUNCTION__);
Elliott Hughes83a25322013-03-14 11:18:53 -07002050 sc.Check(true, "vpI", vm);
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002051 return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
Elliott Hughesa2501992011-08-26 19:39:54 -07002052 }
2053
2054 private:
Elliott Hughes32ae6e32011-09-27 10:46:50 -07002055 static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
Elliott Hughesa2501992011-08-26 19:39:54 -07002056 return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2057 }
2058};
2059
2060const JNIInvokeInterface gCheckInvokeInterface = {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002061 nullptr, // reserved0
2062 nullptr, // reserved1
2063 nullptr, // reserved2
Elliott Hughesa2501992011-08-26 19:39:54 -07002064 CheckJII::DestroyJavaVM,
2065 CheckJII::AttachCurrentThread,
2066 CheckJII::DetachCurrentThread,
2067 CheckJII::GetEnv,
2068 CheckJII::AttachCurrentThreadAsDaemon
2069};
2070
2071const JNIInvokeInterface* GetCheckJniInvokeInterface() {
2072 return &gCheckInvokeInterface;
2073}
2074
2075} // namespace art