check_jni.cc revision 2d10b206f9d0b97396b7dadb9a6415cd39efd341
1a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
2a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * Copyright (C) 2008 The Android Open Source Project
3a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *
4a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * you may not use this file except in compliance with the License.
6a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * You may obtain a copy of the License at
7a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *
8a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *
10a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * Unless required by applicable law or agreed to in writing, software
11a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * See the License for the specific language governing permissions and
14a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * limitations under the License.
15a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
16a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
17a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include "jni_internal.h"
18a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
19a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include <sys/mman.h>
20a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include <zlib.h>
21a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2207ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h"
23a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include "class_linker.h"
242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
254f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
261d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space.h"
27ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
28ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
32b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers#include "mirror/string-inl.h"
332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
346d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
3558df32711162823647356afe25ae2cde04133563Jeff Hao#include "runtime.h"
3600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
37a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#include "thread.h"
38a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
39a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesnamespace art {
40a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
413f6635a2c84d645002f651c312d53f8348646c1fElliott Hughesstatic void JniAbort(const char* jni_function_name, const char* msg) {
42a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  Thread* self = Thread::Current();
4300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(self);
44ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* current_method = self->GetCurrentMethod(nullptr);
45a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
463b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes  std::ostringstream os;
473f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
48a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
49ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  if (jni_function_name != nullptr) {
503f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    os << "\n    in call to " << jni_function_name;
51a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
52a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // TODO: is this useful given that we're about to dump the calling thread's stack?
53ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  if (current_method != nullptr) {
543f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    os << "\n    from " << PrettyMethod(current_method);
55a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  }
56a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  os << "\n";
57a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  self->Dump(os);
58a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
59a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  JavaVMExt* vm = Runtime::Current()->GetJavaVM();
60ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  if (vm->check_jni_abort_hook != nullptr) {
61b264f081424642b06b0bbe7102781895d41faa9dElliott Hughes    vm->check_jni_abort_hook(vm->check_jni_abort_hook_data, os.str());
62a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  } else {
639da7f59c9059397182b9a97e898a42ec06d4d646Ian Rogers    // Ensure that we get a native stack trace for this thread.
649da7f59c9059397182b9a97e898a42ec06d4d646Ian Rogers    self->TransitionFromRunnableToSuspended(kNative);
65a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    LOG(FATAL) << os.str();
669da7f59c9059397182b9a97e898a42ec06d4d646Ian Rogers    self->TransitionFromSuspendedToRunnable();  // Unreachable, keep annotalysis happy.
67a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
68a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}
69a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
703f6635a2c84d645002f651c312d53f8348646c1fElliott Hughesstatic void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
713f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  std::string msg;
723f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  StringAppendV(&msg, fmt, ap);
733f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  JniAbort(jni_function_name, msg.c_str());
743f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes}
753f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes
763f6635a2c84d645002f651c312d53f8348646c1fElliott Hughesvoid JniAbortF(const char* jni_function_name, const char* fmt, ...) {
773f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  va_list args;
783f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  va_start(args, fmt);
793f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  JniAbortV(jni_function_name, fmt, args);
803f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  va_end(args);
813f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes}
823f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes
83a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
84a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
85a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *      JNI function helpers
86a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
87a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
88a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
89959f8ed11ef9493a89b90a1f9a82a811dbc7ae0eIan Rogersstatic bool IsSirtLocalRef(JNIEnv* env, jobject localRef) {
90959f8ed11ef9493a89b90a1f9a82a811dbc7ae0eIan Rogers  return GetIndirectRefKind(localRef) == kSirtOrInvalid &&
910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      reinterpret_cast<JNIEnvExt*>(env)->self->SirtContains(localRef);
92959f8ed11ef9493a89b90a1f9a82a811dbc7ae0eIan Rogers}
93959f8ed11ef9493a89b90a1f9a82a811dbc7ae0eIan Rogers
943f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes// Flags passed into ScopedCheck.
95a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define kFlag_Default       0x0000
96a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
973f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_CritBad       0x0000      // Calling while in critical is not allowed.
983f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_CritOkay      0x0001      // Calling while in critical is allowed.
993f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_CritGet       0x0002      // This is a critical "get".
1003f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_CritRelease   0x0003      // This is a critical "release".
1013f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_CritMask      0x0003      // Bit mask to get "crit" value.
102a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1033f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_ExcepBad      0x0000      // Raised exceptions are not allowed.
1043f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_ExcepOkay     0x0004      // Raised exceptions are allowed.
105a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1063f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_Release       0x0010      // Are we in a non-critical release function?
1073f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_NullableUtf   0x0020      // Are our UTF parameters nullable?
108a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1093f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes#define kFlag_Invocation    0x8000      // Part of the invocation interface (JavaVM*).
110a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
111485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes#define kFlag_ForceTrace    0x80000000  // Add this to a JNI function's flags if you want to trace every call.
112485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes
113a09576416788b916095739e43a16917e7948f3a4Elliott Hughesstatic const char* gBuiltInPrefixes[] = {
114a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Landroid/",
115a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Lcom/android/",
116a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Lcom/google/android/",
117a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Ldalvik/",
118a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Ljava/",
119a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Ljavax/",
120a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Llibcore/",
121a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  "Lorg/apache/harmony/",
122ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr
123a09576416788b916095739e43a16917e7948f3a4Elliott Hughes};
124a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
125ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic bool ShouldTrace(JavaVMExt* vm, mirror::ArtMethod* method)
126b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
128a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // when a native method that matches the -Xjnitrace argument calls a JNI function
129a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // such as NewByteArray.
130a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // If -verbose:third-party-jni is on, we want to log any JNI function calls
131a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  // made by a third-party native method.
13281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  std::string class_name(MethodHelper(method).GetDeclaringClassDescriptor());
13381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  if (!vm->trace.empty() && class_name.find(vm->trace) != std::string::npos) {
134a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    return true;
135a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  }
1364dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  if (VLOG_IS_ON(third_party_jni)) {
137a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
138a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    // like part of Android.
139ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t i = 0; gBuiltInPrefixes[i] != nullptr; ++i) {
14081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes      if (StartsWith(class_name, gBuiltInPrefixes[i])) {
141a09576416788b916095739e43a16917e7948f3a4Elliott Hughes        return false;
142a09576416788b916095739e43a16917e7948f3a4Elliott Hughes      }
143a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    }
144a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    return true;
145a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  }
146a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  return false;
147a09576416788b916095739e43a16917e7948f3a4Elliott Hughes}
148a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
149a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesclass ScopedCheck {
150ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes public:
151a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  // For JNIEnv* functions.
15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  explicit ScopedCheck(JNIEnv* env, int flags, const char* functionName)
153b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
15400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      : soa_(env) {
155365c10235438607541fa2259a5fec48061b90bd8Ian Rogers    Init(flags, functionName, true);
15632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    CheckThread(flags);
157a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
158a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
159a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  // For JavaVM* functions.
16000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: it's not correct that this is a lock function, but making it so aids annotalysis.
16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  explicit ScopedCheck(JavaVM* vm, bool has_method, const char* functionName)
162b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      : soa_(vm) {
164365c10235438607541fa2259a5fec48061b90bd8Ian Rogers    Init(kFlag_Invocation, functionName, has_method);
165a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
166a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
167b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  ~ScopedCheck() UNLOCK_FUNCTION(Locks::mutator_lock_) {}
16800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
16900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const ScopedObjectAccess& soa() {
17000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return soa_;
17100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
17200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
17332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  bool ForceCopy() {
174a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return Runtime::Current()->GetJavaVM()->force_copy;
175a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
176a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
17781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // Checks that 'class_name' is a valid "fully-qualified" JNI class name, like "java/lang/Thread"
17881ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // or "[Ljava/lang/Object;". A ClassLoader can actually normalize class names a couple of
17981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // times, so using "java.lang.Thread" instead of "java/lang/Thread" might work in some
18081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // circumstances, but this is incorrect.
18181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  void CheckClassName(const char* class_name) {
1822d10b206f9d0b97396b7dadb9a6415cd39efd341Ian Rogers    if ((class_name == nullptr) || !IsValidJniClassName(class_name)) {
1833f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_,
1843f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                "illegal class name '%s'\n"
1853f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                "    (should be of the form 'package/Class', [Lpackage/Class;' or '[[B')",
1863f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                class_name);
187a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
188a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
189a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
190a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
191a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that the field is of the appropriate type.  If the field has an
192a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * object type, "java_object" is the object we're trying to assign into it.
193a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
194a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Works for both static and instance fields.
195a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
196ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void CheckFieldType(jvalue value, jfieldID fid, char prim, bool isStatic)
197b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
198ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = CheckFieldID(fid);
199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (f == nullptr) {
200a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
201a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
2022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* field_type = FieldHelper(f).GetType();
2030cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    if (!field_type->IsPrimitive()) {
204ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      jobject java_object = value.l;
205ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (java_object != nullptr) {
2062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
2073f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        // If java_object is a weak global ref whose referent has been cleared,
2083f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        // obj will be NULL.  Otherwise, obj should always be non-NULL
2093f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        // and valid.
210590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj)) {
211128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier          Runtime::Current()->GetHeap()->DumpSpaces();
2123f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes          JniAbortF(function_name_, "field operation on invalid %s: %p",
2133f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                    ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
214a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          return;
2150cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers        } else {
216161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom          if (!obj->InstanceOf(field_type)) {
2173f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes            JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %s",
2183f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                      PrettyField(f).c_str(), PrettyTypeOf(obj).c_str());
2190cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers            return;
2200cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers          }
2210cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers        }
222a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
2230cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    } else if (field_type != Runtime::Current()->GetClassLinker()->FindPrimitiveClass(prim)) {
2243f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "attempt to set field %s with value of wrong type: %c",
2253f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                PrettyField(f).c_str(), prim);
226a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
2270cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers    }
2280cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers
2293f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    if (isStatic != f->IsStatic()) {
230a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (isStatic) {
2313f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "accessing non-static field %s as static", PrettyField(f).c_str());
232a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      } else {
2333f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "accessing static field %s as non-static", PrettyField(f).c_str());
234a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
235a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
236a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
237a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
238a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
239a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
240a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that this instance field ID is valid for this object.
241a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
242a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Assumes "jobj" has already been validated.
243a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
24400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckInstanceFieldID(jobject java_object, jfieldID fid)
245b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
247ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (o == nullptr || !Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
248128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
2493f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "field operation on invalid %s: %p",
2503f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
251a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
252a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
253a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
254ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = CheckFieldID(fid);
255ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (f == nullptr) {
25632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      return;
25732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
2582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
2596d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
260ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (c->FindInstanceField(fh.GetName(), fh.GetTypeDescriptor()) == nullptr) {
2613f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jfieldID %s not valid for an object of class %s",
2623f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                PrettyField(f).c_str(), PrettyTypeOf(o).c_str());
263a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
264a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
265a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
266a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
267a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that the pointer value is non-NULL.
268a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
26932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  void CheckNonNull(const void* ptr) {
270ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (ptr == nullptr) {
2713f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "non-nullable argument was NULL");
272a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
273a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
274a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
275a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
276a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that the method's return type matches the type of call.
277a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * 'expectedType' will be "L" for all objects, including arrays.
278a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
27900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckSig(jmethodID mid, const char* expectedType, bool isStatic)
280b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
281ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = CheckMethodID(mid);
282ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (m == nullptr) {
28332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      return;
28432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
2856d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    if (*expectedType != MethodHelper(m).GetShorty()[0]) {
2863f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "the return type of %s does not match %s",
2873f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                function_name_, PrettyMethod(m).c_str());
2883f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    }
2893f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    if (isStatic != m->IsStatic()) {
290a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (isStatic) {
2913f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "calling non-static method %s with %s",
2923f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                  PrettyMethod(m).c_str(), function_name_);
293a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      } else {
2943f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "calling static method %s with %s",
2953f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                  PrettyMethod(m).c_str(), function_name_);
296a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
297a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
298a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
299a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
300a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
301a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that this static field ID is valid for this class.
302a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
303a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Assumes "java_class" has already been validated.
304a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
30500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckStaticFieldID(jclass java_class, jfieldID fid)
306b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
308ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::ArtField* f = CheckFieldID(fid);
309ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (f == nullptr) {
31032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      return;
31132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
312a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (f->GetDeclaringClass() != c) {
3133f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "static jfieldID %p not valid for class %s",
3143f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                fid, PrettyClass(c).c_str());
315a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
316a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
317a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
318a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
319e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes   * Verify that "mid" is appropriate for "java_class".
320a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
321a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * A mismatch isn't dangerous, because the jmethodID defines the class.  In
322e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes   * fact, java_class is unused in the implementation.  It's best if we don't
323a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * allow bad code in the system though.
324a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
325e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes   * Instances of "java_class" must be instances of the method's declaring class.
326a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
32700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckStaticMethod(jclass java_class, jmethodID mid)
328b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
329ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::ArtMethod* m = CheckMethodID(mid);
330ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (m == nullptr) {
33132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      return;
33232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
3332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = soa_.Decode<mirror::Class*>(java_class);
33467fe2b41a6afccf6ab1a58879eae3e0e8f3d6c7aBrian Carlstrom    if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
3353f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "can't call static %s on class %s",
3363f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                PrettyMethod(m).c_str(), PrettyClass(c).c_str());
337a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
338a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
339a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
340a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
341a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that "mid" is appropriate for "jobj".
342a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
343a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Make sure the object is an instance of the method's declaring class.
344a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * (Note the mid might point to a declaration in an interface; this
345a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * will be handled automatically by the instanceof check.)
346a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckVirtualMethod(jobject java_object, jmethodID mid)
348b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
349ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::ArtMethod* m = CheckMethodID(mid);
350ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (m == nullptr) {
35132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      return;
35232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
3532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
354a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (!o->InstanceOf(m->GetDeclaringClass())) {
3553f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "can't call %s on instance of %s",
3563f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                PrettyMethod(m).c_str(), PrettyTypeOf(o).c_str());
357a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
358a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
359a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
360a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /**
361a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * The format string is a sequence of the following characters,
362a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * and must be followed by arguments of the corresponding types
363a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * in the same order.
364a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
365a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Java primitive types:
366a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * B - jbyte
367a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * C - jchar
368a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * D - jdouble
369a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * F - jfloat
370a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * I - jint
371a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * J - jlong
372a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * S - jshort
373a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Z - jboolean (shown as true and false)
374a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * V - void
375a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
376a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Java reference types:
377a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * L - jobject
378a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * a - jarray
379a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * c - jclass
380a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * s - jstring
381a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
382a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * JNI types:
383a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * b - jboolean (shown as JNI_TRUE and JNI_FALSE)
384a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * f - jfieldID
385a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * m - jmethodID
386a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * p - void*
387a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * r - jint (for release mode arguments)
38878090d119d5e23ff35ecddac1f4a98e4c050bb07Elliott Hughes   * u - const char* (Modified UTF-8)
389a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * z - jsize (for lengths; use i if negative values are okay)
390a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * v - JavaVM*
391a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * E - JNIEnv*
392a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * . - no argument; just print "..." (used for varargs JNI calls)
393a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
394a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Use the kFlag_NullableUtf flag where 'u' field(s) are nullable.
395a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
396df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  void Check(bool entry, const char* fmt0, ...) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
397a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    va_list ap;
398a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
399ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::ArtMethod* traceMethod = nullptr;
4009365f5890eb27e1c98038ecf5a7fa2cad0aa62e6Ian Rogers    if (has_method_ && (!soa_.Vm()->trace.empty() || VLOG_IS_ON(third_party_jni))) {
401a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      // We need to guard some of the invocation interface's calls: a bad caller might
402a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      // use DetachCurrentThread or GetEnv on a thread that's not yet attached.
403a09576416788b916095739e43a16917e7948f3a4Elliott Hughes      Thread* self = Thread::Current();
404ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if ((flags_ & kFlag_Invocation) == 0 || self != nullptr) {
405ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        traceMethod = self->GetCurrentMethod(nullptr);
406a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
407a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
408a09576416788b916095739e43a16917e7948f3a4Elliott Hughes
409ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (((flags_ & kFlag_ForceTrace) != 0) ||
410ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        (traceMethod != nullptr && ShouldTrace(soa_.Vm(), traceMethod))) {
411a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      va_start(ap, fmt0);
412a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      std::string msg;
413a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      for (const char* fmt = fmt0; *fmt;) {
414a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        char ch = *fmt++;
4157934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        if (ch == 'B') {  // jbyte
416a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jbyte b = va_arg(ap, int);
417a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          if (b >= 0 && b < 10) {
418a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "%d", b);
419a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
420a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "%#x (%d)", b, b);
421a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4227934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'C') {  // jchar
423a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jchar c = va_arg(ap, int);
424a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          if (c < 0x7f && c >= ' ') {
425a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "U+%x ('%c')", c, c);
426a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
427a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "U+%x", c);
428a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4297934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'F' || ch == 'D') {  // jfloat, jdouble
430a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "%g", va_arg(ap, double));
4317934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'I' || ch == 'S') {  // jint, jshort
432a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "%d", va_arg(ap, int));
4337934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'J') {  // jlong
434ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          StringAppendF(&msg, "%" PRId64, va_arg(ap, jlong));
4357934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'Z') {  // jboolean
436a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "%s", va_arg(ap, int) ? "true" : "false");
4377934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'V') {  // void
438a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          msg += "void";
4397934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'v') {  // JavaVM*
440a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          JavaVM* vm = va_arg(ap, JavaVM*);
441a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "(JavaVM*)%p", vm);
4427934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'E') {  // JNIEnv*
443a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          JNIEnv* env = va_arg(ap, JNIEnv*);
444a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "(JNIEnv*)%p", env);
4457934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'L' || ch == 'a' || ch == 's') {  // jobject, jarray, jstring
446a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          // For logging purposes, these are identical.
447a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jobject o = va_arg(ap, jobject);
448ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          if (o == nullptr) {
449a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "NULL";
450a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
451a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "%p", o);
452a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4537934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'b') {  // jboolean (JNI-style)
454a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jboolean b = va_arg(ap, int);
455a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          msg += (b ? "JNI_TRUE" : "JNI_FALSE");
4567934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'c') {  // jclass
457a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jclass jc = va_arg(ap, jclass);
4582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Class* c = reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(jc));
459ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          if (c == nullptr) {
460a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "NULL";
461590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          } else if (c == kInvalidIndirectRefObject ||
462590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier              !Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
463485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes            StringAppendF(&msg, "INVALID POINTER:%p", jc);
464485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes          } else if (!c->IsClass()) {
465485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes            msg += "INVALID NON-CLASS OBJECT OF TYPE:" + PrettyTypeOf(c);
466a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
46754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes            msg += PrettyClass(c);
468a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            if (!entry) {
469a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes              StringAppendF(&msg, " (%p)", jc);
470a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            }
471a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4727934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'f') {  // jfieldID
473a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jfieldID fid = va_arg(ap, jfieldID);
474ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom          mirror::ArtField* f = reinterpret_cast<mirror::ArtField*>(fid);
475a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          msg += PrettyField(f);
476a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          if (!entry) {
477a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, " (%p)", fid);
478a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4797934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'z') {  // non-negative jsize
480a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          // You might expect jsize to be size_t, but it's not; it's the same as jint.
481a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          // We only treat this specially so we can do the non-negative check.
482a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          // TODO: maybe this wasn't worth it?
483a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jint i = va_arg(ap, jint);
484a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, "%d", i);
4857934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'm') {  // jmethodID
486a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jmethodID mid = va_arg(ap, jmethodID);
487ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom          mirror::ArtMethod* m = reinterpret_cast<mirror::ArtMethod*>(mid);
488a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          msg += PrettyMethod(m);
489a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          if (!entry) {
490a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, " (%p)", mid);
491a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4927934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'p') {  // void* ("pointer")
493a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          void* p = va_arg(ap, void*);
494ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          if (p == nullptr) {
495a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "NULL";
496a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
497a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "(void*) %p", p);
498a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
4997934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'r') {  // jint (release mode)
500a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          jint releaseMode = va_arg(ap, jint);
501a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          if (releaseMode == 0) {
502a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "0";
503a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else if (releaseMode == JNI_ABORT) {
504a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "JNI_ABORT";
505a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else if (releaseMode == JNI_COMMIT) {
506a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "JNI_COMMIT";
507a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
508a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "invalid release mode %d", releaseMode);
509a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
5107934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        } else if (ch == 'u') {  // const char* (Modified UTF-8)
511a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          const char* utf = va_arg(ap, const char*);
512ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          if (utf == nullptr) {
513a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            msg += "NULL";
514a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
515a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes            StringAppendF(&msg, "\"%s\"", utf);
516a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
517a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == '.') {
518a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          msg += "...";
519a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else {
5203f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes          JniAbortF(function_name_, "unknown trace format specifier: %c", ch);
521a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          return;
522a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
523a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        if (*fmt) {
524a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          StringAppendF(&msg, ", ");
525a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
526a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
527a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      va_end(ap);
528a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
529485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes      if ((flags_ & kFlag_ForceTrace) != 0) {
530485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes        LOG(INFO) << "JNI: call to " << function_name_ << "(" << msg << ")";
531485cac4fa028559f2a81802f7c9d02218ca93359Elliott Hughes      } else if (entry) {
53232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        if (has_method_) {
533a09576416788b916095739e43a16917e7948f3a4Elliott Hughes          std::string methodName(PrettyMethod(traceMethod, false));
53432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          LOG(INFO) << "JNI: " << methodName << " -> " << function_name_ << "(" << msg << ")";
53532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          indent_ = methodName.size() + 1;
536a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else {
53732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          LOG(INFO) << "JNI: -> " << function_name_ << "(" << msg << ")";
53832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          indent_ = 0;
539a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
540a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      } else {
54132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        LOG(INFO) << StringPrintf("JNI: %*s<- %s returned %s", indent_, "", function_name_, msg.c_str());
542a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
543a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
544a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
545a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // We always do the thorough checks on entry, and never on exit...
546a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (entry) {
547a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      va_start(ap, fmt0);
548a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      for (const char* fmt = fmt0; *fmt; ++fmt) {
549a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        char ch = *fmt;
550a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        if (ch == 'a') {
55132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckArray(va_arg(ap, jarray));
552a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'c') {
55332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckInstance(kClass, va_arg(ap, jclass));
554a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'L') {
55532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckObject(va_arg(ap, jobject));
556a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'r') {
55732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckReleaseMode(va_arg(ap, jint));
558a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 's') {
55932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckInstance(kString, va_arg(ap, jstring));
560a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'u') {
56132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          if ((flags_ & kFlag_Release) != 0) {
56232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes            CheckNonNull(va_arg(ap, const char*));
563a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          } else {
56432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes            bool nullable = ((flags_ & kFlag_NullableUtf) != 0);
56532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes            CheckUtfString(va_arg(ap, const char*), nullable);
566a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          }
567a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'z') {
56832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes          CheckLengthPositive(va_arg(ap, jsize));
569ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else if (strchr("BCISZbfmpEv", ch) != nullptr) {
5707934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom          va_arg(ap, uint32_t);  // Skip this argument.
571a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'D' || ch == 'F') {
5727934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom          va_arg(ap, double);  // Skip this argument.
573a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == 'J') {
5747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom          va_arg(ap, uint64_t);  // Skip this argument.
575a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else if (ch == '.') {
576a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        } else {
5773d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes          LOG(FATAL) << "Unknown check format specifier: " << ch;
578a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
579a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
580a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      va_end(ap);
581a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
582a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
583a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
584a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes  enum InstanceKind {
585a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    kClass,
5860f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes    kDirectByteBuffer,
5870f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes    kObject,
5880f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes    kString,
5890f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes    kThrowable,
590a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes  };
591a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
592a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes  /*
593a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   * Verify that "jobj" is a valid non-NULL object reference, and points to
594a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   * an instance of expectedClass.
595a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   *
596a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   * Because we're looking at an object on the GC heap, we have to switch
597a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   * to "running" mode before doing the checks.
598a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes   */
59900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool CheckInstance(InstanceKind kind, jobject java_object)
600b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
601ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const char* what = nullptr;
602a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    switch (kind) {
603a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kClass:
604a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      what = "jclass";
605a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
606a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kDirectByteBuffer:
607a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      what = "direct ByteBuffer";
608a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
609a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kObject:
610a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      what = "jobject";
611a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
612a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kString:
613a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      what = "jstring";
614a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
615a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kThrowable:
616a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      what = "jthrowable";
617a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
618a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    default:
6197b9d996e4cd7d154bb1a244d67139aff0c363cf2Elliott Hughes      LOG(FATAL) << "Unknown kind " << static_cast<int>(kind);
620a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
621a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
622ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (java_object == nullptr) {
6233f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "%s received null %s", function_name_, what);
624a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      return false;
625a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
626a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
6272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* obj = soa_.Decode<mirror::Object*>(java_object);
628590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(obj)) {
629128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
6303f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "%s is an invalid %s: %p (%p)",
6313f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                what, ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object, obj);
632a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      return false;
633a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
634a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
635a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    bool okay = true;
636a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    switch (kind) {
637a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kClass:
638a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      okay = obj->IsClass();
639a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
640a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kDirectByteBuffer:
641a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      UNIMPLEMENTED(FATAL);
642a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
643a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kString:
644a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      okay = obj->GetClass()->IsStringClass();
645a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
646a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kThrowable:
647a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      okay = obj->GetClass()->IsThrowableClass();
648a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
649a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    case kObject:
650a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      break;
651a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
652a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    if (!okay) {
6533f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "%s has wrong type: %s", what, PrettyTypeOf(obj).c_str());
654a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes      return false;
655a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
656a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
657a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    return true;
658a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes  }
659a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes
660ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes private:
66181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // Set "has_method" to true if we have a valid thread with a method pointer.
66281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We won't have one before attaching a thread, after detaching a thread, or
66381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // when shutting down the runtime.
664365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  void Init(int flags, const char* functionName, bool has_method) {
66532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    flags_ = flags;
66632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    function_name_ = functionName;
66781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    has_method_ = has_method;
668a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
669a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
670a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
671a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that "array" is non-NULL and points to an Array object.
672a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
673a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Since we're dealing with objects, switch to "running" mode.
674a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
675b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void CheckArray(jarray java_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
676ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (java_array == nullptr) {
6773f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jarray was NULL");
678a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
679a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
680a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
6812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Array* a = soa_.Decode<mirror::Array*>(java_array);
682590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(a)) {
683128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
6843f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jarray is an invalid %s: %p (%p)",
6853f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(java_array)).c_str(), java_array, a);
686a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } else if (!a->IsArrayInstance()) {
6873f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jarray argument has non-array type: %s", PrettyTypeOf(a).c_str());
688a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
689a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
690a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
69132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  void CheckLengthPositive(jsize length) {
692a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (length < 0) {
6933f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "negative jsize: %d", length);
694a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
695a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
696a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
697ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* CheckFieldID(jfieldID fid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
698ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (fid == nullptr) {
6993f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jfieldID was NULL");
700ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return nullptr;
70132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
702ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = soa_.DecodeField(fid);
703590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(f) || !f->IsArtField()) {
704128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
7053f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "invalid jfieldID: %p", fid);
706ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return nullptr;
70732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
70832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return f;
70932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  }
71032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes
711ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* CheckMethodID(jmethodID mid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
712ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (mid == nullptr) {
7133f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "jmethodID was NULL");
714ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return nullptr;
71532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
716ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = soa_.DecodeMethod(mid);
717590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(m) || !m->IsArtMethod()) {
718128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
7193f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "invalid jmethodID: %p", mid);
720ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return nullptr;
72132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    }
72232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return m;
72332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  }
72432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes
725a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
726a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that "jobj" is a valid object, and that it's an object that JNI
727a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * is allowed to know about.  We allow NULL references.
728a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
729a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Switches to "running" mode before performing checks.
730a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
73100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void CheckObject(jobject java_object)
732b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
733ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (java_object == nullptr) {
734a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
735a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
736a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
7372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* o = soa_.Decode<mirror::Object*>(java_object);
738590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
739128c52c3f97e6726a77cf2f704100915cf6bb9d3Mathieu Chartier      Runtime::Current()->GetHeap()->DumpSpaces();
740c5bfa8f49d8548d7c685a99b411311ef56bedffaElliott Hughes      // TODO: when we remove work_around_app_jni_bugs, this should be impossible.
7413f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "native code passing in reference to invalid %s: %p",
7423f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(java_object)).c_str(), java_object);
743a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
744a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
745a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
746a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
747a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify that the "mode" argument passed to a primitive array Release
748a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * function is one of the valid values.
749a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
75032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  void CheckReleaseMode(jint mode) {
751a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (mode != 0 && mode != JNI_COMMIT && mode != JNI_ABORT) {
75296a9887f0bc912661e0a7478c7eb19847d2e2e06Elliott Hughes      JniAbortF(function_name_, "unknown value for release mode: %d", mode);
753a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
754a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
755a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
756b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void CheckThread(int flags) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
757a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    Thread* self = Thread::Current();
758ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (self == nullptr) {
7593f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_, "a thread (tid %d) is making JNI calls without being attached", GetTid());
760a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
761a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
762a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
763a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // Get the *correct* JNIEnv by going through our TLS pointer.
764a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    JNIEnvExt* threadEnv = self->GetJniEnv();
765a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
7663f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Verify that the current thread is (a) attached and (b) associated with
7673f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // this particular instance of JNIEnv.
76800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (soa_.Env() != threadEnv) {
769987560fee798e48fb725c44b796d8ca7a5872ad6Ian Rogers      JniAbortF(function_name_, "thread %s using JNIEnv* from thread %s",
770987560fee798e48fb725c44b796d8ca7a5872ad6Ian Rogers                ToStr<Thread>(*self).c_str(), ToStr<Thread>(*soa_.Self()).c_str());
771987560fee798e48fb725c44b796d8ca7a5872ad6Ian Rogers      return;
772a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
773a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
7743f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Verify that, if this thread previously made a critical "get" call, we
7753f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // do the corresponding "release" call before we try anything else.
776a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    switch (flags & kFlag_CritMask) {
777a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    case kFlag_CritOkay:    // okay to call this method
778a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      break;
779a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    case kFlag_CritBad:     // not okay to call
780a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (threadEnv->critical) {
7813f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "thread %s using JNI after critical get", ToStr<Thread>(*self).c_str());
782a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return;
783a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
784a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      break;
785a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    case kFlag_CritGet:     // this is a "get" call
7863f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      // Don't check here; we allow nested gets.
787a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      threadEnv->critical++;
788a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      break;
7897934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    case kFlag_CritRelease:  // this is a "release" call
790a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      threadEnv->critical--;
791a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (threadEnv->critical < 0) {
7923f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "thread %s called too many critical releases", ToStr<Thread>(*self).c_str());
793a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return;
794a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
795a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      break;
796a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    default:
7973d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(FATAL) << "Bad flags (internal error): " << flags;
798a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
799a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
8003f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Verify that, if an exception has been raised, the native code doesn't
8013f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // make any JNI calls other than the Exception* methods.
802a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if ((flags & kFlag_ExcepOkay) == 0 && self->IsExceptionPending()) {
80362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location;
80462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Throwable* exception = self->GetException(&throw_location);
80562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      std::string type(PrettyTypeOf(exception));
8068e4d3ed463df1a9f5fdc1e927a6afe6d208558e1Elliott Hughes      JniAbortF(function_name_, "JNI %s called with pending exception '%s' thrown in %s",
8078e4d3ed463df1a9f5fdc1e927a6afe6d208558e1Elliott Hughes                function_name_, type.c_str(), throw_location.Dump().c_str());
808a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
809a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
810a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
811a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
8123f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes  // Verifies that "bytes" points to valid Modified UTF-8 data.
81332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  void CheckUtfString(const char* bytes, bool nullable) {
814ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (bytes == nullptr) {
815a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (!nullable) {
8163f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(function_name_, "non-nullable const char* was NULL");
817a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return;
818a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
819a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
820a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
821a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
822ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const char* errorKind = nullptr;
82332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    uint8_t utf8 = CheckUtfBytes(bytes, &errorKind);
824ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (errorKind != nullptr) {
8253f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(function_name_,
8263f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                "input is not valid Modified UTF-8: illegal %s byte %#x\n"
8273f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                "    string: '%s'", errorKind, utf8, bytes);
828a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      return;
829a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
830a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
831a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
83232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static uint8_t CheckUtfBytes(const char* bytes, const char** errorKind) {
833a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    while (*bytes != '\0') {
834a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      uint8_t utf8 = *(bytes++);
835a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      // Switch on the high four bits.
836a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      switch (utf8 >> 4) {
837a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x00:
838a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x01:
839a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x02:
840a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x03:
841a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x04:
842a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x05:
843a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x06:
844a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x07:
845a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        // Bit pattern 0xxx. No need for any extra bytes.
846a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        break;
847a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x08:
848a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x09:
849a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0a:
850a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0b:
851a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0f:
852a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        /*
853a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes         * Bit pattern 10xx or 1111, which are illegal start bytes.
854a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes         * Note: 1111 is valid for normal UTF-8, but not the
85578090d119d5e23ff35ecddac1f4a98e4c050bb07Elliott Hughes         * Modified UTF-8 used here.
856a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes         */
857a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        *errorKind = "start";
858a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return utf8;
859a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0e:
860a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        // Bit pattern 1110, so there are two additional bytes.
861a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        utf8 = *(bytes++);
862a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        if ((utf8 & 0xc0) != 0x80) {
863a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          *errorKind = "continuation";
864a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          return utf8;
865a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
866a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        // Fall through to take care of the final byte.
867a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0c:
868a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      case 0x0d:
869a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        // Bit pattern 110x, so there is one additional byte.
870a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        utf8 = *(bytes++);
871a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        if ((utf8 & 0xc0) != 0x80) {
872a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          *errorKind = "continuation";
873a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes          return utf8;
874a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        }
875a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        break;
876a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
877a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
878a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return 0;
879a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
880a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
88100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const ScopedObjectAccess soa_;
88232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  const char* function_name_;
88332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  int flags_;
88432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  bool has_method_;
88592cb498ffd2185fc8f8da7cd25fe485d0e7e3355Elliott Hughes  int indent_;
886a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
887a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  DISALLOW_COPY_AND_ASSIGN(ScopedCheck);
888a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
889a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
890a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define CHECK_JNI_ENTRY(flags, types, args...) \
891a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  ScopedCheck sc(env, flags, __FUNCTION__); \
89232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  sc.Check(true, types, ##args)
893a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
894a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define CHECK_JNI_EXIT(type, exp) ({ \
8959b3c3cdb62f7142384e6bf2c0cb6e3a76b16f0e3Mathieu Chartier    auto _rc = (exp); \
89632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  sc.Check(false, type, _rc); \
897a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  _rc; })
898a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define CHECK_JNI_EXIT_VOID() \
89932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  sc.Check(false, "V")
900a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
901a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
902a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
903a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *      Guarded arrays
904a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
905a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
906a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
907a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define kGuardLen       512         /* must be multiple of 2 */
908a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define kGuardPattern   0xd5e3      /* uncommon values; d5e3d5e3 invalid addr */
909a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define kGuardMagic     0xffd5aa96
910a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
911a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/* this gets tucked in at the start of the buffer; struct size must be even */
912a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesstruct GuardedCopy {
913a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  uint32_t magic;
914a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  uLong adler;
91532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  size_t original_length;
91632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  const void* original_ptr;
917a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
918a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /* find the GuardedCopy given the pointer into the "live" data */
91932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static inline const GuardedCopy* FromData(const void* dataBuf) {
92032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return reinterpret_cast<const GuardedCopy*>(ActualBuffer(dataBuf));
921a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
922a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
923a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
924a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Create an over-sized buffer to hold the contents of "buf".  Copy it in,
925a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * filling in the area around it with guard data.
926a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
927a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * We use a 16-bit pattern to make a rogue memset less likely to elude us.
928a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
92932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static void* Create(const void* buf, size_t len, bool modOkay) {
93032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    size_t newLen = ActualLength(len);
93132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    uint8_t* newBuf = DebugAlloc(newLen);
932a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
9333f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Fill it in with a pattern.
934ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint16_t* pat = reinterpret_cast<uint16_t*>(newBuf);
935a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    for (size_t i = 0; i < newLen / 2; i++) {
936a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      *pat++ = kGuardPattern;
937a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
938a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
9393f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Copy the data in; note "len" could be zero.
940a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    memcpy(newBuf + kGuardLen / 2, buf, len);
941a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
9423f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // If modification is not expected, grab a checksum.
943a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    uLong adler = 0;
944a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (!modOkay) {
945a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      adler = adler32(0L, Z_NULL, 0);
946ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      adler = adler32(adler, reinterpret_cast<const Bytef*>(buf), len);
947ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      *reinterpret_cast<uLong*>(newBuf) = adler;
948a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
949a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
950a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    GuardedCopy* pExtra = reinterpret_cast<GuardedCopy*>(newBuf);
951a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    pExtra->magic = kGuardMagic;
952a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    pExtra->adler = adler;
95332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    pExtra->original_ptr = buf;
95432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    pExtra->original_length = len;
955a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
956a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return newBuf + kGuardLen / 2;
957a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
958a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
959a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
960a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Free up the guard buffer, scrub it, and return the original pointer.
961a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
96232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static void* Destroy(void* dataBuf) {
96332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
964ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    void* original_ptr = const_cast<void*>(pExtra->original_ptr);
96532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    size_t len = pExtra->original_length;
96632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    DebugFree(dataBuf, len);
96732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return original_ptr;
968a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
969a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
970a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  /*
971a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * Verify the guard area and, if "modOkay" is false, that the data itself
972a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * has not been altered.
973a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   *
974a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   * The caller has already checked that "dataBuf" is non-NULL.
975a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes   */
97632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static void Check(const char* functionName, const void* dataBuf, bool modOkay) {
977a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static const uint32_t kMagicCmp = kGuardMagic;
97832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    const uint8_t* fullBuf = ActualBuffer(dataBuf);
97932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    const GuardedCopy* pExtra = GuardedCopy::FromData(dataBuf);
980a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
9813f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Before we do anything with "pExtra", check the magic number.  We
9823f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // do the check with memcmp rather than "==" in case the pointer is
9833f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // unaligned.  If it points to completely bogus memory we're going
9843f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // to crash, but there's no easy way around that.
985a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (memcmp(&pExtra->magic, &kMagicCmp, 4) != 0) {
986a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      uint8_t buf[4];
987a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      memcpy(buf, &pExtra->magic, 4);
9883f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(functionName,
9893f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes          "guard magic does not match (found 0x%02x%02x%02x%02x) -- incorrect data pointer %p?",
9907934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom          buf[3], buf[2], buf[1], buf[0], dataBuf);  // Assumes little-endian.
991a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
992a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
99332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    size_t len = pExtra->original_length;
994a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
9953f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Check bottom half of guard; skip over optional checksum storage.
996ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    const uint16_t* pat = reinterpret_cast<const uint16_t*>(fullBuf);
997a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    for (size_t i = sizeof(GuardedCopy) / 2; i < (kGuardLen / 2 - sizeof(GuardedCopy)) / 2; i++) {
998a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (pat[i] != kGuardPattern) {
999ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        JniAbortF(functionName, "guard pattern(1) disturbed at %p +%zd", fullBuf, i*2);
1000a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1001a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1002a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1003a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    int offset = kGuardLen / 2 + len;
1004a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (offset & 0x01) {
10053f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      // Odd byte; expected value depends on endian.
1006a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      const uint16_t patSample = kGuardPattern;
10073f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      uint8_t expected_byte = reinterpret_cast<const uint8_t*>(&patSample)[1];
10083f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      if (fullBuf[offset] != expected_byte) {
10093f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(functionName, "guard pattern disturbed in odd byte after %p +%d 0x%02x 0x%02x",
10103f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                  fullBuf, offset, fullBuf[offset], expected_byte);
1011a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1012a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      offset++;
1013a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1014a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
10153f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // Check top half of guard.
1016ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    pat = reinterpret_cast<const uint16_t*>(fullBuf + offset);
1017a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    for (size_t i = 0; i < kGuardLen / 4; i++) {
1018a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (pat[i] != kGuardPattern) {
1019ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        JniAbortF(functionName, "guard pattern(2) disturbed at %p +%zd", fullBuf, offset + i*2);
1020a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1021a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1022a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
10233f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // If modification is not expected, verify checksum.  Strictly speaking
10243f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // this is wrong: if we told the client that we made a copy, there's no
10253f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes    // reason they can't alter the buffer.
1026a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (!modOkay) {
1027a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      uLong adler = adler32(0L, Z_NULL, 0);
1028a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      adler = adler32(adler, (const Bytef*)dataBuf, len);
1029a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      if (pExtra->adler != adler) {
10303f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes        JniAbortF(functionName, "buffer modified (0x%08lx vs 0x%08lx) at address %p",
10313f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                  pExtra->adler, adler, dataBuf);
1032a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1033a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1034a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1035a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1036a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes private:
103732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static uint8_t* DebugAlloc(size_t len) {
1038ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    void* result = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
1039a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (result == MAP_FAILED) {
1040a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      PLOG(FATAL) << "GuardedCopy::create mmap(" << len << ") failed";
1041a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1042a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return reinterpret_cast<uint8_t*>(result);
1043a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1044a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
104532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static void DebugFree(void* dataBuf, size_t len) {
104632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    uint8_t* fullBuf = ActualBuffer(dataBuf);
104732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    size_t totalByteCount = ActualLength(len);
1048a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: we could mprotect instead, and keep the allocation around for a while.
1049a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // This would be even more expensive, but it might catch more errors.
1050a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // if (mprotect(fullBuf, totalByteCount, PROT_NONE) != 0) {
10517b9d996e4cd7d154bb1a244d67139aff0c363cf2Elliott Hughes    //     PLOG(WARNING) << "mprotect(PROT_NONE) failed";
1052a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // }
1053a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    if (munmap(fullBuf, totalByteCount) != 0) {
1054ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      PLOG(FATAL) << "munmap(" << reinterpret_cast<void*>(fullBuf) << ", " << totalByteCount << ") failed";
1055a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1056a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1057a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
105832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static const uint8_t* ActualBuffer(const void* dataBuf) {
1059a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return reinterpret_cast<const uint8_t*>(dataBuf) - kGuardLen / 2;
1060a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1061a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
106232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static uint8_t* ActualBuffer(void* dataBuf) {
1063a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return reinterpret_cast<uint8_t*>(dataBuf) - kGuardLen / 2;
1064a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1065a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1066a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  // Underlying length of a user allocation of 'length' bytes.
106732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static size_t ActualLength(size_t length) {
1068a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return (length + kGuardLen + 1) & ~0x01;
1069a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1070a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
1071a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1072a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
1073a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * Create a guarded copy of a primitive array.  Modifications to the copied
1074a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * data are allowed.  Returns a pointer to the copied data.
1075a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
10763f6635a2c84d645002f651c312d53f8348646c1fElliott Hughesstatic void* CreateGuardedPACopy(JNIEnv* env, const jarray java_array, jboolean* isCopy) {
107700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
1078a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
10792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
1080a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers  size_t component_size = a->GetClass()->GetComponentSize();
1081a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers  size_t byte_count = a->GetLength() * component_size;
1082ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void* result = GuardedCopy::Create(a->GetRawData(component_size, 0), byte_count, true);
1083ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  if (isCopy != nullptr) {
1084a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    *isCopy = JNI_TRUE;
1085a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1086a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  return result;
1087a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}
1088a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1089a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
1090a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * Perform the array "release" operation, which may or may not copy data
109181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes * back into the managed heap, and may or may not release the underlying storage.
1092a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
10933f6635a2c84d645002f651c312d53f8348646c1fElliott Hughesstatic void ReleaseGuardedPACopy(JNIEnv* env, jarray java_array, void* dataBuf, int mode) {
109400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(env);
10952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = soa.Decode<mirror::Array*>(java_array);
1096a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
109732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  GuardedCopy::Check(__FUNCTION__, dataBuf, true);
1098a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1099a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  if (mode != JNI_ABORT) {
110032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    size_t len = GuardedCopy::FromData(dataBuf)->original_length;
1101ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    memcpy(a->GetRawData(a->GetClass()->GetComponentSize(), 0), dataBuf, len);
1102a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1103a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  if (mode != JNI_COMMIT) {
110432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    GuardedCopy::Destroy(dataBuf);
1105a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1106a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}
1107a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1108a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes/*
1109a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
1110a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes *      JNI functions
1111a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes * ===========================================================================
1112a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes */
1113a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1114a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesclass CheckJNI {
1115a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes public:
1116a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint GetVersion(JNIEnv* env) {
1117a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "E", env);
1118a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetVersion(env));
1119a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1120a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1121a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jclass DefineClass(JNIEnv* env, const char* name, jobject loader, const jbyte* buf, jsize bufLen) {
1122a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EuLpz", env, name, loader, buf, bufLen);
112332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckClassName(name);
1124a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("c", baseEnv(env)->DefineClass(env, name, loader, buf, bufLen));
1125a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1126a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1127a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jclass FindClass(JNIEnv* env, const char* name) {
1128a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Eu", env, name);
112932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckClassName(name);
1130a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("c", baseEnv(env)->FindClass(env, name));
1131a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1132a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1133e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jclass GetSuperclass(JNIEnv* env, jclass c) {
1134e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1135e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("c", baseEnv(env)->GetSuperclass(env, c));
1136a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1137a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1138e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jboolean IsAssignableFrom(JNIEnv* env, jclass c1, jclass c2) {
1139e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecc", env, c1, c2);
1140e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("b", baseEnv(env)->IsAssignableFrom(env, c1, c2));
1141a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1142a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1143a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jmethodID FromReflectedMethod(JNIEnv* env, jobject method) {
1144a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, method);
1145a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: check that 'field' is a java.lang.reflect.Method.
1146a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("m", baseEnv(env)->FromReflectedMethod(env, method));
1147a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1148a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1149a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jfieldID FromReflectedField(JNIEnv* env, jobject field) {
1150a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, field);
1151a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: check that 'field' is a java.lang.reflect.Field.
1152a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("f", baseEnv(env)->FromReflectedField(env, field));
1153a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1154a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1155a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject ToReflectedMethod(JNIEnv* env, jclass cls, jmethodID mid, jboolean isStatic) {
1156a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecmb", env, cls, mid, isStatic);
1157a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedMethod(env, cls, mid, isStatic));
1158a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1159a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1160a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject ToReflectedField(JNIEnv* env, jclass cls, jfieldID fid, jboolean isStatic) {
1161a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecfb", env, cls, fid, isStatic);
1162a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->ToReflectedField(env, cls, fid, isStatic));
1163a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1164a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1165a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint Throw(JNIEnv* env, jthrowable obj) {
1166a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1167a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: check that 'obj' is a java.lang.Throwable.
1168a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->Throw(env, obj));
1169a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1170a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1171e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jint ThrowNew(JNIEnv* env, jclass c, const char* message) {
1172e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_NullableUtf, "Ecu", env, c, message);
1173e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->ThrowNew(env, c, message));
1174a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1175a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1176a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jthrowable ExceptionOccurred(JNIEnv* env) {
1177a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1178a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->ExceptionOccurred(env));
1179a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1180a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1181a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ExceptionDescribe(JNIEnv* env) {
1182a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1183a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ExceptionDescribe(env);
1184a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1185a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1186a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1187a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ExceptionClear(JNIEnv* env) {
1188a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_ExcepOkay, "E", env);
1189a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ExceptionClear(env);
1190a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1191a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1192a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1193a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void FatalError(JNIEnv* env, const char* msg) {
1194c4378df76edd637273813fef4da7f01c4e637defElliott Hughes    // The JNI specification doesn't say it's okay to call FatalError with a pending exception,
1195c4378df76edd637273813fef4da7f01c4e637defElliott Hughes    // but you're about to abort anyway, and it's quite likely that you have a pending exception,
1196c4378df76edd637273813fef4da7f01c4e637defElliott Hughes    // and it's not unimaginable that you don't know that you do. So we allow it.
1197c4378df76edd637273813fef4da7f01c4e637defElliott Hughes    CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_NullableUtf, "Eu", env, msg);
1198a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->FatalError(env, msg);
1199a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1200a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1201a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1202a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint PushLocalFrame(JNIEnv* env, jint capacity) {
1203a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EI", env, capacity);
1204a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->PushLocalFrame(env, capacity));
1205a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1206a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1207a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject PopLocalFrame(JNIEnv* env, jobject res) {
1208a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, res);
1209a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->PopLocalFrame(env, res));
1210a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1211a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1212a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
1213a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1214a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewGlobalRef(env, obj));
1215a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1216a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1217a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject NewLocalRef(JNIEnv* env, jobject ref) {
1218a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, ref);
1219a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewLocalRef(env, ref));
1220a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1221a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1222a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void DeleteGlobalRef(JNIEnv* env, jobject globalRef) {
1223a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, globalRef);
1224ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (globalRef != nullptr && GetIndirectRefKind(globalRef) != kGlobal) {
12253f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(__FUNCTION__, "DeleteGlobalRef on %s: %p",
12263f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(globalRef)).c_str(), globalRef);
1227a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } else {
1228a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      baseEnv(env)->DeleteGlobalRef(env, globalRef);
1229a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      CHECK_JNI_EXIT_VOID();
1230a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1231a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1232a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1233a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void DeleteWeakGlobalRef(JNIEnv* env, jweak weakGlobalRef) {
1234a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, weakGlobalRef);
1235ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (weakGlobalRef != nullptr && GetIndirectRefKind(weakGlobalRef) != kWeakGlobal) {
12363f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(__FUNCTION__, "DeleteWeakGlobalRef on %s: %p",
12373f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(weakGlobalRef)).c_str(), weakGlobalRef);
1238a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } else {
1239a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      baseEnv(env)->DeleteWeakGlobalRef(env, weakGlobalRef);
1240a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      CHECK_JNI_EXIT_VOID();
1241a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1242a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1243a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1244a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void DeleteLocalRef(JNIEnv* env, jobject localRef) {
1245a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, localRef);
1246ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (localRef != nullptr && GetIndirectRefKind(localRef) != kLocal && !IsSirtLocalRef(env, localRef)) {
12473f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(__FUNCTION__, "DeleteLocalRef on %s: %p",
12483f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes                ToStr<IndirectRefKind>(GetIndirectRefKind(localRef)).c_str(), localRef);
1249a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } else {
1250a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      baseEnv(env)->DeleteLocalRef(env, localRef);
1251a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      CHECK_JNI_EXIT_VOID();
1252a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1253a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1254a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1255a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint EnsureLocalCapacity(JNIEnv *env, jint capacity) {
1256a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EI", env, capacity);
1257a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->EnsureLocalCapacity(env, capacity));
1258a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1259a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1260a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jboolean IsSameObject(JNIEnv* env, jobject ref1, jobject ref2) {
1261a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "ELL", env, ref1, ref2);
1262a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("b", baseEnv(env)->IsSameObject(env, ref1, ref2));
1263a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1264a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1265e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jobject AllocObject(JNIEnv* env, jclass c) {
1266e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1267e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->AllocObject(env, c));
1268a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1269a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1270e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
1271e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1272a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    va_list args;
1273a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    va_start(args, mid);
1274e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    jobject result = baseEnv(env)->NewObjectV(env, c, mid, args);
1275a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    va_end(args);
1276a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", result);
1277a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1278a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1279e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jobject NewObjectV(JNIEnv* env, jclass c, jmethodID mid, va_list args) {
1280e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1281e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectV(env, c, mid, args));
1282a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1283a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1284e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jobject NewObjectA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) {
1285e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid);
1286e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewObjectA(env, c, mid, args));
1287a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1288a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1289a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jclass GetObjectClass(JNIEnv* env, jobject obj) {
1290a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1291a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("c", baseEnv(env)->GetObjectClass(env, obj));
1292a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1293a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1294e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jboolean IsInstanceOf(JNIEnv* env, jobject obj, jclass c) {
1295e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "ELc", env, obj, c);
1296e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("b", baseEnv(env)->IsInstanceOf(env, obj, c));
1297a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1298a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1299e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1300e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1301e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("m", baseEnv(env)->GetMethodID(env, c, name, sig));
1302a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1303a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1304e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1305e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1306e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("f", baseEnv(env)->GetFieldID(env, c, name, sig));
1307a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1308a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1309e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1310e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1311e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("m", baseEnv(env)->GetStaticMethodID(env, c, name, sig));
1312a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1313a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1314e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
1315e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ecuu", env, c, name, sig);
1316e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("f", baseEnv(env)->GetStaticFieldID(env, c, name, sig));
1317a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1318a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1319ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#define FIELD_ACCESSORS(_ctype, _jname, _jvalue_type, _type) \
1320e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    static _ctype GetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid) { \
1321e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ecf", env, c, fid); \
1322e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        sc.CheckStaticFieldID(c, fid); \
1323e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        return CHECK_JNI_EXIT(_type, baseEnv(env)->GetStatic##_jname##Field(env, c, fid)); \
1324a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1325a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype Get##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid) { \
1326a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELf", env, obj, fid); \
132732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckInstanceFieldID(obj, fid); \
1328a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return CHECK_JNI_EXIT(_type, baseEnv(env)->Get##_jname##Field(env, obj, fid)); \
1329a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1330e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    static void SetStatic##_jname##Field(JNIEnv* env, jclass c, jfieldID fid, _ctype value) { \
1331e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ecf" _type, env, c, fid, value); \
1332e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        sc.CheckStaticFieldID(c, fid); \
1333a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        /* "value" arg only used when type == ref */ \
1334ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        jvalue java_type_value; \
1335ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        java_type_value._jvalue_type = value; \
1336ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        sc.CheckFieldType(java_type_value, fid, _type[0], true); \
1337e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        baseEnv(env)->SetStatic##_jname##Field(env, c, fid, value); \
1338a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_EXIT_VOID(); \
1339a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1340a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static void Set##_jname##Field(JNIEnv* env, jobject obj, jfieldID fid, _ctype value) { \
1341a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELf" _type, env, obj, fid, value); \
134232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckInstanceFieldID(obj, fid); \
1343a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        /* "value" arg only used when type == ref */ \
1344ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        jvalue java_type_value; \
1345ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        java_type_value._jvalue_type = value; \
1346ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        sc.CheckFieldType(java_type_value, fid, _type[0], false); \
1347a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        baseEnv(env)->Set##_jname##Field(env, obj, fid, value); \
1348a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_EXIT_VOID(); \
1349a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1350a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1351ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jobject, Object, l, "L");
1352ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jboolean, Boolean, z, "Z");
1353ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jbyte, Byte, b, "B");
1354ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jchar, Char, c, "C");
1355ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jshort, Short, s, "S");
1356ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jint, Int, i, "I");
1357ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jlong, Long, j, "J");
1358ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jfloat, Float, f, "F");
1359ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersFIELD_ACCESSORS(jdouble, Double, d, "D");
1360a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1361a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define CALL(_ctype, _jname, _retdecl, _retasgn, _retok, _retsig) \
1362a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    /* Virtual... */ \
1363a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype Call##_jname##Method(JNIEnv* env, jobject obj, \
1364a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        jmethodID mid, ...) \
1365a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1366a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
136732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
136832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1369a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1370a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_list args; \
1371a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_start(args, mid); \
1372ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes        _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
1373a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_end(args); \
1374a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1375a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1376a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype Call##_jname##MethodV(JNIEnv* env, jobject obj, \
1377a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        jmethodID mid, va_list args) \
1378a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1379a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
138032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
138132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1382a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1383ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes        _retasgn(baseEnv(env)->Call##_jname##MethodV(env, obj, mid, args)); \
1384a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1385a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1386a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype Call##_jname##MethodA(JNIEnv* env, jobject obj, \
1387a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        jmethodID mid, jvalue* args) \
1388a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1389a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELm.", env, obj, mid); /* TODO: args! */ \
139032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
139132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1392a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1393ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes        _retasgn(baseEnv(env)->Call##_jname##MethodA(env, obj, mid, args)); \
1394a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1395a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1396a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    /* Non-virtual... */ \
1397a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype CallNonvirtual##_jname##Method(JNIEnv* env, \
1398e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        jobject obj, jclass c, jmethodID mid, ...) \
1399a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1400e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
140132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
140232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1403a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1404a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_list args; \
1405a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_start(args, mid); \
1406e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
1407a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_end(args); \
1408a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1409a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1410a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype CallNonvirtual##_jname##MethodV(JNIEnv* env, \
1411e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        jobject obj, jclass c, jmethodID mid, va_list args) \
1412a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1413e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
141432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
141532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1416a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1417e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodV(env, obj, c, mid, args)); \
1418a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1419a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1420a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _ctype CallNonvirtual##_jname##MethodA(JNIEnv* env, \
1421e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        jobject obj, jclass c, jmethodID mid, jvalue* args) \
1422a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1423e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "ELcm.", env, obj, c, mid); /* TODO: args! */ \
142432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, false); \
142532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckVirtualMethod(obj, mid); \
1426a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1427e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        _retasgn(baseEnv(env)->CallNonvirtual##_jname##MethodA(env, obj, c, mid, args)); \
1428a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1429a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1430a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    /* Static... */ \
1431e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    static _ctype CallStatic##_jname##Method(JNIEnv* env, jclass c, jmethodID mid, ...) \
1432a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1433e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
143432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, true); \
1435e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        sc.CheckStaticMethod(c, mid); \
1436a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1437a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_list args; \
1438a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_start(args, mid); \
1439e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
1440a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        va_end(args); \
1441a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1442a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1443e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    static _ctype CallStatic##_jname##MethodV(JNIEnv* env, jclass c, jmethodID mid, va_list args) \
1444a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1445e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
144632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, true); \
1447e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        sc.CheckStaticMethod(c, mid); \
1448a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1449e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes         _retasgn(baseEnv(env)->CallStatic##_jname##MethodV(env, c, mid, args)); \
1450a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1451a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1452e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    static _ctype CallStatic##_jname##MethodA(JNIEnv* env, jclass c, jmethodID mid, jvalue* args) \
1453a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    { \
1454e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ecm.", env, c, mid); /* TODO: args! */ \
145532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes        sc.CheckSig(mid, _retsig, true); \
1456e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        sc.CheckStaticMethod(c, mid); \
1457a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retdecl; \
1458e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes        _retasgn(baseEnv(env)->CallStatic##_jname##MethodA(env, c, mid, args)); \
1459a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        _retok; \
1460a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1461a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1462a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define NON_VOID_RETURN(_retsig, _ctype) return CHECK_JNI_EXIT(_retsig, (_ctype) result)
1463a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define VOID_RETURN CHECK_JNI_EXIT_VOID()
1464a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
14652dd0e2cea360bc9206eb88ecc40d259e796c239dIan RogersCALL(jobject, Object, mirror::Object* result, result = reinterpret_cast<mirror::Object*>, NON_VOID_RETURN("L", jobject), "L");
1466ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jboolean, Boolean, jboolean result, result =, NON_VOID_RETURN("Z", jboolean), "Z");
1467ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jbyte, Byte, jbyte result, result =, NON_VOID_RETURN("B", jbyte), "B");
1468ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jchar, Char, jchar result, result =, NON_VOID_RETURN("C", jchar), "C");
1469ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jshort, Short, jshort result, result =, NON_VOID_RETURN("S", jshort), "S");
1470ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jint, Int, jint result, result =, NON_VOID_RETURN("I", jint), "I");
1471ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jlong, Long, jlong result, result =, NON_VOID_RETURN("J", jlong), "J");
1472ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jfloat, Float, jfloat result, result =, NON_VOID_RETURN("F", jfloat), "F");
1473ba8eee10607a524f43b55a6f33c13924fb16d435Elliott HughesCALL(jdouble, Double, jdouble result, result =, NON_VOID_RETURN("D", jdouble), "D");
1474a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesCALL(void, Void, , , VOID_RETURN, "V");
1475a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1476a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
1477a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Epz", env, unicodeChars, len);
1478a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("s", baseEnv(env)->NewString(env, unicodeChars, len));
1479a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1480a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1481a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jsize GetStringLength(JNIEnv* env, jstring string) {
1482a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1483a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringLength(env, string));
1484a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1485a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1486a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1487a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, java_string, isCopy);
1488a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    const jchar* result = baseEnv(env)->GetStringChars(env, java_string, isCopy);
1489ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (sc.ForceCopy() && result != nullptr) {
14902dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
1491a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      int byteCount = s->GetLength() * 2;
149232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
1493ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (isCopy != nullptr) {
1494a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        *isCopy = JNI_TRUE;
1495a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1496a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1497a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("p", result);
1498a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1499a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1500a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ReleaseStringChars(JNIEnv* env, jstring string, const jchar* chars) {
1501a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Esp", env, string, chars);
150232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckNonNull(chars);
150332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    if (sc.ForceCopy()) {
150432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      GuardedCopy::Check(__FUNCTION__, chars, false);
1505ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      chars = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(chars)));
1506a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1507a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ReleaseStringChars(env, string, chars);
1508a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1509a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1510a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1511a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
15127934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    CHECK_JNI_ENTRY(kFlag_NullableUtf, "Eu", env, bytes);  // TODO: show pointer and truncate string.
1513a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("s", baseEnv(env)->NewStringUTF(env, bytes));
1514a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1515a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1516a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jsize GetStringUTFLength(JNIEnv* env, jstring string) {
1517a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "Es", env, string);
1518a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetStringUTFLength(env, string));
1519a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1520a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1521a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static const char* GetStringUTFChars(JNIEnv* env, jstring string, jboolean* isCopy) {
1522a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "Esp", env, string, isCopy);
1523a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    const char* result = baseEnv(env)->GetStringUTFChars(env, string, isCopy);
1524ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (sc.ForceCopy() && result != nullptr) {
152532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      result = (const char*) GuardedCopy::Create(result, strlen(result) + 1, false);
1526ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (isCopy != nullptr) {
1527a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        *isCopy = JNI_TRUE;
1528a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1529a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
15307934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return CHECK_JNI_EXIT("u", result);  // TODO: show pointer and truncate string.
1531a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1532a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1533a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char* utf) {
15347934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    CHECK_JNI_ENTRY(kFlag_ExcepOkay | kFlag_Release, "Esu", env, string, utf);  // TODO: show pointer and truncate string.
153532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    if (sc.ForceCopy()) {
153632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      GuardedCopy::Check(__FUNCTION__, utf, false);
1537ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      utf = reinterpret_cast<const char*>(GuardedCopy::Destroy(const_cast<char*>(utf)));
1538a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1539a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ReleaseStringUTFChars(env, string, utf);
1540a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1541a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1542a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1543a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jsize GetArrayLength(JNIEnv* env, jarray array) {
1544a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "Ea", env, array);
1545a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetArrayLength(env, array));
1546a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1547a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1548a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass elementClass, jobject initialElement) {
1549a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EzcL", env, length, elementClass, initialElement);
1550a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("a", baseEnv(env)->NewObjectArray(env, length, elementClass, initialElement));
1551a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1552a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1553a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index) {
1554a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EaI", env, array, index);
1555a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->GetObjectArrayElement(env, array, index));
1556a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1557a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1558a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void SetObjectArrayElement(JNIEnv* env, jobjectArray array, jsize index, jobject value) {
1559a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EaIL", env, array, index, value);
1560a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->SetObjectArrayElement(env, array, index, value);
1561a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1562a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1563a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1564a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define NEW_PRIMITIVE_ARRAY(_artype, _jname) \
1565a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static _artype New##_jname##Array(JNIEnv* env, jsize length) { \
1566a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "Ez", env, length); \
1567a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        return CHECK_JNI_EXIT("a", baseEnv(env)->New##_jname##Array(env, length)); \
1568a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1569a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jbooleanArray, Boolean);
1570a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jbyteArray, Byte);
1571a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jcharArray, Char);
1572a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jshortArray, Short);
1573a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jintArray, Int);
1574a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jlongArray, Long);
1575a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jfloatArray, Float);
1576a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesNEW_PRIMITIVE_ARRAY(jdoubleArray, Double);
1577a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
157832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughesstruct ForceCopyGetChecker {
1579ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes public:
1580a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  ForceCopyGetChecker(ScopedCheck& sc, jboolean* isCopy) {
158132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    force_copy = sc.ForceCopy();
158232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    no_copy = 0;
1583ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (force_copy && isCopy != nullptr) {
15843f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      // Capture this before the base call tramples on it.
1585ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      no_copy = *reinterpret_cast<uint32_t*>(isCopy);
1586a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1587a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1588a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1589a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  template<typename ResultT>
159032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  ResultT Check(JNIEnv* env, jarray array, jboolean* isCopy, ResultT result) {
1591ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (force_copy && result != nullptr) {
159277129ff50fc0234b62684e556d2c0bcb86123e80Mathieu Chartier      result = reinterpret_cast<ResultT>(CreateGuardedPACopy(env, array, isCopy));
1593a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1594a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return result;
1595a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1596a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
159732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  uint32_t no_copy;
159832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  bool force_copy;
1599a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
1600a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1601a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1602a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static _ctype* Get##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, jboolean* isCopy) { \
1603a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Eap", env, array, isCopy); \
160432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    _ctype* result = ForceCopyGetChecker(sc, isCopy).Check(env, array, isCopy, baseEnv(env)->Get##_jname##ArrayElements(env, array, isCopy)); \
1605a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("p", result); \
1606a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1607a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1608a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname) \
1609a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void Release##_jname##ArrayElements(JNIEnv* env, _ctype##Array array, _ctype* elems, jint mode) { \
1610a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "Eapr", env, array, elems, mode); \
161132ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckNonNull(elems); \
161232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    if (sc.ForceCopy()) { \
1613a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      ReleaseGuardedPACopy(env, array, elems, mode); \
1614a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    } \
1615a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->Release##_jname##ArrayElements(env, array, elems, mode); \
1616a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID(); \
1617a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1618a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1619a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1620a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static void Get##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, _ctype* buf) { \
1621a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1622a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        baseEnv(env)->Get##_jname##ArrayRegion(env, array, start, len, buf); \
1623a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_EXIT_VOID(); \
1624a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1625a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1626a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname) \
1627a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    static void Set##_jname##ArrayRegion(JNIEnv* env, _ctype##Array array, jsize start, jsize len, const _ctype* buf) { \
1628a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_ENTRY(kFlag_Default, "EaIIp", env, array, start, len, buf); \
1629a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        baseEnv(env)->Set##_jname##ArrayRegion(env, array, start, len, buf); \
1630a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        CHECK_JNI_EXIT_VOID(); \
1631a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1632a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1633a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes#define PRIMITIVE_ARRAY_FUNCTIONS(_ctype, _jname, _typechar) \
1634a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    GET_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1635a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    RELEASE_PRIMITIVE_ARRAY_ELEMENTS(_ctype, _jname); \
1636a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    GET_PRIMITIVE_ARRAY_REGION(_ctype, _jname); \
1637a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    SET_PRIMITIVE_ARRAY_REGION(_ctype, _jname);
1638a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
16393f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes// TODO: verify primitive array type matches call type.
1640a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jboolean, Boolean, 'Z');
1641a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jbyte, Byte, 'B');
1642a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jchar, Char, 'C');
1643a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jshort, Short, 'S');
1644a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jint, Int, 'I');
1645a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jlong, Long, 'J');
1646a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jfloat, Float, 'F');
1647a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott HughesPRIMITIVE_ARRAY_FUNCTIONS(jdouble, Double, 'D');
1648a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1649e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jint RegisterNatives(JNIEnv* env, jclass c, const JNINativeMethod* methods, jint nMethods) {
1650e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EcpI", env, c, methods, nMethods);
1651e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->RegisterNatives(env, c, methods, nMethods));
1652a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1653a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1654e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes  static jint UnregisterNatives(JNIEnv* env, jclass c) {
1655e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ec", env, c);
1656e84278b035c8a48f6032ef98fae008d875fba7a4Elliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->UnregisterNatives(env, c));
1657a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1658a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1659a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint MonitorEnter(JNIEnv* env, jobject obj) {
1660a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1661a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
16627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JNI_ERR;  // Only for jni_internal_test. Real code will have aborted already.
1663a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
1664a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorEnter(env, obj));
1665a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1666a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1667a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint MonitorExit(JNIEnv* env, jobject obj) {
1668a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default | kFlag_ExcepOkay, "EL", env, obj);
1669a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    if (!sc.CheckInstance(ScopedCheck::kObject, obj)) {
16707934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JNI_ERR;  // Only for jni_internal_test. Real code will have aborted already.
1671a92853eb60fbce1fa6d3bbd6a6013fb514fdf5cbElliott Hughes    }
1672a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->MonitorExit(env, obj));
1673a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1674a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1675a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint GetJavaVM(JNIEnv *env, JavaVM **vm) {
1676a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, vm);
1677a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetJavaVM(env, vm));
1678a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1679a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1680a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void GetStringRegion(JNIEnv* env, jstring str, jsize start, jsize len, jchar* buf) {
1681a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1682a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->GetStringRegion(env, str, start, len, buf);
1683a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1684a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1685a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1686a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void GetStringUTFRegion(JNIEnv* env, jstring str, jsize start, jsize len, char* buf) {
1687a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay, "EsIIp", env, str, start, len, buf);
1688a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->GetStringUTFRegion(env, str, start, len, buf);
1689a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1690a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1691a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1692a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray array, jboolean* isCopy) {
1693a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritGet, "Eap", env, array, isCopy);
1694a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    void* result = baseEnv(env)->GetPrimitiveArrayCritical(env, array, isCopy);
1695ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (sc.ForceCopy() && result != nullptr) {
1696a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      result = CreateGuardedPACopy(env, array, isCopy);
1697a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1698a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("p", result);
1699a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1700a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1701a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void* carray, jint mode) {
1702a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Eapr", env, array, carray, mode);
170332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckNonNull(carray);
170432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    if (sc.ForceCopy()) {
1705a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      ReleaseGuardedPACopy(env, array, carray, mode);
1706a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1707a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ReleasePrimitiveArrayCritical(env, array, carray, mode);
1708a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1709a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1710a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1711a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* isCopy) {
1712a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritGet, "Esp", env, java_string, isCopy);
1713a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    const jchar* result = baseEnv(env)->GetStringCritical(env, java_string, isCopy);
1714ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (sc.ForceCopy() && result != nullptr) {
17152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* s = sc.soa().Decode<mirror::String*>(java_string);
1716a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      int byteCount = s->GetLength() * 2;
171732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      result = (const jchar*) GuardedCopy::Create(result, byteCount, false);
1718ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (isCopy != nullptr) {
1719a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes        *isCopy = JNI_TRUE;
1720a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes      }
1721a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1722a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("p", result);
1723a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1724a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1725a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void ReleaseStringCritical(JNIEnv* env, jstring string, const jchar* carray) {
1726a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritRelease | kFlag_ExcepOkay, "Esp", env, string, carray);
172732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.CheckNonNull(carray);
172832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    if (sc.ForceCopy()) {
172932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes      GuardedCopy::Check(__FUNCTION__, carray, false);
1730ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes      carray = reinterpret_cast<const jchar*>(GuardedCopy::Destroy(const_cast<jchar*>(carray)));
1731a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1732a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    baseEnv(env)->ReleaseStringCritical(env, string, carray);
1733a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_EXIT_VOID();
1734a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1735a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1736a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
1737a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, obj);
1738a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewWeakGlobalRef(env, obj));
1739a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1740a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1741a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jboolean ExceptionCheck(JNIEnv* env) {
1742a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_CritOkay | kFlag_ExcepOkay, "E", env);
1743a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("b", baseEnv(env)->ExceptionCheck(env));
1744a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1745a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1746a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobjectRefType GetObjectRefType(JNIEnv* env, jobject obj) {
1747a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // Note: we use "Ep" rather than "EL" because this is the one JNI function
1748a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // that it's okay to pass an invalid reference to.
1749a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "Ep", env, obj);
1750a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: proper decoding of jobjectRefType!
1751a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("I", baseEnv(env)->GetObjectRefType(env, obj));
1752a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1753a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1754a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
1755a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
1756ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (address == nullptr) {
17573f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes      JniAbortF(__FUNCTION__, "non-nullable address is NULL");
1758a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1759ef809d09cf0d28d999ac69969e4506d8afa4624dNarayan Kamath    if (capacity < 0) {
1760ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      JniAbortF(__FUNCTION__, "capacity must be non-negative: %" PRId64, capacity);
1761a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    }
1762a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
1763a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1764a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1765a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static void* GetDirectBufferAddress(JNIEnv* env, jobject buf) {
1766a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1767a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: check that 'buf' is a java.nio.Buffer.
1768a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("p", baseEnv(env)->GetDirectBufferAddress(env, buf));
1769a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1770a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1771a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jlong GetDirectBufferCapacity(JNIEnv* env, jobject buf) {
1772a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    CHECK_JNI_ENTRY(kFlag_Default, "EL", env, buf);
1773a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    // TODO: check that 'buf' is a java.nio.Buffer.
1774a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return CHECK_JNI_EXIT("J", baseEnv(env)->GetDirectBufferCapacity(env, buf));
1775a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1776a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1777a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes private:
1778a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static inline const JNINativeInterface* baseEnv(JNIEnv* env) {
1779a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return reinterpret_cast<JNIEnvExt*>(env)->unchecked_functions;
1780a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
1781a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
1782a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
1783a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesconst JNINativeInterface gCheckNativeInterface = {
1784ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved0.
1785ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved1.
1786ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved2.
1787ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved3.
1788a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetVersion,
1789a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::DefineClass,
1790a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::FindClass,
1791a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::FromReflectedMethod,
1792a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::FromReflectedField,
1793a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ToReflectedMethod,
1794a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetSuperclass,
1795a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::IsAssignableFrom,
1796a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ToReflectedField,
1797a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::Throw,
1798a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ThrowNew,
1799a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ExceptionOccurred,
1800a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ExceptionDescribe,
1801a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ExceptionClear,
1802a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::FatalError,
1803a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::PushLocalFrame,
1804a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::PopLocalFrame,
1805a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewGlobalRef,
1806a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::DeleteGlobalRef,
1807a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::DeleteLocalRef,
1808a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::IsSameObject,
1809a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewLocalRef,
1810a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::EnsureLocalCapacity,
1811a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::AllocObject,
1812a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewObject,
1813a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewObjectV,
1814a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewObjectA,
1815a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetObjectClass,
1816a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::IsInstanceOf,
1817a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetMethodID,
1818a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallObjectMethod,
1819a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallObjectMethodV,
1820a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallObjectMethodA,
1821a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallBooleanMethod,
1822a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallBooleanMethodV,
1823a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallBooleanMethodA,
1824a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallByteMethod,
1825a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallByteMethodV,
1826a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallByteMethodA,
1827a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallCharMethod,
1828a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallCharMethodV,
1829a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallCharMethodA,
1830a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallShortMethod,
1831a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallShortMethodV,
1832a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallShortMethodA,
1833a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallIntMethod,
1834a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallIntMethodV,
1835a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallIntMethodA,
1836a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallLongMethod,
1837a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallLongMethodV,
1838a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallLongMethodA,
1839a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallFloatMethod,
1840a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallFloatMethodV,
1841a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallFloatMethodA,
1842a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallDoubleMethod,
1843a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallDoubleMethodV,
1844a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallDoubleMethodA,
1845a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallVoidMethod,
1846a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallVoidMethodV,
1847a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallVoidMethodA,
1848a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualObjectMethod,
1849a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualObjectMethodV,
1850a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualObjectMethodA,
1851a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualBooleanMethod,
1852a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualBooleanMethodV,
1853a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualBooleanMethodA,
1854a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualByteMethod,
1855a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualByteMethodV,
1856a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualByteMethodA,
1857a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualCharMethod,
1858a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualCharMethodV,
1859a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualCharMethodA,
1860a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualShortMethod,
1861a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualShortMethodV,
1862a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualShortMethodA,
1863a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualIntMethod,
1864a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualIntMethodV,
1865a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualIntMethodA,
1866a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualLongMethod,
1867a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualLongMethodV,
1868a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualLongMethodA,
1869a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualFloatMethod,
1870a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualFloatMethodV,
1871a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualFloatMethodA,
1872a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualDoubleMethod,
1873a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualDoubleMethodV,
1874a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualDoubleMethodA,
1875a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualVoidMethod,
1876a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualVoidMethodV,
1877a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallNonvirtualVoidMethodA,
1878a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetFieldID,
1879a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetObjectField,
1880a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetBooleanField,
1881a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetByteField,
1882a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetCharField,
1883a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetShortField,
1884a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetIntField,
1885a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetLongField,
1886a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetFloatField,
1887a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetDoubleField,
1888a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetObjectField,
1889a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetBooleanField,
1890a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetByteField,
1891a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetCharField,
1892a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetShortField,
1893a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetIntField,
1894a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetLongField,
1895a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetFloatField,
1896a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetDoubleField,
1897a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticMethodID,
1898a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticObjectMethod,
1899a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticObjectMethodV,
1900a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticObjectMethodA,
1901a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticBooleanMethod,
1902a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticBooleanMethodV,
1903a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticBooleanMethodA,
1904a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticByteMethod,
1905a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticByteMethodV,
1906a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticByteMethodA,
1907a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticCharMethod,
1908a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticCharMethodV,
1909a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticCharMethodA,
1910a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticShortMethod,
1911a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticShortMethodV,
1912a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticShortMethodA,
1913a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticIntMethod,
1914a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticIntMethodV,
1915a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticIntMethodA,
1916a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticLongMethod,
1917a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticLongMethodV,
1918a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticLongMethodA,
1919a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticFloatMethod,
1920a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticFloatMethodV,
1921a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticFloatMethodA,
1922a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticDoubleMethod,
1923a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticDoubleMethodV,
1924a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticDoubleMethodA,
1925a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticVoidMethod,
1926a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticVoidMethodV,
1927a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::CallStaticVoidMethodA,
1928a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticFieldID,
1929a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticObjectField,
1930a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticBooleanField,
1931a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticByteField,
1932a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticCharField,
1933a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticShortField,
1934a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticIntField,
1935a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticLongField,
1936a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticFloatField,
1937a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStaticDoubleField,
1938a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticObjectField,
1939a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticBooleanField,
1940a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticByteField,
1941a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticCharField,
1942a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticShortField,
1943a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticIntField,
1944a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticLongField,
1945a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticFloatField,
1946a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetStaticDoubleField,
1947a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewString,
1948a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringLength,
1949a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringChars,
1950a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseStringChars,
1951a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewStringUTF,
1952a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringUTFLength,
1953a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringUTFChars,
1954a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseStringUTFChars,
1955a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetArrayLength,
1956a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewObjectArray,
1957a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetObjectArrayElement,
1958a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetObjectArrayElement,
1959a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewBooleanArray,
1960a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewByteArray,
1961a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewCharArray,
1962a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewShortArray,
1963a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewIntArray,
1964a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewLongArray,
1965a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewFloatArray,
1966a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewDoubleArray,
1967a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetBooleanArrayElements,
1968a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetByteArrayElements,
1969a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetCharArrayElements,
1970a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetShortArrayElements,
1971a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetIntArrayElements,
1972a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetLongArrayElements,
1973a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetFloatArrayElements,
1974a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetDoubleArrayElements,
1975a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseBooleanArrayElements,
1976a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseByteArrayElements,
1977a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseCharArrayElements,
1978a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseShortArrayElements,
1979a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseIntArrayElements,
1980a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseLongArrayElements,
1981a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseFloatArrayElements,
1982a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseDoubleArrayElements,
1983a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetBooleanArrayRegion,
1984a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetByteArrayRegion,
1985a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetCharArrayRegion,
1986a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetShortArrayRegion,
1987a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetIntArrayRegion,
1988a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetLongArrayRegion,
1989a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetFloatArrayRegion,
1990a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetDoubleArrayRegion,
1991a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetBooleanArrayRegion,
1992a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetByteArrayRegion,
1993a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetCharArrayRegion,
1994a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetShortArrayRegion,
1995a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetIntArrayRegion,
1996a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetLongArrayRegion,
1997a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetFloatArrayRegion,
1998a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::SetDoubleArrayRegion,
1999a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::RegisterNatives,
2000a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::UnregisterNatives,
2001a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::MonitorEnter,
2002a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::MonitorExit,
2003a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetJavaVM,
2004a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringRegion,
2005a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringUTFRegion,
2006a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetPrimitiveArrayCritical,
2007a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleasePrimitiveArrayCritical,
2008a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetStringCritical,
2009a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ReleaseStringCritical,
2010a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewWeakGlobalRef,
2011a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::DeleteWeakGlobalRef,
2012a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::ExceptionCheck,
2013a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::NewDirectByteBuffer,
2014a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetDirectBufferAddress,
2015a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetDirectBufferCapacity,
2016a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJNI::GetObjectRefType,
2017a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
2018a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2019a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesconst JNINativeInterface* GetCheckJniNativeInterface() {
2020a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  return &gCheckNativeInterface;
2021a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}
2022a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2023a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesclass CheckJII {
2024ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes public:
2025a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint DestroyJavaVM(JavaVM* vm) {
2026a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    ScopedCheck sc(vm, false, __FUNCTION__);
202732ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.Check(true, "v", vm);
202832ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return CHECK_JNI_EXIT("I", BaseVm(vm)->DestroyJavaVM(vm));
2029a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2030a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2031a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2032a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    ScopedCheck sc(vm, false, __FUNCTION__);
203332ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.Check(true, "vpp", vm, p_env, thr_args);
203432ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThread(vm, p_env, thr_args));
2035a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2036a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2037a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
2038a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    ScopedCheck sc(vm, false, __FUNCTION__);
203932ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.Check(true, "vpp", vm, p_env, thr_args);
204032ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return CHECK_JNI_EXIT("I", BaseVm(vm)->AttachCurrentThreadAsDaemon(vm, p_env, thr_args));
2041a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2042a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2043a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint DetachCurrentThread(JavaVM* vm) {
2044a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    ScopedCheck sc(vm, true, __FUNCTION__);
204532ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    sc.Check(true, "v", vm);
204632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return CHECK_JNI_EXIT("I", BaseVm(vm)->DetachCurrentThread(vm));
2047a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2048a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2049a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  static jint GetEnv(JavaVM* vm, void** env, jint version) {
2050a09576416788b916095739e43a16917e7948f3a4Elliott Hughes    ScopedCheck sc(vm, true, __FUNCTION__);
205183a25328c595975097cf3948451088cbfc64fc09Elliott Hughes    sc.Check(true, "vpI", vm);
205232ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes    return CHECK_JNI_EXIT("I", BaseVm(vm)->GetEnv(vm, env, version));
2053a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2054a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2055a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes private:
205632ae6e33303cc51b524d9eef9b2dd547ecc638e9Elliott Hughes  static inline const JNIInvokeInterface* BaseVm(JavaVM* vm) {
2057a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes    return reinterpret_cast<JavaVMExt*>(vm)->unchecked_functions;
2058a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  }
2059a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
2060a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2061a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesconst JNIInvokeInterface gCheckInvokeInterface = {
2062ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved0
2063ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved1
2064ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  nullptr,  // reserved2
2065a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJII::DestroyJavaVM,
2066a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJII::AttachCurrentThread,
2067a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJII::DetachCurrentThread,
2068a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJII::GetEnv,
2069a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  CheckJII::AttachCurrentThreadAsDaemon
2070a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes};
2071a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2072a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughesconst JNIInvokeInterface* GetCheckJniInvokeInterface() {
2073a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes  return &gCheckInvokeInterface;
2074a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}
2075a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
2076a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes}  // namespace art
2077