blob: ee9b2e1d9d205c3e770fca407c4b53e4c3c49587 [file] [log] [blame]
Dmitriy Ivanov0416d882014-11-04 09:38:18 -08001/*
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
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080027#include "utils.h"
28
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080029extern "C" int main_global_default_serial() {
30 return 3370318;
31}
32
33extern "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
41extern "C" int main_global_default_get_serial();
42
43// This one calls main_global_protected_serial
44extern "C" int main_global_protected_get_serial();
45
46// This one calls lib_global_default_serial
47extern "C" int lib_global_default_get_serial();
48
49// This one calls lib_global_protected_serial
50extern "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.
55TEST(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
61TEST(dl, main_does_not_preempt_global_protected) {
62 ASSERT_EQ(3370318, main_global_protected_get_serial());
63}
64
65// check same things for lib
66TEST(dl, lib_preempts_global_default) {
67 ASSERT_EQ(3370318, lib_global_default_get_serial());
68}
69
70TEST(dl, lib_does_not_preempt_global_protected) {
71 ASSERT_EQ(3370318, lib_global_protected_get_serial());
72}
73
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080074TEST(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
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080089// TODO: Add tests for LD_PRELOADs