1 /*
2 * Copyright (C) 2021 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 #ifndef ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_
18 #define ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_
19
20 #include <string>
21 #include <unordered_map>
22
23 #include <android-base/stringprintf.h>
24 #include <gmock/gmock.h>
25 #include <jni.h>
26
27 #include "native_loader_namespace.h"
28 #include "nativeloader/dlext_namespaces.h"
29
30 namespace android {
31 namespace nativeloader {
32
33 using ::testing::Return;
34 using ::testing::_;
35
36 // gmock interface that represents interested platform APIs on libdl_android and libnativebridge
37 class Platform {
38 public:
~Platform()39 virtual ~Platform() {}
40
41 // These mock_* are the APIs semantically the same across libdl_android and libnativebridge.
42 // Instead of having two set of mock APIs for the two, define only one set with an additional
43 // argument 'bool bridged' to identify the context (i.e., called for libdl_android or
44 // libnativebridge).
45 using mock_namespace_handle = char*;
46 virtual bool mock_init_anonymous_namespace(bool bridged, const char* sonames,
47 const char* search_paths) = 0;
48 virtual mock_namespace_handle mock_create_namespace(
49 bool bridged, const char* name, const char* ld_library_path, const char* default_library_path,
50 uint64_t type, const char* permitted_when_isolated_path, mock_namespace_handle parent) = 0;
51 virtual bool mock_link_namespaces(bool bridged, mock_namespace_handle from,
52 mock_namespace_handle to, const char* sonames) = 0;
53 virtual mock_namespace_handle mock_get_exported_namespace(bool bridged, const char* name) = 0;
54 virtual void* mock_dlopen_ext(bool bridged, const char* filename, int flags,
55 mock_namespace_handle ns) = 0;
56
57 // libnativebridge APIs for which libdl_android has no corresponding APIs
58 virtual bool NativeBridgeInitialized() = 0;
59 virtual const char* NativeBridgeGetError() = 0;
60 virtual bool NativeBridgeIsPathSupported(const char*) = 0;
61 virtual bool NativeBridgeIsSupported(const char*) = 0;
62
63 // To mock "ClassLoader Object.getParent()"
64 virtual const char* JniObject_getParent(const char*) = 0;
65 };
66
67 // The mock does not actually create a namespace object. But simply casts the pointer to the
68 // string for the namespace name as the handle to the namespace object.
69 #define TO_ANDROID_NAMESPACE(str) \
70 reinterpret_cast<struct android_namespace_t*>(const_cast<char*>(str))
71
72 #define TO_BRIDGED_NAMESPACE(str) \
73 reinterpret_cast<struct native_bridge_namespace_t*>(const_cast<char*>(str))
74
75 #define TO_MOCK_NAMESPACE(ns) reinterpret_cast<Platform::mock_namespace_handle>(ns)
76
77 // These represents built-in namespaces created by the linker according to ld.config.txt
78 static std::unordered_map<std::string, Platform::mock_namespace_handle> namespaces = {
79 #define NAMESPACE_ENTRY(ns) {ns, TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(ns))}
80 NAMESPACE_ENTRY("com_android_i18n"),
81 NAMESPACE_ENTRY("com_android_neuralnetworks"),
82 NAMESPACE_ENTRY("com_android_art"),
83
84 // TODO(b/191644631) This can be removed when the test becomes more test-friendly.
85 // This is added so that the test can exercise the JNI lib related behavior.
86 NAMESPACE_ENTRY("com_android_conscrypt"),
87
88 NAMESPACE_ENTRY("default"),
89 NAMESPACE_ENTRY("sphal"),
90 NAMESPACE_ENTRY("product"),
91 NAMESPACE_ENTRY("system"),
92 NAMESPACE_ENTRY("vndk"),
93 NAMESPACE_ENTRY("vndk_product"),
94 #undef NAMESPACE_ENTRY
95 };
96
97 // The actual gmock object
98 class MockPlatform : public Platform {
99 public:
MockPlatform(bool is_bridged)100 explicit MockPlatform(bool is_bridged) : is_bridged_(is_bridged) {
101 ON_CALL(*this, NativeBridgeIsSupported(_)).WillByDefault(Return(is_bridged_));
102 ON_CALL(*this, NativeBridgeIsPathSupported(_)).WillByDefault(Return(is_bridged_));
103 ON_CALL(*this, mock_get_exported_namespace(_, _))
104 .WillByDefault(testing::Invoke([](bool, const char* name) -> mock_namespace_handle {
105 if (namespaces.find(name) != namespaces.end()) {
106 return namespaces[name];
107 }
108 std::string msg = android::base::StringPrintf("(namespace %s not found)", name);
109 // The strdup'ed string will leak, but the test is already failing if we get here.
110 return TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(strdup(msg.c_str())));
111 }));
112 }
113
114 // Mocking the common APIs
115 MOCK_METHOD3(mock_init_anonymous_namespace, bool(bool, const char*, const char*));
116 MOCK_METHOD7(mock_create_namespace,
117 mock_namespace_handle(bool, const char*, const char*, const char*, uint64_t,
118 const char*, mock_namespace_handle));
119 MOCK_METHOD4(mock_link_namespaces,
120 bool(bool, mock_namespace_handle, mock_namespace_handle, const char*));
121 MOCK_METHOD2(mock_get_exported_namespace, mock_namespace_handle(bool, const char*));
122 MOCK_METHOD4(mock_dlopen_ext, void*(bool, const char*, int, mock_namespace_handle));
123
124 // Mocking libnativebridge APIs
125 MOCK_METHOD0(NativeBridgeInitialized, bool());
126 MOCK_METHOD0(NativeBridgeGetError, const char*());
127 MOCK_METHOD1(NativeBridgeIsPathSupported, bool(const char*));
128 MOCK_METHOD1(NativeBridgeIsSupported, bool(const char*));
129
130 // Mocking "ClassLoader Object.getParent()"
131 MOCK_METHOD1(JniObject_getParent, const char*(const char*));
132
133 private:
134 bool is_bridged_;
135 };
136
137 static std::unique_ptr<MockPlatform> mock;
138
139 // Provide C wrappers for the mock object. These symbols must be exported by ld
140 // to be able to override the real symbols in the shared libs.
141 extern "C" {
142
143 // libdl_android APIs
144
android_init_anonymous_namespace(const char * sonames,const char * search_path)145 bool android_init_anonymous_namespace(const char* sonames, const char* search_path) {
146 return mock->mock_init_anonymous_namespace(false, sonames, search_path);
147 }
148
android_create_namespace(const char * name,const char * ld_library_path,const char * default_library_path,uint64_t type,const char * permitted_when_isolated_path,struct android_namespace_t * parent)149 struct android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
150 const char* default_library_path,
151 uint64_t type,
152 const char* permitted_when_isolated_path,
153 struct android_namespace_t* parent) {
154 return TO_ANDROID_NAMESPACE(
155 mock->mock_create_namespace(false, name, ld_library_path, default_library_path, type,
156 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
157 }
158
android_link_namespaces(struct android_namespace_t * from,struct android_namespace_t * to,const char * sonames)159 bool android_link_namespaces(struct android_namespace_t* from, struct android_namespace_t* to,
160 const char* sonames) {
161 return mock->mock_link_namespaces(false, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
162 }
163
android_get_exported_namespace(const char * name)164 struct android_namespace_t* android_get_exported_namespace(const char* name) {
165 return TO_ANDROID_NAMESPACE(mock->mock_get_exported_namespace(false, name));
166 }
167
android_dlopen_ext(const char * filename,int flags,const android_dlextinfo * info)168 void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* info) {
169 return mock->mock_dlopen_ext(false, filename, flags, TO_MOCK_NAMESPACE(info->library_namespace));
170 }
171
172 // libnativebridge APIs
173
NativeBridgeIsSupported(const char * libpath)174 bool NativeBridgeIsSupported(const char* libpath) {
175 return mock->NativeBridgeIsSupported(libpath);
176 }
177
NativeBridgeGetExportedNamespace(const char * name)178 struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
179 return TO_BRIDGED_NAMESPACE(mock->mock_get_exported_namespace(true, name));
180 }
181
NativeBridgeCreateNamespace(const char * name,const char * ld_library_path,const char * default_library_path,uint64_t type,const char * permitted_when_isolated_path,struct native_bridge_namespace_t * parent)182 struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
183 const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
184 const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent) {
185 return TO_BRIDGED_NAMESPACE(
186 mock->mock_create_namespace(true, name, ld_library_path, default_library_path, type,
187 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
188 }
189
NativeBridgeLinkNamespaces(struct native_bridge_namespace_t * from,struct native_bridge_namespace_t * to,const char * sonames)190 bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
191 struct native_bridge_namespace_t* to, const char* sonames) {
192 return mock->mock_link_namespaces(true, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
193 }
194
NativeBridgeLoadLibraryExt(const char * libpath,int flag,struct native_bridge_namespace_t * ns)195 void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
196 struct native_bridge_namespace_t* ns) {
197 return mock->mock_dlopen_ext(true, libpath, flag, TO_MOCK_NAMESPACE(ns));
198 }
199
NativeBridgeInitialized()200 bool NativeBridgeInitialized() {
201 return mock->NativeBridgeInitialized();
202 }
203
NativeBridgeInitAnonymousNamespace(const char * public_ns_sonames,const char * anon_ns_library_path)204 bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
205 const char* anon_ns_library_path) {
206 return mock->mock_init_anonymous_namespace(true, public_ns_sonames, anon_ns_library_path);
207 }
208
NativeBridgeGetError()209 const char* NativeBridgeGetError() {
210 return mock->NativeBridgeGetError();
211 }
212
NativeBridgeIsPathSupported(const char * path)213 bool NativeBridgeIsPathSupported(const char* path) {
214 return mock->NativeBridgeIsPathSupported(path);
215 }
216
217 } // extern "C"
218
219 // A very simple JNI mock.
220 // jstring is a pointer to utf8 char array. We don't need utf16 char here.
221 // jobject, jclass, and jmethodID are also a pointer to utf8 char array
222 // Only a few JNI methods that are actually used in libnativeloader are mocked.
CreateJNINativeInterface()223 JNINativeInterface* CreateJNINativeInterface() {
224 JNINativeInterface* inf = new JNINativeInterface();
225 memset(inf, 0, sizeof(JNINativeInterface));
226
227 inf->GetStringUTFChars = [](JNIEnv*, jstring s, jboolean*) -> const char* {
228 return reinterpret_cast<const char*>(s);
229 };
230
231 inf->ReleaseStringUTFChars = [](JNIEnv*, jstring, const char*) -> void { return; };
232
233 inf->NewStringUTF = [](JNIEnv*, const char* bytes) -> jstring {
234 return reinterpret_cast<jstring>(const_cast<char*>(bytes));
235 };
236
237 inf->FindClass = [](JNIEnv*, const char* name) -> jclass {
238 return reinterpret_cast<jclass>(const_cast<char*>(name));
239 };
240
241 inf->CallObjectMethodV = [](JNIEnv*, jobject obj, jmethodID mid, va_list) -> jobject {
242 if (strcmp("getParent", reinterpret_cast<const char*>(mid)) == 0) {
243 // JniObject_getParent can be a valid jobject or nullptr if there is
244 // no parent classloader.
245 const char* ret = mock->JniObject_getParent(reinterpret_cast<const char*>(obj));
246 return reinterpret_cast<jobject>(const_cast<char*>(ret));
247 }
248 return nullptr;
249 };
250
251 inf->GetMethodID = [](JNIEnv*, jclass, const char* name, const char*) -> jmethodID {
252 return reinterpret_cast<jmethodID>(const_cast<char*>(name));
253 };
254
255 inf->NewWeakGlobalRef = [](JNIEnv*, jobject obj) -> jobject { return obj; };
256
257 inf->IsSameObject = [](JNIEnv*, jobject a, jobject b) -> jboolean {
258 return strcmp(reinterpret_cast<const char*>(a), reinterpret_cast<const char*>(b)) == 0;
259 };
260
261 return inf;
262 }
263
264 } // namespace nativeloader
265 } // namespace android
266
267 #endif // ART_LIBNATIVELOADER_NATIVE_LOADER_TEST_H_
268