blob: fdd10fefc473575c6ab96385180f8a4da28d81c8 [file] [log] [blame]
Andreas Gampe75a7db62016-09-26 12:04:26 -07001/*
2 * Copyright (C) 2016 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_IMT_CONFLICT_TABLE_H_
18#define ART_RUNTIME_IMT_CONFLICT_TABLE_H_
19
20#include <cstddef>
21
22#include "base/casts.h"
23#include "base/enums.h"
24#include "base/macros.h"
25
26namespace art {
27
28class ArtMethod;
29
30// Table to resolve IMT conflicts at runtime. The table is attached to
31// the jni entrypoint of IMT conflict ArtMethods.
32// The table contains a list of pairs of { interface_method, implementation_method }
33// with the last entry being null to make an assembly implementation of a lookup
34// faster.
35class ImtConflictTable {
36 enum MethodIndex {
37 kMethodInterface,
38 kMethodImplementation,
39 kMethodCount, // Number of elements in enum.
40 };
41
42 public:
43 // Build a new table copying `other` and adding the new entry formed of
44 // the pair { `interface_method`, `implementation_method` }
45 ImtConflictTable(ImtConflictTable* other,
46 ArtMethod* interface_method,
47 ArtMethod* implementation_method,
48 PointerSize pointer_size) {
49 const size_t count = other->NumEntries(pointer_size);
50 for (size_t i = 0; i < count; ++i) {
51 SetInterfaceMethod(i, pointer_size, other->GetInterfaceMethod(i, pointer_size));
52 SetImplementationMethod(i, pointer_size, other->GetImplementationMethod(i, pointer_size));
53 }
54 SetInterfaceMethod(count, pointer_size, interface_method);
55 SetImplementationMethod(count, pointer_size, implementation_method);
56 // Add the null marker.
57 SetInterfaceMethod(count + 1, pointer_size, nullptr);
58 SetImplementationMethod(count + 1, pointer_size, nullptr);
59 }
60
61 // num_entries excludes the header.
62 ImtConflictTable(size_t num_entries, PointerSize pointer_size) {
63 SetInterfaceMethod(num_entries, pointer_size, nullptr);
64 SetImplementationMethod(num_entries, pointer_size, nullptr);
65 }
66
67 // Set an entry at an index.
68 void SetInterfaceMethod(size_t index, PointerSize pointer_size, ArtMethod* method) {
69 SetMethod(index * kMethodCount + kMethodInterface, pointer_size, method);
70 }
71
72 void SetImplementationMethod(size_t index, PointerSize pointer_size, ArtMethod* method) {
73 SetMethod(index * kMethodCount + kMethodImplementation, pointer_size, method);
74 }
75
76 ArtMethod* GetInterfaceMethod(size_t index, PointerSize pointer_size) const {
77 return GetMethod(index * kMethodCount + kMethodInterface, pointer_size);
78 }
79
80 ArtMethod* GetImplementationMethod(size_t index, PointerSize pointer_size) const {
81 return GetMethod(index * kMethodCount + kMethodImplementation, pointer_size);
82 }
83
84 // Return true if two conflict tables are the same.
85 bool Equals(ImtConflictTable* other, PointerSize pointer_size) const {
86 size_t num = NumEntries(pointer_size);
87 if (num != other->NumEntries(pointer_size)) {
88 return false;
89 }
90 for (size_t i = 0; i < num; ++i) {
91 if (GetInterfaceMethod(i, pointer_size) != other->GetInterfaceMethod(i, pointer_size) ||
92 GetImplementationMethod(i, pointer_size) !=
93 other->GetImplementationMethod(i, pointer_size)) {
94 return false;
95 }
96 }
97 return true;
98 }
99
100 // Visit all of the entries.
101 // NO_THREAD_SAFETY_ANALYSIS for calling with held locks. Visitor is passed a pair of ArtMethod*
102 // and also returns one. The order is <interface, implementation>.
103 template<typename Visitor>
104 void Visit(const Visitor& visitor, PointerSize pointer_size) NO_THREAD_SAFETY_ANALYSIS {
105 uint32_t table_index = 0;
106 for (;;) {
107 ArtMethod* interface_method = GetInterfaceMethod(table_index, pointer_size);
108 if (interface_method == nullptr) {
109 break;
110 }
111 ArtMethod* implementation_method = GetImplementationMethod(table_index, pointer_size);
112 auto input = std::make_pair(interface_method, implementation_method);
113 std::pair<ArtMethod*, ArtMethod*> updated = visitor(input);
114 if (input.first != updated.first) {
115 SetInterfaceMethod(table_index, pointer_size, updated.first);
116 }
117 if (input.second != updated.second) {
118 SetImplementationMethod(table_index, pointer_size, updated.second);
119 }
120 ++table_index;
121 }
122 }
123
124 // Lookup the implementation ArtMethod associated to `interface_method`. Return null
125 // if not found.
126 ArtMethod* Lookup(ArtMethod* interface_method, PointerSize pointer_size) const {
127 uint32_t table_index = 0;
128 for (;;) {
129 ArtMethod* current_interface_method = GetInterfaceMethod(table_index, pointer_size);
130 if (current_interface_method == nullptr) {
131 break;
132 }
133 if (current_interface_method == interface_method) {
134 return GetImplementationMethod(table_index, pointer_size);
135 }
136 ++table_index;
137 }
138 return nullptr;
139 }
140
141 // Compute the number of entries in this table.
142 size_t NumEntries(PointerSize pointer_size) const {
143 uint32_t table_index = 0;
144 while (GetInterfaceMethod(table_index, pointer_size) != nullptr) {
145 ++table_index;
146 }
147 return table_index;
148 }
149
150 // Compute the size in bytes taken by this table.
151 size_t ComputeSize(PointerSize pointer_size) const {
152 // Add the end marker.
153 return ComputeSize(NumEntries(pointer_size), pointer_size);
154 }
155
156 // Compute the size in bytes needed for copying the given `table` and add
157 // one more entry.
158 static size_t ComputeSizeWithOneMoreEntry(ImtConflictTable* table, PointerSize pointer_size) {
159 return table->ComputeSize(pointer_size) + EntrySize(pointer_size);
160 }
161
162 // Compute size with a fixed number of entries.
163 static size_t ComputeSize(size_t num_entries, PointerSize pointer_size) {
164 return (num_entries + 1) * EntrySize(pointer_size); // Add one for null terminator.
165 }
166
167 static size_t EntrySize(PointerSize pointer_size) {
168 return static_cast<size_t>(pointer_size) * static_cast<size_t>(kMethodCount);
169 }
170
171 private:
172 ArtMethod* GetMethod(size_t index, PointerSize pointer_size) const {
173 if (pointer_size == PointerSize::k64) {
174 return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(data64_[index]));
175 } else {
176 return reinterpret_cast<ArtMethod*>(static_cast<uintptr_t>(data32_[index]));
177 }
178 }
179
180 void SetMethod(size_t index, PointerSize pointer_size, ArtMethod* method) {
181 if (pointer_size == PointerSize::k64) {
182 data64_[index] = dchecked_integral_cast<uint64_t>(reinterpret_cast<uintptr_t>(method));
183 } else {
184 data32_[index] = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(method));
185 }
186 }
187
188 // Array of entries that the assembly stubs will iterate over. Note that this is
189 // not fixed size, and we allocate data prior to calling the constructor
190 // of ImtConflictTable.
191 union {
192 uint32_t data32_[0];
193 uint64_t data64_[0];
194 };
195
196 DISALLOW_COPY_AND_ASSIGN(ImtConflictTable);
197};
198
199} // namespace art
200
201#endif // ART_RUNTIME_IMT_CONFLICT_TABLE_H_