blob: c9ca4c9be33a31d7ae653b96a1f75ca46e3d0355 [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"
Chang Xing605fe242017-07-20 15:57:21 -070022#include "handle_scope-inl.h"
Mathieu Chartier9e050df2017-08-09 10:05:47 -070023#include "mirror/class-inl.h"
Chang Xing605fe242017-07-20 15:57:21 -070024#include "runtime.h"
Mathieu Chartier9e050df2017-08-09 10:05:47 -070025#include "verifier/verifier_enums.h"
Chang Xing605fe242017-07-20 15:57:21 -070026
27namespace art {
28
Andreas Gampe87658f32019-04-18 18:39:02 +000029AotClassLinker::AotClassLinker(InternTable* intern_table)
30 : ClassLinker(intern_table, /*fast_class_not_found_exceptions=*/ false) {}
Chang Xing605fe242017-07-20 15:57:21 -070031
32AotClassLinker::~AotClassLinker() {}
33
34// Wrap the original InitializeClass with creation of transaction when in strict mode.
Vladimir Marko4617d582019-03-28 13:48:31 +000035bool AotClassLinker::InitializeClass(Thread* self,
36 Handle<mirror::Class> klass,
37 bool can_init_statics,
38 bool can_init_parents) {
Chang Xing605fe242017-07-20 15:57:21 -070039 Runtime* const runtime = Runtime::Current();
Chang Xingadbb91c2017-07-17 11:23:55 -070040 bool strict_mode_ = runtime->IsActiveStrictTransactionMode();
Chang Xing605fe242017-07-20 15:57:21 -070041
42 DCHECK(klass != nullptr);
43 if (klass->IsInitialized() || klass->IsInitializing()) {
44 return ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
45 }
46
Chang Xingadbb91c2017-07-17 11:23:55 -070047 // Don't initialize klass if it's superclass is not initialized, because superclass might abort
48 // the transaction and rolled back after klass's change is commited.
49 if (strict_mode_ && !klass->IsInterface() && klass->HasSuperClass()) {
Vladimir Marko2c64a832018-01-04 11:31:56 +000050 if (klass->GetSuperClass()->GetStatus() == ClassStatus::kInitializing) {
Chang Xingadbb91c2017-07-17 11:23:55 -070051 runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve "
52 + klass->PrettyTypeOf() + " because it's superclass is not initialized.");
53 return false;
54 }
55 }
56
57 if (strict_mode_) {
Vladimir Marko4617d582019-03-28 13:48:31 +000058 runtime->EnterTransactionMode(true, klass.Get()->AsClass().Ptr());
Chang Xing605fe242017-07-20 15:57:21 -070059 }
60 bool success = ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
61
Chang Xingadbb91c2017-07-17 11:23:55 -070062 if (strict_mode_) {
Chang Xing605fe242017-07-20 15:57:21 -070063 if (success) {
64 // Exit Transaction if success.
65 runtime->ExitTransactionMode();
66 } else {
Nicolas Geoffrayabadf022017-08-03 08:25:41 +000067 // If not successfully initialized, the last transaction must abort. Don't rollback
68 // immediately, leave the cleanup to compiler driver which needs abort message and exception.
69 DCHECK(runtime->IsTransactionAborted());
Chang Xing605fe242017-07-20 15:57:21 -070070 DCHECK(self->IsExceptionPending());
71 }
72 }
73 return success;
74}
Mathieu Chartier9e050df2017-08-09 10:05:47 -070075
76verifier::FailureKind AotClassLinker::PerformClassVerification(Thread* self,
77 Handle<mirror::Class> klass,
78 verifier::HardFailLogMode log_level,
79 std::string* error_msg) {
80 Runtime* const runtime = Runtime::Current();
81 CompilerCallbacks* callbacks = runtime->GetCompilerCallbacks();
Andreas Gampe5d3b0022017-08-31 10:36:31 -070082 ClassStatus old_status = callbacks->GetPreviousClassState(
83 ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()));
Andreas Gamped3ea5b32017-09-04 14:55:34 -070084 // Was it verified? Report no failure.
Vladimir Marko2c64a832018-01-04 11:31:56 +000085 if (old_status >= ClassStatus::kVerified) {
Mathieu Chartier9e050df2017-08-09 10:05:47 -070086 return verifier::FailureKind::kNoFailure;
87 }
Andreas Gamped3ea5b32017-09-04 14:55:34 -070088 // Does it need to be verified at runtime? Report soft failure.
Vladimir Marko2c64a832018-01-04 11:31:56 +000089 if (old_status >= ClassStatus::kRetryVerificationAtRuntime) {
Andreas Gamped3ea5b32017-09-04 14:55:34 -070090 // Error messages from here are only reported through -verbose:class. It is not worth it to
91 // create a message.
92 return verifier::FailureKind::kSoftFailure;
93 }
94 // Do the actual work.
Mathieu Chartier9e050df2017-08-09 10:05:47 -070095 return ClassLinker::PerformClassVerification(self, klass, log_level, error_msg);
96}
97
Chang Xing605fe242017-07-20 15:57:21 -070098} // namespace art