blob: 5d8b61df7c6d80657ad1466c5926eac6d28cbd8f [file] [log] [blame]
Chang Xing605fe242017-07-20 15:57:21 -07001/*
2 * Copyright (C) 2017 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#include "aot_class_linker.h"
18
Andreas Gampe5d3b0022017-08-31 10:36:31 -070019#include "class_status.h"
Mathieu Chartier9e050df2017-08-09 10:05:47 -070020#include "compiler_callbacks.h"
David Sehr312f3b22018-03-19 08:39:26 -070021#include "dex/class_reference.h"
Vladimir Marko7ed2d382019-11-25 10:41:53 +000022#include "gc/heap.h"
Chang Xing605fe242017-07-20 15:57:21 -070023#include "handle_scope-inl.h"
Mathieu Chartier9e050df2017-08-09 10:05:47 -070024#include "mirror/class-inl.h"
Chang Xing605fe242017-07-20 15:57:21 -070025#include "runtime.h"
Mathieu Chartier9e050df2017-08-09 10:05:47 -070026#include "verifier/verifier_enums.h"
Chang Xing605fe242017-07-20 15:57:21 -070027
28namespace art {
29
Andreas Gampe87658f32019-04-18 18:39:02 +000030AotClassLinker::AotClassLinker(InternTable* intern_table)
31 : ClassLinker(intern_table, /*fast_class_not_found_exceptions=*/ false) {}
Chang Xing605fe242017-07-20 15:57:21 -070032
33AotClassLinker::~AotClassLinker() {}
34
Chang Xing0c2c2222017-08-04 14:36:17 -070035bool AotClassLinker::CanAllocClass() {
36 // AllocClass doesn't work under transaction, so we abort.
37 if (Runtime::Current()->IsActiveTransaction()) {
38 Runtime::Current()->AbortTransactionAndThrowAbortError(
39 Thread::Current(), "Can't resolve type within transaction.");
40 return false;
41 }
42 return ClassLinker::CanAllocClass();
43}
44
Chang Xing605fe242017-07-20 15:57:21 -070045// Wrap the original InitializeClass with creation of transaction when in strict mode.
Vladimir Marko4617d582019-03-28 13:48:31 +000046bool AotClassLinker::InitializeClass(Thread* self,
47 Handle<mirror::Class> klass,
48 bool can_init_statics,
49 bool can_init_parents) {
Chang Xing605fe242017-07-20 15:57:21 -070050 Runtime* const runtime = Runtime::Current();
Vladimir Marko889b72d2019-11-12 11:01:13 +000051 bool strict_mode = runtime->IsActiveStrictTransactionMode();
Chang Xing605fe242017-07-20 15:57:21 -070052
53 DCHECK(klass != nullptr);
54 if (klass->IsInitialized() || klass->IsInitializing()) {
55 return ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
56 }
57
Vladimir Marko889b72d2019-11-12 11:01:13 +000058 // When compiling a boot image extension, do not initialize a class defined
59 // in a dex file belonging to the boot image we're compiling against.
60 // However, we must allow the initialization of TransactionAbortError,
61 // VerifyError, etc. outside of a transaction.
62 if (!strict_mode && runtime->GetHeap()->ObjectIsInBootImageSpace(klass->GetDexCache())) {
63 if (runtime->IsActiveTransaction()) {
64 runtime->AbortTransactionAndThrowAbortError(self, "Can't initialize " + klass->PrettyTypeOf()
65 + " because it is defined in a boot image dex file.");
66 return false;
67 }
68 CHECK(klass->IsThrowableClass()) << klass->PrettyDescriptor();
69 }
70
Chang Xing0c2c2222017-08-04 14:36:17 -070071 // When in strict_mode, don't initialize a class if it belongs to boot but not initialized.
Vladimir Marko889b72d2019-11-12 11:01:13 +000072 if (strict_mode && klass->IsBootStrapClassLoaded()) {
Chang Xing0c2c2222017-08-04 14:36:17 -070073 runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve "
74 + klass->PrettyTypeOf() + " because it is an uninitialized boot class.");
75 return false;
76 }
77
Chang Xingadbb91c2017-07-17 11:23:55 -070078 // Don't initialize klass if it's superclass is not initialized, because superclass might abort
79 // the transaction and rolled back after klass's change is commited.
Vladimir Marko889b72d2019-11-12 11:01:13 +000080 if (strict_mode && !klass->IsInterface() && klass->HasSuperClass()) {
Vladimir Marko2c64a832018-01-04 11:31:56 +000081 if (klass->GetSuperClass()->GetStatus() == ClassStatus::kInitializing) {
Chang Xingadbb91c2017-07-17 11:23:55 -070082 runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve "
83 + klass->PrettyTypeOf() + " because it's superclass is not initialized.");
84 return false;
85 }
86 }
87
Vladimir Marko889b72d2019-11-12 11:01:13 +000088 if (strict_mode) {
Vladimir Marko672c0802019-07-26 13:03:13 +010089 runtime->EnterTransactionMode(/*strict=*/ true, klass.Get());
Chang Xing605fe242017-07-20 15:57:21 -070090 }
91 bool success = ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
92
Vladimir Marko889b72d2019-11-12 11:01:13 +000093 if (strict_mode) {
Chang Xing605fe242017-07-20 15:57:21 -070094 if (success) {
95 // Exit Transaction if success.
96 runtime->ExitTransactionMode();
97 } else {
Chang Xing0c2c2222017-08-04 14:36:17 -070098 // If not successfully initialized, don't rollback immediately, leave the cleanup to compiler
99 // driver which needs abort message and exception.
Chang Xing605fe242017-07-20 15:57:21 -0700100 DCHECK(self->IsExceptionPending());
101 }
102 }
103 return success;
104}
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700105
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000106verifier::FailureKind AotClassLinker::PerformClassVerification(
107 Thread* self,
108 verifier::VerifierDeps* verifier_deps,
109 Handle<mirror::Class> klass,
110 verifier::HardFailLogMode log_level,
111 std::string* error_msg) {
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700112 Runtime* const runtime = Runtime::Current();
113 CompilerCallbacks* callbacks = runtime->GetCompilerCallbacks();
Andreas Gampe5d3b0022017-08-31 10:36:31 -0700114 ClassStatus old_status = callbacks->GetPreviousClassState(
115 ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()));
Andreas Gamped3ea5b32017-09-04 14:55:34 -0700116 // Was it verified? Report no failure.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000117 if (old_status >= ClassStatus::kVerified) {
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700118 return verifier::FailureKind::kNoFailure;
119 }
Nicolas Geoffray1715efa2020-06-05 18:34:49 +0100120 if (old_status >= ClassStatus::kVerifiedNeedsAccessChecks) {
121 return verifier::FailureKind::kAccessChecksFailure;
122 }
Andreas Gamped3ea5b32017-09-04 14:55:34 -0700123 // Does it need to be verified at runtime? Report soft failure.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000124 if (old_status >= ClassStatus::kRetryVerificationAtRuntime) {
Andreas Gamped3ea5b32017-09-04 14:55:34 -0700125 // Error messages from here are only reported through -verbose:class. It is not worth it to
126 // create a message.
127 return verifier::FailureKind::kSoftFailure;
128 }
129 // Do the actual work.
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000130 return ClassLinker::PerformClassVerification(self, verifier_deps, klass, log_level, error_msg);
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700131}
132
Vladimir Marko7ed2d382019-11-25 10:41:53 +0000133bool AotClassLinker::CanReferenceInBootImageExtension(ObjPtr<mirror::Class> klass, gc::Heap* heap) {
134 // Do not allow referencing a class or instance of a class defined in a dex file
135 // belonging to the boot image we're compiling against but not itself in the boot image;
136 // or a class referencing such classes as component type, superclass or interface.
137 // Allowing this could yield duplicate class objects from multiple extensions.
138
139 if (heap->ObjectIsInBootImageSpace(klass)) {
140 return true; // Already included in the boot image we're compiling against.
141 }
142
143 // Treat arrays and primitive types specially because they do not have a DexCache that we
144 // can use to check whether the dex file belongs to the boot image we're compiling against.
145 DCHECK(!klass->IsPrimitive()); // Primitive classes must be in the primary boot image.
146 if (klass->IsArrayClass()) {
147 DCHECK(heap->ObjectIsInBootImageSpace(klass->GetIfTable())); // IfTable is OK.
148 // Arrays of all dimensions are tied to the dex file of the non-array component type.
149 do {
150 klass = klass->GetComponentType();
151 } while (klass->IsArrayClass());
152 if (klass->IsPrimitive()) {
153 return false;
154 }
155 // Do not allow arrays of erroneous classes (the array class is not itself erroneous).
156 if (klass->IsErroneous()) {
157 return false;
158 }
159 }
160
161 // Check the class itself.
162 if (heap->ObjectIsInBootImageSpace(klass->GetDexCache())) {
163 return false;
164 }
165
166 // Check superclasses.
167 ObjPtr<mirror::Class> superclass = klass->GetSuperClass();
168 while (!heap->ObjectIsInBootImageSpace(superclass)) {
169 DCHECK(superclass != nullptr); // Cannot skip Object which is in the primary boot image.
170 if (heap->ObjectIsInBootImageSpace(superclass->GetDexCache())) {
171 return false;
172 }
173 superclass = superclass->GetSuperClass();
174 }
175
176 // Check IfTable. This includes direct and indirect interfaces.
177 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
178 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
179 ObjPtr<mirror::Class> interface = if_table->GetInterface(i);
180 DCHECK(interface != nullptr);
181 if (!heap->ObjectIsInBootImageSpace(interface) &&
182 heap->ObjectIsInBootImageSpace(interface->GetDexCache())) {
183 return false;
184 }
185 }
186
187 if (kIsDebugBuild) {
188 // All virtual methods must come from classes we have already checked above.
189 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
190 ObjPtr<mirror::Class> k = klass;
191 while (!heap->ObjectIsInBootImageSpace(k)) {
192 for (auto& m : k->GetVirtualMethods(pointer_size)) {
193 ObjPtr<mirror::Class> declaring_class = m.GetDeclaringClass();
194 CHECK(heap->ObjectIsInBootImageSpace(declaring_class) ||
195 !heap->ObjectIsInBootImageSpace(declaring_class->GetDexCache()));
196 }
197 k = k->GetSuperClass();
198 }
199 }
200
201 return true;
202}
203
Vladimir Markod1f73512020-04-02 10:50:35 +0100204bool AotClassLinker::SetUpdatableBootClassPackages(const std::vector<std::string>& packages) {
205 DCHECK(updatable_boot_class_path_descriptor_prefixes_.empty());
206 // Transform package names to descriptor prefixes.
207 std::vector<std::string> prefixes;
208 prefixes.reserve(packages.size());
209 for (const std::string& package : packages) {
210 if (package.empty() || package.find('/') != std::string::npos) {
211 LOG(ERROR) << "Invalid package name: " << package;
212 return false;
213 }
214 std::string prefix = 'L' + package + '/';
215 std::replace(prefix.begin(), prefix.end(), '.', '/');
216 prefixes.push_back(std::move(prefix));
217 }
218 // Sort and remove unnecessary prefixes.
219 std::sort(prefixes.begin(), prefixes.end());
220 std::string last_prefix;
221 auto end_it = std::remove_if(
222 prefixes.begin(),
223 prefixes.end(),
224 [&last_prefix](const std::string& s) {
225 if (!last_prefix.empty() && StartsWith(s, last_prefix)) {
226 return true;
227 } else {
228 last_prefix = s;
229 return false;
230 }
231 });
232 prefixes.resize(std::distance(prefixes.begin(), end_it));
233 prefixes.shrink_to_fit();
234 updatable_boot_class_path_descriptor_prefixes_.swap(prefixes);
235 return true;
236}
237
238bool AotClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor) {
239 std::string_view descriptor_sv(descriptor);
240 for (const std::string& prefix : updatable_boot_class_path_descriptor_prefixes_) {
241 if (StartsWith(descriptor_sv, prefix)) {
242 return true;
243 }
244 }
245 return false;
246}
Calin Juravle33787682019-07-26 14:27:18 -0700247void AotClassLinker::SetSdkChecker(std::unique_ptr<SdkChecker>&& sdk_checker) {
248 sdk_checker_ = std::move(sdk_checker);
249}
250
251const SdkChecker* AotClassLinker::GetSdkChecker() const {
252 return sdk_checker_.get();
253}
254
255bool AotClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method) const
256 REQUIRES_SHARED(Locks::mutator_lock_) {
257 return sdk_checker_ != nullptr && sdk_checker_->ShouldDenyAccess(art_method);
258}
259bool AotClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field) const
260 REQUIRES_SHARED(Locks::mutator_lock_) {
261 return sdk_checker_ != nullptr && sdk_checker_->ShouldDenyAccess(art_field);
262}
263bool AotClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor) const {
264 return sdk_checker_ != nullptr && sdk_checker_->ShouldDenyAccess(type_descriptor);
265}
Vladimir Markod1f73512020-04-02 10:50:35 +0100266
Calin Juravle2c2724c2021-01-14 19:54:23 -0800267void AotClassLinker::SetEnablePublicSdkChecks(bool enabled) {
268 if (sdk_checker_ != nullptr) {
269 sdk_checker_->SetEnabled(enabled);
270 }
271}
272
Chang Xing605fe242017-07-20 15:57:21 -0700273} // namespace art