blob: 3b05aae06b044085996c26f0243a5a49f3e2bcd9 [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
Kiyoung Kim760e4952023-10-18 10:26:00 +090021#include <android-base/properties.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010022#include <android-base/strings.h>
Kiyoung Kim760e4952023-10-18 10:26:00 +090023#include <dlfcn.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 Stjernholme5f1d632022-11-16 15:28:12 +000035using ::testing::StartsWith;
Martin Stjernholm48297332019-11-12 21:21:32 +000036using ::testing::StrEq;
Stefano Cianciulli419e5a12022-07-04 14:38:21 +000037using internal::ConfigEntry; // NOLINT - ConfigEntry is actually used
Calin Juravle91d2c5c2021-05-07 22:44:29 +000038using internal::ParseApexLibrariesConfig;
Martin Stjernholm26659432021-04-16 19:55:03 +010039using internal::ParseConfig;
Martin Stjernholm48297332019-11-12 21:21:32 +000040
Martin Stjernholmbe08b202019-11-12 20:11:00 +000041#if defined(__LP64__)
42#define LIB_DIR "lib64"
43#else
44#define LIB_DIR "lib"
45#endif
46
Orion Hodson9b16e342019-10-09 13:29:16 +010047static void* const any_nonnull = reinterpret_cast<void*>(0x12345678);
48
49// Custom matcher for comparing namespace handles
50MATCHER_P(NsEq, other, "") {
51 *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other;
52 return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0;
53}
54
55/////////////////////////////////////////////////////////////////
56
57// Test fixture
58class NativeLoaderTest : public ::testing::TestWithParam<bool> {
59 protected:
60 bool IsBridged() { return GetParam(); }
61
62 void SetUp() override {
Martin Stjernholm48297332019-11-12 21:21:32 +000063 mock = std::make_unique<testing::NiceMock<MockPlatform>>(IsBridged());
Orion Hodson9b16e342019-10-09 13:29:16 +010064
65 env = std::make_unique<JNIEnv>();
66 env->functions = CreateJNINativeInterface();
67 }
68
69 void SetExpectations() {
70 std::vector<std::string> default_public_libs =
71 android::base::Split(preloadable_public_libraries(), ":");
Stefano Cianciulli944df152022-06-29 08:48:55 +000072 for (const std::string& l : default_public_libs) {
Martin Stjernholm26659432021-04-16 19:55:03 +010073 EXPECT_CALL(*mock,
74 mock_dlopen_ext(false, StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE, NotNull()))
Orion Hodson9b16e342019-10-09 13:29:16 +010075 .WillOnce(Return(any_nonnull));
76 }
77 }
78
79 void RunTest() { InitializeNativeLoader(); }
80
81 void TearDown() override {
82 ResetNativeLoader();
83 delete env->functions;
84 mock.reset();
85 }
86
87 std::unique_ptr<JNIEnv> env;
88};
89
90/////////////////////////////////////////////////////////////////
91
92TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) {
93 SetExpectations();
94 RunTest();
95}
96
Martin Stjernholm26659432021-04-16 19:55:03 +010097TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInApex) {
98 const char* test_lib_path = "libfoo.so";
99 void* fake_handle = &fake_handle; // Arbitrary non-null value
100 EXPECT_CALL(*mock,
101 mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("com_android_art")))
102 .WillOnce(Return(fake_handle));
103
104 bool needs_native_bridge = false;
105 char* errmsg = nullptr;
106 EXPECT_EQ(fake_handle,
107 OpenNativeLibrary(env.get(),
108 /*target_sdk_version=*/17,
109 test_lib_path,
110 /*class_loader=*/nullptr,
111 /*caller_location=*/"/apex/com.android.art/javalib/myloadinglib.jar",
112 /*library_path=*/nullptr,
113 &needs_native_bridge,
114 &errmsg));
115 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
116 // should maybe change.
117 EXPECT_EQ(needs_native_bridge, false);
118 EXPECT_EQ(errmsg, nullptr);
119}
120
121TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderInFramework) {
122 const char* test_lib_path = "libfoo.so";
123 void* fake_handle = &fake_handle; // Arbitrary non-null value
124 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
125 .WillOnce(Return(fake_handle));
126
127 bool needs_native_bridge = false;
128 char* errmsg = nullptr;
129 EXPECT_EQ(fake_handle,
130 OpenNativeLibrary(env.get(),
131 /*target_sdk_version=*/17,
132 test_lib_path,
133 /*class_loader=*/nullptr,
134 /*caller_location=*/"/system/framework/framework.jar!classes1.dex",
135 /*library_path=*/nullptr,
136 &needs_native_bridge,
137 &errmsg));
138 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
139 // should maybe change.
140 EXPECT_EQ(needs_native_bridge, false);
141 EXPECT_EQ(errmsg, nullptr);
142}
143
144TEST_P(NativeLoaderTest, OpenNativeLibraryWithoutClassloaderAndCallerLocation) {
145 const char* test_lib_path = "libfoo.so";
146 void* fake_handle = &fake_handle; // Arbitrary non-null value
147 EXPECT_CALL(*mock, mock_dlopen_ext(false, StrEq(test_lib_path), RTLD_NOW, NsEq("system")))
148 .WillOnce(Return(fake_handle));
149
150 bool needs_native_bridge = false;
151 char* errmsg = nullptr;
152 EXPECT_EQ(fake_handle,
153 OpenNativeLibrary(env.get(),
154 /*target_sdk_version=*/17,
155 test_lib_path,
156 /*class_loader=*/nullptr,
157 /*caller_location=*/nullptr,
158 /*library_path=*/nullptr,
159 &needs_native_bridge,
160 &errmsg));
161 // OpenNativeLibrary never uses nativebridge when there's no classloader. That
162 // should maybe change.
163 EXPECT_EQ(needs_native_bridge, false);
164 EXPECT_EQ(errmsg, nullptr);
165}
166
Orion Hodson9b16e342019-10-09 13:29:16 +0100167INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool());
168
169/////////////////////////////////////////////////////////////////
170
Martin Stjernholm5af718b2022-08-24 23:17:09 +0100171std::string append_extended_libraries(const std::string& libs) {
Stefano Cianciulli2be604b2022-06-07 08:27:24 +0000172 const std::string& ext_libs = extended_public_libraries();
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000173 if (!ext_libs.empty()) {
Martin Stjernholm5af718b2022-08-24 23:17:09 +0100174 return libs + ":" + ext_libs;
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000175 }
Martin Stjernholm5af718b2022-08-24 23:17:09 +0100176 return libs;
177}
178
179std::string default_public_and_extended_libraries() {
180 return append_extended_libraries(default_public_libraries());
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000181}
182
Orion Hodson9b16e342019-10-09 13:29:16 +0100183class NativeLoaderTest_Create : public NativeLoaderTest {
184 protected:
185 // Test inputs (initialized to the default values). Overriding these
186 // must be done before calling SetExpectations() and RunTest().
187 uint32_t target_sdk_version = 29;
188 std::string class_loader = "my_classloader";
189 bool is_shared = false;
190 std::string dex_path = "/data/app/foo/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000191 std::string library_path = "/data/app/foo/" LIB_DIR "/arm";
192 std::string permitted_path = "/data/app/foo/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100193
194 // expected output (.. for the default test inputs)
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000195 std::string expected_namespace_prefix = "clns";
Orion Hodson9b16e342019-10-09 13:29:16 +0100196 uint64_t expected_namespace_flags =
197 ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS;
198 std::string expected_library_path = library_path;
199 std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path;
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900200 std::string expected_parent_namespace = "system";
Orion Hodson9b16e342019-10-09 13:29:16 +0100201 bool expected_link_with_platform_ns = true;
202 bool expected_link_with_art_ns = true;
Victor Changd20e51d2020-05-05 16:01:19 +0100203 bool expected_link_with_i18n_ns = true;
Jooyung Han180e1e72021-06-20 17:54:38 +0900204 bool expected_link_with_conscrypt_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100205 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
Justin Yuna21b5842021-08-10 14:05:49 +0900206 bool expected_link_with_product_ns = !product_public_libraries().empty();
Orion Hodson9b16e342019-10-09 13:29:16 +0100207 bool expected_link_with_vndk_ns = false;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900208 bool expected_link_with_vndk_product_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100209 bool expected_link_with_default_ns = false;
210 bool expected_link_with_neuralnetworks_ns = true;
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000211 std::string expected_shared_libs_to_platform_ns = default_public_and_extended_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900212 std::string expected_shared_libs_to_art_ns = apex_public_libraries().at("com_android_art");
213 std::string expected_shared_libs_to_i18n_ns = apex_public_libraries().at("com_android_i18n");
Jooyung Han180e1e72021-06-20 17:54:38 +0900214 std::string expected_shared_libs_to_conscrypt_ns = apex_jni_libraries("com_android_conscrypt");
Orion Hodson9b16e342019-10-09 13:29:16 +0100215 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
Justin Yuna21b5842021-08-10 14:05:49 +0900216 std::string expected_shared_libs_to_product_ns = product_public_libraries();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900217 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
218 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000219 std::string expected_shared_libs_to_default_ns = default_public_and_extended_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900220 std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks");
Orion Hodson9b16e342019-10-09 13:29:16 +0100221
222 void SetExpectations() {
223 NativeLoaderTest::SetExpectations();
224
225 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
226
Martin Stjernholm48297332019-11-12 21:21:32 +0000227 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
228 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
Orion Hodson9b16e342019-10-09 13:29:16 +0100229
230 EXPECT_CALL(*mock, mock_create_namespace(
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000231 Eq(IsBridged()), StartsWith(expected_namespace_prefix + "-"), nullptr,
Orion Hodson9b16e342019-10-09 13:29:16 +0100232 StrEq(expected_library_path), expected_namespace_flags,
233 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
234 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
235 if (expected_link_with_platform_ns) {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900236 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100237 StrEq(expected_shared_libs_to_platform_ns)))
238 .WillOnce(Return(true));
239 }
240 if (expected_link_with_art_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900241 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100242 StrEq(expected_shared_libs_to_art_ns)))
243 .WillOnce(Return(true));
244 }
Victor Changd20e51d2020-05-05 16:01:19 +0100245 if (expected_link_with_i18n_ns) {
246 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
247 StrEq(expected_shared_libs_to_i18n_ns)))
248 .WillOnce(Return(true));
249 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100250 if (expected_link_with_sphal_ns) {
251 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
252 StrEq(expected_shared_libs_to_sphal_ns)))
253 .WillOnce(Return(true));
254 }
Justin Yuna21b5842021-08-10 14:05:49 +0900255 if (expected_link_with_product_ns) {
256 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("product"),
257 StrEq(expected_shared_libs_to_product_ns)))
258 .WillOnce(Return(true));
259 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100260 if (expected_link_with_vndk_ns) {
261 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
262 StrEq(expected_shared_libs_to_vndk_ns)))
263 .WillOnce(Return(true));
264 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900265 if (expected_link_with_vndk_product_ns) {
266 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
267 StrEq(expected_shared_libs_to_vndk_product_ns)))
268 .WillOnce(Return(true));
269 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100270 if (expected_link_with_default_ns) {
271 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
272 StrEq(expected_shared_libs_to_default_ns)))
273 .WillOnce(Return(true));
274 }
275 if (expected_link_with_neuralnetworks_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900276 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100277 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
278 .WillOnce(Return(true));
279 }
Jooyung Han180e1e72021-06-20 17:54:38 +0900280 if (expected_link_with_conscrypt_ns) {
281 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_conscrypt"),
282 StrEq(expected_shared_libs_to_conscrypt_ns)))
283 .WillOnce(Return(true));
284 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100285 }
286
287 void RunTest() {
288 NativeLoaderTest::RunTest();
289
290 jstring err = CreateClassLoaderNamespace(
291 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
292 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100293 env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100294
295 // no error
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100296 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100297
298 if (!IsBridged()) {
299 struct android_namespace_t* ns =
300 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
301
302 // The created namespace is for this apk
303 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
304 } else {
305 struct NativeLoaderNamespace* ns =
306 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
307
308 // The created namespace is for the this apk
309 EXPECT_STREQ(dex_path.c_str(),
310 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
311 }
312 }
313
314 JNIEnv* env() { return NativeLoaderTest::env.get(); }
315};
316
317TEST_P(NativeLoaderTest_Create, DownloadedApp) {
318 SetExpectations();
319 RunTest();
320}
321
322TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
323 dex_path = "/system/app/foo/foo.apk";
324 is_shared = true;
325
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000326 expected_namespace_prefix = "clns-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100327 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
328 SetExpectations();
329 RunTest();
330}
331
332TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
333 dex_path = "/vendor/app/foo/foo.apk";
334 is_shared = true;
335
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000336 expected_namespace_prefix = "clns-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100337 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
338 SetExpectations();
339 RunTest();
340}
341
342TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
343 dex_path = "/vendor/app/foo/foo.apk";
344 is_shared = false;
345
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000346 expected_namespace_prefix = "vendor-clns";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000347 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
348 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100349 expected_shared_libs_to_platform_ns =
Martin Stjernholm630cb4e2022-03-24 00:50:33 +0000350 default_public_libraries() + ":" + llndk_libraries_vendor();
Kiyoung Kimcd9c9842023-10-19 17:12:13 +0900351 if (android::base::GetProperty("ro.vndk.version", "") != "") {
352 expected_link_with_vndk_ns = true;
353 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100354 SetExpectations();
355 RunTest();
356}
357
Justin Yun3db26d52019-12-16 14:09:39 +0900358TEST_P(NativeLoaderTest_Create, BundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100359 dex_path = "/product/app/foo/foo.apk";
360 is_shared = true;
361
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000362 expected_namespace_prefix = "clns-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100363 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
364 SetExpectations();
365 RunTest();
366}
367
Jooyung Han180e1e72021-06-20 17:54:38 +0900368TEST_P(NativeLoaderTest_Create, SystemServerWithApexJars) {
369 dex_path = "/system/framework/services.jar:/apex/com.android.conscrypt/javalib/service-foo.jar";
370 is_shared = true;
371
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000372 expected_namespace_prefix = "clns-shared";
Jooyung Han180e1e72021-06-20 17:54:38 +0900373 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
374 expected_link_with_conscrypt_ns = true;
375 SetExpectations();
376 RunTest();
377}
378
Justin Yun3db26d52019-12-16 14:09:39 +0900379TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100380 dex_path = "/product/app/foo/foo.apk";
381 is_shared = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100382
Kiyoung Kim760e4952023-10-18 10:26:00 +0900383 if (is_product_treblelized()) {
384 expected_namespace_prefix = "product-clns";
385 expected_library_path =
386 expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
387 expected_permitted_path =
388 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
389 expected_shared_libs_to_platform_ns =
390 append_extended_libraries(default_public_libraries() + ":" + llndk_libraries_product());
Kiyoung Kimcd9c9842023-10-19 17:12:13 +0900391 if (android::base::GetProperty("ro.product.vndk.version", "") != "") {
392 expected_link_with_vndk_product_ns = true;
393 }
Kiyoung Kim760e4952023-10-18 10:26:00 +0900394 }
Kiyoung Kimc8c29532023-09-22 13:52:47 +0900395
Orion Hodson9b16e342019-10-09 13:29:16 +0100396 SetExpectations();
397 RunTest();
398}
399
400TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
401 if (IsBridged()) {
402 // There is no shared lib in translated arch
403 // TODO(jiyong): revisit this
404 return;
405 }
406 // compared to apks, for java shared libs, library_path is empty; java shared
407 // libs don't have their own native libs. They use platform's.
408 library_path = "";
409 expected_library_path = library_path;
410 // no ALSO_USED_AS_ANONYMOUS
411 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
412 SetExpectations();
413 RunTest();
414}
415
416TEST_P(NativeLoaderTest_Create, TwoApks) {
417 SetExpectations();
418 const uint32_t second_app_target_sdk_version = 29;
419 const std::string second_app_class_loader = "second_app_classloader";
420 const bool second_app_is_shared = false;
421 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000422 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
423 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100424 const std::string expected_second_app_permitted_path =
425 std::string("/data:/mnt/expand:") + second_app_permitted_path;
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000426 const std::string expected_second_app_parent_namespace = "clns";
Orion Hodson9b16e342019-10-09 13:29:16 +0100427 // no ALSO_USED_AS_ANONYMOUS
428 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
429
430 // The scenario is that second app is loaded by the first app.
431 // So the first app's classloader (`classloader`) is parent of the second
432 // app's classloader.
433 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
434 .WillByDefault(Return(class_loader.c_str()));
435
436 // namespace for the second app is created. Its parent is set to the namespace
437 // of the first app.
438 EXPECT_CALL(*mock, mock_create_namespace(
Martin Stjernholme5f1d632022-11-16 15:28:12 +0000439 Eq(IsBridged()), StartsWith(expected_namespace_prefix + "-"), nullptr,
Orion Hodson9b16e342019-10-09 13:29:16 +0100440 StrEq(second_app_library_path), expected_second_namespace_flags,
441 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
442 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
443 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
444 .WillRepeatedly(Return(true));
445
446 RunTest();
447 jstring err = CreateClassLoaderNamespace(
448 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
449 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
450 env()->NewStringUTF(second_app_library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100451 env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100452
453 // success
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100454 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100455
456 if (!IsBridged()) {
457 struct android_namespace_t* ns =
458 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
459
460 // The created namespace is for the second apk
461 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
462 } else {
463 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
464 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
465
466 // The created namespace is for the second apk
467 EXPECT_STREQ(second_app_dex_path.c_str(),
468 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
469 }
470}
471
472INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
473
474const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
475 [](const struct ConfigEntry&) -> Result<bool> { return true; };
476
477TEST(NativeLoaderConfigParser, NamesAndComments) {
478 const char file_content[] = R"(
479######
480
481libA.so
482#libB.so
483
484
485 libC.so
486libD.so
487 #### libE.so
488)";
489 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
490 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900491 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100492 ASSERT_EQ(expected_result, *result);
493}
494
495TEST(NativeLoaderConfigParser, WithBitness) {
496 const char file_content[] = R"(
497libA.so 32
498libB.so 64
499libC.so
500)";
501#if defined(__LP64__)
502 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
503#else
504 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
505#endif
506 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900507 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100508 ASSERT_EQ(expected_result, *result);
509}
510
511TEST(NativeLoaderConfigParser, WithNoPreload) {
512 const char file_content[] = R"(
513libA.so nopreload
514libB.so nopreload
515libC.so
516)";
517
518 const std::vector<std::string> expected_result = {"libC.so"};
519 Result<std::vector<std::string>> result =
520 ParseConfig(file_content,
521 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900522 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100523 ASSERT_EQ(expected_result, *result);
524}
525
526TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) {
527 const char file_content[] = R"(
528libA.so nopreload 32
529libB.so 64 nopreload
530libC.so 32
531libD.so 64
532libE.so nopreload
533)";
534
535#if defined(__LP64__)
536 const std::vector<std::string> expected_result = {"libD.so"};
537#else
538 const std::vector<std::string> expected_result = {"libC.so"};
539#endif
540 Result<std::vector<std::string>> result =
541 ParseConfig(file_content,
542 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900543 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100544 ASSERT_EQ(expected_result, *result);
545}
546
547TEST(NativeLoaderConfigParser, RejectMalformed) {
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900548 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
549 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
550 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
551 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
552 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
553 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
Orion Hodson9b16e342019-10-09 13:29:16 +0100554}
555
Jooyung Hancd616d02020-09-01 14:53:23 +0900556TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900557 const char file_content[] = R"(
558# comment
Jooyung Hancd616d02020-09-01 14:53:23 +0900559jni com_android_foo libfoo.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900560# Empty line is ignored
561
Jooyung Hancd616d02020-09-01 14:53:23 +0900562jni com_android_bar libbar.so:libbar2.so
563
564 public com_android_bar libpublic.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900565)";
566
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000567 Result<std::map<std::string, std::string>> jni_libs =
568 ParseApexLibrariesConfig(file_content, "jni");
Jooyung Hancd616d02020-09-01 14:53:23 +0900569 ASSERT_RESULT_OK(jni_libs);
570 std::map<std::string, std::string> expected_jni_libs {
Jooyung Han538f99a2020-03-03 00:46:50 +0900571 {"com_android_foo", "libfoo.so"},
572 {"com_android_bar", "libbar.so:libbar2.so"},
573 };
Jooyung Hancd616d02020-09-01 14:53:23 +0900574 ASSERT_EQ(expected_jni_libs, *jni_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900575
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000576 Result<std::map<std::string, std::string>> public_libs =
577 ParseApexLibrariesConfig(file_content, "public");
Jooyung Hancd616d02020-09-01 14:53:23 +0900578 ASSERT_RESULT_OK(public_libs);
579 std::map<std::string, std::string> expected_public_libs {
580 {"com_android_bar", "libpublic.so"},
581 };
582 ASSERT_EQ(expected_public_libs, *public_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900583}
584
Jooyung Hancd616d02020-09-01 14:53:23 +0900585TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) {
586 const char file_content[] = R"(
587jni com_android_foo libfoo
588# missing <library list>
589jni com_android_bar
590)";
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000591 Result<std::map<std::string, std::string>> result = ParseApexLibrariesConfig(file_content, "jni");
Jooyung Hancd616d02020-09-01 14:53:23 +0900592 ASSERT_FALSE(result.ok());
593 ASSERT_EQ("Malformed line \"jni com_android_bar\"", result.error().message());
Jooyung Han538f99a2020-03-03 00:46:50 +0900594}
595
Jooyung Hancd616d02020-09-01 14:53:23 +0900596TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) {
597 const char file_content[] = R"(
598jni apex1 lib
599public apex2 lib
600# unknown tag
601unknown com_android_foo libfoo
602)";
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000603 Result<std::map<std::string, std::string>> result = ParseApexLibrariesConfig(file_content, "jni");
Jooyung Hancd616d02020-09-01 14:53:23 +0900604 ASSERT_FALSE(result.ok());
605 ASSERT_EQ("Invalid tag \"unknown com_android_foo libfoo\"", result.error().message());
606}
607
608TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) {
609 const char file_content[] = R"(
610# apex linker namespace should be mangled ('.' -> '_')
611jni com.android.foo lib
612)";
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000613 Result<std::map<std::string, std::string>> result = ParseApexLibrariesConfig(file_content, "jni");
Jooyung Hancd616d02020-09-01 14:53:23 +0900614 ASSERT_FALSE(result.ok());
615 ASSERT_EQ("Invalid apex_namespace \"jni com.android.foo lib\"", result.error().message());
616}
617
618TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) {
619 const char file_content[] = R"(
620# library list is ":" separated list of filenames
621jni com_android_foo lib64/libfoo.so
622)";
Martin Stjernholm8a9b51e2024-01-30 21:33:09 +0000623 Result<std::map<std::string, std::string>> result = ParseApexLibrariesConfig(file_content, "jni");
Jooyung Hancd616d02020-09-01 14:53:23 +0900624 ASSERT_FALSE(result.ok());
625 ASSERT_EQ("Invalid library_list \"jni com_android_foo lib64/libfoo.so\"", result.error().message());
626}
627
Orion Hodson9b16e342019-10-09 13:29:16 +0100628} // namespace nativeloader
629} // namespace android
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100630
631#endif // defined(ART_TARGET_ANDROID)