OSDN Git Service

Merge "Expand whitelist"
[android-x86/bionic.git] / tests / dl_test.cpp
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 "utils.h"
28
29 extern "C" int main_global_default_serial() {
30   return 3370318;
31 }
32
33 extern "C" int main_global_protected_serial() {
34   return 2716057;
35 }
36
37 // The following functions are defined in DT_NEEDED
38 // libdl_preempt_test.so library.
39
40 // This one calls main_global_default_serial
41 extern "C" int main_global_default_get_serial();
42
43 // This one calls main_global_protected_serial
44 extern "C" int main_global_protected_get_serial();
45
46 // This one calls lib_global_default_serial
47 extern "C" int lib_global_default_get_serial();
48
49 // This one calls lib_global_protected_serial
50 extern "C" int lib_global_protected_get_serial();
51
52 // This test verifies that the global default function
53 // main_global_default_serial() is preempted by
54 // the function defined above.
55 TEST(dl, main_preempts_global_default) {
56   ASSERT_EQ(3370318, main_global_default_get_serial());
57 }
58
59 // This one makes sure that the global protected
60 // symbols do not get preempted
61 TEST(dl, main_does_not_preempt_global_protected) {
62   ASSERT_EQ(3370318, main_global_protected_get_serial());
63 }
64
65 // check same things for lib
66 TEST(dl, lib_preempts_global_default) {
67   ASSERT_EQ(3370318, lib_global_default_get_serial());
68 }
69
70 TEST(dl, lib_does_not_preempt_global_protected) {
71   ASSERT_EQ(3370318, lib_global_protected_get_serial());
72 }
73
74 TEST(dl, exec_linker) {
75 #if defined(__BIONIC__)
76 #if defined(__LP64__)
77   static constexpr const char* kPathToLinker = "/system/bin/linker64";
78 #else
79   static constexpr const char* kPathToLinker = "/system/bin/linker";
80 #endif
81   ExecTestHelper eth;
82   std::string expected_output = std::string("This is ") + kPathToLinker +
83                                 ", the helper program for dynamic executables.\n";
84   eth.SetArgs( { kPathToLinker, nullptr });
85   eth.Run([&]() { execve(kPathToLinker, eth.GetArgs(), eth.GetEnv()); }, 0, expected_output.c_str());
86 #endif
87 }
88
89 // TODO: Add tests for LD_PRELOADs