Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 17 | #if defined(ART_TARGET_ANDROID) |
| 18 | |
Martin Stjernholm | 19d1feb | 2021-03-30 22:35:24 +0100 | [diff] [blame] | 19 | #include "native_loader_test.h" |
| 20 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 21 | #include <dlfcn.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 22 | |
| 23 | #include <android-base/strings.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 25 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 26 | #include "nativehelper/scoped_utf_chars.h" |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 27 | #include "nativeloader/native_loader.h" |
| 28 | #include "public_libraries.h" |
| 29 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace nativeloader { |
| 32 | |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 33 | using ::testing::Eq; |
Martin Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 34 | using ::testing::NotNull; |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 35 | using ::testing::StrEq; |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 36 | using internal::ConfigEntry; |
Calin Juravle | 91d2c5c | 2021-05-07 22:44:29 +0000 | [diff] [blame] | 37 | using internal::ParseApexLibrariesConfig; |
Martin Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 38 | using internal::ParseConfig; |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 39 | |
Martin Stjernholm | be08b20 | 2019-11-12 20:11:00 +0000 | [diff] [blame] | 40 | #if defined(__LP64__) |
| 41 | #define LIB_DIR "lib64" |
| 42 | #else |
| 43 | #define LIB_DIR "lib" |
| 44 | #endif |
| 45 | |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 46 | static void* const any_nonnull = reinterpret_cast<void*>(0x12345678); |
| 47 | |
| 48 | // Custom matcher for comparing namespace handles |
| 49 | MATCHER_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 |
| 57 | class NativeLoaderTest : public ::testing::TestWithParam<bool> { |
| 58 | protected: |
| 59 | bool IsBridged() { return GetParam(); } |
| 60 | |
| 61 | void SetUp() override { |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 62 | mock = std::make_unique<testing::NiceMock<MockPlatform>>(IsBridged()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 63 | |
| 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 Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 72 | EXPECT_CALL(*mock, |
| 73 | mock_dlopen_ext(false, StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE, NotNull())) |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 74 | .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 | |
| 91 | TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) { |
| 92 | SetExpectations(); |
| 93 | RunTest(); |
| 94 | } |
| 95 | |
Martin Stjernholm | 2665943 | 2021-04-16 19:55:03 +0100 | [diff] [blame] | 96 | TEST_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 | |
| 120 | TEST_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 | |
| 143 | TEST_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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 166 | INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool()); |
| 167 | |
| 168 | ///////////////////////////////////////////////////////////////// |
| 169 | |
Martin Stjernholm | 630cb4e | 2022-03-24 00:50:33 +0000 | [diff] [blame] | 170 | std::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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 179 | class 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 Stjernholm | be08b20 | 2019-11-12 20:11:00 +0000 | [diff] [blame] | 187 | std::string library_path = "/data/app/foo/" LIB_DIR "/arm"; |
| 188 | std::string permitted_path = "/data/app/foo/" LIB_DIR; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 189 | |
| 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 Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 196 | std::string expected_parent_namespace = "system"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 197 | bool expected_link_with_platform_ns = true; |
| 198 | bool expected_link_with_art_ns = true; |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 199 | bool expected_link_with_i18n_ns = true; |
Jooyung Han | 180e1e7 | 2021-06-20 17:54:38 +0900 | [diff] [blame] | 200 | bool expected_link_with_conscrypt_ns = false; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 201 | bool expected_link_with_sphal_ns = !vendor_public_libraries().empty(); |
Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 202 | bool expected_link_with_product_ns = !product_public_libraries().empty(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 203 | bool expected_link_with_vndk_ns = false; |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 204 | bool expected_link_with_vndk_product_ns = false; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 205 | bool expected_link_with_default_ns = false; |
| 206 | bool expected_link_with_neuralnetworks_ns = true; |
Martin Stjernholm | 630cb4e | 2022-03-24 00:50:33 +0000 | [diff] [blame] | 207 | std::string expected_shared_libs_to_platform_ns = default_public_and_extended_libraries(); |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 208 | 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 Han | 180e1e7 | 2021-06-20 17:54:38 +0900 | [diff] [blame] | 210 | std::string expected_shared_libs_to_conscrypt_ns = apex_jni_libraries("com_android_conscrypt"); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 211 | std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries(); |
Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 212 | std::string expected_shared_libs_to_product_ns = product_public_libraries(); |
Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 213 | 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 Stjernholm | 630cb4e | 2022-03-24 00:50:33 +0000 | [diff] [blame] | 215 | std::string expected_shared_libs_to_default_ns = default_public_and_extended_libraries(); |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 216 | std::string expected_shared_libs_to_neuralnetworks_ns = apex_public_libraries().at("com_android_neuralnetworks"); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 217 | |
| 218 | void SetExpectations() { |
| 219 | NativeLoaderTest::SetExpectations(); |
| 220 | |
| 221 | ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr)); |
| 222 | |
Martin Stjernholm | 4829733 | 2019-11-12 21:21:32 +0000 | [diff] [blame] | 223 | EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(testing::AnyNumber()); |
| 224 | EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(testing::AnyNumber()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 225 | |
| 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 Kim | 99c19ca | 2020-01-29 16:09:38 +0900 | [diff] [blame] | 232 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("system"), |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 233 | StrEq(expected_shared_libs_to_platform_ns))) |
| 234 | .WillOnce(Return(true)); |
| 235 | } |
| 236 | if (expected_link_with_art_ns) { |
Kiyoung Kim | 272b36d | 2020-02-19 16:08:47 +0900 | [diff] [blame] | 237 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_art"), |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 238 | StrEq(expected_shared_libs_to_art_ns))) |
| 239 | .WillOnce(Return(true)); |
| 240 | } |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 241 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 246 | 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 Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 251 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 256 | 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 Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 261 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 266 | 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 Kim | 272b36d | 2020-02-19 16:08:47 +0900 | [diff] [blame] | 272 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("com_android_neuralnetworks"), |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 273 | StrEq(expected_shared_libs_to_neuralnetworks_ns))) |
| 274 | .WillOnce(Return(true)); |
| 275 | } |
Jooyung Han | 180e1e7 | 2021-06-20 17:54:38 +0900 | [diff] [blame] | 276 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 281 | } |
| 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 Stjernholm | b309240 | 2020-09-04 00:49:44 +0100 | [diff] [blame] | 289 | env()->NewStringUTF(permitted_path.c_str()), /*uses_library_list=*/ nullptr); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 290 | |
| 291 | // no error |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 292 | EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 293 | |
| 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 | |
| 313 | TEST_P(NativeLoaderTest_Create, DownloadedApp) { |
| 314 | SetExpectations(); |
| 315 | RunTest(); |
| 316 | } |
| 317 | |
| 318 | TEST_P(NativeLoaderTest_Create, BundledSystemApp) { |
| 319 | dex_path = "/system/app/foo/foo.apk"; |
| 320 | is_shared = true; |
| 321 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 322 | expected_namespace_name = "classloader-namespace-shared"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 323 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 324 | SetExpectations(); |
| 325 | RunTest(); |
| 326 | } |
| 327 | |
| 328 | TEST_P(NativeLoaderTest_Create, BundledVendorApp) { |
| 329 | dex_path = "/vendor/app/foo/foo.apk"; |
| 330 | is_shared = true; |
| 331 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 332 | expected_namespace_name = "classloader-namespace-shared"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 333 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 334 | SetExpectations(); |
| 335 | RunTest(); |
| 336 | } |
| 337 | |
| 338 | TEST_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 Stjernholm | be08b20 | 2019-11-12 20:11:00 +0000 | [diff] [blame] | 343 | expected_library_path = expected_library_path + ":/vendor/" LIB_DIR; |
| 344 | expected_permitted_path = expected_permitted_path + ":/vendor/" LIB_DIR; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 345 | expected_shared_libs_to_platform_ns = |
Martin Stjernholm | 630cb4e | 2022-03-24 00:50:33 +0000 | [diff] [blame] | 346 | default_public_libraries() + ":" + llndk_libraries_vendor(); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 347 | expected_link_with_vndk_ns = true; |
| 348 | SetExpectations(); |
| 349 | RunTest(); |
| 350 | } |
| 351 | |
Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 352 | TEST_P(NativeLoaderTest_Create, BundledProductApp) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 353 | dex_path = "/product/app/foo/foo.apk"; |
| 354 | is_shared = true; |
| 355 | |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 356 | expected_namespace_name = "classloader-namespace-shared"; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 357 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 358 | SetExpectations(); |
| 359 | RunTest(); |
| 360 | } |
| 361 | |
Jooyung Han | 180e1e7 | 2021-06-20 17:54:38 +0900 | [diff] [blame] | 362 | TEST_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 Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 373 | TEST_P(NativeLoaderTest_Create, UnbundledProductApp) { |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 374 | dex_path = "/product/app/foo/foo.apk"; |
| 375 | is_shared = false; |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 376 | |
Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 377 | 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 Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 382 | expected_link_with_vndk_product_ns = true; |
Martin Stjernholm | 43b3caa | 2022-09-02 21:33:04 +0100 | [diff] [blame^] | 383 | |
| 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 Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 400 | } |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 401 | SetExpectations(); |
| 402 | RunTest(); |
| 403 | } |
| 404 | |
| 405 | TEST_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 | |
| 421 | TEST_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 Stjernholm | be08b20 | 2019-11-12 20:11:00 +0000 | [diff] [blame] | 427 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 429 | 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 Stjernholm | b309240 | 2020-09-04 00:49:44 +0100 | [diff] [blame] | 456 | env()->NewStringUTF(second_app_permitted_path.c_str()), /*uses_library_list=*/ nullptr); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 457 | |
| 458 | // success |
Martin Stjernholm | 94fd9ea | 2019-10-24 16:57:34 +0100 | [diff] [blame] | 459 | EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env(), err).c_str()); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 460 | |
| 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 | |
| 477 | INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool()); |
| 478 | |
| 479 | const std::function<Result<bool>(const struct ConfigEntry&)> always_true = |
| 480 | [](const struct ConfigEntry&) -> Result<bool> { return true; }; |
| 481 | |
| 482 | TEST(NativeLoaderConfigParser, NamesAndComments) { |
| 483 | const char file_content[] = R"( |
| 484 | ###### |
| 485 | |
| 486 | libA.so |
| 487 | #libB.so |
| 488 | |
| 489 | |
| 490 | libC.so |
| 491 | libD.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 Innocenti | ac5ae3c | 2020-02-12 10:43:42 +0900 | [diff] [blame] | 496 | ASSERT_RESULT_OK(result); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 497 | ASSERT_EQ(expected_result, *result); |
| 498 | } |
| 499 | |
| 500 | TEST(NativeLoaderConfigParser, WithBitness) { |
| 501 | const char file_content[] = R"( |
| 502 | libA.so 32 |
| 503 | libB.so 64 |
| 504 | libC.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 Innocenti | ac5ae3c | 2020-02-12 10:43:42 +0900 | [diff] [blame] | 512 | ASSERT_RESULT_OK(result); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 513 | ASSERT_EQ(expected_result, *result); |
| 514 | } |
| 515 | |
| 516 | TEST(NativeLoaderConfigParser, WithNoPreload) { |
| 517 | const char file_content[] = R"( |
| 518 | libA.so nopreload |
| 519 | libB.so nopreload |
| 520 | libC.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 Innocenti | ac5ae3c | 2020-02-12 10:43:42 +0900 | [diff] [blame] | 527 | ASSERT_RESULT_OK(result); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 528 | ASSERT_EQ(expected_result, *result); |
| 529 | } |
| 530 | |
| 531 | TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) { |
| 532 | const char file_content[] = R"( |
| 533 | libA.so nopreload 32 |
| 534 | libB.so 64 nopreload |
| 535 | libC.so 32 |
| 536 | libD.so 64 |
| 537 | libE.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 Innocenti | ac5ae3c | 2020-02-12 10:43:42 +0900 | [diff] [blame] | 548 | ASSERT_RESULT_OK(result); |
Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 549 | ASSERT_EQ(expected_result, *result); |
| 550 | } |
| 551 | |
| 552 | TEST(NativeLoaderConfigParser, RejectMalformed) { |
Bernie Innocenti | ac5ae3c | 2020-02-12 10:43:42 +0900 | [diff] [blame] | 553 | 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 561 | TEST(NativeLoaderApexLibrariesConfigParser, BasicLoading) { |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 562 | const char file_content[] = R"( |
| 563 | # comment |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 564 | jni com_android_foo libfoo.so |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 565 | # Empty line is ignored |
| 566 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 567 | jni com_android_bar libbar.so:libbar2.so |
| 568 | |
| 569 | public com_android_bar libpublic.so |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 570 | )"; |
| 571 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 572 | auto jni_libs = ParseApexLibrariesConfig(file_content, "jni"); |
| 573 | ASSERT_RESULT_OK(jni_libs); |
| 574 | std::map<std::string, std::string> expected_jni_libs { |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 575 | {"com_android_foo", "libfoo.so"}, |
| 576 | {"com_android_bar", "libbar.so:libbar2.so"}, |
| 577 | }; |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 578 | ASSERT_EQ(expected_jni_libs, *jni_libs); |
Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 579 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 580 | 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 Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 586 | } |
| 587 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 588 | TEST(NativeLoaderApexLibrariesConfigParser, RejectMalformedLine) { |
| 589 | const char file_content[] = R"( |
| 590 | jni com_android_foo libfoo |
| 591 | # missing <library list> |
| 592 | jni 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 Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 597 | } |
| 598 | |
Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 599 | TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidTag) { |
| 600 | const char file_content[] = R"( |
| 601 | jni apex1 lib |
| 602 | public apex2 lib |
| 603 | # unknown tag |
| 604 | unknown 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 | |
| 611 | TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidApexNamespace) { |
| 612 | const char file_content[] = R"( |
| 613 | # apex linker namespace should be mangled ('.' -> '_') |
| 614 | jni 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 | |
| 621 | TEST(NativeLoaderApexLibrariesConfigParser, RejectInvalidLibraryList) { |
| 622 | const char file_content[] = R"( |
| 623 | # library list is ":" separated list of filenames |
| 624 | jni 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 Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 631 | } // namespace nativeloader |
| 632 | } // namespace android |
Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 633 | |
| 634 | #endif // defined(ART_TARGET_ANDROID) |