| 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 | |
| 17 | #include <dlfcn.h> |
| 18 | #include <memory> |
| 19 | #include <unordered_map> |
| 20 | |
| 21 | #include <android-base/strings.h> |
| 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <jni.h> |
| 25 | |
| 26 | #include "native_loader_namespace.h" |
| 27 | #include "nativeloader/dlext_namespaces.h" |
| 28 | #include "nativeloader/native_loader.h" |
| 29 | #include "public_libraries.h" |
| 30 | |
| 31 | using namespace ::testing; |
| 32 | using namespace ::android::nativeloader::internal; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace nativeloader { |
| 36 | |
| 37 | // gmock interface that represents interested platform APIs on libdl and libnativebridge |
| 38 | class Platform { |
| 39 | public: |
| 40 | virtual ~Platform() {} |
| 41 | |
| 42 | // libdl APIs |
| 43 | virtual void* dlopen(const char* filename, int flags) = 0; |
| 44 | virtual int dlclose(void* handle) = 0; |
| 45 | virtual char* dlerror(void) = 0; |
| 46 | |
| 47 | // These mock_* are the APIs semantically the same across libdl and libnativebridge. |
| 48 | // Instead of having two set of mock APIs for the two, define only one set with an additional |
| 49 | // argument 'bool bridged' to identify the context (i.e., called for libdl or libnativebridge). |
| 50 | typedef char* mock_namespace_handle; |
| 51 | virtual bool mock_init_anonymous_namespace(bool bridged, const char* sonames, |
| 52 | const char* search_paths) = 0; |
| 53 | virtual mock_namespace_handle mock_create_namespace( |
| 54 | bool bridged, const char* name, const char* ld_library_path, const char* default_library_path, |
| 55 | uint64_t type, const char* permitted_when_isolated_path, mock_namespace_handle parent) = 0; |
| 56 | virtual bool mock_link_namespaces(bool bridged, mock_namespace_handle from, |
| 57 | mock_namespace_handle to, const char* sonames) = 0; |
| 58 | virtual mock_namespace_handle mock_get_exported_namespace(bool bridged, const char* name) = 0; |
| 59 | virtual void* mock_dlopen_ext(bool bridged, const char* filename, int flags, |
| 60 | mock_namespace_handle ns) = 0; |
| 61 | |
| 62 | // libnativebridge APIs for which libdl has no corresponding APIs |
| 63 | virtual bool NativeBridgeInitialized() = 0; |
| 64 | virtual const char* NativeBridgeGetError() = 0; |
| 65 | virtual bool NativeBridgeIsPathSupported(const char*) = 0; |
| 66 | virtual bool NativeBridgeIsSupported(const char*) = 0; |
| 67 | |
| 68 | // To mock "ClassLoader Object.getParent()" |
| 69 | virtual const char* JniObject_getParent(const char*) = 0; |
| 70 | }; |
| 71 | |
| 72 | // The mock does not actually create a namespace object. But simply casts the pointer to the |
| 73 | // string for the namespace name as the handle to the namespace object. |
| 74 | #define TO_ANDROID_NAMESPACE(str) \ |
| 75 | reinterpret_cast<struct android_namespace_t*>(const_cast<char*>(str)) |
| 76 | |
| 77 | #define TO_BRIDGED_NAMESPACE(str) \ |
| 78 | reinterpret_cast<struct native_bridge_namespace_t*>(const_cast<char*>(str)) |
| 79 | |
| 80 | #define TO_MOCK_NAMESPACE(ns) reinterpret_cast<Platform::mock_namespace_handle>(ns) |
| 81 | |
| 82 | // These represents built-in namespaces created by the linker according to ld.config.txt |
| 83 | static std::unordered_map<std::string, Platform::mock_namespace_handle> namespaces = { |
| 84 | {"platform", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("platform"))}, |
| 85 | {"default", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("default"))}, |
| 86 | {"art", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("art"))}, |
| 87 | {"sphal", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("sphal"))}, |
| 88 | {"vndk", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("vndk"))}, |
| 89 | {"neuralnetworks", TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("neuralnetworks"))}, |
| 90 | }; |
| 91 | |
| 92 | // The actual gmock object |
| 93 | class MockPlatform : public Platform { |
| 94 | public: |
| 95 | explicit MockPlatform(bool is_bridged) : is_bridged_(is_bridged) { |
| 96 | ON_CALL(*this, NativeBridgeIsSupported(_)).WillByDefault(Return(is_bridged_)); |
| 97 | ON_CALL(*this, NativeBridgeIsPathSupported(_)).WillByDefault(Return(is_bridged_)); |
| 98 | ON_CALL(*this, mock_get_exported_namespace(_, _)) |
| 99 | .WillByDefault(Invoke([](bool, const char* name) -> mock_namespace_handle { |
| 100 | if (namespaces.find(name) != namespaces.end()) { |
| 101 | return namespaces[name]; |
| 102 | } |
| 103 | return TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE("(namespace not found")); |
| 104 | })); |
| 105 | } |
| 106 | |
| 107 | // Mocking libdl APIs |
| 108 | MOCK_METHOD2(dlopen, void*(const char*, int)); |
| 109 | MOCK_METHOD1(dlclose, int(void*)); |
| 110 | MOCK_METHOD0(dlerror, char*()); |
| 111 | |
| 112 | // Mocking the common APIs |
| 113 | MOCK_METHOD3(mock_init_anonymous_namespace, bool(bool, const char*, const char*)); |
| 114 | MOCK_METHOD7(mock_create_namespace, |
| 115 | mock_namespace_handle(bool, const char*, const char*, const char*, uint64_t, |
| 116 | const char*, mock_namespace_handle)); |
| 117 | MOCK_METHOD4(mock_link_namespaces, |
| 118 | bool(bool, mock_namespace_handle, mock_namespace_handle, const char*)); |
| 119 | MOCK_METHOD2(mock_get_exported_namespace, mock_namespace_handle(bool, const char*)); |
| 120 | MOCK_METHOD4(mock_dlopen_ext, void*(bool, const char*, int, mock_namespace_handle)); |
| 121 | |
| 122 | // Mocking libnativebridge APIs |
| 123 | MOCK_METHOD0(NativeBridgeInitialized, bool()); |
| 124 | MOCK_METHOD0(NativeBridgeGetError, const char*()); |
| 125 | MOCK_METHOD1(NativeBridgeIsPathSupported, bool(const char*)); |
| 126 | MOCK_METHOD1(NativeBridgeIsSupported, bool(const char*)); |
| 127 | |
| 128 | // Mocking "ClassLoader Object.getParent()" |
| 129 | MOCK_METHOD1(JniObject_getParent, const char*(const char*)); |
| 130 | |
| 131 | private: |
| 132 | bool is_bridged_; |
| 133 | }; |
| 134 | |
| 135 | static std::unique_ptr<MockPlatform> mock; |
| 136 | |
| 137 | // Provide C wrappers for the mock object. |
| 138 | extern "C" { |
| 139 | void* dlopen(const char* file, int flag) { |
| 140 | return mock->dlopen(file, flag); |
| 141 | } |
| 142 | |
| 143 | int dlclose(void* handle) { |
| 144 | return mock->dlclose(handle); |
| 145 | } |
| 146 | |
| 147 | char* dlerror(void) { |
| 148 | return mock->dlerror(); |
| 149 | } |
| 150 | |
| 151 | bool android_init_anonymous_namespace(const char* sonames, const char* search_path) { |
| 152 | return mock->mock_init_anonymous_namespace(false, sonames, search_path); |
| 153 | } |
| 154 | |
| 155 | struct android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path, |
| 156 | const char* default_library_path, |
| 157 | uint64_t type, |
| 158 | const char* permitted_when_isolated_path, |
| 159 | struct android_namespace_t* parent) { |
| 160 | return TO_ANDROID_NAMESPACE( |
| 161 | mock->mock_create_namespace(false, name, ld_library_path, default_library_path, type, |
| 162 | permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent))); |
| 163 | } |
| 164 | |
| 165 | bool android_link_namespaces(struct android_namespace_t* from, struct android_namespace_t* to, |
| 166 | const char* sonames) { |
| 167 | return mock->mock_link_namespaces(false, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames); |
| 168 | } |
| 169 | |
| 170 | struct android_namespace_t* android_get_exported_namespace(const char* name) { |
| 171 | return TO_ANDROID_NAMESPACE(mock->mock_get_exported_namespace(false, name)); |
| 172 | } |
| 173 | |
| 174 | void* android_dlopen_ext(const char* filename, int flags, const android_dlextinfo* info) { |
| 175 | return mock->mock_dlopen_ext(false, filename, flags, TO_MOCK_NAMESPACE(info->library_namespace)); |
| 176 | } |
| 177 | |
| 178 | // libnativebridge APIs |
| 179 | bool NativeBridgeIsSupported(const char* libpath) { |
| 180 | return mock->NativeBridgeIsSupported(libpath); |
| 181 | } |
| 182 | |
| 183 | struct native_bridge_namespace_t* NativeBridgeGetExportedNamespace(const char* name) { |
| 184 | return TO_BRIDGED_NAMESPACE(mock->mock_get_exported_namespace(true, name)); |
| 185 | } |
| 186 | |
| 187 | struct native_bridge_namespace_t* NativeBridgeCreateNamespace( |
| 188 | const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type, |
| 189 | const char* permitted_when_isolated_path, struct native_bridge_namespace_t* parent) { |
| 190 | return TO_BRIDGED_NAMESPACE( |
| 191 | mock->mock_create_namespace(true, name, ld_library_path, default_library_path, type, |
| 192 | permitted_when_isolated_path, TO_MOCK_NAMESPACE(parent))); |
| 193 | } |
| 194 | |
| 195 | bool NativeBridgeLinkNamespaces(struct native_bridge_namespace_t* from, |
| 196 | struct native_bridge_namespace_t* to, const char* sonames) { |
| 197 | return mock->mock_link_namespaces(true, TO_MOCK_NAMESPACE(from), TO_MOCK_NAMESPACE(to), sonames); |
| 198 | } |
| 199 | |
| 200 | void* NativeBridgeLoadLibraryExt(const char* libpath, int flag, |
| 201 | struct native_bridge_namespace_t* ns) { |
| 202 | return mock->mock_dlopen_ext(true, libpath, flag, TO_MOCK_NAMESPACE(ns)); |
| 203 | } |
| 204 | |
| 205 | bool NativeBridgeInitialized() { |
| 206 | return mock->NativeBridgeInitialized(); |
| 207 | } |
| 208 | |
| 209 | bool NativeBridgeInitAnonymousNamespace(const char* public_ns_sonames, |
| 210 | const char* anon_ns_library_path) { |
| 211 | return mock->mock_init_anonymous_namespace(true, public_ns_sonames, anon_ns_library_path); |
| 212 | } |
| 213 | |
| 214 | const char* NativeBridgeGetError() { |
| 215 | return mock->NativeBridgeGetError(); |
| 216 | } |
| 217 | |
| 218 | bool NativeBridgeIsPathSupported(const char* path) { |
| 219 | return mock->NativeBridgeIsPathSupported(path); |
| 220 | } |
| 221 | |
| 222 | } // extern "C" |
| 223 | |
| 224 | // A very simple JNI mock. |
| 225 | // jstring is a pointer to utf8 char array. We don't need utf16 char here. |
| 226 | // jobject, jclass, and jmethodID are also a pointer to utf8 char array |
| 227 | // Only a few JNI methods that are actually used in libnativeloader are mocked. |
| 228 | JNINativeInterface* CreateJNINativeInterface() { |
| 229 | JNINativeInterface* inf = new JNINativeInterface(); |
| 230 | memset(inf, 0, sizeof(JNINativeInterface)); |
| 231 | |
| 232 | inf->GetStringUTFChars = [](JNIEnv*, jstring s, jboolean*) -> const char* { |
| 233 | return reinterpret_cast<const char*>(s); |
| 234 | }; |
| 235 | |
| 236 | inf->ReleaseStringUTFChars = [](JNIEnv*, jstring, const char*) -> void { return; }; |
| 237 | |
| 238 | inf->NewStringUTF = [](JNIEnv*, const char* bytes) -> jstring { |
| 239 | return reinterpret_cast<jstring>(const_cast<char*>(bytes)); |
| 240 | }; |
| 241 | |
| 242 | inf->FindClass = [](JNIEnv*, const char* name) -> jclass { |
| 243 | return reinterpret_cast<jclass>(const_cast<char*>(name)); |
| 244 | }; |
| 245 | |
| 246 | inf->CallObjectMethodV = [](JNIEnv*, jobject obj, jmethodID mid, va_list) -> jobject { |
| 247 | if (strcmp("getParent", reinterpret_cast<const char*>(mid)) == 0) { |
| 248 | // JniObject_getParent can be a valid jobject or nullptr if there is |
| 249 | // no parent classloader. |
| 250 | const char* ret = mock->JniObject_getParent(reinterpret_cast<const char*>(obj)); |
| 251 | return reinterpret_cast<jobject>(const_cast<char*>(ret)); |
| 252 | } |
| 253 | return nullptr; |
| 254 | }; |
| 255 | |
| 256 | inf->GetMethodID = [](JNIEnv*, jclass, const char* name, const char*) -> jmethodID { |
| 257 | return reinterpret_cast<jmethodID>(const_cast<char*>(name)); |
| 258 | }; |
| 259 | |
| 260 | inf->NewWeakGlobalRef = [](JNIEnv*, jobject obj) -> jobject { return obj; }; |
| 261 | |
| 262 | inf->IsSameObject = [](JNIEnv*, jobject a, jobject b) -> jboolean { |
| 263 | return strcmp(reinterpret_cast<const char*>(a), reinterpret_cast<const char*>(b)) == 0; |
| 264 | }; |
| 265 | |
| 266 | return inf; |
| 267 | } |
| 268 | |
| 269 | static void* const any_nonnull = reinterpret_cast<void*>(0x12345678); |
| 270 | |
| 271 | // Custom matcher for comparing namespace handles |
| 272 | MATCHER_P(NsEq, other, "") { |
| 273 | *result_listener << "comparing " << reinterpret_cast<const char*>(arg) << " and " << other; |
| 274 | return strcmp(reinterpret_cast<const char*>(arg), reinterpret_cast<const char*>(other)) == 0; |
| 275 | } |
| 276 | |
| 277 | ///////////////////////////////////////////////////////////////// |
| 278 | |
| 279 | // Test fixture |
| 280 | class NativeLoaderTest : public ::testing::TestWithParam<bool> { |
| 281 | protected: |
| 282 | bool IsBridged() { return GetParam(); } |
| 283 | |
| 284 | void SetUp() override { |
| 285 | mock = std::make_unique<NiceMock<MockPlatform>>(IsBridged()); |
| 286 | |
| 287 | env = std::make_unique<JNIEnv>(); |
| 288 | env->functions = CreateJNINativeInterface(); |
| 289 | } |
| 290 | |
| 291 | void SetExpectations() { |
| 292 | std::vector<std::string> default_public_libs = |
| 293 | android::base::Split(preloadable_public_libraries(), ":"); |
| 294 | for (auto l : default_public_libs) { |
| 295 | EXPECT_CALL(*mock, dlopen(StrEq(l.c_str()), RTLD_NOW | RTLD_NODELETE)) |
| 296 | .WillOnce(Return(any_nonnull)); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void RunTest() { InitializeNativeLoader(); } |
| 301 | |
| 302 | void TearDown() override { |
| 303 | ResetNativeLoader(); |
| 304 | delete env->functions; |
| 305 | mock.reset(); |
| 306 | } |
| 307 | |
| 308 | std::unique_ptr<JNIEnv> env; |
| 309 | }; |
| 310 | |
| 311 | ///////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | TEST_P(NativeLoaderTest, InitializeLoadsDefaultPublicLibraries) { |
| 314 | SetExpectations(); |
| 315 | RunTest(); |
| 316 | } |
| 317 | |
| 318 | INSTANTIATE_TEST_SUITE_P(NativeLoaderTests, NativeLoaderTest, testing::Bool()); |
| 319 | |
| 320 | ///////////////////////////////////////////////////////////////// |
| 321 | |
| 322 | class NativeLoaderTest_Create : public NativeLoaderTest { |
| 323 | protected: |
| 324 | // Test inputs (initialized to the default values). Overriding these |
| 325 | // must be done before calling SetExpectations() and RunTest(). |
| 326 | uint32_t target_sdk_version = 29; |
| 327 | std::string class_loader = "my_classloader"; |
| 328 | bool is_shared = false; |
| 329 | std::string dex_path = "/data/app/foo/classes.dex"; |
| 330 | std::string library_path = "/data/app/foo/lib/arm"; |
| 331 | std::string permitted_path = "/data/app/foo/lib"; |
| 332 | |
| 333 | // expected output (.. for the default test inputs) |
| 334 | std::string expected_namespace_name = "classloader-namespace"; |
| 335 | uint64_t expected_namespace_flags = |
| 336 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS; |
| 337 | std::string expected_library_path = library_path; |
| 338 | std::string expected_permitted_path = std::string("/data:/mnt/expand:") + permitted_path; |
| 339 | std::string expected_parent_namespace = "platform"; |
| 340 | bool expected_link_with_platform_ns = true; |
| 341 | bool expected_link_with_art_ns = true; |
| 342 | bool expected_link_with_sphal_ns = !vendor_public_libraries().empty(); |
| 343 | bool expected_link_with_vndk_ns = false; |
| 344 | bool expected_link_with_default_ns = false; |
| 345 | bool expected_link_with_neuralnetworks_ns = true; |
| 346 | std::string expected_shared_libs_to_platform_ns = default_public_libraries(); |
| 347 | std::string expected_shared_libs_to_art_ns = art_public_libraries(); |
| 348 | std::string expected_shared_libs_to_sphal_ns = vendor_public_libraries(); |
| 349 | std::string expected_shared_libs_to_vndk_ns = vndksp_libraries(); |
| 350 | std::string expected_shared_libs_to_default_ns = default_public_libraries(); |
| 351 | std::string expected_shared_libs_to_neuralnetworks_ns = neuralnetworks_public_libraries(); |
| 352 | |
| 353 | void SetExpectations() { |
| 354 | NativeLoaderTest::SetExpectations(); |
| 355 | |
| 356 | ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr)); |
| 357 | |
| 358 | EXPECT_CALL(*mock, NativeBridgeIsPathSupported(_)).Times(AnyNumber()); |
| 359 | EXPECT_CALL(*mock, NativeBridgeInitialized()).Times(AnyNumber()); |
| 360 | |
| 361 | EXPECT_CALL(*mock, mock_create_namespace( |
| 362 | Eq(IsBridged()), StrEq(expected_namespace_name), nullptr, |
| 363 | StrEq(expected_library_path), expected_namespace_flags, |
| 364 | StrEq(expected_permitted_path), NsEq(expected_parent_namespace.c_str()))) |
| 365 | .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(dex_path.c_str())))); |
| 366 | if (expected_link_with_platform_ns) { |
| 367 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("platform"), |
| 368 | StrEq(expected_shared_libs_to_platform_ns))) |
| 369 | .WillOnce(Return(true)); |
| 370 | } |
| 371 | if (expected_link_with_art_ns) { |
| 372 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("art"), |
| 373 | StrEq(expected_shared_libs_to_art_ns))) |
| 374 | .WillOnce(Return(true)); |
| 375 | } |
| 376 | if (expected_link_with_sphal_ns) { |
| 377 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("sphal"), |
| 378 | StrEq(expected_shared_libs_to_sphal_ns))) |
| 379 | .WillOnce(Return(true)); |
| 380 | } |
| 381 | if (expected_link_with_vndk_ns) { |
| 382 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("vndk"), |
| 383 | StrEq(expected_shared_libs_to_vndk_ns))) |
| 384 | .WillOnce(Return(true)); |
| 385 | } |
| 386 | if (expected_link_with_default_ns) { |
| 387 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("default"), |
| 388 | StrEq(expected_shared_libs_to_default_ns))) |
| 389 | .WillOnce(Return(true)); |
| 390 | } |
| 391 | if (expected_link_with_neuralnetworks_ns) { |
| 392 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), _, NsEq("neuralnetworks"), |
| 393 | StrEq(expected_shared_libs_to_neuralnetworks_ns))) |
| 394 | .WillOnce(Return(true)); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void RunTest() { |
| 399 | NativeLoaderTest::RunTest(); |
| 400 | |
| 401 | jstring err = CreateClassLoaderNamespace( |
| 402 | env(), target_sdk_version, env()->NewStringUTF(class_loader.c_str()), is_shared, |
| 403 | env()->NewStringUTF(dex_path.c_str()), env()->NewStringUTF(library_path.c_str()), |
| 404 | env()->NewStringUTF(permitted_path.c_str())); |
| 405 | |
| 406 | // no error |
| 407 | EXPECT_EQ(err, nullptr); |
| 408 | |
| 409 | if (!IsBridged()) { |
| 410 | struct android_namespace_t* ns = |
| 411 | FindNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str())); |
| 412 | |
| 413 | // The created namespace is for this apk |
| 414 | EXPECT_EQ(dex_path.c_str(), reinterpret_cast<const char*>(ns)); |
| 415 | } else { |
| 416 | struct NativeLoaderNamespace* ns = |
| 417 | FindNativeLoaderNamespaceByClassLoader(env(), env()->NewStringUTF(class_loader.c_str())); |
| 418 | |
| 419 | // The created namespace is for the this apk |
| 420 | EXPECT_STREQ(dex_path.c_str(), |
| 421 | reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace())); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | JNIEnv* env() { return NativeLoaderTest::env.get(); } |
| 426 | }; |
| 427 | |
| 428 | TEST_P(NativeLoaderTest_Create, DownloadedApp) { |
| 429 | SetExpectations(); |
| 430 | RunTest(); |
| 431 | } |
| 432 | |
| 433 | TEST_P(NativeLoaderTest_Create, BundledSystemApp) { |
| 434 | dex_path = "/system/app/foo/foo.apk"; |
| 435 | is_shared = true; |
| 436 | |
| 437 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 438 | SetExpectations(); |
| 439 | RunTest(); |
| 440 | } |
| 441 | |
| 442 | TEST_P(NativeLoaderTest_Create, BundledVendorApp) { |
| 443 | dex_path = "/vendor/app/foo/foo.apk"; |
| 444 | is_shared = true; |
| 445 | |
| 446 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 447 | SetExpectations(); |
| 448 | RunTest(); |
| 449 | } |
| 450 | |
| 451 | TEST_P(NativeLoaderTest_Create, UnbundledVendorApp) { |
| 452 | dex_path = "/vendor/app/foo/foo.apk"; |
| 453 | is_shared = false; |
| 454 | |
| 455 | expected_namespace_name = "vendor-classloader-namespace"; |
| 456 | expected_library_path = expected_library_path + ":/vendor/lib"; |
| 457 | expected_permitted_path = expected_permitted_path + ":/vendor/lib"; |
| 458 | expected_shared_libs_to_platform_ns = |
| 459 | expected_shared_libs_to_platform_ns + ":" + llndk_libraries(); |
| 460 | expected_link_with_vndk_ns = true; |
| 461 | SetExpectations(); |
| 462 | RunTest(); |
| 463 | } |
| 464 | |
| 465 | TEST_P(NativeLoaderTest_Create, BundledProductApp_pre30) { |
| 466 | dex_path = "/product/app/foo/foo.apk"; |
| 467 | is_shared = true; |
| 468 | |
| 469 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 470 | SetExpectations(); |
| 471 | RunTest(); |
| 472 | } |
| 473 | |
| 474 | TEST_P(NativeLoaderTest_Create, BundledProductApp_post30) { |
| 475 | dex_path = "/product/app/foo/foo.apk"; |
| 476 | is_shared = true; |
| 477 | target_sdk_version = 30; |
| 478 | |
| 479 | expected_namespace_flags |= ANDROID_NAMESPACE_TYPE_SHARED; |
| 480 | SetExpectations(); |
| 481 | RunTest(); |
| 482 | } |
| 483 | |
| 484 | TEST_P(NativeLoaderTest_Create, UnbundledProductApp_pre30) { |
| 485 | dex_path = "/product/app/foo/foo.apk"; |
| 486 | is_shared = false; |
| 487 | SetExpectations(); |
| 488 | RunTest(); |
| 489 | } |
| 490 | |
| 491 | TEST_P(NativeLoaderTest_Create, UnbundledProductApp_post30) { |
| 492 | dex_path = "/product/app/foo/foo.apk"; |
| 493 | is_shared = false; |
| 494 | target_sdk_version = 30; |
| 495 | |
| 496 | expected_namespace_name = "vendor-classloader-namespace"; |
| 497 | expected_library_path = expected_library_path + ":/product/lib:/system/product/lib"; |
| 498 | expected_permitted_path = expected_permitted_path + ":/product/lib:/system/product/lib"; |
| 499 | expected_shared_libs_to_platform_ns = |
| 500 | expected_shared_libs_to_platform_ns + ":" + llndk_libraries(); |
| 501 | expected_link_with_vndk_ns = true; |
| 502 | SetExpectations(); |
| 503 | RunTest(); |
| 504 | } |
| 505 | |
| 506 | TEST_P(NativeLoaderTest_Create, NamespaceForSharedLibIsNotUsedAsAnonymousNamespace) { |
| 507 | if (IsBridged()) { |
| 508 | // There is no shared lib in translated arch |
| 509 | // TODO(jiyong): revisit this |
| 510 | return; |
| 511 | } |
| 512 | // compared to apks, for java shared libs, library_path is empty; java shared |
| 513 | // libs don't have their own native libs. They use platform's. |
| 514 | library_path = ""; |
| 515 | expected_library_path = library_path; |
| 516 | // no ALSO_USED_AS_ANONYMOUS |
| 517 | expected_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 518 | SetExpectations(); |
| 519 | RunTest(); |
| 520 | } |
| 521 | |
| 522 | TEST_P(NativeLoaderTest_Create, TwoApks) { |
| 523 | SetExpectations(); |
| 524 | const uint32_t second_app_target_sdk_version = 29; |
| 525 | const std::string second_app_class_loader = "second_app_classloader"; |
| 526 | const bool second_app_is_shared = false; |
| 527 | const std::string second_app_dex_path = "/data/app/bar/classes.dex"; |
| 528 | const std::string second_app_library_path = "/data/app/bar/lib/arm"; |
| 529 | const std::string second_app_permitted_path = "/data/app/bar/lib"; |
| 530 | const std::string expected_second_app_permitted_path = |
| 531 | std::string("/data:/mnt/expand:") + second_app_permitted_path; |
| 532 | const std::string expected_second_app_parent_namespace = "classloader-namespace"; |
| 533 | // no ALSO_USED_AS_ANONYMOUS |
| 534 | const uint64_t expected_second_namespace_flags = ANDROID_NAMESPACE_TYPE_ISOLATED; |
| 535 | |
| 536 | // The scenario is that second app is loaded by the first app. |
| 537 | // So the first app's classloader (`classloader`) is parent of the second |
| 538 | // app's classloader. |
| 539 | ON_CALL(*mock, JniObject_getParent(StrEq(second_app_class_loader))) |
| 540 | .WillByDefault(Return(class_loader.c_str())); |
| 541 | |
| 542 | // namespace for the second app is created. Its parent is set to the namespace |
| 543 | // of the first app. |
| 544 | EXPECT_CALL(*mock, mock_create_namespace( |
| 545 | Eq(IsBridged()), StrEq(expected_namespace_name), nullptr, |
| 546 | StrEq(second_app_library_path), expected_second_namespace_flags, |
| 547 | StrEq(expected_second_app_permitted_path), NsEq(dex_path.c_str()))) |
| 548 | .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(second_app_dex_path.c_str())))); |
| 549 | EXPECT_CALL(*mock, mock_link_namespaces(Eq(IsBridged()), NsEq(second_app_dex_path.c_str()), _, _)) |
| 550 | .WillRepeatedly(Return(true)); |
| 551 | |
| 552 | RunTest(); |
| 553 | jstring err = CreateClassLoaderNamespace( |
| 554 | env(), second_app_target_sdk_version, env()->NewStringUTF(second_app_class_loader.c_str()), |
| 555 | second_app_is_shared, env()->NewStringUTF(second_app_dex_path.c_str()), |
| 556 | env()->NewStringUTF(second_app_library_path.c_str()), |
| 557 | env()->NewStringUTF(second_app_permitted_path.c_str())); |
| 558 | |
| 559 | // success |
| 560 | EXPECT_EQ(err, nullptr); |
| 561 | |
| 562 | if (!IsBridged()) { |
| 563 | struct android_namespace_t* ns = |
| 564 | FindNamespaceByClassLoader(env(), env()->NewStringUTF(second_app_class_loader.c_str())); |
| 565 | |
| 566 | // The created namespace is for the second apk |
| 567 | EXPECT_EQ(second_app_dex_path.c_str(), reinterpret_cast<const char*>(ns)); |
| 568 | } else { |
| 569 | struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader( |
| 570 | env(), env()->NewStringUTF(second_app_class_loader.c_str())); |
| 571 | |
| 572 | // The created namespace is for the second apk |
| 573 | EXPECT_STREQ(second_app_dex_path.c_str(), |
| 574 | reinterpret_cast<const char*>(ns->ToRawNativeBridgeNamespace())); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | INSTANTIATE_TEST_SUITE_P(NativeLoaderTests_Create, NativeLoaderTest_Create, testing::Bool()); |
| 579 | |
| 580 | const std::function<Result<bool>(const struct ConfigEntry&)> always_true = |
| 581 | [](const struct ConfigEntry&) -> Result<bool> { return true; }; |
| 582 | |
| 583 | TEST(NativeLoaderConfigParser, NamesAndComments) { |
| 584 | const char file_content[] = R"( |
| 585 | ###### |
| 586 | |
| 587 | libA.so |
| 588 | #libB.so |
| 589 | |
| 590 | |
| 591 | libC.so |
| 592 | libD.so |
| 593 | #### libE.so |
| 594 | )"; |
| 595 | const std::vector<std::string> expected_result = {"libA.so", "libC.so", "libD.so"}; |
| 596 | Result<std::vector<std::string>> result = ParseConfig(file_content, always_true); |
| 597 | ASSERT_TRUE(result) << result.error().message(); |
| 598 | ASSERT_EQ(expected_result, *result); |
| 599 | } |
| 600 | |
| 601 | TEST(NativeLoaderConfigParser, WithBitness) { |
| 602 | const char file_content[] = R"( |
| 603 | libA.so 32 |
| 604 | libB.so 64 |
| 605 | libC.so |
| 606 | )"; |
| 607 | #if defined(__LP64__) |
| 608 | const std::vector<std::string> expected_result = {"libB.so", "libC.so"}; |
| 609 | #else |
| 610 | const std::vector<std::string> expected_result = {"libA.so", "libC.so"}; |
| 611 | #endif |
| 612 | Result<std::vector<std::string>> result = ParseConfig(file_content, always_true); |
| 613 | ASSERT_TRUE(result) << result.error().message(); |
| 614 | ASSERT_EQ(expected_result, *result); |
| 615 | } |
| 616 | |
| 617 | TEST(NativeLoaderConfigParser, WithNoPreload) { |
| 618 | const char file_content[] = R"( |
| 619 | libA.so nopreload |
| 620 | libB.so nopreload |
| 621 | libC.so |
| 622 | )"; |
| 623 | |
| 624 | const std::vector<std::string> expected_result = {"libC.so"}; |
| 625 | Result<std::vector<std::string>> result = |
| 626 | ParseConfig(file_content, |
| 627 | [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; }); |
| 628 | ASSERT_TRUE(result) << result.error().message(); |
| 629 | ASSERT_EQ(expected_result, *result); |
| 630 | } |
| 631 | |
| 632 | TEST(NativeLoaderConfigParser, WithNoPreloadAndBitness) { |
| 633 | const char file_content[] = R"( |
| 634 | libA.so nopreload 32 |
| 635 | libB.so 64 nopreload |
| 636 | libC.so 32 |
| 637 | libD.so 64 |
| 638 | libE.so nopreload |
| 639 | )"; |
| 640 | |
| 641 | #if defined(__LP64__) |
| 642 | const std::vector<std::string> expected_result = {"libD.so"}; |
| 643 | #else |
| 644 | const std::vector<std::string> expected_result = {"libC.so"}; |
| 645 | #endif |
| 646 | Result<std::vector<std::string>> result = |
| 647 | ParseConfig(file_content, |
| 648 | [](const struct ConfigEntry& entry) -> Result<bool> { return !entry.nopreload; }); |
| 649 | ASSERT_TRUE(result) << result.error().message(); |
| 650 | ASSERT_EQ(expected_result, *result); |
| 651 | } |
| 652 | |
| 653 | TEST(NativeLoaderConfigParser, RejectMalformed) { |
| 654 | ASSERT_FALSE(ParseConfig("libA.so 32 64", always_true)); |
| 655 | ASSERT_FALSE(ParseConfig("libA.so 32 32", always_true)); |
| 656 | ASSERT_FALSE(ParseConfig("libA.so 32 nopreload 64", always_true)); |
| 657 | ASSERT_FALSE(ParseConfig("32 libA.so nopreload", always_true)); |
| 658 | ASSERT_FALSE(ParseConfig("nopreload libA.so 32", always_true)); |
| 659 | ASSERT_FALSE(ParseConfig("libA.so nopreload # comment", always_true)); |
| 660 | } |
| 661 | |
| 662 | } // namespace nativeloader |
| 663 | } // namespace android |