• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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     // Must take context of the class which is being loaded
33     auto *ctx = klass->GetLoadContext();
34     for (size_t i = vtable.size(); i != 0;) {
35         i--;
36         auto kmethod = vtable[i];
37         if (kmethod->GetName() != imethod->GetName()) {
38             continue;
39         }
40         if (!ETSProtoIsOverriddenBy(ctx, imethod->GetProtoId(), kmethod->GetProtoId())) {
41             continue;
42         }
43         if (candidate != nullptr) {
44             OnVTableConflict(errHandler, ClassLinker::Error::MULTIPLE_IMPLEMENT, "Multiple implementations", kmethod,
45                              candidate);
46             return std::nullopt;
47         }
48         candidate = kmethod;
49     }
50     return candidate;
51 }
52 
LinearizeITable(ClassLinker * classLinker,Class * base,Span<Class * > classInterfaces,PandaUnorderedSet<Class * > && interfaces)53 static Span<ITable::Entry> LinearizeITable(ClassLinker *classLinker, Class *base, Span<Class *> classInterfaces,
54                                            PandaUnorderedSet<Class *> &&interfaces)
55 {
56     auto allocator = classLinker->GetAllocator();
57     Span<ITable::Entry> itable {interfaces.empty() ? nullptr : allocator->AllocArray<ITable::Entry>(interfaces.size()),
58                                 interfaces.size()};
59 
60     for (auto &entry : itable) {
61         entry.SetMethods({nullptr, nullptr});
62     }
63 
64     if (base != nullptr) {
65         auto superItable = base->GetITable().Get();
66         for (size_t i = 0; i < superItable.size(); i++) {
67             itable[i] = superItable[i].Copy(allocator);
68             interfaces.erase(superItable[i].GetInterface());
69         }
70     }
71 
72     size_t const superItableSize = base != nullptr ? base->GetITable().Size() : 0;
73 
74     size_t shift = superItableSize;
75 
76     for (auto interface : classInterfaces) {
77         auto table = interface->GetITable().Get();
78         for (auto &item : table) {
79             auto iterator = interfaces.find(item.GetInterface());
80             if (iterator != interfaces.end()) {
81                 itable[shift++] = item.Copy(allocator);
82                 interfaces.erase(item.GetInterface());
83             }
84         }
85         auto iterator = interfaces.find(interface);
86         if (iterator != interfaces.end()) {
87             itable[shift++].SetInterface(interface);
88             interfaces.erase(interface);
89         }
90     }
91 
92     ASSERT(interfaces.empty());
93     return itable;
94 }
95 
96 // add interfaces
97 // interfaces of a superclass (they are located before others)
98 // self interfaces and interfaces of self interfaces
99 // add methods if it's not an interface (methods are located only for classes)
Build(ClassLinker * classLinker,Class * base,Span<Class * > classInterfaces,bool isInterface)100 bool EtsITableBuilder::Build(ClassLinker *classLinker, Class *base, Span<Class *> classInterfaces, bool isInterface)
101 {
102     PandaUnorderedSet<Class *> interfaces;
103 
104     if (base != nullptr) {
105         auto superItable = base->GetITable().Get();
106         for (auto item : superItable) {
107             interfaces.insert(item.GetInterface());
108         }
109     }
110 
111     for (auto interface : classInterfaces) {
112         auto table = interface->GetITable().Get();
113         for (auto item : table) {
114             interfaces.insert(item.GetInterface());
115         }
116         interfaces.insert(interface);
117     }
118 
119     Span<ITable::Entry> itable = LinearizeITable(classLinker, base, classInterfaces, std::move(interfaces));
120 
121     if (!isInterface) {
122         size_t const superItableSize = base != nullptr ? base->GetITable().Size() : 0;
123         for (size_t i = superItableSize; i < itable.Size(); i++) {
124             auto &entry = itable[i];
125             auto methods = entry.GetInterface()->GetVirtualMethods();
126             Method **methodsAlloc = nullptr;
127             if (!methods.Empty()) {
128                 methodsAlloc = classLinker->GetAllocator()->AllocArray<Method *>(methods.size());
129             }
130             Span<Method *> methodsArray = {methodsAlloc, methods.size()};
131             entry.SetMethods(methodsArray);
132         }
133     }
134 
135     itable_ = ITable(itable);
136     return true;
137 }
138 
Resolve(Class * klass)139 bool EtsITableBuilder::Resolve(Class *klass)
140 {
141     if (klass->IsInterface()) {
142         return true;
143     }
144 
145     UpdateClass(klass);
146     for (size_t i = itable_.Size(); i > 0; i--) {
147         auto entry = itable_[i - 1];
148         auto methods = entry.GetInterface()->GetVirtualMethods();
149         for (size_t j = 0; j < methods.size(); j++) {
150             auto res = FindMethodInVTable(klass, &methods[j], errorHandler_);
151             if (!res.has_value()) {
152                 return false;
153             }
154             if (res.value() == nullptr) {
155                 res = &methods[j];
156             }
157             entry.GetMethods()[j] = res.value();
158         }
159     }
160     return true;
161 }
162 
UpdateClass(Class * klass)163 void EtsITableBuilder::UpdateClass(Class *klass)
164 {
165     klass->SetITable(itable_);
166     DumpITable(klass);
167 }
168 
DumpITable(Class * klass)169 void EtsITableBuilder::DumpITable([[maybe_unused]] Class *klass)
170 {
171 #ifndef NDEBUG
172     LOG(DEBUG, CLASS_LINKER) << "itable of class " << klass->GetName() << ":";
173     auto itable = klass->GetITable();
174     size_t idxI = 0;
175     for (size_t i = 0; i < itable.Size(); i++) {
176         auto entry = itable[i];
177         auto interface = entry.GetInterface();
178         LOG(DEBUG, CLASS_LINKER) << "[ interface - " << idxI++ << " ] " << interface->GetName() << ":";
179         // #22950 itable has nullptr entries
180     }
181 #endif  // NDEBUG
182 }
183 
184 }  // namespace ark::ets
185