Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2012 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 <libgen.h>
     21 #include <limits.h>
     22 #include <stdio.h>
     23 #include <stdint.h>
     24 
     25 #include <string>
     26 
     27 #include "gtest_globals.h"
     28 #include "utils.h"
     29 
     30 extern "C" int main_global_default_serial() {
     31   return 3370318;
     32 }
     33 
     34 extern "C" int main_global_protected_serial() {
     35   return 2716057;
     36 }
     37 
     38 // The following functions are defined in DT_NEEDED
     39 // libdl_preempt_test.so library.
     40 
     41 // This one calls main_global_default_serial
     42 extern "C" int main_global_default_get_serial();
     43 
     44 // This one calls main_global_protected_serial
     45 extern "C" int main_global_protected_get_serial();
     46 
     47 // This one calls lib_global_default_serial
     48 extern "C" int lib_global_default_get_serial();
     49 
     50 // This one calls lib_global_protected_serial
     51 extern "C" int lib_global_protected_get_serial();
     52 
     53 // This test verifies that the global default function
     54 // main_global_default_serial() is preempted by
     55 // the function defined above.
     56 TEST(dl, main_preempts_global_default) {
     57   ASSERT_EQ(3370318, main_global_default_get_serial());
     58 }
     59 
     60 // This one makes sure that the global protected
     61 // symbols do not get preempted
     62 TEST(dl, main_does_not_preempt_global_protected) {
     63   ASSERT_EQ(3370318, main_global_protected_get_serial());
     64 }
     65 
     66 // check same things for lib
     67 TEST(dl, lib_preempts_global_default) {
     68   ASSERT_EQ(3370318, lib_global_default_get_serial());
     69 }
     70 
     71 TEST(dl, lib_does_not_preempt_global_protected) {
     72   ASSERT_EQ(3370318, lib_global_protected_get_serial());
     73 }
     74 
     75 TEST(dl, exec_linker) {
     76 #if defined(__BIONIC__)
     77 #if defined(__LP64__)
     78   static constexpr const char* kPathToLinker = "/system/bin/linker64";
     79 #else
     80   static constexpr const char* kPathToLinker = "/system/bin/linker";
     81 #endif
     82   ExecTestHelper eth;
     83   std::string expected_output = std::string("This is ") + kPathToLinker +
     84                                 ", the helper program for dynamic executables.\n";
     85   eth.SetArgs( { kPathToLinker, nullptr });
     86   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
     87 #endif
     88 }
     89 
     90 TEST(dl, preinit_system_calls) {
     91 #if defined(__BIONIC__)
     92   std::string helper = get_testlib_root() +
     93       "/preinit_syscall_test_helper/preinit_syscall_test_helper";
     94   chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
     95   ExecTestHelper eth;
     96   eth.SetArgs({ helper.c_str(), nullptr });
     97   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
     98 #endif
     99 }
    100 
    101 TEST(dl, xfail_preinit_getauxval) {
    102 #if defined(__BIONIC__)
    103   std::string helper = get_testlib_root() +
    104       "/preinit_getauxval_test_helper/preinit_getauxval_test_helper";
    105   chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
    106   ExecTestHelper eth;
    107   eth.SetArgs({ helper.c_str(), nullptr });
    108   eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
    109 #endif
    110 }
    111 
    112 // TODO: Add tests for LD_PRELOADs
    113