aboutsummaryrefslogtreecommitdiff
path: root/tests/link_test.cpp
blob: 127a3d9896e476b50f39528d8161e83c79a33a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2017 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <gtest/gtest.h>

#include <dlfcn.h>
#include <link.h>
#if __has_include(<sys/auxv.h>)
#include <sys/auxv.h>
#endif

#include <string>
#include <unordered_map>

TEST(link, dl_iterate_phdr_early_exit) {
  static size_t call_count = 0;
  ASSERT_EQ(123, dl_iterate_phdr([](dl_phdr_info*, size_t, void*) { ++call_count; return 123; },
                                 nullptr));
  ASSERT_EQ(1u, call_count);
}

TEST(link, dl_iterate_phdr) {
  struct Functor {
    static int Callback(dl_phdr_info* i, size_t s, void* data) {
      static_cast<Functor*>(data)->DoChecks(i, s);
      return 0;
    }
    void DoChecks(dl_phdr_info* info, size_t s) {
      ASSERT_EQ(sizeof(dl_phdr_info), s);

      ASSERT_TRUE(info->dlpi_name != nullptr);

      // An ELF file must have at least a PT_LOAD program header.
      ASSERT_NE(nullptr, info->dlpi_phdr);
      ASSERT_NE(0, info->dlpi_phnum);

      // Find the first PT_LOAD program header so we can find the ELF header.
      bool found_load = false;
      for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) {
        const ElfW(Phdr)* phdr = reinterpret_cast<const ElfW(Phdr)*>(&info->dlpi_phdr[i]);
        if (phdr->p_type == PT_LOAD) {
          const ElfW(Ehdr)* ehdr = reinterpret_cast<const ElfW(Ehdr)*>(info->dlpi_addr +
                                                                       phdr->p_vaddr);
          // Does it look like an ELF file?
          ASSERT_EQ(0, memcmp(ehdr, ELFMAG, SELFMAG));
          // Does the e_phnum match what dl_iterate_phdr told us?
          ASSERT_EQ(info->dlpi_phnum, ehdr->e_phnum);
          found_load = true;
          break;
        }
      }
      ASSERT_EQ(true, found_load);
    }
    size_t count;
  } f = {};
  ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));
}

// Verify that the module load/unload counters from dl_iterate_phdr are incremented.
TEST(link, dl_iterate_phdr_counters) {
  struct Counters {
    bool inited = false;
    uint64_t adds = 0;
    uint64_t subs = 0;
  };

  auto get_adds_subs = []() {
    auto callback = [](dl_phdr_info* info, size_t size, void* data) {
      Counters& counters = *static_cast<Counters*>(data);
      EXPECT_GE(size, sizeof(dl_phdr_info));
      if (!counters.inited) {
        counters.inited = true;
        counters.adds = info->dlpi_adds;
        counters.subs = info->dlpi_subs;
      } else {
        // The counters have the same value for each module.
        EXPECT_EQ(counters.adds, info->dlpi_adds);
        EXPECT_EQ(counters.subs, info->dlpi_subs);
      }
      return 0;
    };

    Counters counters {};
    EXPECT_EQ(0, dl_iterate_phdr(callback, &counters));
    EXPECT_TRUE(counters.inited);
    return counters;
  };

  // dlopen increments the 'adds' counter.
  const auto before_dlopen = get_adds_subs();
  void* const handle = dlopen("libtest_empty.so", RTLD_NOW);
  ASSERT_NE(nullptr, handle);
  const auto after_dlopen = get_adds_subs();
  ASSERT_LT(before_dlopen.adds, after_dlopen.adds);
  ASSERT_EQ(before_dlopen.subs, after_dlopen.subs);

  // dlclose increments the 'subs' counter.
  const auto before_dlclose = after_dlopen;
  dlclose(handle);
  const auto after_dlclose = get_adds_subs();
  ASSERT_EQ(before_dlclose.adds, after_dlclose.adds);
  ASSERT_LT(before_dlclose.subs, after_dlclose.subs);
}

struct ProgHdr {
  const ElfW(Phdr)* table;
  size_t size;
};

__attribute__((__unused__))
static ElfW(Addr) find_exe_load_bias(const ProgHdr& phdr) {
  for (size_t i = 0; i < phdr.size; ++i) {
    if (phdr.table[i].p_type == PT_PHDR) {
      return reinterpret_cast<ElfW(Addr)>(phdr.table) - phdr.table[i].p_vaddr;
    }
  }
  return 0;
}

__attribute__((__unused__))
static ElfW(Dyn)* find_dynamic(const ProgHdr& phdr, ElfW(Addr) load_bias) {
  for (size_t i = 0; i < phdr.size; ++i) {
    if (phdr.table[i].p_type == PT_DYNAMIC) {
      return reinterpret_cast<ElfW(Dyn)*>(phdr.table[i].p_vaddr + load_bias);
    }
  }
  return nullptr;
}

__attribute__((__unused__))
static r_debug* find_exe_r_debug(ElfW(Dyn)* dynamic) {
  for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
    if (d->d_tag == DT_DEBUG) {
      return reinterpret_cast<r_debug*>(d->d_un.d_val);
    }
  }
  return nullptr;
}

