1 /*
2  * Copyright (C) 2015 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 "sharpening.h"
18 
19 #include "art_method-inl.h"
20 #include "base/casts.h"
21 #include "base/enums.h"
22 #include "base/logging.h"
23 #include "class_linker.h"
24 #include "code_generator.h"
25 #include "driver/compiler_options.h"
26 #include "driver/dex_compilation_unit.h"
27 #include "gc/heap.h"
28 #include "gc/space/image_space.h"
29 #include "handle_scope-inl.h"
30 #include "jit/jit.h"
31 #include "mirror/dex_cache.h"
32 #include "mirror/string.h"
33 #include "nodes.h"
34 #include "runtime.h"
35 #include "scoped_thread_state_change-inl.h"
36 
37 namespace art {
38 
IsInBootImage(ArtMethod * method)39 static bool IsInBootImage(ArtMethod* method) {
40   gc::Heap* heap = Runtime::Current()->GetHeap();
41   DCHECK_EQ(heap->IsBootImageAddress(method),
42             std::any_of(heap->GetBootImageSpaces().begin(),
43                         heap->GetBootImageSpaces().end(),
44                         [=](gc::space::ImageSpace* space) REQUIRES_SHARED(Locks::mutator_lock_) {
45                           return space->GetImageHeader().GetMethodsSection().Contains(
46                               reinterpret_cast<uint8_t*>(method) - space->Begin());
47                         }));
48   return heap->IsBootImageAddress(method);
49 }
50 
BootImageAOTCanEmbedMethod(ArtMethod * method,const CompilerOptions & compiler_options)51 static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
52   DCHECK(compiler_options.IsBootImage() || compiler_options.IsBootImageExtension());
53   ScopedObjectAccess soa(Thread::Current());
54   ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
55   DCHECK(klass != nullptr);
56   const DexFile& dex_file = klass->GetDexFile();
57   return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
58 }
59 
SharpenLoadMethod(ArtMethod * callee,bool has_method_id,bool for_interface_call,CodeGenerator * codegen)60 HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenLoadMethod(
61     ArtMethod* callee,
62     bool has_method_id,
63     bool for_interface_call,
64     CodeGenerator* codegen) {
65   if (kIsDebugBuild) {
66     ScopedObjectAccess soa(Thread::Current());  // Required for GetDeclaringClass below.
67     DCHECK(callee != nullptr);
68     DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
69   }
70 
71   MethodLoadKind method_load_kind;
72   CodePtrLocation code_ptr_location;
73   uint64_t method_load_data = 0u;
74 
75   // Note: we never call an ArtMethod through a known code pointer, as
76   // we do not want to keep on invoking it if it gets deoptimized. This
77   // applies to both AOT and JIT.
78   // This also avoids having to find out if the code pointer of an ArtMethod
79   // is the resolution trampoline (for ensuring the class is initialized), or
80   // the interpreter entrypoint. Such code pointers we do not want to call
81   // directly.
82   // Only in the case of a recursive call can we call directly, as we know the
83   // class is initialized already or being initialized, and the call will not
84   // be invoked once the method is deoptimized.
85 
86   // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
87   // situations.
88   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
89   if (callee == codegen->GetGraph()->GetArtMethod() &&
90       !codegen->GetGraph()->IsDebuggable() &&
91       // The runtime expects the canonical interface method being passed as
92       // hidden argument when doing an invokeinterface. Because default methods
93       // can be called through invokevirtual, we may get a copied method if we
94       // load 'recursively'.
95       (!for_interface_call || !callee->IsDefault())) {
96     // Recursive load.
97     method_load_kind = MethodLoadKind::kRecursive;
98     code_ptr_location = CodePtrLocation::kCallSelf;
99   } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
100     if (!compiler_options.GetCompilePic()) {
101       // Test configuration, do not sharpen.
102       method_load_kind = MethodLoadKind::kRuntimeCall;
103     } else if (IsInBootImage(callee)) {
104       DCHECK(compiler_options.IsBootImageExtension());
105       method_load_kind = MethodLoadKind::kBootImageRelRo;
106     } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
107       method_load_kind = MethodLoadKind::kBootImageLinkTimePcRelative;
108     } else if (!has_method_id) {
109       method_load_kind = MethodLoadKind::kRuntimeCall;
110     } else {
111       DCHECK(!callee->IsCopied());
112       // Use PC-relative access to the .bss methods array.
113       method_load_kind = MethodLoadKind::kBssEntry;
114     }
115     code_ptr_location = CodePtrLocation::kCallArtMethod;
116   } else if (compiler_options.IsJitCompiler()) {
117     ScopedObjectAccess soa(Thread::Current());
118     if (Runtime::Current()->GetJit()->CanEncodeMethod(
119             callee,
120             compiler_options.IsJitCompilerForSharedCode())) {
121       method_load_kind = MethodLoadKind::kJitDirectAddress;
122       method_load_data = reinterpret_cast<uintptr_t>(callee);
123       code_ptr_location = CodePtrLocation::kCallArtMethod;
124     } else {
125       // Do not sharpen.
126       method_load_kind = MethodLoadKind::kRuntimeCall;
127       code_ptr_location = CodePtrLocation::kCallArtMethod;
128     }
129   } else if (IsInBootImage(callee)) {
130     // Use PC-relative access to the .data.bimg.rel.ro methods array.
131     method_load_kind = MethodLoadKind::kBootImageRelRo;
132     code_ptr_location = CodePtrLocation::kCallArtMethod;
133   } else if (!has_method_id) {
134     method_load_kind = MethodLoadKind::kRuntimeCall;
135     code_ptr_location = CodePtrLocation::kCallArtMethod;
136   } else {
137     DCHECK(!callee->IsCopied());
138     // Use PC-relative access to the .bss methods array.
139     method_load_kind = MethodLoadKind::kBssEntry;
140     code_ptr_location = CodePtrLocation::kCallArtMethod;
141   }
142 
143   if (method_load_kind != MethodLoadKind::kRuntimeCall && callee->IsCriticalNative()) {
144     DCHECK_NE(method_load_kind, MethodLoadKind::kRecursive);
145     DCHECK(callee->IsStatic());
146     code_ptr_location = CodePtrLocation::kCallCriticalNative;
147   }
148 
149   if (codegen->GetGraph()->IsDebuggable()) {
150     // For debuggable apps always use the code pointer from ArtMethod
151     // so that we don't circumvent instrumentation stubs if installed.
152     code_ptr_location = CodePtrLocation::kCallArtMethod;
153   }
154 
155   HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
156       method_load_kind, code_ptr_location, method_load_data
157   };
158   return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
159 }
160 
ComputeLoadClassKind(HLoadClass * load_class,CodeGenerator * codegen,const DexCompilationUnit & dex_compilation_unit)161 HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
162     HLoadClass* load_class,
163     CodeGenerator* codegen,
164     const DexCompilationUnit& dex_compilation_unit) {
165   Handle<mirror::Class> klass = load_class->GetClass();
166   DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
167          load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
168       << load_class->GetLoadKind();
169   DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
170   const DexFile& dex_file = load_class->GetDexFile();
171   dex::TypeIndex type_index = load_class->GetTypeIndex();
172   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
173 
174   bool is_in_boot_image = false;
175   HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
176 
177   if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
178     DCHECK(!load_class->NeedsAccessCheck());
179     // Loading from the ArtMethod* is the most efficient retrieval in code size.
180     // TODO: This may not actually be true for all architectures and
181     // locations of target classes. The additional register pressure
182     // for using the ArtMethod* should be considered.
183     desired_load_kind = HLoadClass::LoadKind::kReferrersClass;
184   } else if (load_class->NeedsAccessCheck()) {
185     DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kRuntimeCall);
186     if (klass != nullptr) {
187       // Resolved class that needs access check must be really inaccessible
188       // and the access check is bound to fail. Just emit the runtime call.
189       desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
190     } else if (compiler_options.IsJitCompiler()) {
191       // Unresolved class while JITting means that either we never hit this
192       // instruction or it failed. Either way, just emit the runtime call.
193       // (Though we could consider emitting Deoptimize instead and
194       // recompile if the instruction succeeds in interpreter.)
195       desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
196     } else {
197       // For AOT, check if the class is in the same literal package as the
198       // compiling class and pick an appropriate .bss entry.
199       auto package_length = [](const char* descriptor) {
200         const char* slash_pos = strrchr(descriptor, '/');
201         return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
202       };
203       const char* klass_descriptor = dex_file.StringByTypeIdx(type_index);
204       const uint32_t klass_package_length = package_length(klass_descriptor);
205       const DexFile* referrer_dex_file = dex_compilation_unit.GetDexFile();
206       const dex::TypeIndex referrer_type_index =
207           referrer_dex_file->GetClassDef(dex_compilation_unit.GetClassDefIndex()).class_idx_;
208       const char* referrer_descriptor = referrer_dex_file->StringByTypeIdx(referrer_type_index);
209       const uint32_t referrer_package_length = package_length(referrer_descriptor);
210       bool same_package =
211           (referrer_package_length == klass_package_length) &&
212           memcmp(referrer_descriptor, klass_descriptor, referrer_package_length) == 0;
213       desired_load_kind = same_package
214           ? HLoadClass::LoadKind::kBssEntryPackage
215           : HLoadClass::LoadKind::kBssEntryPublic;
216     }
217   } else {
218     Runtime* runtime = Runtime::Current();
219     if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
220       // Compiling boot image or boot image extension. Check if the class is a boot image class.
221       DCHECK(!compiler_options.IsJitCompiler());
222       if (!compiler_options.GetCompilePic()) {
223         // Test configuration, do not sharpen.
224         desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
225       } else if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get())) {
226         DCHECK(compiler_options.IsBootImageExtension());
227         is_in_boot_image = true;
228         desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
229       } else if ((klass != nullptr) &&
230                  compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
231         is_in_boot_image = true;
232         desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
233       } else {
234         // Not a boot image class.
235         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
236       }
237     } else {
238       is_in_boot_image = (klass != nullptr) &&
239           runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
240       if (compiler_options.IsJitCompiler()) {
241         DCHECK(!compiler_options.GetCompilePic());
242         if (is_in_boot_image) {
243           desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
244         } else if (klass != nullptr) {
245           if (runtime->GetJit()->CanEncodeClass(
246                   klass.Get(),
247                   compiler_options.IsJitCompilerForSharedCode())) {
248             desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
249           } else {
250             // Shared JIT code cannot encode a literal that the GC can move.
251             VLOG(jit) << "Unable to encode in shared region class literal: "
252                       << klass->PrettyClass();
253             desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
254           }
255         } else {
256           // Class not loaded yet. This happens when the dex code requesting
257           // this `HLoadClass` hasn't been executed in the interpreter.
258           // Fallback to the dex cache.
259           // TODO(ngeoffray): Generate HDeoptimize instead.
260           desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
261         }
262       } else if (is_in_boot_image) {
263         // AOT app compilation, boot image class.
264         desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
265       } else {
266         // Not JIT and the klass is not in boot image.
267         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
268       }
269     }
270   }
271   DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
272 
273   if (is_in_boot_image) {
274     load_class->MarkInBootImage();
275   }
276   HLoadClass::LoadKind load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
277 
278   if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
279     if (load_kind == HLoadClass::LoadKind::kRuntimeCall ||
280         load_kind == HLoadClass::LoadKind::kBssEntry ||
281         load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
282         load_kind == HLoadClass::LoadKind::kBssEntryPackage) {
283       // We actually cannot reference this class, we're forced to bail.
284       // We cannot reference this class with Bss, as the entrypoint will lookup the class
285       // in the caller's dex file, but that dex file does not reference the class.
286       return HLoadClass::LoadKind::kInvalid;
287     }
288   }
289   return load_kind;
290 }
291 
CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass,CodeGenerator * codegen)292 static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
293     REQUIRES_SHARED(Locks::mutator_lock_) {
294   DCHECK(!klass->IsProxyClass());
295   DCHECK(!klass->IsArrayClass());
296 
297   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
298   if (compiler_options.IsJitCompiler()) {
299     // If we're JITting, try to assign a type check bitstring (fall through).
300   } else if (codegen->GetCompilerOptions().IsBootImage()) {
301     const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
302     if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
303       return false;
304     }
305     // If the target is a boot image class, try to assign a type check bitstring (fall through).
306     // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
307   } else {
308     // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
309     // already assigned in the boot image.
310     return false;
311   }
312 
313   // Try to assign a type check bitstring.
314   MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
315   if ((false) &&  // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
316                   // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
317       kIsDebugBuild &&
318       compiler_options.IsBootImage() &&
319       compiler_options.IsForceDeterminism()) {
320     SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
321     CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
322         << klass->PrettyDescriptor() << "/" << old_state
323         << " in " << codegen->GetGraph()->PrettyMethod();
324   }
325   SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
326   return state == SubtypeCheckInfo::kAssigned;
327 }
328 
ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,CodeGenerator * codegen,bool needs_access_check)329 TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
330                                                 CodeGenerator* codegen,
331                                                 bool needs_access_check) {
332   if (klass == nullptr) {
333     return TypeCheckKind::kUnresolvedCheck;
334   } else if (klass->IsInterface()) {
335     return TypeCheckKind::kInterfaceCheck;
336   } else if (klass->IsArrayClass()) {
337     if (klass->GetComponentType()->IsObjectClass()) {
338       return TypeCheckKind::kArrayObjectCheck;
339     } else if (klass->CannotBeAssignedFromOtherTypes()) {
340       return TypeCheckKind::kExactCheck;
341     } else {
342       return TypeCheckKind::kArrayCheck;
343     }
344   } else if (klass->IsFinal()) {  // TODO: Consider using bitstring for final classes.
345     return TypeCheckKind::kExactCheck;
346   } else if (kBitstringSubtypeCheckEnabled &&
347              !needs_access_check &&
348              CanUseTypeCheckBitstring(klass, codegen)) {
349     // TODO: We should not need the `!needs_access_check` check but getting rid of that
350     // requires rewriting some optimizations in instruction simplifier.
351     return TypeCheckKind::kBitstringCheck;
352   } else if (klass->IsAbstract()) {
353     return TypeCheckKind::kAbstractClassCheck;
354   } else {
355     return TypeCheckKind::kClassHierarchyCheck;
356   }
357 }
358 
ProcessLoadString(HLoadString * load_string,CodeGenerator * codegen,const DexCompilationUnit & dex_compilation_unit,VariableSizedHandleScope * handles)359 void HSharpening::ProcessLoadString(
360     HLoadString* load_string,
361     CodeGenerator* codegen,
362     const DexCompilationUnit& dex_compilation_unit,
363     VariableSizedHandleScope* handles) {
364   DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
365 
366   const DexFile& dex_file = load_string->GetDexFile();
367   dex::StringIndex string_index = load_string->GetStringIndex();
368 
369   HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
370   {
371     Runtime* runtime = Runtime::Current();
372     ClassLinker* class_linker = runtime->GetClassLinker();
373     ScopedObjectAccess soa(Thread::Current());
374     StackHandleScope<1> hs(soa.Self());
375     Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
376         ? dex_compilation_unit.GetDexCache()
377         : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
378     ObjPtr<mirror::String> string = nullptr;
379 
380     const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
381     if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
382       // Compiling boot image or boot image extension. Resolve the string and allocate it
383       // if needed, to ensure the string will be added to the boot image.
384       DCHECK(!compiler_options.IsJitCompiler());
385       if (compiler_options.GetCompilePic()) {
386         if (compiler_options.IsForceDeterminism()) {
387           // Strings for methods we're compiling should be pre-resolved but Strings in inlined
388           // methods may not be if these inlined methods are not in the boot image profile.
389           // Multiple threads allocating new Strings can cause non-deterministic boot image
390           // because of the image relying on the order of GC roots we walk. (We could fix that
391           // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
392           // strings even if that results in omitting them from the boot image and using the
393           // sub-optimal load kind kBssEntry.
394           string = class_linker->LookupString(string_index, dex_cache.Get());
395         } else {
396           string = class_linker->ResolveString(string_index, dex_cache);
397           CHECK(string != nullptr);
398         }
399         if (string != nullptr) {
400           if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
401             DCHECK(compiler_options.IsBootImageExtension());
402             desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
403           } else {
404             desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
405           }
406         } else {
407           desired_load_kind = HLoadString::LoadKind::kBssEntry;
408         }
409       } else {
410         // Test configuration, do not sharpen.
411         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
412       }
413     } else if (compiler_options.IsJitCompiler()) {
414       DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
415       string = class_linker->LookupString(string_index, dex_cache.Get());
416       if (string != nullptr) {
417         gc::Heap* heap = runtime->GetHeap();
418         if (heap->ObjectIsInBootImageSpace(string)) {
419           desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
420         } else if (runtime->GetJit()->CanEncodeString(
421                   string,
422                   compiler_options.IsJitCompilerForSharedCode())) {
423           desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
424         } else {
425           // Shared JIT code cannot encode a literal that the GC can move.
426           VLOG(jit) << "Unable to encode in shared region string literal: "
427                     << string->ToModifiedUtf8();
428           desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
429         }
430       } else {
431         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
432       }
433     } else {
434       // AOT app compilation. Try to lookup the string without allocating if not found.
435       string = class_linker->LookupString(string_index, dex_cache.Get());
436       if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
437         desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
438       } else {
439         desired_load_kind = HLoadString::LoadKind::kBssEntry;
440       }
441     }
442     if (string != nullptr) {
443       load_string->SetString(handles->NewHandle(string));
444     }
445   }
446   DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
447 
448   HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
449   load_string->SetLoadKind(load_kind);
450 }
451 
452 }  // namespace art
453