1 /*
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 
19 #include "class_status.h"
20 #include "compiler_callbacks.h"
21 #include "dex/class_reference.h"
22 #include "handle_scope-inl.h"
23 #include "mirror/class-inl.h"
24 #include "runtime.h"
25 #include "verifier/verifier_enums.h"
26 
27 namespace art {
28 
AotClassLinker(InternTable * intern_table)29 AotClassLinker::AotClassLinker(InternTable* intern_table)
30     : ClassLinker(intern_table, /*fast_class_not_found_exceptions=*/ false) {}
31 
~AotClassLinker()32 AotClassLinker::~AotClassLinker() {}
33 
34 // Wrap the original InitializeClass with creation of transaction when in strict mode.
InitializeClass(Thread * self,Handle<mirror::Class> klass,bool can_init_statics,bool can_init_parents)35 bool AotClassLinker::InitializeClass(Thread* self,
36                                      Handle<mirror::Class> klass,
37                                      bool can_init_statics,
38                                      bool can_init_parents) {
39   Runtime* const runtime = Runtime::Current();
40   bool strict_mode_ = runtime->IsActiveStrictTransactionMode();
41 
42   DCHECK(klass != nullptr);
43   if (klass->IsInitialized() || klass->IsInitializing()) {
44     return ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
45   }
46 
47   // 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()) {
50     if (klass->GetSuperClass()->GetStatus() == ClassStatus::kInitializing) {
51       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_) {
58     runtime->EnterTransactionMode(true, klass.Get()->AsClass().Ptr());
59   }
60   bool success = ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents);
61 
62   if (strict_mode_) {
63     if (success) {
64       // Exit Transaction if success.
65       runtime->ExitTransactionMode();
66     } else {
67       // 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());
70       DCHECK(self->IsExceptionPending());
71     }
72   }
73   return success;
74 }
75 
PerformClassVerification(Thread * self,Handle<mirror::Class> klass,verifier::HardFailLogMode log_level,std::string * error_msg)76 verifier::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();
82   ClassStatus old_status = callbacks->GetPreviousClassState(
83       ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()));
84   // Was it verified? Report no failure.
85   if (old_status >= ClassStatus::kVerified) {
86     return verifier::FailureKind::kNoFailure;
87   }
88   // Does it need to be verified at runtime? Report soft failure.
89   if (old_status >= ClassStatus::kRetryVerificationAtRuntime) {
90     // 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.
95   return ClassLinker::PerformClassVerification(self, klass, log_level, error_msg);
96 }
97 
98 }  // namespace art
99