blob: 24ee22759cc77e277ff66096d598c29134ee6798 [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"
Ian Rogersc0542af2014-09-03 16:16:56 -070027#include "runtime-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080028#include "verify_object.h"
Mathieu Chartierc56057e2014-05-04 13:18:58 -070029
30namespace art {
31namespace mirror {
32class Object;
33} // namespace mirror
34
35// Verifies that the indirect table lookup is valid.
36// Returns "false" if something looks bad.
37inline bool IndirectReferenceTable::GetChecked(IndirectRef iref) const {
38 if (UNLIKELY(iref == nullptr)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070039 LOG(WARNING) << "Attempt to look up nullptr " << kind_;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070040 return false;
41 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042 if (UNLIKELY(GetIndirectRefKind(iref) == kHandleScopeOrInvalid)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): invalid %s %p",
44 GetIndirectRefKindString(kind_),
45 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070046 return false;
47 }
Andreas Gampee03662b2016-10-13 17:12:56 -070048 const uint32_t top_index = segment_state_.top_index;
49 uint32_t idx = ExtractIndex(iref);
50 if (UNLIKELY(idx >= top_index)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051 std::string msg = android::base::StringPrintf(
Andreas Gampef1e86302016-10-03 11:42:31 -070052 "JNI ERROR (app bug): accessed stale %s %p (index %d in a table of size %d)",
53 GetIndirectRefKindString(kind_),
54 iref,
55 idx,
Andreas Gampee03662b2016-10-13 17:12:56 -070056 top_index);
Andreas Gampef1e86302016-10-03 11:42:31 -070057 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070058 return false;
59 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070060 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061 AbortIfNoCheckJNI(android::base::StringPrintf("JNI ERROR (app bug): accessed deleted %s %p",
62 GetIndirectRefKindString(kind_),
63 iref));
Mathieu Chartierc56057e2014-05-04 13:18:58 -070064 return false;
65 }
66 if (UNLIKELY(!CheckEntry("use", iref, idx))) {
67 return false;
68 }
69 return true;
70}
71
72// Make sure that the entry at "idx" is correctly paired with "iref".
Andreas Gampee03662b2016-10-13 17:12:56 -070073inline bool IndirectReferenceTable::CheckEntry(const char* what,
74 IndirectRef iref,
75 uint32_t idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070076 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070077 if (UNLIKELY(checkRef != iref)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080078 std::string msg = android::base::StringPrintf(
Andreas Gampef1e86302016-10-03 11:42:31 -070079 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
80 what,
81 GetIndirectRefKindString(kind_),
82 iref,
83 checkRef);
84 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070085 return false;
86 }
87 return true;
88}
89
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070090template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier8778c522016-10-04 19:06:30 -070091inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
Mathieu Chartierc56057e2014-05-04 13:18:58 -070092 if (!GetChecked(iref)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070093 return nullptr;
Mathieu Chartierc56057e2014-05-04 13:18:58 -070094 }
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070095 uint32_t idx = ExtractIndex(iref);
Mathieu Chartier8778c522016-10-04 19:06:30 -070096 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070097 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070098 return obj;
99}
100
Mathieu Chartier8778c522016-10-04 19:06:30 -0700101inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
Jeff Hao39b6c242015-05-19 20:30:23 -0700102 if (!GetChecked(iref)) {
103 LOG(WARNING) << "IndirectReferenceTable Update failed to find reference " << iref;
104 return;
105 }
106 uint32_t idx = ExtractIndex(iref);
107 table_[idx].SetReference(obj);
108}
109
Mathieu Chartier8778c522016-10-04 19:06:30 -0700110inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
111 ++serial_;
112 if (serial_ == kIRTPrevCount) {
113 serial_ = 0;
114 }
115 references_[serial_] = GcRoot<mirror::Object>(obj);
116}
117
118inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
119 DCHECK_LT(serial_, kIRTPrevCount);
120 references_[serial_] = GcRoot<mirror::Object>(obj);
121}
122
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700123} // namespace art
124
125#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_