blob: c1dd4aaf1d417bd6dd14190e6318102d8c3d914a [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 Stjernholm8d817952021-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;
Jooyung Han8e4a3bc2021-06-20 17:54:38 +0900191 bool expected_link_with_conscrypt_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100192 bool expected_link_with_sphal_ns = !vendor_public_libraries().empty();
193 bool expected_link_with_vndk_ns = false;
Justin Yuneb4f08c2020-02-18 11:29:07 +0900194 bool expected_link_with_vndk_product_ns = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100195 bool expected_link_with_default_ns = false;
196 bool expected_link_with_neuralnetworks_ns = true;
197 std::string expected_shared_libs_to_platform_ns = default_public_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900198 std::string expected_shared_libs_to_art_ns = apex_public_libraries().at("com_android_art");
199 std::string expected_shared_libs_to_i18n_ns = apex_public_libraries().at("com_android_i18n");
Jooyung Han8e4a3bc2021-06-20 17:54:38 +0900200 std::string expected_shared_libs_to_conscrypt_ns = apex_jni_libraries("com_android_conscrypt");
Orion Hodson9b16e342019-10-09 13:29:16 +0100201 std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900202 std::string expected_shared_libs_to_vndk_ns = vndksp_libraries_vendor();
203 std::string expected_shared_libs_to_vndk_product_ns = vndksp_libraries_product();
Orion Hodson9b16e342019-10-09 13:29:16 +0100204 std::string expected_shared_libs_to_default_ns = default_public_libraries();
Jooyung Hancd616d02020-09-01 14:53:23 +0900205 std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks");
Orion Hodson9b16e342019-10-09 13:29:16 +0100206
207 void SetExpectations() {
208 NativeLoaderTest::SetExpectations();
209
210 ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
211
Martin Stjernholm48297332019-11-12 21:21:32 +0000212 EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber());
213 EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber());
Orion Hodson9b16e342019-10-09 13:29:16 +0100214
215 EXPECT_CALL(*mock, mock_create_namespace(
216 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
217 StrEq(expected_library_path), expected_namespace_flags,
218 StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str())))
219 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str()))));
220 if (expected_link_with_platform_ns) {
Kiyoung Kim99c19ca2020-01-29 16:09:38 +0900221 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100222 StrEq(expected_shared_libs_to_platform_ns)))
223 .WillOnce(Return(true));
224 }
225 if (expected_link_with_art_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900226 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100227 StrEq(expected_shared_libs_to_art_ns)))
228 .WillOnce(Return(true));
229 }
Victor Changd20e51d2020-05-05 16:01:19 +0100230 if (expected_link_with_i18n_ns) {
231 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_i18n"),
232 StrEq(expected_shared_libs_to_i18n_ns)))
233 .WillOnce(Return(true));
234 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100235 if (expected_link_with_sphal_ns) {
236 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"),
237 StrEq(expected_shared_libs_to_sphal_ns)))
238 .WillOnce(Return(true));
239 }
240 if (expected_link_with_vndk_ns) {
241 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"),
242 StrEq(expected_shared_libs_to_vndk_ns)))
243 .WillOnce(Return(true));
244 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900245 if (expected_link_with_vndk_product_ns) {
246 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk_product"),
247 StrEq(expected_shared_libs_to_vndk_product_ns)))
248 .WillOnce(Return(true));
249 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100250 if (expected_link_with_default_ns) {
251 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"),
252 StrEq(expected_shared_libs_to_default_ns)))
253 .WillOnce(Return(true));
254 }
255 if (expected_link_with_neuralnetworks_ns) {
Kiyoung Kim272b36d2020-02-19 16:08:47 +0900256 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"),
Orion Hodson9b16e342019-10-09 13:29:16 +0100257 StrEq(expected_shared_libs_to_neuralnetworks_ns)))
258 .WillOnce(Return(true));
259 }
Jooyung Han8e4a3bc2021-06-20 17:54:38 +0900260 if (expected_link_with_conscrypt_ns) {
261 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_conscrypt"),
262 StrEq(expected_shared_libs_to_conscrypt_ns)))
263 .WillOnce(Return(true));
264 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100265 }
266
267 void RunTest() {
268 NativeLoaderTest::RunTest();
269
270 jstring err = CreateClassLoaderNamespace(
271 env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared,
272 env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100273 env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100274
275 // no error
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100276 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100277
278 if (!IsBridged()) {
279 struct android_namespace_t* ns =
280 FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
281
282 // The created namespace is for this apk
283 EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns));
284 } else {
285 struct NativeLoaderNamespace* ns =
286 FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str()));
287
288 // The created namespace is for the this apk
289 EXPECT_STREQ(dex_path.c_str(),
290 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
291 }
292 }
293
294 JNIEnv* env() { return NativeLoaderTest::env.get(); }
295};
296
297TEST_P(NativeLoaderTest_Create, DownloadedApp) {
298 SetExpectations();
299 RunTest();
300}
301
302TEST_P(NativeLoaderTest_Create, BundledSystemApp) {
303 dex_path = "/system/app/foo/foo.apk";
304 is_shared = true;
305
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100306 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100307 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
308 SetExpectations();
309 RunTest();
310}
311
312TEST_P(NativeLoaderTest_Create, BundledVendorApp) {
313 dex_path = "/vendor/app/foo/foo.apk";
314 is_shared = true;
315
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100316 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100317 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
318 SetExpectations();
319 RunTest();
320}
321
322TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) {
323 dex_path = "/vendor/app/foo/foo.apk";
324 is_shared = false;
325
326 expected_namespace_name = "vendor-classloader-namespace";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000327 expected_library_path = expected_library_path + ":/vendor/" LIB_DIR;
328 expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100329 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900330 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_vendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100331 expected_link_with_vndk_ns = true;
332 SetExpectations();
333 RunTest();
334}
335
Justin Yun3db26d52019-12-16 14:09:39 +0900336TEST_P(NativeLoaderTest_Create, BundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100337 dex_path = "/product/app/foo/foo.apk";
338 is_shared = true;
339
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100340 expected_namespace_name = "classloader-namespace-shared";
Orion Hodson9b16e342019-10-09 13:29:16 +0100341 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
342 SetExpectations();
343 RunTest();
344}
345
Jooyung Han8e4a3bc2021-06-20 17:54:38 +0900346TEST_P(NativeLoaderTest_Create, SystemServerWithApexJars) {
347 dex_path = "/system/framework/services.jar:/apex/com.android.conscrypt/javalib/service-foo.jar";
348 is_shared = true;
349
350 expected_namespace_name = "classloader-namespace-shared";
351 expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED;
352 expected_link_with_conscrypt_ns = true;
353 SetExpectations();
354 RunTest();
355}
356
Justin Yun3db26d52019-12-16 14:09:39 +0900357TEST_P(NativeLoaderTest_Create, UnbundledProductApp) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100358 dex_path = "/product/app/foo/foo.apk";
359 is_shared = false;
Orion Hodson9b16e342019-10-09 13:29:16 +0100360
Justin Yun3db26d52019-12-16 14:09:39 +0900361 if (is_product_vndk_version_defined()) {
362 expected_namespace_name = "vendor-classloader-namespace";
363 expected_library_path = expected_library_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
364 expected_permitted_path =
365 expected_permitted_path + ":/product/" LIB_DIR ":/system/product/" LIB_DIR;
366 expected_shared_libs_to_platform_ns =
Justin Yun089c1352020-02-06 16:53:08 +0900367 expected_shared_libs_to_platform_ns + ":" + llndk_libraries_product();
Justin Yuneb4f08c2020-02-18 11:29:07 +0900368 expected_link_with_vndk_product_ns = true;
Justin Yun3db26d52019-12-16 14:09:39 +0900369 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100370 SetExpectations();
371 RunTest();
372}
373
374TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) {
375 if (IsBridged()) {
376 // There is no shared lib in translated arch
377 // TODO(jiyong): revisit this
378 return;
379 }
380 // compared to apks, for java shared libs, library_path is empty; java shared
381 // libs don't have their own native libs. They use platform's.
382 library_path = "";
383 expected_library_path = library_path;
384 // no ALSO_USED_AS_ANONYMOUS
385 expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
386 SetExpectations();
387 RunTest();
388}
389
390TEST_P(NativeLoaderTest_Create, TwoApks) {
391 SetExpectations();
392 const uint32_t second_app_target_sdk_version = 29;
393 const std::string second_app_class_loader = "second_app_classloader";
394 const bool second_app_is_shared = false;
395 const std::string second_app_dex_path = "/data/app/bar/classes.dex";
Martin Stjernholmbe08b202019-11-12 20:11:00 +0000396 const std::string second_app_library_path = "/data/app/bar/" LIB_DIR "/arm";
397 const std::string second_app_permitted_path = "/data/app/bar/" LIB_DIR;
Orion Hodson9b16e342019-10-09 13:29:16 +0100398 const std::string expected_second_app_permitted_path =
399 std::string("/data:/mnt/expand:") + second_app_permitted_path;
400 const std::string expected_second_app_parent_namespace = "classloader-namespace";
401 // no ALSO_USED_AS_ANONYMOUS
402 const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED;
403
404 // The scenario is that second app is loaded by the first app.
405 // So the first app's classloader (`classloader`) is parent of the second
406 // app's classloader.
407 ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader)))
408 .WillByDefault(Return(class_loader.c_str()));
409
410 // namespace for the second app is created. Its parent is set to the namespace
411 // of the first app.
412 EXPECT_CALL(*mock, mock_create_namespace(
413 Eq(IsBridged()), StrEq(expected_namespace_name), nullptr,
414 StrEq(second_app_library_path), expected_second_namespace_flags,
415 StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str())))
416 .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str()))));
417 EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _))
418 .WillRepeatedly(Return(true));
419
420 RunTest();
421 jstring err = CreateClassLoaderNamespace(
422 env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()),
423 second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()),
424 env()->NewStringUTF(second_app_library_path.c_str()),
Martin Stjernholmb3092402020-09-04 00:49:44 +0100425 env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr);
Orion Hodson9b16e342019-10-09 13:29:16 +0100426
427 // success
Martin Stjernholm94fd9ea2019-10-24 16:57:34 +0100428 EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100429
430 if (!IsBridged()) {
431 struct android_namespace_t* ns =
432 FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str()));
433
434 // The created namespace is for the second apk
435 EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns));
436 } else {
437 struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
438 env(), env()->NewStringUTF(second_app_class_loader.c_str()));
439
440 // The created namespace is for the second apk
441 EXPECT_STREQ(second_app_dex_path.c_str(),
442 reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace()));
443 }
444}
445
446INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool());
447
448const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
449 [](const struct ConfigEntry&) -> Result<bool> { return true; };
450
451TEST(NativeLoaderConfigParser, NamesAndComments) {
452 const char file_content[] = R"(
453######
454
455libA.so
456#libB.so
457
458
459 libC.so
460libD.so
461 #### libE.so
462)";
463 const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"};
464 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900465 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100466 ASSERT_EQ(expected_result, *result);
467}
468
469TEST(NativeLoaderConfigParser, WithBitness) {
470 const char file_content[] = R"(
471libA.so 32
472libB.so 64
473libC.so
474)";
475#if defined(__LP64__)
476 const std::vector<std::string> expected_result = {"libB.so", "libC.so"};
477#else
478 const std::vector<std::string> expected_result = {"libA.so", "libC.so"};
479#endif
480 Result<std::vector<std::string>> result = ParseConfig(file_content, always_true);
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900481 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100482 ASSERT_EQ(expected_result, *result);
483}
484
485TEST(NativeLoaderConfigParser, WithNoPreload) {
486 const char file_content[] = R"(
487libA.so nopreload
488libB.so nopreload
489libC.so
490)";
491
492 const std::vector<std::string> expected_result = {"libC.so"};
493 Result<std::vector<std::string>> result =
494 ParseConfig(file_content,
495 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
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, WithNoPreloadAndBitness) {
501 const char file_content[] = R"(
502libA.so nopreload 32
503libB.so 64 nopreload
504libC.so 32
505libD.so 64
506libE.so nopreload
507)";
508
509#if defined(__LP64__)
510 const std::vector<std::string> expected_result = {"libD.so"};
511#else
512 const std::vector<std::string> expected_result = {"libC.so"};
513#endif
514 Result<std::vector<std::string>> result =
515 ParseConfig(file_content,
516 [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; });
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900517 ASSERT_RESULT_OK(result);
Orion Hodson9b16e342019-10-09 13:29:16 +0100518 ASSERT_EQ(expected_result, *result);
519}
520
521TEST(NativeLoaderConfigParser, RejectMalformed) {
Bernie Innocentiac5ae3c2020-02-12 10:43:42 +0900522 ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true).ok());
523 ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true).ok());
524 ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true).ok());
525 ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true).ok());
526 ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true).ok());
527 ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true).ok());
Orion Hodson9b16e342019-10-09 13:29:16 +0100528}
529
Jooyung Hancd616d02020-09-01 14:53:23 +0900530TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900531 const char file_content[] = R"(
532# comment
Jooyung Hancd616d02020-09-01 14:53:23 +0900533jni com_android_foo libfoo.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900534# Empty line is ignored
535
Jooyung Hancd616d02020-09-01 14:53:23 +0900536jni com_android_bar libbar.so:libbar2.so
537
538 public com_android_bar libpublic.so
Jooyung Han538f99a2020-03-03 00:46:50 +0900539)";
540
Jooyung Hancd616d02020-09-01 14:53:23 +0900541 auto jni_libs = ParseApexLibrariesConfig(file_content, "jni");
542 ASSERT_RESULT_OK(jni_libs);
543 std::map<std::string, std::string> expected_jni_libs {
Jooyung Han538f99a2020-03-03 00:46:50 +0900544 {"com_android_foo", "libfoo.so"},
545 {"com_android_bar", "libbar.so:libbar2.so"},
546 };
Jooyung Hancd616d02020-09-01 14:53:23 +0900547 ASSERT_EQ(expected_jni_libs, *jni_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900548
Jooyung Hancd616d02020-09-01 14:53:23 +0900549 auto public_libs = ParseApexLibrariesConfig(file_content, "public");
550 ASSERT_RESULT_OK(public_libs);
551 std::map<std::string, std::string> expected_public_libs {
552 {"com_android_bar", "libpublic.so"},
553 };
554 ASSERT_EQ(expected_public_libs, *public_libs);
Jooyung Han538f99a2020-03-03 00:46:50 +0900555}
556
Jooyung Hancd616d02020-09-01 14:53:23 +0900557TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) {
558 const char file_content[] = R"(
559jni com_android_foo libfoo
560# missing <library list>
561jni com_android_bar
562)";
563 auto result = ParseApexLibrariesConfig(file_content, "jni");
564 ASSERT_FALSE(result.ok());
565 ASSERT_EQ("Malformed line \"jni com_android_bar\"", result.error().message());
Jooyung Han538f99a2020-03-03 00:46:50 +0900566}
567
Jooyung Hancd616d02020-09-01 14:53:23 +0900568TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) {
569 const char file_content[] = R"(
570jni apex1 lib
571public apex2 lib
572# unknown tag
573unknown com_android_foo libfoo
574)";
575 auto result = ParseApexLibrariesConfig(file_content, "jni");
576 ASSERT_FALSE(result.ok());
577 ASSERT_EQ("Invalid tag \"unknown com_android_foo libfoo\"", result.error().message());
578}
579
580TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) {
581 const char file_content[] = R"(
582# apex linker namespace should be mangled ('.' -> '_')
583jni com.android.foo lib
584)";
585 auto result = ParseApexLibrariesConfig(file_content, "jni");
586 ASSERT_FALSE(result.ok());
587 ASSERT_EQ("Invalid apex_namespace \"jni com.android.foo lib\"", result.error().message());
588}
589
590TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) {
591 const char file_content[] = R"(
592# library list is ":" separated list of filenames
593jni com_android_foo lib64/libfoo.so
594)";
595 auto result = ParseApexLibrariesConfig(file_content, "jni");
596 ASSERT_FALSE(result.ok());
597 ASSERT_EQ("Invalid library_list \"jni com_android_foo lib64/libfoo.so\"", result.error().message());
598}
599
Orion Hodson9b16e342019-10-09 13:29:16 +0100600} // namespace nativeloader
601} // namespace android
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100602
603#endif // defined(ART_TARGET_ANDROID)