1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "jni_internal.h"
18 
19 #include <cstdarg>
20 #include <memory>
21 #include <utility>
22 
23 #include "art_field-inl.h"
24 #include "art_method-inl.h"
25 #include "base/allocator.h"
26 #include "base/atomic.h"
27 #include "base/enums.h"
28 #include "base/file_utils.h"
29 #include "base/logging.h"  // For VLOG.
30 #include "base/mutex.h"
31 #include "base/safe_map.h"
32 #include "base/stl_util.h"
33 #include "class_linker-inl.h"
34 #include "class_root.h"
35 #include "dex/dex_file-inl.h"
36 #include "dex/utf.h"
37 #include "fault_handler.h"
38 #include "hidden_api.h"
39 #include "gc/accounting/card_table-inl.h"
40 #include "gc_root.h"
41 #include "indirect_reference_table-inl.h"
42 #include "interpreter/interpreter.h"
43 #include "java_vm_ext.h"
44 #include "jni_env_ext.h"
45 #include "jvalue-inl.h"
46 #include "mirror/class-alloc-inl.h"
47 #include "mirror/class-inl.h"
48 #include "mirror/class_loader.h"
49 #include "mirror/dex_cache-inl.h"
50 #include "mirror/field-inl.h"
51 #include "mirror/method.h"
52 #include "mirror/object-inl.h"
53 #include "mirror/object_array-alloc-inl.h"
54 #include "mirror/object_array-inl.h"
55 #include "mirror/string-alloc-inl.h"
56 #include "mirror/string-inl.h"
57 #include "mirror/throwable.h"
58 #include "nativehelper/scoped_local_ref.h"
59 #include "parsed_options.h"
60 #include "reflection.h"
61 #include "runtime.h"
62 #include "scoped_thread_state_change-inl.h"
63 #include "thread.h"
64 #include "well_known_classes.h"
65 
66 namespace {
67 // Frees the given va_list upon destruction.
68 // This also guards the returns from inside of the CHECK_NON_NULL_ARGUMENTs.
69 struct ScopedVAArgs {
ScopedVAArgs__anon29e82fe90111::ScopedVAArgs70   explicit ScopedVAArgs(va_list* args): args(args) {}
71   ScopedVAArgs(const ScopedVAArgs&) = delete;
72   ScopedVAArgs(ScopedVAArgs&&) = delete;
~ScopedVAArgs__anon29e82fe90111::ScopedVAArgs73   ~ScopedVAArgs() { va_end(*args); }
74 
75  private:
76   va_list* args;
77 };
78 
79 }  // namespace
80 
81 namespace art {
82 
83 // Consider turning this on when there is errors which could be related to JNI array copies such as
84 // things not rendering correctly. E.g. b/16858794
85 static constexpr bool kWarnJniAbort = false;
86 
87 template<typename T>
ShouldDenyAccessToMember(T * member,Thread * self)88 ALWAYS_INLINE static bool ShouldDenyAccessToMember(T* member, Thread* self)
89     REQUIRES_SHARED(Locks::mutator_lock_) {
90   return hiddenapi::ShouldDenyAccessToMember(
91       member,
92       [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
93         // Construct AccessContext from the first calling class on stack.
94         // If the calling class cannot be determined, e.g. unattached threads,
95         // we conservatively assume the caller is trusted.
96         ObjPtr<mirror::Class> caller = GetCallingClass(self, /* num_frames */ 1);
97         return caller.IsNull() ? hiddenapi::AccessContext(/* is_trusted= */ true)
98                                : hiddenapi::AccessContext(caller);
99       },
100       hiddenapi::AccessMethod::kJNI);
101 }
102 
103 // Helpers to call instrumentation functions for fields. These take jobjects so we don't need to set
104 // up handles for the rare case where these actually do something. Once these functions return it is
105 // possible there will be a pending exception if the instrumentation happens to throw one.
NotifySetObjectField(ArtField * field,jobject obj,jobject jval)106 static void NotifySetObjectField(ArtField* field, jobject obj, jobject jval)
107     REQUIRES_SHARED(Locks::mutator_lock_) {
108   DCHECK_EQ(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
109   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
110   if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
111     Thread* self = Thread::Current();
112     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
113                                                    /*check_suspended=*/ true,
114                                                    /*abort_on_error=*/ false);
115 
116     if (cur_method == nullptr) {
117       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
118       // of these changes.
119       return;
120     }
121     DCHECK(cur_method->IsNative());
122     JValue val;
123     val.SetL(self->DecodeJObject(jval));
124     instrumentation->FieldWriteEvent(self,
125                                      self->DecodeJObject(obj),
126                                      cur_method,
127                                      0,  // dex_pc is always 0 since this is a native method.
128                                      field,
129                                      val);
130   }
131 }
132 
NotifySetPrimitiveField(ArtField * field,jobject obj,JValue val)133 static void NotifySetPrimitiveField(ArtField* field, jobject obj, JValue val)
134     REQUIRES_SHARED(Locks::mutator_lock_) {
135   DCHECK_NE(field->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
136   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
137   if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
138     Thread* self = Thread::Current();
139     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
140                                                    /*check_suspended=*/ true,
141                                                    /*abort_on_error=*/ false);
142 
143     if (cur_method == nullptr) {
144       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
145       // of these changes.
146       return;
147     }
148     DCHECK(cur_method->IsNative());
149     instrumentation->FieldWriteEvent(self,
150                                      self->DecodeJObject(obj),
151                                      cur_method,
152                                      0,  // dex_pc is always 0 since this is a native method.
153                                      field,
154                                      val);
155   }
156 }
157 
NotifyGetField(ArtField * field,jobject obj)158 static void NotifyGetField(ArtField* field, jobject obj)
159     REQUIRES_SHARED(Locks::mutator_lock_) {
160   instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
161   if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
162     Thread* self = Thread::Current();
163     ArtMethod* cur_method = self->GetCurrentMethod(/*dex_pc=*/ nullptr,
164                                                    /*check_suspended=*/ true,
165                                                    /*abort_on_error=*/ false);
166 
167     if (cur_method == nullptr) {
168       // Set/Get Fields can be issued without a method during runtime startup/teardown. Ignore all
169       // of these changes.
170       return;
171     }
172     DCHECK(cur_method->IsNative());
173     instrumentation->FieldReadEvent(self,
174                                     self->DecodeJObject(obj),
175                                     cur_method,
176                                     0,  // dex_pc is always 0 since this is a native method.
177                                     field);
178   }
179 }
180 
181 // Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
182 // separated with slashes but aren't wrapped with "L;" like regular descriptors
183 // (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
184 // exception; there the "L;" must be present ("[La/b/C;"). Historically we've
185 // supported names with dots too (such as "a.b.C").
NormalizeJniClassDescriptor(const char * name)186 static std::string NormalizeJniClassDescriptor(const char* name) {
187   std::string result;
188   // Add the missing "L;" if necessary.
189   if (name[0] == '[') {
190     result = name;
191   } else {
192     result += 'L';
193     result += name;
194     result += ';';
195   }
196   // Rewrite '.' as '/' for backwards compatibility.
197   if (result.find('.') != std::string::npos) {
198     LOG(WARNING) << "Call to JNI FindClass with dots in name: "
199                  << "\"" << name << "\"";
200     std::replace(result.begin(), result.end(), '.', '/');
201   }
202   return result;
203 }
204 
ReportInvalidJNINativeMethod(const ScopedObjectAccess & soa,ObjPtr<mirror::Class> c,const char * kind,jint idx)205 static void ReportInvalidJNINativeMethod(const ScopedObjectAccess& soa,
206                                          ObjPtr<mirror::Class> c,
207                                          const char* kind,
208                                          jint idx)
209     REQUIRES_SHARED(Locks::mutator_lock_) {
210   LOG(ERROR)
211       << "Failed to register native method in " << c->PrettyDescriptor()
212       << " in " << c->GetDexCache()->GetLocation()->ToModifiedUtf8()
213       << ": " << kind << " is null at index " << idx;
214   soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
215                                  "%s is null at index %d",
216                                  kind,
217                                  idx);
218 }
219 
220 template<bool kEnableIndexIds>
FindMethodID(ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)221 static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
222                               const char* name, const char* sig, bool is_static)
223     REQUIRES_SHARED(Locks::mutator_lock_) {
224   return jni::EncodeArtMethod<kEnableIndexIds>(FindMethodJNI(soa, jni_class, name, sig, is_static));
225 }
226 
227 template<bool kEnableIndexIds>
GetClassLoader(const ScopedObjectAccess & soa)228 static ObjPtr<mirror::ClassLoader> GetClassLoader(const ScopedObjectAccess& soa)
229     REQUIRES_SHARED(Locks::mutator_lock_) {
230   ArtMethod* method = soa.Self()->GetCurrentMethod(nullptr);
231   // If we are running Runtime.nativeLoad, use the overriding ClassLoader it set.
232   if (method ==
233       jni::DecodeArtMethod<kEnableIndexIds>(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
234     return soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
235   }
236   // If we have a method, use its ClassLoader for context.
237   if (method != nullptr) {
238     return method->GetDeclaringClass()->GetClassLoader();
239   }
240   // We don't have a method, so try to use the system ClassLoader.
241   ObjPtr<mirror::ClassLoader> class_loader =
242       soa.Decode<mirror::ClassLoader>(Runtime::Current()->GetSystemClassLoader());
243   if (class_loader != nullptr) {
244     return class_loader;
245   }
246   // See if the override ClassLoader is set for gtests.
247   class_loader = soa.Decode<mirror::ClassLoader>(soa.Self()->GetClassLoaderOverride());
248   if (class_loader != nullptr) {
249     // If so, CommonCompilerTest should have marked the runtime as a compiler not compiling an
250     // image.
251     CHECK(Runtime::Current()->IsAotCompiler());
252     CHECK(!Runtime::Current()->IsCompilingBootImage());
253     return class_loader;
254   }
255   // Use the BOOTCLASSPATH.
256   return nullptr;
257 }
258 
259 template<bool kEnableIndexIds>
FindFieldID(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)260 static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
261                             const char* sig, bool is_static)
262     REQUIRES_SHARED(Locks::mutator_lock_) {
263   return jni::EncodeArtField<kEnableIndexIds>(FindFieldJNI(soa, jni_class, name, sig, is_static));
264 }
265 
ThrowAIOOBE(ScopedObjectAccess & soa,ObjPtr<mirror::Array> array,jsize start,jsize length,const char * identifier)266 static void ThrowAIOOBE(ScopedObjectAccess& soa,
267                         ObjPtr<mirror::Array> array,
268                         jsize start,
269                         jsize length,
270                         const char* identifier)
271     REQUIRES_SHARED(Locks::mutator_lock_) {
272   std::string type(array->PrettyTypeOf());
273   soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
274                                  "%s offset=%d length=%d %s.length=%d",
275                                  type.c_str(), start, length, identifier, array->GetLength());
276 }
277 
ThrowSIOOBE(ScopedObjectAccess & soa,jsize start,jsize length,jsize array_length)278 static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
279                         jsize array_length)
280     REQUIRES_SHARED(Locks::mutator_lock_) {
281   soa.Self()->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
282                                  "offset=%d length=%d string.length()=%d", start, length,
283                                  array_length);
284 }
285 
ThrowNoSuchMethodError(const ScopedObjectAccess & soa,ObjPtr<mirror::Class> c,const char * name,const char * sig,const char * kind)286 static void ThrowNoSuchMethodError(const ScopedObjectAccess& soa,
287                                    ObjPtr<mirror::Class> c,
288                                    const char* name,
289                                    const char* sig,
290                                    const char* kind)
291     REQUIRES_SHARED(Locks::mutator_lock_) {
292   std::string temp;
293   soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
294                                  "no %s method \"%s.%s%s\"",
295                                  kind,
296                                  c->GetDescriptor(&temp),
297                                  name,
298                                  sig);
299 }
300 
EnsureInitialized(Thread * self,ObjPtr<mirror::Class> klass)301 static ObjPtr<mirror::Class> EnsureInitialized(Thread* self, ObjPtr<mirror::Class> klass)
302     REQUIRES_SHARED(Locks::mutator_lock_) {
303   if (LIKELY(klass->IsInitialized())) {
304     return klass;
305   }
306   StackHandleScope<1> hs(self);
307   Handle<mirror::Class> h_klass(hs.NewHandle(klass));
308   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
309     return nullptr;
310   }
311   return h_klass.Get();
312 }
313 
FindMethodJNI(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)314 ArtMethod* FindMethodJNI(const ScopedObjectAccess& soa,
315                          jclass jni_class,
316                          const char* name,
317                          const char* sig,
318                          bool is_static) {
319   ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class));
320   if (c == nullptr) {
321     return nullptr;
322   }
323   ArtMethod* method = nullptr;
324   auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
325   if (c->IsInterface()) {
326     method = c->FindInterfaceMethod(name, sig, pointer_size);
327   } else {
328     method = c->FindClassMethod(name, sig, pointer_size);
329   }
330   if (method != nullptr && ShouldDenyAccessToMember(method, soa.Self())) {
331     method = nullptr;
332   }
333   if (method == nullptr || method->IsStatic() != is_static) {
334     ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
335     return nullptr;
336   }
337   return method;
338 }
339 
FindFieldJNI(const ScopedObjectAccess & soa,jclass jni_class,const char * name,const char * sig,bool is_static)340 ArtField* FindFieldJNI(const ScopedObjectAccess& soa,
341                        jclass jni_class,
342                        const char* name,
343                        const char* sig,
344                        bool is_static) {
345   StackHandleScope<2> hs(soa.Self());
346   Handle<mirror::Class> c(
347       hs.NewHandle(EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(jni_class))));
348   if (c == nullptr) {
349     return nullptr;
350   }
351   ArtField* field = nullptr;
352   ObjPtr<mirror::Class> field_type;
353   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
354   if (UNLIKELY(sig[0] == '\0')) {
355     DCHECK(field == nullptr);
356   } else if (sig[1] != '\0') {
357     Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
358     field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
359   } else {
360     field_type = class_linker->FindPrimitiveClass(*sig);
361   }
362   if (field_type == nullptr) {
363     // Failed to find type from the signature of the field.
364     DCHECK(sig[0] == '\0' || soa.Self()->IsExceptionPending());
365     StackHandleScope<1> hs2(soa.Self());
366     Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
367     soa.Self()->ClearException();
368     std::string temp;
369     soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
370                                    "no type \"%s\" found and so no field \"%s\" "
371                                    "could be found in class \"%s\" or its superclasses", sig, name,
372                                    c->GetDescriptor(&temp));
373     if (cause != nullptr) {
374       soa.Self()->GetException()->SetCause(cause.Get());
375     }
376     return nullptr;
377   }
378   std::string temp;
379   if (is_static) {
380     field = mirror::Class::FindStaticField(
381         soa.Self(), c.Get(), name, field_type->GetDescriptor(&temp));
382   } else {
383     field = c->FindInstanceField(name, field_type->GetDescriptor(&temp));
384   }
385   if (field != nullptr && ShouldDenyAccessToMember(field, soa.Self())) {
386     field = nullptr;
387   }
388   if (field == nullptr) {
389     soa.Self()->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
390                                    "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
391                                    sig, name, c->GetDescriptor(&temp));
392     return nullptr;
393   }
394   return field;
395 }
396 
ThrowNewException(JNIEnv * env,jclass exception_class,const char * msg,jobject cause)397 int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
398     REQUIRES(!Locks::mutator_lock_) {
399   // Turn the const char* into a java.lang.String.
400   ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
401   if (msg != nullptr && s.get() == nullptr) {
402     return JNI_ERR;
403   }
404 
405   // Choose an appropriate constructor and set up the arguments.
406   jvalue args[2];
407   const char* signature;
408   if (msg == nullptr && cause == nullptr) {
409     signature = "()V";
410   } else if (msg != nullptr && cause == nullptr) {
411     signature = "(Ljava/lang/String;)V";
412     args[0].l = s.get();
413   } else if (msg == nullptr && cause != nullptr) {
414     signature = "(Ljava/lang/Throwable;)V";
415     args[0].l = cause;
416   } else {
417     signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
418     args[0].l = s.get();
419     args[1].l = cause;
420   }
421   jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
422   if (mid == nullptr) {
423     ScopedObjectAccess soa(env);
424     LOG(ERROR) << "No <init>" << signature << " in "
425         << mirror::Class::PrettyClass(soa.Decode<mirror::Class>(exception_class));
426     return JNI_ERR;
427   }
428 
429   ScopedLocalRef<jthrowable> exception(
430       env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
431   if (exception.get() == nullptr) {
432     return JNI_ERR;
433   }
434   ScopedObjectAccess soa(env);
435   soa.Self()->SetException(soa.Decode<mirror::Throwable>(exception.get()));
436   return JNI_OK;
437 }
438 
JavaVmExtFromEnv(JNIEnv * env)439 static JavaVMExt* JavaVmExtFromEnv(JNIEnv* env) {
440   return reinterpret_cast<JNIEnvExt*>(env)->GetVm();
441 }
442 
443 #define CHECK_NON_NULL_ARGUMENT(value) \
444     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, nullptr)
445 
446 #define CHECK_NON_NULL_ARGUMENT_RETURN_VOID(value) \
447     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, )
448 
449 #define CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(value) \
450     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, 0)
451 
452 #define CHECK_NON_NULL_ARGUMENT_RETURN(value, return_val) \
453     CHECK_NON_NULL_ARGUMENT_FN_NAME(__FUNCTION__, value, return_val)
454 
455 #define CHECK_NON_NULL_ARGUMENT_FN_NAME(name, value, return_val) \
456   if (UNLIKELY((value) == nullptr)) { \
457     JavaVmExtFromEnv(env)->JniAbort(name, #value " == null"); \
458     return return_val; \
459   }
460 
461 #define CHECK_NON_NULL_MEMCPY_ARGUMENT(length, value) \
462   if (UNLIKELY((length) != 0 && (value) == nullptr)) { \
463     JavaVmExtFromEnv(env)->JniAbort(__FUNCTION__, #value " == null"); \
464     return; \
465   }
466 
467 template <bool kNative>
FindMethod(ObjPtr<mirror::Class> c,std::string_view name,std::string_view sig)468 static ArtMethod* FindMethod(ObjPtr<mirror::Class> c,
469                              std::string_view name,
470                              std::string_view sig)
471     REQUIRES_SHARED(Locks::mutator_lock_) {
472   auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
473   for (auto& method : c->GetMethods(pointer_size)) {
474     if (kNative == method.IsNative() && name == method.GetName() && method.GetSignature() == sig) {
475       return &method;
476     }
477   }
478   return nullptr;
479 }
480 
481 template <bool kEnableIndexIds>
482 class JNI {
483  public:
GetVersion(JNIEnv *)484   static jint GetVersion(JNIEnv*) {
485     return JNI_VERSION_1_6;
486   }
487 
DefineClass(JNIEnv *,const char *,jobject,const jbyte *,jsize)488   static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
489     LOG(WARNING) << "JNI DefineClass is not supported";
490     return nullptr;
491   }
492 
FindClass(JNIEnv * env,const char * name)493   static jclass FindClass(JNIEnv* env, const char* name) {
494     CHECK_NON_NULL_ARGUMENT(name);
495     Runtime* runtime = Runtime::Current();
496     ClassLinker* class_linker = runtime->GetClassLinker();
497     std::string descriptor(NormalizeJniClassDescriptor(name));
498     ScopedObjectAccess soa(env);
499     ObjPtr<mirror::Class> c = nullptr;
500     if (runtime->IsStarted()) {
501       StackHandleScope<1> hs(soa.Self());
502       Handle<mirror::ClassLoader> class_loader(hs.NewHandle(GetClassLoader<kEnableIndexIds>(soa)));
503       c = class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader);
504     } else {
505       c = class_linker->FindSystemClass(soa.Self(), descriptor.c_str());
506     }
507     return soa.AddLocalReference<jclass>(c);
508   }
509 
FromReflectedMethod(JNIEnv * env,jobject jlr_method)510   static jmethodID FromReflectedMethod(JNIEnv* env, jobject jlr_method) {
511     CHECK_NON_NULL_ARGUMENT(jlr_method);
512     ScopedObjectAccess soa(env);
513     return jni::EncodeArtMethod<kEnableIndexIds>(ArtMethod::FromReflectedMethod(soa, jlr_method));
514   }
515 
FromReflectedField(JNIEnv * env,jobject jlr_field)516   static jfieldID FromReflectedField(JNIEnv* env, jobject jlr_field) {
517     CHECK_NON_NULL_ARGUMENT(jlr_field);
518     ScopedObjectAccess soa(env);
519     ObjPtr<mirror::Object> obj_field = soa.Decode<mirror::Object>(jlr_field);
520     if (obj_field->GetClass() != GetClassRoot<mirror::Field>()) {
521       // Not even a java.lang.reflect.Field, return null. TODO, is this check necessary?
522       return nullptr;
523     }
524     ObjPtr<mirror::Field> field = ObjPtr<mirror::Field>::DownCast(obj_field);
525     return jni::EncodeArtField<kEnableIndexIds>(field->GetArtField());
526   }
527 
ToReflectedMethod(JNIEnv * env,jclass,jmethodID mid,jboolean)528   static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
529     CHECK_NON_NULL_ARGUMENT(mid);
530     ScopedObjectAccess soa(env);
531     ArtMethod* m = jni::DecodeArtMethod(mid);
532     ObjPtr<mirror::Executable> method;
533     DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
534     DCHECK(!Runtime::Current()->IsActiveTransaction());
535     if (m->IsConstructor()) {
536       method = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
537     } else {
538       method = mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), m);
539     }
540     return soa.AddLocalReference<jobject>(method);
541   }
542 
ToReflectedField(JNIEnv * env,jclass,jfieldID fid,jboolean)543   static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
544     CHECK_NON_NULL_ARGUMENT(fid);
545     ScopedObjectAccess soa(env);
546     ArtField* f = jni::DecodeArtField(fid);
547     return soa.AddLocalReference<jobject>(
548         mirror::Field::CreateFromArtField<kRuntimePointerSize>(soa.Self(), f, true));
549   }
550 
GetObjectClass(JNIEnv * env,jobject java_object)551   static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
552     CHECK_NON_NULL_ARGUMENT(java_object);
553     ScopedObjectAccess soa(env);
554     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
555     return soa.AddLocalReference<jclass>(o->GetClass());
556   }
557 
GetSuperclass(JNIEnv * env,jclass java_class)558   static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
559     CHECK_NON_NULL_ARGUMENT(java_class);
560     ScopedObjectAccess soa(env);
561     ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
562     return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
563   }
564 
565   // Note: java_class1 should be safely castable to java_class2, and
566   // not the other way around.
IsAssignableFrom(JNIEnv * env,jclass java_class1,jclass java_class2)567   static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
568     CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
569     CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
570     ScopedObjectAccess soa(env);
571     ObjPtr<mirror::Class> c1 = soa.Decode<mirror::Class>(java_class1);
572     ObjPtr<mirror::Class> c2 = soa.Decode<mirror::Class>(java_class2);
573     return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
574   }
575 
IsInstanceOf(JNIEnv * env,jobject jobj,jclass java_class)576   static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
577     CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
578     if (jobj == nullptr) {
579       // Note: JNI is different from regular Java instanceof in this respect
580       return JNI_TRUE;
581     } else {
582       ScopedObjectAccess soa(env);
583       ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
584       ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
585       return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
586     }
587   }
588 
Throw(JNIEnv * env,jthrowable java_exception)589   static jint Throw(JNIEnv* env, jthrowable java_exception) {
590     ScopedObjectAccess soa(env);
591     ObjPtr<mirror::Throwable> exception = soa.Decode<mirror::Throwable>(java_exception);
592     if (exception == nullptr) {
593       return JNI_ERR;
594     }
595     soa.Self()->SetException(exception);
596     return JNI_OK;
597   }
598 
ThrowNew(JNIEnv * env,jclass c,const char * msg)599   static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
600     CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
601     return ThrowNewException(env, c, msg, nullptr);
602   }
603 
ExceptionCheck(JNIEnv * env)604   static jboolean ExceptionCheck(JNIEnv* env) {
605     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
606   }
607 
ExceptionClear(JNIEnv * env)608   static void ExceptionClear(JNIEnv* env) {
609     ScopedObjectAccess soa(env);
610     soa.Self()->ClearException();
611   }
612 
ExceptionDescribe(JNIEnv * env)613   static void ExceptionDescribe(JNIEnv* env) {
614     ScopedObjectAccess soa(env);
615 
616     // If we have no exception to describe, pass through.
617     if (!soa.Self()->GetException()) {
618       return;
619     }
620 
621     StackHandleScope<1> hs(soa.Self());
622     Handle<mirror::Throwable> old_exception(
623         hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
624     soa.Self()->ClearException();
625     ScopedLocalRef<jthrowable> exception(env,
626                                          soa.AddLocalReference<jthrowable>(old_exception.Get()));
627     ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
628     jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
629     if (mid == nullptr) {
630       LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
631                    << mirror::Object::PrettyTypeOf(old_exception.Get());
632     } else {
633       env->CallVoidMethod(exception.get(), mid);
634       if (soa.Self()->IsExceptionPending()) {
635         LOG(WARNING) << "JNI WARNING: " << mirror::Object::PrettyTypeOf(soa.Self()->GetException())
636                      << " thrown while calling printStackTrace";
637         soa.Self()->ClearException();
638       }
639     }
640     soa.Self()->SetException(old_exception.Get());
641   }
642 
ExceptionOccurred(JNIEnv * env)643   static jthrowable ExceptionOccurred(JNIEnv* env) {
644     ScopedObjectAccess soa(env);
645     ObjPtr<mirror::Object> exception = soa.Self()->GetException();
646     return soa.AddLocalReference<jthrowable>(exception);
647   }
648 
FatalError(JNIEnv *,const char * msg)649   static void FatalError(JNIEnv*, const char* msg) {
650     LOG(FATAL) << "JNI FatalError called: " << msg;
651   }
652 
PushLocalFrame(JNIEnv * env,jint capacity)653   static jint PushLocalFrame(JNIEnv* env, jint capacity) {
654     // TODO: SOA may not be necessary but I do it to please lock annotations.
655     ScopedObjectAccess soa(env);
656     if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
657       return JNI_ERR;
658     }
659     down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
660     return JNI_OK;
661   }
662 
PopLocalFrame(JNIEnv * env,jobject java_survivor)663   static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
664     ScopedObjectAccess soa(env);
665     ObjPtr<mirror::Object> survivor = soa.Decode<mirror::Object>(java_survivor);
666     soa.Env()->PopFrame();
667     return soa.AddLocalReference<jobject>(survivor);
668   }
669 
EnsureLocalCapacity(JNIEnv * env,jint desired_capacity)670   static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
671     // TODO: SOA may not be necessary but I do it to please lock annotations.
672     ScopedObjectAccess soa(env);
673     return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
674   }
675 
NewGlobalRef(JNIEnv * env,jobject obj)676   static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
677     ScopedObjectAccess soa(env);
678     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
679     return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
680   }
681 
DeleteGlobalRef(JNIEnv * env,jobject obj)682   static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
683     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
684     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
685     vm->DeleteGlobalRef(self, obj);
686   }
687 
NewWeakGlobalRef(JNIEnv * env,jobject obj)688   static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
689     ScopedObjectAccess soa(env);
690     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
691     return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
692   }
693 
DeleteWeakGlobalRef(JNIEnv * env,jweak obj)694   static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
695     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
696     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
697     vm->DeleteWeakGlobalRef(self, obj);
698   }
699 
NewLocalRef(JNIEnv * env,jobject obj)700   static jobject NewLocalRef(JNIEnv* env, jobject obj) {
701     ScopedObjectAccess soa(env);
702     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
703     // Check for null after decoding the object to handle cleared weak globals.
704     if (decoded_obj == nullptr) {
705       return nullptr;
706     }
707     return soa.AddLocalReference<jobject>(decoded_obj);
708   }
709 
DeleteLocalRef(JNIEnv * env,jobject obj)710   static void DeleteLocalRef(JNIEnv* env, jobject obj) {
711     if (obj == nullptr) {
712       return;
713     }
714     // SOA is only necessary to have exclusion between GC root marking and removing.
715     // We don't want to have the GC attempt to mark a null root if we just removed
716     // it. b/22119403
717     ScopedObjectAccess soa(env);
718     auto* ext_env = down_cast<JNIEnvExt*>(env);
719     if (!ext_env->locals_.Remove(ext_env->local_ref_cookie_, obj)) {
720       // Attempting to delete a local reference that is not in the
721       // topmost local reference frame is a no-op.  DeleteLocalRef returns
722       // void and doesn't throw any exceptions, but we should probably
723       // complain about it so the user will notice that things aren't
724       // going quite the way they expect.
725       LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
726                    << "failed to find entry";
727     }
728   }
729 
IsSameObject(JNIEnv * env,jobject obj1,jobject obj2)730   static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
731     if (obj1 == obj2) {
732       return JNI_TRUE;
733     } else {
734       ScopedObjectAccess soa(env);
735       return (soa.Decode<mirror::Object>(obj1) == soa.Decode<mirror::Object>(obj2))
736               ? JNI_TRUE : JNI_FALSE;
737     }
738   }
739 
AllocObject(JNIEnv * env,jclass java_class)740   static jobject AllocObject(JNIEnv* env, jclass java_class) {
741     CHECK_NON_NULL_ARGUMENT(java_class);
742     ScopedObjectAccess soa(env);
743     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(java_class));
744     if (c == nullptr) {
745       return nullptr;
746     }
747     if (c->IsStringClass()) {
748       gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
749       return soa.AddLocalReference<jobject>(
750           mirror::String::AllocEmptyString(soa.Self(), allocator_type));
751     }
752     return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
753   }
754 
NewObject(JNIEnv * env,jclass java_class,jmethodID mid,...)755   static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
756     va_list args;
757     va_start(args, mid);
758     ScopedVAArgs free_args_later(&args);
759     CHECK_NON_NULL_ARGUMENT(java_class);
760     CHECK_NON_NULL_ARGUMENT(mid);
761     jobject result = NewObjectV(env, java_class, mid, args);
762     return result;
763   }
764 
NewObjectV(JNIEnv * env,jclass java_class,jmethodID mid,va_list args)765   static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
766     CHECK_NON_NULL_ARGUMENT(java_class);
767     CHECK_NON_NULL_ARGUMENT(mid);
768     ScopedObjectAccess soa(env);
769     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
770                                                 soa.Decode<mirror::Class>(java_class));
771     if (c == nullptr) {
772       return nullptr;
773     }
774     if (c->IsStringClass()) {
775       // Replace calls to String.<init> with equivalent StringFactory call.
776       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
777           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
778       return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
779     }
780     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
781     if (result == nullptr) {
782       return nullptr;
783     }
784     jobject local_result = soa.AddLocalReference<jobject>(result);
785     CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
786     if (soa.Self()->IsExceptionPending()) {
787       return nullptr;
788     }
789     return local_result;
790   }
791 
NewObjectA(JNIEnv * env,jclass java_class,jmethodID mid,const jvalue * args)792   static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, const jvalue* args) {
793     CHECK_NON_NULL_ARGUMENT(java_class);
794     CHECK_NON_NULL_ARGUMENT(mid);
795     ScopedObjectAccess soa(env);
796     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
797                                                 soa.Decode<mirror::Class>(java_class));
798     if (c == nullptr) {
799       return nullptr;
800     }
801     if (c->IsStringClass()) {
802       // Replace calls to String.<init> with equivalent StringFactory call.
803       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
804           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
805       return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
806     }
807     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
808     if (result == nullptr) {
809       return nullptr;
810     }
811     jobject local_result = soa.AddLocalReference<jobjectArray>(result);
812     CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
813     if (soa.Self()->IsExceptionPending()) {
814       return nullptr;
815     }
816     return local_result;
817   }
818 
GetMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)819   static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
820     CHECK_NON_NULL_ARGUMENT(java_class);
821     CHECK_NON_NULL_ARGUMENT(name);
822     CHECK_NON_NULL_ARGUMENT(sig);
823     ScopedObjectAccess soa(env);
824     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, false);
825   }
826 
GetStaticMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)827   static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
828                                      const char* sig) {
829     CHECK_NON_NULL_ARGUMENT(java_class);
830     CHECK_NON_NULL_ARGUMENT(name);
831     CHECK_NON_NULL_ARGUMENT(sig);
832     ScopedObjectAccess soa(env);
833     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, true);
834   }
835 
CallObjectMethod(JNIEnv * env,jobject obj,jmethodID mid,...)836   static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
837     va_list ap;
838     va_start(ap, mid);
839     ScopedVAArgs free_args_later(&ap);
840     CHECK_NON_NULL_ARGUMENT(obj);
841     CHECK_NON_NULL_ARGUMENT(mid);
842     ScopedObjectAccess soa(env);
843     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
844     return soa.AddLocalReference<jobject>(result.GetL());
845   }
846 
CallObjectMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)847   static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
848     CHECK_NON_NULL_ARGUMENT(obj);
849     CHECK_NON_NULL_ARGUMENT(mid);
850     ScopedObjectAccess soa(env);
851     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
852     return soa.AddLocalReference<jobject>(result.GetL());
853   }
854 
CallObjectMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)855   static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
856     CHECK_NON_NULL_ARGUMENT(obj);
857     CHECK_NON_NULL_ARGUMENT(mid);
858     ScopedObjectAccess soa(env);
859     JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
860     return soa.AddLocalReference<jobject>(result.GetL());
861   }
862 
CallBooleanMethod(JNIEnv * env,jobject obj,jmethodID mid,...)863   static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
864     va_list ap;
865     va_start(ap, mid);
866     ScopedVAArgs free_args_later(&ap);
867     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
868     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
869     ScopedObjectAccess soa(env);
870     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
871     return result.GetZ();
872   }
873 
CallBooleanMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)874   static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
875     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
876     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
877     ScopedObjectAccess soa(env);
878     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
879   }
880 
CallBooleanMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)881   static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
882     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
883     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
884     ScopedObjectAccess soa(env);
885     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
886   }
887 
CallByteMethod(JNIEnv * env,jobject obj,jmethodID mid,...)888   static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
889     va_list ap;
890     va_start(ap, mid);
891     ScopedVAArgs free_args_later(&ap);
892     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
893     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
894     ScopedObjectAccess soa(env);
895     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
896     return result.GetB();
897   }
898 
CallByteMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)899   static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
900     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
901     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
902     ScopedObjectAccess soa(env);
903     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
904   }
905 
CallByteMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)906   static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
907     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
908     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
909     ScopedObjectAccess soa(env);
910     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
911   }
912 
CallCharMethod(JNIEnv * env,jobject obj,jmethodID mid,...)913   static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
914     va_list ap;
915     va_start(ap, mid);
916     ScopedVAArgs free_args_later(&ap);
917     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
918     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
919     ScopedObjectAccess soa(env);
920     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
921     return result.GetC();
922   }
923 
CallCharMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)924   static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
925     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
926     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
927     ScopedObjectAccess soa(env);
928     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
929   }
930 
CallCharMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)931   static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
932     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
933     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
934     ScopedObjectAccess soa(env);
935     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
936   }
937 
CallDoubleMethod(JNIEnv * env,jobject obj,jmethodID mid,...)938   static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
939     va_list ap;
940     va_start(ap, mid);
941     ScopedVAArgs free_args_later(&ap);
942     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
943     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
944     ScopedObjectAccess soa(env);
945     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
946     return result.GetD();
947   }
948 
CallDoubleMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)949   static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
950     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
951     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
952     ScopedObjectAccess soa(env);
953     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
954   }
955 
CallDoubleMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)956   static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
957     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
958     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
959     ScopedObjectAccess soa(env);
960     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
961   }
962 
CallFloatMethod(JNIEnv * env,jobject obj,jmethodID mid,...)963   static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
964     va_list ap;
965     va_start(ap, mid);
966     ScopedVAArgs free_args_later(&ap);
967     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
968     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
969     ScopedObjectAccess soa(env);
970     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
971     return result.GetF();
972   }
973 
CallFloatMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)974   static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
975     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
976     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
977     ScopedObjectAccess soa(env);
978     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
979   }
980 
CallFloatMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)981   static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
982     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
983     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
984     ScopedObjectAccess soa(env);
985     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
986   }
987 
CallIntMethod(JNIEnv * env,jobject obj,jmethodID mid,...)988   static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
989     va_list ap;
990     va_start(ap, mid);
991     ScopedVAArgs free_args_later(&ap);
992     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
993     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
994     ScopedObjectAccess soa(env);
995     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
996     return result.GetI();
997   }
998 
CallIntMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)999   static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1000     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1001     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1002     ScopedObjectAccess soa(env);
1003     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
1004   }
1005 
CallIntMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1006   static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1007     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1008     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1009     ScopedObjectAccess soa(env);
1010     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
1011   }
1012 
CallLongMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1013   static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1014     va_list ap;
1015     va_start(ap, mid);
1016     ScopedVAArgs free_args_later(&ap);
1017     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1018     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1019     ScopedObjectAccess soa(env);
1020     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1021     return result.GetJ();
1022   }
1023 
CallLongMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1024   static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1025     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1026     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1027     ScopedObjectAccess soa(env);
1028     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
1029   }
1030 
CallLongMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1031   static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1032     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1033     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1034     ScopedObjectAccess soa(env);
1035     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
1036   }
1037 
CallShortMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1038   static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1039     va_list ap;
1040     va_start(ap, mid);
1041     ScopedVAArgs free_args_later(&ap);
1042     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1043     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1044     ScopedObjectAccess soa(env);
1045     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1046     return result.GetS();
1047   }
1048 
CallShortMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1049   static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1050     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1051     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1052     ScopedObjectAccess soa(env);
1053     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
1054   }
1055 
CallShortMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1056   static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1057     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1058     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1059     ScopedObjectAccess soa(env);
1060     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
1061   }
1062 
CallVoidMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1063   static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1064     va_list ap;
1065     va_start(ap, mid);
1066     ScopedVAArgs free_args_later(&ap);
1067     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1068     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1069     ScopedObjectAccess soa(env);
1070     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
1071   }
1072 
CallVoidMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1073   static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1074     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1075     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1076     ScopedObjectAccess soa(env);
1077     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
1078   }
1079 
CallVoidMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1080   static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1081     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1082     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1083     ScopedObjectAccess soa(env);
1084     InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
1085   }
1086 
CallNonvirtualObjectMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1087   static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1088     va_list ap;
1089     va_start(ap, mid);
1090     ScopedVAArgs free_args_later(&ap);
1091     CHECK_NON_NULL_ARGUMENT(obj);
1092     CHECK_NON_NULL_ARGUMENT(mid);
1093     ScopedObjectAccess soa(env);
1094     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1095     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1096     return local_result;
1097   }
1098 
CallNonvirtualObjectMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1099   static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1100                                              va_list args) {
1101     CHECK_NON_NULL_ARGUMENT(obj);
1102     CHECK_NON_NULL_ARGUMENT(mid);
1103     ScopedObjectAccess soa(env);
1104     JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1105     return soa.AddLocalReference<jobject>(result.GetL());
1106   }
1107 
CallNonvirtualObjectMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1108   static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1109                                              const jvalue* args) {
1110     CHECK_NON_NULL_ARGUMENT(obj);
1111     CHECK_NON_NULL_ARGUMENT(mid);
1112     ScopedObjectAccess soa(env);
1113     JValue result(InvokeWithJValues(soa, obj, mid, args));
1114     return soa.AddLocalReference<jobject>(result.GetL());
1115   }
1116 
CallNonvirtualBooleanMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1117   static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1118                                               ...) {
1119     va_list ap;
1120     va_start(ap, mid);
1121     ScopedVAArgs free_args_later(&ap);
1122     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1123     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1124     ScopedObjectAccess soa(env);
1125     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1126     return result.GetZ();
1127   }
1128 
CallNonvirtualBooleanMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1129   static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1130                                                va_list args) {
1131     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1132     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1133     ScopedObjectAccess soa(env);
1134     return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
1135   }
1136 
CallNonvirtualBooleanMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1137   static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1138                                                const jvalue* args) {
1139     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1140     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1141     ScopedObjectAccess soa(env);
1142     return InvokeWithJValues(soa, obj, mid, args).GetZ();
1143   }
1144 
CallNonvirtualByteMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1145   static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1146     va_list ap;
1147     va_start(ap, mid);
1148     ScopedVAArgs free_args_later(&ap);
1149     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1150     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1151     ScopedObjectAccess soa(env);
1152     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1153     return result.GetB();
1154   }
1155 
CallNonvirtualByteMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1156   static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1157                                          va_list args) {
1158     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1159     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1160     ScopedObjectAccess soa(env);
1161     return InvokeWithVarArgs(soa, obj, mid, args).GetB();
1162   }
1163 
CallNonvirtualByteMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1164   static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1165                                          const jvalue* args) {
1166     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1167     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1168     ScopedObjectAccess soa(env);
1169     return InvokeWithJValues(soa, obj, mid, args).GetB();
1170   }
1171 
CallNonvirtualCharMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1172   static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1173     va_list ap;
1174     va_start(ap, mid);
1175     ScopedVAArgs free_args_later(&ap);
1176     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1177     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1178     ScopedObjectAccess soa(env);
1179     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1180     return result.GetC();
1181   }
1182 
CallNonvirtualCharMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1183   static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1184                                          va_list args) {
1185     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1186     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1187     ScopedObjectAccess soa(env);
1188     return InvokeWithVarArgs(soa, obj, mid, args).GetC();
1189   }
1190 
CallNonvirtualCharMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1191   static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1192                                          const jvalue* args) {
1193     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1194     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1195     ScopedObjectAccess soa(env);
1196     return InvokeWithJValues(soa, obj, mid, args).GetC();
1197   }
1198 
CallNonvirtualShortMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1199   static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1200     va_list ap;
1201     va_start(ap, mid);
1202     ScopedVAArgs free_args_later(&ap);
1203     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1204     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1205     ScopedObjectAccess soa(env);
1206     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1207     return result.GetS();
1208   }
1209 
CallNonvirtualShortMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1210   static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1211                                            va_list args) {
1212     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1213     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1214     ScopedObjectAccess soa(env);
1215     return InvokeWithVarArgs(soa, obj, mid, args).GetS();
1216   }
1217 
CallNonvirtualShortMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1218   static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1219                                            const jvalue* args) {
1220     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1221     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1222     ScopedObjectAccess soa(env);
1223     return InvokeWithJValues(soa, obj, mid, args).GetS();
1224   }
1225 
CallNonvirtualIntMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1226   static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1227     va_list ap;
1228     va_start(ap, mid);
1229     ScopedVAArgs free_args_later(&ap);
1230     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1231     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1232     ScopedObjectAccess soa(env);
1233     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1234     return result.GetI();
1235   }
1236 
CallNonvirtualIntMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1237   static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1238                                        va_list args) {
1239     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1240     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1241     ScopedObjectAccess soa(env);
1242     return InvokeWithVarArgs(soa, obj, mid, args).GetI();
1243   }
1244 
CallNonvirtualIntMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1245   static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1246                                        const jvalue* args) {
1247     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1248     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1249     ScopedObjectAccess soa(env);
1250     return InvokeWithJValues(soa, obj, mid, args).GetI();
1251   }
1252 
CallNonvirtualLongMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1253   static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1254     va_list ap;
1255     va_start(ap, mid);
1256     ScopedVAArgs free_args_later(&ap);
1257     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1258     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1259     ScopedObjectAccess soa(env);
1260     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1261     return result.GetJ();
1262   }
1263 
CallNonvirtualLongMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1264   static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1265                                          va_list args) {
1266     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1267     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1268     ScopedObjectAccess soa(env);
1269     return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
1270   }
1271 
CallNonvirtualLongMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1272   static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1273                                          const jvalue* args) {
1274     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1275     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1276     ScopedObjectAccess soa(env);
1277     return InvokeWithJValues(soa, obj, mid, args).GetJ();
1278   }
1279 
CallNonvirtualFloatMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1280   static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1281     va_list ap;
1282     va_start(ap, mid);
1283     ScopedVAArgs free_args_later(&ap);
1284     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1285     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1286     ScopedObjectAccess soa(env);
1287     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1288     return result.GetF();
1289   }
1290 
CallNonvirtualFloatMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1291   static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1292                                            va_list args) {
1293     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1294     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1295     ScopedObjectAccess soa(env);
1296     return InvokeWithVarArgs(soa, obj, mid, args).GetF();
1297   }
1298 
CallNonvirtualFloatMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1299   static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1300                                            const jvalue* args) {
1301     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1302     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1303     ScopedObjectAccess soa(env);
1304     return InvokeWithJValues(soa, obj, mid, args).GetF();
1305   }
1306 
CallNonvirtualDoubleMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1307   static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1308     va_list ap;
1309     va_start(ap, mid);
1310     ScopedVAArgs free_args_later(&ap);
1311     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1312     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1313     ScopedObjectAccess soa(env);
1314     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1315     return result.GetD();
1316   }
1317 
CallNonvirtualDoubleMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1318   static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1319                                              va_list args) {
1320     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1321     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1322     ScopedObjectAccess soa(env);
1323     return InvokeWithVarArgs(soa, obj, mid, args).GetD();
1324   }
1325 
CallNonvirtualDoubleMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1326   static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1327                                              const jvalue* args) {
1328     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1329     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1330     ScopedObjectAccess soa(env);
1331     return InvokeWithJValues(soa, obj, mid, args).GetD();
1332   }
1333 
CallNonvirtualVoidMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1334   static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1335     va_list ap;
1336     va_start(ap, mid);
1337     ScopedVAArgs free_args_later(&ap);
1338     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1339     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1340     ScopedObjectAccess soa(env);
1341     InvokeWithVarArgs(soa, obj, mid, ap);
1342   }
1343 
CallNonvirtualVoidMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1344   static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1345                                         va_list args) {
1346     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1347     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1348     ScopedObjectAccess soa(env);
1349     InvokeWithVarArgs(soa, obj, mid, args);
1350   }
1351 
CallNonvirtualVoidMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1352   static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1353                                         const jvalue* args) {
1354     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1355     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1356     ScopedObjectAccess soa(env);
1357     InvokeWithJValues(soa, obj, mid, args);
1358   }
1359 
GetFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1360   static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1361     CHECK_NON_NULL_ARGUMENT(java_class);
1362     CHECK_NON_NULL_ARGUMENT(name);
1363     CHECK_NON_NULL_ARGUMENT(sig);
1364     ScopedObjectAccess soa(env);
1365     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, false);
1366   }
1367 
GetStaticFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1368   static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1369                                    const char* sig) {
1370     CHECK_NON_NULL_ARGUMENT(java_class);
1371     CHECK_NON_NULL_ARGUMENT(name);
1372     CHECK_NON_NULL_ARGUMENT(sig);
1373     ScopedObjectAccess soa(env);
1374     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, true);
1375   }
1376 
GetObjectField(JNIEnv * env,jobject obj,jfieldID fid)1377   static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
1378     CHECK_NON_NULL_ARGUMENT(obj);
1379     CHECK_NON_NULL_ARGUMENT(fid);
1380     ScopedObjectAccess soa(env);
1381     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1382     NotifyGetField(f, obj);
1383     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
1384     return soa.AddLocalReference<jobject>(f->GetObject(o));
1385   }
1386 
GetStaticObjectField(JNIEnv * env,jclass,jfieldID fid)1387   static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
1388     CHECK_NON_NULL_ARGUMENT(fid);
1389     ScopedObjectAccess soa(env);
1390     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1391     NotifyGetField(f, nullptr);
1392     return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
1393   }
1394 
SetObjectField(JNIEnv * env,jobject java_object,jfieldID fid,jobject java_value)1395   static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
1396     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1397     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1398     ScopedObjectAccess soa(env);
1399     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1400     NotifySetObjectField(f, java_object, java_value);
1401     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
1402     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1403     f->SetObject<false>(o, v);
1404   }
1405 
SetStaticObjectField(JNIEnv * env,jclass,jfieldID fid,jobject java_value)1406   static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
1407     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1408     ScopedObjectAccess soa(env);
1409     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1410     NotifySetObjectField(f, nullptr, java_value);
1411     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1412     f->SetObject<false>(f->GetDeclaringClass(), v);
1413   }
1414 
1415 #define GET_PRIMITIVE_FIELD(fn, instance) \
1416   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1417   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1418   ScopedObjectAccess soa(env); \
1419   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1420   NotifyGetField(f, instance); \
1421   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1422   return f->Get ##fn (o)
1423 
1424 #define GET_STATIC_PRIMITIVE_FIELD(fn) \
1425   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1426   ScopedObjectAccess soa(env); \
1427   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1428   NotifyGetField(f, nullptr); \
1429   return f->Get ##fn (f->GetDeclaringClass())
1430 
1431 #define SET_PRIMITIVE_FIELD(fn, instance, value) \
1432   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1433   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1434   ScopedObjectAccess soa(env); \
1435   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1436   NotifySetPrimitiveField(f, instance, JValue::FromPrimitive<decltype(value)>(value)); \
1437   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1438   f->Set ##fn <false>(o, value)
1439 
1440 #define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1441   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1442   ScopedObjectAccess soa(env); \
1443   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1444   NotifySetPrimitiveField(f, nullptr, JValue::FromPrimitive<decltype(value)>(value)); \
1445   f->Set ##fn <false>(f->GetDeclaringClass(), value)
1446 
GetBooleanField(JNIEnv * env,jobject obj,jfieldID fid)1447   static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1448     GET_PRIMITIVE_FIELD(Boolean, obj);
1449   }
1450 
GetByteField(JNIEnv * env,jobject obj,jfieldID fid)1451   static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1452     GET_PRIMITIVE_FIELD(Byte, obj);
1453   }
1454 
GetCharField(JNIEnv * env,jobject obj,jfieldID fid)1455   static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1456     GET_PRIMITIVE_FIELD(Char, obj);
1457   }
1458 
GetShortField(JNIEnv * env,jobject obj,jfieldID fid)1459   static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1460     GET_PRIMITIVE_FIELD(Short, obj);
1461   }
1462 
GetIntField(JNIEnv * env,jobject obj,jfieldID fid)1463   static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1464     GET_PRIMITIVE_FIELD(Int, obj);
1465   }
1466 
GetLongField(JNIEnv * env,jobject obj,jfieldID fid)1467   static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1468     GET_PRIMITIVE_FIELD(Long, obj);
1469   }
1470 
GetFloatField(JNIEnv * env,jobject obj,jfieldID fid)1471   static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1472     GET_PRIMITIVE_FIELD(Float, obj);
1473   }
1474 
GetDoubleField(JNIEnv * env,jobject obj,jfieldID fid)1475   static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1476     GET_PRIMITIVE_FIELD(Double, obj);
1477   }
1478 
GetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid)1479   static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
1480     GET_STATIC_PRIMITIVE_FIELD(Boolean);
1481   }
1482 
GetStaticByteField(JNIEnv * env,jclass,jfieldID fid)1483   static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
1484     GET_STATIC_PRIMITIVE_FIELD(Byte);
1485   }
1486 
GetStaticCharField(JNIEnv * env,jclass,jfieldID fid)1487   static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
1488     GET_STATIC_PRIMITIVE_FIELD(Char);
1489   }
1490 
GetStaticShortField(JNIEnv * env,jclass,jfieldID fid)1491   static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
1492     GET_STATIC_PRIMITIVE_FIELD(Short);
1493   }
1494 
GetStaticIntField(JNIEnv * env,jclass,jfieldID fid)1495   static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
1496     GET_STATIC_PRIMITIVE_FIELD(Int);
1497   }
1498 
GetStaticLongField(JNIEnv * env,jclass,jfieldID fid)1499   static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
1500     GET_STATIC_PRIMITIVE_FIELD(Long);
1501   }
1502 
GetStaticFloatField(JNIEnv * env,jclass,jfieldID fid)1503   static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
1504     GET_STATIC_PRIMITIVE_FIELD(Float);
1505   }
1506 
GetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid)1507   static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
1508     GET_STATIC_PRIMITIVE_FIELD(Double);
1509   }
1510 
SetBooleanField(JNIEnv * env,jobject obj,jfieldID fid,jboolean v)1511   static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1512     SET_PRIMITIVE_FIELD(Boolean, obj, v);
1513   }
1514 
SetByteField(JNIEnv * env,jobject obj,jfieldID fid,jbyte v)1515   static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1516     SET_PRIMITIVE_FIELD(Byte, obj, v);
1517   }
1518 
SetCharField(JNIEnv * env,jobject obj,jfieldID fid,jchar v)1519   static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1520     SET_PRIMITIVE_FIELD(Char, obj, v);
1521   }
1522 
SetFloatField(JNIEnv * env,jobject obj,jfieldID fid,jfloat v)1523   static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1524     SET_PRIMITIVE_FIELD(Float, obj, v);
1525   }
1526 
SetDoubleField(JNIEnv * env,jobject obj,jfieldID fid,jdouble v)1527   static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1528     SET_PRIMITIVE_FIELD(Double, obj, v);
1529   }
1530 
SetIntField(JNIEnv * env,jobject obj,jfieldID fid,jint v)1531   static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1532     SET_PRIMITIVE_FIELD(Int, obj, v);
1533   }
1534 
SetLongField(JNIEnv * env,jobject obj,jfieldID fid,jlong v)1535   static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1536     SET_PRIMITIVE_FIELD(Long, obj, v);
1537   }
1538 
SetShortField(JNIEnv * env,jobject obj,jfieldID fid,jshort v)1539   static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1540     SET_PRIMITIVE_FIELD(Short, obj, v);
1541   }
1542 
SetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid,jboolean v)1543   static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1544     SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
1545   }
1546 
SetStaticByteField(JNIEnv * env,jclass,jfieldID fid,jbyte v)1547   static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1548     SET_STATIC_PRIMITIVE_FIELD(Byte, v);
1549   }
1550 
SetStaticCharField(JNIEnv * env,jclass,jfieldID fid,jchar v)1551   static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1552     SET_STATIC_PRIMITIVE_FIELD(Char, v);
1553   }
1554 
SetStaticFloatField(JNIEnv * env,jclass,jfieldID fid,jfloat v)1555   static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1556     SET_STATIC_PRIMITIVE_FIELD(Float, v);
1557   }
1558 
SetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid,jdouble v)1559   static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1560     SET_STATIC_PRIMITIVE_FIELD(Double, v);
1561   }
1562 
SetStaticIntField(JNIEnv * env,jclass,jfieldID fid,jint v)1563   static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1564     SET_STATIC_PRIMITIVE_FIELD(Int, v);
1565   }
1566 
SetStaticLongField(JNIEnv * env,jclass,jfieldID fid,jlong v)1567   static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1568     SET_STATIC_PRIMITIVE_FIELD(Long, v);
1569   }
1570 
SetStaticShortField(JNIEnv * env,jclass,jfieldID fid,jshort v)1571   static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1572     SET_STATIC_PRIMITIVE_FIELD(Short, v);
1573   }
1574 
CallStaticObjectMethod(JNIEnv * env,jclass,jmethodID mid,...)1575   static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1576     va_list ap;
1577     va_start(ap, mid);
1578     ScopedVAArgs free_args_later(&ap);
1579     CHECK_NON_NULL_ARGUMENT(mid);
1580     ScopedObjectAccess soa(env);
1581     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1582     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1583     return local_result;
1584   }
1585 
CallStaticObjectMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1586   static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1587     CHECK_NON_NULL_ARGUMENT(mid);
1588     ScopedObjectAccess soa(env);
1589     JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
1590     return soa.AddLocalReference<jobject>(result.GetL());
1591   }
1592 
CallStaticObjectMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1593   static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1594     CHECK_NON_NULL_ARGUMENT(mid);
1595     ScopedObjectAccess soa(env);
1596     JValue result(InvokeWithJValues(soa, nullptr, mid, args));
1597     return soa.AddLocalReference<jobject>(result.GetL());
1598   }
1599 
CallStaticBooleanMethod(JNIEnv * env,jclass,jmethodID mid,...)1600   static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1601     va_list ap;
1602     va_start(ap, mid);
1603     ScopedVAArgs free_args_later(&ap);
1604     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1605     ScopedObjectAccess soa(env);
1606     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1607     return result.GetZ();
1608   }
1609 
CallStaticBooleanMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1610   static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1611     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1612     ScopedObjectAccess soa(env);
1613     return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
1614   }
1615 
CallStaticBooleanMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1616   static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1617     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1618     ScopedObjectAccess soa(env);
1619     return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
1620   }
1621 
CallStaticByteMethod(JNIEnv * env,jclass,jmethodID mid,...)1622   static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1623     va_list ap;
1624     va_start(ap, mid);
1625     ScopedVAArgs free_args_later(&ap);
1626     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1627     ScopedObjectAccess soa(env);
1628     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1629     return result.GetB();
1630   }
1631 
CallStaticByteMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1632   static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1633     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1634     ScopedObjectAccess soa(env);
1635     return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
1636   }
1637 
CallStaticByteMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1638   static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1639     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1640     ScopedObjectAccess soa(env);
1641     return InvokeWithJValues(soa, nullptr, mid, args).GetB();
1642   }
1643 
CallStaticCharMethod(JNIEnv * env,jclass,jmethodID mid,...)1644   static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1645     va_list ap;
1646     va_start(ap, mid);
1647     ScopedVAArgs free_args_later(&ap);
1648     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1649     ScopedObjectAccess soa(env);
1650     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1651     return result.GetC();
1652   }
1653 
CallStaticCharMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1654   static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1655     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1656     ScopedObjectAccess soa(env);
1657     return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
1658   }
1659 
CallStaticCharMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1660   static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1661     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1662     ScopedObjectAccess soa(env);
1663     return InvokeWithJValues(soa, nullptr, mid, args).GetC();
1664   }
1665 
CallStaticShortMethod(JNIEnv * env,jclass,jmethodID mid,...)1666   static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1667     va_list ap;
1668     va_start(ap, mid);
1669     ScopedVAArgs free_args_later(&ap);
1670     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1671     ScopedObjectAccess soa(env);
1672     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1673     return result.GetS();
1674   }
1675 
CallStaticShortMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1676   static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1677     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1678     ScopedObjectAccess soa(env);
1679     return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
1680   }
1681 
CallStaticShortMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1682   static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1683     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1684     ScopedObjectAccess soa(env);
1685     return InvokeWithJValues(soa, nullptr, mid, args).GetS();
1686   }
1687 
CallStaticIntMethod(JNIEnv * env,jclass,jmethodID mid,...)1688   static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1689     va_list ap;
1690     va_start(ap, mid);
1691     ScopedVAArgs free_args_later(&ap);
1692     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1693     ScopedObjectAccess soa(env);
1694     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1695     return result.GetI();
1696   }
1697 
CallStaticIntMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1698   static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1699     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1700     ScopedObjectAccess soa(env);
1701     return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
1702   }
1703 
CallStaticIntMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1704   static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1705     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1706     ScopedObjectAccess soa(env);
1707     return InvokeWithJValues(soa, nullptr, mid, args).GetI();
1708   }
1709 
CallStaticLongMethod(JNIEnv * env,jclass,jmethodID mid,...)1710   static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1711     va_list ap;
1712     va_start(ap, mid);
1713     ScopedVAArgs free_args_later(&ap);
1714     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1715     ScopedObjectAccess soa(env);
1716     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1717     return result.GetJ();
1718   }
1719 
CallStaticLongMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1720   static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1721     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1722     ScopedObjectAccess soa(env);
1723     return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
1724   }
1725 
CallStaticLongMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1726   static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1727     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1728     ScopedObjectAccess soa(env);
1729     return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
1730   }
1731 
CallStaticFloatMethod(JNIEnv * env,jclass,jmethodID mid,...)1732   static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1733     va_list ap;
1734     va_start(ap, mid);
1735     ScopedVAArgs free_args_later(&ap);
1736     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1737     ScopedObjectAccess soa(env);
1738     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1739     return result.GetF();
1740   }
1741 
CallStaticFloatMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1742   static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1743     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1744     ScopedObjectAccess soa(env);
1745     return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
1746   }
1747 
CallStaticFloatMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1748   static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1749     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1750     ScopedObjectAccess soa(env);
1751     return InvokeWithJValues(soa, nullptr, mid, args).GetF();
1752   }
1753 
CallStaticDoubleMethod(JNIEnv * env,jclass,jmethodID mid,...)1754   static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1755     va_list ap;
1756     va_start(ap, mid);
1757     ScopedVAArgs free_args_later(&ap);
1758     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1759     ScopedObjectAccess soa(env);
1760     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1761     return result.GetD();
1762   }
1763 
CallStaticDoubleMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1764   static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1765     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1766     ScopedObjectAccess soa(env);
1767     return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
1768   }
1769 
CallStaticDoubleMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1770   static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1771     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1772     ScopedObjectAccess soa(env);
1773     return InvokeWithJValues(soa, nullptr, mid, args).GetD();
1774   }
1775 
CallStaticVoidMethod(JNIEnv * env,jclass,jmethodID mid,...)1776   static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1777     va_list ap;
1778     va_start(ap, mid);
1779     ScopedVAArgs free_args_later(&ap);
1780     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1781     ScopedObjectAccess soa(env);
1782     InvokeWithVarArgs(soa, nullptr, mid, ap);
1783   }
1784 
CallStaticVoidMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1785   static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1786     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1787     ScopedObjectAccess soa(env);
1788     InvokeWithVarArgs(soa, nullptr, mid, args);
1789   }
1790 
CallStaticVoidMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1791   static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1792     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1793     ScopedObjectAccess soa(env);
1794     InvokeWithJValues(soa, nullptr, mid, args);
1795   }
1796 
NewString(JNIEnv * env,const jchar * chars,jsize char_count)1797   static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
1798     if (UNLIKELY(char_count < 0)) {
1799       JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
1800       return nullptr;
1801     }
1802     if (UNLIKELY(chars == nullptr && char_count > 0)) {
1803       JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
1804       return nullptr;
1805     }
1806     ScopedObjectAccess soa(env);
1807     ObjPtr<mirror::String> result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
1808     return soa.AddLocalReference<jstring>(result);
1809   }
1810 
NewStringUTF(JNIEnv * env,const char * utf)1811   static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1812     if (utf == nullptr) {
1813       return nullptr;
1814     }
1815     ScopedObjectAccess soa(env);
1816     ObjPtr<mirror::String> result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
1817     return soa.AddLocalReference<jstring>(result);
1818   }
1819 
GetStringLength(JNIEnv * env,jstring java_string)1820   static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1821     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1822     ScopedObjectAccess soa(env);
1823     return soa.Decode<mirror::String>(java_string)->GetLength();
1824   }
1825 
GetStringUTFLength(JNIEnv * env,jstring java_string)1826   static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1827     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1828     ScopedObjectAccess soa(env);
1829     return soa.Decode<mirror::String>(java_string)->GetUtfLength();
1830   }
1831 
GetStringRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,jchar * buf)1832   static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1833                               jchar* buf) {
1834     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1835     ScopedObjectAccess soa(env);
1836     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1837     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1838       ThrowSIOOBE(soa, start, length, s->GetLength());
1839     } else {
1840       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1841       if (s->IsCompressed()) {
1842         for (int i = 0; i < length; ++i) {
1843           buf[i] = static_cast<jchar>(s->CharAt(start+i));
1844         }
1845       } else {
1846         const jchar* chars = static_cast<jchar*>(s->GetValue());
1847         memcpy(buf, chars + start, length * sizeof(jchar));
1848       }
1849     }
1850   }
1851 
GetStringUTFRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,char * buf)1852   static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1853                                  char* buf) {
1854     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1855     ScopedObjectAccess soa(env);
1856     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1857     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1858       ThrowSIOOBE(soa, start, length, s->GetLength());
1859     } else {
1860       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1861       if (s->IsCompressed()) {
1862         for (int i = 0; i < length; ++i) {
1863           buf[i] = s->CharAt(start+i);
1864         }
1865       } else {
1866         const jchar* chars = s->GetValue();
1867         size_t bytes = CountUtf8Bytes(chars + start, length);
1868         ConvertUtf16ToModifiedUtf8(buf, bytes, chars + start, length);
1869       }
1870     }
1871   }
1872 
GetStringChars(JNIEnv * env,jstring java_string,jboolean * is_copy)1873   static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1874     CHECK_NON_NULL_ARGUMENT(java_string);
1875     ScopedObjectAccess soa(env);
1876     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1877     gc::Heap* heap = Runtime::Current()->GetHeap();
1878     if (heap->IsMovableObject(s) || s->IsCompressed()) {
1879       jchar* chars = new jchar[s->GetLength()];
1880       if (s->IsCompressed()) {
1881         int32_t length = s->GetLength();
1882         for (int i = 0; i < length; ++i) {
1883           chars[i] = s->CharAt(i);
1884         }
1885       } else {
1886         memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
1887       }
1888       if (is_copy != nullptr) {
1889         *is_copy = JNI_TRUE;
1890       }
1891       return chars;
1892     }
1893     if (is_copy != nullptr) {
1894       *is_copy = JNI_FALSE;
1895     }
1896     return static_cast<jchar*>(s->GetValue());
1897   }
1898 
ReleaseStringChars(JNIEnv * env,jstring java_string,const jchar * chars)1899   static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
1900     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1901     ScopedObjectAccess soa(env);
1902     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1903     if (s->IsCompressed() || (s->IsCompressed() == false && chars != s->GetValue())) {
1904       delete[] chars;
1905     }
1906   }
1907 
GetStringCritical(JNIEnv * env,jstring java_string,jboolean * is_copy)1908   static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1909     CHECK_NON_NULL_ARGUMENT(java_string);
1910     ScopedObjectAccess soa(env);
1911     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1912     gc::Heap* heap = Runtime::Current()->GetHeap();
1913     if (heap->IsMovableObject(s)) {
1914       StackHandleScope<1> hs(soa.Self());
1915       HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
1916       if (!kUseReadBarrier) {
1917         heap->IncrementDisableMovingGC(soa.Self());
1918       } else {
1919         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1920         // to occur thanks to the to-space invariant.
1921         heap->IncrementDisableThreadFlip(soa.Self());
1922       }
1923     }
1924     if (s->IsCompressed()) {
1925       if (is_copy != nullptr) {
1926         *is_copy = JNI_TRUE;
1927       }
1928       int32_t length = s->GetLength();
1929       jchar* chars = new jchar[length];
1930       for (int i = 0; i < length; ++i) {
1931         chars[i] = s->CharAt(i);
1932       }
1933       return chars;
1934     } else {
1935       if (is_copy != nullptr) {
1936         *is_copy = JNI_FALSE;
1937       }
1938       return static_cast<jchar*>(s->GetValue());
1939     }
1940   }
1941 
ReleaseStringCritical(JNIEnv * env,jstring java_string,const jchar * chars)1942   static void ReleaseStringCritical(JNIEnv* env,
1943                                     jstring java_string,
1944                                     const jchar* chars) {
1945     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1946     ScopedObjectAccess soa(env);
1947     gc::Heap* heap = Runtime::Current()->GetHeap();
1948     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1949     if (heap->IsMovableObject(s)) {
1950       if (!kUseReadBarrier) {
1951         heap->DecrementDisableMovingGC(soa.Self());
1952       } else {
1953         heap->DecrementDisableThreadFlip(soa.Self());
1954       }
1955     }
1956     if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
1957       delete[] chars;
1958     }
1959   }
1960 
GetStringUTFChars(JNIEnv * env,jstring java_string,jboolean * is_copy)1961   static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1962     if (java_string == nullptr) {
1963       return nullptr;
1964     }
1965     if (is_copy != nullptr) {
1966       *is_copy = JNI_TRUE;
1967     }
1968     ScopedObjectAccess soa(env);
1969     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1970     size_t byte_count = s->GetUtfLength();
1971     char* bytes = new char[byte_count + 1];
1972     CHECK(bytes != nullptr);  // bionic aborts anyway.
1973     if (s->IsCompressed()) {
1974       for (size_t i = 0; i < byte_count; ++i) {
1975         bytes[i] = s->CharAt(i);
1976       }
1977     } else {
1978       const uint16_t* chars = s->GetValue();
1979       ConvertUtf16ToModifiedUtf8(bytes, byte_count, chars, s->GetLength());
1980     }
1981     bytes[byte_count] = '\0';
1982     return bytes;
1983   }
1984 
ReleaseStringUTFChars(JNIEnv *,jstring,const char * chars)1985   static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
1986     delete[] chars;
1987   }
1988 
GetArrayLength(JNIEnv * env,jarray java_array)1989   static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
1990     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
1991     ScopedObjectAccess soa(env);
1992     ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_array);
1993     if (UNLIKELY(!obj->IsArrayInstance())) {
1994       soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", obj->PrettyTypeOf().c_str());
1995       return 0;
1996     }
1997     ObjPtr<mirror::Array> array = obj->AsArray();
1998     return array->GetLength();
1999   }
2000 
GetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index)2001   static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
2002     CHECK_NON_NULL_ARGUMENT(java_array);
2003     ScopedObjectAccess soa(env);
2004     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2005         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2006     return soa.AddLocalReference<jobject>(array->Get(index));
2007   }
2008 
SetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index,jobject java_value)2009   static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2010                                     jobject java_value) {
2011     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2012     ScopedObjectAccess soa(env);
2013     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2014         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2015     ObjPtr<mirror::Object> value = soa.Decode<mirror::Object>(java_value);
2016     array->Set<false>(index, value);
2017   }
2018 
NewBooleanArray(JNIEnv * env,jsize length)2019   static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2020     return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
2021   }
2022 
NewByteArray(JNIEnv * env,jsize length)2023   static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2024     return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
2025   }
2026 
NewCharArray(JNIEnv * env,jsize length)2027   static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2028     return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
2029   }
2030 
NewDoubleArray(JNIEnv * env,jsize length)2031   static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2032     return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
2033   }
2034 
NewFloatArray(JNIEnv * env,jsize length)2035   static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2036     return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
2037   }
2038 
NewIntArray(JNIEnv * env,jsize length)2039   static jintArray NewIntArray(JNIEnv* env, jsize length) {
2040     return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
2041   }
2042 
NewLongArray(JNIEnv * env,jsize length)2043   static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2044     return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
2045   }
2046 
NewObjectArray(JNIEnv * env,jsize length,jclass element_jclass,jobject initial_element)2047   static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2048                                      jobject initial_element) {
2049     if (UNLIKELY(length < 0)) {
2050       JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
2051       return nullptr;
2052     }
2053     CHECK_NON_NULL_ARGUMENT(element_jclass);
2054 
2055     // Compute the array class corresponding to the given element class.
2056     ScopedObjectAccess soa(env);
2057     ObjPtr<mirror::Class> array_class;
2058     {
2059       ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(element_jclass);
2060       if (UNLIKELY(element_class->IsPrimitive())) {
2061         soa.Vm()->JniAbortF("NewObjectArray",
2062                             "not an object type: %s",
2063                             element_class->PrettyDescriptor().c_str());
2064         return nullptr;
2065       }
2066       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2067       array_class = class_linker->FindArrayClass(soa.Self(), element_class);
2068       if (UNLIKELY(array_class == nullptr)) {
2069         return nullptr;
2070       }
2071     }
2072 
2073     // Allocate and initialize if necessary.
2074     ObjPtr<mirror::ObjectArray<mirror::Object>> result =
2075         mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
2076     if (result != nullptr && initial_element != nullptr) {
2077       ObjPtr<mirror::Object> initial_object = soa.Decode<mirror::Object>(initial_element);
2078       if (initial_object != nullptr) {
2079         ObjPtr<mirror::Class> element_class = result->GetClass()->GetComponentType();
2080         if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2081           soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
2082                               "element type of '%s'",
2083                               mirror::Class::PrettyDescriptor(initial_object->GetClass()).c_str(),
2084                               element_class->PrettyDescriptor().c_str());
2085           return nullptr;
2086         } else {
2087           for (jsize i = 0; i < length; ++i) {
2088             result->SetWithoutChecks<false>(i, initial_object);
2089           }
2090         }
2091       }
2092     }
2093     return soa.AddLocalReference<jobjectArray>(result);
2094   }
2095 
NewShortArray(JNIEnv * env,jsize length)2096   static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2097     return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
2098   }
2099 
GetPrimitiveArrayCritical(JNIEnv * env,jarray java_array,jboolean * is_copy)2100   static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
2101     CHECK_NON_NULL_ARGUMENT(java_array);
2102     ScopedObjectAccess soa(env);
2103     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2104     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2105       soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2106                           array->GetClass()->PrettyDescriptor().c_str());
2107       return nullptr;
2108     }
2109     gc::Heap* heap = Runtime::Current()->GetHeap();
2110     if (heap->IsMovableObject(array)) {
2111       if (!kUseReadBarrier) {
2112         heap->IncrementDisableMovingGC(soa.Self());
2113       } else {
2114         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2115         // to occur thanks to the to-space invariant.
2116         heap->IncrementDisableThreadFlip(soa.Self());
2117       }
2118       // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2119       array = soa.Decode<mirror::Array>(java_array);
2120     }
2121     if (is_copy != nullptr) {
2122       *is_copy = JNI_FALSE;
2123     }
2124     return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
2125   }
2126 
ReleasePrimitiveArrayCritical(JNIEnv * env,jarray java_array,void * elements,jint mode)2127   static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2128                                             jint mode) {
2129     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2130     ScopedObjectAccess soa(env);
2131     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2132     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2133       soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2134                           array->GetClass()->PrettyDescriptor().c_str());
2135       return;
2136     }
2137     const size_t component_size = array->GetClass()->GetComponentSize();
2138     ReleasePrimitiveArray(soa, array, component_size, elements, mode);
2139   }
2140 
GetBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * is_copy)2141   static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
2142     return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
2143   }
2144 
GetByteArrayElements(JNIEnv * env,jbyteArray array,jboolean * is_copy)2145   static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
2146     return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
2147   }
2148 
GetCharArrayElements(JNIEnv * env,jcharArray array,jboolean * is_copy)2149   static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
2150     return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
2151   }
2152 
GetDoubleArrayElements(JNIEnv * env,jdoubleArray array,jboolean * is_copy)2153   static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
2154     return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
2155   }
2156 
GetFloatArrayElements(JNIEnv * env,jfloatArray array,jboolean * is_copy)2157   static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
2158     return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
2159   }
2160 
GetIntArrayElements(JNIEnv * env,jintArray array,jboolean * is_copy)2161   static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
2162     return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
2163   }
2164 
GetLongArrayElements(JNIEnv * env,jlongArray array,jboolean * is_copy)2165   static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
2166     return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
2167   }
2168 
GetShortArrayElements(JNIEnv * env,jshortArray array,jboolean * is_copy)2169   static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
2170     return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
2171   }
2172 
ReleaseBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * elements,jint mode)2173   static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2174                                           jint mode) {
2175     ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2176                                                                          mode);
2177   }
2178 
ReleaseByteArrayElements(JNIEnv * env,jbyteArray array,jbyte * elements,jint mode)2179   static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2180     ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
2181   }
2182 
ReleaseCharArrayElements(JNIEnv * env,jcharArray array,jchar * elements,jint mode)2183   static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2184     ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
2185   }
2186 
ReleaseDoubleArrayElements(JNIEnv * env,jdoubleArray array,jdouble * elements,jint mode)2187   static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2188                                          jint mode) {
2189     ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
2190   }
2191 
ReleaseFloatArrayElements(JNIEnv * env,jfloatArray array,jfloat * elements,jint mode)2192   static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2193                                         jint mode) {
2194     ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
2195   }
2196 
ReleaseIntArrayElements(JNIEnv * env,jintArray array,jint * elements,jint mode)2197   static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2198     ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
2199   }
2200 
ReleaseLongArrayElements(JNIEnv * env,jlongArray array,jlong * elements,jint mode)2201   static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2202     ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
2203   }
2204 
ReleaseShortArrayElements(JNIEnv * env,jshortArray array,jshort * elements,jint mode)2205   static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2206                                         jint mode) {
2207     ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
2208   }
2209 
GetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,jboolean * buf)2210   static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2211                                     jboolean* buf) {
2212     GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2213                                                                            length, buf);
2214   }
2215 
GetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,jbyte * buf)2216   static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2217                                  jbyte* buf) {
2218     GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2219   }
2220 
GetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,jchar * buf)2221   static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2222                                  jchar* buf) {
2223     GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2224   }
2225 
GetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,jdouble * buf)2226   static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2227                                    jdouble* buf) {
2228     GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2229                                                                         buf);
2230   }
2231 
GetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,jfloat * buf)2232   static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2233                                   jfloat* buf) {
2234     GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2235                                                                      buf);
2236   }
2237 
GetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,jint * buf)2238   static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2239                                 jint* buf) {
2240     GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2241   }
2242 
GetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,jlong * buf)2243   static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2244                                  jlong* buf) {
2245     GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2246   }
2247 
GetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,jshort * buf)2248   static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2249                                   jshort* buf) {
2250     GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2251                                                                      buf);
2252   }
2253 
SetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,const jboolean * buf)2254   static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2255                                     const jboolean* buf) {
2256     SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2257                                                                            length, buf);
2258   }
2259 
SetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,const jbyte * buf)2260   static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2261                                  const jbyte* buf) {
2262     SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2263   }
2264 
SetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,const jchar * buf)2265   static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2266                                  const jchar* buf) {
2267     SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2268   }
2269 
SetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,const jdouble * buf)2270   static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2271                                    const jdouble* buf) {
2272     SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2273                                                                         buf);
2274   }
2275 
SetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,const jfloat * buf)2276   static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2277                                   const jfloat* buf) {
2278     SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2279                                                                      buf);
2280   }
2281 
SetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,const jint * buf)2282   static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2283                                 const jint* buf) {
2284     SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2285   }
2286 
SetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,const jlong * buf)2287   static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2288                                  const jlong* buf) {
2289     SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2290   }
2291 
SetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,const jshort * buf)2292   static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2293                                   const jshort* buf) {
2294     SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2295                                                                      buf);
2296   }
2297 
RegisterNatives(JNIEnv * env,jclass java_class,const JNINativeMethod * methods,jint method_count)2298   static jint RegisterNatives(JNIEnv* env,
2299                               jclass java_class,
2300                               const JNINativeMethod* methods,
2301                               jint method_count) {
2302     if (UNLIKELY(method_count < 0)) {
2303       JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2304                                        method_count);
2305       return JNI_ERR;  // Not reached except in unit tests.
2306     }
2307     CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
2308     ScopedObjectAccess soa(env);
2309     StackHandleScope<1> hs(soa.Self());
2310     Handle<mirror::Class> c = hs.NewHandle(soa.Decode<mirror::Class>(java_class));
2311     if (UNLIKELY(method_count == 0)) {
2312       LOG(WARNING) << "JNI RegisterNativeMethods: attempt to register 0 native methods for "
2313           << c->PrettyDescriptor();
2314       return JNI_OK;
2315     }
2316     CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", methods, JNI_ERR);
2317     for (jint i = 0; i < method_count; ++i) {
2318       const char* name = methods[i].name;
2319       const char* sig = methods[i].signature;
2320       const void* fnPtr = methods[i].fnPtr;
2321       if (UNLIKELY(name == nullptr)) {
2322         ReportInvalidJNINativeMethod(soa, c.Get(), "method name", i);
2323         return JNI_ERR;
2324       } else if (UNLIKELY(sig == nullptr)) {
2325         ReportInvalidJNINativeMethod(soa, c.Get(), "method signature", i);
2326         return JNI_ERR;
2327       } else if (UNLIKELY(fnPtr == nullptr)) {
2328         ReportInvalidJNINativeMethod(soa, c.Get(), "native function", i);
2329         return JNI_ERR;
2330       }
2331       bool is_fast = false;
2332       // Notes about fast JNI calls:
2333       //
2334       // On a normal JNI call, the calling thread usually transitions
2335       // from the kRunnable state to the kNative state. But if the
2336       // called native function needs to access any Java object, it
2337       // will have to transition back to the kRunnable state.
2338       //
2339       // There is a cost to this double transition. For a JNI call
2340       // that should be quick, this cost may dominate the call cost.
2341       //
2342       // On a fast JNI call, the calling thread avoids this double
2343       // transition by not transitioning from kRunnable to kNative and
2344       // stays in the kRunnable state.
2345       //
2346       // There are risks to using a fast JNI call because it can delay
2347       // a response to a thread suspension request which is typically
2348       // used for a GC root scanning, etc. If a fast JNI call takes a
2349       // long time, it could cause longer thread suspension latency
2350       // and GC pauses.
2351       //
2352       // Thus, fast JNI should be used with care. It should be used
2353       // for a JNI call that takes a short amount of time (eg. no
2354       // long-running loop) and does not block (eg. no locks, I/O,
2355       // etc.)
2356       //
2357       // A '!' prefix in the signature in the JNINativeMethod
2358       // indicates that it's a fast JNI call and the runtime omits the
2359       // thread state transition from kRunnable to kNative at the
2360       // entry.
2361       if (*sig == '!') {
2362         is_fast = true;
2363         ++sig;
2364       }
2365 
2366       // Note: the right order is to try to find the method locally
2367       // first, either as a direct or a virtual method. Then move to
2368       // the parent.
2369       ArtMethod* m = nullptr;
2370       bool warn_on_going_to_parent = down_cast<JNIEnvExt*>(env)->GetVm()->IsCheckJniEnabled();
2371       for (ObjPtr<mirror::Class> current_class = c.Get();
2372            current_class != nullptr;
2373            current_class = current_class->GetSuperClass()) {
2374         // Search first only comparing methods which are native.
2375         m = FindMethod<true>(current_class, name, sig);
2376         if (m != nullptr) {
2377           break;
2378         }
2379 
2380         // Search again comparing to all methods, to find non-native methods that match.
2381         m = FindMethod<false>(current_class, name, sig);
2382         if (m != nullptr) {
2383           break;
2384         }
2385 
2386         if (warn_on_going_to_parent) {
2387           LOG(WARNING) << "CheckJNI: method to register \"" << name << "\" not in the given class. "
2388                        << "This is slow, consider changing your RegisterNatives calls.";
2389           warn_on_going_to_parent = false;
2390         }
2391       }
2392 
2393       if (m == nullptr) {
2394         c->DumpClass(LOG_STREAM(ERROR), mirror::Class::kDumpClassFullDetail);
2395         LOG(ERROR)
2396             << "Failed to register native method "
2397             << c->PrettyDescriptor() << "." << name << sig << " in "
2398             << c->GetDexCache()->GetLocation()->ToModifiedUtf8();
2399         ThrowNoSuchMethodError(soa, c.Get(), name, sig, "static or non-static");
2400         return JNI_ERR;
2401       } else if (!m->IsNative()) {
2402         LOG(ERROR)
2403             << "Failed to register non-native method "
2404             << c->PrettyDescriptor() << "." << name << sig
2405             << " as native";
2406         ThrowNoSuchMethodError(soa, c.Get(), name, sig, "native");
2407         return JNI_ERR;
2408       }
2409 
2410       VLOG(jni) << "[Registering JNI native method " << m->PrettyMethod() << "]";
2411 
2412       if (UNLIKELY(is_fast)) {
2413         // There are a few reasons to switch:
2414         // 1) We don't support !bang JNI anymore, it will turn to a hard error later.
2415         // 2) @FastNative is actually faster. At least 1.5x faster than !bang JNI.
2416         //    and switching is super easy, remove ! in C code, add annotation in .java code.
2417         // 3) Good chance of hitting DCHECK failures in ScopedFastNativeObjectAccess
2418         //    since that checks for presence of @FastNative and not for ! in the descriptor.
2419         LOG(WARNING) << "!bang JNI is deprecated. Switch to @FastNative for " << m->PrettyMethod();
2420         is_fast = false;
2421         // TODO: make this a hard register error in the future.
2422       }
2423 
2424       const void* final_function_ptr = m->RegisterNative(fnPtr);
2425       UNUSED(final_function_ptr);
2426     }
2427     return JNI_OK;
2428   }
2429 
UnregisterNatives(JNIEnv * env,jclass java_class)2430   static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
2431     CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_ERR);
2432     ScopedObjectAccess soa(env);
2433     ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
2434 
2435     VLOG(jni) << "[Unregistering JNI native methods for " << mirror::Class::PrettyClass(c) << "]";
2436 
2437     size_t unregistered_count = 0;
2438     auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2439     for (auto& m : c->GetMethods(pointer_size)) {
2440       if (m.IsNative()) {
2441         m.UnregisterNative();
2442         unregistered_count++;
2443       }
2444     }
2445 
2446     if (unregistered_count == 0) {
2447       LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2448           << mirror::Class::PrettyDescriptor(c) << "' that contains no native methods";
2449     }
2450     return JNI_OK;
2451   }
2452 
MonitorEnter(JNIEnv * env,jobject java_object)2453   static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2454     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2455     ScopedObjectAccess soa(env);
2456     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2457     o = o->MonitorEnter(soa.Self());
2458     if (soa.Self()->HoldsLock(o)) {
2459       soa.Env()->monitors_.Add(o);
2460     }
2461     if (soa.Self()->IsExceptionPending()) {
2462       return JNI_ERR;
2463     }
2464     return JNI_OK;
2465   }
2466 
MonitorExit(JNIEnv * env,jobject java_object)2467   static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2468     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2469     ScopedObjectAccess soa(env);
2470     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2471     bool remove_mon = soa.Self()->HoldsLock(o);
2472     o->MonitorExit(soa.Self());
2473     if (remove_mon) {
2474       soa.Env()->monitors_.Remove(o);
2475     }
2476     if (soa.Self()->IsExceptionPending()) {
2477       return JNI_ERR;
2478     }
2479     return JNI_OK;
2480   }
2481 
GetJavaVM(JNIEnv * env,JavaVM ** vm)2482   static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2483     CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
2484     Runtime* runtime = Runtime::Current();
2485     if (runtime != nullptr) {
2486       *vm = runtime->GetJavaVM();
2487     } else {
2488       *vm = nullptr;
2489     }
2490     return (*vm != nullptr) ? JNI_OK : JNI_ERR;
2491   }
2492 
NewDirectByteBuffer(JNIEnv * env,void * address,jlong capacity)2493   static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2494     if (capacity < 0) {
2495       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2496                                        capacity);
2497       return nullptr;
2498     }
2499     if (address == nullptr && capacity != 0) {
2500       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2501                                        "non-zero capacity for nullptr pointer: %" PRId64, capacity);
2502       return nullptr;
2503     }
2504 
2505     // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
2506     if (capacity > INT_MAX) {
2507       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2508                                        "buffer capacity greater than maximum jint: %" PRId64,
2509                                        capacity);
2510       return nullptr;
2511     }
2512     jlong address_arg = reinterpret_cast<jlong>(address);
2513     jint capacity_arg = static_cast<jint>(capacity);
2514 
2515     jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2516                                     WellKnownClasses::java_nio_DirectByteBuffer_init,
2517                                     address_arg, capacity_arg);
2518     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? nullptr : result;
2519   }
2520 
GetDirectBufferAddress(JNIEnv * env,jobject java_buffer)2521   static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
2522     return reinterpret_cast<void*>(env->GetLongField(
2523         java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
2524   }
2525 
GetDirectBufferCapacity(JNIEnv * env,jobject java_buffer)2526   static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
2527     return static_cast<jlong>(env->GetIntField(
2528         java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
2529   }
2530 
GetObjectRefType(JNIEnv * env ATTRIBUTE_UNUSED,jobject java_object)2531   static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2532     if (java_object == nullptr) {
2533       return JNIInvalidRefType;
2534     }
2535 
2536     // Do we definitely know what kind of reference this is?
2537     IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2538     IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
2539     switch (kind) {
2540     case kLocal:
2541       return JNILocalRefType;
2542     case kGlobal:
2543       return JNIGlobalRefType;
2544     case kWeakGlobal:
2545       return JNIWeakGlobalRefType;
2546     case kHandleScopeOrInvalid:
2547       // Assume value is in a handle scope.
2548       return JNILocalRefType;
2549     }
2550     LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2551     UNREACHABLE();
2552   }
2553 
2554  private:
EnsureLocalCapacityInternal(ScopedObjectAccess & soa,jint desired_capacity,const char * caller)2555   static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2556                                           const char* caller)
2557       REQUIRES_SHARED(Locks::mutator_lock_) {
2558     if (desired_capacity < 0) {
2559       LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2560       return JNI_ERR;
2561     }
2562 
2563     std::string error_msg;
2564     if (!soa.Env()->locals_.EnsureFreeCapacity(static_cast<size_t>(desired_capacity), &error_msg)) {
2565       std::string caller_error = android::base::StringPrintf("%s: %s", caller, error_msg.c_str());
2566       soa.Self()->ThrowOutOfMemoryError(caller_error.c_str());
2567       return JNI_ERR;
2568     }
2569     return JNI_OK;
2570   }
2571 
2572   template<typename JniT, typename ArtT>
NewPrimitiveArray(JNIEnv * env,jsize length)2573   static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
2574     ScopedObjectAccess soa(env);
2575     if (UNLIKELY(length < 0)) {
2576       soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2577       return nullptr;
2578     }
2579     ObjPtr<ArtT> result = ArtT::Alloc(soa.Self(), length);
2580     return soa.AddLocalReference<JniT>(result);
2581   }
2582 
2583   template <typename JArrayT, typename ElementT, typename ArtArrayT>
DecodeAndCheckArrayType(ScopedObjectAccess & soa,JArrayT java_array,const char * fn_name,const char * operation)2584   static ObjPtr<ArtArrayT> DecodeAndCheckArrayType(ScopedObjectAccess& soa,
2585                                                    JArrayT java_array,
2586                                                    const char* fn_name,
2587                                                    const char* operation)
2588       REQUIRES_SHARED(Locks::mutator_lock_) {
2589     ObjPtr<ArtArrayT> array = soa.Decode<ArtArrayT>(java_array);
2590     ObjPtr<mirror::Class> expected_array_class = GetClassRoot<ArtArrayT>();
2591     if (UNLIKELY(expected_array_class != array->GetClass())) {
2592       soa.Vm()->JniAbortF(fn_name,
2593                           "attempt to %s %s primitive array elements with an object of type %s",
2594                           operation,
2595                           mirror::Class::PrettyDescriptor(
2596                               expected_array_class->GetComponentType()).c_str(),
2597                           mirror::Class::PrettyDescriptor(array->GetClass()).c_str());
2598       return nullptr;
2599     }
2600     DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2601     return array;
2602   }
2603 
2604   template <typename ArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArray(JNIEnv * env,ArrayT java_array,jboolean * is_copy)2605   static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2606     CHECK_NON_NULL_ARGUMENT(java_array);
2607     ScopedObjectAccess soa(env);
2608     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2609         soa, java_array, "GetArrayElements", "get");
2610     if (UNLIKELY(array == nullptr)) {
2611       return nullptr;
2612     }
2613     // Only make a copy if necessary.
2614     if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2615       if (is_copy != nullptr) {
2616         *is_copy = JNI_TRUE;
2617       }
2618       const size_t component_size = sizeof(ElementT);
2619       size_t size = array->GetLength() * component_size;
2620       void* data = new uint64_t[RoundUp(size, 8) / 8];
2621       memcpy(data, array->GetData(), size);
2622       return reinterpret_cast<ElementT*>(data);
2623     } else {
2624       if (is_copy != nullptr) {
2625         *is_copy = JNI_FALSE;
2626       }
2627       return reinterpret_cast<ElementT*>(array->GetData());
2628     }
2629   }
2630 
2631   template <typename ArrayT, typename ElementT, typename ArtArrayT>
ReleasePrimitiveArray(JNIEnv * env,ArrayT java_array,ElementT * elements,jint mode)2632   static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2633     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2634     ScopedObjectAccess soa(env);
2635     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2636         soa, java_array, "ReleaseArrayElements", "release");
2637     if (array == nullptr) {
2638       return;
2639     }
2640     ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2641   }
2642 
ReleasePrimitiveArray(ScopedObjectAccess & soa,ObjPtr<mirror::Array> array,size_t component_size,void * elements,jint mode)2643   static void ReleasePrimitiveArray(ScopedObjectAccess& soa,
2644                                     ObjPtr<mirror::Array> array,
2645                                     size_t component_size,
2646                                     void* elements,
2647                                     jint mode)
2648       REQUIRES_SHARED(Locks::mutator_lock_) {
2649     void* array_data = array->GetRawData(component_size, 0);
2650     gc::Heap* heap = Runtime::Current()->GetHeap();
2651     bool is_copy = array_data != elements;
2652     size_t bytes = array->GetLength() * component_size;
2653     if (is_copy) {
2654       // Sanity check: If elements is not the same as the java array's data, it better not be a
2655       // heap address. TODO: This might be slow to check, may be worth keeping track of which
2656       // copies we make?
2657       if (heap->IsNonDiscontinuousSpaceHeapAddress(elements)) {
2658         soa.Vm()->JniAbortF("ReleaseArrayElements",
2659                             "invalid element pointer %p, array elements are %p",
2660                             reinterpret_cast<void*>(elements), array_data);
2661         return;
2662       }
2663       if (mode != JNI_ABORT) {
2664         memcpy(array_data, elements, bytes);
2665       } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2666         // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2667         LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2668         soa.Self()->DumpJavaStack(LOG_STREAM(WARNING));
2669       }
2670     }
2671     if (mode != JNI_COMMIT) {
2672       if (is_copy) {
2673         delete[] reinterpret_cast<uint64_t*>(elements);
2674       } else if (heap->IsMovableObject(array)) {
2675         // Non copy to a movable object must means that we had disabled the moving GC.
2676         if (!kUseReadBarrier) {
2677           heap->DecrementDisableMovingGC(soa.Self());
2678         } else {
2679           heap->DecrementDisableThreadFlip(soa.Self());
2680         }
2681       }
2682     }
2683   }
2684 
2685   template <typename JArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,ElementT * buf)2686   static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2687                                       jsize start, jsize length, ElementT* buf) {
2688     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2689     ScopedObjectAccess soa(env);
2690     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2691         soa, java_array, "GetPrimitiveArrayRegion", "get region of");
2692     if (array != nullptr) {
2693       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2694         ThrowAIOOBE(soa, array, start, length, "src");
2695       } else {
2696         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2697         ElementT* data = array->GetData();
2698         memcpy(buf, data + start, length * sizeof(ElementT));
2699       }
2700     }
2701   }
2702 
2703   template <typename JArrayT, typename ElementT, typename ArtArrayT>
SetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,const ElementT * buf)2704   static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2705                                       jsize start, jsize length, const ElementT* buf) {
2706     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2707     ScopedObjectAccess soa(env);
2708     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2709         soa, java_array, "SetPrimitiveArrayRegion", "set region of");
2710     if (array != nullptr) {
2711       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2712         ThrowAIOOBE(soa, array, start, length, "dst");
2713       } else {
2714         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2715         ElementT* data = array->GetData();
2716         memcpy(data + start, buf, length * sizeof(ElementT));
2717       }
2718     }
2719   }
2720 };
2721 
2722 template<bool kEnableIndexIds>
2723 struct JniNativeInterfaceFunctions {
2724   using JNIImpl = JNI<kEnableIndexIds>;
2725   static constexpr JNINativeInterface gJniNativeInterface = {
2726     nullptr,  // reserved0.
2727     nullptr,  // reserved1.
2728     nullptr,  // reserved2.
2729     nullptr,  // reserved3.
2730     JNIImpl::GetVersion,
2731     JNIImpl::DefineClass,
2732     JNIImpl::FindClass,
2733     JNIImpl::FromReflectedMethod,
2734     JNIImpl::FromReflectedField,
2735     JNIImpl::ToReflectedMethod,
2736     JNIImpl::GetSuperclass,
2737     JNIImpl::IsAssignableFrom,
2738     JNIImpl::ToReflectedField,
2739     JNIImpl::Throw,
2740     JNIImpl::ThrowNew,
2741     JNIImpl::ExceptionOccurred,
2742     JNIImpl::ExceptionDescribe,
2743     JNIImpl::ExceptionClear,
2744     JNIImpl::FatalError,
2745     JNIImpl::PushLocalFrame,
2746     JNIImpl::PopLocalFrame,
2747     JNIImpl::NewGlobalRef,
2748     JNIImpl::DeleteGlobalRef,
2749     JNIImpl::DeleteLocalRef,
2750     JNIImpl::IsSameObject,
2751     JNIImpl::NewLocalRef,
2752     JNIImpl::EnsureLocalCapacity,
2753     JNIImpl::AllocObject,
2754     JNIImpl::NewObject,
2755     JNIImpl::NewObjectV,
2756     JNIImpl::NewObjectA,
2757     JNIImpl::GetObjectClass,
2758     JNIImpl::IsInstanceOf,
2759     JNIImpl::GetMethodID,
2760     JNIImpl::CallObjectMethod,
2761     JNIImpl::CallObjectMethodV,
2762     JNIImpl::CallObjectMethodA,
2763     JNIImpl::CallBooleanMethod,
2764     JNIImpl::CallBooleanMethodV,
2765     JNIImpl::CallBooleanMethodA,
2766     JNIImpl::CallByteMethod,
2767     JNIImpl::CallByteMethodV,
2768     JNIImpl::CallByteMethodA,
2769     JNIImpl::CallCharMethod,
2770     JNIImpl::CallCharMethodV,
2771     JNIImpl::CallCharMethodA,
2772     JNIImpl::CallShortMethod,
2773     JNIImpl::CallShortMethodV,
2774     JNIImpl::CallShortMethodA,
2775     JNIImpl::CallIntMethod,
2776     JNIImpl::CallIntMethodV,
2777     JNIImpl::CallIntMethodA,
2778     JNIImpl::CallLongMethod,
2779     JNIImpl::CallLongMethodV,
2780     JNIImpl::CallLongMethodA,
2781     JNIImpl::CallFloatMethod,
2782     JNIImpl::CallFloatMethodV,
2783     JNIImpl::CallFloatMethodA,
2784     JNIImpl::CallDoubleMethod,
2785     JNIImpl::CallDoubleMethodV,
2786     JNIImpl::CallDoubleMethodA,
2787     JNIImpl::CallVoidMethod,
2788     JNIImpl::CallVoidMethodV,
2789     JNIImpl::CallVoidMethodA,
2790     JNIImpl::CallNonvirtualObjectMethod,
2791     JNIImpl::CallNonvirtualObjectMethodV,
2792     JNIImpl::CallNonvirtualObjectMethodA,
2793     JNIImpl::CallNonvirtualBooleanMethod,
2794     JNIImpl::CallNonvirtualBooleanMethodV,
2795     JNIImpl::CallNonvirtualBooleanMethodA,
2796     JNIImpl::CallNonvirtualByteMethod,
2797     JNIImpl::CallNonvirtualByteMethodV,
2798     JNIImpl::CallNonvirtualByteMethodA,
2799     JNIImpl::CallNonvirtualCharMethod,
2800     JNIImpl::CallNonvirtualCharMethodV,
2801     JNIImpl::CallNonvirtualCharMethodA,
2802     JNIImpl::CallNonvirtualShortMethod,
2803     JNIImpl::CallNonvirtualShortMethodV,
2804     JNIImpl::CallNonvirtualShortMethodA,
2805     JNIImpl::CallNonvirtualIntMethod,
2806     JNIImpl::CallNonvirtualIntMethodV,
2807     JNIImpl::CallNonvirtualIntMethodA,
2808     JNIImpl::CallNonvirtualLongMethod,
2809     JNIImpl::CallNonvirtualLongMethodV,
2810     JNIImpl::CallNonvirtualLongMethodA,
2811     JNIImpl::CallNonvirtualFloatMethod,
2812     JNIImpl::CallNonvirtualFloatMethodV,
2813     JNIImpl::CallNonvirtualFloatMethodA,
2814     JNIImpl::CallNonvirtualDoubleMethod,
2815     JNIImpl::CallNonvirtualDoubleMethodV,
2816     JNIImpl::CallNonvirtualDoubleMethodA,
2817     JNIImpl::CallNonvirtualVoidMethod,
2818     JNIImpl::CallNonvirtualVoidMethodV,
2819     JNIImpl::CallNonvirtualVoidMethodA,
2820     JNIImpl::GetFieldID,
2821     JNIImpl::GetObjectField,
2822     JNIImpl::GetBooleanField,
2823     JNIImpl::GetByteField,
2824     JNIImpl::GetCharField,
2825     JNIImpl::GetShortField,
2826     JNIImpl::GetIntField,
2827     JNIImpl::GetLongField,
2828     JNIImpl::GetFloatField,
2829     JNIImpl::GetDoubleField,
2830     JNIImpl::SetObjectField,
2831     JNIImpl::SetBooleanField,
2832     JNIImpl::SetByteField,
2833     JNIImpl::SetCharField,
2834     JNIImpl::SetShortField,
2835     JNIImpl::SetIntField,
2836     JNIImpl::SetLongField,
2837     JNIImpl::SetFloatField,
2838     JNIImpl::SetDoubleField,
2839     JNIImpl::GetStaticMethodID,
2840     JNIImpl::CallStaticObjectMethod,
2841     JNIImpl::CallStaticObjectMethodV,
2842     JNIImpl::CallStaticObjectMethodA,
2843     JNIImpl::CallStaticBooleanMethod,
2844     JNIImpl::CallStaticBooleanMethodV,
2845     JNIImpl::CallStaticBooleanMethodA,
2846     JNIImpl::CallStaticByteMethod,
2847     JNIImpl::CallStaticByteMethodV,
2848     JNIImpl::CallStaticByteMethodA,
2849     JNIImpl::CallStaticCharMethod,
2850     JNIImpl::CallStaticCharMethodV,
2851     JNIImpl::CallStaticCharMethodA,
2852     JNIImpl::CallStaticShortMethod,
2853     JNIImpl::CallStaticShortMethodV,
2854     JNIImpl::CallStaticShortMethodA,
2855     JNIImpl::CallStaticIntMethod,
2856     JNIImpl::CallStaticIntMethodV,
2857     JNIImpl::CallStaticIntMethodA,
2858     JNIImpl::CallStaticLongMethod,
2859     JNIImpl::CallStaticLongMethodV,
2860     JNIImpl::CallStaticLongMethodA,
2861     JNIImpl::CallStaticFloatMethod,
2862     JNIImpl::CallStaticFloatMethodV,
2863     JNIImpl::CallStaticFloatMethodA,
2864     JNIImpl::CallStaticDoubleMethod,
2865     JNIImpl::CallStaticDoubleMethodV,
2866     JNIImpl::CallStaticDoubleMethodA,
2867     JNIImpl::CallStaticVoidMethod,
2868     JNIImpl::CallStaticVoidMethodV,
2869     JNIImpl::CallStaticVoidMethodA,
2870     JNIImpl::GetStaticFieldID,
2871     JNIImpl::GetStaticObjectField,
2872     JNIImpl::GetStaticBooleanField,
2873     JNIImpl::GetStaticByteField,
2874     JNIImpl::GetStaticCharField,
2875     JNIImpl::GetStaticShortField,
2876     JNIImpl::GetStaticIntField,
2877     JNIImpl::GetStaticLongField,
2878     JNIImpl::GetStaticFloatField,
2879     JNIImpl::GetStaticDoubleField,
2880     JNIImpl::SetStaticObjectField,
2881     JNIImpl::SetStaticBooleanField,
2882     JNIImpl::SetStaticByteField,
2883     JNIImpl::SetStaticCharField,
2884     JNIImpl::SetStaticShortField,
2885     JNIImpl::SetStaticIntField,
2886     JNIImpl::SetStaticLongField,
2887     JNIImpl::SetStaticFloatField,
2888     JNIImpl::SetStaticDoubleField,
2889     JNIImpl::NewString,
2890     JNIImpl::GetStringLength,
2891     JNIImpl::GetStringChars,
2892     JNIImpl::ReleaseStringChars,
2893     JNIImpl::NewStringUTF,
2894     JNIImpl::GetStringUTFLength,
2895     JNIImpl::GetStringUTFChars,
2896     JNIImpl::ReleaseStringUTFChars,
2897     JNIImpl::GetArrayLength,
2898     JNIImpl::NewObjectArray,
2899     JNIImpl::GetObjectArrayElement,
2900     JNIImpl::SetObjectArrayElement,
2901     JNIImpl::NewBooleanArray,
2902     JNIImpl::NewByteArray,
2903     JNIImpl::NewCharArray,
2904     JNIImpl::NewShortArray,
2905     JNIImpl::NewIntArray,
2906     JNIImpl::NewLongArray,
2907     JNIImpl::NewFloatArray,
2908     JNIImpl::NewDoubleArray,
2909     JNIImpl::GetBooleanArrayElements,
2910     JNIImpl::GetByteArrayElements,
2911     JNIImpl::GetCharArrayElements,
2912     JNIImpl::GetShortArrayElements,
2913     JNIImpl::GetIntArrayElements,
2914     JNIImpl::GetLongArrayElements,
2915     JNIImpl::GetFloatArrayElements,
2916     JNIImpl::GetDoubleArrayElements,
2917     JNIImpl::ReleaseBooleanArrayElements,
2918     JNIImpl::ReleaseByteArrayElements,
2919     JNIImpl::ReleaseCharArrayElements,
2920     JNIImpl::ReleaseShortArrayElements,
2921     JNIImpl::ReleaseIntArrayElements,
2922     JNIImpl::ReleaseLongArrayElements,
2923     JNIImpl::ReleaseFloatArrayElements,
2924     JNIImpl::ReleaseDoubleArrayElements,
2925     JNIImpl::GetBooleanArrayRegion,
2926     JNIImpl::GetByteArrayRegion,
2927     JNIImpl::GetCharArrayRegion,
2928     JNIImpl::GetShortArrayRegion,
2929     JNIImpl::GetIntArrayRegion,
2930     JNIImpl::GetLongArrayRegion,
2931     JNIImpl::GetFloatArrayRegion,
2932     JNIImpl::GetDoubleArrayRegion,
2933     JNIImpl::SetBooleanArrayRegion,
2934     JNIImpl::SetByteArrayRegion,
2935     JNIImpl::SetCharArrayRegion,
2936     JNIImpl::SetShortArrayRegion,
2937     JNIImpl::SetIntArrayRegion,
2938     JNIImpl::SetLongArrayRegion,
2939     JNIImpl::SetFloatArrayRegion,
2940     JNIImpl::SetDoubleArrayRegion,
2941     JNIImpl::RegisterNatives,
2942     JNIImpl::UnregisterNatives,
2943     JNIImpl::MonitorEnter,
2944     JNIImpl::MonitorExit,
2945     JNIImpl::GetJavaVM,
2946     JNIImpl::GetStringRegion,
2947     JNIImpl::GetStringUTFRegion,
2948     JNIImpl::GetPrimitiveArrayCritical,
2949     JNIImpl::ReleasePrimitiveArrayCritical,
2950     JNIImpl::GetStringCritical,
2951     JNIImpl::ReleaseStringCritical,
2952     JNIImpl::NewWeakGlobalRef,
2953     JNIImpl::DeleteWeakGlobalRef,
2954     JNIImpl::ExceptionCheck,
2955     JNIImpl::NewDirectByteBuffer,
2956     JNIImpl::GetDirectBufferAddress,
2957     JNIImpl::GetDirectBufferCapacity,
2958     JNIImpl::GetObjectRefType,
2959   };
2960 };
2961 
GetJniNativeInterface()2962 const JNINativeInterface* GetJniNativeInterface() {
2963   // The template argument is passed down through the Encode/DecodeArtMethod/Field calls so if
2964   // JniIdType is kPointer the calls will be a simple cast with no branches. This ensures that
2965   // the normal case is still fast.
2966   return Runtime::Current()->GetJniIdType() == JniIdType::kPointer
2967              ? &JniNativeInterfaceFunctions<false>::gJniNativeInterface
2968              : &JniNativeInterfaceFunctions<true>::gJniNativeInterface;
2969 }
2970 
2971 void (*gJniSleepForeverStub[])()  = {
2972   nullptr,  // reserved0.
2973   nullptr,  // reserved1.
2974   nullptr,  // reserved2.
2975   nullptr,  // reserved3.
2976   SleepForever,
2977   SleepForever,
2978   SleepForever,
2979   SleepForever,
2980   SleepForever,
2981   SleepForever,
2982   SleepForever,
2983   SleepForever,
2984   SleepForever,
2985   SleepForever,
2986   SleepForever,
2987   SleepForever,
2988   SleepForever,
2989   SleepForever,
2990   SleepForever,
2991   SleepForever,
2992   SleepForever,
2993   SleepForever,
2994   SleepForever,
2995   SleepForever,
2996   SleepForever,
2997   SleepForever,
2998   SleepForever,
2999   SleepForever,
3000   SleepForever,
3001   SleepForever,
3002   SleepForever,
3003   SleepForever,
3004   SleepForever,
3005   SleepForever,
3006   SleepForever,
3007   SleepForever,
3008   SleepForever,
3009   SleepForever,
3010   SleepForever,
3011   SleepForever,
3012   SleepForever,
3013   SleepForever,
3014   SleepForever,
3015   SleepForever,
3016   SleepForever,
3017   SleepForever,
3018   SleepForever,
3019   SleepForever,
3020   SleepForever,
3021   SleepForever,
3022   SleepForever,
3023   SleepForever,
3024   SleepForever,
3025   SleepForever,
3026   SleepForever,
3027   SleepForever,
3028   SleepForever,
3029   SleepForever,
3030   SleepForever,
3031   SleepForever,
3032   SleepForever,
3033   SleepForever,
3034   SleepForever,
3035   SleepForever,
3036   SleepForever,
3037   SleepForever,
3038   SleepForever,
3039   SleepForever,
3040   SleepForever,
3041   SleepForever,
3042   SleepForever,
3043   SleepForever,
3044   SleepForever,
3045   SleepForever,
3046   SleepForever,
3047   SleepForever,
3048   SleepForever,
3049   SleepForever,
3050   SleepForever,
3051   SleepForever,
3052   SleepForever,
3053   SleepForever,
3054   SleepForever,
3055   SleepForever,
3056   SleepForever,
3057   SleepForever,
3058   SleepForever,
3059   SleepForever,
3060   SleepForever,
3061   SleepForever,
3062   SleepForever,
3063   SleepForever,
3064   SleepForever,
3065   SleepForever,
3066   SleepForever,
3067   SleepForever,
3068   SleepForever,
3069   SleepForever,
3070   SleepForever,
3071   SleepForever,
3072   SleepForever,
3073   SleepForever,
3074   SleepForever,
3075   SleepForever,
3076   SleepForever,
3077   SleepForever,
3078   SleepForever,
3079   SleepForever,
3080   SleepForever,
3081   SleepForever,
3082   SleepForever,
3083   SleepForever,
3084   SleepForever,
3085   SleepForever,
3086   SleepForever,
3087   SleepForever,
3088   SleepForever,
3089   SleepForever,
3090   SleepForever,
3091   SleepForever,
3092   SleepForever,
3093   SleepForever,
3094   SleepForever,
3095   SleepForever,
3096   SleepForever,
3097   SleepForever,
3098   SleepForever,
3099   SleepForever,
3100   SleepForever,
3101   SleepForever,
3102   SleepForever,
3103   SleepForever,
3104   SleepForever,
3105   SleepForever,
3106   SleepForever,
3107   SleepForever,
3108   SleepForever,
3109   SleepForever,
3110   SleepForever,
3111   SleepForever,
3112   SleepForever,
3113   SleepForever,
3114   SleepForever,
3115   SleepForever,
3116   SleepForever,
3117   SleepForever,
3118   SleepForever,
3119   SleepForever,
3120   SleepForever,
3121   SleepForever,
3122   SleepForever,
3123   SleepForever,
3124   SleepForever,
3125   SleepForever,
3126   SleepForever,
3127   SleepForever,
3128   SleepForever,
3129   SleepForever,
3130   SleepForever,
3131   SleepForever,
3132   SleepForever,
3133   SleepForever,
3134   SleepForever,
3135   SleepForever,
3136   SleepForever,
3137   SleepForever,
3138   SleepForever,
3139   SleepForever,
3140   SleepForever,
3141   SleepForever,
3142   SleepForever,
3143   SleepForever,
3144   SleepForever,
3145   SleepForever,
3146   SleepForever,
3147   SleepForever,
3148   SleepForever,
3149   SleepForever,
3150   SleepForever,
3151   SleepForever,
3152   SleepForever,
3153   SleepForever,
3154   SleepForever,
3155   SleepForever,
3156   SleepForever,
3157   SleepForever,
3158   SleepForever,
3159   SleepForever,
3160   SleepForever,
3161   SleepForever,
3162   SleepForever,
3163   SleepForever,
3164   SleepForever,
3165   SleepForever,
3166   SleepForever,
3167   SleepForever,
3168   SleepForever,
3169   SleepForever,
3170   SleepForever,
3171   SleepForever,
3172   SleepForever,
3173   SleepForever,
3174   SleepForever,
3175   SleepForever,
3176   SleepForever,
3177   SleepForever,
3178   SleepForever,
3179   SleepForever,
3180   SleepForever,
3181   SleepForever,
3182   SleepForever,
3183   SleepForever,
3184   SleepForever,
3185   SleepForever,
3186   SleepForever,
3187   SleepForever,
3188   SleepForever,
3189   SleepForever,
3190   SleepForever,
3191   SleepForever,
3192   SleepForever,
3193   SleepForever,
3194   SleepForever,
3195   SleepForever,
3196   SleepForever,
3197   SleepForever,
3198   SleepForever,
3199   SleepForever,
3200   SleepForever,
3201   SleepForever,
3202   SleepForever,
3203   SleepForever,
3204   SleepForever,
3205 };
3206 
GetRuntimeShutdownNativeInterface()3207 const JNINativeInterface* GetRuntimeShutdownNativeInterface() {
3208   return reinterpret_cast<JNINativeInterface*>(&gJniSleepForeverStub);
3209 }
3210 
3211 }  // namespace art
3212 
operator <<(std::ostream & os,const jobjectRefType & rhs)3213 std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3214   switch (rhs) {
3215   case JNIInvalidRefType:
3216     os << "JNIInvalidRefType";
3217     return os;
3218   case JNILocalRefType:
3219     os << "JNILocalRefType";
3220     return os;
3221   case JNIGlobalRefType:
3222     os << "JNIGlobalRefType";
3223     return os;
3224   case JNIWeakGlobalRefType:
3225     os << "JNIWeakGlobalRefType";
3226     return os;
3227   default:
3228     LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
3229     UNREACHABLE();
3230   }
3231 }
3232