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 "mirror/class.h"
24 #include "runtime.h"
25 #include "scoped_thread_state_change-inl.h"
26 #include "thread.h"
27 
28 namespace art {
29 
30 namespace {
31 
32 // Initialize an intrinsic. Returns true if the intrinsic is already
33 // initialized, false otherwise.
InitializeIntrinsic(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)34 bool InitializeIntrinsic(Thread* self,
35                          Intrinsics intrinsic,
36                          InvokeType invoke_type,
37                          const char* class_name,
38                          const char* method_name,
39                          const char* signature)
40     REQUIRES_SHARED(Locks::mutator_lock_) {
41   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
42   PointerSize image_size = class_linker->GetImagePointerSize();
43   ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name);
44   if (cls == nullptr) {
45     LOG(FATAL) << "Could not find class of intrinsic " << class_name;
46   }
47 
48   ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size);
49   if (method == nullptr || method->GetDeclaringClass() != cls) {
50     LOG(FATAL) << "Could not find method of intrinsic "
51                << class_name << " " << method_name << " " << signature;
52   }
53 
54   CHECK_EQ(method->GetInvokeType(), invoke_type);
55   if (method->IsIntrinsic()) {
56     CHECK_EQ(method->GetIntrinsic(), static_cast<uint32_t>(intrinsic));
57     return true;
58   } else {
59     method->SetIntrinsic(static_cast<uint32_t>(intrinsic));
60     return false;
61   }
62 }
63 
64 }  // namespace
65 
InitializeIntrinsics()66 void InitializeIntrinsics() {
67   ScopedObjectAccess soa(Thread::Current());
68   // Initialization here uses the short-circuit operator || to stop
69   // initializing if there's an already initialized intrinsic.
70 #define SETUP_INTRINSICS(Name, InvokeType, _, __, ___, ClassName, MethodName, Signature) \
71   InitializeIntrinsic(soa.Self(),                                                        \
72                       Intrinsics::k##Name,                                               \
73                       InvokeType,                                                        \
74                       ClassName,                                                         \
75                       MethodName,                                                        \
76                       Signature) ||
77 #include "intrinsics_list.h"
78   INTRINSICS_LIST(SETUP_INTRINSICS)
79 #undef INTRINSICS_LIST
80 #undef SETUP_INTRINSICS
81       true;
82 }
83 
84 }  // namespace art
85