• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ets_itable_builder.h"
17 
18 #include "plugins/ets/runtime/ets_class_linker_extension.h"
19 #include "plugins/ets/runtime/ets_vm.h"
20 #include "plugins/ets/runtime/ets_vtable_builder.h"
21 #include "runtime/include/class_linker.h"
22 #include "runtime/include/exceptions.h"
23 #include "runtime/include/mem/panda_containers.h"
24 
25 namespace ark::ets {
26 
FindMethodInVTable(Class * klass,Method * imethod,ClassLinkerErrorHandler * errHandler)27 static std::optional<Method *> FindMethodInVTable(Class *klass, Method *imethod, ClassLinkerErrorHandler *errHandler)
28 {
29     auto vtable = klass->GetVTable();
30     Method *candidate = nullptr;
31 
32     for (size_t i = vtable.size(); i != 0;) {
33         i--;
34         auto kmethod = vtable[i];
35         if (kmethod->GetName() != imethod->GetName()) {
36             continue;
37         }
38         if (!ETSProtoIsOverriddenBy(imethod->GetProtoId(), kmethod->GetProtoId())) {
39             continue;
40         }
41         if (candidate != nullptr) {
42             OnVTableConflict(errHandler, ClassLinker::Error::MULTIPLE_IMPLEMENT, "Multiple implementations", kmethod,
43                              candidate);
44             return std::nullopt;
45         }
46         candidate = kmethod;
47     }
48     return candidate;
49 }
50 
LinearizeITable(ClassLinker * classLinker,Class * base,Span<Class * > classInterfaces,PandaUnorderedSet<Class * > && interfaces)51 static Span<ITable::Entry> LinearizeITable(ClassLinker *classLinker, Class *base, Span<Class *> classInterfaces,
52                                            PandaUnorderedSet<Class *> &&interfaces)
53 {
54     auto allocator = classLinker->GetAllocator();
55     Span<ITable::Entry> itable {interfaces.empty() ? nullptr : allocator->AllocArray<ITable::Entry>(interfaces.size()),
56                                 interfaces.size()};
57 
58     for (auto &entry : itable) {
59         entry.SetMethods({nullptr, nullptr});
60     }
61 
62     if (base != nullptr) {
63         auto superItable = base->GetITable().Get();
64         for (size_t i = 0; i < superItable.size(); i++) {
65             itable[i] = superItable[i].Copy(allocator);
66             interfaces.erase(superItable[i].GetInterface());
67         }
68     }
69 
70     size_t const superItableSize = base != nullptr ? base->GetITable().Size() : 0;
71 
72     size_t shift = superItableSize;
73 
74     for (auto interface : classInterfaces) {
75         auto table = interface->GetITable().Get();
76         for (auto &item : table) {
77             auto iterator = interfaces.find(item.GetInterface());
78             if (iterator != interfaces.end()) {
79                 itable[shift++] = item.Copy(allocator);
80                 interfaces.erase(item.GetInterface());
81             }
82         }
83         auto iterator = interfaces.find(interface);
84         if (iterator != interfaces.end()) {
85             itable[shift++].SetInterface(interface);
86             interfaces.erase(interface);
87         }
88     }
89 
90     ASSERT(interfaces.empty());
91     return itable;
92 }
93 
94 // add interfaces
95 // interfaces of a superclass (they are located before others)
96 // self interfaces and interfaces of self interfaces
97 // add methods if it's not an interface (methods are located only for classes)
Build(ClassLinker * classLinker,Class * base,Span<Class * > classInterfaces,bool isInterface)98 bool EtsITableBuilder::Build(ClassLinker *classLinker, Class *base, Span<Class *> classInterfaces, bool isInterface)
99 {
100     PandaUnorderedSet<Class *> interfaces;
101 
102     if (base != nullptr) {
103         auto superItable = base->GetITable().Get();
104         for (auto item : superItable) {
105             interfaces.insert(item.GetInterface());
106         }
107     }
108 
109     for (auto interface : classInterfaces) {
110         auto table = interface->GetITable().Get();
111         for (auto item : table) {
112             interfaces.insert(item.GetInterface());
113         }
114         interfaces.insert(interface);
115     }
116 
117     Span<ITable::Entry> itable = LinearizeITable(classLinker, base, classInterfaces, std::move(interfaces));
118 
119     if (!isInterface) {
120         size_t const superItableSize = base != nullptr ? base->GetITable().Size() : 0;
121         for (size_t i = superItableSize; i < itable.Size(); i++) {
122             auto &entry = itable[i];
123             auto methods = entry.GetInterface()->GetVirtualMethods();
124             Method **methodsAlloc = nullptr;
125             if (!methods.Empty()) {
126                 methodsAlloc = classLinker->GetAllocator()->AllocArray<Method *>(methods.size());
127             }
128             Span<Method *> methodsArray = {methodsAlloc, methods.size()};
129             entry.SetMethods(methodsArray);
130         }
131     }
132 
133     itable_ = ITable(itable);
134     return true;
135 }
136 
Resolve(Class * klass)137 bool EtsITableBuilder::Resolve(Class *klass)
138 {
139     if (klass->IsInterface()) {
140         return true;
141     }
142 
143     UpdateClass(klass);
144     for (size_t i = itable_.Size(); i > 0; i--) {
145         auto entry = itable_[i - 1];
146         auto methods = entry.GetInterface()->GetVirtualMethods();
147         for (size_t j = 0; j < methods.size(); j++) {
148             auto res = FindMethodInVTable(klass, &methods[j], errorHandler_);
149             if (!res.has_value()) {
150                 return false;
151             }
152             if (res.value() == nullptr) {
153                 res = &methods[j];
154             }
155             entry.GetMethods()[j] = res.value();
156         }
157     }
158     return true;
159 }
160 
UpdateClass(Class * klass)161 void EtsITableBuilder::UpdateClass(Class *klass)
162 {
163     klass->SetITable(itable_);
164     DumpITable(klass);
165 }
166 
DumpITable(Class * klass)167 void EtsITableBuilder::DumpITable([[maybe_unused]] Class *klass)
168 {
169 #ifndef NDEBUG
170     LOG(DEBUG, CLASS_LINKER) << "itable of class " << klass->GetName() << ":";
171     auto itable = klass->GetITable();
172     size_t idxI = 0;
173     size_t idxM = 0;
174     for (size_t i = 0; i < itable.Size(); i++) {
175         auto entry = itable[i];
176         auto interface = entry.GetInterface();
177         LOG(DEBUG, CLASS_LINKER) << "[ interface - " << idxI++ << " ] " << interface->GetName() << ":";
178         auto methods = entry.GetMethods();
179         for (auto *method : methods) {
180             LOG(DEBUG, CLASS_LINKER) << "[ method - " << idxM++ << " ] " << method->GetFullName() << " - "
181                                      << method->GetFileId().GetOffset();
182         }
183     }
184 #endif  // NDEBUG
185 }
186 
187 }  // namespace ark::ets
188