1 /*
2  * Copyright (C) 2012 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 #ifndef ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_
18 #define ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_
19 
20 #include "compiler_driver.h"
21 
22 #include "art_field-inl.h"
23 #include "art_method-inl.h"
24 #include "base/enums.h"
25 #include "class_linker-inl.h"
26 #include "dex_compilation_unit.h"
27 #include "mirror/class_loader.h"
28 #include "mirror/dex_cache-inl.h"
29 #include "scoped_thread_state_change-inl.h"
30 #include "handle_scope-inl.h"
31 
32 namespace art {
33 
ResolveClass(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,dex::TypeIndex cls_index,const DexCompilationUnit * mUnit)34 inline mirror::Class* CompilerDriver::ResolveClass(
35     const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
36     Handle<mirror::ClassLoader> class_loader, dex::TypeIndex cls_index,
37     const DexCompilationUnit* mUnit) {
38   DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
39   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
40   mirror::Class* cls = mUnit->GetClassLinker()->ResolveType(
41       *mUnit->GetDexFile(), cls_index, dex_cache, class_loader);
42   DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending());
43   if (UNLIKELY(cls == nullptr)) {
44     // Clean up any exception left by type resolution.
45     soa.Self()->ClearException();
46   }
47   return cls;
48 }
49 
ResolveCompilingMethodsClass(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,const DexCompilationUnit * mUnit)50 inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
51     const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
52     Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
53   DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
54   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
55   const DexFile::MethodId& referrer_method_id =
56       mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
57   return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit);
58 }
59 
ResolveFieldWithDexFile(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,const DexFile * dex_file,uint32_t field_idx,bool is_static)60 inline ArtField* CompilerDriver::ResolveFieldWithDexFile(
61     const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
62     Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file,
63     uint32_t field_idx, bool is_static) {
64   DCHECK_EQ(dex_cache->GetDexFile(), dex_file);
65   ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField(
66       *dex_file, field_idx, dex_cache, class_loader, is_static);
67   DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
68   if (UNLIKELY(resolved_field == nullptr)) {
69     // Clean up any exception left by type resolution.
70     soa.Self()->ClearException();
71     return nullptr;
72   }
73   if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
74     // ClassLinker can return a field of the wrong kind directly from the DexCache.
75     // Silently return null on such incompatible class change.
76     return nullptr;
77   }
78   return resolved_field;
79 }
80 
ResolveField(const ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,const DexCompilationUnit * mUnit,uint32_t field_idx,bool is_static)81 inline ArtField* CompilerDriver::ResolveField(
82     const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
83     Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
84     uint32_t field_idx, bool is_static) {
85   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
86   return ResolveFieldWithDexFile(soa, dex_cache, class_loader, mUnit->GetDexFile(), field_idx,
87                                  is_static);
88 }
89 
IsFastInstanceField(mirror::DexCache * dex_cache,mirror::Class * referrer_class,ArtField * resolved_field,uint16_t field_idx)90 inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
91     mirror::DexCache* dex_cache, mirror::Class* referrer_class,
92     ArtField* resolved_field, uint16_t field_idx) {
93   DCHECK(!resolved_field->IsStatic());
94   ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
95   bool fast_get = referrer_class != nullptr &&
96       referrer_class->CanAccessResolvedField(fields_class,
97                                              resolved_field,
98                                              dex_cache,
99                                              field_idx);
100   bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
101   return std::make_pair(fast_get, fast_put);
102 }
103 
104 template <typename ArtMember>
CanAccessResolvedMember(mirror::Class * referrer_class ATTRIBUTE_UNUSED,mirror::Class * access_to ATTRIBUTE_UNUSED,ArtMember * member ATTRIBUTE_UNUSED,mirror::DexCache * dex_cache ATTRIBUTE_UNUSED,uint32_t field_idx ATTRIBUTE_UNUSED)105 inline bool CompilerDriver::CanAccessResolvedMember(mirror::Class* referrer_class ATTRIBUTE_UNUSED,
106                                                     mirror::Class* access_to ATTRIBUTE_UNUSED,
107                                                     ArtMember* member ATTRIBUTE_UNUSED,
108                                                     mirror::DexCache* dex_cache ATTRIBUTE_UNUSED,
109                                                     uint32_t field_idx ATTRIBUTE_UNUSED) {
110   // Not defined for ArtMember values other than ArtField or ArtMethod.
111   UNREACHABLE();
112 }
113 
114 template <>
115 inline bool CompilerDriver::CanAccessResolvedMember<ArtField>(mirror::Class* referrer_class,
116                                                               mirror::Class* access_to,
117                                                               ArtField* field,
118                                                               mirror::DexCache* dex_cache,
119                                                               uint32_t field_idx) {
120   return referrer_class->CanAccessResolvedField(access_to, field, dex_cache, field_idx);
121 }
122 
123 template <>
124 inline bool CompilerDriver::CanAccessResolvedMember<ArtMethod>(
125     mirror::Class* referrer_class,
126     mirror::Class* access_to,
127     ArtMethod* method,
128     mirror::DexCache* dex_cache,
129     uint32_t field_idx) {
130   return referrer_class->CanAccessResolvedMethod(access_to, method, dex_cache, field_idx);
131 }
132 
ResolveMethod(ScopedObjectAccess & soa,Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,const DexCompilationUnit * mUnit,uint32_t method_idx,InvokeType invoke_type,bool check_incompatible_class_change)133 inline ArtMethod* CompilerDriver::ResolveMethod(
134     ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
135     Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
136     uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) {
137   DCHECK_EQ(class_loader.Get(), mUnit->GetClassLoader().Get());
138   ArtMethod* resolved_method =
139       check_incompatible_class_change
140           ? mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kForceICCECheck>(
141               *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type)
142           : mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
143               *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type);
144   if (UNLIKELY(resolved_method == nullptr)) {
145     DCHECK(soa.Self()->IsExceptionPending());
146     // Clean up any exception left by type resolution.
147     soa.Self()->ClearException();
148   }
149   return resolved_method;
150 }
151 
152 }  // namespace art
153 
154 #endif  // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_
155