Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 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_env_ext.h" |
| 18 | |
| 19 | #include "check_jni.h" |
| 20 | #include "indirect_reference_table.h" |
| 21 | #include "java_vm_ext.h" |
| 22 | #include "jni_internal.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | static constexpr size_t kMonitorsInitial = 32; // Arbitrary. |
| 27 | static constexpr size_t kMonitorsMax = 4096; // Arbitrary sanity check. |
| 28 | |
| 29 | static constexpr size_t kLocalsInitial = 64; // Arbitrary. |
| 30 | |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 31 | // Checking "locals" requires the mutator lock, but at creation time we're really only interested |
| 32 | // in validity, which isn't changing. To avoid grabbing the mutator lock, factored out and tagged |
| 33 | // with NO_THREAD_SAFETY_ANALYSIS. |
| 34 | static bool CheckLocalsValid(JNIEnvExt* in) NO_THREAD_SAFETY_ANALYSIS { |
| 35 | if (in == nullptr) { |
| 36 | return false; |
| 37 | } |
| 38 | return in->locals.IsValid(); |
| 39 | } |
| 40 | |
| 41 | JNIEnvExt* JNIEnvExt::Create(Thread* self_in, JavaVMExt* vm_in) { |
| 42 | std::unique_ptr<JNIEnvExt> ret(new JNIEnvExt(self_in, vm_in)); |
| 43 | if (CheckLocalsValid(ret.get())) { |
| 44 | return ret.release(); |
| 45 | } |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 49 | JNIEnvExt::JNIEnvExt(Thread* self_in, JavaVMExt* vm_in) |
| 50 | : self(self_in), |
| 51 | vm(vm_in), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 52 | local_ref_cookie(IRT_FIRST_SEGMENT), |
Andreas Gampe | 3f5881f | 2015-04-08 10:26:16 -0700 | [diff] [blame] | 53 | locals(kLocalsInitial, kLocalsMax, kLocal, false), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 54 | check_jni(false), |
| 55 | critical(0), |
| 56 | monitors("monitors", kMonitorsInitial, kMonitorsMax) { |
| 57 | functions = unchecked_functions = GetJniNativeInterface(); |
| 58 | if (vm->IsCheckJniEnabled()) { |
| 59 | SetCheckJniEnabled(true); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | JNIEnvExt::~JNIEnvExt() { |
| 64 | } |
| 65 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 66 | jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 67 | if (obj == nullptr) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj)); |
| 71 | } |
| 72 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 73 | void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 74 | if (obj != nullptr) { |
| 75 | locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void JNIEnvExt::SetCheckJniEnabled(bool enabled) { |
| 80 | check_jni = enabled; |
| 81 | functions = enabled ? GetCheckJniNativeInterface() : GetJniNativeInterface(); |
| 82 | } |
| 83 | |
| 84 | void JNIEnvExt::DumpReferenceTables(std::ostream& os) { |
| 85 | locals.Dump(os); |
| 86 | monitors.Dump(os); |
| 87 | } |
| 88 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 89 | void JNIEnvExt::PushFrame(int capacity) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 90 | UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast. |
| 91 | // TODO: take 'capacity' into account. |
| 92 | stacked_local_ref_cookies.push_back(local_ref_cookie); |
| 93 | local_ref_cookie = locals.GetSegmentState(); |
| 94 | } |
| 95 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 96 | void JNIEnvExt::PopFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 97 | locals.SetSegmentState(local_ref_cookie); |
| 98 | local_ref_cookie = stacked_local_ref_cookies.back(); |
| 99 | stacked_local_ref_cookies.pop_back(); |
| 100 | } |
| 101 | |
| 102 | Offset JNIEnvExt::SegmentStateOffset() { |
| 103 | return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) + |
| 104 | IndirectReferenceTable::SegmentStateOffset().Int32Value()); |
| 105 | } |
| 106 | |
| 107 | } // namespace art |