1 /*
2  * Copyright (C) 2014 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 "mir_field_info.h"
18 
19 #include <string.h>
20 
21 #include "base/logging.h"
22 #include "dex/verified_method.h"
23 #include "driver/compiler_driver.h"
24 #include "driver/compiler_driver-inl.h"
25 #include "mirror/class_loader.h"  // Only to allow casts in Handle<ClassLoader>.
26 #include "mirror/dex_cache.h"     // Only to allow casts in Handle<DexCache>.
27 #include "scoped_thread_state_change.h"
28 #include "handle_scope-inl.h"
29 
30 namespace art {
31 
Resolve(CompilerDriver * compiler_driver,const DexCompilationUnit * mUnit,MirIFieldLoweringInfo * field_infos,size_t count)32 void MirIFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver,
33                                     const DexCompilationUnit* mUnit,
34                                     MirIFieldLoweringInfo* field_infos, size_t count) {
35   if (kIsDebugBuild) {
36     DCHECK(field_infos != nullptr);
37     DCHECK_NE(count, 0u);
38     for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
39       MirIFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType(), it->IsQuickened());
40       unresolved.field_offset_ = it->field_offset_;
41       unresolved.CheckEquals(*it);
42     }
43   }
44 
45   // We're going to resolve fields and check access in a tight loop. It's better to hold
46   // the lock and needed references once than re-acquiring them again and again.
47   ScopedObjectAccess soa(Thread::Current());
48   StackHandleScope<3> hs(soa.Self());
49   Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit)));
50   Handle<mirror::ClassLoader> class_loader(
51       hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit)));
52   Handle<mirror::Class> referrer_class(hs.NewHandle(
53       compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
54   const VerifiedMethod* const verified_method = mUnit->GetVerifiedMethod();
55   // Even if the referrer class is unresolved (i.e. we're compiling a method without class
56   // definition) we still want to resolve fields and record all available info.
57   for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
58     uint32_t field_idx;
59     ArtField* resolved_field;
60     if (!it->IsQuickened()) {
61       field_idx = it->field_idx_;
62       resolved_field = compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit,
63                                                      field_idx, false);
64     } else {
65       const auto mir_offset = it->field_idx_;
66       // For quickened instructions, it->field_offset_ actually contains the mir offset.
67       // We need to use the de-quickening info to get dex file / field idx
68       auto* field_idx_ptr = verified_method->GetDequickenIndex(mir_offset);
69       CHECK(field_idx_ptr != nullptr);
70       field_idx = field_idx_ptr->index;
71       StackHandleScope<1> hs2(soa.Self());
72       auto h_dex_cache = hs2.NewHandle(compiler_driver->FindDexCache(field_idx_ptr->dex_file));
73       resolved_field = compiler_driver->ResolveFieldWithDexFile(
74           soa, h_dex_cache, class_loader, field_idx_ptr->dex_file, field_idx, false);
75       // Since we don't have a valid field index we can't go slow path later.
76       CHECK(resolved_field != nullptr);
77     }
78     if (UNLIKELY(resolved_field == nullptr)) {
79       continue;
80     }
81     compiler_driver->GetResolvedFieldDexFileLocation(resolved_field,
82         &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_);
83     bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field);
84     it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field);
85     std::pair<bool, bool> fast_path = compiler_driver->IsFastInstanceField(
86         dex_cache.Get(), referrer_class.Get(), resolved_field, field_idx);
87     it->flags_ = 0u |  // Without kFlagIsStatic.
88         (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
89         (is_volatile ? kFlagIsVolatile : 0u) |
90         (fast_path.first ? kFlagFastGet : 0u) |
91         (fast_path.second ? kFlagFastPut : 0u);
92   }
93 }
94 
Resolve(CompilerDriver * compiler_driver,const DexCompilationUnit * mUnit,MirSFieldLoweringInfo * field_infos,size_t count)95 void MirSFieldLoweringInfo::Resolve(CompilerDriver* compiler_driver,
96                                     const DexCompilationUnit* mUnit,
97                                     MirSFieldLoweringInfo* field_infos, size_t count) {
98   if (kIsDebugBuild) {
99     DCHECK(field_infos != nullptr);
100     DCHECK_NE(count, 0u);
101     for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
102       MirSFieldLoweringInfo unresolved(it->field_idx_, it->MemAccessType());
103       // In 64-bit builds, there's padding after storage_index_, don't include it in memcmp.
104       size_t size = OFFSETOF_MEMBER(MirSFieldLoweringInfo, storage_index_) +
105           sizeof(it->storage_index_);
106       DCHECK_EQ(memcmp(&unresolved, &*it, size), 0);
107     }
108   }
109 
110   // We're going to resolve fields and check access in a tight loop. It's better to hold
111   // the lock and needed references once than re-acquiring them again and again.
112   ScopedObjectAccess soa(Thread::Current());
113   StackHandleScope<3> hs(soa.Self());
114   Handle<mirror::DexCache> dex_cache(hs.NewHandle(compiler_driver->GetDexCache(mUnit)));
115   Handle<mirror::ClassLoader> class_loader(
116       hs.NewHandle(compiler_driver->GetClassLoader(soa, mUnit)));
117   Handle<mirror::Class> referrer_class_handle(hs.NewHandle(
118       compiler_driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit)));
119   // Even if the referrer class is unresolved (i.e. we're compiling a method without class
120   // definition) we still want to resolve fields and record all available info.
121 
122   for (auto it = field_infos, end = field_infos + count; it != end; ++it) {
123     uint32_t field_idx = it->field_idx_;
124     ArtField* resolved_field =
125         compiler_driver->ResolveField(soa, dex_cache, class_loader, mUnit, field_idx, true);
126     if (UNLIKELY(resolved_field == nullptr)) {
127       continue;
128     }
129     compiler_driver->GetResolvedFieldDexFileLocation(resolved_field,
130         &it->declaring_dex_file_, &it->declaring_class_idx_, &it->declaring_field_idx_);
131     bool is_volatile = compiler_driver->IsFieldVolatile(resolved_field) ? 1u : 0u;
132 
133     mirror::Class* referrer_class = referrer_class_handle.Get();
134     std::pair<bool, bool> fast_path = compiler_driver->IsFastStaticField(
135         dex_cache.Get(), referrer_class, resolved_field, field_idx, &it->storage_index_);
136     uint16_t flags = kFlagIsStatic |
137         (it->flags_ & (kMemAccessTypeMask << kBitMemAccessTypeBegin)) |
138         (is_volatile ? kFlagIsVolatile : 0u) |
139         (fast_path.first ? kFlagFastGet : 0u) |
140         (fast_path.second ? kFlagFastPut : 0u);
141     if (fast_path.first) {
142       it->field_offset_ = compiler_driver->GetFieldOffset(resolved_field);
143       bool is_referrers_class =
144           compiler_driver->IsStaticFieldInReferrerClass(referrer_class, resolved_field);
145       bool is_class_initialized =
146           compiler_driver->IsStaticFieldsClassInitialized(referrer_class, resolved_field);
147       bool is_class_in_dex_cache = !is_referrers_class &&  // If referrer's class, we don't care.
148           compiler_driver->CanAssumeTypeIsPresentInDexCache(*dex_cache->GetDexFile(),
149                                                             it->storage_index_);
150       flags |= (is_referrers_class ? kFlagIsReferrersClass : 0u) |
151           (is_class_initialized ? kFlagClassIsInitialized : 0u) |
152           (is_class_in_dex_cache ? kFlagClassIsInDexCache : 0u);
153     }
154     it->flags_ = flags;
155   }
156 }
157 
158 }  // namespace art
159