blob: 9673bd9728dfe4243f41f781860760e21f140bd1 [file] [log] [blame]
Mathieu Chartierc56057e2014-05-04 13:18:58 -07001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
18#define ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_
19
20#include "indirect_reference_table.h"
21
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include "android-base/stringprintf.h"
23
Mathieu Chartier8778c522016-10-04 19:06:30 -070024#include "base/dumpable.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "gc_root-inl.h"
Mathieu Chartier8778c522016-10-04 19:06:30 -070026#include "obj_ptr-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080027#include "verify_object.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070028
29namespace art {
30namespace mirror {
31class Object;
32} // namespace mirror
33
34// Verifies that the indirect table lookup is valid.
35// Returns "false" if something looks bad.
36inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
37 if (UNLIKELY(iref == nullptr)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070039 return false;
40 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): invalid %s %p",
43 GetIndirectRefKindString(kind_),
44 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070045 return false;
46 }
Andreas Gampee03662b2016-10-13 17:12:56 -070047 const uint32_t top_index = segment_state_.top_index;
48 uint32_t idx = ExtractIndex(iref);
49 if (UNLIKELY(idx >= top_index)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080050 std::string msg = android::base::StringPrintf(
Andreas Gampef1e86302016-10-03 11:42:31 -070051 "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)",
52 GetIndirectRefKindString(kind_),
53 iref,
54 idx,
Andreas Gampee03662b2016-10-13 17:12:56 -070055 top_index);
Andreas Gampef1e86302016-10-03 11:42:31 -070056 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070057 return false;
58 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070059 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080060 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
61 GetIndirectRefKindString(kind_),
62 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070063 return false;
64 }
65 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
66 return false;
67 }
68 return true;
69}
70
71// Make sure that the entry at "idx" is correctly paired with "iref".
Andreas Gampee03662b2016-10-13 17:12:56 -070072inline bool IndirectReferenceTable::CheckEntry(const char* what,
73 IndirectRef iref,
74 uint32_t idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070075 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070076 if (UNLIKELY(checkRef != iref)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080077 std::string msg = android::base::StringPrintf(
Andreas Gampef1e86302016-10-03 11:42:31 -070078 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
79 what,
80 GetIndirectRefKindString(kind_),
81 iref,
82 checkRef);
83 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070084 return false;
85 }
86 return true;
87}
88
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070089template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier8778c522016-10-04 19:06:30 -070090inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070091 if (!GetChecked(iref)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070092 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070093 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070094 uint32_t idx = ExtractIndex(iref);
Mathieu Chartier8778c522016-10-04 19:06:30 -070095 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070096 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070097 return obj;
98}
99
Mathieu Chartier8778c522016-10-04 19:06:30 -0700100inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
Jeff Hao39b6c242015-05-19 20:30:23 -0700101 if (!GetChecked(iref)) {
102 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
103 return;
104 }
105 uint32_t idx = ExtractIndex(iref);
106 table_[idx].SetReference(obj);
107}
108
Mathieu Chartier8778c522016-10-04 19:06:30 -0700109inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
110 ++serial_;
111 if (serial_ == kIRTPrevCount) {
112 serial_ = 0;
113 }
Andreas Gamped4901292017-05-30 18:41:34 -0700114 references_[serial_] = GcRoot<mirror::Object>(obj.Ptr());
Mathieu Chartier8778c522016-10-04 19:06:30 -0700115}
116
117inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
118 DCHECK_LT(serial_, kIRTPrevCount);
Andreas Gamped4901292017-05-30 18:41:34 -0700119 references_[serial_] = GcRoot<mirror::Object>(obj.Ptr());
Mathieu Chartier8778c522016-10-04 19:06:30 -0700120}
121
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700122} // namespace art
123
124#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_