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-inl.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.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     if (m->IsConstructor()) {
535       method = mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), m);
536     } else {
537       method = mirror::Method::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), m);
538     }
539     return soa.AddLocalReference<jobject>(method);
540   }
541 
ToReflectedField(JNIEnv * env,jclass,jfieldID fid,jboolean)542   static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
543     CHECK_NON_NULL_ARGUMENT(fid);
544     ScopedObjectAccess soa(env);
545     ArtField* f = jni::DecodeArtField(fid);
546     return soa.AddLocalReference<jobject>(
547         mirror::Field::CreateFromArtField(soa.Self(), f, true));
548   }
549 
GetObjectClass(JNIEnv * env,jobject java_object)550   static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
551     CHECK_NON_NULL_ARGUMENT(java_object);
552     ScopedObjectAccess soa(env);
553     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
554     return soa.AddLocalReference<jclass>(o->GetClass());
555   }
556 
GetSuperclass(JNIEnv * env,jclass java_class)557   static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
558     CHECK_NON_NULL_ARGUMENT(java_class);
559     ScopedObjectAccess soa(env);
560     ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
561     return soa.AddLocalReference<jclass>(c->IsInterface() ? nullptr : c->GetSuperClass());
562   }
563 
564   // Note: java_class1 should be safely castable to java_class2, and
565   // not the other way around.
IsAssignableFrom(JNIEnv * env,jclass java_class1,jclass java_class2)566   static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
567     CHECK_NON_NULL_ARGUMENT_RETURN(java_class1, JNI_FALSE);
568     CHECK_NON_NULL_ARGUMENT_RETURN(java_class2, JNI_FALSE);
569     ScopedObjectAccess soa(env);
570     ObjPtr<mirror::Class> c1 = soa.Decode<mirror::Class>(java_class1);
571     ObjPtr<mirror::Class> c2 = soa.Decode<mirror::Class>(java_class2);
572     return c2->IsAssignableFrom(c1) ? JNI_TRUE : JNI_FALSE;
573   }
574 
IsInstanceOf(JNIEnv * env,jobject jobj,jclass java_class)575   static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
576     CHECK_NON_NULL_ARGUMENT_RETURN(java_class, JNI_FALSE);
577     if (jobj == nullptr) {
578       // Note: JNI is different from regular Java instanceof in this respect
579       return JNI_TRUE;
580     } else {
581       ScopedObjectAccess soa(env);
582       ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(jobj);
583       ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(java_class);
584       return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
585     }
586   }
587 
Throw(JNIEnv * env,jthrowable java_exception)588   static jint Throw(JNIEnv* env, jthrowable java_exception) {
589     ScopedObjectAccess soa(env);
590     ObjPtr<mirror::Throwable> exception = soa.Decode<mirror::Throwable>(java_exception);
591     if (exception == nullptr) {
592       return JNI_ERR;
593     }
594     soa.Self()->SetException(exception);
595     return JNI_OK;
596   }
597 
ThrowNew(JNIEnv * env,jclass c,const char * msg)598   static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
599     CHECK_NON_NULL_ARGUMENT_RETURN(c, JNI_ERR);
600     return ThrowNewException(env, c, msg, nullptr);
601   }
602 
ExceptionCheck(JNIEnv * env)603   static jboolean ExceptionCheck(JNIEnv* env) {
604     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
605   }
606 
ExceptionClear(JNIEnv * env)607   static void ExceptionClear(JNIEnv* env) {
608     ScopedObjectAccess soa(env);
609     soa.Self()->ClearException();
610   }
611 
ExceptionDescribe(JNIEnv * env)612   static void ExceptionDescribe(JNIEnv* env) {
613     ScopedObjectAccess soa(env);
614 
615     // If we have no exception to describe, pass through.
616     if (!soa.Self()->GetException()) {
617       return;
618     }
619 
620     StackHandleScope<1> hs(soa.Self());
621     Handle<mirror::Throwable> old_exception(
622         hs.NewHandle<mirror::Throwable>(soa.Self()->GetException()));
623     soa.Self()->ClearException();
624     ScopedLocalRef<jthrowable> exception(env,
625                                          soa.AddLocalReference<jthrowable>(old_exception.Get()));
626     ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
627     jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
628     if (mid == nullptr) {
629       LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
630                    << mirror::Object::PrettyTypeOf(old_exception.Get());
631     } else {
632       env->CallVoidMethod(exception.get(), mid);
633       if (soa.Self()->IsExceptionPending()) {
634         LOG(WARNING) << "JNI WARNING: " << mirror::Object::PrettyTypeOf(soa.Self()->GetException())
635                      << " thrown while calling printStackTrace";
636         soa.Self()->ClearException();
637       }
638     }
639     soa.Self()->SetException(old_exception.Get());
640   }
641 
ExceptionOccurred(JNIEnv * env)642   static jthrowable ExceptionOccurred(JNIEnv* env) {
643     ScopedObjectAccess soa(env);
644     ObjPtr<mirror::Object> exception = soa.Self()->GetException();
645     return soa.AddLocalReference<jthrowable>(exception);
646   }
647 
FatalError(JNIEnv *,const char * msg)648   static void FatalError(JNIEnv*, const char* msg) {
649     LOG(FATAL) << "JNI FatalError called: " << msg;
650   }
651 
PushLocalFrame(JNIEnv * env,jint capacity)652   static jint PushLocalFrame(JNIEnv* env, jint capacity) {
653     // TODO: SOA may not be necessary but I do it to please lock annotations.
654     ScopedObjectAccess soa(env);
655     if (EnsureLocalCapacityInternal(soa, capacity, "PushLocalFrame") != JNI_OK) {
656       return JNI_ERR;
657     }
658     down_cast<JNIEnvExt*>(env)->PushFrame(capacity);
659     return JNI_OK;
660   }
661 
PopLocalFrame(JNIEnv * env,jobject java_survivor)662   static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
663     ScopedObjectAccess soa(env);
664     ObjPtr<mirror::Object> survivor = soa.Decode<mirror::Object>(java_survivor);
665     soa.Env()->PopFrame();
666     return soa.AddLocalReference<jobject>(survivor);
667   }
668 
EnsureLocalCapacity(JNIEnv * env,jint desired_capacity)669   static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
670     // TODO: SOA may not be necessary but I do it to please lock annotations.
671     ScopedObjectAccess soa(env);
672     return EnsureLocalCapacityInternal(soa, desired_capacity, "EnsureLocalCapacity");
673   }
674 
NewGlobalRef(JNIEnv * env,jobject obj)675   static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
676     ScopedObjectAccess soa(env);
677     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
678     return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj);
679   }
680 
DeleteGlobalRef(JNIEnv * env,jobject obj)681   static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
682     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
683     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
684     vm->DeleteGlobalRef(self, obj);
685   }
686 
NewWeakGlobalRef(JNIEnv * env,jobject obj)687   static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
688     ScopedObjectAccess soa(env);
689     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
690     return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj);
691   }
692 
DeleteWeakGlobalRef(JNIEnv * env,jweak obj)693   static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
694     JavaVMExt* vm = down_cast<JNIEnvExt*>(env)->GetVm();
695     Thread* self = down_cast<JNIEnvExt*>(env)->self_;
696     vm->DeleteWeakGlobalRef(self, obj);
697   }
698 
NewLocalRef(JNIEnv * env,jobject obj)699   static jobject NewLocalRef(JNIEnv* env, jobject obj) {
700     ScopedObjectAccess soa(env);
701     ObjPtr<mirror::Object> decoded_obj = soa.Decode<mirror::Object>(obj);
702     // Check for null after decoding the object to handle cleared weak globals.
703     if (decoded_obj == nullptr) {
704       return nullptr;
705     }
706     return soa.AddLocalReference<jobject>(decoded_obj);
707   }
708 
DeleteLocalRef(JNIEnv * env,jobject obj)709   static void DeleteLocalRef(JNIEnv* env, jobject obj) {
710     if (obj == nullptr) {
711       return;
712     }
713     // SOA is only necessary to have exclusion between GC root marking and removing.
714     // We don't want to have the GC attempt to mark a null root if we just removed
715     // it. b/22119403
716     ScopedObjectAccess soa(env);
717     auto* ext_env = down_cast<JNIEnvExt*>(env);
718     if (!ext_env->locals_.Remove(ext_env->local_ref_cookie_, obj)) {
719       // Attempting to delete a local reference that is not in the
720       // topmost local reference frame is a no-op.  DeleteLocalRef returns
721       // void and doesn't throw any exceptions, but we should probably
722       // complain about it so the user will notice that things aren't
723       // going quite the way they expect.
724       LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
725                    << "failed to find entry";
726     }
727   }
728 
IsSameObject(JNIEnv * env,jobject obj1,jobject obj2)729   static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
730     if (obj1 == obj2) {
731       return JNI_TRUE;
732     } else {
733       ScopedObjectAccess soa(env);
734       return (soa.Decode<mirror::Object>(obj1) == soa.Decode<mirror::Object>(obj2))
735               ? JNI_TRUE : JNI_FALSE;
736     }
737   }
738 
AllocObject(JNIEnv * env,jclass java_class)739   static jobject AllocObject(JNIEnv* env, jclass java_class) {
740     CHECK_NON_NULL_ARGUMENT(java_class);
741     ScopedObjectAccess soa(env);
742     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(), soa.Decode<mirror::Class>(java_class));
743     if (c == nullptr) {
744       return nullptr;
745     }
746     if (c->IsStringClass()) {
747       gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
748       return soa.AddLocalReference<jobject>(
749           mirror::String::AllocEmptyString(soa.Self(), allocator_type));
750     }
751     return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
752   }
753 
NewObject(JNIEnv * env,jclass java_class,jmethodID mid,...)754   static jobject NewObject(JNIEnv* env, jclass java_class, jmethodID mid, ...) {
755     va_list args;
756     va_start(args, mid);
757     ScopedVAArgs free_args_later(&args);
758     CHECK_NON_NULL_ARGUMENT(java_class);
759     CHECK_NON_NULL_ARGUMENT(mid);
760     jobject result = NewObjectV(env, java_class, mid, args);
761     return result;
762   }
763 
NewObjectV(JNIEnv * env,jclass java_class,jmethodID mid,va_list args)764   static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
765     CHECK_NON_NULL_ARGUMENT(java_class);
766     CHECK_NON_NULL_ARGUMENT(mid);
767     ScopedObjectAccess soa(env);
768     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
769                                                 soa.Decode<mirror::Class>(java_class));
770     if (c == nullptr) {
771       return nullptr;
772     }
773     if (c->IsStringClass()) {
774       // Replace calls to String.<init> with equivalent StringFactory call.
775       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
776           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
777       return CallStaticObjectMethodV(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
778     }
779     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
780     if (result == nullptr) {
781       return nullptr;
782     }
783     jobject local_result = soa.AddLocalReference<jobject>(result);
784     CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
785     if (soa.Self()->IsExceptionPending()) {
786       return nullptr;
787     }
788     return local_result;
789   }
790 
NewObjectA(JNIEnv * env,jclass java_class,jmethodID mid,const jvalue * args)791   static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, const jvalue* args) {
792     CHECK_NON_NULL_ARGUMENT(java_class);
793     CHECK_NON_NULL_ARGUMENT(mid);
794     ScopedObjectAccess soa(env);
795     ObjPtr<mirror::Class> c = EnsureInitialized(soa.Self(),
796                                                 soa.Decode<mirror::Class>(java_class));
797     if (c == nullptr) {
798       return nullptr;
799     }
800     if (c->IsStringClass()) {
801       // Replace calls to String.<init> with equivalent StringFactory call.
802       jmethodID sf_mid = jni::EncodeArtMethod<kEnableIndexIds>(
803           WellKnownClasses::StringInitToStringFactory(jni::DecodeArtMethod(mid)));
804       return CallStaticObjectMethodA(env, WellKnownClasses::java_lang_StringFactory, sf_mid, args);
805     }
806     ObjPtr<mirror::Object> result = c->AllocObject(soa.Self());
807     if (result == nullptr) {
808       return nullptr;
809     }
810     jobject local_result = soa.AddLocalReference<jobjectArray>(result);
811     CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
812     if (soa.Self()->IsExceptionPending()) {
813       return nullptr;
814     }
815     return local_result;
816   }
817 
GetMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)818   static jmethodID GetMethodID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
819     CHECK_NON_NULL_ARGUMENT(java_class);
820     CHECK_NON_NULL_ARGUMENT(name);
821     CHECK_NON_NULL_ARGUMENT(sig);
822     ScopedObjectAccess soa(env);
823     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, false);
824   }
825 
GetStaticMethodID(JNIEnv * env,jclass java_class,const char * name,const char * sig)826   static jmethodID GetStaticMethodID(JNIEnv* env, jclass java_class, const char* name,
827                                      const char* sig) {
828     CHECK_NON_NULL_ARGUMENT(java_class);
829     CHECK_NON_NULL_ARGUMENT(name);
830     CHECK_NON_NULL_ARGUMENT(sig);
831     ScopedObjectAccess soa(env);
832     return FindMethodID<kEnableIndexIds>(soa, java_class, name, sig, true);
833   }
834 
CallObjectMethod(JNIEnv * env,jobject obj,jmethodID mid,...)835   static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
836     va_list ap;
837     va_start(ap, mid);
838     ScopedVAArgs free_args_later(&ap);
839     CHECK_NON_NULL_ARGUMENT(obj);
840     CHECK_NON_NULL_ARGUMENT(mid);
841     ScopedObjectAccess soa(env);
842     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
843     return soa.AddLocalReference<jobject>(result.GetL());
844   }
845 
CallObjectMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)846   static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
847     CHECK_NON_NULL_ARGUMENT(obj);
848     CHECK_NON_NULL_ARGUMENT(mid);
849     ScopedObjectAccess soa(env);
850     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
851     return soa.AddLocalReference<jobject>(result.GetL());
852   }
853 
CallObjectMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)854   static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
855     CHECK_NON_NULL_ARGUMENT(obj);
856     CHECK_NON_NULL_ARGUMENT(mid);
857     ScopedObjectAccess soa(env);
858     JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
859     return soa.AddLocalReference<jobject>(result.GetL());
860   }
861 
CallBooleanMethod(JNIEnv * env,jobject obj,jmethodID mid,...)862   static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
863     va_list ap;
864     va_start(ap, mid);
865     ScopedVAArgs free_args_later(&ap);
866     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
867     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
868     ScopedObjectAccess soa(env);
869     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
870     return result.GetZ();
871   }
872 
CallBooleanMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)873   static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
874     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
875     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
876     ScopedObjectAccess soa(env);
877     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
878   }
879 
CallBooleanMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)880   static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
881     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
882     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
883     ScopedObjectAccess soa(env);
884     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
885   }
886 
CallByteMethod(JNIEnv * env,jobject obj,jmethodID mid,...)887   static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
888     va_list ap;
889     va_start(ap, mid);
890     ScopedVAArgs free_args_later(&ap);
891     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
892     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
893     ScopedObjectAccess soa(env);
894     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
895     return result.GetB();
896   }
897 
CallByteMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)898   static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
899     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
900     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
901     ScopedObjectAccess soa(env);
902     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
903   }
904 
CallByteMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)905   static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
906     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
907     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
908     ScopedObjectAccess soa(env);
909     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
910   }
911 
CallCharMethod(JNIEnv * env,jobject obj,jmethodID mid,...)912   static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
913     va_list ap;
914     va_start(ap, mid);
915     ScopedVAArgs free_args_later(&ap);
916     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
917     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
918     ScopedObjectAccess soa(env);
919     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
920     return result.GetC();
921   }
922 
CallCharMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)923   static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
924     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
925     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
926     ScopedObjectAccess soa(env);
927     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
928   }
929 
CallCharMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)930   static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
931     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
932     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
933     ScopedObjectAccess soa(env);
934     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
935   }
936 
CallDoubleMethod(JNIEnv * env,jobject obj,jmethodID mid,...)937   static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
938     va_list ap;
939     va_start(ap, mid);
940     ScopedVAArgs free_args_later(&ap);
941     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
942     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
943     ScopedObjectAccess soa(env);
944     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
945     return result.GetD();
946   }
947 
CallDoubleMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)948   static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
949     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
950     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
951     ScopedObjectAccess soa(env);
952     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
953   }
954 
CallDoubleMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)955   static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
956     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
957     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
958     ScopedObjectAccess soa(env);
959     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
960   }
961 
CallFloatMethod(JNIEnv * env,jobject obj,jmethodID mid,...)962   static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
963     va_list ap;
964     va_start(ap, mid);
965     ScopedVAArgs free_args_later(&ap);
966     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
967     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
968     ScopedObjectAccess soa(env);
969     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
970     return result.GetF();
971   }
972 
CallFloatMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)973   static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
974     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
975     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
976     ScopedObjectAccess soa(env);
977     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
978   }
979 
CallFloatMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)980   static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
981     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
982     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
983     ScopedObjectAccess soa(env);
984     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
985   }
986 
CallIntMethod(JNIEnv * env,jobject obj,jmethodID mid,...)987   static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
988     va_list ap;
989     va_start(ap, mid);
990     ScopedVAArgs free_args_later(&ap);
991     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
992     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
993     ScopedObjectAccess soa(env);
994     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
995     return result.GetI();
996   }
997 
CallIntMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)998   static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
999     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1000     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1001     ScopedObjectAccess soa(env);
1002     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
1003   }
1004 
CallIntMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1005   static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1006     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1007     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1008     ScopedObjectAccess soa(env);
1009     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
1010   }
1011 
CallLongMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1012   static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1013     va_list ap;
1014     va_start(ap, mid);
1015     ScopedVAArgs free_args_later(&ap);
1016     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1017     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1018     ScopedObjectAccess soa(env);
1019     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1020     return result.GetJ();
1021   }
1022 
CallLongMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1023   static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1024     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1025     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1026     ScopedObjectAccess soa(env);
1027     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
1028   }
1029 
CallLongMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1030   static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1031     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1032     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1033     ScopedObjectAccess soa(env);
1034     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
1035   }
1036 
CallShortMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1037   static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1038     va_list ap;
1039     va_start(ap, mid);
1040     ScopedVAArgs free_args_later(&ap);
1041     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1042     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1043     ScopedObjectAccess soa(env);
1044     JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
1045     return result.GetS();
1046   }
1047 
CallShortMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1048   static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1049     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1050     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1051     ScopedObjectAccess soa(env);
1052     return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
1053   }
1054 
CallShortMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1055   static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1056     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1057     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1058     ScopedObjectAccess soa(env);
1059     return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
1060   }
1061 
CallVoidMethod(JNIEnv * env,jobject obj,jmethodID mid,...)1062   static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
1063     va_list ap;
1064     va_start(ap, mid);
1065     ScopedVAArgs free_args_later(&ap);
1066     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1067     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1068     ScopedObjectAccess soa(env);
1069     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
1070   }
1071 
CallVoidMethodV(JNIEnv * env,jobject obj,jmethodID mid,va_list args)1072   static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
1073     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1074     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1075     ScopedObjectAccess soa(env);
1076     InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
1077   }
1078 
CallVoidMethodA(JNIEnv * env,jobject obj,jmethodID mid,const jvalue * args)1079   static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, const jvalue* args) {
1080     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1081     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1082     ScopedObjectAccess soa(env);
1083     InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
1084   }
1085 
CallNonvirtualObjectMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1086   static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1087     va_list ap;
1088     va_start(ap, mid);
1089     ScopedVAArgs free_args_later(&ap);
1090     CHECK_NON_NULL_ARGUMENT(obj);
1091     CHECK_NON_NULL_ARGUMENT(mid);
1092     ScopedObjectAccess soa(env);
1093     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1094     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1095     return local_result;
1096   }
1097 
CallNonvirtualObjectMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1098   static jobject CallNonvirtualObjectMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1099                                              va_list args) {
1100     CHECK_NON_NULL_ARGUMENT(obj);
1101     CHECK_NON_NULL_ARGUMENT(mid);
1102     ScopedObjectAccess soa(env);
1103     JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1104     return soa.AddLocalReference<jobject>(result.GetL());
1105   }
1106 
CallNonvirtualObjectMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1107   static jobject CallNonvirtualObjectMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1108                                              const jvalue* args) {
1109     CHECK_NON_NULL_ARGUMENT(obj);
1110     CHECK_NON_NULL_ARGUMENT(mid);
1111     ScopedObjectAccess soa(env);
1112     JValue result(InvokeWithJValues(soa, obj, mid, args));
1113     return soa.AddLocalReference<jobject>(result.GetL());
1114   }
1115 
CallNonvirtualBooleanMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1116   static jboolean CallNonvirtualBooleanMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1117                                               ...) {
1118     va_list ap;
1119     va_start(ap, mid);
1120     ScopedVAArgs free_args_later(&ap);
1121     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1122     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1123     ScopedObjectAccess soa(env);
1124     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1125     return result.GetZ();
1126   }
1127 
CallNonvirtualBooleanMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1128   static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1129                                                va_list args) {
1130     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1131     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1132     ScopedObjectAccess soa(env);
1133     return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
1134   }
1135 
CallNonvirtualBooleanMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1136   static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1137                                                const jvalue* args) {
1138     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1139     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1140     ScopedObjectAccess soa(env);
1141     return InvokeWithJValues(soa, obj, mid, args).GetZ();
1142   }
1143 
CallNonvirtualByteMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1144   static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1145     va_list ap;
1146     va_start(ap, mid);
1147     ScopedVAArgs free_args_later(&ap);
1148     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1149     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1150     ScopedObjectAccess soa(env);
1151     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1152     return result.GetB();
1153   }
1154 
CallNonvirtualByteMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1155   static jbyte CallNonvirtualByteMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1156                                          va_list args) {
1157     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1158     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1159     ScopedObjectAccess soa(env);
1160     return InvokeWithVarArgs(soa, obj, mid, args).GetB();
1161   }
1162 
CallNonvirtualByteMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1163   static jbyte CallNonvirtualByteMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1164                                          const jvalue* args) {
1165     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1166     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1167     ScopedObjectAccess soa(env);
1168     return InvokeWithJValues(soa, obj, mid, args).GetB();
1169   }
1170 
CallNonvirtualCharMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1171   static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1172     va_list ap;
1173     va_start(ap, mid);
1174     ScopedVAArgs free_args_later(&ap);
1175     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1176     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1177     ScopedObjectAccess soa(env);
1178     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1179     return result.GetC();
1180   }
1181 
CallNonvirtualCharMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1182   static jchar CallNonvirtualCharMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1183                                          va_list args) {
1184     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1185     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1186     ScopedObjectAccess soa(env);
1187     return InvokeWithVarArgs(soa, obj, mid, args).GetC();
1188   }
1189 
CallNonvirtualCharMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1190   static jchar CallNonvirtualCharMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1191                                          const jvalue* args) {
1192     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1193     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1194     ScopedObjectAccess soa(env);
1195     return InvokeWithJValues(soa, obj, mid, args).GetC();
1196   }
1197 
CallNonvirtualShortMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1198   static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1199     va_list ap;
1200     va_start(ap, mid);
1201     ScopedVAArgs free_args_later(&ap);
1202     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1203     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1204     ScopedObjectAccess soa(env);
1205     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1206     return result.GetS();
1207   }
1208 
CallNonvirtualShortMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1209   static jshort CallNonvirtualShortMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1210                                            va_list args) {
1211     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1212     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1213     ScopedObjectAccess soa(env);
1214     return InvokeWithVarArgs(soa, obj, mid, args).GetS();
1215   }
1216 
CallNonvirtualShortMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1217   static jshort CallNonvirtualShortMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1218                                            const jvalue* args) {
1219     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1220     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1221     ScopedObjectAccess soa(env);
1222     return InvokeWithJValues(soa, obj, mid, args).GetS();
1223   }
1224 
CallNonvirtualIntMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1225   static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1226     va_list ap;
1227     va_start(ap, mid);
1228     ScopedVAArgs free_args_later(&ap);
1229     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1230     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1231     ScopedObjectAccess soa(env);
1232     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1233     return result.GetI();
1234   }
1235 
CallNonvirtualIntMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1236   static jint CallNonvirtualIntMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1237                                        va_list args) {
1238     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1239     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1240     ScopedObjectAccess soa(env);
1241     return InvokeWithVarArgs(soa, obj, mid, args).GetI();
1242   }
1243 
CallNonvirtualIntMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1244   static jint CallNonvirtualIntMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1245                                        const jvalue* args) {
1246     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1247     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1248     ScopedObjectAccess soa(env);
1249     return InvokeWithJValues(soa, obj, mid, args).GetI();
1250   }
1251 
CallNonvirtualLongMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1252   static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1253     va_list ap;
1254     va_start(ap, mid);
1255     ScopedVAArgs free_args_later(&ap);
1256     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1257     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1258     ScopedObjectAccess soa(env);
1259     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1260     return result.GetJ();
1261   }
1262 
CallNonvirtualLongMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1263   static jlong CallNonvirtualLongMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1264                                          va_list args) {
1265     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1266     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1267     ScopedObjectAccess soa(env);
1268     return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
1269   }
1270 
CallNonvirtualLongMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1271   static jlong CallNonvirtualLongMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1272                                          const jvalue* args) {
1273     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1274     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1275     ScopedObjectAccess soa(env);
1276     return InvokeWithJValues(soa, obj, mid, args).GetJ();
1277   }
1278 
CallNonvirtualFloatMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1279   static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1280     va_list ap;
1281     va_start(ap, mid);
1282     ScopedVAArgs free_args_later(&ap);
1283     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1284     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1285     ScopedObjectAccess soa(env);
1286     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1287     return result.GetF();
1288   }
1289 
CallNonvirtualFloatMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1290   static jfloat CallNonvirtualFloatMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1291                                            va_list args) {
1292     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1293     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1294     ScopedObjectAccess soa(env);
1295     return InvokeWithVarArgs(soa, obj, mid, args).GetF();
1296   }
1297 
CallNonvirtualFloatMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1298   static jfloat CallNonvirtualFloatMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1299                                            const jvalue* args) {
1300     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1301     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1302     ScopedObjectAccess soa(env);
1303     return InvokeWithJValues(soa, obj, mid, args).GetF();
1304   }
1305 
CallNonvirtualDoubleMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1306   static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1307     va_list ap;
1308     va_start(ap, mid);
1309     ScopedVAArgs free_args_later(&ap);
1310     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1311     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1312     ScopedObjectAccess soa(env);
1313     JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1314     return result.GetD();
1315   }
1316 
CallNonvirtualDoubleMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1317   static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1318                                              va_list args) {
1319     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1320     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1321     ScopedObjectAccess soa(env);
1322     return InvokeWithVarArgs(soa, obj, mid, args).GetD();
1323   }
1324 
CallNonvirtualDoubleMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1325   static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1326                                              const jvalue* args) {
1327     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(obj);
1328     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1329     ScopedObjectAccess soa(env);
1330     return InvokeWithJValues(soa, obj, mid, args).GetD();
1331   }
1332 
CallNonvirtualVoidMethod(JNIEnv * env,jobject obj,jclass,jmethodID mid,...)1333   static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
1334     va_list ap;
1335     va_start(ap, mid);
1336     ScopedVAArgs free_args_later(&ap);
1337     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1338     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1339     ScopedObjectAccess soa(env);
1340     InvokeWithVarArgs(soa, obj, mid, ap);
1341   }
1342 
CallNonvirtualVoidMethodV(JNIEnv * env,jobject obj,jclass,jmethodID mid,va_list args)1343   static void CallNonvirtualVoidMethodV(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1344                                         va_list args) {
1345     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1346     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1347     ScopedObjectAccess soa(env);
1348     InvokeWithVarArgs(soa, obj, mid, args);
1349   }
1350 
CallNonvirtualVoidMethodA(JNIEnv * env,jobject obj,jclass,jmethodID mid,const jvalue * args)1351   static void CallNonvirtualVoidMethodA(JNIEnv* env, jobject obj, jclass, jmethodID mid,
1352                                         const jvalue* args) {
1353     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(obj);
1354     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1355     ScopedObjectAccess soa(env);
1356     InvokeWithJValues(soa, obj, mid, args);
1357   }
1358 
GetFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1359   static jfieldID GetFieldID(JNIEnv* env, jclass java_class, const char* name, const char* sig) {
1360     CHECK_NON_NULL_ARGUMENT(java_class);
1361     CHECK_NON_NULL_ARGUMENT(name);
1362     CHECK_NON_NULL_ARGUMENT(sig);
1363     ScopedObjectAccess soa(env);
1364     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, false);
1365   }
1366 
GetStaticFieldID(JNIEnv * env,jclass java_class,const char * name,const char * sig)1367   static jfieldID GetStaticFieldID(JNIEnv* env, jclass java_class, const char* name,
1368                                    const char* sig) {
1369     CHECK_NON_NULL_ARGUMENT(java_class);
1370     CHECK_NON_NULL_ARGUMENT(name);
1371     CHECK_NON_NULL_ARGUMENT(sig);
1372     ScopedObjectAccess soa(env);
1373     return FindFieldID<kEnableIndexIds>(soa, java_class, name, sig, true);
1374   }
1375 
GetObjectField(JNIEnv * env,jobject obj,jfieldID fid)1376   static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
1377     CHECK_NON_NULL_ARGUMENT(obj);
1378     CHECK_NON_NULL_ARGUMENT(fid);
1379     ScopedObjectAccess soa(env);
1380     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1381     NotifyGetField(f, obj);
1382     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(obj);
1383     return soa.AddLocalReference<jobject>(f->GetObject(o));
1384   }
1385 
GetStaticObjectField(JNIEnv * env,jclass,jfieldID fid)1386   static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
1387     CHECK_NON_NULL_ARGUMENT(fid);
1388     ScopedObjectAccess soa(env);
1389     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1390     NotifyGetField(f, nullptr);
1391     return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
1392   }
1393 
SetObjectField(JNIEnv * env,jobject java_object,jfieldID fid,jobject java_value)1394   static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
1395     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_object);
1396     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1397     ScopedObjectAccess soa(env);
1398     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1399     NotifySetObjectField(f, java_object, java_value);
1400     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
1401     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1402     f->SetObject<false>(o, v);
1403   }
1404 
SetStaticObjectField(JNIEnv * env,jclass,jfieldID fid,jobject java_value)1405   static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
1406     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid);
1407     ScopedObjectAccess soa(env);
1408     ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid);
1409     NotifySetObjectField(f, nullptr, java_value);
1410     ObjPtr<mirror::Object> v = soa.Decode<mirror::Object>(java_value);
1411     f->SetObject<false>(f->GetDeclaringClass(), v);
1412   }
1413 
1414 #define GET_PRIMITIVE_FIELD(fn, instance) \
1415   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(instance); \
1416   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1417   ScopedObjectAccess soa(env); \
1418   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1419   NotifyGetField(f, instance); \
1420   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1421   return f->Get ##fn (o)
1422 
1423 #define GET_STATIC_PRIMITIVE_FIELD(fn) \
1424   CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(fid); \
1425   ScopedObjectAccess soa(env); \
1426   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1427   NotifyGetField(f, nullptr); \
1428   return f->Get ##fn (f->GetDeclaringClass())
1429 
1430 #define SET_PRIMITIVE_FIELD(fn, instance, value) \
1431   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(instance); \
1432   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1433   ScopedObjectAccess soa(env); \
1434   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1435   NotifySetPrimitiveField(f, instance, JValue::FromPrimitive<decltype(value)>(value)); \
1436   ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(instance); \
1437   f->Set ##fn <false>(o, value)
1438 
1439 #define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1440   CHECK_NON_NULL_ARGUMENT_RETURN_VOID(fid); \
1441   ScopedObjectAccess soa(env); \
1442   ArtField* f = jni::DecodeArtField<kEnableIndexIds>(fid); \
1443   NotifySetPrimitiveField(f, nullptr, JValue::FromPrimitive<decltype(value)>(value)); \
1444   f->Set ##fn <false>(f->GetDeclaringClass(), value)
1445 
GetBooleanField(JNIEnv * env,jobject obj,jfieldID fid)1446   static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1447     GET_PRIMITIVE_FIELD(Boolean, obj);
1448   }
1449 
GetByteField(JNIEnv * env,jobject obj,jfieldID fid)1450   static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1451     GET_PRIMITIVE_FIELD(Byte, obj);
1452   }
1453 
GetCharField(JNIEnv * env,jobject obj,jfieldID fid)1454   static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1455     GET_PRIMITIVE_FIELD(Char, obj);
1456   }
1457 
GetShortField(JNIEnv * env,jobject obj,jfieldID fid)1458   static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1459     GET_PRIMITIVE_FIELD(Short, obj);
1460   }
1461 
GetIntField(JNIEnv * env,jobject obj,jfieldID fid)1462   static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1463     GET_PRIMITIVE_FIELD(Int, obj);
1464   }
1465 
GetLongField(JNIEnv * env,jobject obj,jfieldID fid)1466   static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1467     GET_PRIMITIVE_FIELD(Long, obj);
1468   }
1469 
GetFloatField(JNIEnv * env,jobject obj,jfieldID fid)1470   static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1471     GET_PRIMITIVE_FIELD(Float, obj);
1472   }
1473 
GetDoubleField(JNIEnv * env,jobject obj,jfieldID fid)1474   static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1475     GET_PRIMITIVE_FIELD(Double, obj);
1476   }
1477 
GetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid)1478   static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
1479     GET_STATIC_PRIMITIVE_FIELD(Boolean);
1480   }
1481 
GetStaticByteField(JNIEnv * env,jclass,jfieldID fid)1482   static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
1483     GET_STATIC_PRIMITIVE_FIELD(Byte);
1484   }
1485 
GetStaticCharField(JNIEnv * env,jclass,jfieldID fid)1486   static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
1487     GET_STATIC_PRIMITIVE_FIELD(Char);
1488   }
1489 
GetStaticShortField(JNIEnv * env,jclass,jfieldID fid)1490   static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
1491     GET_STATIC_PRIMITIVE_FIELD(Short);
1492   }
1493 
GetStaticIntField(JNIEnv * env,jclass,jfieldID fid)1494   static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
1495     GET_STATIC_PRIMITIVE_FIELD(Int);
1496   }
1497 
GetStaticLongField(JNIEnv * env,jclass,jfieldID fid)1498   static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
1499     GET_STATIC_PRIMITIVE_FIELD(Long);
1500   }
1501 
GetStaticFloatField(JNIEnv * env,jclass,jfieldID fid)1502   static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
1503     GET_STATIC_PRIMITIVE_FIELD(Float);
1504   }
1505 
GetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid)1506   static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
1507     GET_STATIC_PRIMITIVE_FIELD(Double);
1508   }
1509 
SetBooleanField(JNIEnv * env,jobject obj,jfieldID fid,jboolean v)1510   static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1511     SET_PRIMITIVE_FIELD(Boolean, obj, v);
1512   }
1513 
SetByteField(JNIEnv * env,jobject obj,jfieldID fid,jbyte v)1514   static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1515     SET_PRIMITIVE_FIELD(Byte, obj, v);
1516   }
1517 
SetCharField(JNIEnv * env,jobject obj,jfieldID fid,jchar v)1518   static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1519     SET_PRIMITIVE_FIELD(Char, obj, v);
1520   }
1521 
SetFloatField(JNIEnv * env,jobject obj,jfieldID fid,jfloat v)1522   static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1523     SET_PRIMITIVE_FIELD(Float, obj, v);
1524   }
1525 
SetDoubleField(JNIEnv * env,jobject obj,jfieldID fid,jdouble v)1526   static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1527     SET_PRIMITIVE_FIELD(Double, obj, v);
1528   }
1529 
SetIntField(JNIEnv * env,jobject obj,jfieldID fid,jint v)1530   static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1531     SET_PRIMITIVE_FIELD(Int, obj, v);
1532   }
1533 
SetLongField(JNIEnv * env,jobject obj,jfieldID fid,jlong v)1534   static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1535     SET_PRIMITIVE_FIELD(Long, obj, v);
1536   }
1537 
SetShortField(JNIEnv * env,jobject obj,jfieldID fid,jshort v)1538   static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1539     SET_PRIMITIVE_FIELD(Short, obj, v);
1540   }
1541 
SetStaticBooleanField(JNIEnv * env,jclass,jfieldID fid,jboolean v)1542   static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
1543     SET_STATIC_PRIMITIVE_FIELD(Boolean, v);
1544   }
1545 
SetStaticByteField(JNIEnv * env,jclass,jfieldID fid,jbyte v)1546   static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
1547     SET_STATIC_PRIMITIVE_FIELD(Byte, v);
1548   }
1549 
SetStaticCharField(JNIEnv * env,jclass,jfieldID fid,jchar v)1550   static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
1551     SET_STATIC_PRIMITIVE_FIELD(Char, v);
1552   }
1553 
SetStaticFloatField(JNIEnv * env,jclass,jfieldID fid,jfloat v)1554   static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
1555     SET_STATIC_PRIMITIVE_FIELD(Float, v);
1556   }
1557 
SetStaticDoubleField(JNIEnv * env,jclass,jfieldID fid,jdouble v)1558   static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
1559     SET_STATIC_PRIMITIVE_FIELD(Double, v);
1560   }
1561 
SetStaticIntField(JNIEnv * env,jclass,jfieldID fid,jint v)1562   static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
1563     SET_STATIC_PRIMITIVE_FIELD(Int, v);
1564   }
1565 
SetStaticLongField(JNIEnv * env,jclass,jfieldID fid,jlong v)1566   static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
1567     SET_STATIC_PRIMITIVE_FIELD(Long, v);
1568   }
1569 
SetStaticShortField(JNIEnv * env,jclass,jfieldID fid,jshort v)1570   static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
1571     SET_STATIC_PRIMITIVE_FIELD(Short, v);
1572   }
1573 
CallStaticObjectMethod(JNIEnv * env,jclass,jmethodID mid,...)1574   static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1575     va_list ap;
1576     va_start(ap, mid);
1577     ScopedVAArgs free_args_later(&ap);
1578     CHECK_NON_NULL_ARGUMENT(mid);
1579     ScopedObjectAccess soa(env);
1580     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1581     jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
1582     return local_result;
1583   }
1584 
CallStaticObjectMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1585   static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1586     CHECK_NON_NULL_ARGUMENT(mid);
1587     ScopedObjectAccess soa(env);
1588     JValue result(InvokeWithVarArgs(soa, nullptr, mid, args));
1589     return soa.AddLocalReference<jobject>(result.GetL());
1590   }
1591 
CallStaticObjectMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1592   static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1593     CHECK_NON_NULL_ARGUMENT(mid);
1594     ScopedObjectAccess soa(env);
1595     JValue result(InvokeWithJValues(soa, nullptr, mid, args));
1596     return soa.AddLocalReference<jobject>(result.GetL());
1597   }
1598 
CallStaticBooleanMethod(JNIEnv * env,jclass,jmethodID mid,...)1599   static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1600     va_list ap;
1601     va_start(ap, mid);
1602     ScopedVAArgs free_args_later(&ap);
1603     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1604     ScopedObjectAccess soa(env);
1605     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1606     return result.GetZ();
1607   }
1608 
CallStaticBooleanMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1609   static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1610     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1611     ScopedObjectAccess soa(env);
1612     return InvokeWithVarArgs(soa, nullptr, mid, args).GetZ();
1613   }
1614 
CallStaticBooleanMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1615   static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1616     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1617     ScopedObjectAccess soa(env);
1618     return InvokeWithJValues(soa, nullptr, mid, args).GetZ();
1619   }
1620 
CallStaticByteMethod(JNIEnv * env,jclass,jmethodID mid,...)1621   static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1622     va_list ap;
1623     va_start(ap, mid);
1624     ScopedVAArgs free_args_later(&ap);
1625     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1626     ScopedObjectAccess soa(env);
1627     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1628     return result.GetB();
1629   }
1630 
CallStaticByteMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1631   static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1632     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1633     ScopedObjectAccess soa(env);
1634     return InvokeWithVarArgs(soa, nullptr, mid, args).GetB();
1635   }
1636 
CallStaticByteMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1637   static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1638     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1639     ScopedObjectAccess soa(env);
1640     return InvokeWithJValues(soa, nullptr, mid, args).GetB();
1641   }
1642 
CallStaticCharMethod(JNIEnv * env,jclass,jmethodID mid,...)1643   static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1644     va_list ap;
1645     va_start(ap, mid);
1646     ScopedVAArgs free_args_later(&ap);
1647     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1648     ScopedObjectAccess soa(env);
1649     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1650     return result.GetC();
1651   }
1652 
CallStaticCharMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1653   static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1654     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1655     ScopedObjectAccess soa(env);
1656     return InvokeWithVarArgs(soa, nullptr, mid, args).GetC();
1657   }
1658 
CallStaticCharMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1659   static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1660     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1661     ScopedObjectAccess soa(env);
1662     return InvokeWithJValues(soa, nullptr, mid, args).GetC();
1663   }
1664 
CallStaticShortMethod(JNIEnv * env,jclass,jmethodID mid,...)1665   static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1666     va_list ap;
1667     va_start(ap, mid);
1668     ScopedVAArgs free_args_later(&ap);
1669     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1670     ScopedObjectAccess soa(env);
1671     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1672     return result.GetS();
1673   }
1674 
CallStaticShortMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1675   static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1676     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1677     ScopedObjectAccess soa(env);
1678     return InvokeWithVarArgs(soa, nullptr, mid, args).GetS();
1679   }
1680 
CallStaticShortMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1681   static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1682     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1683     ScopedObjectAccess soa(env);
1684     return InvokeWithJValues(soa, nullptr, mid, args).GetS();
1685   }
1686 
CallStaticIntMethod(JNIEnv * env,jclass,jmethodID mid,...)1687   static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1688     va_list ap;
1689     va_start(ap, mid);
1690     ScopedVAArgs free_args_later(&ap);
1691     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1692     ScopedObjectAccess soa(env);
1693     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1694     return result.GetI();
1695   }
1696 
CallStaticIntMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1697   static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1698     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1699     ScopedObjectAccess soa(env);
1700     return InvokeWithVarArgs(soa, nullptr, mid, args).GetI();
1701   }
1702 
CallStaticIntMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1703   static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1704     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1705     ScopedObjectAccess soa(env);
1706     return InvokeWithJValues(soa, nullptr, mid, args).GetI();
1707   }
1708 
CallStaticLongMethod(JNIEnv * env,jclass,jmethodID mid,...)1709   static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1710     va_list ap;
1711     va_start(ap, mid);
1712     ScopedVAArgs free_args_later(&ap);
1713     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1714     ScopedObjectAccess soa(env);
1715     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1716     return result.GetJ();
1717   }
1718 
CallStaticLongMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1719   static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1720     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1721     ScopedObjectAccess soa(env);
1722     return InvokeWithVarArgs(soa, nullptr, mid, args).GetJ();
1723   }
1724 
CallStaticLongMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1725   static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1726     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1727     ScopedObjectAccess soa(env);
1728     return InvokeWithJValues(soa, nullptr, mid, args).GetJ();
1729   }
1730 
CallStaticFloatMethod(JNIEnv * env,jclass,jmethodID mid,...)1731   static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1732     va_list ap;
1733     va_start(ap, mid);
1734     ScopedVAArgs free_args_later(&ap);
1735     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1736     ScopedObjectAccess soa(env);
1737     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1738     return result.GetF();
1739   }
1740 
CallStaticFloatMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1741   static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1742     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1743     ScopedObjectAccess soa(env);
1744     return InvokeWithVarArgs(soa, nullptr, mid, args).GetF();
1745   }
1746 
CallStaticFloatMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1747   static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1748     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1749     ScopedObjectAccess soa(env);
1750     return InvokeWithJValues(soa, nullptr, mid, args).GetF();
1751   }
1752 
CallStaticDoubleMethod(JNIEnv * env,jclass,jmethodID mid,...)1753   static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1754     va_list ap;
1755     va_start(ap, mid);
1756     ScopedVAArgs free_args_later(&ap);
1757     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1758     ScopedObjectAccess soa(env);
1759     JValue result(InvokeWithVarArgs(soa, nullptr, mid, ap));
1760     return result.GetD();
1761   }
1762 
CallStaticDoubleMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1763   static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1764     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1765     ScopedObjectAccess soa(env);
1766     return InvokeWithVarArgs(soa, nullptr, mid, args).GetD();
1767   }
1768 
CallStaticDoubleMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1769   static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1770     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(mid);
1771     ScopedObjectAccess soa(env);
1772     return InvokeWithJValues(soa, nullptr, mid, args).GetD();
1773   }
1774 
CallStaticVoidMethod(JNIEnv * env,jclass,jmethodID mid,...)1775   static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
1776     va_list ap;
1777     va_start(ap, mid);
1778     ScopedVAArgs free_args_later(&ap);
1779     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1780     ScopedObjectAccess soa(env);
1781     InvokeWithVarArgs(soa, nullptr, mid, ap);
1782   }
1783 
CallStaticVoidMethodV(JNIEnv * env,jclass,jmethodID mid,va_list args)1784   static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
1785     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1786     ScopedObjectAccess soa(env);
1787     InvokeWithVarArgs(soa, nullptr, mid, args);
1788   }
1789 
CallStaticVoidMethodA(JNIEnv * env,jclass,jmethodID mid,const jvalue * args)1790   static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, const jvalue* args) {
1791     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(mid);
1792     ScopedObjectAccess soa(env);
1793     InvokeWithJValues(soa, nullptr, mid, args);
1794   }
1795 
NewString(JNIEnv * env,const jchar * chars,jsize char_count)1796   static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
1797     if (UNLIKELY(char_count < 0)) {
1798       JavaVmExtFromEnv(env)->JniAbortF("NewString", "char_count < 0: %d", char_count);
1799       return nullptr;
1800     }
1801     if (UNLIKELY(chars == nullptr && char_count > 0)) {
1802       JavaVmExtFromEnv(env)->JniAbortF("NewString", "chars == null && char_count > 0");
1803       return nullptr;
1804     }
1805     ScopedObjectAccess soa(env);
1806     ObjPtr<mirror::String> result = mirror::String::AllocFromUtf16(soa.Self(), char_count, chars);
1807     return soa.AddLocalReference<jstring>(result);
1808   }
1809 
NewStringUTF(JNIEnv * env,const char * utf)1810   static jstring NewStringUTF(JNIEnv* env, const char* utf) {
1811     if (utf == nullptr) {
1812       return nullptr;
1813     }
1814     ScopedObjectAccess soa(env);
1815     ObjPtr<mirror::String> result = mirror::String::AllocFromModifiedUtf8(soa.Self(), utf);
1816     return soa.AddLocalReference<jstring>(result);
1817   }
1818 
GetStringLength(JNIEnv * env,jstring java_string)1819   static jsize GetStringLength(JNIEnv* env, jstring java_string) {
1820     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1821     ScopedObjectAccess soa(env);
1822     return soa.Decode<mirror::String>(java_string)->GetLength();
1823   }
1824 
GetStringUTFLength(JNIEnv * env,jstring java_string)1825   static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
1826     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_string);
1827     ScopedObjectAccess soa(env);
1828     return soa.Decode<mirror::String>(java_string)->GetUtfLength();
1829   }
1830 
GetStringRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,jchar * buf)1831   static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1832                               jchar* buf) {
1833     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1834     ScopedObjectAccess soa(env);
1835     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1836     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1837       ThrowSIOOBE(soa, start, length, s->GetLength());
1838     } else {
1839       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1840       if (s->IsCompressed()) {
1841         for (int i = 0; i < length; ++i) {
1842           buf[i] = static_cast<jchar>(s->CharAt(start+i));
1843         }
1844       } else {
1845         const jchar* chars = static_cast<jchar*>(s->GetValue());
1846         memcpy(buf, chars + start, length * sizeof(jchar));
1847       }
1848     }
1849   }
1850 
GetStringUTFRegion(JNIEnv * env,jstring java_string,jsize start,jsize length,char * buf)1851   static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length,
1852                                  char* buf) {
1853     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1854     ScopedObjectAccess soa(env);
1855     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1856     if (start < 0 || length < 0 || length > s->GetLength() - start) {
1857       ThrowSIOOBE(soa, start, length, s->GetLength());
1858     } else {
1859       CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
1860       if (s->IsCompressed()) {
1861         for (int i = 0; i < length; ++i) {
1862           buf[i] = s->CharAt(start+i);
1863         }
1864       } else {
1865         const jchar* chars = s->GetValue();
1866         size_t bytes = CountUtf8Bytes(chars + start, length);
1867         ConvertUtf16ToModifiedUtf8(buf, bytes, chars + start, length);
1868       }
1869     }
1870   }
1871 
GetStringChars(JNIEnv * env,jstring java_string,jboolean * is_copy)1872   static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1873     CHECK_NON_NULL_ARGUMENT(java_string);
1874     ScopedObjectAccess soa(env);
1875     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1876     gc::Heap* heap = Runtime::Current()->GetHeap();
1877     if (heap->IsMovableObject(s) || s->IsCompressed()) {
1878       jchar* chars = new jchar[s->GetLength()];
1879       if (s->IsCompressed()) {
1880         int32_t length = s->GetLength();
1881         for (int i = 0; i < length; ++i) {
1882           chars[i] = s->CharAt(i);
1883         }
1884       } else {
1885         memcpy(chars, s->GetValue(), sizeof(jchar) * s->GetLength());
1886       }
1887       if (is_copy != nullptr) {
1888         *is_copy = JNI_TRUE;
1889       }
1890       return chars;
1891     }
1892     if (is_copy != nullptr) {
1893       *is_copy = JNI_FALSE;
1894     }
1895     return static_cast<jchar*>(s->GetValue());
1896   }
1897 
ReleaseStringChars(JNIEnv * env,jstring java_string,const jchar * chars)1898   static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar* chars) {
1899     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1900     ScopedObjectAccess soa(env);
1901     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1902     if (s->IsCompressed() || (s->IsCompressed() == false && chars != s->GetValue())) {
1903       delete[] chars;
1904     }
1905   }
1906 
GetStringCritical(JNIEnv * env,jstring java_string,jboolean * is_copy)1907   static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1908     CHECK_NON_NULL_ARGUMENT(java_string);
1909     ScopedObjectAccess soa(env);
1910     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1911     gc::Heap* heap = Runtime::Current()->GetHeap();
1912     if (heap->IsMovableObject(s)) {
1913       StackHandleScope<1> hs(soa.Self());
1914       HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
1915       if (!kUseReadBarrier) {
1916         heap->IncrementDisableMovingGC(soa.Self());
1917       } else {
1918         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
1919         // to occur thanks to the to-space invariant.
1920         heap->IncrementDisableThreadFlip(soa.Self());
1921       }
1922     }
1923     if (s->IsCompressed()) {
1924       if (is_copy != nullptr) {
1925         *is_copy = JNI_TRUE;
1926       }
1927       int32_t length = s->GetLength();
1928       jchar* chars = new jchar[length];
1929       for (int i = 0; i < length; ++i) {
1930         chars[i] = s->CharAt(i);
1931       }
1932       return chars;
1933     } else {
1934       if (is_copy != nullptr) {
1935         *is_copy = JNI_FALSE;
1936       }
1937       return static_cast<jchar*>(s->GetValue());
1938     }
1939   }
1940 
ReleaseStringCritical(JNIEnv * env,jstring java_string,const jchar * chars)1941   static void ReleaseStringCritical(JNIEnv* env,
1942                                     jstring java_string,
1943                                     const jchar* chars) {
1944     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_string);
1945     ScopedObjectAccess soa(env);
1946     gc::Heap* heap = Runtime::Current()->GetHeap();
1947     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1948     if (heap->IsMovableObject(s)) {
1949       if (!kUseReadBarrier) {
1950         heap->DecrementDisableMovingGC(soa.Self());
1951       } else {
1952         heap->DecrementDisableThreadFlip(soa.Self());
1953       }
1954     }
1955     if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
1956       delete[] chars;
1957     }
1958   }
1959 
GetStringUTFChars(JNIEnv * env,jstring java_string,jboolean * is_copy)1960   static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
1961     if (java_string == nullptr) {
1962       return nullptr;
1963     }
1964     if (is_copy != nullptr) {
1965       *is_copy = JNI_TRUE;
1966     }
1967     ScopedObjectAccess soa(env);
1968     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
1969     size_t byte_count = s->GetUtfLength();
1970     char* bytes = new char[byte_count + 1];
1971     CHECK(bytes != nullptr);  // bionic aborts anyway.
1972     if (s->IsCompressed()) {
1973       for (size_t i = 0; i < byte_count; ++i) {
1974         bytes[i] = s->CharAt(i);
1975       }
1976     } else {
1977       const uint16_t* chars = s->GetValue();
1978       ConvertUtf16ToModifiedUtf8(bytes, byte_count, chars, s->GetLength());
1979     }
1980     bytes[byte_count] = '\0';
1981     return bytes;
1982   }
1983 
ReleaseStringUTFChars(JNIEnv *,jstring,const char * chars)1984   static void ReleaseStringUTFChars(JNIEnv*, jstring, const char* chars) {
1985     delete[] chars;
1986   }
1987 
GetArrayLength(JNIEnv * env,jarray java_array)1988   static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
1989     CHECK_NON_NULL_ARGUMENT_RETURN_ZERO(java_array);
1990     ScopedObjectAccess soa(env);
1991     ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(java_array);
1992     if (UNLIKELY(!obj->IsArrayInstance())) {
1993       soa.Vm()->JniAbortF("GetArrayLength", "not an array: %s", obj->PrettyTypeOf().c_str());
1994       return 0;
1995     }
1996     ObjPtr<mirror::Array> array = obj->AsArray();
1997     return array->GetLength();
1998   }
1999 
GetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index)2000   static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
2001     CHECK_NON_NULL_ARGUMENT(java_array);
2002     ScopedObjectAccess soa(env);
2003     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2004         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2005     return soa.AddLocalReference<jobject>(array->Get(index));
2006   }
2007 
SetObjectArrayElement(JNIEnv * env,jobjectArray java_array,jsize index,jobject java_value)2008   static void SetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index,
2009                                     jobject java_value) {
2010     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2011     ScopedObjectAccess soa(env);
2012     ObjPtr<mirror::ObjectArray<mirror::Object>> array =
2013         soa.Decode<mirror::ObjectArray<mirror::Object>>(java_array);
2014     ObjPtr<mirror::Object> value = soa.Decode<mirror::Object>(java_value);
2015     array->Set<false>(index, value);
2016   }
2017 
NewBooleanArray(JNIEnv * env,jsize length)2018   static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
2019     return NewPrimitiveArray<jbooleanArray, mirror::BooleanArray>(env, length);
2020   }
2021 
NewByteArray(JNIEnv * env,jsize length)2022   static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
2023     return NewPrimitiveArray<jbyteArray, mirror::ByteArray>(env, length);
2024   }
2025 
NewCharArray(JNIEnv * env,jsize length)2026   static jcharArray NewCharArray(JNIEnv* env, jsize length) {
2027     return NewPrimitiveArray<jcharArray, mirror::CharArray>(env, length);
2028   }
2029 
NewDoubleArray(JNIEnv * env,jsize length)2030   static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
2031     return NewPrimitiveArray<jdoubleArray, mirror::DoubleArray>(env, length);
2032   }
2033 
NewFloatArray(JNIEnv * env,jsize length)2034   static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
2035     return NewPrimitiveArray<jfloatArray, mirror::FloatArray>(env, length);
2036   }
2037 
NewIntArray(JNIEnv * env,jsize length)2038   static jintArray NewIntArray(JNIEnv* env, jsize length) {
2039     return NewPrimitiveArray<jintArray, mirror::IntArray>(env, length);
2040   }
2041 
NewLongArray(JNIEnv * env,jsize length)2042   static jlongArray NewLongArray(JNIEnv* env, jsize length) {
2043     return NewPrimitiveArray<jlongArray, mirror::LongArray>(env, length);
2044   }
2045 
NewObjectArray(JNIEnv * env,jsize length,jclass element_jclass,jobject initial_element)2046   static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass,
2047                                      jobject initial_element) {
2048     if (UNLIKELY(length < 0)) {
2049       JavaVmExtFromEnv(env)->JniAbortF("NewObjectArray", "negative array length: %d", length);
2050       return nullptr;
2051     }
2052     CHECK_NON_NULL_ARGUMENT(element_jclass);
2053 
2054     // Compute the array class corresponding to the given element class.
2055     ScopedObjectAccess soa(env);
2056     ObjPtr<mirror::Class> array_class;
2057     {
2058       ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(element_jclass);
2059       if (UNLIKELY(element_class->IsPrimitive())) {
2060         soa.Vm()->JniAbortF("NewObjectArray",
2061                             "not an object type: %s",
2062                             element_class->PrettyDescriptor().c_str());
2063         return nullptr;
2064       }
2065       ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2066       array_class = class_linker->FindArrayClass(soa.Self(), element_class);
2067       if (UNLIKELY(array_class == nullptr)) {
2068         return nullptr;
2069       }
2070     }
2071 
2072     // Allocate and initialize if necessary.
2073     ObjPtr<mirror::ObjectArray<mirror::Object>> result =
2074         mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), array_class, length);
2075     if (result != nullptr && initial_element != nullptr) {
2076       ObjPtr<mirror::Object> initial_object = soa.Decode<mirror::Object>(initial_element);
2077       if (initial_object != nullptr) {
2078         ObjPtr<mirror::Class> element_class = result->GetClass()->GetComponentType();
2079         if (UNLIKELY(!element_class->IsAssignableFrom(initial_object->GetClass()))) {
2080           soa.Vm()->JniAbortF("NewObjectArray", "cannot assign object of type '%s' to array with "
2081                               "element type of '%s'",
2082                               mirror::Class::PrettyDescriptor(initial_object->GetClass()).c_str(),
2083                               element_class->PrettyDescriptor().c_str());
2084           return nullptr;
2085         } else {
2086           for (jsize i = 0; i < length; ++i) {
2087             result->SetWithoutChecks<false>(i, initial_object);
2088           }
2089         }
2090       }
2091     }
2092     return soa.AddLocalReference<jobjectArray>(result);
2093   }
2094 
NewShortArray(JNIEnv * env,jsize length)2095   static jshortArray NewShortArray(JNIEnv* env, jsize length) {
2096     return NewPrimitiveArray<jshortArray, mirror::ShortArray>(env, length);
2097   }
2098 
GetPrimitiveArrayCritical(JNIEnv * env,jarray java_array,jboolean * is_copy)2099   static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
2100     CHECK_NON_NULL_ARGUMENT(java_array);
2101     ScopedObjectAccess soa(env);
2102     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2103     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2104       soa.Vm()->JniAbortF("GetPrimitiveArrayCritical", "expected primitive array, given %s",
2105                           array->GetClass()->PrettyDescriptor().c_str());
2106       return nullptr;
2107     }
2108     gc::Heap* heap = Runtime::Current()->GetHeap();
2109     if (heap->IsMovableObject(array)) {
2110       if (!kUseReadBarrier) {
2111         heap->IncrementDisableMovingGC(soa.Self());
2112       } else {
2113         // For the CC collector, we only need to wait for the thread flip rather than the whole GC
2114         // to occur thanks to the to-space invariant.
2115         heap->IncrementDisableThreadFlip(soa.Self());
2116       }
2117       // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
2118       array = soa.Decode<mirror::Array>(java_array);
2119     }
2120     if (is_copy != nullptr) {
2121       *is_copy = JNI_FALSE;
2122     }
2123     return array->GetRawData(array->GetClass()->GetComponentSize(), 0);
2124   }
2125 
ReleasePrimitiveArrayCritical(JNIEnv * env,jarray java_array,void * elements,jint mode)2126   static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray java_array, void* elements,
2127                                             jint mode) {
2128     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2129     ScopedObjectAccess soa(env);
2130     ObjPtr<mirror::Array> array = soa.Decode<mirror::Array>(java_array);
2131     if (UNLIKELY(!array->GetClass()->IsPrimitiveArray())) {
2132       soa.Vm()->JniAbortF("ReleasePrimitiveArrayCritical", "expected primitive array, given %s",
2133                           array->GetClass()->PrettyDescriptor().c_str());
2134       return;
2135     }
2136     const size_t component_size = array->GetClass()->GetComponentSize();
2137     ReleasePrimitiveArray(soa, array, component_size, elements, mode);
2138   }
2139 
GetBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * is_copy)2140   static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
2141     return GetPrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, is_copy);
2142   }
2143 
GetByteArrayElements(JNIEnv * env,jbyteArray array,jboolean * is_copy)2144   static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
2145     return GetPrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, is_copy);
2146   }
2147 
GetCharArrayElements(JNIEnv * env,jcharArray array,jboolean * is_copy)2148   static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
2149     return GetPrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, is_copy);
2150   }
2151 
GetDoubleArrayElements(JNIEnv * env,jdoubleArray array,jboolean * is_copy)2152   static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
2153     return GetPrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, is_copy);
2154   }
2155 
GetFloatArrayElements(JNIEnv * env,jfloatArray array,jboolean * is_copy)2156   static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
2157     return GetPrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, is_copy);
2158   }
2159 
GetIntArrayElements(JNIEnv * env,jintArray array,jboolean * is_copy)2160   static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
2161     return GetPrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, is_copy);
2162   }
2163 
GetLongArrayElements(JNIEnv * env,jlongArray array,jboolean * is_copy)2164   static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
2165     return GetPrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, is_copy);
2166   }
2167 
GetShortArrayElements(JNIEnv * env,jshortArray array,jboolean * is_copy)2168   static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
2169     return GetPrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, is_copy);
2170   }
2171 
ReleaseBooleanArrayElements(JNIEnv * env,jbooleanArray array,jboolean * elements,jint mode)2172   static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* elements,
2173                                           jint mode) {
2174     ReleasePrimitiveArray<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, elements,
2175                                                                          mode);
2176   }
2177 
ReleaseByteArrayElements(JNIEnv * env,jbyteArray array,jbyte * elements,jint mode)2178   static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte* elements, jint mode) {
2179     ReleasePrimitiveArray<jbyteArray, jbyte, mirror::ByteArray>(env, array, elements, mode);
2180   }
2181 
ReleaseCharArrayElements(JNIEnv * env,jcharArray array,jchar * elements,jint mode)2182   static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar* elements, jint mode) {
2183     ReleasePrimitiveArray<jcharArray, jchar, mirror::CharArray>(env, array, elements, mode);
2184   }
2185 
ReleaseDoubleArrayElements(JNIEnv * env,jdoubleArray array,jdouble * elements,jint mode)2186   static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble* elements,
2187                                          jint mode) {
2188     ReleasePrimitiveArray<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, elements, mode);
2189   }
2190 
ReleaseFloatArrayElements(JNIEnv * env,jfloatArray array,jfloat * elements,jint mode)2191   static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat* elements,
2192                                         jint mode) {
2193     ReleasePrimitiveArray<jfloatArray, jfloat, mirror::FloatArray>(env, array, elements, mode);
2194   }
2195 
ReleaseIntArrayElements(JNIEnv * env,jintArray array,jint * elements,jint mode)2196   static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint* elements, jint mode) {
2197     ReleasePrimitiveArray<jintArray, jint, mirror::IntArray>(env, array, elements, mode);
2198   }
2199 
ReleaseLongArrayElements(JNIEnv * env,jlongArray array,jlong * elements,jint mode)2200   static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong* elements, jint mode) {
2201     ReleasePrimitiveArray<jlongArray, jlong, mirror::LongArray>(env, array, elements, mode);
2202   }
2203 
ReleaseShortArrayElements(JNIEnv * env,jshortArray array,jshort * elements,jint mode)2204   static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort* elements,
2205                                         jint mode) {
2206     ReleasePrimitiveArray<jshortArray, jshort, mirror::ShortArray>(env, array, elements, mode);
2207   }
2208 
GetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,jboolean * buf)2209   static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2210                                     jboolean* buf) {
2211     GetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2212                                                                            length, buf);
2213   }
2214 
GetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,jbyte * buf)2215   static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2216                                  jbyte* buf) {
2217     GetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2218   }
2219 
GetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,jchar * buf)2220   static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2221                                  jchar* buf) {
2222     GetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2223   }
2224 
GetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,jdouble * buf)2225   static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2226                                    jdouble* buf) {
2227     GetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2228                                                                         buf);
2229   }
2230 
GetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,jfloat * buf)2231   static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2232                                   jfloat* buf) {
2233     GetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2234                                                                      buf);
2235   }
2236 
GetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,jint * buf)2237   static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2238                                 jint* buf) {
2239     GetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2240   }
2241 
GetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,jlong * buf)2242   static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2243                                  jlong* buf) {
2244     GetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2245   }
2246 
GetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,jshort * buf)2247   static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2248                                   jshort* buf) {
2249     GetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2250                                                                      buf);
2251   }
2252 
SetBooleanArrayRegion(JNIEnv * env,jbooleanArray array,jsize start,jsize length,const jboolean * buf)2253   static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length,
2254                                     const jboolean* buf) {
2255     SetPrimitiveArrayRegion<jbooleanArray, jboolean, mirror::BooleanArray>(env, array, start,
2256                                                                            length, buf);
2257   }
2258 
SetByteArrayRegion(JNIEnv * env,jbyteArray array,jsize start,jsize length,const jbyte * buf)2259   static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length,
2260                                  const jbyte* buf) {
2261     SetPrimitiveArrayRegion<jbyteArray, jbyte, mirror::ByteArray>(env, array, start, length, buf);
2262   }
2263 
SetCharArrayRegion(JNIEnv * env,jcharArray array,jsize start,jsize length,const jchar * buf)2264   static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length,
2265                                  const jchar* buf) {
2266     SetPrimitiveArrayRegion<jcharArray, jchar, mirror::CharArray>(env, array, start, length, buf);
2267   }
2268 
SetDoubleArrayRegion(JNIEnv * env,jdoubleArray array,jsize start,jsize length,const jdouble * buf)2269   static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length,
2270                                    const jdouble* buf) {
2271     SetPrimitiveArrayRegion<jdoubleArray, jdouble, mirror::DoubleArray>(env, array, start, length,
2272                                                                         buf);
2273   }
2274 
SetFloatArrayRegion(JNIEnv * env,jfloatArray array,jsize start,jsize length,const jfloat * buf)2275   static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length,
2276                                   const jfloat* buf) {
2277     SetPrimitiveArrayRegion<jfloatArray, jfloat, mirror::FloatArray>(env, array, start, length,
2278                                                                      buf);
2279   }
2280 
SetIntArrayRegion(JNIEnv * env,jintArray array,jsize start,jsize length,const jint * buf)2281   static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length,
2282                                 const jint* buf) {
2283     SetPrimitiveArrayRegion<jintArray, jint, mirror::IntArray>(env, array, start, length, buf);
2284   }
2285 
SetLongArrayRegion(JNIEnv * env,jlongArray array,jsize start,jsize length,const jlong * buf)2286   static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length,
2287                                  const jlong* buf) {
2288     SetPrimitiveArrayRegion<jlongArray, jlong, mirror::LongArray>(env, array, start, length, buf);
2289   }
2290 
SetShortArrayRegion(JNIEnv * env,jshortArray array,jsize start,jsize length,const jshort * buf)2291   static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length,
2292                                   const jshort* buf) {
2293     SetPrimitiveArrayRegion<jshortArray, jshort, mirror::ShortArray>(env, array, start, length,
2294                                                                      buf);
2295   }
2296 
RegisterNatives(JNIEnv * env,jclass java_class,const JNINativeMethod * methods,jint method_count)2297   static jint RegisterNatives(JNIEnv* env,
2298                               jclass java_class,
2299                               const JNINativeMethod* methods,
2300                               jint method_count) {
2301     if (UNLIKELY(method_count < 0)) {
2302       JavaVmExtFromEnv(env)->JniAbortF("RegisterNatives", "negative method count: %d",
2303                                        method_count);
2304       return JNI_ERR;  // Not reached except in unit tests.
2305     }
2306     CHECK_NON_NULL_ARGUMENT_FN_NAME("RegisterNatives", java_class, JNI_ERR);
2307     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
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 = class_linker->RegisterNative(soa.Self(), m, 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     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2439     auto pointer_size = class_linker->GetImagePointerSize();
2440     for (auto& m : c->GetMethods(pointer_size)) {
2441       if (m.IsNative()) {
2442         class_linker->UnregisterNative(soa.Self(), &m);
2443         unregistered_count++;
2444       }
2445     }
2446 
2447     if (unregistered_count == 0) {
2448       LOG(WARNING) << "JNI UnregisterNatives: attempt to unregister native methods of class '"
2449           << mirror::Class::PrettyDescriptor(c) << "' that contains no native methods";
2450     }
2451     return JNI_OK;
2452   }
2453 
MonitorEnter(JNIEnv * env,jobject java_object)2454   static jint MonitorEnter(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2455     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2456     ScopedObjectAccess soa(env);
2457     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2458     o = o->MonitorEnter(soa.Self());
2459     if (soa.Self()->HoldsLock(o)) {
2460       soa.Env()->monitors_.Add(o);
2461     }
2462     if (soa.Self()->IsExceptionPending()) {
2463       return JNI_ERR;
2464     }
2465     return JNI_OK;
2466   }
2467 
MonitorExit(JNIEnv * env,jobject java_object)2468   static jint MonitorExit(JNIEnv* env, jobject java_object) NO_THREAD_SAFETY_ANALYSIS {
2469     CHECK_NON_NULL_ARGUMENT_RETURN(java_object, JNI_ERR);
2470     ScopedObjectAccess soa(env);
2471     ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(java_object);
2472     bool remove_mon = soa.Self()->HoldsLock(o);
2473     o->MonitorExit(soa.Self());
2474     if (remove_mon) {
2475       soa.Env()->monitors_.Remove(o);
2476     }
2477     if (soa.Self()->IsExceptionPending()) {
2478       return JNI_ERR;
2479     }
2480     return JNI_OK;
2481   }
2482 
GetJavaVM(JNIEnv * env,JavaVM ** vm)2483   static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
2484     CHECK_NON_NULL_ARGUMENT_RETURN(vm, JNI_ERR);
2485     Runtime* runtime = Runtime::Current();
2486     if (runtime != nullptr) {
2487       *vm = runtime->GetJavaVM();
2488     } else {
2489       *vm = nullptr;
2490     }
2491     return (*vm != nullptr) ? JNI_OK : JNI_ERR;
2492   }
2493 
NewDirectByteBuffer(JNIEnv * env,void * address,jlong capacity)2494   static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
2495     if (capacity < 0) {
2496       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64,
2497                                        capacity);
2498       return nullptr;
2499     }
2500     if (address == nullptr && capacity != 0) {
2501       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2502                                        "non-zero capacity for nullptr pointer: %" PRId64, capacity);
2503       return nullptr;
2504     }
2505 
2506     // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
2507     if (capacity > INT_MAX) {
2508       JavaVmExtFromEnv(env)->JniAbortF("NewDirectByteBuffer",
2509                                        "buffer capacity greater than maximum jint: %" PRId64,
2510                                        capacity);
2511       return nullptr;
2512     }
2513     jlong address_arg = reinterpret_cast<jlong>(address);
2514     jint capacity_arg = static_cast<jint>(capacity);
2515 
2516     jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2517                                     WellKnownClasses::java_nio_DirectByteBuffer_init,
2518                                     address_arg, capacity_arg);
2519     return static_cast<JNIEnvExt*>(env)->self_->IsExceptionPending() ? nullptr : result;
2520   }
2521 
GetDirectBufferAddress(JNIEnv * env,jobject java_buffer)2522   static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
2523     // Return null if |java_buffer| is not defined.
2524     if (java_buffer == nullptr) {
2525       return nullptr;
2526     }
2527 
2528     // Return null if |java_buffer| is not a java.nio.Buffer instance.
2529     if (!IsInstanceOf(env, java_buffer, WellKnownClasses::java_nio_Buffer)) {
2530       return nullptr;
2531     }
2532 
2533     // Buffer.address is non-null when the |java_buffer| is direct.
2534     return reinterpret_cast<void*>(env->GetLongField(
2535         java_buffer, WellKnownClasses::java_nio_Buffer_address));
2536   }
2537 
GetDirectBufferCapacity(JNIEnv * env,jobject java_buffer)2538   static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
2539     if (java_buffer == nullptr) {
2540       return -1;
2541     }
2542 
2543     if (!IsInstanceOf(env, java_buffer, WellKnownClasses::java_nio_Buffer)) {
2544       return -1;
2545     }
2546 
2547     // When checking the buffer capacity, it's important to note that a zero-sized direct buffer
2548     // may have a null address field which means we can't tell whether it is direct or not.
2549     // We therefore call Buffer.isDirect(). One path that creates such a buffer is
2550     // FileChannel.map() if the file size is zero.
2551     //
2552     // NB GetDirectBufferAddress() does not need to call Buffer.isDirect() since it is only
2553     // able return a valid address if the Buffer address field is not-null.
2554     jboolean direct = env->CallBooleanMethod(java_buffer,
2555                                              WellKnownClasses::java_nio_Buffer_isDirect);
2556     if (!direct) {
2557       return -1;
2558     }
2559 
2560     return static_cast<jlong>(env->GetIntField(
2561         java_buffer, WellKnownClasses::java_nio_Buffer_capacity));
2562   }
2563 
GetObjectRefType(JNIEnv * env ATTRIBUTE_UNUSED,jobject java_object)2564   static jobjectRefType GetObjectRefType(JNIEnv* env ATTRIBUTE_UNUSED, jobject java_object) {
2565     if (java_object == nullptr) {
2566       return JNIInvalidRefType;
2567     }
2568 
2569     // Do we definitely know what kind of reference this is?
2570     IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2571     IndirectRefKind kind = IndirectReferenceTable::GetIndirectRefKind(ref);
2572     switch (kind) {
2573     case kLocal:
2574       return JNILocalRefType;
2575     case kGlobal:
2576       return JNIGlobalRefType;
2577     case kWeakGlobal:
2578       return JNIWeakGlobalRefType;
2579     case kHandleScopeOrInvalid:
2580       // Assume value is in a handle scope.
2581       return JNILocalRefType;
2582     }
2583     LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2584     UNREACHABLE();
2585   }
2586 
2587  private:
EnsureLocalCapacityInternal(ScopedObjectAccess & soa,jint desired_capacity,const char * caller)2588   static jint EnsureLocalCapacityInternal(ScopedObjectAccess& soa, jint desired_capacity,
2589                                           const char* caller)
2590       REQUIRES_SHARED(Locks::mutator_lock_) {
2591     if (desired_capacity < 0) {
2592       LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2593       return JNI_ERR;
2594     }
2595 
2596     std::string error_msg;
2597     if (!soa.Env()->locals_.EnsureFreeCapacity(static_cast<size_t>(desired_capacity), &error_msg)) {
2598       std::string caller_error = android::base::StringPrintf("%s: %s", caller, error_msg.c_str());
2599       soa.Self()->ThrowOutOfMemoryError(caller_error.c_str());
2600       return JNI_ERR;
2601     }
2602     return JNI_OK;
2603   }
2604 
2605   template<typename JniT, typename ArtT>
NewPrimitiveArray(JNIEnv * env,jsize length)2606   static JniT NewPrimitiveArray(JNIEnv* env, jsize length) {
2607     ScopedObjectAccess soa(env);
2608     if (UNLIKELY(length < 0)) {
2609       soa.Vm()->JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2610       return nullptr;
2611     }
2612     ObjPtr<ArtT> result = ArtT::Alloc(soa.Self(), length);
2613     return soa.AddLocalReference<JniT>(result);
2614   }
2615 
2616   template <typename JArrayT, typename ElementT, typename ArtArrayT>
DecodeAndCheckArrayType(ScopedObjectAccess & soa,JArrayT java_array,const char * fn_name,const char * operation)2617   static ObjPtr<ArtArrayT> DecodeAndCheckArrayType(ScopedObjectAccess& soa,
2618                                                    JArrayT java_array,
2619                                                    const char* fn_name,
2620                                                    const char* operation)
2621       REQUIRES_SHARED(Locks::mutator_lock_) {
2622     ObjPtr<ArtArrayT> array = soa.Decode<ArtArrayT>(java_array);
2623     ObjPtr<mirror::Class> expected_array_class = GetClassRoot<ArtArrayT>();
2624     if (UNLIKELY(expected_array_class != array->GetClass())) {
2625       soa.Vm()->JniAbortF(fn_name,
2626                           "attempt to %s %s primitive array elements with an object of type %s",
2627                           operation,
2628                           mirror::Class::PrettyDescriptor(
2629                               expected_array_class->GetComponentType()).c_str(),
2630                           mirror::Class::PrettyDescriptor(array->GetClass()).c_str());
2631       return nullptr;
2632     }
2633     DCHECK_EQ(sizeof(ElementT), array->GetClass()->GetComponentSize());
2634     return array;
2635   }
2636 
2637   template <typename ArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArray(JNIEnv * env,ArrayT java_array,jboolean * is_copy)2638   static ElementT* GetPrimitiveArray(JNIEnv* env, ArrayT java_array, jboolean* is_copy) {
2639     CHECK_NON_NULL_ARGUMENT(java_array);
2640     ScopedObjectAccess soa(env);
2641     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2642         soa, java_array, "GetArrayElements", "get");
2643     if (UNLIKELY(array == nullptr)) {
2644       return nullptr;
2645     }
2646     // Only make a copy if necessary.
2647     if (Runtime::Current()->GetHeap()->IsMovableObject(array)) {
2648       if (is_copy != nullptr) {
2649         *is_copy = JNI_TRUE;
2650       }
2651       const size_t component_size = sizeof(ElementT);
2652       size_t size = array->GetLength() * component_size;
2653       void* data = new uint64_t[RoundUp(size, 8) / 8];
2654       memcpy(data, array->GetData(), size);
2655       return reinterpret_cast<ElementT*>(data);
2656     } else {
2657       if (is_copy != nullptr) {
2658         *is_copy = JNI_FALSE;
2659       }
2660       return reinterpret_cast<ElementT*>(array->GetData());
2661     }
2662   }
2663 
2664   template <typename ArrayT, typename ElementT, typename ArtArrayT>
ReleasePrimitiveArray(JNIEnv * env,ArrayT java_array,ElementT * elements,jint mode)2665   static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, ElementT* elements, jint mode) {
2666     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2667     ScopedObjectAccess soa(env);
2668     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<ArrayT, ElementT, ArtArrayT>(
2669         soa, java_array, "ReleaseArrayElements", "release");
2670     if (array == nullptr) {
2671       return;
2672     }
2673     ReleasePrimitiveArray(soa, array, sizeof(ElementT), elements, mode);
2674   }
2675 
ReleasePrimitiveArray(ScopedObjectAccess & soa,ObjPtr<mirror::Array> array,size_t component_size,void * elements,jint mode)2676   static void ReleasePrimitiveArray(ScopedObjectAccess& soa,
2677                                     ObjPtr<mirror::Array> array,
2678                                     size_t component_size,
2679                                     void* elements,
2680                                     jint mode)
2681       REQUIRES_SHARED(Locks::mutator_lock_) {
2682     void* array_data = array->GetRawData(component_size, 0);
2683     gc::Heap* heap = Runtime::Current()->GetHeap();
2684     bool is_copy = array_data != elements;
2685     size_t bytes = array->GetLength() * component_size;
2686     if (is_copy) {
2687       // Integrity check: If elements is not the same as the java array's data, it better not be a
2688       // heap address. TODO: This might be slow to check, may be worth keeping track of which
2689       // copies we make?
2690       if (heap->IsNonDiscontinuousSpaceHeapAddress(elements)) {
2691         soa.Vm()->JniAbortF("ReleaseArrayElements",
2692                             "invalid element pointer %p, array elements are %p",
2693                             reinterpret_cast<void*>(elements), array_data);
2694         return;
2695       }
2696       if (mode != JNI_ABORT) {
2697         memcpy(array_data, elements, bytes);
2698       } else if (kWarnJniAbort && memcmp(array_data, elements, bytes) != 0) {
2699         // Warn if we have JNI_ABORT and the arrays don't match since this is usually an error.
2700         LOG(WARNING) << "Possible incorrect JNI_ABORT in Release*ArrayElements";
2701         soa.Self()->DumpJavaStack(LOG_STREAM(WARNING));
2702       }
2703     }
2704     if (mode != JNI_COMMIT) {
2705       if (is_copy) {
2706         delete[] reinterpret_cast<uint64_t*>(elements);
2707       } else if (heap->IsMovableObject(array)) {
2708         // Non copy to a movable object must means that we had disabled the moving GC.
2709         if (!kUseReadBarrier) {
2710           heap->DecrementDisableMovingGC(soa.Self());
2711         } else {
2712           heap->DecrementDisableThreadFlip(soa.Self());
2713         }
2714       }
2715     }
2716   }
2717 
2718   template <typename JArrayT, typename ElementT, typename ArtArrayT>
GetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,ElementT * buf)2719   static void GetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2720                                       jsize start, jsize length, ElementT* buf) {
2721     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2722     ScopedObjectAccess soa(env);
2723     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2724         soa, java_array, "GetPrimitiveArrayRegion", "get region of");
2725     if (array != nullptr) {
2726       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2727         ThrowAIOOBE(soa, array, start, length, "src");
2728       } else {
2729         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2730         ElementT* data = array->GetData();
2731         memcpy(buf, data + start, length * sizeof(ElementT));
2732       }
2733     }
2734   }
2735 
2736   template <typename JArrayT, typename ElementT, typename ArtArrayT>
SetPrimitiveArrayRegion(JNIEnv * env,JArrayT java_array,jsize start,jsize length,const ElementT * buf)2737   static void SetPrimitiveArrayRegion(JNIEnv* env, JArrayT java_array,
2738                                       jsize start, jsize length, const ElementT* buf) {
2739     CHECK_NON_NULL_ARGUMENT_RETURN_VOID(java_array);
2740     ScopedObjectAccess soa(env);
2741     ObjPtr<ArtArrayT> array = DecodeAndCheckArrayType<JArrayT, ElementT, ArtArrayT>(
2742         soa, java_array, "SetPrimitiveArrayRegion", "set region of");
2743     if (array != nullptr) {
2744       if (start < 0 || length < 0 || length > array->GetLength() - start) {
2745         ThrowAIOOBE(soa, array, start, length, "dst");
2746       } else {
2747         CHECK_NON_NULL_MEMCPY_ARGUMENT(length, buf);
2748         ElementT* data = array->GetData();
2749         memcpy(data + start, buf, length * sizeof(ElementT));
2750       }
2751     }
2752   }
2753 };
2754 
2755 template<bool kEnableIndexIds>
2756 struct JniNativeInterfaceFunctions {
2757   using JNIImpl = JNI<kEnableIndexIds>;
2758   static constexpr JNINativeInterface gJniNativeInterface = {
2759     nullptr,  // reserved0.
2760     nullptr,  // reserved1.
2761     nullptr,  // reserved2.
2762     nullptr,  // reserved3.
2763     JNIImpl::GetVersion,
2764     JNIImpl::DefineClass,
2765     JNIImpl::FindClass,
2766     JNIImpl::FromReflectedMethod,
2767     JNIImpl::FromReflectedField,
2768     JNIImpl::ToReflectedMethod,
2769     JNIImpl::GetSuperclass,
2770     JNIImpl::IsAssignableFrom,
2771     JNIImpl::ToReflectedField,
2772     JNIImpl::Throw,
2773     JNIImpl::ThrowNew,
2774     JNIImpl::ExceptionOccurred,
2775     JNIImpl::ExceptionDescribe,
2776     JNIImpl::ExceptionClear,
2777     JNIImpl::FatalError,
2778     JNIImpl::PushLocalFrame,
2779     JNIImpl::PopLocalFrame,
2780     JNIImpl::NewGlobalRef,
2781     JNIImpl::DeleteGlobalRef,
2782     JNIImpl::DeleteLocalRef,
2783     JNIImpl::IsSameObject,
2784     JNIImpl::NewLocalRef,
2785     JNIImpl::EnsureLocalCapacity,
2786     JNIImpl::AllocObject,
2787     JNIImpl::NewObject,
2788     JNIImpl::NewObjectV,
2789     JNIImpl::NewObjectA,
2790     JNIImpl::GetObjectClass,
2791     JNIImpl::IsInstanceOf,
2792     JNIImpl::GetMethodID,
2793     JNIImpl::CallObjectMethod,
2794     JNIImpl::CallObjectMethodV,
2795     JNIImpl::CallObjectMethodA,
2796     JNIImpl::CallBooleanMethod,
2797     JNIImpl::CallBooleanMethodV,
2798     JNIImpl::CallBooleanMethodA,
2799     JNIImpl::CallByteMethod,
2800     JNIImpl::CallByteMethodV,
2801     JNIImpl::CallByteMethodA,
2802     JNIImpl::CallCharMethod,
2803     JNIImpl::CallCharMethodV,
2804     JNIImpl::CallCharMethodA,
2805     JNIImpl::CallShortMethod,
2806     JNIImpl::CallShortMethodV,
2807     JNIImpl::CallShortMethodA,
2808     JNIImpl::CallIntMethod,
2809     JNIImpl::CallIntMethodV,
2810     JNIImpl::CallIntMethodA,
2811     JNIImpl::CallLongMethod,
2812     JNIImpl::CallLongMethodV,
2813     JNIImpl::CallLongMethodA,
2814     JNIImpl::CallFloatMethod,
2815     JNIImpl::CallFloatMethodV,
2816     JNIImpl::CallFloatMethodA,
2817     JNIImpl::CallDoubleMethod,
2818     JNIImpl::CallDoubleMethodV,
2819     JNIImpl::CallDoubleMethodA,
2820     JNIImpl::CallVoidMethod,
2821     JNIImpl::CallVoidMethodV,
2822     JNIImpl::CallVoidMethodA,
2823     JNIImpl::CallNonvirtualObjectMethod,
2824     JNIImpl::CallNonvirtualObjectMethodV,
2825     JNIImpl::CallNonvirtualObjectMethodA,
2826     JNIImpl::CallNonvirtualBooleanMethod,
2827     JNIImpl::CallNonvirtualBooleanMethodV,
2828     JNIImpl::CallNonvirtualBooleanMethodA,
2829     JNIImpl::CallNonvirtualByteMethod,
2830     JNIImpl::CallNonvirtualByteMethodV,
2831     JNIImpl::CallNonvirtualByteMethodA,
2832     JNIImpl::CallNonvirtualCharMethod,
2833     JNIImpl::CallNonvirtualCharMethodV,
2834     JNIImpl::CallNonvirtualCharMethodA,
2835     JNIImpl::CallNonvirtualShortMethod,
2836     JNIImpl::CallNonvirtualShortMethodV,
2837     JNIImpl::CallNonvirtualShortMethodA,
2838     JNIImpl::CallNonvirtualIntMethod,
2839     JNIImpl::CallNonvirtualIntMethodV,
2840     JNIImpl::CallNonvirtualIntMethodA,
2841     JNIImpl::CallNonvirtualLongMethod,
2842     JNIImpl::CallNonvirtualLongMethodV,
2843     JNIImpl::CallNonvirtualLongMethodA,
2844     JNIImpl::CallNonvirtualFloatMethod,
2845     JNIImpl::CallNonvirtualFloatMethodV,
2846     JNIImpl::CallNonvirtualFloatMethodA,
2847     JNIImpl::CallNonvirtualDoubleMethod,
2848     JNIImpl::CallNonvirtualDoubleMethodV,
2849     JNIImpl::CallNonvirtualDoubleMethodA,
2850     JNIImpl::CallNonvirtualVoidMethod,
2851     JNIImpl::CallNonvirtualVoidMethodV,
2852     JNIImpl::CallNonvirtualVoidMethodA,
2853     JNIImpl::GetFieldID,
2854     JNIImpl::GetObjectField,
2855     JNIImpl::GetBooleanField,
2856     JNIImpl::GetByteField,
2857     JNIImpl::GetCharField,
2858     JNIImpl::GetShortField,
2859     JNIImpl::GetIntField,
2860     JNIImpl::GetLongField,
2861     JNIImpl::GetFloatField,
2862     JNIImpl::GetDoubleField,
2863     JNIImpl::SetObjectField,
2864     JNIImpl::SetBooleanField,
2865     JNIImpl::SetByteField,
2866     JNIImpl::SetCharField,
2867     JNIImpl::SetShortField,
2868     JNIImpl::SetIntField,
2869     JNIImpl::SetLongField,
2870     JNIImpl::SetFloatField,
2871     JNIImpl::SetDoubleField,
2872     JNIImpl::GetStaticMethodID,
2873     JNIImpl::CallStaticObjectMethod,
2874     JNIImpl::CallStaticObjectMethodV,
2875     JNIImpl::CallStaticObjectMethodA,
2876     JNIImpl::CallStaticBooleanMethod,
2877     JNIImpl::CallStaticBooleanMethodV,
2878     JNIImpl::CallStaticBooleanMethodA,
2879     JNIImpl::CallStaticByteMethod,
2880     JNIImpl::CallStaticByteMethodV,
2881     JNIImpl::CallStaticByteMethodA,
2882     JNIImpl::CallStaticCharMethod,
2883     JNIImpl::CallStaticCharMethodV,
2884     JNIImpl::CallStaticCharMethodA,
2885     JNIImpl::CallStaticShortMethod,
2886     JNIImpl::CallStaticShortMethodV,
2887     JNIImpl::CallStaticShortMethodA,
2888     JNIImpl::CallStaticIntMethod,
2889     JNIImpl::CallStaticIntMethodV,
2890     JNIImpl::CallStaticIntMethodA,
2891     JNIImpl::CallStaticLongMethod,
2892     JNIImpl::CallStaticLongMethodV,
2893     JNIImpl::CallStaticLongMethodA,
2894     JNIImpl::CallStaticFloatMethod,
2895     JNIImpl::CallStaticFloatMethodV,
2896     JNIImpl::CallStaticFloatMethodA,
2897     JNIImpl::CallStaticDoubleMethod,
2898     JNIImpl::CallStaticDoubleMethodV,
2899     JNIImpl::CallStaticDoubleMethodA,
2900     JNIImpl::CallStaticVoidMethod,
2901     JNIImpl::CallStaticVoidMethodV,
2902     JNIImpl::CallStaticVoidMethodA,
2903     JNIImpl::GetStaticFieldID,
2904     JNIImpl::GetStaticObjectField,
2905     JNIImpl::GetStaticBooleanField,
2906     JNIImpl::GetStaticByteField,
2907     JNIImpl::GetStaticCharField,
2908     JNIImpl::GetStaticShortField,
2909     JNIImpl::GetStaticIntField,
2910     JNIImpl::GetStaticLongField,
2911     JNIImpl::GetStaticFloatField,
2912     JNIImpl::GetStaticDoubleField,
2913     JNIImpl::SetStaticObjectField,
2914     JNIImpl::SetStaticBooleanField,
2915     JNIImpl::SetStaticByteField,
2916     JNIImpl::SetStaticCharField,
2917     JNIImpl::SetStaticShortField,
2918     JNIImpl::SetStaticIntField,
2919     JNIImpl::SetStaticLongField,
2920     JNIImpl::SetStaticFloatField,
2921     JNIImpl::SetStaticDoubleField,
2922     JNIImpl::NewString,
2923     JNIImpl::GetStringLength,
2924     JNIImpl::GetStringChars,
2925     JNIImpl::ReleaseStringChars,
2926     JNIImpl::NewStringUTF,
2927     JNIImpl::GetStringUTFLength,
2928     JNIImpl::GetStringUTFChars,
2929     JNIImpl::ReleaseStringUTFChars,
2930     JNIImpl::GetArrayLength,
2931     JNIImpl::NewObjectArray,
2932     JNIImpl::GetObjectArrayElement,
2933     JNIImpl::SetObjectArrayElement,
2934     JNIImpl::NewBooleanArray,
2935     JNIImpl::NewByteArray,
2936     JNIImpl::NewCharArray,
2937     JNIImpl::NewShortArray,
2938     JNIImpl::NewIntArray,
2939     JNIImpl::NewLongArray,
2940     JNIImpl::NewFloatArray,
2941     JNIImpl::NewDoubleArray,
2942     JNIImpl::GetBooleanArrayElements,
2943     JNIImpl::GetByteArrayElements,
2944     JNIImpl::GetCharArrayElements,
2945     JNIImpl::GetShortArrayElements,
2946     JNIImpl::GetIntArrayElements,
2947     JNIImpl::GetLongArrayElements,
2948     JNIImpl::GetFloatArrayElements,
2949     JNIImpl::GetDoubleArrayElements,
2950     JNIImpl::ReleaseBooleanArrayElements,
2951     JNIImpl::ReleaseByteArrayElements,
2952     JNIImpl::ReleaseCharArrayElements,
2953     JNIImpl::ReleaseShortArrayElements,
2954     JNIImpl::ReleaseIntArrayElements,
2955     JNIImpl::ReleaseLongArrayElements,
2956     JNIImpl::ReleaseFloatArrayElements,
2957     JNIImpl::ReleaseDoubleArrayElements,
2958     JNIImpl::GetBooleanArrayRegion,
2959     JNIImpl::GetByteArrayRegion,
2960     JNIImpl::GetCharArrayRegion,
2961     JNIImpl::GetShortArrayRegion,
2962     JNIImpl::GetIntArrayRegion,
2963     JNIImpl::GetLongArrayRegion,
2964     JNIImpl::GetFloatArrayRegion,
2965     JNIImpl::GetDoubleArrayRegion,
2966     JNIImpl::SetBooleanArrayRegion,
2967     JNIImpl::SetByteArrayRegion,
2968     JNIImpl::SetCharArrayRegion,
2969     JNIImpl::SetShortArrayRegion,
2970     JNIImpl::SetIntArrayRegion,
2971     JNIImpl::SetLongArrayRegion,
2972     JNIImpl::SetFloatArrayRegion,
2973     JNIImpl::SetDoubleArrayRegion,
2974     JNIImpl::RegisterNatives,
2975     JNIImpl::UnregisterNatives,
2976     JNIImpl::MonitorEnter,
2977     JNIImpl::MonitorExit,
2978     JNIImpl::GetJavaVM,
2979     JNIImpl::GetStringRegion,
2980     JNIImpl::GetStringUTFRegion,
2981     JNIImpl::GetPrimitiveArrayCritical,
2982     JNIImpl::ReleasePrimitiveArrayCritical,
2983     JNIImpl::GetStringCritical,
2984     JNIImpl::ReleaseStringCritical,
2985     JNIImpl::NewWeakGlobalRef,
2986     JNIImpl::DeleteWeakGlobalRef,
2987     JNIImpl::ExceptionCheck,
2988     JNIImpl::NewDirectByteBuffer,
2989     JNIImpl::GetDirectBufferAddress,
2990     JNIImpl::GetDirectBufferCapacity,
2991     JNIImpl::GetObjectRefType,
2992   };
2993 };
2994 
GetJniNativeInterface()2995 const JNINativeInterface* GetJniNativeInterface() {
2996   // The template argument is passed down through the Encode/DecodeArtMethod/Field calls so if
2997   // JniIdType is kPointer the calls will be a simple cast with no branches. This ensures that
2998   // the normal case is still fast.
2999   return Runtime::Current()->GetJniIdType() == JniIdType::kPointer
3000              ? &JniNativeInterfaceFunctions<false>::gJniNativeInterface
3001              : &JniNativeInterfaceFunctions<true>::gJniNativeInterface;
3002 }
3003 
3004 void (*gJniSleepForeverStub[])()  = {
3005   nullptr,  // reserved0.
3006   nullptr,  // reserved1.
3007   nullptr,  // reserved2.
3008   nullptr,  // reserved3.
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   SleepForever,
3206   SleepForever,
3207   SleepForever,
3208   SleepForever,
3209   SleepForever,
3210   SleepForever,
3211   SleepForever,
3212   SleepForever,
3213   SleepForever,
3214   SleepForever,
3215   SleepForever,
3216   SleepForever,
3217   SleepForever,
3218   SleepForever,
3219   SleepForever,
3220   SleepForever,
3221   SleepForever,
3222   SleepForever,
3223   SleepForever,
3224   SleepForever,
3225   SleepForever,
3226   SleepForever,
3227   SleepForever,
3228   SleepForever,
3229   SleepForever,
3230   SleepForever,
3231   SleepForever,
3232   SleepForever,
3233   SleepForever,
3234   SleepForever,
3235   SleepForever,
3236   SleepForever,
3237   SleepForever,
3238 };
3239 
GetRuntimeShutdownNativeInterface()3240 const JNINativeInterface* GetRuntimeShutdownNativeInterface() {
3241   return reinterpret_cast<JNINativeInterface*>(&gJniSleepForeverStub);
3242 }
3243 
3244 }  // namespace art
3245 
operator <<(std::ostream & os,const jobjectRefType & rhs)3246 std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
3247   switch (rhs) {
3248   case JNIInvalidRefType:
3249     os << "JNIInvalidRefType";
3250     return os;
3251   case JNILocalRefType:
3252     os << "JNILocalRefType";
3253     return os;
3254   case JNIGlobalRefType:
3255     os << "JNIGlobalRefType";
3256     return os;
3257   case JNIWeakGlobalRefType:
3258     os << "JNIWeakGlobalRefType";
3259     return os;
3260   default:
3261     LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
3262     UNREACHABLE();
3263   }
3264 }
3265