Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2014 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 <gtest/gtest.h>
     18 
     19 #include <dlfcn.h>
     20 #include <elf.h>
     21 #include <errno.h>
     22 #include <fcntl.h>
     23 #include <inttypes.h>
     24 #include <stdio.h>
     25 #include <string.h>
     26 #include <unistd.h>
     27 #include <android/dlext.h>
     28 #include <sys/mman.h>
     29 #include <sys/types.h>
     30 #include <sys/wait.h>
     31 
     32 #include <pagemap/pagemap.h>
     33 #include <ziparchive/zip_archive.h>
     34 
     35 #include "TemporaryFile.h"
     36 #include "utils.h"
     37 #include "dlext_private.h"
     38 
     39 #define ASSERT_DL_NOTNULL(ptr) \
     40     ASSERT_TRUE(ptr != nullptr) << "dlerror: " << dlerror()
     41 
     42 #define ASSERT_DL_ZERO(i) \
     43     ASSERT_EQ(0, i) << "dlerror: " << dlerror()
     44 
     45 #define ASSERT_NOERROR(i) \
     46     ASSERT_NE(-1, i) << "errno: " << strerror(errno)
     47 
     48 #define ASSERT_SUBSTR(needle, haystack) \
     49     ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
     50 
     51 
     52 typedef int (*fn)(void);
     53 #define LIBNAME "libdlext_test.so"
     54 #define LIBNAME_NORELRO "libdlext_test_norelro.so"
     55 #define LIBSIZE 1024*1024 // how much address space to reserve for it
     56 
     57 #if defined(__LP64__)
     58 #define NATIVE_TESTS_PATH "/nativetest64"
     59 #else
     60 #define NATIVE_TESTS_PATH "/nativetest"
     61 #endif
     62 
     63 #define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
     64 #define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
     65 #define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
     66 #define LIBZIP_SIMPLE_ZIP "libdir/libatest_simple_zip.so"
     67 
     68 class DlExtTest : public ::testing::Test {
     69 protected:
     70   virtual void SetUp() {
     71     handle_ = nullptr;
     72     // verify that we don't have the library loaded already
     73     void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
     74     ASSERT_TRUE(h == nullptr);
     75     h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
     76     ASSERT_TRUE(h == nullptr);
     77     // call dlerror() to swallow the error, and check it was the one we wanted
     78     ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
     79   }
     80 
     81   virtual void TearDown() {
     82     if (handle_ != nullptr) {
     83       ASSERT_DL_ZERO(dlclose(handle_));
     84     }
     85   }
     86 
     87   void* handle_;
     88 };
     89 
     90 TEST_F(DlExtTest, ExtInfoNull) {
     91   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
     92   ASSERT_DL_NOTNULL(handle_);
     93   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
     94   ASSERT_DL_NOTNULL(f);
     95   EXPECT_EQ(4, f());
     96 }
     97 
     98 TEST_F(DlExtTest, ExtInfoNoFlags) {
     99   android_dlextinfo extinfo;
    100   extinfo.flags = 0;
    101   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    102   ASSERT_DL_NOTNULL(handle_);
    103   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    104   ASSERT_DL_NOTNULL(f);
    105   EXPECT_EQ(4, f());
    106 }
    107 
    108 TEST_F(DlExtTest, ExtInfoUseFd) {
    109   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
    110 
    111   android_dlextinfo extinfo;
    112   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
    113   extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
    114   ASSERT_TRUE(extinfo.library_fd != -1);
    115   handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
    116   ASSERT_DL_NOTNULL(handle_);
    117   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    118   ASSERT_DL_NOTNULL(f);
    119   EXPECT_EQ(4, f());
    120 
    121   uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
    122   ASSERT_DL_NOTNULL(taxicab_number);
    123   EXPECT_EQ(1729U, *taxicab_number);
    124 }
    125 
    126 TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
    127   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
    128 
    129   android_dlextinfo extinfo;
    130   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
    131   extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
    132 
    133   // Find the offset of the shared library in the zip.
    134   ZipArchiveHandle handle;
    135   ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
    136   ZipEntry zip_entry;
    137   ZipString zip_name;
    138   zip_name.name = reinterpret_cast<const uint8_t*>(LIBZIP_SIMPLE_ZIP);
    139   zip_name.name_length = sizeof(LIBZIP_SIMPLE_ZIP) - 1;
    140   ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
    141   extinfo.library_fd_offset = zip_entry.offset;
    142   CloseArchive(handle);
    143 
    144   handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo);
    145   ASSERT_DL_NOTNULL(handle_);
    146 
    147   uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
    148   ASSERT_DL_NOTNULL(taxicab_number);
    149   EXPECT_EQ(1729U, *taxicab_number);
    150 }
    151 
    152 TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
    153   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
    154   // lib_path is relative when $ANDROID_DATA is relative
    155   char lib_realpath_buf[PATH_MAX];
    156   ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
    157   const std::string lib_realpath = std::string(lib_realpath_buf);
    158 
    159   android_dlextinfo extinfo;
    160   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
    161   extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC));
    162   extinfo.library_fd_offset = 17;
    163 
    164   handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
    165   ASSERT_TRUE(handle_ == nullptr);
    166   ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror());
    167 
    168   // Test an address above 2^44, for http://b/18178121 .
    169   extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE;
    170   handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
    171   ASSERT_TRUE(handle_ == nullptr);
    172   ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror());
    173 
    174   extinfo.library_fd_offset = 0LL - PAGE_SIZE;
    175   handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
    176   ASSERT_TRUE(handle_ == nullptr);
    177   ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
    178 
    179   extinfo.library_fd_offset = 0;
    180   handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
    181   ASSERT_TRUE(handle_ == nullptr);
    182   ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
    183 
    184   // Check if dlsym works after unsuccessful dlopen().
    185   // Supply non-exiting one to make linker visit every soinfo.
    186   void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___");
    187   ASSERT_TRUE(sym == nullptr);
    188 
    189   close(extinfo.library_fd);
    190 }
    191 
    192 TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) {
    193   android_dlextinfo extinfo;
    194   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
    195   // This offset will not be used, so it doesn't matter.
    196   extinfo.library_fd_offset = 0;
    197 
    198   handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo);
    199   ASSERT_TRUE(handle_ == nullptr);
    200   ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror());
    201 }
    202 
    203 TEST(dlext, android_dlopen_ext_force_load_smoke) {
    204   // 1. Open actual file
    205   void* handle = dlopen("libdlext_test.so", RTLD_NOW);
    206   ASSERT_DL_NOTNULL(handle);
    207   // 2. Open link with force_load flag set
    208   android_dlextinfo extinfo;
    209   extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
    210   void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
    211   ASSERT_DL_NOTNULL(handle2);
    212   ASSERT_TRUE(handle != handle2);
    213 
    214   dlclose(handle2);
    215   dlclose(handle);
    216 }
    217 
    218 TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
    219   // Check if soname lookup still returns already loaded library
    220   // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
    221   void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
    222   ASSERT_DL_NOTNULL(handle);
    223 
    224   android_dlextinfo extinfo;
    225   extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
    226 
    227   // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
    228   void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
    229 
    230   ASSERT_DL_NOTNULL(handle2);
    231   ASSERT_TRUE(handle == handle2);
    232 
    233   dlclose(handle2);
    234   dlclose(handle);
    235 }
    236 
    237 TEST(dlfcn, dlopen_from_zip_absolute_path) {
    238   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
    239 
    240   void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
    241   ASSERT_TRUE(handle != nullptr) << dlerror();
    242 
    243   uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
    244   ASSERT_DL_NOTNULL(taxicab_number);
    245   EXPECT_EQ(1729U, *taxicab_number);
    246 
    247   dlclose(handle);
    248 }
    249 
    250 TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
    251   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH;
    252 
    253   void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
    254 
    255   ASSERT_TRUE(handle != nullptr) << dlerror();
    256 
    257   typedef void *(* dlopen_b_fn)();
    258   dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b");
    259   ASSERT_TRUE(fn != nullptr) << dlerror();
    260 
    261   void *p = fn();
    262   ASSERT_TRUE(p != nullptr) << dlerror();
    263 
    264   dlclose(p);
    265   dlclose(handle);
    266 }
    267 
    268 TEST(dlfcn, dlopen_from_zip_ld_library_path) {
    269   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
    270 
    271   typedef void (*fn_t)(const char*);
    272   fn_t android_update_LD_LIBRARY_PATH =
    273       reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH"));
    274 
    275   ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror();
    276 
    277   void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
    278   ASSERT_TRUE(handle == nullptr);
    279 
    280   android_update_LD_LIBRARY_PATH(lib_path.c_str());
    281 
    282   handle = dlopen("libdlext_test_zip.so", RTLD_NOW);
    283   ASSERT_TRUE(handle != nullptr) << dlerror();
    284 
    285   int (*fn)(void);
    286   fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber"));
    287   ASSERT_TRUE(fn != nullptr);
    288   EXPECT_EQ(4, fn());
    289 
    290   uint32_t* taxicab_number =
    291           reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
    292   ASSERT_DL_NOTNULL(taxicab_number);
    293   EXPECT_EQ(1729U, *taxicab_number);
    294 
    295   dlclose(handle);
    296 }
    297 
    298 
    299 TEST_F(DlExtTest, Reserved) {
    300   void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    301   ASSERT_TRUE(start != MAP_FAILED);
    302   android_dlextinfo extinfo;
    303   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
    304   extinfo.reserved_addr = start;
    305   extinfo.reserved_size = LIBSIZE;
    306   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    307   ASSERT_DL_NOTNULL(handle_);
    308   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    309   ASSERT_DL_NOTNULL(f);
    310   EXPECT_GE(reinterpret_cast<void*>(f), start);
    311   EXPECT_LT(reinterpret_cast<void*>(f),
    312             reinterpret_cast<char*>(start) + LIBSIZE);
    313   EXPECT_EQ(4, f());
    314 
    315   // Check that after dlclose reserved address space is unmapped (and can be reused)
    316   dlclose(handle_);
    317   handle_ = nullptr;
    318 
    319   void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    320   ASSERT_NE(start, new_start) << "dlclose unmapped reserved space";
    321 }
    322 
    323 TEST_F(DlExtTest, ReservedTooSmall) {
    324   void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    325   ASSERT_TRUE(start != MAP_FAILED);
    326   android_dlextinfo extinfo;
    327   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
    328   extinfo.reserved_addr = start;
    329   extinfo.reserved_size = PAGE_SIZE;
    330   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    331   EXPECT_EQ(nullptr, handle_);
    332 }
    333 
    334 TEST_F(DlExtTest, ReservedHint) {
    335   void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    336   ASSERT_TRUE(start != MAP_FAILED);
    337   android_dlextinfo extinfo;
    338   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
    339   extinfo.reserved_addr = start;
    340   extinfo.reserved_size = LIBSIZE;
    341   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    342   ASSERT_DL_NOTNULL(handle_);
    343   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    344   ASSERT_DL_NOTNULL(f);
    345   EXPECT_GE(reinterpret_cast<void*>(f), start);
    346   EXPECT_LT(reinterpret_cast<void*>(f),
    347             reinterpret_cast<char*>(start) + LIBSIZE);
    348   EXPECT_EQ(4, f());
    349 }
    350 
    351 TEST_F(DlExtTest, ReservedHintTooSmall) {
    352   void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    353   ASSERT_TRUE(start != MAP_FAILED);
    354   android_dlextinfo extinfo;
    355   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
    356   extinfo.reserved_addr = start;
    357   extinfo.reserved_size = PAGE_SIZE;
    358   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    359   ASSERT_DL_NOTNULL(handle_);
    360   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    361   ASSERT_DL_NOTNULL(f);
    362   EXPECT_TRUE(reinterpret_cast<void*>(f) < start ||
    363               (reinterpret_cast<void*>(f) >=
    364                reinterpret_cast<char*>(start) + PAGE_SIZE));
    365   EXPECT_EQ(4, f());
    366 }
    367 
    368 TEST_F(DlExtTest, LoadAtFixedAddress) {
    369   void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    370   ASSERT_TRUE(start != MAP_FAILED);
    371   munmap(start, LIBSIZE);
    372 
    373   android_dlextinfo extinfo;
    374   extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
    375   extinfo.reserved_addr = start;
    376 
    377   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    378   ASSERT_DL_NOTNULL(handle_);
    379   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    380   ASSERT_DL_NOTNULL(f);
    381   EXPECT_GE(reinterpret_cast<void*>(f), start);
    382   EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
    383 
    384   EXPECT_EQ(4, f());
    385   dlclose(handle_);
    386   handle_ = nullptr;
    387 
    388   // Check that dlclose unmapped the file
    389   void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    390   ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
    391 }
    392 
    393 TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
    394   void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
    395                          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    396   ASSERT_TRUE(start != MAP_FAILED);
    397   munmap(start, LIBSIZE + PAGE_SIZE);
    398   void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
    399                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    400   ASSERT_TRUE(new_addr != MAP_FAILED);
    401 
    402   android_dlextinfo extinfo;
    403   extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
    404   extinfo.reserved_addr = start;
    405 
    406   handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
    407   ASSERT_TRUE(handle_ == nullptr);
    408 }
    409 
    410 class DlExtRelroSharingTest : public DlExtTest {
    411 protected:
    412   virtual void SetUp() {
    413     DlExtTest::SetUp();
    414     void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    415     ASSERT_TRUE(start != MAP_FAILED);
    416     extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
    417     extinfo_.reserved_addr = start;
    418     extinfo_.reserved_size = LIBSIZE;
    419     extinfo_.relro_fd = -1;
    420   }
    421 
    422   virtual void TearDown() {
    423     DlExtTest::TearDown();
    424   }
    425 
    426   void CreateRelroFile(const char* lib, const char* relro_file) {
    427     int relro_fd = open(relro_file, O_RDWR | O_TRUNC);
    428     ASSERT_NOERROR(relro_fd);
    429 
    430     pid_t pid = fork();
    431     if (pid == 0) {
    432       // child process
    433       extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO;
    434       extinfo_.relro_fd = relro_fd;
    435       void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
    436       if (handle == nullptr) {
    437         fprintf(stderr, "in child: %s\n", dlerror());
    438         exit(1);
    439       }
    440       exit(0);
    441     }
    442 
    443     // continuing in parent
    444     ASSERT_NOERROR(close(relro_fd));
    445     ASSERT_NOERROR(pid);
    446     AssertChildExited(pid, 0);
    447 
    448     // reopen file for reading so it can be used
    449     relro_fd = open(relro_file, O_RDONLY);
    450     ASSERT_NOERROR(relro_fd);
    451     extinfo_.flags |= ANDROID_DLEXT_USE_RELRO;
    452     extinfo_.relro_fd = relro_fd;
    453   }
    454 
    455   void TryUsingRelro(const char* lib) {
    456     handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
    457     ASSERT_DL_NOTNULL(handle_);
    458     fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
    459     ASSERT_DL_NOTNULL(f);
    460     EXPECT_EQ(4, f());
    461 
    462     uint32_t* taxicab_number =
    463             reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
    464     ASSERT_DL_NOTNULL(taxicab_number);
    465     EXPECT_EQ(1729U, *taxicab_number);
    466   }
    467 
    468   void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out);
    469 
    470   android_dlextinfo extinfo_;
    471 };
    472 
    473 TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) {
    474   TemporaryFile tf; // Use tf to get an unique filename.
    475   ASSERT_NOERROR(close(tf.fd));
    476 
    477   ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
    478   ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
    479 
    480   // Use destructor of tf to close and unlink the file.
    481   tf.fd = extinfo_.relro_fd;
    482 }
    483 
    484 TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) {
    485   TemporaryFile tf; // // Use tf to get an unique filename.
    486   ASSERT_NOERROR(close(tf.fd));
    487 
    488   ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
    489   ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
    490 
    491   // Use destructor of tf to close and unlink the file.
    492   tf.fd = extinfo_.relro_fd;
    493 }
    494 
    495 TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
    496   ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
    497 }
    498 
    499 TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
    500   if (geteuid() != 0) {
    501     GTEST_LOG_(INFO) << "This test must be run as root.\n";
    502     return;
    503   }
    504 
    505   TemporaryFile tf; // Use tf to get an unique filename.
    506   ASSERT_NOERROR(close(tf.fd));
    507 
    508   ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
    509 
    510   int pipefd[2];
    511   ASSERT_NOERROR(pipe(pipefd));
    512 
    513   size_t without_sharing, with_sharing;
    514   ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
    515   ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
    516 
    517   // We expect the sharing to save at least 10% of the total PSS. In practice
    518   // it saves 40%+ for this test.
    519   size_t expected_size = without_sharing - (without_sharing/10);
    520   EXPECT_LT(with_sharing, expected_size);
    521 
    522   // Use destructor of tf to close and unlink the file.
    523   tf.fd = extinfo_.relro_fd;
    524 }
    525 
    526 void getPss(pid_t pid, size_t* pss_out) {
    527   pm_kernel_t* kernel;
    528   ASSERT_EQ(0, pm_kernel_create(&kernel));
    529 
    530   pm_process_t* process;
    531   ASSERT_EQ(0, pm_process_create(kernel, pid, &process));
    532 
    533   pm_map_t** maps;
    534   size_t num_maps;
    535   ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
    536 
    537   size_t total_pss = 0;
    538   for (size_t i = 0; i < num_maps; i++) {
    539     pm_memusage_t usage;
    540     ASSERT_EQ(0, pm_map_usage(maps[i], &usage));
    541     total_pss += usage.pss;
    542   }
    543   *pss_out = total_pss;
    544 
    545   free(maps);
    546   pm_process_destroy(process);
    547   pm_kernel_destroy(kernel);
    548 }
    549 
    550 void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro,
    551                                                        size_t* pss_out) {
    552   const int CHILDREN = 20;
    553 
    554   // Create children
    555   pid_t child_pids[CHILDREN];
    556   int childpipe[CHILDREN];
    557   for (int i=0; i<CHILDREN; ++i) {
    558     char read_buf;
    559     int child_done_pipe[2], parent_done_pipe[2];
    560     ASSERT_NOERROR(pipe(child_done_pipe));
    561     ASSERT_NOERROR(pipe(parent_done_pipe));
    562 
    563     pid_t child = fork();
    564     if (child == 0) {
    565       // close the 'wrong' ends of the pipes in the child
    566       close(child_done_pipe[0]);
    567       close(parent_done_pipe[1]);
    568 
    569       // open the library
    570       void* handle;
    571       if (share_relro) {
    572         handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
    573       } else {
    574         handle = dlopen(lib, RTLD_NOW);
    575       }
    576       if (handle == nullptr) {
    577         fprintf(stderr, "in child: %s\n", dlerror());
    578         exit(1);
    579       }
    580 
    581       // close write end of child_done_pipe to signal the parent that we're done.
    582       close(child_done_pipe[1]);
    583 
    584       // wait for the parent to close parent_done_pipe, then exit
    585       read(parent_done_pipe[0], &read_buf, 1);
    586       exit(0);
    587     }
    588 
    589     ASSERT_NOERROR(child);
    590 
    591     // close the 'wrong' ends of the pipes in the parent
    592     close(child_done_pipe[1]);
    593     close(parent_done_pipe[0]);
    594 
    595     // wait for the child to be done
    596     read(child_done_pipe[0], &read_buf, 1);
    597     close(child_done_pipe[0]);
    598 
    599     // save the child's pid and the parent_done_pipe
    600     child_pids[i] = child;
    601     childpipe[i] = parent_done_pipe[1];
    602   }
    603 
    604   // Sum the PSS of all the children
    605   size_t total_pss = 0;
    606   for (int i=0; i<CHILDREN; ++i) {
    607     size_t child_pss;
    608     ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
    609     total_pss += child_pss;
    610   }
    611   *pss_out = total_pss;
    612 
    613   // Close pipes and wait for children to exit
    614   for (int i=0; i<CHILDREN; ++i) {
    615     ASSERT_NOERROR(close(childpipe[i]));
    616   }
    617   for (int i = 0; i < CHILDREN; ++i) {
    618     AssertChildExited(child_pids[i], 0);
    619   }
    620 }
    621 
    622 // Testing namespaces
    623 static const char* g_public_lib = "libnstest_public.so";
    624 
    625 TEST(dlext, ns_smoke) {
    626   static const char* root_lib = "libnstest_root.so";
    627   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
    628 
    629   ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr));
    630   ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
    631                "a library with soname \"libnstest_public.so\" was not found in the "
    632                "default namespace",
    633                dlerror());
    634 
    635   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
    636 
    637   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
    638   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
    639   ASSERT_TRUE(handle_public != nullptr) << dlerror();
    640 
    641   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
    642 
    643   // Check that libraries added to public namespace are NODELETE
    644   dlclose(handle_public);
    645   handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
    646                          RTLD_NOW | RTLD_NOLOAD);
    647 
    648   ASSERT_TRUE(handle_public != nullptr) << dlerror();
    649 
    650   android_namespace_t* ns1 =
    651           android_create_namespace("private", nullptr,
    652                                    (lib_path + "/private_namespace_libs").c_str(),
    653                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
    654   ASSERT_TRUE(ns1 != nullptr) << dlerror();
    655 
    656   android_namespace_t* ns2 =
    657           android_create_namespace("private_isolated", nullptr,
    658                                    (lib_path + "/private_namespace_libs").c_str(),
    659                                    ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
    660   ASSERT_TRUE(ns2 != nullptr) << dlerror();
    661 
    662   // This should not have affect search path for default namespace:
    663   ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
    664   void* handle = dlopen(g_public_lib, RTLD_NOW);
    665   ASSERT_TRUE(handle != nullptr) << dlerror();
    666   dlclose(handle);
    667 
    668   android_dlextinfo extinfo;
    669   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
    670   extinfo.library_namespace = ns1;
    671 
    672   void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    673   ASSERT_TRUE(handle1 != nullptr) << dlerror();
    674 
    675   extinfo.library_namespace = ns2;
    676   void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    677   ASSERT_TRUE(handle2 != nullptr) << dlerror();
    678 
    679   ASSERT_TRUE(handle1 != handle2);
    680 
    681   // dlopen for a public library using an absolute path should work for isolated namespaces
    682   extinfo.library_namespace = ns2;
    683   handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo);
    684   ASSERT_TRUE(handle != nullptr) << dlerror();
    685   ASSERT_TRUE(handle == handle_public);
    686 
    687   dlclose(handle);
    688 
    689   typedef const char* (*fn_t)();
    690 
    691   fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
    692   ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror();
    693   fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
    694   ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror();
    695 
    696   EXPECT_STREQ("This string is local to root library", ns_get_local_string1());
    697   EXPECT_STREQ("This string is local to root library", ns_get_local_string2());
    698 
    699   ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2());
    700 
    701   fn_t ns_get_private_extern_string1 =
    702           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
    703   ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror();
    704   fn_t ns_get_private_extern_string2 =
    705           reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
    706   ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror();
    707 
    708   EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1());
    709   EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
    710 
    711   ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2());
    712 
    713   fn_t ns_get_public_extern_string1 =
    714           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
    715   ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror();
    716   fn_t ns_get_public_extern_string2 =
    717           reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
    718   ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror();
    719 
    720   EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1());
    721   ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2());
    722 
    723   // and now check that dlopen() does the right thing in terms of preserving namespace
    724   fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
    725   ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror();
    726   fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
    727   ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror();
    728 
    729   EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1());
    730   EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
    731 
    732   ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2());
    733 
    734   dlclose(handle1);
    735 
    736   // Check if handle2 is still alive (and well)
    737   ASSERT_STREQ("This string is local to root library", ns_get_local_string2());
    738   ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2());
    739   ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2());
    740   ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2());
    741 
    742   dlclose(handle2);
    743 }
    744 
    745 TEST(dlext, ns_isolated) {
    746   static const char* root_lib = "libnstest_root_not_isolated.so";
    747   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
    748 
    749   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
    750   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
    751   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
    752   ASSERT_TRUE(handle_public != nullptr) << dlerror();
    753 
    754   android_set_application_target_sdk_version(42U); // something > 23
    755 
    756   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
    757 
    758   android_namespace_t* ns_not_isolated =
    759           android_create_namespace("private", nullptr,
    760                                    (lib_path + "/private_namespace_libs").c_str(),
    761                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
    762   ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
    763 
    764   android_namespace_t* ns_isolated =
    765           android_create_namespace("private_isolated1",
    766                                    nullptr,
    767                                    (lib_path + "/private_namespace_libs").c_str(),
    768                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
    769                                    nullptr,
    770                                    nullptr);
    771   ASSERT_TRUE(ns_isolated != nullptr) << dlerror();
    772 
    773   android_namespace_t* ns_isolated2 =
    774           android_create_namespace("private_isolated2",
    775                                    (lib_path + "/private_namespace_libs").c_str(),
    776                                    nullptr,
    777                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
    778                                    lib_path.c_str(),
    779                                    nullptr);
    780   ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
    781 
    782   ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
    783   ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
    784 
    785   std::string lib_private_external_path =
    786       lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
    787 
    788   // Load lib_private_external_path to default namespace
    789   // (it should remain invisible for the isolated namespaces after this)
    790   void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
    791   ASSERT_TRUE(handle != nullptr) << dlerror();
    792 
    793   android_dlextinfo extinfo;
    794   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
    795   extinfo.library_namespace = ns_not_isolated;
    796 
    797   void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    798   ASSERT_TRUE(handle1 != nullptr) << dlerror();
    799 
    800   extinfo.library_namespace = ns_isolated;
    801 
    802   void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    803   ASSERT_TRUE(handle2 == nullptr);
    804   ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
    805 
    806   // Check dlopen by absolute path
    807   handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
    808   ASSERT_TRUE(handle2 == nullptr);
    809   ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
    810             " or dlopened by \"" + get_executable_path() +  "\" is not accessible"
    811             " for the namespace \"private_isolated1\"", dlerror());
    812 
    813   extinfo.library_namespace = ns_isolated2;
    814 
    815   // this should work because isolation_path for private_isolated2 includes lib_path
    816   handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    817   ASSERT_TRUE(handle2 != nullptr) << dlerror();
    818   dlclose(handle2);
    819 
    820   // Check dlopen by absolute path
    821   handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
    822   ASSERT_TRUE(handle2 != nullptr) << dlerror();
    823   dlclose(handle2);
    824 
    825   typedef const char* (*fn_t)();
    826   fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
    827   ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
    828 
    829   ASSERT_STREQ("This string is local to root library", ns_get_local_string());
    830 
    831   fn_t ns_get_private_extern_string =
    832           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
    833   ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
    834 
    835   ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
    836 
    837   fn_t ns_get_public_extern_string =
    838           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
    839   ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
    840 
    841   ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
    842 
    843   fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
    844   ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
    845 
    846   ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
    847 
    848   dlclose(handle1);
    849 }
    850 
    851 TEST(dlext, ns_shared) {
    852   static const char* root_lib = "libnstest_root_not_isolated.so";
    853   static const char* root_lib_isolated = "libnstest_root.so";
    854   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
    855 
    856   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
    857   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
    858   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
    859   ASSERT_TRUE(handle_public != nullptr) << dlerror();
    860 
    861   android_set_application_target_sdk_version(42U); // something > 23
    862 
    863   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
    864 
    865   // preload this library to the default namespace to check if it
    866   // is shared later on.
    867   void* handle_dlopened =
    868           dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
    869   ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
    870 
    871   android_namespace_t* ns_not_isolated =
    872           android_create_namespace("private", nullptr,
    873                                    (lib_path + "/private_namespace_libs").c_str(),
    874                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
    875   ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
    876 
    877   android_namespace_t* ns_isolated_shared =
    878           android_create_namespace("private_isolated_shared", nullptr,
    879                                    (lib_path + "/private_namespace_libs").c_str(),
    880                                    ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
    881                                    nullptr, nullptr);
    882   ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
    883 
    884   ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr);
    885   ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
    886 
    887   std::string lib_private_external_path =
    888       lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
    889 
    890   // Load lib_private_external_path to default namespace
    891   // (it should remain invisible for the isolated namespaces after this)
    892   void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW);
    893   ASSERT_TRUE(handle != nullptr) << dlerror();
    894 
    895   android_dlextinfo extinfo;
    896   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
    897   extinfo.library_namespace = ns_not_isolated;
    898 
    899   void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    900   ASSERT_TRUE(handle1 != nullptr) << dlerror();
    901 
    902   extinfo.library_namespace = ns_isolated_shared;
    903 
    904   void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
    905   ASSERT_TRUE(handle2 == nullptr);
    906   ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror());
    907 
    908   // Check dlopen by absolute path
    909   handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo);
    910   ASSERT_TRUE(handle2 == nullptr);
    911   ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed"
    912             " or dlopened by \"" + get_executable_path() + "\" is not accessible"
    913             " for the namespace \"private_isolated_shared\"", dlerror());
    914 
    915   // load libnstest_root.so to shared namespace in order to check that everything is different
    916   // except shared libnstest_dlopened.so
    917 
    918   handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo);
    919 
    920   typedef const char* (*fn_t)();
    921   fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string"));
    922   ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror();
    923   fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string"));
    924   ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror();
    925 
    926   ASSERT_STREQ("This string is local to root library", ns_get_local_string());
    927   ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared());
    928   ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared());
    929 
    930   fn_t ns_get_private_extern_string =
    931           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string"));
    932   ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror();
    933   fn_t ns_get_private_extern_string_shared =
    934           reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string"));
    935   ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror();
    936 
    937   ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string());
    938   ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared());
    939   ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared());
    940 
    941   fn_t ns_get_public_extern_string =
    942           reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string"));
    943   ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror();
    944   fn_t ns_get_public_extern_string_shared =
    945           reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string"));
    946   ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror();
    947 
    948   ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string());
    949   ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared());
    950   ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared());
    951 
    952   fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string"));
    953   ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror();
    954   fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string"));
    955   ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror();
    956   const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string"));
    957   ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror();
    958 
    959   ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string());
    960   ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string);
    961   ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared());
    962   ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared());
    963   ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared());
    964 
    965   dlclose(handle1);
    966   dlclose(handle2);
    967 }
    968 
    969 TEST(dlext, ns_shared_dlclose) {
    970   std::string path = "libc.so:libc++.so:libdl.so:libm.so";
    971 
    972   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
    973 
    974   android_set_application_target_sdk_version(42U); // something > 23
    975 
    976   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
    977 
    978   // preload this library to the default namespace to check if it
    979   // is shared later on.
    980   void* handle_dlopened =
    981           dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
    982   ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
    983 
    984   android_namespace_t* ns_isolated_shared =
    985           android_create_namespace("private_isolated_shared", nullptr,
    986                                    (lib_path + "/private_namespace_libs").c_str(),
    987                                    ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
    988                                    nullptr, nullptr);
    989   ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
    990 
    991   // Check if "libnstest_dlopened.so" is loaded (and the same)
    992   android_dlextinfo extinfo;
    993   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
    994   extinfo.library_namespace = ns_isolated_shared;
    995 
    996   void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
    997   ASSERT_TRUE(handle != nullptr) << dlerror();
    998   ASSERT_TRUE(handle == handle_dlopened);
    999   dlclose(handle);
   1000   dlclose(handle_dlopened);
   1001 
   1002   // And now check that the library cannot be found by soname (and is no longer loaded)
   1003   handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo);
   1004   ASSERT_TRUE(handle == nullptr)
   1005       << "Error: libnstest_dlopened.so is still accessible in shared namespace";
   1006 
   1007   handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
   1008                               RTLD_NOW | RTLD_NOLOAD, &extinfo);
   1009   ASSERT_TRUE(handle == nullptr)
   1010       << "Error: libnstest_dlopened.so is still accessible in shared namespace";
   1011 
   1012   handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD);
   1013   ASSERT_TRUE(handle == nullptr)
   1014       << "Error: libnstest_dlopened.so is still accessible in default namespace";
   1015 
   1016   handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
   1017                   RTLD_NOW | RTLD_NOLOAD);
   1018   ASSERT_TRUE(handle == nullptr)
   1019       << "Error: libnstest_dlopened.so is still accessible in default namespace";
   1020 
   1021   // Now lets see if the soinfo area gets reused in the wrong way:
   1022   // load a library to default namespace.
   1023   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
   1024   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   1025   ASSERT_TRUE(handle_public != nullptr) << dlerror();
   1026 
   1027   // try to find it in shared namespace
   1028   handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo);
   1029   ASSERT_TRUE(handle == nullptr)
   1030       << "Error: " << g_public_lib << " is accessible in shared namespace";
   1031 }
   1032 
   1033 TEST(dlext, ns_isolated_rtld_global) {
   1034   static const char* root_lib = "libnstest_root.so";
   1035   std::string path = "libc.so:libc++.so:libdl.so:libm.so";
   1036 
   1037   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
   1038 
   1039   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
   1040 
   1041   const std::string lib_public_path = lib_path + "/public_namespace_libs";
   1042 
   1043   android_namespace_t* ns1 =
   1044           android_create_namespace("isolated1",
   1045                                    nullptr,
   1046                                    (lib_path + "/private_namespace_libs").c_str(),
   1047                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
   1048                                    lib_public_path.c_str(),
   1049                                    nullptr);
   1050   ASSERT_TRUE(ns1 != nullptr) << dlerror();
   1051 
   1052   android_namespace_t* ns2 =
   1053           android_create_namespace("isolated2",
   1054                                    nullptr,
   1055                                    (lib_path + "/private_namespace_libs").c_str(),
   1056                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
   1057                                    lib_public_path.c_str(),
   1058                                    nullptr);
   1059   ASSERT_TRUE(ns2 != nullptr) << dlerror();
   1060 
   1061   android_dlextinfo extinfo;
   1062   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
   1063   extinfo.library_namespace = ns1;
   1064 
   1065   void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(),
   1066                                            RTLD_GLOBAL,
   1067                                            &extinfo);
   1068 
   1069   ASSERT_TRUE(handle_global != nullptr) << dlerror();
   1070 
   1071   android_namespace_t* ns1_child =
   1072         android_create_namespace("isolated1_child",
   1073                                  nullptr,
   1074                                  (lib_path + "/private_namespace_libs").c_str(),
   1075                                  ANDROID_NAMESPACE_TYPE_ISOLATED,
   1076                                  nullptr,
   1077                                  ns1);
   1078 
   1079   // Now - only ns1 and ns1 child should be able to dlopen root_lib
   1080   // attempt to use ns2 should result in dlerror()
   1081 
   1082   // Check ns1_child first.
   1083   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
   1084   extinfo.library_namespace = ns1_child;
   1085 
   1086   void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
   1087   ASSERT_TRUE(handle1 != nullptr) << dlerror();
   1088 
   1089   // now ns1
   1090   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
   1091   extinfo.library_namespace = ns1;
   1092 
   1093   handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
   1094   ASSERT_TRUE(handle1 != nullptr) << dlerror();
   1095 
   1096   // and ns2 should fail
   1097   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
   1098   extinfo.library_namespace = ns2;
   1099 
   1100   handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
   1101   ASSERT_TRUE(handle1 == nullptr);
   1102   ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
   1103 }
   1104 
   1105 TEST(dlext, ns_anonymous) {
   1106   static const char* root_lib = "libnstest_root.so";
   1107   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
   1108 
   1109   const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
   1110 
   1111   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
   1112   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   1113 
   1114   ASSERT_TRUE(handle_public != nullptr) << dlerror();
   1115 
   1116   ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
   1117       << dlerror();
   1118 
   1119   android_namespace_t* ns = android_create_namespace(
   1120                                 "private", nullptr,
   1121                                 (lib_path + "/private_namespace_libs").c_str(),
   1122                                 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
   1123 
   1124   ASSERT_TRUE(ns != nullptr) << dlerror();
   1125 
   1126   std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
   1127 
   1128   android_dlextinfo extinfo;
   1129   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
   1130   extinfo.library_namespace = ns;
   1131 
   1132   // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string
   1133   void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo);
   1134   ASSERT_TRUE(handle != nullptr) << dlerror();
   1135 
   1136   uintptr_t ns_get_dlopened_string_addr =
   1137       reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string"));
   1138   ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror();
   1139   typedef const char* (*fn_t)();
   1140   fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr);
   1141 
   1142   std::vector<map_record> maps;
   1143   Maps::parse_maps(&maps);
   1144 
   1145   uintptr_t addr_start = 0;
   1146   uintptr_t addr_end = 0;
   1147   std::vector<map_record> maps_to_copy;
   1148 
   1149   for (const auto& rec : maps) {
   1150     if (rec.pathname == private_library_absolute_path) {
   1151       if (addr_start == 0) {
   1152         addr_start = rec.addr_start;
   1153       }
   1154       addr_end = rec.addr_end;
   1155 
   1156       maps_to_copy.push_back(rec);
   1157     }
   1158   }
   1159 
   1160   // some sanity checks..
   1161   ASSERT_TRUE(addr_start > 0);
   1162   ASSERT_TRUE(addr_end > 0);
   1163   ASSERT_EQ(3U, maps_to_copy.size());
   1164   ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start);
   1165   ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end);
   1166 
   1167   // copy
   1168   uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start,
   1169                                                              PROT_NONE, MAP_ANON | MAP_PRIVATE,
   1170                                                              -1, 0));
   1171   ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED);
   1172 
   1173   for (const auto& rec : maps_to_copy) {
   1174     uintptr_t offset = rec.addr_start - addr_start;
   1175     size_t size = rec.addr_end - rec.addr_start;
   1176     void* addr = reinterpret_cast<void*>(reserved_addr + offset);
   1177     void* map = mmap(addr, size, PROT_READ | PROT_WRITE,
   1178                      MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0);
   1179     ASSERT_TRUE(map != MAP_FAILED);
   1180     memcpy(map, reinterpret_cast<void*>(rec.addr_start), size);
   1181     mprotect(map, size, rec.perms);
   1182   }
   1183 
   1184   // call the function copy
   1185   uintptr_t ns_get_dlopened_string_offset  = ns_get_dlopened_string_addr - addr_start;
   1186   fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset);
   1187   ASSERT_STREQ("This string is from private namespace (dlopened library)",
   1188                ns_get_dlopened_string_anon());
   1189 
   1190   // They should belong to different namespaces (private and anonymous)
   1191   ASSERT_STREQ("This string is from private namespace (dlopened library)",
   1192                ns_get_dlopened_string_private());
   1193 
   1194   ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private());
   1195 }
   1196 
   1197 TEST(dlext, dlopen_handle_value_platform) {
   1198   void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
   1199   ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0)
   1200           << "dlopen should return odd value for the handle";
   1201   dlclose(handle);
   1202 }
   1203 
   1204 TEST(dlext, dlopen_handle_value_app_compat) {
   1205   android_set_application_target_sdk_version(23);
   1206   void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL);
   1207   ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0)
   1208           << "dlopen should return valid pointer";
   1209   dlclose(handle);
   1210 }
   1211 
   1212