1 /* 2 * Copyright (C) 2017 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 #define LOG_TAG "Singleton_test" 18 19 #include <dlfcn.h> 20 21 #include <android-base/file.h> 22 #include <android-base/stringprintf.h> 23 #include <utils/Singleton.h> 24 25 #include <gtest/gtest.h> 26 27 #include "Singleton_test.h" 28 29 namespace android { 30 31 TEST(SingletonTest, bug35674422) { 32 std::string path = android::base::GetExecutableDirectory(); 33 // libutils_tests_singleton1.so contains the ANDROID_SINGLETON_STATIC_INSTANCE 34 // definition of SingletonTestData, load it first. 35 std::string lib = android::base::StringPrintf("%s/libutils_tests_singleton1.so", path.c_str()); 36 void* handle1 = dlopen(lib.c_str(), RTLD_NOW); 37 ASSERT_TRUE(handle1 != nullptr) << dlerror(); 38 39 // libutils_tests_singleton2.so references SingletonTestData but should not 40 // have a definition 41 lib = android::base::StringPrintf("%s/libutils_tests_singleton2.so", path.c_str()); 42 void* handle2 = dlopen(lib.c_str(), RTLD_NOW); 43 ASSERT_TRUE(handle2 != nullptr) << dlerror(); 44 45 using has_fn_t = decltype(&singletonHasInstance); 46 using get_fn_t = decltype(&singletonGetInstanceContents); 47 using set_fn_t = decltype(&singletonSetInstanceContents); 48 49 has_fn_t has1 = reinterpret_cast<has_fn_t>(dlsym(handle1, "singletonHasInstance")); 50 ASSERT_TRUE(has1 != nullptr) << dlerror(); 51 has_fn_t has2 = reinterpret_cast<has_fn_t>(dlsym(handle2, "singletonHasInstance")); 52 ASSERT_TRUE(has2 != nullptr) << dlerror(); 53 get_fn_t get1 = reinterpret_cast<get_fn_t>(dlsym(handle1, "singletonGetInstanceContents")); 54 ASSERT_TRUE(get1 != nullptr) << dlerror(); 55 get_fn_t get2 = reinterpret_cast<get_fn_t>(dlsym(handle2, "singletonGetInstanceContents")); 56 ASSERT_TRUE(get2 != nullptr) << dlerror(); 57 set_fn_t set1 = reinterpret_cast<set_fn_t>(dlsym(handle2, "singletonSetInstanceContents")); 58 ASSERT_TRUE(set1 != nullptr) << dlerror(); 59 60 EXPECT_FALSE(has1()); 61 EXPECT_FALSE(has2()); 62 set1(12345678U); 63 EXPECT_TRUE(has1()); 64 EXPECT_TRUE(has2()); 65 EXPECT_EQ(12345678U, get1()); 66 EXPECT_EQ(12345678U, get2()); 67 } 68 69 } 70