blob: 79e5070b01e58f759277924cef94c14a00dd6ba8 [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
Martin Stjernholm19d1feb2021-03-30 22:35:24 +010019#include "native_loader_test.h"
20
Orion Hodson9b16e342019-10-09 13:29:16 +010021#include <dlfcn.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010022
23#include <android-base/strings.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010024#include <gtest/gtest.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010025
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +010026#include "nativehelper/scoped_utf_chars.h"
Orion Hodson9b16e342019-10-09 13:29:16 +010027#include "nativeloader/native_loader.h"
28#include "public_libraries.h"
29
Orion Hodson9b16e342019-10-09 13:29:16 +010030namespace android {
31namespace nativeloader {
32
Martin Stjernholm48297332019-11-12 21:21:32 +000033using ::testing::Eq;
Martin Stjernholm26659432021-04-16 19:55:03 +010034using ::testing::NotNull;
Martin Stjernholm48297332019-11-12 21:21:32 +000035using ::testing::StrEq;
Martin Stjernholm48297332019-11-12 21:21:32 +000036using internal::ConfigEntry;
Calin Juravle91d2c5c2021-05-07 22:44:29 +000037using internal::ParseApexLibrariesConfig;
Martin Stjernholm26659432021-04-16 19:55:03 +010038using internal::ParseConfig;
Martin Stjernholm48297332019-11-12 21:21:32 +000039
Martin Stjernholmbe08b202019-11-12 20:11:00 +000040#if defined(__LP64__)
41#define LIB_DIR "lib64"
42#else
43#define LIB_DIR "lib"
44#endif
45
Orion Hodson9b16e342019-10-09 13:29:16 +010046static void* const any_nonnull = reinterpret_cast<void*>(0x12345678);
47
48// Custom matcher for comparing namespace handles
49MATCHER_P(NsEq, other, "") {
50 *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other;
51 return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0;
52}
53
54/////////////////////////////////////////////////////////////////
55
56// Test fixture
57class NativeLoaderTest : public ::testing::TestWithParam<bool> {
58 protected:
59 bool IsBridged() { return GetParam(); }
60
61 void SetUp() override {
Martin Stjernholm48297332019-11-12 21:21:32 +000062 mock = std::make_unique<testing::NiceMock<MockPlatform>>(IsBridged());
Orion Hodson9b16e342019-10-09 13:29:16 +010063
64 env = std::make_unique<JNIEnv>();
65 env->functions = CreateJNINativeInterface();
66 }
67
68 void SetExpectations() {
69 std::vector<std::string> default_public_libs =
70 android::base::Split(preloadable_public_libraries(), ":");
71 for (auto l : default_public_libs) {
Martin Stjernholm26659432021-04-16 19:55:03 +010072 EXPECT_CALL(*mock,
73 mock_dlopen_ext(false, StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE, NotNull()))
Orion Hodson9b16e342019-10-09 13:29:16 +010074 .WillOnce(Return(any_nonnull));
75 }
76 }
77
78 void RunTest() { InitializeNativeLoader(); }
79
80 void TearDown() override {
81 ResetNativeLoader();
82 delete env->functions;
83 mock.reset();
84 }
85
86 std::unique_ptr<JNIEnv> env;
87};
88
89/////////////////////////////////////////////////////////////////
90
91TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) {
92 SetExpectations();
93 RunTest();
94}
95
Martin Stjernholm26659432021-04-16 19:55:03 +010096TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInApex) {
97 const char* test_lib_path = "libfoo.so";
98 void* fake_handle = &fake_handle; // Arbitrary non-null value
99 EXPECT_CALL(*mock,
100 mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("com_android_art")))
101 .WillOnce(Return(fake_handle));
102
103 bool needs_native_bridge = false;
104 char* errmsg = nullptr;
105 EXPECT_EQ(fake_handle,
106 OpenNativeLibrary(env.get(),
107 /*target_sdk_version=*/17,
108 test_lib_path,
109 /*class_loader=*/nullptr,
110 /*caller_location=*/"/apex/com.android.art/javalib/myloadinglib.jar",
111 /*library_path=*/nullptr,
112 &needs_native_bridge,
113 &errmsg));
114 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
115 // should maybe change.
116 EXPECT_EQ(needs_native_bridge, false);
117 EXPECT_EQ(errmsg, nullptr);
118}
119
120TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInFramework) {
121 const char* test_lib_path = "libfoo.so";
122 void* fake_handle = &fake_handle; // Arbitrary non-null value
123 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
124 .WillOnce(Return(fake_handle));
125
126 bool needs_native_bridge = false;
127 char* errmsg = nullptr;
128 EXPECT_EQ(fake_handle,
129 OpenNativeLibrary(env.get(),
130 /*target_sdk_version=*/17,
131 test_lib_path,
132 /*class_loader=*/nullptr,
133 /*caller_location=*/"/system/framework/framework.jar!classes1.dex",
134 /*library_path=*/nullptr,
135 &needs_native_bridge,
136 &errmsg));
137 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
138 // should maybe change.
139 EXPECT_EQ(needs_native_bridge, false);
140 EXPECT_EQ(errmsg, nullptr);
141}
142
143TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderAndCallerLocation) {
144 const char* test_lib_path = "libfoo.so";
145 void* fake_handle = &fake_handle; // Arbitrary non-null value
146 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
147 .WillOnce(Return(fake_handle));
148
149 bool needs_native_bridge = false;
150 char* errmsg = nullptr;
151 EXPECT_EQ(fake_handle,
152 OpenNativeLibrary(env.get(),
153 /*target_sdk_version=*/17,
154 test_lib_path,
155 /*class_loader=*/nullptr,
156 /*caller_location=*/nullptr,
157 /*library_path=*/nullptr,
158 &needs_native_bridge,
159 &errmsg));
160 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
161 // should maybe change.
162 EXPECT_EQ(needs_native_bridge, false);
163 EXPECT_EQ(errmsg, nullptr);
164}
165
Orion Hodson9b16e342019-10-09 13:29:16 +0100166INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool());
167
168/////////////////////////////////////////////////////////////////
169
170class NativeLoaderTest_Create : public NativeLoaderTest {
171 protected:
172 // Test inputs (initialized to the default values). Overriding these
173 // must be done before calling SetExpectations() and RunTest().
174 uint32_t target_sdk_version = 29;
175 std::string class_loader = "my_classloader";
176 bool is_shared = false;
177 std::string dex_path = "/data/app/foo/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000178 std::string library_path = "/data/app/foo/" LIB_DIR "/arm";
179 std::string permitted_path = "/data/app/foo/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100180
181 // expected output (.. for the default test inputs)
182 std::string expected_namespace_name = "classloader-namespace";
183 uint64_t expected_namespace_flags =
184 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
185 std::string expected_library_path = library_path;
186 std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900187 std::string expected_parent_namespace = "system";
Orion Hodson9b16e342019-10-09 13:29:16 +0100188 bool expected_link_with_platform_ns = true;
189 bool expected_link_with_art_ns = true;
Victor Changd20e51d2020-05-05 16:01:19 +0100190 bool expected_link_with_i18n_ns = true;
Orion Hodson9b16e342019-10-09 13:29:16 +0100191 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
192 bool expected_link_with_vndk_ns = false;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900193 bool expected_link_with_vndk_product_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100194 bool expected_link_with_default_ns = false;
195 bool expected_link_with_neuralnetworks_ns = true;
196 std::string expected_shared_libs_to_platform_ns = default_public_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900197 std::string expected_shared_libs_to_art_ns = apex_public_libraries().at("com_android_art");
198 std::string expected_shared_libs_to_i18n_ns = apex_public_libraries().at("com_android_i18n");
Orion Hodson9b16e342019-10-09 13:29:16 +0100199 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900200 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
201 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
Orion Hodson9b16e342019-10-09 13:29:16 +0100202 std::string expected_shared_libs_to_default_ns = default_public_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900203 std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks");
Orion Hodson9b16e342019-10-09 13:29:16 +0100204
205 void SetExpectations() {
206 NativeLoaderTest::SetExpectations();
207
208 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
209
Martin Stjernholm48297332019-11-12 21:21:32 +0000210 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
211 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
Orion Hodson9b16e342019-10-09 13:29:16 +0100212
213 EXPECT_CALL(*mock, mock_create_namespace(
214 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
215 StrEq(expected_library_path), expected_namespace_flags,
216 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
217 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
218 if (expected_link_with_platform_ns) {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900219 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100220 StrEq(expected_shared_libs_to_platform_ns)))
221 .WillOnce(Return(true));
222 }
223 if (expected_link_with_art_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900224 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100225 StrEq(expected_shared_libs_to_art_ns)))
226 .WillOnce(Return(true));
227 }
Victor Changd20e51d2020-05-05 16:01:19 +0100228 if (expected_link_with_i18n_ns) {
229 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
230 StrEq(expected_shared_libs_to_i18n_ns)))
231 .WillOnce(Return(true));
232 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100233 if (expected_link_with_sphal_ns) {
234 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
235 StrEq(expected_shared_libs_to_sphal_ns)))
236 .WillOnce(Return(true));
237 }
238 if (expected_link_with_vndk_ns) {
239 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
240 StrEq(expected_shared_libs_to_vndk_ns)))
241 .WillOnce(Return(true));
242 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900243 if (expected_link_with_vndk_product_ns) {
244 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
245 StrEq(expected_shared_libs_to_vndk_product_ns)))
246 .WillOnce(Return(true));
247 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100248 if (expected_link_with_default_ns) {
249 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
250 StrEq(expected_shared_libs_to_default_ns)))
251 .WillOnce(Return(true));
252 }
253 if (expected_link_with_neuralnetworks_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900254 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100255 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
256 .WillOnce(Return(true));
257 }
258 }
259
260 void RunTest() {
261 NativeLoaderTest::RunTest();
262
263 jstring err = CreateClassLoaderNamespace(
264 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
265 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100266 env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100267
268 // no error
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100269 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100270
271 if (!IsBridged()) {
272 struct android_namespace_t* ns =
273 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
274
275 // The created namespace is for this apk
276 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
277 } else {
278 struct NativeLoaderNamespace* ns =
279 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
280
281 // The created namespace is for the this apk
282 EXPECT_STREQ(dex_path.c_str(),
283 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
284 }
285 }
286
287 JNIEnv* env() { return NativeLoaderTest::env.get(); }
288};
289
290TEST_P(NativeLoaderTest_Create, DownloadedApp) {
291 SetExpectations();
292 RunTest();
293}
294
295TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
296 dex_path = "/system/app/foo/foo.apk";
297 is_shared = true;
298
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100299 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100300 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
301 SetExpectations();
302 RunTest();
303}
304
305TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
306 dex_path = "/vendor/app/foo/foo.apk";
307 is_shared = true;
308
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100309 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100310 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
311 SetExpectations();
312 RunTest();
313}
314
315TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
316 dex_path = "/vendor/app/foo/foo.apk";
317 is_shared = false;
318
319 expected_namespace_name = "vendor-classloader-namespace";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000320 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
321 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100322 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900323 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_vendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100324 expected_link_with_vndk_ns = true;
325 SetExpectations();
326 RunTest();
327}
328
Justin Yun3db26d52019-12-16 14:09:39 +0900329TEST_P(NativeLoaderTest_Create, BundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100330 dex_path = "/product/app/foo/foo.apk";
331 is_shared = true;
332
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100333 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100334 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
335 SetExpectations();
336 RunTest();
337}
338
Justin Yun3db26d52019-12-16 14:09:39 +0900339TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100340 dex_path = "/product/app/foo/foo.apk";
341 is_shared = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100342
Justin Yun3db26d52019-12-16 14:09:39 +0900343 if (is_product_vndk_version_defined()) {
344 expected_namespace_name = "vendor-classloader-namespace";
345 expected_library_path = expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
346 expected_permitted_path =
347 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
348 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900349 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_product();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900350 expected_link_with_vndk_product_ns = true;
Justin Yun3db26d52019-12-16 14:09:39 +0900351 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100352 SetExpectations();
353 RunTest();
354}
355
356TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
357 if (IsBridged()) {
358 // There is no shared lib in translated arch
359 // TODO(jiyong): revisit this
360 return;
361 }
362 // compared to apks, for java shared libs, library_path is empty; java shared
363 // libs don't have their own native libs. They use platform's.
364 library_path = "";
365 expected_library_path = library_path;
366 // no ALSO_USED_AS_ANONYMOUS
367 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
368 SetExpectations();
369 RunTest();
370}
371
372TEST_P(NativeLoaderTest_Create, TwoApks) {
373 SetExpectations();
374 const uint32_t second_app_target_sdk_version = 29;
375 const std::string second_app_class_loader = "second_app_classloader";
376 const bool second_app_is_shared = false;
377 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000378 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
379 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100380 const std::string expected_second_app_permitted_path =
381 std::string("/data:/mnt/expand:") + second_app_permitted_path;
382 const std::string expected_second_app_parent_namespace = "classloader-namespace";
383 // no ALSO_USED_AS_ANONYMOUS
384 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
385
386 // The scenario is that second app is loaded by the first app.
387 // So the first app's classloader (`classloader`) is parent of the second
388 // app's classloader.
389 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
390 .WillByDefault(Return(class_loader.c_str()));
391
392 // namespace for the second app is created. Its parent is set to the namespace
393 // of the first app.
394 EXPECT_CALL(*mock, mock_create_namespace(
395 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
396 StrEq(second_app_library_path), expected_second_namespace_flags,
397 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
398 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
399 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
400 .WillRepeatedly(Return(true));
401
402 RunTest();
403 jstring err = CreateClassLoaderNamespace(
404 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
405 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
406 env()->NewStringUTF(second_app_library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100407 env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100408
409 // success
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100410 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100411
412 if (!IsBridged()) {
413 struct android_namespace_t* ns =
414 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
415
416 // The created namespace is for the second apk
417 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
418 } else {
419 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
420 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
421
422 // The created namespace is for the second apk
423 EXPECT_STREQ(second_app_dex_path.c_str(),
424 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
425 }
426}
427
428INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
429
430const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
431 [](const struct ConfigEntry&) -> Result<bool> { return true; };
432
433TEST(NativeLoaderConfigParser, NamesAndComments) {
434 const char file_content[] = R"(
435######
436
437libA.so
438#libB.so
439
440
441 libC.so
442libD.so
443 #### libE.so
444)";
445 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
446 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900447 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100448 ASSERT_EQ(expected_result, *result);
449}
450
451TEST(NativeLoaderConfigParser, WithBitness) {
452 const char file_content[] = R"(
453libA.so 32
454libB.so 64
455libC.so
456)";
457#if defined(__LP64__)
458 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
459#else
460 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
461#endif
462 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900463 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100464 ASSERT_EQ(expected_result, *result);
465}
466
467TEST(NativeLoaderConfigParser, WithNoPreload) {
468 const char file_content[] = R"(
469libA.so nopreload
470libB.so nopreload
471libC.so
472)";
473
474 const std::vector<std::string> expected_result = {"libC.so"};
475 Result<std::vector<std::string>> result =
476 ParseConfig(file_content,
477 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900478 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100479 ASSERT_EQ(expected_result, *result);
480}
481
482TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) {
483 const char file_content[] = R"(
484libA.so nopreload 32
485libB.so 64 nopreload
486libC.so 32
487libD.so 64
488libE.so nopreload
489)";
490
491#if defined(__LP64__)
492 const std::vector<std::string> expected_result = {"libD.so"};
493#else
494 const std::vector<std::string> expected_result = {"libC.so"};
495#endif
496 Result<std::vector<std::string>> result =
497 ParseConfig(file_content,
498 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900499 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100500 ASSERT_EQ(expected_result, *result);
501}
502
503TEST(NativeLoaderConfigParser, RejectMalformed) {
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900504 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
505 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
506 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
507 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
508 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
509 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
Orion Hodson9b16e342019-10-09 13:29:16 +0100510}
511
Jooyung Hancd616d02020-09-01 14:53:23 +0900512TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900513 const char file_content[] = R"(
514# comment
Jooyung Hancd616d02020-09-01 14:53:23 +0900515jni com_android_foo libfoo.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900516# Empty line is ignored
517
Jooyung Hancd616d02020-09-01 14:53:23 +0900518jni com_android_bar libbar.so:libbar2.so
519
520 public com_android_bar libpublic.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900521)";
522
Jooyung Hancd616d02020-09-01 14:53:23 +0900523 auto jni_libs = ParseApexLibrariesConfig(file_content, "jni");
524 ASSERT_RESULT_OK(jni_libs);
525 std::map<std::string, std::string> expected_jni_libs {
Jooyung Han538f99a2020-03-03 00:46:50 +0900526 {"com_android_foo", "libfoo.so"},
527 {"com_android_bar", "libbar.so:libbar2.so"},
528 };
Jooyung Hancd616d02020-09-01 14:53:23 +0900529 ASSERT_EQ(expected_jni_libs, *jni_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900530
Jooyung Hancd616d02020-09-01 14:53:23 +0900531 auto public_libs = ParseApexLibrariesConfig(file_content, "public");
532 ASSERT_RESULT_OK(public_libs);
533 std::map<std::string, std::string> expected_public_libs {
534 {"com_android_bar", "libpublic.so"},
535 };
536 ASSERT_EQ(expected_public_libs, *public_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900537}
538
Jooyung Hancd616d02020-09-01 14:53:23 +0900539TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) {
540 const char file_content[] = R"(
541jni com_android_foo libfoo
542# missing <library list>
543jni com_android_bar
544)";
545 auto result = ParseApexLibrariesConfig(file_content, "jni");
546 ASSERT_FALSE(result.ok());
547 ASSERT_EQ("Malformed line \"jni com_android_bar\"", result.error().message());
Jooyung Han538f99a2020-03-03 00:46:50 +0900548}
549
Jooyung Hancd616d02020-09-01 14:53:23 +0900550TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) {
551 const char file_content[] = R"(
552jni apex1 lib
553public apex2 lib
554# unknown tag
555unknown com_android_foo libfoo
556)";
557 auto result = ParseApexLibrariesConfig(file_content, "jni");
558 ASSERT_FALSE(result.ok());
559 ASSERT_EQ("Invalid tag \"unknown com_android_foo libfoo\"", result.error().message());
560}
561
562TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) {
563 const char file_content[] = R"(
564# apex linker namespace should be mangled ('.' -> '_')
565jni com.android.foo lib
566)";
567 auto result = ParseApexLibrariesConfig(file_content, "jni");
568 ASSERT_FALSE(result.ok());
569 ASSERT_EQ("Invalid apex_namespace \"jni com.android.foo lib\"", result.error().message());
570}
571
572TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) {
573 const char file_content[] = R"(
574# library list is ":" separated list of filenames
575jni com_android_foo lib64/libfoo.so
576)";
577 auto result = ParseApexLibrariesConfig(file_content, "jni");
578 ASSERT_FALSE(result.ok());
579 ASSERT_EQ("Invalid library_list \"jni com_android_foo lib64/libfoo.so\"", result.error().message());
580}
581
Orion Hodson9b16e342019-10-09 13:29:16 +0100582} // namespace nativeloader
583} // namespace android
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100584
585#endif // defined(ART_TARGET_ANDROID)