1 /*
2  * Copyright (C) 2013 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 "dex_compilation_unit.h"
18 
19 #include "art_field.h"
20 #include "base/utils.h"
21 #include "dex/class_accessor-inl.h"
22 #include "dex/code_item_accessors-inl.h"
23 #include "dex/descriptors_names.h"
24 #include "mirror/class-inl.h"
25 #include "mirror/dex_cache.h"
26 #include "scoped_thread_state_change-inl.h"
27 
28 namespace art {
29 
DexCompilationUnit(Handle<mirror::ClassLoader> class_loader,ClassLinker * class_linker,const DexFile & dex_file,const dex::CodeItem * code_item,uint16_t class_def_idx,uint32_t method_idx,uint32_t access_flags,const VerifiedMethod * verified_method,Handle<mirror::DexCache> dex_cache,Handle<mirror::Class> compiling_class)30 DexCompilationUnit::DexCompilationUnit(Handle<mirror::ClassLoader> class_loader,
31                                        ClassLinker* class_linker,
32                                        const DexFile& dex_file,
33                                        const dex::CodeItem* code_item,
34                                        uint16_t class_def_idx,
35                                        uint32_t method_idx,
36                                        uint32_t access_flags,
37                                        const VerifiedMethod* verified_method,
38                                        Handle<mirror::DexCache> dex_cache,
39                                        Handle<mirror::Class> compiling_class)
40     : class_loader_(class_loader),
41       class_linker_(class_linker),
42       dex_file_(&dex_file),
43       code_item_(code_item),
44       class_def_idx_(class_def_idx),
45       dex_method_idx_(method_idx),
46       access_flags_(access_flags),
47       verified_method_(verified_method),
48       dex_cache_(dex_cache),
49       code_item_accessor_(dex_file, code_item),
50       compiling_class_(compiling_class) {}
51 
GetSymbol()52 const std::string& DexCompilationUnit::GetSymbol() {
53   if (symbol_.empty()) {
54     symbol_ = "dex_";
55     symbol_ += MangleForJni(dex_file_->PrettyMethod(dex_method_idx_));
56   }
57   return symbol_;
58 }
59 
RequiresConstructorBarrier() const60 bool DexCompilationUnit::RequiresConstructorBarrier() const {
61   // Constructor barriers are applicable only for <init> methods.
62   DCHECK(!IsStatic());
63   DCHECK(IsConstructor());
64 
65   // We require a constructor barrier if there are final instance fields.
66   if (GetCompilingClass().GetReference() != nullptr && !GetCompilingClass().IsNull()) {
67     // Decoding class data can be slow, so iterate over fields of the compiling class if resolved.
68     ScopedObjectAccess soa(Thread::Current());
69     ObjPtr<mirror::Class> compiling_class = GetCompilingClass().Get();
70     for (size_t i = 0, size = compiling_class->NumInstanceFields(); i != size; ++i) {
71       ArtField* field = compiling_class->GetInstanceField(i);
72       if (field->IsFinal()) {
73         return true;
74       }
75     }
76   } else {
77     // Iterate over field definitions in the class data.
78     ClassAccessor accessor(*GetDexFile(), GetClassDefIndex());
79     for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
80       if (field.IsFinal()) {
81         return true;
82       }
83     }
84   }
85   return false;
86 }
87 
88 }  // namespace art
89