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 | |
| 19 | #include "handle_scope-inl.h" |
| 20 | #include "mirror/class.h" |
| 21 | #include "mirror/object-inl.h" |
| 22 | #include "runtime.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | AotClassLinker::AotClassLinker(InternTable *intern_table) : ClassLinker(intern_table) {} |
| 27 | |
| 28 | AotClassLinker::~AotClassLinker() {} |
| 29 | |
| 30 | // Wrap the original InitializeClass with creation of transaction when in strict mode. |
| 31 | bool AotClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 32 | bool can_init_statics, bool can_init_parents) { |
| 33 | Runtime* const runtime = Runtime::Current(); |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame^] | 34 | bool strict_mode_ = runtime->IsActiveStrictTransactionMode(); |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 35 | |
| 36 | DCHECK(klass != nullptr); |
| 37 | if (klass->IsInitialized() || klass->IsInitializing()) { |
| 38 | return ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents); |
| 39 | } |
| 40 | |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame^] | 41 | // Don't initialize klass if it's superclass is not initialized, because superclass might abort |
| 42 | // the transaction and rolled back after klass's change is commited. |
| 43 | if (strict_mode_ && !klass->IsInterface() && klass->HasSuperClass()) { |
| 44 | if (klass->GetSuperClass()->GetStatus() == mirror::Class::kStatusInitializing) { |
| 45 | runtime->AbortTransactionAndThrowAbortError(self, "Can't resolve " |
| 46 | + klass->PrettyTypeOf() + " because it's superclass is not initialized."); |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (strict_mode_) { |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 52 | runtime->EnterTransactionMode(true, klass.Get()->AsClass()); |
| 53 | } |
| 54 | bool success = ClassLinker::InitializeClass(self, klass, can_init_statics, can_init_parents); |
| 55 | |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame^] | 56 | if (strict_mode_) { |
Chang Xing | 605fe24 | 2017-07-20 15:57:21 -0700 | [diff] [blame] | 57 | if (success) { |
| 58 | // Exit Transaction if success. |
| 59 | runtime->ExitTransactionMode(); |
| 60 | } else { |
| 61 | // If not successfully initialized, the last transaction must abort. Don't rollback |
| 62 | // immediately, leave the cleanup to compiler driver which needs abort message and exception. |
| 63 | DCHECK(runtime->IsTransactionAborted()); |
| 64 | DCHECK(self->IsExceptionPending()); |
| 65 | } |
| 66 | } |
| 67 | return success; |
| 68 | } |
| 69 | } // namespace art |