blob: fb2dc1f8799b9b0205107e25c006b39c84658c83 [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.
Vladimir Marko17491ac2020-12-01 12:02:29 +000036inline bool IndirectReferenceTable::IsValidReference(IndirectRef iref,
37 /*out*/std::string* error_msg) const {
38 DCHECK(iref != nullptr);
39 DCHECK_EQ(GetIndirectRefKind(iref), kind_);
Andreas Gampee03662b2016-10-13 17:12:56 -070040 const uint32_t top_index = segment_state_.top_index;
41 uint32_t idx = ExtractIndex(iref);
42 if (UNLIKELY(idx >= top_index)) {
Vladimir Marko17491ac2020-12-01 12:02:29 +000043 *error_msg = android::base::StringPrintf("deleted reference at index %u in a table of size %u",
44 idx,
45 top_index);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070046 return false;
47 }
Mathieu Chartier4838d662014-09-25 15:27:43 -070048 if (UNLIKELY(table_[idx].GetReference()->IsNull())) {
Vladimir Marko17491ac2020-12-01 12:02:29 +000049 *error_msg = android::base::StringPrintf("deleted reference at index %u", idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070050 return false;
51 }
Vladimir Marko17491ac2020-12-01 12:02:29 +000052 uint32_t iref_serial = DecodeSerial(reinterpret_cast<uintptr_t>(iref));
53 uint32_t entry_serial = table_[idx].GetSerial();
54 if (UNLIKELY(iref_serial != entry_serial)) {
55 *error_msg = android::base::StringPrintf("stale reference with serial number %u v. current %u",
56 iref_serial,
57 entry_serial);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070058 return false;
59 }
60 return true;
61}
62
63// Make sure that the entry at "idx" is correctly paired with "iref".
Andreas Gampee03662b2016-10-13 17:12:56 -070064inline bool IndirectReferenceTable::CheckEntry(const char* what,
65 IndirectRef iref,
66 uint32_t idx) const {
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070067 IndirectRef checkRef = ToIndirectRef(idx);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070068 if (UNLIKELY(checkRef != iref)) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069 std::string msg = android::base::StringPrintf(
Andreas Gampef1e86302016-10-03 11:42:31 -070070 "JNI ERROR (app bug): attempt to %s stale %s %p (should be %p)",
71 what,
72 GetIndirectRefKindString(kind_),
73 iref,
74 checkRef);
75 AbortIfNoCheckJNI(msg);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070076 return false;
77 }
78 return true;
79}
80
Hiroshi Yamauchi196851b2014-05-29 12:16:04 -070081template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier8778c522016-10-04 19:06:30 -070082inline ObjPtr<mirror::Object> IndirectReferenceTable::Get(IndirectRef iref) const {
Vladimir Marko17491ac2020-12-01 12:02:29 +000083 DCHECK_EQ(GetIndirectRefKind(iref), kind_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070084 uint32_t idx = ExtractIndex(iref);
Vladimir Marko17491ac2020-12-01 12:02:29 +000085 DCHECK_LT(idx, segment_state_.top_index);
86 DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
87 DCHECK(!table_[idx].GetReference()->IsNull());
Mathieu Chartier8778c522016-10-04 19:06:30 -070088 ObjPtr<mirror::Object> obj = table_[idx].GetReference()->Read<kReadBarrierOption>();
Mathieu Chartier9d156d52016-10-06 17:44:26 -070089 VerifyObject(obj);
Mathieu Chartierc56057e2014-05-04 13:18:58 -070090 return obj;
91}
92
Mathieu Chartier8778c522016-10-04 19:06:30 -070093inline void IndirectReferenceTable::Update(IndirectRef iref, ObjPtr<mirror::Object> obj) {
Vladimir Marko17491ac2020-12-01 12:02:29 +000094 DCHECK_EQ(GetIndirectRefKind(iref), kind_);
Jeff Hao39b6c242015-05-19 20:30:23 -070095 uint32_t idx = ExtractIndex(iref);
Vladimir Marko17491ac2020-12-01 12:02:29 +000096 DCHECK_LT(idx, segment_state_.top_index);
97 DCHECK_EQ(DecodeSerial(reinterpret_cast<uintptr_t>(iref)), table_[idx].GetSerial());
98 DCHECK(!table_[idx].GetReference()->IsNull());
Jeff Hao39b6c242015-05-19 20:30:23 -070099 table_[idx].SetReference(obj);
100}
101
Mathieu Chartier8778c522016-10-04 19:06:30 -0700102inline void IrtEntry::Add(ObjPtr<mirror::Object> obj) {
103 ++serial_;
104 if (serial_ == kIRTPrevCount) {
105 serial_ = 0;
106 }
Vladimir Markobcf17522018-06-01 13:14:32 +0100107 references_[serial_] = GcRoot<mirror::Object>(obj);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700108}
109
110inline void IrtEntry::SetReference(ObjPtr<mirror::Object> obj) {
111 DCHECK_LT(serial_, kIRTPrevCount);
Vladimir Markobcf17522018-06-01 13:14:32 +0100112 references_[serial_] = GcRoot<mirror::Object>(obj);
Mathieu Chartier8778c522016-10-04 19:06:30 -0700113}
114
Mathieu Chartierc56057e2014-05-04 13:18:58 -0700115} // namespace art
116
117#endif // ART_RUNTIME_INDIRECT_REFERENCE_TABLE_INL_H_