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