1 /*
2 * Copyright (C) 2017 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 "runtime_intrinsics.h"
18
19 #include "art_method-inl.h"
20 #include "class_linker.h"
21 #include "dex/invoke_type.h"
22 #include "intrinsics_enum.h"
23 #include "intrinsics_list.h"
24 #include "mirror/class.h"
25 #include "runtime.h"
26 #include "scoped_thread_state_change-inl.h"
27 #include "thread.h"
28
29 namespace art {
30
31 namespace {
32
FindIntrinsicMethod(Thread * self,const char * class_name,const char * method_name,const char * signature)33 ArtMethod* FindIntrinsicMethod(Thread* self,
34 const char* class_name,
35 const char* method_name,
36 const char* signature)
37 REQUIRES_SHARED(Locks::mutator_lock_) {
38 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
39 PointerSize image_size = class_linker->GetImagePointerSize();
40 ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name);
41 if (cls == nullptr) {
42 LOG(FATAL) << "Could not find class of intrinsic " << class_name;
43 }
44
45 ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size);
46 if (method == nullptr || method->GetDeclaringClass() != cls) {
47 LOG(FATAL) << "Could not find method of intrinsic "
48 << class_name << " " << method_name << " " << signature;
49 }
50 return method;
51 }
52
53 // Initialize an intrinsic. Returns true if the intrinsic is already
54 // initialized, false otherwise.
InitializeIntrinsic(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)55 bool InitializeIntrinsic(Thread* self,
56 Intrinsics intrinsic,
57 InvokeType invoke_type,
58 const char* class_name,
59 const char* method_name,
60 const char* signature)
61 REQUIRES_SHARED(Locks::mutator_lock_) {
62 ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
63
64 CHECK_EQ(method->GetInvokeType(), invoke_type);
65 if (method->IsIntrinsic()) {
66 CHECK_EQ(method->GetIntrinsic(), static_cast<uint32_t>(intrinsic));
67 return true;
68 } else {
69 method->SetIntrinsic(static_cast<uint32_t>(intrinsic));
70 return false;
71 }
72 }
73
74 // Returns true if the intrinsic is already initialized, false otherwise.
IsIntrinsicInitialized(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)75 bool IsIntrinsicInitialized(Thread* self,
76 Intrinsics intrinsic,
77 InvokeType invoke_type,
78 const char* class_name,
79 const char* method_name,
80 const char* signature)
81 REQUIRES_SHARED(Locks::mutator_lock_) {
82 ArtMethod* method = FindIntrinsicMethod(self, class_name, method_name, signature);
83
84 CHECK_EQ(method->GetInvokeType(), invoke_type);
85 if (method->IsIntrinsic()) {
86 CHECK_EQ(method->GetIntrinsic(), static_cast<uint32_t>(intrinsic));
87 return true;
88 } else {
89 return false;
90 }
91 }
92
AreAllIntrinsicsInitialized()93 bool AreAllIntrinsicsInitialized() {
94 ScopedObjectAccess soa(Thread::Current());
95 #define IS_INTRINSIC_INITIALIZED(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
96 IsIntrinsicInitialized(soa.Self(), \
97 Intrinsics::k##Name, \
98 InvokeType, \
99 ClassName, \
100 MethodName, \
101 Signature) &&
102 bool result = INTRINSICS_LIST(IS_INTRINSIC_INITIALIZED) true;
103 #undef IS_INTRINSIC_INITIALIZED
104 return result;
105 }
106
107 } // namespace
108
InitializeIntrinsics()109 void InitializeIntrinsics() {
110 ScopedObjectAccess soa(Thread::Current());
111 // Initialization here uses the short-circuit operator || to stop
112 // initializing if there's an already initialized intrinsic.
113 #define INITIALIZE_INTRINSIC(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
114 InitializeIntrinsic(soa.Self(), \
115 Intrinsics::k##Name, \
116 InvokeType, \
117 ClassName, \
118 MethodName, \
119 Signature) ||
120 INTRINSICS_LIST(INITIALIZE_INTRINSIC) true;
121 #undef INITIALIZE_INTRINSIC
122 DCHECK(AreAllIntrinsicsInitialized());
123 }
124
125 } // namespace art
126