// Walk the DT_DEBUG/_r_debug global module list and compare it with the same
// information from dl_iterate_phdr. Verify that the executable appears first
// in _r_debug.
TEST(link, r_debug) {
#if __has_include(<sys/auxv.h>)
  // Find the executable's PT_DYNAMIC segment and DT_DEBUG value. The linker
  // will write the address of its _r_debug global into the .dynamic section.
  ProgHdr exe_phdr = {
    .table = reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)),
    .size = getauxval(AT_PHNUM)
  };
  ASSERT_NE(nullptr, exe_phdr.table);
  ElfW(Addr) exe_load_bias = find_exe_load_bias(exe_phdr);
  ASSERT_NE(0u, exe_load_bias);
  ElfW(Dyn)* exe_dynamic = find_dynamic(exe_phdr, exe_load_bias);
  ASSERT_NE(nullptr, exe_dynamic);
  r_debug* dbg = find_exe_r_debug(exe_dynamic);
  ASSERT_NE(nullptr, dbg);

  // Use dl_iterate_phdr to build a table mapping from load bias values to
  // solib names and PT_DYNAMIC segments.
  struct DlIterateInfo {
    std::string name;
    ElfW(Dyn)* dynamic;
  };
  struct Functor {
    std::unordered_map<ElfW(Addr), DlIterateInfo> dl_iter_mods;
    static int Callback(dl_phdr_info* i, size_t s, void* data) {
      static_cast<Functor*>(data)->AddModule(i, s);
      return 0;
    }
    void AddModule(dl_phdr_info* info, size_t s) {
      ASSERT_EQ(sizeof(dl_phdr_info), s);
      ASSERT_TRUE(dl_iter_mods.find(info->dlpi_addr) == dl_iter_mods.end());
      ASSERT_TRUE(info->dlpi_name != nullptr);
      dl_iter_mods[info->dlpi_addr] = {
        .name = info->dlpi_name,
        .dynamic = find_dynamic({ info->dlpi_phdr, info->dlpi_phnum }, info->dlpi_addr)
      };
    }
  } f = {};
  ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));

  size_t map_size = 0;

  for (link_map* map = dbg->r_map; map != nullptr; map = map->l_next) {
    ASSERT_NE(0u, map->l_addr);
    ASSERT_NE(nullptr, map->l_ld);
    ASSERT_NE(nullptr, map->l_name);

    auto it = f.dl_iter_mods.find(map->l_addr);
    ASSERT_TRUE(it != f.dl_iter_mods.end());
    const DlIterateInfo& info = it->second;
    ASSERT_EQ(info.name, map->l_name);
    ASSERT_EQ(info.dynamic, map->l_ld);

    ++map_size;
  }

  // _r_debug and dl_iterate_phdr should report the same set of modules. We
  // verified above that every _r_debug module was reported by dl_iterate_phdr,
  // so checking the sizes verifies the converse.
  ASSERT_EQ(f.dl_iter_mods.size(), map_size);

  // Make sure the first entry is the executable. gdbserver assumes this and
  // removes the first entry from its list of shared objects that it sends back
  // to gdb.
  ASSERT_EQ(exe_load_bias, dbg->r_map->l_addr);
  ASSERT_EQ(exe_dynamic, dbg->r_map->l_ld);
#endif
}

#if __arm__
static uintptr_t read_exidx_func(uintptr_t* entry) {
  int32_t offset = *entry;
  // Sign-extend from int31 to int32.
  if ((offset & 0x40000000) != 0) {
    offset += -0x7fffffff - 1;
  }
  return reinterpret_cast<uintptr_t>(entry) + offset;
}
__attribute__((__unused__)) static void another_function_in_same_ELF_file() {}
#endif

TEST(link, dl_unwind_find_exidx) {
#if __arm__
  int count = 0;
  struct eit_entry_t {
    uintptr_t one;
    uintptr_t two;
  };
  eit_entry_t* entries = reinterpret_cast<eit_entry_t*>(dl_unwind_find_exidx(
      reinterpret_cast<_Unwind_Ptr>(read_exidx_func), &count));
  ASSERT_TRUE(entries != nullptr);
  ASSERT_GT(count, 0);

  // Validity checks.
  uintptr_t func = reinterpret_cast<uintptr_t>(read_exidx_func);
  bool found = false;
  for (int i = 0; i < count; ++i) {
    // Entries must have bit 31 clear.
    ASSERT_TRUE((entries[i].one & (1<<31)) == 0);

    uintptr_t exidx_func = read_exidx_func(&entries[i].one);

    // If our function is compiled for thumb, exception table contains our address - 1.
    if (func == exidx_func || func == exidx_func + 1) found = true;

    // Entries must be sorted. Some addresses may appear twice if function
    // is compiled for arm.
    if (i > 0) {
      EXPECT_GE(exidx_func, read_exidx_func(&entries[i - 1].one)) << i;
    }
  }
  ASSERT_TRUE(found);
#else
  GTEST_SKIP() << "dl_unwind_find_exidx is an ARM-only API";
#endif
}