blob: e64e1a518c1686faccc371b4a640b4b504ce4974 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010017#if defined(ART_TARGET_ANDROID)
18
Orion Hodson9b16e342019-10-09 13:29:16 +010019#include <dlfcn.h>
20#include <memory>
21#include <unordered_map>
22
23#include <android-base/strings.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <jni.h>
27
28#include "native_loader_namespace.h"
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010029#include "nativehelper/scoped_utf_chars.h"
Orion Hodson9b16e342019-10-09 13:29:16 +010030#include "nativeloader/dlext_namespaces.h"
31#include "nativeloader/native_loader.h"
32#include "public_libraries.h"
33
Orion Hodson9b16e342019-10-09 13:29:16 +010034namespace android {
35namespace nativeloader {
36
Martin Stjernholm48297332019-11-12 21:21:32 +000037using ::testing::Eq;
38using ::testing::Return;
39using ::testing::StrEq;
40using ::testing::_;
41using internal::ConfigEntry;
42using internal::ParseConfig;
Jooyung Han538f99a2020-03-03 00:46:50 +090043using internal::ParseJniConfig;
Martin Stjernholm48297332019-11-12 21:21:32 +000044
Martin Stjernholmbe08b202019-11-12 20:11:00 +000045#if defined(__LP64__)
46#define LIB_DIR "lib64"
47#else
48#define LIB_DIR "lib"
49#endif
50
Orion Hodson9b16e342019-10-09 13:29:16 +010051// gmock interface that represents interested platform APIs on libdl and libnativebridge
52class Platform {
53 public:
54 virtual ~Platform() {}
55
56 // libdl APIs
57 virtual void* dlopen(const char* filename, int flags) = 0;
58 virtual int dlclose(void* handle) = 0;
59 virtual char* dlerror(void) = 0;
60
61 // These mock_* are the APIs semantically the same across libdl and libnativebridge.
62 // Instead of having two set of mock APIs for the two, define only one set with an additional
63 // argument 'bool bridged' to identify the context (i.e., called for libdl or libnativebridge).
64 typedef char* mock_namespace_handle;
65 virtual bool mock_init_anonymous_namespace(bool bridged, const char* sonames,
66 const char* search_paths) = 0;
67 virtual mock_namespace_handle mock_create_namespace(
68 bool bridged, const char* name, const char* ld_library_path, const char* default_library_path,
69 uint64_t type, const char* permitted_when_isolated_path, mock_namespace_handle parent) = 0;
70 virtual bool mock_link_namespaces(bool bridged, mock_namespace_handle from,
71 mock_namespace_handle to, const char* sonames) = 0;
72 virtual mock_namespace_handle mock_get_exported_namespace(bool bridged, const char* name) = 0;
73 virtual void* mock_dlopen_ext(bool bridged, const char* filename, int flags,
74 mock_namespace_handle ns) = 0;
75
76 // libnativebridge APIs for which libdl has no corresponding APIs
77 virtual bool NativeBridgeInitialized() = 0;
78 virtual const char* NativeBridgeGetError() = 0;
79 virtual bool NativeBridgeIsPathSupported(const char*) = 0;
80 virtual bool NativeBridgeIsSupported(const char*) = 0;
81
82 // To mock "ClassLoader Object.getParent()"
83 virtual const char* JniObject_getParent(const char*) = 0;
84};
85
86// The mock does not actually create a namespace object. But simply casts the pointer to the
87// string for the namespace name as the handle to the namespace object.
88#define TO_ANDROID_NAMESPACE(str) \
89 reinterpret_cast<struct android_namespace_t*>(const_cast<char*>(str))
90
91#define TO_BRIDGED_NAMESPACE(str) \
92 reinterpret_cast<struct native_bridge_namespace_t*>(const_cast<char*>(str))
93
94#define TO_MOCK_NAMESPACE(ns) reinterpret_cast<Platform::mock_namespace_handle>(ns)
95
96// These represents built-in namespaces created by the linker according to ld.config.txt
97static std::unordered_map<std::string, Platform::mock_namespace_handle> namespaces = {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +090098 {"system", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("system"))},
Orion Hodson9b16e342019-10-09 13:29:16 +010099 {"default", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("default"))},
Victor Changd20e51d2020-05-05 16:01:19 +0100100 {"com_android_i18n", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("com_android_i18n"))},
Orion Hodson9b16e342019-10-09 13:29:16 +0100101 {"sphal", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("sphal"))},
102 {"vndk", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("vndk"))},
Justin Yuneb4f08c2020-02-18 11:29:07 +0900103 {"vndk_product", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("vndk_product"))},
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900104 {"com_android_neuralnetworks", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("com_android_neuralnetworks"))},
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900105 {"com_android_os_statsd", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("com_android_os_statsd"))},
Orion Hodson9b16e342019-10-09 13:29:16 +0100106};
107
108// The actual gmock object
109class MockPlatform : public Platform {
110 public:
111 explicit MockPlatform(bool is_bridged) : is_bridged_(is_bridged) {
112 ON_CALL(*this, NativeBridgeIsSupported(_)).WillByDefault(Return(is_bridged_));
113 ON_CALL(*this, NativeBridgeIsPathSupported(_)).WillByDefault(Return(is_bridged_));
114 ON_CALL(*this, mock_get_exported_namespace(_, _))
Martin Stjernholm48297332019-11-12 21:21:32 +0000115 .WillByDefault(testing::Invoke([](bool, const char* name) -> mock_namespace_handle {
Orion Hodson9b16e342019-10-09 13:29:16 +0100116 if (namespaces.find(name) != namespaces.end()) {
117 return namespaces[name];
118 }
119 return TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("(namespace not found"));
120 }));
121 }
122
123 // Mocking libdl APIs
124 MOCK_METHOD2(dlopen, void*(const char*, int));
125 MOCK_METHOD1(dlclose, int(void*));
126 MOCK_METHOD0(dlerror, char*());
127
128 // Mocking the common APIs
129 MOCK_METHOD3(mock_init_anonymous_namespace, bool(bool, const char*, const char*));
130 MOCK_METHOD7(mock_create_namespace,
131 mock_namespace_handle(bool, const char*, const char*, const char*, uint64_t,
132 const char*, mock_namespace_handle));
133 MOCK_METHOD4(mock_link_namespaces,
134 bool(bool, mock_namespace_handle, mock_namespace_handle, const char*));
135 MOCK_METHOD2(mock_get_exported_namespace, mock_namespace_handle(bool, const char*));
136 MOCK_METHOD4(mock_dlopen_ext, void*(bool, const char*, int, mock_namespace_handle));
137
138 // Mocking libnativebridge APIs
139 MOCK_METHOD0(NativeBridgeInitialized, bool());
140 MOCK_METHOD0(NativeBridgeGetError, const char*());
141 MOCK_METHOD1(NativeBridgeIsPathSupported, bool(const char*));
142 MOCK_METHOD1(NativeBridgeIsSupported, bool(const char*));
143
144 // Mocking "ClassLoader Object.getParent()"
145 MOCK_METHOD1(JniObject_getParent, const char*(const char*));
146
147 private:
148 bool is_bridged_;
149};
150
151static std::unique_ptr<MockPlatform> mock;
152
153// Provide C wrappers for the mock object.
154extern "C" {
155void* dlopen(const char* file, int flag) {
156 return mock->dlopen(file, flag);
157}
158
159int dlclose(void* handle) {
160 return mock->dlclose(handle);
161}
162
163char* dlerror(void) {
164 return mock->dlerror();
165}
166
167bool android_init_anonymous_namespace(const char* sonames, const char* search_path) {
168 return mock->mock_init_anonymous_namespace(false, sonames, search_path);
169}
170
171struct android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
172 const char* default_library_path,
173 uint64_t type,
174 const char* permitted_when_isolated_path,
175 struct android_namespace_t* parent) {
176 return TO_ANDROID_NAMESPACE(
177 mock->mock_create_namespace(false, name, ld_library_path, default_library_path, type,
178 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
179}
180
181bool android_link_namespaces(struct android_namespace_t* from, struct android_namespace_t* to,
182 const char* sonames) {
183 return mock->mock_link_namespaces(false, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
184}
185
186struct android_namespace_t* android_get_exported_namespace(const char* name) {
187 return TO_ANDROID_NAMESPACE(mock->mock_get_exported_namespace(false, name));
188}
189
190void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* info) {
191 return mock->mock_dlopen_ext(false, filename, flags, TO_MOCK_NAMESPACE(info->library_namespace));
192}
193
194// libnativebridge APIs
195bool NativeBridgeIsSupported(const char* libpath) {
196 return mock->NativeBridgeIsSupported(libpath);
197}
198
199struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) {
200 return TO_BRIDGED_NAMESPACE(mock->mock_get_exported_namespace(true, name));
201}
202
203struct native_bridge_namespace_t* NativeBridgeCreateNamespace(
204 const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type,
205 const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent) {
206 return TO_BRIDGED_NAMESPACE(
207 mock->mock_create_namespace(true, name, ld_library_path, default_library_path, type,
208 permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent)));
209}
210
211bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from,
212 struct native_bridge_namespace_t* to, const char* sonames) {
213 return mock->mock_link_namespaces(true, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames);
214}
215
216void* NativeBridgeLoadLibraryExt(const char* libpath, int flag,
217 struct native_bridge_namespace_t* ns) {
218 return mock->mock_dlopen_ext(true, libpath, flag, TO_MOCK_NAMESPACE(ns));
219}
220
221bool NativeBridgeInitialized() {
222 return mock->NativeBridgeInitialized();
223}
224
225bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames,
226 const char* anon_ns_library_path) {
227 return mock->mock_init_anonymous_namespace(true, public_ns_sonames, anon_ns_library_path);
228}
229
230const char* NativeBridgeGetError() {
231 return mock->NativeBridgeGetError();
232}
233
234bool NativeBridgeIsPathSupported(const char* path) {
235 return mock->NativeBridgeIsPathSupported(path);
236}
237
238} // extern "C"
239
240// A very simple JNI mock.
241// jstring is a pointer to utf8 char array. We don't need utf16 char here.
242// jobject, jclass, and jmethodID are also a pointer to utf8 char array
243// Only a few JNI methods that are actually used in libnativeloader are mocked.
244JNINativeInterface* CreateJNINativeInterface() {
245 JNINativeInterface* inf = new JNINativeInterface();
246 memset(inf, 0, sizeof(JNINativeInterface));
247
248 inf->GetStringUTFChars = [](JNIEnv*, jstring s, jboolean*) -> const char* {
249 return reinterpret_cast<const char*>(s);
250 };
251
252 inf->ReleaseStringUTFChars = [](JNIEnv*, jstring, const char*) -> void { return; };
253
254 inf->NewStringUTF = [](JNIEnv*, const char* bytes) -> jstring {
255 return reinterpret_cast<jstring>(const_cast<char*>(bytes));
256 };
257
258 inf->FindClass = [](JNIEnv*, const char* name) -> jclass {
259 return reinterpret_cast<jclass>(const_cast<char*>(name));
260 };
261
262 inf->CallObjectMethodV = [](JNIEnv*, jobject obj, jmethodID mid, va_list) -> jobject {
263 if (strcmp("getParent", reinterpret_cast<const char*>(mid)) == 0) {
264 // JniObject_getParent can be a valid jobject or nullptr if there is
265 // no parent classloader.
266 const char* ret = mock->JniObject_getParent(reinterpret_cast<const char*>(obj));
267 return reinterpret_cast<jobject>(const_cast<char*>(ret));
268 }
269 return nullptr;
270 };
271
272 inf->GetMethodID = [](JNIEnv*, jclass, const char* name, const char*) -> jmethodID {
273 return reinterpret_cast<jmethodID>(const_cast<char*>(name));
274 };
275
276 inf->NewWeakGlobalRef = [](JNIEnv*, jobject obj) -> jobject { return obj; };
277
278 inf->IsSameObject = [](JNIEnv*, jobject a, jobject b) -> jboolean {
279 return strcmp(reinterpret_cast<const char*>(a), reinterpret_cast<const char*>(b)) == 0;
280 };
281
282 return inf;
283}
284
285static void* const any_nonnull = reinterpret_cast<void*>(0x12345678);
286
287// Custom matcher for comparing namespace handles
288MATCHER_P(NsEq, other, "") {
289 *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other;
290 return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0;
291}
292
293/////////////////////////////////////////////////////////////////
294
295// Test fixture
296class NativeLoaderTest : public ::testing::TestWithParam<bool> {
297 protected:
298 bool IsBridged() { return GetParam(); }
299
300 void SetUp() override {
Martin Stjernholm48297332019-11-12 21:21:32 +0000301 mock = std::make_unique<testing::NiceMock<MockPlatform>>(IsBridged());
Orion Hodson9b16e342019-10-09 13:29:16 +0100302
303 env = std::make_unique<JNIEnv>();
304 env->functions = CreateJNINativeInterface();
305 }
306
307 void SetExpectations() {
308 std::vector<std::string> default_public_libs =
309 android::base::Split(preloadable_public_libraries(), ":");
310 for (auto l : default_public_libs) {
311 EXPECT_CALL(*mock, dlopen(StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE))
312 .WillOnce(Return(any_nonnull));
313 }
314 }
315
316 void RunTest() { InitializeNativeLoader(); }
317
318 void TearDown() override {
319 ResetNativeLoader();
320 delete env->functions;
321 mock.reset();
322 }
323
324 std::unique_ptr<JNIEnv> env;
325};
326
327/////////////////////////////////////////////////////////////////
328
329TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) {
330 SetExpectations();
331 RunTest();
332}
333
334INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool());
335
336/////////////////////////////////////////////////////////////////
337
338class NativeLoaderTest_Create : public NativeLoaderTest {
339 protected:
340 // Test inputs (initialized to the default values). Overriding these
341 // must be done before calling SetExpectations() and RunTest().
342 uint32_t target_sdk_version = 29;
343 std::string class_loader = "my_classloader";
344 bool is_shared = false;
345 std::string dex_path = "/data/app/foo/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000346 std::string library_path = "/data/app/foo/" LIB_DIR "/arm";
347 std::string permitted_path = "/data/app/foo/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100348
349 // expected output (.. for the default test inputs)
350 std::string expected_namespace_name = "classloader-namespace";
351 uint64_t expected_namespace_flags =
352 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
353 std::string expected_library_path = library_path;
354 std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900355 std::string expected_parent_namespace = "system";
Orion Hodson9b16e342019-10-09 13:29:16 +0100356 bool expected_link_with_platform_ns = true;
357 bool expected_link_with_art_ns = true;
Victor Changd20e51d2020-05-05 16:01:19 +0100358 bool expected_link_with_i18n_ns = true;
Orion Hodson9b16e342019-10-09 13:29:16 +0100359 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
360 bool expected_link_with_vndk_ns = false;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900361 bool expected_link_with_vndk_product_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100362 bool expected_link_with_default_ns = false;
363 bool expected_link_with_neuralnetworks_ns = true;
Jeffrey Huang52575032020-02-11 17:33:45 -0800364 bool expected_link_with_statsd_ns = true;
Orion Hodson9b16e342019-10-09 13:29:16 +0100365 std::string expected_shared_libs_to_platform_ns = default_public_libraries();
366 std::string expected_shared_libs_to_art_ns = art_public_libraries();
Victor Changd20e51d2020-05-05 16:01:19 +0100367 std::string expected_shared_libs_to_i18n_ns = i18n_public_libraries();
Orion Hodson9b16e342019-10-09 13:29:16 +0100368 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900369 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
370 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
Orion Hodson9b16e342019-10-09 13:29:16 +0100371 std::string expected_shared_libs_to_default_ns = default_public_libraries();
372 std::string expected_shared_libs_to_neuralnetworks_ns = neuralnetworks_public_libraries();
Jeffrey Huang52575032020-02-11 17:33:45 -0800373 std::string expected_shared_libs_to_statsd_ns = statsd_public_libraries();
Orion Hodson9b16e342019-10-09 13:29:16 +0100374
375 void SetExpectations() {
376 NativeLoaderTest::SetExpectations();
377
378 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
379
Martin Stjernholm48297332019-11-12 21:21:32 +0000380 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
381 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
Orion Hodson9b16e342019-10-09 13:29:16 +0100382
383 EXPECT_CALL(*mock, mock_create_namespace(
384 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
385 StrEq(expected_library_path), expected_namespace_flags,
386 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
387 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
388 if (expected_link_with_platform_ns) {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900389 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100390 StrEq(expected_shared_libs_to_platform_ns)))
391 .WillOnce(Return(true));
392 }
393 if (expected_link_with_art_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900394 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100395 StrEq(expected_shared_libs_to_art_ns)))
396 .WillOnce(Return(true));
397 }
Victor Changd20e51d2020-05-05 16:01:19 +0100398 if (expected_link_with_i18n_ns) {
399 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
400 StrEq(expected_shared_libs_to_i18n_ns)))
401 .WillOnce(Return(true));
402 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100403 if (expected_link_with_sphal_ns) {
404 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
405 StrEq(expected_shared_libs_to_sphal_ns)))
406 .WillOnce(Return(true));
407 }
408 if (expected_link_with_vndk_ns) {
409 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
410 StrEq(expected_shared_libs_to_vndk_ns)))
411 .WillOnce(Return(true));
412 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900413 if (expected_link_with_vndk_product_ns) {
414 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
415 StrEq(expected_shared_libs_to_vndk_product_ns)))
416 .WillOnce(Return(true));
417 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100418 if (expected_link_with_default_ns) {
419 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
420 StrEq(expected_shared_libs_to_default_ns)))
421 .WillOnce(Return(true));
422 }
423 if (expected_link_with_neuralnetworks_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900424 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100425 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
426 .WillOnce(Return(true));
427 }
Jeffrey Huang52575032020-02-11 17:33:45 -0800428 if (expected_link_with_statsd_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900429 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_os_statsd"),
Jeffrey Huang52575032020-02-11 17:33:45 -0800430 StrEq(expected_shared_libs_to_statsd_ns)))
431 .WillOnce(Return(true));
432 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100433 }
434
435 void RunTest() {
436 NativeLoaderTest::RunTest();
437
438 jstring err = CreateClassLoaderNamespace(
439 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
440 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
441 env()->NewStringUTF(permitted_path.c_str()));
442
443 // no error
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100444 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100445
446 if (!IsBridged()) {
447 struct android_namespace_t* ns =
448 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
449
450 // The created namespace is for this apk
451 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
452 } else {
453 struct NativeLoaderNamespace* ns =
454 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
455
456 // The created namespace is for the this apk
457 EXPECT_STREQ(dex_path.c_str(),
458 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
459 }
460 }
461
462 JNIEnv* env() { return NativeLoaderTest::env.get(); }
463};
464
465TEST_P(NativeLoaderTest_Create, DownloadedApp) {
466 SetExpectations();
467 RunTest();
468}
469
470TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
471 dex_path = "/system/app/foo/foo.apk";
472 is_shared = true;
473
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100474 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100475 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
476 SetExpectations();
477 RunTest();
478}
479
480TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
481 dex_path = "/vendor/app/foo/foo.apk";
482 is_shared = true;
483
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100484 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100485 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
486 SetExpectations();
487 RunTest();
488}
489
490TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
491 dex_path = "/vendor/app/foo/foo.apk";
492 is_shared = false;
493
494 expected_namespace_name = "vendor-classloader-namespace";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000495 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
496 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100497 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900498 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_vendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100499 expected_link_with_vndk_ns = true;
500 SetExpectations();
501 RunTest();
502}
503
Justin Yun3db26d52019-12-16 14:09:39 +0900504TEST_P(NativeLoaderTest_Create, BundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100505 dex_path = "/product/app/foo/foo.apk";
506 is_shared = true;
507
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100508 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100509 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
510 SetExpectations();
511 RunTest();
512}
513
Justin Yun3db26d52019-12-16 14:09:39 +0900514TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100515 dex_path = "/product/app/foo/foo.apk";
516 is_shared = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100517
Justin Yun3db26d52019-12-16 14:09:39 +0900518 if (is_product_vndk_version_defined()) {
519 expected_namespace_name = "vendor-classloader-namespace";
520 expected_library_path = expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
521 expected_permitted_path =
522 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
523 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900524 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_product();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900525 expected_link_with_vndk_product_ns = true;
Justin Yun3db26d52019-12-16 14:09:39 +0900526 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100527 SetExpectations();
528 RunTest();
529}
530
531TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
532 if (IsBridged()) {
533 // There is no shared lib in translated arch
534 // TODO(jiyong): revisit this
535 return;
536 }
537 // compared to apks, for java shared libs, library_path is empty; java shared
538 // libs don't have their own native libs. They use platform's.
539 library_path = "";
540 expected_library_path = library_path;
541 // no ALSO_USED_AS_ANONYMOUS
542 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
543 SetExpectations();
544 RunTest();
545}
546
547TEST_P(NativeLoaderTest_Create, TwoApks) {
548 SetExpectations();
549 const uint32_t second_app_target_sdk_version = 29;
550 const std::string second_app_class_loader = "second_app_classloader";
551 const bool second_app_is_shared = false;
552 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000553 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
554 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100555 const std::string expected_second_app_permitted_path =
556 std::string("/data:/mnt/expand:") + second_app_permitted_path;
557 const std::string expected_second_app_parent_namespace = "classloader-namespace";
558 // no ALSO_USED_AS_ANONYMOUS
559 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
560
561 // The scenario is that second app is loaded by the first app.
562 // So the first app's classloader (`classloader`) is parent of the second
563 // app's classloader.
564 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
565 .WillByDefault(Return(class_loader.c_str()));
566
567 // namespace for the second app is created. Its parent is set to the namespace
568 // of the first app.
569 EXPECT_CALL(*mock, mock_create_namespace(
570 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
571 StrEq(second_app_library_path), expected_second_namespace_flags,
572 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
573 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
574 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
575 .WillRepeatedly(Return(true));
576
577 RunTest();
578 jstring err = CreateClassLoaderNamespace(
579 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
580 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
581 env()->NewStringUTF(second_app_library_path.c_str()),
582 env()->NewStringUTF(second_app_permitted_path.c_str()));
583
584 // success
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100585 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100586
587 if (!IsBridged()) {
588 struct android_namespace_t* ns =
589 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
590
591 // The created namespace is for the second apk
592 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
593 } else {
594 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
595 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
596
597 // The created namespace is for the second apk
598 EXPECT_STREQ(second_app_dex_path.c_str(),
599 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
600 }
601}
602
603INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
604
605const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
606 [](const struct ConfigEntry&) -> Result<bool> { return true; };
607
608TEST(NativeLoaderConfigParser, NamesAndComments) {
609 const char file_content[] = R"(
610######
611
612libA.so
613#libB.so
614
615
616 libC.so
617libD.so
618 #### libE.so
619)";
620 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
621 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900622 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100623 ASSERT_EQ(expected_result, *result);
624}
625
626TEST(NativeLoaderConfigParser, WithBitness) {
627 const char file_content[] = R"(
628libA.so 32
629libB.so 64
630libC.so
631)";
632#if defined(__LP64__)
633 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
634#else
635 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
636#endif
637 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900638 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100639 ASSERT_EQ(expected_result, *result);
640}
641
642TEST(NativeLoaderConfigParser, WithNoPreload) {
643 const char file_content[] = R"(
644libA.so nopreload
645libB.so nopreload
646libC.so
647)";
648
649 const std::vector<std::string> expected_result = {"libC.so"};
650 Result<std::vector<std::string>> result =
651 ParseConfig(file_content,
652 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900653 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100654 ASSERT_EQ(expected_result, *result);
655}
656
657TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) {
658 const char file_content[] = R"(
659libA.so nopreload 32
660libB.so 64 nopreload
661libC.so 32
662libD.so 64
663libE.so nopreload
664)";
665
666#if defined(__LP64__)
667 const std::vector<std::string> expected_result = {"libD.so"};
668#else
669 const std::vector<std::string> expected_result = {"libC.so"};
670#endif
671 Result<std::vector<std::string>> result =
672 ParseConfig(file_content,
673 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900674 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100675 ASSERT_EQ(expected_result, *result);
676}
677
678TEST(NativeLoaderConfigParser, RejectMalformed) {
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900679 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
680 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
681 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
682 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
683 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
684 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
Orion Hodson9b16e342019-10-09 13:29:16 +0100685}
686
Jooyung Han538f99a2020-03-03 00:46:50 +0900687TEST(NativeLoaderJniConfigParser, BasicLoading) {
688 const char file_content[] = R"(
689# comment
690com_android_foo libfoo.so
691# Empty line is ignored
692
693com_android_bar libbar.so:libbar2.so
694)";
695
696 std::map<std::string, std::string> expected_result{
697 {"com_android_foo", "libfoo.so"},
698 {"com_android_bar", "libbar.so:libbar2.so"},
699 };
700
701 Result<std::map<std::string, std::string>> result = ParseJniConfig(file_content);
702 ASSERT_RESULT_OK(result);
703 ASSERT_EQ(expected_result, *result);
704}
705
706TEST(NativeLoaderJniConfigParser, RejectMalformed) {
707 ASSERT_FALSE(ParseJniConfig("com_android_foo").ok());
708}
709
Orion Hodson9b16e342019-10-09 13:29:16 +0100710} // namespace nativeloader
711} // namespace android
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100712
713#endif // defined(ART_TARGET_ANDROID)