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