blob: b1a7e3538aeb1481da78ca737746a4d7980cfc56 [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
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000170std::string default_public_and_extended_libraries() {
171 std::string public_libs = default_public_libraries();
172 std::string ext_libs = extended_public_libraries();
173 if (!ext_libs.empty()) {
174 public_libs = public_libs + ":" + ext_libs;
175 }
176 return public_libs;
177}
178
Orion Hodson9b16e342019-10-09 13:29:16 +0100179class NativeLoaderTest_Create : public NativeLoaderTest {
180 protected:
181 // Test inputs (initialized to the default values). Overriding these
182 // must be done before calling SetExpectations() and RunTest().
183 uint32_t target_sdk_version = 29;
184 std::string class_loader = "my_classloader";
185 bool is_shared = false;
186 std::string dex_path = "/data/app/foo/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000187 std::string library_path = "/data/app/foo/" LIB_DIR "/arm";
188 std::string permitted_path = "/data/app/foo/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100189
190 // expected output (.. for the default test inputs)
191 std::string expected_namespace_name = "classloader-namespace";
192 uint64_t expected_namespace_flags =
193 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
194 std::string expected_library_path = library_path;
195 std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900196 std::string expected_parent_namespace = "system";
Orion Hodson9b16e342019-10-09 13:29:16 +0100197 bool expected_link_with_platform_ns = true;
198 bool expected_link_with_art_ns = true;
Victor Changd20e51d2020-05-05 16:01:19 +0100199 bool expected_link_with_i18n_ns = true;
Jooyung Han180e1e72021-06-20 17:54:38 +0900200 bool expected_link_with_conscrypt_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100201 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
Justin Yuna21b5842021-08-10 14:05:49 +0900202 bool expected_link_with_product_ns = !product_public_libraries().empty();
Orion Hodson9b16e342019-10-09 13:29:16 +0100203 bool expected_link_with_vndk_ns = false;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900204 bool expected_link_with_vndk_product_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100205 bool expected_link_with_default_ns = false;
206 bool expected_link_with_neuralnetworks_ns = true;
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000207 std::string expected_shared_libs_to_platform_ns = default_public_and_extended_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900208 std::string expected_shared_libs_to_art_ns = apex_public_libraries().at("com_android_art");
209 std::string expected_shared_libs_to_i18n_ns = apex_public_libraries().at("com_android_i18n");
Jooyung Han180e1e72021-06-20 17:54:38 +0900210 std::string expected_shared_libs_to_conscrypt_ns = apex_jni_libraries("com_android_conscrypt");
Orion Hodson9b16e342019-10-09 13:29:16 +0100211 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
Justin Yuna21b5842021-08-10 14:05:49 +0900212 std::string expected_shared_libs_to_product_ns = product_public_libraries();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900213 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
214 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000215 std::string expected_shared_libs_to_default_ns = default_public_and_extended_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900216 std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks");
Orion Hodson9b16e342019-10-09 13:29:16 +0100217
218 void SetExpectations() {
219 NativeLoaderTest::SetExpectations();
220
221 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
222
Martin Stjernholm48297332019-11-12 21:21:32 +0000223 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
224 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
Orion Hodson9b16e342019-10-09 13:29:16 +0100225
226 EXPECT_CALL(*mock, mock_create_namespace(
227 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
228 StrEq(expected_library_path), expected_namespace_flags,
229 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
230 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
231 if (expected_link_with_platform_ns) {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900232 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100233 StrEq(expected_shared_libs_to_platform_ns)))
234 .WillOnce(Return(true));
235 }
236 if (expected_link_with_art_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900237 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100238 StrEq(expected_shared_libs_to_art_ns)))
239 .WillOnce(Return(true));
240 }
Victor Changd20e51d2020-05-05 16:01:19 +0100241 if (expected_link_with_i18n_ns) {
242 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
243 StrEq(expected_shared_libs_to_i18n_ns)))
244 .WillOnce(Return(true));
245 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100246 if (expected_link_with_sphal_ns) {
247 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
248 StrEq(expected_shared_libs_to_sphal_ns)))
249 .WillOnce(Return(true));
250 }
Justin Yuna21b5842021-08-10 14:05:49 +0900251 if (expected_link_with_product_ns) {
252 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("product"),
253 StrEq(expected_shared_libs_to_product_ns)))
254 .WillOnce(Return(true));
255 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100256 if (expected_link_with_vndk_ns) {
257 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
258 StrEq(expected_shared_libs_to_vndk_ns)))
259 .WillOnce(Return(true));
260 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900261 if (expected_link_with_vndk_product_ns) {
262 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
263 StrEq(expected_shared_libs_to_vndk_product_ns)))
264 .WillOnce(Return(true));
265 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100266 if (expected_link_with_default_ns) {
267 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
268 StrEq(expected_shared_libs_to_default_ns)))
269 .WillOnce(Return(true));
270 }
271 if (expected_link_with_neuralnetworks_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900272 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100273 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
274 .WillOnce(Return(true));
275 }
Jooyung Han180e1e72021-06-20 17:54:38 +0900276 if (expected_link_with_conscrypt_ns) {
277 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_conscrypt"),
278 StrEq(expected_shared_libs_to_conscrypt_ns)))
279 .WillOnce(Return(true));
280 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100281 }
282
283 void RunTest() {
284 NativeLoaderTest::RunTest();
285
286 jstring err = CreateClassLoaderNamespace(
287 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
288 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100289 env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100290
291 // no error
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100292 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100293
294 if (!IsBridged()) {
295 struct android_namespace_t* ns =
296 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
297
298 // The created namespace is for this apk
299 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
300 } else {
301 struct NativeLoaderNamespace* ns =
302 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
303
304 // The created namespace is for the this apk
305 EXPECT_STREQ(dex_path.c_str(),
306 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
307 }
308 }
309
310 JNIEnv* env() { return NativeLoaderTest::env.get(); }
311};
312
313TEST_P(NativeLoaderTest_Create, DownloadedApp) {
314 SetExpectations();
315 RunTest();
316}
317
318TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
319 dex_path = "/system/app/foo/foo.apk";
320 is_shared = true;
321
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100322 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100323 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
324 SetExpectations();
325 RunTest();
326}
327
328TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
329 dex_path = "/vendor/app/foo/foo.apk";
330 is_shared = true;
331
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100332 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100333 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
334 SetExpectations();
335 RunTest();
336}
337
338TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
339 dex_path = "/vendor/app/foo/foo.apk";
340 is_shared = false;
341
342 expected_namespace_name = "vendor-classloader-namespace";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000343 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
344 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100345 expected_shared_libs_to_platform_ns =
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000346 default_public_libraries() + ":" + llndk_libraries_vendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100347 expected_link_with_vndk_ns = true;
348 SetExpectations();
349 RunTest();
350}
351
Justin Yun3db26d52019-12-16 14:09:39 +0900352TEST_P(NativeLoaderTest_Create, BundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100353 dex_path = "/product/app/foo/foo.apk";
354 is_shared = true;
355
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100356 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100357 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
358 SetExpectations();
359 RunTest();
360}
361
Jooyung Han180e1e72021-06-20 17:54:38 +0900362TEST_P(NativeLoaderTest_Create, SystemServerWithApexJars) {
363 dex_path = "/system/framework/services.jar:/apex/com.android.conscrypt/javalib/service-foo.jar";
364 is_shared = true;
365
366 expected_namespace_name = "classloader-namespace-shared";
367 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
368 expected_link_with_conscrypt_ns = true;
369 SetExpectations();
370 RunTest();
371}
372
Justin Yun3db26d52019-12-16 14:09:39 +0900373TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100374 dex_path = "/product/app/foo/foo.apk";
375 is_shared = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100376
Justin Yun3db26d52019-12-16 14:09:39 +0900377 if (is_product_vndk_version_defined()) {
378 expected_namespace_name = "vendor-classloader-namespace";
379 expected_library_path = expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
380 expected_permitted_path =
381 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900382 expected_link_with_vndk_product_ns = true;
Martin Stjernholm43b3caa2022-09-02 21:33:04 +0100383
384 // The handling of extended libraries for product apps changed in the
385 // M-2022-10 release of the ART module (https://r.android.com/2194871).
386 // Since this test is in CTS for T, we need to accept both new and old
387 // behaviour, i.e. with and without the extended public libraries appended
388 // at the end. Skip the EXPECT_CALL in
389 // NativeLoaderTest_Create::SetExpectations and create a more lenient
390 // variant of it here.
391 expected_link_with_platform_ns = false;
392 expected_shared_libs_to_platform_ns =
393 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_product();
394 EXPECT_CALL(*mock,
395 mock_link_namespaces(Eq(IsBridged()),
396 _,
397 NsEq("system"),
398 ::testing::StartsWith(expected_shared_libs_to_platform_ns)))
399 .WillOnce(Return(true));
Justin Yun3db26d52019-12-16 14:09:39 +0900400 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100401 SetExpectations();
402 RunTest();
403}
404
405TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
406 if (IsBridged()) {
407 // There is no shared lib in translated arch
408 // TODO(jiyong): revisit this
409 return;
410 }
411 // compared to apks, for java shared libs, library_path is empty; java shared
412 // libs don't have their own native libs. They use platform's.
413 library_path = "";
414 expected_library_path = library_path;
415 // no ALSO_USED_AS_ANONYMOUS
416 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
417 SetExpectations();
418 RunTest();
419}
420
421TEST_P(NativeLoaderTest_Create, TwoApks) {
422 SetExpectations();
423 const uint32_t second_app_target_sdk_version = 29;
424 const std::string second_app_class_loader = "second_app_classloader";
425 const bool second_app_is_shared = false;
426 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000427 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
428 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100429 const std::string expected_second_app_permitted_path =
430 std::string("/data:/mnt/expand:") + second_app_permitted_path;
431 const std::string expected_second_app_parent_namespace = "classloader-namespace";
432 // no ALSO_USED_AS_ANONYMOUS
433 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
434
435 // The scenario is that second app is loaded by the first app.
436 // So the first app's classloader (`classloader`) is parent of the second
437 // app's classloader.
438 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
439 .WillByDefault(Return(class_loader.c_str()));
440
441 // namespace for the second app is created. Its parent is set to the namespace
442 // of the first app.
443 EXPECT_CALL(*mock, mock_create_namespace(
444 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
445 StrEq(second_app_library_path), expected_second_namespace_flags,
446 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
447 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
448 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
449 .WillRepeatedly(Return(true));
450
451 RunTest();
452 jstring err = CreateClassLoaderNamespace(
453 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
454 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
455 env()->NewStringUTF(second_app_library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100456 env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100457
458 // success
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100459 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100460
461 if (!IsBridged()) {
462 struct android_namespace_t* ns =
463 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
464
465 // The created namespace is for the second apk
466 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
467 } else {
468 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
469 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
470
471 // The created namespace is for the second apk
472 EXPECT_STREQ(second_app_dex_path.c_str(),
473 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
474 }
475}
476
477INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
478
479const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
480 [](const struct ConfigEntry&) -> Result<bool> { return true; };
481
482TEST(NativeLoaderConfigParser, NamesAndComments) {
483 const char file_content[] = R"(
484######
485
486libA.so
487#libB.so
488
489
490 libC.so
491libD.so
492 #### libE.so
493)";
494 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
495 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900496 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100497 ASSERT_EQ(expected_result, *result);
498}
499
500TEST(NativeLoaderConfigParser, WithBitness) {
501 const char file_content[] = R"(
502libA.so 32
503libB.so 64
504libC.so
505)";
506#if defined(__LP64__)
507 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
508#else
509 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
510#endif
511 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900512 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100513 ASSERT_EQ(expected_result, *result);
514}
515
516TEST(NativeLoaderConfigParser, WithNoPreload) {
517 const char file_content[] = R"(
518libA.so nopreload
519libB.so nopreload
520libC.so
521)";
522
523 const std::vector<std::string> expected_result = {"libC.so"};
524 Result<std::vector<std::string>> result =
525 ParseConfig(file_content,
526 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900527 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100528 ASSERT_EQ(expected_result, *result);
529}
530
531TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) {
532 const char file_content[] = R"(
533libA.so nopreload 32
534libB.so 64 nopreload
535libC.so 32
536libD.so 64
537libE.so nopreload
538)";
539
540#if defined(__LP64__)
541 const std::vector<std::string> expected_result = {"libD.so"};
542#else
543 const std::vector<std::string> expected_result = {"libC.so"};
544#endif
545 Result<std::vector<std::string>> result =
546 ParseConfig(file_content,
547 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900548 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100549 ASSERT_EQ(expected_result, *result);
550}
551
552TEST(NativeLoaderConfigParser, RejectMalformed) {
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900553 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
554 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
555 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
556 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
557 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
558 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
Orion Hodson9b16e342019-10-09 13:29:16 +0100559}
560
Jooyung Hancd616d02020-09-01 14:53:23 +0900561TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900562 const char file_content[] = R"(
563# comment
Jooyung Hancd616d02020-09-01 14:53:23 +0900564jni com_android_foo libfoo.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900565# Empty line is ignored
566
Jooyung Hancd616d02020-09-01 14:53:23 +0900567jni com_android_bar libbar.so:libbar2.so
568
569 public com_android_bar libpublic.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900570)";
571
Jooyung Hancd616d02020-09-01 14:53:23 +0900572 auto jni_libs = ParseApexLibrariesConfig(file_content, "jni");
573 ASSERT_RESULT_OK(jni_libs);
574 std::map<std::string, std::string> expected_jni_libs {
Jooyung Han538f99a2020-03-03 00:46:50 +0900575 {"com_android_foo", "libfoo.so"},
576 {"com_android_bar", "libbar.so:libbar2.so"},
577 };
Jooyung Hancd616d02020-09-01 14:53:23 +0900578 ASSERT_EQ(expected_jni_libs, *jni_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900579
Jooyung Hancd616d02020-09-01 14:53:23 +0900580 auto public_libs = ParseApexLibrariesConfig(file_content, "public");
581 ASSERT_RESULT_OK(public_libs);
582 std::map<std::string, std::string> expected_public_libs {
583 {"com_android_bar", "libpublic.so"},
584 };
585 ASSERT_EQ(expected_public_libs, *public_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900586}
587
Jooyung Hancd616d02020-09-01 14:53:23 +0900588TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) {
589 const char file_content[] = R"(
590jni com_android_foo libfoo
591# missing <library list>
592jni com_android_bar
593)";
594 auto result = ParseApexLibrariesConfig(file_content, "jni");
595 ASSERT_FALSE(result.ok());
596 ASSERT_EQ("Malformed line \"jni com_android_bar\"", result.error().message());
Jooyung Han538f99a2020-03-03 00:46:50 +0900597}
598
Jooyung Hancd616d02020-09-01 14:53:23 +0900599TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) {
600 const char file_content[] = R"(
601jni apex1 lib
602public apex2 lib
603# unknown tag
604unknown com_android_foo libfoo
605)";
606 auto result = ParseApexLibrariesConfig(file_content, "jni");
607 ASSERT_FALSE(result.ok());
608 ASSERT_EQ("Invalid tag \"unknown com_android_foo libfoo\"", result.error().message());
609}
610
611TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) {
612 const char file_content[] = R"(
613# apex linker namespace should be mangled ('.' -> '_')
614jni com.android.foo lib
615)";
616 auto result = ParseApexLibrariesConfig(file_content, "jni");
617 ASSERT_FALSE(result.ok());
618 ASSERT_EQ("Invalid apex_namespace \"jni com.android.foo lib\"", result.error().message());
619}
620
621TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) {
622 const char file_content[] = R"(
623# library list is ":" separated list of filenames
624jni com_android_foo lib64/libfoo.so
625)";
626 auto result = ParseApexLibrariesConfig(file_content, "jni");
627 ASSERT_FALSE(result.ok());
628 ASSERT_EQ("Invalid library_list \"jni com_android_foo lib64/libfoo.so\"", result.error().message());
629}
630
Orion Hodson9b16e342019-10-09 13:29:16 +0100631} // namespace nativeloader
632} // namespace android
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100633
634#endif // defined(ART_TARGET_ANDROID)