Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 5d3b002 | 2017-08-31 10:36:31 -0700 | [diff] [blame] | 19 | #include "class_status.h" |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 20 | #include "compiler_callbacks.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 21 | #include "dex/class_reference.h" |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 22 | #include "handle_scope-inl.h" |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 24 | #include "runtime.h" |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 25 | #include "verifier/verifier_enums.h" |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 29 | AotClassLinker::AotClassLinker(InternTable* intern_table) |
| 30 | : ClassLinker(intern_table, /*fast_class_not_found_exceptions=*/ false) {} |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 31 | |
| 32 | AotClassLinker::~AotClassLinker() {} |
| 33 | |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 34 | bool AotClassLinker::CanAllocClass() { |
| 35 | // AllocClass doesn't work under transaction, so we abort. |
| 36 | if (Runtime::Current()->IsActiveTransaction()) { |
| 37 | Runtime::Current()->AbortTransactionAndThrowAbortError( |
| 38 | Thread::Current(), "Can't resolve type within transaction."); |
| 39 | return false; |
| 40 | } |
| 41 | return ClassLinker::CanAllocClass(); |
| 42 | } |
| 43 | |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 44 | // Wrap the original InitializeClass with creation of transaction when in strict mode. |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 45 | bool AotClassLinker::InitializeClass(Thread* self, |
| 46 | Handle<mirror::Class> klass, |
| 47 | bool can_init_statics, |
| 48 | bool can_init_parents) { |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 49 | Runtime* const runtime = Runtime::Current(); |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 50 | bool strict_mode_ = runtime->IsActiveStrictTransactionMode(); |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 51 | |
| 52 | DCHECK(klass != nullptr); |
| 53 | if (klass->IsInitialized() || klass->IsInitializing()) { |
| 54 | return ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents); |
| 55 | } |
| 56 | |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 57 | // When in strict_mode, don't initialize a class if it belongs to boot but not initialized. |
| 58 | if (strict_mode_ && klass->IsBootStrapClassLoaded()) { |
| 59 | runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve " |
| 60 | + klass->PrettyTypeOf() + " because it is an uninitialized boot class."); |
| 61 | return false; |
| 62 | } |
| 63 | |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 64 | // Don't initialize klass if it's superclass is not initialized, because superclass might abort |
| 65 | // the transaction and rolled back after klass's change is commited. |
| 66 | if (strict_mode_ && !klass->IsInterface() && klass->HasSuperClass()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 67 | if (klass->GetSuperClass()->GetStatus() == ClassStatus::kInitializing) { |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 68 | runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve " |
| 69 | + klass->PrettyTypeOf() + " because it's superclass is not initialized."); |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (strict_mode_) { |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 75 | runtime->EnterTransactionMode(/*strict=*/ true, klass.Get()); |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 76 | } |
| 77 | bool success = ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents); |
| 78 | |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 79 | if (strict_mode_) { |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 80 | if (success) { |
| 81 | // Exit Transaction if success. |
| 82 | runtime->ExitTransactionMode(); |
| 83 | } else { |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 84 | // If not successfully initialized, don't rollback immediately, leave the cleanup to compiler |
| 85 | // driver which needs abort message and exception. |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 86 | DCHECK(self->IsExceptionPending()); |
| 87 | } |
| 88 | } |
| 89 | return success; |
| 90 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 91 | |
| 92 | verifier::FailureKind AotClassLinker::PerformClassVerification(Thread* self, |
| 93 | Handle<mirror::Class> klass, |
| 94 | verifier::HardFailLogMode log_level, |
| 95 | std::string* error_msg) { |
| 96 | Runtime* const runtime = Runtime::Current(); |
| 97 | CompilerCallbacks* callbacks = runtime->GetCompilerCallbacks(); |
Andreas Gampe | 5d3b002 | 2017-08-31 10:36:31 -0700 | [diff] [blame] | 98 | ClassStatus old_status = callbacks->GetPreviousClassState( |
| 99 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex())); |
Andreas Gampe | d3ea5b3 | 2017-09-04 14:55:34 -0700 | [diff] [blame] | 100 | // Was it verified? Report no failure. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 101 | if (old_status >= ClassStatus::kVerified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 102 | return verifier::FailureKind::kNoFailure; |
| 103 | } |
Andreas Gampe | d3ea5b3 | 2017-09-04 14:55:34 -0700 | [diff] [blame] | 104 | // Does it need to be verified at runtime? Report soft failure. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 105 | if (old_status >= ClassStatus::kRetryVerificationAtRuntime) { |
Andreas Gampe | d3ea5b3 | 2017-09-04 14:55:34 -0700 | [diff] [blame] | 106 | // Error messages from here are only reported through -verbose:class. It is not worth it to |
| 107 | // create a message. |
| 108 | return verifier::FailureKind::kSoftFailure; |
| 109 | } |
| 110 | // Do the actual work. |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 111 | return ClassLinker::PerformClassVerification(self, klass, log_level, error_msg); |
| 112 | } |
| 113 | |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 114 | } // namespace art |