1 /*
2 * Copyright (C) 2019 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 "palette/palette.h"
18
19 #include <dlfcn.h>
20 #include <stdlib.h>
21
22 #include <android/log.h>
23 #include <android-base/macros.h>
24
25 namespace {
26
27 // Logging tag.
28 static constexpr const char* kLogTag = "libartpalette";
29
30 // Name of the palette library present in the /system partition.
31 static constexpr const char* kPaletteSystemLibrary = "libartpalette-system.so";
32
33 // Generic method used when a dynamically loaded palette instance does not
34 // support a method.
PaletteMethodNotSupported()35 palette_status_t PaletteMethodNotSupported() {
36 return PALETTE_STATUS_NOT_SUPPORTED;
37 }
38
39 // Declare type aliases for pointers to each function in the interface.
40 #define PALETTE_METHOD_TYPE_ALIAS(Name, ...) \
41 using Name ## Method = palette_status_t(*)(__VA_ARGS__);
42 PALETTE_METHOD_LIST(PALETTE_METHOD_TYPE_ALIAS)
43 #undef PALETTE_METHOD_TYPE_ALIAS
44
45 // Singleton class responsible for dynamically loading the palette library and
46 // binding functions there to method pointers.
47 class PaletteLoader {
48 public:
Instance()49 static PaletteLoader& Instance() {
50 static PaletteLoader instance;
51 return instance;
52 }
53
54 // Accessor methods to get instances of palette methods.
55 #define PALETTE_LOADER_METHOD_ACCESSOR(Name, ...) \
56 Name ## Method Get ## Name ## Method() const { return Name ## Method ## _; }
57 PALETTE_METHOD_LIST(PALETTE_LOADER_METHOD_ACCESSOR)
58 #undef PALETTE_LOADER_METHOD_ACCESSOR
59
60 private:
61 PaletteLoader();
62
63 static void* OpenLibrary();
64 static void* GetMethod(void* palette_lib, const char* name);
65
66 // Handle to the palette library from dlopen().
67 void* palette_lib_;
68
69 // Fields to store pointers to palette methods.
70 #define PALETTE_LOADER_METHOD_FIELD(Name, ...) \
71 const Name ## Method Name ## Method ## _;
72 PALETTE_METHOD_LIST(PALETTE_LOADER_METHOD_FIELD)
73 #undef PALETTE_LOADER_METHOD_FIELD
74
75 DISALLOW_COPY_AND_ASSIGN(PaletteLoader);
76 };
77
OpenLibrary()78 void* PaletteLoader::OpenLibrary() {
79 void* handle = dlopen(kPaletteSystemLibrary, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
80 if (handle == nullptr) {
81 // dlerror message includes details of error and file being opened.
82 __android_log_assert(nullptr, kLogTag, "%s", dlerror());
83 }
84 return handle;
85 }
86
GetMethod(void * palette_lib,const char * name)87 void* PaletteLoader::GetMethod(void* palette_lib, const char* name) {
88 void* method = nullptr;
89 if (palette_lib != nullptr) {
90 method = dlsym(palette_lib, name);
91 }
92 if (method == nullptr) {
93 return reinterpret_cast<void*>(PaletteMethodNotSupported);
94 }
95 // TODO(oth): consider new GetMethodSignature() in the Palette API which
96 // would allow checking the validity of the type signatures.
97 return method;
98 }
99
PaletteLoader()100 PaletteLoader::PaletteLoader() :
101 palette_lib_(OpenLibrary())
102 #define PALETTE_LOADER_BIND_METHOD(Name, ...) \
103 , Name ## Method ## _(reinterpret_cast<Name ## Method>(GetMethod(palette_lib_, #Name)))
104 PALETTE_METHOD_LIST(PALETTE_LOADER_BIND_METHOD)
105 #undef PALETTE_LOADER_BIND_METHOD
106 {
107 }
108
109 } // namespace
110
111 extern "C" {
112
PaletteSchedSetPriority(int32_t tid,int32_t java_priority)113 palette_status_t PaletteSchedSetPriority(int32_t tid, int32_t java_priority) {
114 PaletteSchedSetPriorityMethod m = PaletteLoader::Instance().GetPaletteSchedSetPriorityMethod();
115 return m(tid, java_priority);
116 }
117
PaletteSchedGetPriority(int32_t tid,int32_t * java_priority)118 palette_status_t PaletteSchedGetPriority(int32_t tid, /*out*/int32_t* java_priority) {
119 PaletteSchedGetPriorityMethod m = PaletteLoader::Instance().GetPaletteSchedGetPriorityMethod();
120 return m(tid, java_priority);
121 }
122
PaletteWriteCrashThreadStacks(const char * stack,size_t stack_len)123 palette_status_t PaletteWriteCrashThreadStacks(/*in*/const char* stack, size_t stack_len) {
124 PaletteWriteCrashThreadStacksMethod m =
125 PaletteLoader::Instance().GetPaletteWriteCrashThreadStacksMethod();
126 return m(stack, stack_len);
127 }
128
PaletteTraceEnabled(bool * enabled)129 palette_status_t PaletteTraceEnabled(/*out*/bool* enabled) {
130 PaletteTraceEnabledMethod m = PaletteLoader::Instance().GetPaletteTraceEnabledMethod();
131 return m(enabled);
132 }
133
PaletteTraceBegin(const char * name)134 palette_status_t PaletteTraceBegin(/*in*/const char* name) {
135 PaletteTraceBeginMethod m = PaletteLoader::Instance().GetPaletteTraceBeginMethod();
136 return m(name);
137 }
138
PaletteTraceEnd()139 palette_status_t PaletteTraceEnd() {
140 PaletteTraceEndMethod m = PaletteLoader::Instance().GetPaletteTraceEndMethod();
141 return m();
142 }
143
PaletteTraceIntegerValue(const char * name,int32_t value)144 palette_status_t PaletteTraceIntegerValue(/*in*/const char* name, int32_t value) {
145 PaletteTraceIntegerValueMethod m = PaletteLoader::Instance().GetPaletteTraceIntegerValueMethod();
146 return m(name, value);
147 }
148
PaletteAshmemCreateRegion(const char * name,size_t size,int * fd)149 palette_status_t PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) {
150 PaletteAshmemCreateRegionMethod m =
151 PaletteLoader::Instance().GetPaletteAshmemCreateRegionMethod();
152 return m(name, size, fd);
153 }
154
PaletteAshmemSetProtRegion(int fd,int prot)155 palette_status_t PaletteAshmemSetProtRegion(int fd, int prot) {
156 PaletteAshmemSetProtRegionMethod m =
157 PaletteLoader::Instance().GetPaletteAshmemSetProtRegionMethod();
158 return m(fd, prot);
159 }
160
PaletteCreateOdrefreshStagingDirectory(const char ** staging_dir)161 palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
162 PaletteCreateOdrefreshStagingDirectoryMethod m =
163 PaletteLoader::Instance().GetPaletteCreateOdrefreshStagingDirectoryMethod();
164 return m(staging_dir);
165 }
166
PaletteShouldReportDex2oatCompilation(bool * value)167 palette_status_t PaletteShouldReportDex2oatCompilation(bool* value) {
168 PaletteShouldReportDex2oatCompilationMethod m =
169 PaletteLoader::Instance().GetPaletteShouldReportDex2oatCompilationMethod();
170 return m(value);
171 }
172
PaletteNotifyStartDex2oatCompilation(int source_fd,int art_fd,int oat_fd,int vdex_fd)173 palette_status_t PaletteNotifyStartDex2oatCompilation(int source_fd,
174 int art_fd,
175 int oat_fd,
176 int vdex_fd) {
177 PaletteNotifyStartDex2oatCompilationMethod m =
178 PaletteLoader::Instance().GetPaletteNotifyStartDex2oatCompilationMethod();
179 return m(source_fd, art_fd, oat_fd, vdex_fd);
180 }
181
PaletteNotifyEndDex2oatCompilation(int source_fd,int art_fd,int oat_fd,int vdex_fd)182 palette_status_t PaletteNotifyEndDex2oatCompilation(int source_fd,
183 int art_fd,
184 int oat_fd,
185 int vdex_fd) {
186 PaletteNotifyEndDex2oatCompilationMethod m =
187 PaletteLoader::Instance().GetPaletteNotifyEndDex2oatCompilationMethod();
188 return m(source_fd, art_fd, oat_fd, vdex_fd);
189 }
190
PaletteNotifyDexFileLoaded(const char * path)191 palette_status_t PaletteNotifyDexFileLoaded(const char* path) {
192 PaletteNotifyDexFileLoadedMethod m =
193 PaletteLoader::Instance().GetPaletteNotifyDexFileLoadedMethod();
194 return m(path);
195 }
196
PaletteNotifyOatFileLoaded(const char * path)197 palette_status_t PaletteNotifyOatFileLoaded(const char* path) {
198 PaletteNotifyOatFileLoadedMethod m =
199 PaletteLoader::Instance().GetPaletteNotifyOatFileLoadedMethod();
200 return m(path);
201 }
202
PaletteShouldReportJniInvocations(bool * value)203 palette_status_t PaletteShouldReportJniInvocations(bool* value) {
204 PaletteShouldReportJniInvocationsMethod m =
205 PaletteLoader::Instance().GetPaletteShouldReportJniInvocationsMethod();
206 return m(value);
207 }
208
PaletteNotifyBeginJniInvocation(JNIEnv * env)209 palette_status_t PaletteNotifyBeginJniInvocation(JNIEnv* env) {
210 PaletteNotifyBeginJniInvocationMethod m =
211 PaletteLoader::Instance().GetPaletteNotifyBeginJniInvocationMethod();
212 return m(env);
213 }
214
PaletteNotifyEndJniInvocation(JNIEnv * env)215 palette_status_t PaletteNotifyEndJniInvocation(JNIEnv* env) {
216 PaletteNotifyEndJniInvocationMethod m =
217 PaletteLoader::Instance().GetPaletteNotifyEndJniInvocationMethod();
218 return m(env);
219 }
220
221 } // extern "C"
222