• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "intrinsics.h"
18 
19 #include "art_field-inl.h"
20 #include "art_method-inl.h"
21 #include "base/utils.h"
22 #include "class_linker.h"
23 #include "class_root-inl.h"
24 #include "code_generator.h"
25 #include "dex/invoke_type.h"
26 #include "driver/compiler_options.h"
27 #include "gc/space/image_space.h"
28 #include "image-inl.h"
29 #include "intrinsic_objects.h"
30 #include "nodes.h"
31 #include "obj_ptr-inl.h"
32 #include "scoped_thread_state_change-inl.h"
33 #include "thread-current-inl.h"
34 
35 namespace art {
36 
operator <<(std::ostream & os,const Intrinsics & intrinsic)37 std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
38   switch (intrinsic) {
39     case Intrinsics::kNone:
40       os << "None";
41       break;
42 #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \
43     case Intrinsics::k ## Name: \
44       os << # Name; \
45       break;
46 #include "intrinsics_list.h"
47       INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
48 #undef STATIC_INTRINSICS_LIST
49 #undef VIRTUAL_INTRINSICS_LIST
50 #undef OPTIMIZING_INTRINSICS
51   }
52   return os;
53 }
54 
55 static const char kIntegerCacheDescriptor[] = "Ljava/lang/Integer$IntegerCache;";
56 static const char kIntegerDescriptor[] = "Ljava/lang/Integer;";
57 static const char kIntegerArrayDescriptor[] = "[Ljava/lang/Integer;";
58 static const char kLowFieldName[] = "low";
59 static const char kHighFieldName[] = "high";
60 static const char kValueFieldName[] = "value";
61 
GetBootImageLiveObjects()62 static ObjPtr<mirror::ObjectArray<mirror::Object>> GetBootImageLiveObjects()
63     REQUIRES_SHARED(Locks::mutator_lock_) {
64   gc::Heap* heap = Runtime::Current()->GetHeap();
65   const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
66   DCHECK(!boot_image_spaces.empty());
67   const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
68   ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
69       ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
70           main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
71   DCHECK(boot_image_live_objects != nullptr);
72   DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
73   return boot_image_live_objects;
74 }
75 
LookupInitializedClass(Thread * self,ClassLinker * class_linker,const char * descriptor)76 static ObjPtr<mirror::Class> LookupInitializedClass(Thread* self,
77                                                     ClassLinker* class_linker,
78                                                     const char* descriptor)
79         REQUIRES_SHARED(Locks::mutator_lock_) {
80   ObjPtr<mirror::Class> klass =
81       class_linker->LookupClass(self, descriptor, /* class_loader= */ nullptr);
82   DCHECK(klass != nullptr);
83   DCHECK(klass->IsInitialized());
84   return klass;
85 }
86 
GetIntegerCacheArray(ObjPtr<mirror::Class> cache_class)87 static ObjPtr<mirror::ObjectArray<mirror::Object>> GetIntegerCacheArray(
88     ObjPtr<mirror::Class> cache_class) REQUIRES_SHARED(Locks::mutator_lock_) {
89   ArtField* cache_field = cache_class->FindDeclaredStaticField("cache", kIntegerArrayDescriptor);
90   DCHECK(cache_field != nullptr);
91   return ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(cache_field->GetObject(cache_class));
92 }
93 
GetIntegerCacheField(ObjPtr<mirror::Class> cache_class,const char * field_name)94 static int32_t GetIntegerCacheField(ObjPtr<mirror::Class> cache_class, const char* field_name)
95     REQUIRES_SHARED(Locks::mutator_lock_) {
96   ArtField* field = cache_class->FindDeclaredStaticField(field_name, "I");
97   DCHECK(field != nullptr);
98   return field->GetInt(cache_class);
99 }
100 
CheckIntegerCache(Thread * self,ClassLinker * class_linker,ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_cache)101 static bool CheckIntegerCache(Thread* self,
102                               ClassLinker* class_linker,
103                               ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,
104                               ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_cache)
105     REQUIRES_SHARED(Locks::mutator_lock_) {
106   DCHECK(boot_image_cache != nullptr);
107 
108   // Since we have a cache in the boot image, both java.lang.Integer and
109   // java.lang.Integer$IntegerCache must be initialized in the boot image.
110   ObjPtr<mirror::Class> cache_class =
111       LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor);
112   ObjPtr<mirror::Class> integer_class =
113       LookupInitializedClass(self, class_linker, kIntegerDescriptor);
114 
115   // Check that the current cache is the same as the `boot_image_cache`.
116   ObjPtr<mirror::ObjectArray<mirror::Object>> current_cache = GetIntegerCacheArray(cache_class);
117   if (current_cache != boot_image_cache) {
118     return false;  // Messed up IntegerCache.cache.
119   }
120 
121   // Check that the range matches the boot image cache length.
122   int32_t low = GetIntegerCacheField(cache_class, kLowFieldName);
123   int32_t high = GetIntegerCacheField(cache_class, kHighFieldName);
124   if (boot_image_cache->GetLength() != high - low + 1) {
125     return false;  // Messed up IntegerCache.low or IntegerCache.high.
126   }
127 
128   // Check that the elements match the boot image intrinsic objects and check their values as well.
129   ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I");
130   DCHECK(value_field != nullptr);
131   for (int32_t i = 0, len = boot_image_cache->GetLength(); i != len; ++i) {
132     ObjPtr<mirror::Object> boot_image_object =
133         IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, i);
134     DCHECK(Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boot_image_object));
135     // No need for read barrier for comparison with a boot image object.
136     ObjPtr<mirror::Object> current_object =
137         boot_image_cache->GetWithoutChecks<kVerifyNone, kWithoutReadBarrier>(i);
138     if (boot_image_object != current_object) {
139       return false;  // Messed up IntegerCache.cache[i]
140     }
141     if (value_field->GetInt(boot_image_object) != low + i) {
142       return false;  // Messed up IntegerCache.cache[i].value.
143     }
144   }
145 
146   return true;
147 }
148 
CanReferenceBootImageObjects(HInvoke * invoke,const CompilerOptions & compiler_options)149 static bool CanReferenceBootImageObjects(HInvoke* invoke, const CompilerOptions& compiler_options) {
150   // Piggyback on the method load kind to determine whether we can use PC-relative addressing
151   // for AOT. This should cover both the testing config (non-PIC boot image) and codegens that
152   // reject PC-relative load kinds and fall back to the runtime call.
153   if (compiler_options.IsAotCompiler() &&
154       !invoke->AsInvokeStaticOrDirect()->HasPcRelativeMethodLoadKind()) {
155     return false;
156   }
157   if (!compiler_options.IsBootImage() &&
158       Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()) {
159     DCHECK(compiler_options.IsJitCompiler());
160     return false;  // Running without boot image, cannot use required boot image objects.
161   }
162   return true;
163 }
164 
ComputeIntegerValueOfLocations(HInvoke * invoke,CodeGenerator * codegen,Location return_location,Location first_argument_location)165 void IntrinsicVisitor::ComputeIntegerValueOfLocations(HInvoke* invoke,
166                                                       CodeGenerator* codegen,
167                                                       Location return_location,
168                                                       Location first_argument_location) {
169   // The intrinsic will call if it needs to allocate a j.l.Integer.
170   LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
171   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
172   if (!CanReferenceBootImageObjects(invoke, compiler_options)) {
173     return;
174   }
175   if (compiler_options.IsBootImage()) {
176     if (!compiler_options.IsImageClass(kIntegerCacheDescriptor) ||
177         !compiler_options.IsImageClass(kIntegerDescriptor)) {
178       return;
179     }
180     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
181     Thread* self = Thread::Current();
182     ScopedObjectAccess soa(self);
183     ObjPtr<mirror::Class> cache_class = class_linker->LookupClass(
184         self, kIntegerCacheDescriptor, /* class_loader= */ nullptr);
185     DCHECK(cache_class != nullptr);
186     if (UNLIKELY(!cache_class->IsInitialized())) {
187       LOG(WARNING) << "Image class " << cache_class->PrettyDescriptor() << " is uninitialized.";
188       return;
189     }
190     ObjPtr<mirror::Class> integer_class =
191         class_linker->LookupClass(self, kIntegerDescriptor, /* class_loader= */ nullptr);
192     DCHECK(integer_class != nullptr);
193     if (UNLIKELY(!integer_class->IsInitialized())) {
194       LOG(WARNING) << "Image class " << integer_class->PrettyDescriptor() << " is uninitialized.";
195       return;
196     }
197     int32_t low = GetIntegerCacheField(cache_class, kLowFieldName);
198     int32_t high = GetIntegerCacheField(cache_class, kHighFieldName);
199     if (kIsDebugBuild) {
200       ObjPtr<mirror::ObjectArray<mirror::Object>> current_cache = GetIntegerCacheArray(cache_class);
201       CHECK(current_cache != nullptr);
202       CHECK_EQ(current_cache->GetLength(), high - low + 1);
203       ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I");
204       CHECK(value_field != nullptr);
205       for (int32_t i = 0, len = current_cache->GetLength(); i != len; ++i) {
206         ObjPtr<mirror::Object> current_object = current_cache->GetWithoutChecks(i);
207         CHECK(current_object != nullptr);
208         CHECK_EQ(value_field->GetInt(current_object), low + i);
209       }
210     }
211     if (invoke->InputAt(0)->IsIntConstant()) {
212       int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
213       if (static_cast<uint32_t>(value) - static_cast<uint32_t>(low) <
214           static_cast<uint32_t>(high - low + 1)) {
215         // No call, we shall use direct pointer to the Integer object.
216         call_kind = LocationSummary::kNoCall;
217       }
218     }
219   } else {
220     Runtime* runtime = Runtime::Current();
221     Thread* self = Thread::Current();
222     ScopedObjectAccess soa(self);
223     ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = GetBootImageLiveObjects();
224     ObjPtr<mirror::ObjectArray<mirror::Object>> cache =
225         IntrinsicObjects::GetIntegerValueOfCache(boot_image_live_objects);
226     if (cache == nullptr) {
227       return;  // No cache in the boot image.
228     }
229     if (compiler_options.IsJitCompiler()) {
230       if (!CheckIntegerCache(self, runtime->GetClassLinker(), boot_image_live_objects, cache)) {
231         return;  // The cache was somehow messed up, probably by using reflection.
232       }
233     } else {
234       DCHECK(compiler_options.IsAotCompiler());
235       DCHECK(CheckIntegerCache(self, runtime->GetClassLinker(), boot_image_live_objects, cache));
236       if (invoke->InputAt(0)->IsIntConstant()) {
237         int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
238         // Retrieve the `value` from the lowest cached Integer.
239         ObjPtr<mirror::Object> low_integer =
240             IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, 0u);
241         ObjPtr<mirror::Class> integer_class =
242             low_integer->GetClass<kVerifyNone, kWithoutReadBarrier>();
243         ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I");
244         DCHECK(value_field != nullptr);
245         int32_t low = value_field->GetInt(low_integer);
246         if (static_cast<uint32_t>(value) - static_cast<uint32_t>(low) <
247             static_cast<uint32_t>(cache->GetLength())) {
248           // No call, we shall use direct pointer to the Integer object. Note that we cannot
249           // do this for JIT as the "low" can change through reflection before emitting the code.
250           call_kind = LocationSummary::kNoCall;
251         }
252       }
253     }
254   }
255 
256   ArenaAllocator* allocator = codegen->GetGraph()->GetAllocator();
257   LocationSummary* locations = new (allocator) LocationSummary(invoke, call_kind, kIntrinsified);
258   if (call_kind == LocationSummary::kCallOnMainOnly) {
259     locations->SetInAt(0, Location::RegisterOrConstant(invoke->InputAt(0)));
260     locations->AddTemp(first_argument_location);
261     locations->SetOut(return_location);
262   } else {
263     locations->SetInAt(0, Location::ConstantLocation(invoke->InputAt(0)->AsConstant()));
264     locations->SetOut(Location::RequiresRegister());
265   }
266 }
267 
GetIntegerCacheLowFromIntegerCache(Thread * self,ClassLinker * class_linker)268 static int32_t GetIntegerCacheLowFromIntegerCache(Thread* self, ClassLinker* class_linker)
269     REQUIRES_SHARED(Locks::mutator_lock_) {
270   ObjPtr<mirror::Class> cache_class =
271       LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor);
272   return GetIntegerCacheField(cache_class, kLowFieldName);
273 }
274 
IntegerValueOfInfo()275 inline IntrinsicVisitor::IntegerValueOfInfo::IntegerValueOfInfo()
276     : value_offset(0),
277       low(0),
278       length(0u),
279       value_boot_image_reference(kInvalidReference) {}
280 
ComputeIntegerValueOfInfo(HInvoke * invoke,const CompilerOptions & compiler_options)281 IntrinsicVisitor::IntegerValueOfInfo IntrinsicVisitor::ComputeIntegerValueOfInfo(
282     HInvoke* invoke, const CompilerOptions& compiler_options) {
283   // Note that we could cache all of the data looked up here. but there's no good
284   // location for it. We don't want to add it to WellKnownClasses, to avoid creating global
285   // jni values. Adding it as state to the compiler singleton seems like wrong
286   // separation of concerns.
287   // The need for this data should be pretty rare though.
288 
289   // Note that at this point we can no longer abort the code generation. Therefore,
290   // we need to provide data that shall not lead to a crash even if the fields were
291   // modified through reflection since ComputeIntegerValueOfLocations() when JITting.
292 
293   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
294   Thread* self = Thread::Current();
295   ScopedObjectAccess soa(self);
296 
297   IntegerValueOfInfo info;
298   if (compiler_options.IsBootImage()) {
299     ObjPtr<mirror::Class> integer_class = invoke->GetResolvedMethod()->GetDeclaringClass();
300     DCHECK(integer_class->DescriptorEquals(kIntegerDescriptor));
301     ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I");
302     DCHECK(value_field != nullptr);
303     info.value_offset = value_field->GetOffset().Uint32Value();
304     ObjPtr<mirror::Class> cache_class =
305         LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor);
306     info.low = GetIntegerCacheField(cache_class, kLowFieldName);
307     int32_t high = GetIntegerCacheField(cache_class, kHighFieldName);
308     info.length = dchecked_integral_cast<uint32_t>(high - info.low + 1);
309 
310     if (invoke->InputAt(0)->IsIntConstant()) {
311       int32_t input_value = invoke->InputAt(0)->AsIntConstant()->GetValue();
312       uint32_t index = static_cast<uint32_t>(input_value) - static_cast<uint32_t>(info.low);
313       if (index < static_cast<uint32_t>(info.length)) {
314         info.value_boot_image_reference = IntrinsicObjects::EncodePatch(
315             IntrinsicObjects::PatchType::kIntegerValueOfObject, index);
316       } else {
317         // Not in the cache.
318         info.value_boot_image_reference = IntegerValueOfInfo::kInvalidReference;
319       }
320     } else {
321       info.array_data_boot_image_reference =
322           IntrinsicObjects::EncodePatch(IntrinsicObjects::PatchType::kIntegerValueOfArray);
323     }
324   } else {
325     ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = GetBootImageLiveObjects();
326     ObjPtr<mirror::Object> low_integer =
327         IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, 0u);
328     ObjPtr<mirror::Class> integer_class = low_integer->GetClass<kVerifyNone, kWithoutReadBarrier>();
329     ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I");
330     DCHECK(value_field != nullptr);
331     info.value_offset = value_field->GetOffset().Uint32Value();
332     if (compiler_options.IsJitCompiler()) {
333       // Use the current `IntegerCache.low` for JIT to avoid truly surprising behavior if the
334       // code messes up the `value` field in the lowest cached Integer using reflection.
335       info.low = GetIntegerCacheLowFromIntegerCache(self, class_linker);
336     } else {
337       // For app AOT, the `low_integer->value` should be the same as `IntegerCache.low`.
338       info.low = value_field->GetInt(low_integer);
339       DCHECK_EQ(info.low, GetIntegerCacheLowFromIntegerCache(self, class_linker));
340     }
341     // Do not look at `IntegerCache.high`, use the immutable length of the cache array instead.
342     info.length = dchecked_integral_cast<uint32_t>(
343         IntrinsicObjects::GetIntegerValueOfCache(boot_image_live_objects)->GetLength());
344 
345     if (invoke->InputAt(0)->IsIntConstant()) {
346       int32_t input_value = invoke->InputAt(0)->AsIntConstant()->GetValue();
347       uint32_t index = static_cast<uint32_t>(input_value) - static_cast<uint32_t>(info.low);
348       if (index < static_cast<uint32_t>(info.length)) {
349         ObjPtr<mirror::Object> integer =
350             IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, index);
351         info.value_boot_image_reference = CodeGenerator::GetBootImageOffset(integer);
352       } else {
353         // Not in the cache.
354         info.value_boot_image_reference = IntegerValueOfInfo::kInvalidReference;
355       }
356     } else {
357       info.array_data_boot_image_reference =
358           CodeGenerator::GetBootImageOffset(boot_image_live_objects) +
359           IntrinsicObjects::GetIntegerValueOfArrayDataOffset(boot_image_live_objects).Uint32Value();
360     }
361   }
362 
363   return info;
364 }
365 
GetReferenceDisableIntrinsicOffset()366 MemberOffset IntrinsicVisitor::GetReferenceDisableIntrinsicOffset() {
367   ScopedObjectAccess soa(Thread::Current());
368   // The "disableIntrinsic" is the first static field.
369   ArtField* field = GetClassRoot<mirror::Reference>()->GetStaticField(0);
370   DCHECK_STREQ(field->GetName(), "disableIntrinsic");
371   return field->GetOffset();
372 }
373 
GetReferenceSlowPathEnabledOffset()374 MemberOffset IntrinsicVisitor::GetReferenceSlowPathEnabledOffset() {
375   ScopedObjectAccess soa(Thread::Current());
376   // The "slowPathEnabled" is the second static field.
377   ArtField* field = GetClassRoot<mirror::Reference>()->GetStaticField(1);
378   DCHECK_STREQ(field->GetName(), "slowPathEnabled");
379   return field->GetOffset();
380 }
381 
CreateReferenceGetReferentLocations(HInvoke * invoke,CodeGenerator * codegen)382 void IntrinsicVisitor::CreateReferenceGetReferentLocations(HInvoke* invoke,
383                                                            CodeGenerator* codegen) {
384   if (!CanReferenceBootImageObjects(invoke, codegen->GetCompilerOptions())) {
385     return;
386   }
387 
388   ArenaAllocator* allocator = codegen->GetGraph()->GetAllocator();
389   LocationSummary* locations =
390       new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
391   locations->SetInAt(0, Location::RequiresRegister());
392   locations->SetOut(Location::RequiresRegister());
393 }
394 
CreateReferenceRefersToLocations(HInvoke * invoke)395 void IntrinsicVisitor::CreateReferenceRefersToLocations(HInvoke* invoke) {
396   if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
397     // Unimplemented for non-Baker read barrier.
398     return;
399   }
400 
401   ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
402   LocationSummary* locations =
403       new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
404   locations->SetInAt(0, Location::RequiresRegister());
405   locations->SetInAt(1, Location::RequiresRegister());
406   locations->SetOut(Location::RequiresRegister());
407 }
408 
AssertNonMovableStringClass()409 void IntrinsicVisitor::AssertNonMovableStringClass() {
410   if (kIsDebugBuild) {
411     ScopedObjectAccess soa(Thread::Current());
412     ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>();
413     CHECK(!art::Runtime::Current()->GetHeap()->IsMovableObject(string_class));
414   }
415 }
416 
417 }  // namespace art
418