Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 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 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 20 | #include "art_method-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 21 | #include "base/utils.h" |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 22 | #include "class_linker.h" |
Roland Levillain | 1d775d2 | 2018-09-07 13:56:57 +0100 | [diff] [blame^] | 23 | #include "class_root.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 24 | #include "dex/invoke_type.h" |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 25 | #include "driver/compiler_options.h" |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 26 | #include "gc/space/image_space.h" |
| 27 | #include "image-inl.h" |
| 28 | #include "intrinsic_objects.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 29 | #include "nodes.h" |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 30 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 32 | #include "thread-current-inl.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 36 | // Check that intrinsic enum values fit within space set aside in ArtMethod modifier flags. |
| 37 | #define CHECK_INTRINSICS_ENUM_VALUES(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
| 38 | static_assert( \ |
| 39 | static_cast<uint32_t>(Intrinsics::k ## Name) <= (kAccIntrinsicBits >> CTZ(kAccIntrinsicBits)), \ |
Orion Hodson | 4a4610a | 2017-09-28 16:57:55 +0100 | [diff] [blame] | 40 | "Instrinsics enumeration space overflow."); |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 41 | #include "intrinsics_list.h" |
| 42 | INTRINSICS_LIST(CHECK_INTRINSICS_ENUM_VALUES) |
| 43 | #undef INTRINSICS_LIST |
| 44 | #undef CHECK_INTRINSICS_ENUM_VALUES |
| 45 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 46 | // Function that returns whether an intrinsic is static/direct or virtual. |
| 47 | static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) { |
| 48 | switch (i) { |
| 49 | case Intrinsics::kNone: |
| 50 | return kInterface; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 51 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 52 | case Intrinsics::k ## Name: \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 53 | return IsStatic; |
| 54 | #include "intrinsics_list.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 55 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 56 | #undef INTRINSICS_LIST |
| 57 | #undef OPTIMIZING_INTRINSICS |
| 58 | } |
| 59 | return kInterface; |
| 60 | } |
| 61 | |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 62 | // Function that returns whether an intrinsic needs an environment or not. |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 63 | static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) { |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 64 | switch (i) { |
| 65 | case Intrinsics::kNone: |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 66 | return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic. |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 67 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 68 | case Intrinsics::k ## Name: \ |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 69 | return NeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 70 | #include "intrinsics_list.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 71 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 72 | #undef INTRINSICS_LIST |
| 73 | #undef OPTIMIZING_INTRINSICS |
| 74 | } |
Agi Csaki | 05f2056 | 2015-08-19 14:58:14 -0700 | [diff] [blame] | 75 | return kNeedsEnvironmentOrCache; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 76 | } |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 77 | |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 78 | // Function that returns whether an intrinsic has side effects. |
| 79 | static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) { |
| 80 | switch (i) { |
| 81 | case Intrinsics::kNone: |
| 82 | return kAllSideEffects; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 83 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 84 | case Intrinsics::k ## Name: \ |
| 85 | return SideEffects; |
| 86 | #include "intrinsics_list.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 87 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 88 | #undef INTRINSICS_LIST |
| 89 | #undef OPTIMIZING_INTRINSICS |
| 90 | } |
| 91 | return kAllSideEffects; |
| 92 | } |
| 93 | |
| 94 | // Function that returns whether an intrinsic can throw exceptions. |
| 95 | static inline IntrinsicExceptions GetExceptions(Intrinsics i) { |
| 96 | switch (i) { |
| 97 | case Intrinsics::kNone: |
| 98 | return kCanThrow; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 99 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 100 | case Intrinsics::k ## Name: \ |
| 101 | return Exceptions; |
| 102 | #include "intrinsics_list.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 103 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 104 | #undef INTRINSICS_LIST |
| 105 | #undef OPTIMIZING_INTRINSICS |
| 106 | } |
| 107 | return kCanThrow; |
| 108 | } |
| 109 | |
Orion Hodson | 4c71d00 | 2017-11-29 11:03:25 +0000 | [diff] [blame] | 110 | static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) |
| 111 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 112 | // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual. |
| 113 | // |
| 114 | // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization |
| 115 | // failure occured. We might be in a situation where we have inlined a method that calls an |
| 116 | // intrinsic, but that method is in a different dex file on which we do not have a |
| 117 | // verified_method that would have helped the compiler driver sharpen the call. In that case, |
| 118 | // make sure that the intrinsic is actually for some final method (or in a final class), as |
| 119 | // otherwise the intrinsics setup is broken. |
| 120 | // |
| 121 | // For the last direction, we have intrinsics for virtual functions that will perform a check |
| 122 | // inline. If the precise type is known, however, the instruction will be sharpened to an |
| 123 | // InvokeStaticOrDirect. |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 124 | InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 125 | InvokeType invoke_type = invoke->GetInvokeType(); |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 126 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 127 | switch (intrinsic_type) { |
| 128 | case kStatic: |
| 129 | return (invoke_type == kStatic); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 130 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 131 | case kDirect: |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 132 | if (invoke_type == kDirect) { |
| 133 | return true; |
| 134 | } |
| 135 | if (invoke_type == kVirtual) { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 136 | ArtMethod* art_method = invoke->GetResolvedMethod(); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 137 | return (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal()); |
Andreas Gampe | bfb5ba9 | 2015-09-01 15:45:02 +0000 | [diff] [blame] | 138 | } |
| 139 | return false; |
| 140 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 141 | case kVirtual: |
| 142 | // Call might be devirtualized. |
Mingyao Yang | 6b1aebe | 2017-11-27 15:39:04 -0800 | [diff] [blame] | 143 | return (invoke_type == kVirtual || invoke_type == kDirect || invoke_type == kInterface); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 144 | |
Orion Hodson | b1b5206 | 2017-11-27 11:51:42 +0000 | [diff] [blame] | 145 | case kSuper: |
| 146 | case kInterface: |
| 147 | case kPolymorphic: |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 148 | case kCustom: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 149 | return false; |
| 150 | } |
Orion Hodson | b1b5206 | 2017-11-27 11:51:42 +0000 | [diff] [blame] | 151 | LOG(FATAL) << "Unknown intrinsic invoke type: " << intrinsic_type; |
| 152 | UNREACHABLE(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Mingyao Yang | 6b1aebe | 2017-11-27 15:39:04 -0800 | [diff] [blame] | 155 | bool IntrinsicsRecognizer::Recognize(HInvoke* invoke, |
| 156 | ArtMethod* art_method, |
| 157 | /*out*/ bool* wrong_invoke_type) { |
| 158 | if (art_method == nullptr) { |
| 159 | art_method = invoke->GetResolvedMethod(); |
| 160 | } |
Orion Hodson | b1b5206 | 2017-11-27 11:51:42 +0000 | [diff] [blame] | 161 | *wrong_invoke_type = false; |
| 162 | if (art_method == nullptr || !art_method->IsIntrinsic()) { |
| 163 | return false; |
| 164 | } |
| 165 | |
Orion Hodson | 4c71d00 | 2017-11-29 11:03:25 +0000 | [diff] [blame] | 166 | // TODO: b/65872996 The intent is that polymorphic signature methods should |
| 167 | // be compiler intrinsics. At present, they are only interpreter intrinsics. |
| 168 | if (art_method->IsPolymorphicSignature()) { |
| 169 | return false; |
Aart Bik | f0010dd | 2017-11-21 16:31:53 -0800 | [diff] [blame] | 170 | } |
Orion Hodson | b1b5206 | 2017-11-27 11:51:42 +0000 | [diff] [blame] | 171 | |
| 172 | Intrinsics intrinsic = static_cast<Intrinsics>(art_method->GetIntrinsic()); |
| 173 | if (CheckInvokeType(intrinsic, invoke) == false) { |
| 174 | *wrong_invoke_type = true; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | invoke->SetIntrinsic(intrinsic, |
| 179 | NeedsEnvironmentOrCache(intrinsic), |
| 180 | GetSideEffects(intrinsic), |
| 181 | GetExceptions(intrinsic)); |
| 182 | return true; |
Aart Bik | f0010dd | 2017-11-21 16:31:53 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 185 | bool IntrinsicsRecognizer::Run() { |
| 186 | bool didRecognize = false; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 187 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 188 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 189 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 190 | inst_it.Advance()) { |
| 191 | HInstruction* inst = inst_it.Current(); |
| 192 | if (inst->IsInvoke()) { |
Aart Bik | f0010dd | 2017-11-21 16:31:53 -0800 | [diff] [blame] | 193 | bool wrong_invoke_type = false; |
Mingyao Yang | 6b1aebe | 2017-11-27 15:39:04 -0800 | [diff] [blame] | 194 | if (Recognize(inst->AsInvoke(), /* art_method */ nullptr, &wrong_invoke_type)) { |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 195 | didRecognize = true; |
Aart Bik | f0010dd | 2017-11-21 16:31:53 -0800 | [diff] [blame] | 196 | MaybeRecordStat(stats_, MethodCompilationStat::kIntrinsicRecognized); |
| 197 | } else if (wrong_invoke_type) { |
| 198 | LOG(WARNING) |
| 199 | << "Found an intrinsic with unexpected invoke type: " |
| 200 | << inst->AsInvoke()->GetResolvedMethod()->PrettyMethod() << " " |
| 201 | << inst->DebugName(); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 206 | return didRecognize; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) { |
| 210 | switch (intrinsic) { |
| 211 | case Intrinsics::kNone: |
David Brazdil | 109c89a | 2015-07-31 17:10:43 +0100 | [diff] [blame] | 212 | os << "None"; |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 213 | break; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 214 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions, ...) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 215 | case Intrinsics::k ## Name: \ |
| 216 | os << # Name; \ |
| 217 | break; |
| 218 | #include "intrinsics_list.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 219 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 220 | #undef STATIC_INTRINSICS_LIST |
| 221 | #undef VIRTUAL_INTRINSICS_LIST |
| 222 | #undef OPTIMIZING_INTRINSICS |
| 223 | } |
| 224 | return os; |
| 225 | } |
| 226 | |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 227 | static const char kIntegerCacheDescriptor[] = "Ljava/lang/Integer$IntegerCache;"; |
| 228 | static const char kIntegerDescriptor[] = "Ljava/lang/Integer;"; |
| 229 | static const char kIntegerArrayDescriptor[] = "[Ljava/lang/Integer;"; |
| 230 | static const char kLowFieldName[] = "low"; |
| 231 | static const char kHighFieldName[] = "high"; |
| 232 | static const char kValueFieldName[] = "value"; |
| 233 | |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 234 | static ObjPtr<mirror::ObjectArray<mirror::Object>> GetBootImageLiveObjects() |
| 235 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 236 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 237 | const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces(); |
| 238 | DCHECK(!boot_image_spaces.empty()); |
| 239 | const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader(); |
| 240 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 241 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 242 | main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects)); |
| 243 | DCHECK(boot_image_live_objects != nullptr); |
| 244 | DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects)); |
| 245 | return boot_image_live_objects; |
| 246 | } |
| 247 | |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 248 | static ObjPtr<mirror::Class> LookupInitializedClass(Thread* self, |
| 249 | ClassLinker* class_linker, |
| 250 | const char* descriptor) |
| 251 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 252 | ObjPtr<mirror::Class> klass = |
| 253 | class_linker->LookupClass(self, descriptor, /* class_loader */ nullptr); |
| 254 | DCHECK(klass != nullptr); |
| 255 | DCHECK(klass->IsInitialized()); |
| 256 | return klass; |
| 257 | } |
| 258 | |
| 259 | static ObjPtr<mirror::ObjectArray<mirror::Object>> GetIntegerCacheArray( |
| 260 | ObjPtr<mirror::Class> cache_class) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 261 | ArtField* cache_field = cache_class->FindDeclaredStaticField("cache", kIntegerArrayDescriptor); |
| 262 | DCHECK(cache_field != nullptr); |
| 263 | return ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(cache_field->GetObject(cache_class)); |
| 264 | } |
| 265 | |
| 266 | static int32_t GetIntegerCacheField(ObjPtr<mirror::Class> cache_class, const char* field_name) |
| 267 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 268 | ArtField* field = cache_class->FindDeclaredStaticField(field_name, "I"); |
| 269 | DCHECK(field != nullptr); |
| 270 | return field->GetInt(cache_class); |
| 271 | } |
| 272 | |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 273 | static bool CheckIntegerCache(Thread* self, |
| 274 | ClassLinker* class_linker, |
| 275 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects, |
| 276 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_cache) |
| 277 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 278 | DCHECK(boot_image_cache != nullptr); |
| 279 | |
| 280 | // Since we have a cache in the boot image, both java.lang.Integer and |
| 281 | // java.lang.Integer$IntegerCache must be initialized in the boot image. |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 282 | ObjPtr<mirror::Class> cache_class = |
| 283 | LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 284 | ObjPtr<mirror::Class> integer_class = |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 285 | LookupInitializedClass(self, class_linker, kIntegerDescriptor); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 286 | |
| 287 | // Check that the current cache is the same as the `boot_image_cache`. |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 288 | ObjPtr<mirror::ObjectArray<mirror::Object>> current_cache = GetIntegerCacheArray(cache_class); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 289 | if (current_cache != boot_image_cache) { |
| 290 | return false; // Messed up IntegerCache.cache. |
| 291 | } |
| 292 | |
| 293 | // Check that the range matches the boot image cache length. |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 294 | int32_t low = GetIntegerCacheField(cache_class, kLowFieldName); |
| 295 | int32_t high = GetIntegerCacheField(cache_class, kHighFieldName); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 296 | if (boot_image_cache->GetLength() != high - low + 1) { |
| 297 | return false; // Messed up IntegerCache.low or IntegerCache.high. |
| 298 | } |
| 299 | |
| 300 | // Check that the elements match the boot image intrinsic objects and check their values as well. |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 301 | ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I"); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 302 | DCHECK(value_field != nullptr); |
| 303 | for (int32_t i = 0, len = boot_image_cache->GetLength(); i != len; ++i) { |
| 304 | ObjPtr<mirror::Object> boot_image_object = |
| 305 | IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, i); |
| 306 | DCHECK(Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boot_image_object)); |
| 307 | // No need for read barrier for comparison with a boot image object. |
| 308 | ObjPtr<mirror::Object> current_object = |
| 309 | boot_image_cache->GetWithoutChecks<kVerifyNone, kWithoutReadBarrier>(i); |
| 310 | if (boot_image_object != current_object) { |
| 311 | return false; // Messed up IntegerCache.cache[i] |
| 312 | } |
| 313 | if (value_field->GetInt(boot_image_object) != low + i) { |
| 314 | return false; // Messed up IntegerCache.cache[i].value. |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 321 | void IntrinsicVisitor::ComputeIntegerValueOfLocations(HInvoke* invoke, |
| 322 | CodeGenerator* codegen, |
| 323 | Location return_location, |
| 324 | Location first_argument_location) { |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 325 | // The intrinsic will call if it needs to allocate a j.l.Integer. |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 326 | LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly; |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 327 | const CompilerOptions& compiler_options = codegen->GetCompilerOptions(); |
| 328 | if (compiler_options.IsBootImage()) { |
| 329 | // Piggyback on the method load kind to determine whether we can use PC-relative addressing. |
| 330 | // This should cover both the testing config (non-PIC boot image) and codegens that reject |
| 331 | // PC-relative load kinds and fall back to the runtime call. |
| 332 | if (!invoke->AsInvokeStaticOrDirect()->HasPcRelativeMethodLoadKind()) { |
| 333 | return; |
| 334 | } |
| 335 | if (!compiler_options.IsImageClass(kIntegerCacheDescriptor) || |
| 336 | !compiler_options.IsImageClass(kIntegerDescriptor)) { |
| 337 | return; |
| 338 | } |
| 339 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 340 | Thread* self = Thread::Current(); |
| 341 | ScopedObjectAccess soa(self); |
| 342 | ObjPtr<mirror::Class> cache_class = class_linker->LookupClass( |
| 343 | self, kIntegerCacheDescriptor, /* class_loader */ nullptr); |
| 344 | DCHECK(cache_class != nullptr); |
| 345 | if (UNLIKELY(!cache_class->IsInitialized())) { |
| 346 | LOG(WARNING) << "Image class " << cache_class->PrettyDescriptor() << " is uninitialized."; |
| 347 | return; |
| 348 | } |
| 349 | ObjPtr<mirror::Class> integer_class = |
| 350 | class_linker->LookupClass(self, kIntegerDescriptor, /* class_loader */ nullptr); |
| 351 | DCHECK(integer_class != nullptr); |
| 352 | if (UNLIKELY(!integer_class->IsInitialized())) { |
| 353 | LOG(WARNING) << "Image class " << integer_class->PrettyDescriptor() << " is uninitialized."; |
| 354 | return; |
| 355 | } |
| 356 | int32_t low = GetIntegerCacheField(cache_class, kLowFieldName); |
| 357 | int32_t high = GetIntegerCacheField(cache_class, kHighFieldName); |
| 358 | if (kIsDebugBuild) { |
| 359 | ObjPtr<mirror::ObjectArray<mirror::Object>> current_cache = GetIntegerCacheArray(cache_class); |
| 360 | CHECK(current_cache != nullptr); |
| 361 | CHECK_EQ(current_cache->GetLength(), high - low + 1); |
| 362 | ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I"); |
| 363 | CHECK(value_field != nullptr); |
| 364 | for (int32_t i = 0, len = current_cache->GetLength(); i != len; ++i) { |
| 365 | ObjPtr<mirror::Object> current_object = current_cache->GetWithoutChecks(i); |
| 366 | CHECK(current_object != nullptr); |
| 367 | CHECK_EQ(value_field->GetInt(current_object), low + i); |
| 368 | } |
| 369 | } |
| 370 | if (invoke->InputAt(0)->IsIntConstant()) { |
| 371 | int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| 372 | if (static_cast<uint32_t>(value) - static_cast<uint32_t>(low) < |
| 373 | static_cast<uint32_t>(high - low + 1)) { |
| 374 | // No call, we shall use direct pointer to the Integer object. |
| 375 | call_kind = LocationSummary::kNoCall; |
| 376 | } |
| 377 | } |
| 378 | } else { |
| 379 | Runtime* runtime = Runtime::Current(); |
| 380 | if (runtime->GetHeap()->GetBootImageSpaces().empty()) { |
| 381 | return; // Running without boot image, cannot use required boot image objects. |
| 382 | } |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 383 | Thread* self = Thread::Current(); |
| 384 | ScopedObjectAccess soa(self); |
| 385 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = GetBootImageLiveObjects(); |
| 386 | ObjPtr<mirror::ObjectArray<mirror::Object>> cache = |
| 387 | IntrinsicObjects::GetIntegerValueOfCache(boot_image_live_objects); |
| 388 | if (cache == nullptr) { |
| 389 | return; // No cache in the boot image. |
| 390 | } |
| 391 | if (runtime->UseJitCompilation()) { |
| 392 | if (!CheckIntegerCache(self, runtime->GetClassLinker(), boot_image_live_objects, cache)) { |
| 393 | return; // The cache was somehow messed up, probably by using reflection. |
| 394 | } |
| 395 | } else { |
| 396 | DCHECK(runtime->IsAotCompiler()); |
| 397 | DCHECK(CheckIntegerCache(self, runtime->GetClassLinker(), boot_image_live_objects, cache)); |
| 398 | if (invoke->InputAt(0)->IsIntConstant()) { |
| 399 | int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| 400 | // Retrieve the `value` from the lowest cached Integer. |
| 401 | ObjPtr<mirror::Object> low_integer = |
| 402 | IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, 0u); |
| 403 | ObjPtr<mirror::Class> integer_class = |
| 404 | low_integer->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 405 | ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I"); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 406 | DCHECK(value_field != nullptr); |
| 407 | int32_t low = value_field->GetInt(low_integer); |
| 408 | if (static_cast<uint32_t>(value) - static_cast<uint32_t>(low) < |
| 409 | static_cast<uint32_t>(cache->GetLength())) { |
| 410 | // No call, we shall use direct pointer to the Integer object. Note that we cannot |
| 411 | // do this for JIT as the "low" can change through reflection before emitting the code. |
| 412 | call_kind = LocationSummary::kNoCall; |
| 413 | } |
| 414 | } |
| 415 | } |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 416 | } |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 417 | |
| 418 | ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator(); |
| 419 | LocationSummary* locations = new (allocator) LocationSummary(invoke, call_kind, kIntrinsified); |
| 420 | if (call_kind == LocationSummary::kCallOnMainOnly) { |
| 421 | locations->SetInAt(0, Location::RegisterOrConstant(invoke->InputAt(0))); |
| 422 | locations->AddTemp(first_argument_location); |
| 423 | locations->SetOut(return_location); |
| 424 | } else { |
| 425 | locations->SetInAt(0, Location::ConstantLocation(invoke->InputAt(0)->AsConstant())); |
| 426 | locations->SetOut(Location::RequiresRegister()); |
| 427 | } |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 430 | static int32_t GetIntegerCacheLowFromIntegerCache(Thread* self, ClassLinker* class_linker) |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 431 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 432 | ObjPtr<mirror::Class> cache_class = |
| 433 | LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor); |
| 434 | return GetIntegerCacheField(cache_class, kLowFieldName); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static uint32_t CalculateBootImageOffset(ObjPtr<mirror::Object> object) |
| 438 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 439 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 440 | DCHECK(heap->ObjectIsInBootImageSpace(object)); |
| 441 | return reinterpret_cast<const uint8_t*>(object.Ptr()) - heap->GetBootImageSpaces()[0]->Begin(); |
| 442 | } |
| 443 | |
| 444 | inline IntrinsicVisitor::IntegerValueOfInfo::IntegerValueOfInfo() |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 445 | : value_offset(0), |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 446 | low(0), |
| 447 | length(0u), |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 448 | integer_boot_image_offset(kInvalidReference), |
| 449 | value_boot_image_reference(kInvalidReference) {} |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 450 | |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 451 | IntrinsicVisitor::IntegerValueOfInfo IntrinsicVisitor::ComputeIntegerValueOfInfo( |
| 452 | HInvoke* invoke, const CompilerOptions& compiler_options) { |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 453 | // Note that we could cache all of the data looked up here. but there's no good |
| 454 | // location for it. We don't want to add it to WellKnownClasses, to avoid creating global |
| 455 | // jni values. Adding it as state to the compiler singleton seems like wrong |
| 456 | // separation of concerns. |
| 457 | // The need for this data should be pretty rare though. |
| 458 | |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 459 | // Note that at this point we can no longer abort the code generation. Therefore, |
| 460 | // we need to provide data that shall not lead to a crash even if the fields were |
| 461 | // modified through reflection since ComputeIntegerValueOfLocations() when JITting. |
| 462 | |
| 463 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 464 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 465 | Thread* self = Thread::Current(); |
| 466 | ScopedObjectAccess soa(self); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 467 | |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 468 | IntegerValueOfInfo info; |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 469 | if (compiler_options.IsBootImage()) { |
| 470 | ObjPtr<mirror::Class> integer_class = |
| 471 | LookupInitializedClass(self, class_linker, kIntegerDescriptor); |
| 472 | ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I"); |
| 473 | DCHECK(value_field != nullptr); |
| 474 | info.value_offset = value_field->GetOffset().Uint32Value(); |
| 475 | ObjPtr<mirror::Class> cache_class = |
| 476 | LookupInitializedClass(self, class_linker, kIntegerCacheDescriptor); |
| 477 | info.low = GetIntegerCacheField(cache_class, kLowFieldName); |
| 478 | int32_t high = GetIntegerCacheField(cache_class, kHighFieldName); |
| 479 | info.length = dchecked_integral_cast<uint32_t>(high - info.low + 1); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 480 | |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 481 | info.integer_boot_image_offset = IntegerValueOfInfo::kInvalidReference; |
| 482 | if (invoke->InputAt(0)->IsIntConstant()) { |
| 483 | int32_t input_value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| 484 | uint32_t index = static_cast<uint32_t>(input_value) - static_cast<uint32_t>(info.low); |
| 485 | if (index < static_cast<uint32_t>(info.length)) { |
| 486 | info.value_boot_image_reference = IntrinsicObjects::EncodePatch( |
| 487 | IntrinsicObjects::PatchType::kIntegerValueOfObject, index); |
| 488 | } else { |
| 489 | // Not in the cache. |
| 490 | info.value_boot_image_reference = IntegerValueOfInfo::kInvalidReference; |
| 491 | } |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 492 | } else { |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 493 | info.array_data_boot_image_reference = |
| 494 | IntrinsicObjects::EncodePatch(IntrinsicObjects::PatchType::kIntegerValueOfArray); |
Vladimir Marko | eebb821 | 2018-06-05 14:57:24 +0100 | [diff] [blame] | 495 | } |
| 496 | } else { |
Vladimir Marko | 6fd1606 | 2018-06-26 11:02:04 +0100 | [diff] [blame] | 497 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = GetBootImageLiveObjects(); |
| 498 | ObjPtr<mirror::Object> low_integer = |
| 499 | IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, 0u); |
| 500 | ObjPtr<mirror::Class> integer_class = low_integer->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 501 | ArtField* value_field = integer_class->FindDeclaredInstanceField(kValueFieldName, "I"); |
| 502 | DCHECK(value_field != nullptr); |
| 503 | info.value_offset = value_field->GetOffset().Uint32Value(); |
| 504 | if (runtime->UseJitCompilation()) { |
| 505 | // Use the current `IntegerCache.low` for JIT to avoid truly surprising behavior if the |
| 506 | // code messes up the `value` field in the lowest cached Integer using reflection. |
| 507 | info.low = GetIntegerCacheLowFromIntegerCache(self, class_linker); |
| 508 | } else { |
| 509 | // For app AOT, the `low_integer->value` should be the same as `IntegerCache.low`. |
| 510 | info.low = value_field->GetInt(low_integer); |
| 511 | DCHECK_EQ(info.low, GetIntegerCacheLowFromIntegerCache(self, class_linker)); |
| 512 | } |
| 513 | // Do not look at `IntegerCache.high`, use the immutable length of the cache array instead. |
| 514 | info.length = dchecked_integral_cast<uint32_t>( |
| 515 | IntrinsicObjects::GetIntegerValueOfCache(boot_image_live_objects)->GetLength()); |
| 516 | |
| 517 | info.integer_boot_image_offset = CalculateBootImageOffset(integer_class); |
| 518 | if (invoke->InputAt(0)->IsIntConstant()) { |
| 519 | int32_t input_value = invoke->InputAt(0)->AsIntConstant()->GetValue(); |
| 520 | uint32_t index = static_cast<uint32_t>(input_value) - static_cast<uint32_t>(info.low); |
| 521 | if (index < static_cast<uint32_t>(info.length)) { |
| 522 | ObjPtr<mirror::Object> integer = |
| 523 | IntrinsicObjects::GetIntegerValueOfObject(boot_image_live_objects, index); |
| 524 | info.value_boot_image_reference = CalculateBootImageOffset(integer); |
| 525 | } else { |
| 526 | // Not in the cache. |
| 527 | info.value_boot_image_reference = IntegerValueOfInfo::kInvalidReference; |
| 528 | } |
| 529 | } else { |
| 530 | info.array_data_boot_image_reference = |
| 531 | CalculateBootImageOffset(boot_image_live_objects) + |
| 532 | IntrinsicObjects::GetIntegerValueOfArrayDataOffset(boot_image_live_objects).Uint32Value(); |
| 533 | } |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Nicolas Geoffray | 331605a | 2017-03-01 11:01:41 +0000 | [diff] [blame] | 536 | return info; |
| 537 | } |
| 538 | |
Roland Levillain | 1d775d2 | 2018-09-07 13:56:57 +0100 | [diff] [blame^] | 539 | void IntrinsicVisitor::AssertNonMovableStringClass() { |
| 540 | if (kIsDebugBuild) { |
| 541 | Thread* const self = Thread::Current(); |
| 542 | ReaderMutexLock mu(self, *Locks::mutator_lock_); |
| 543 | ObjPtr<mirror::Class> string_class = GetClassRoot<art::mirror::String>(); |
| 544 | CHECK(!art::Runtime::Current()->GetHeap()->IsMovableObject(string_class)); |
| 545 | } |
| 546 | } |
| 547 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 548 | } // namespace art |