Elliott Hughes | 7c10abb | 2017-04-21 17:15:41 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include <link.h> |
| 32 | |
| 33 | TEST(link, dl_iterate_phdr_early_exit) { |
| 34 | static size_t call_count = 0; |
| 35 | ASSERT_EQ(123, dl_iterate_phdr([](dl_phdr_info*, size_t, void*) { ++call_count; return 123; }, |
| 36 | nullptr)); |
| 37 | ASSERT_EQ(1u, call_count); |
| 38 | } |
| 39 | |
| 40 | TEST(link, dl_iterate_phdr) { |
| 41 | struct Functor { |
| 42 | static int Callback(dl_phdr_info* i, size_t s, void* data) { |
| 43 | reinterpret_cast<Functor*>(data)->DoChecks(i, s); |
| 44 | return 0; |
| 45 | } |
| 46 | void DoChecks(dl_phdr_info* info, size_t s) { |
| 47 | ASSERT_EQ(sizeof(dl_phdr_info), s); |
| 48 | |
| 49 | // TODO: why does the entry for the main executable have a null dlpi_addr and no name anyway? |
| 50 | if (++count == 1 && info->dlpi_addr == 0) return; |
| 51 | |
| 52 | ASSERT_TRUE(info->dlpi_name != nullptr); |
| 53 | |
| 54 | // Find the first PT_LOAD program header so we can find the ELF header. |
| 55 | for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) { |
| 56 | const ElfW(Phdr)* phdr = reinterpret_cast<const ElfW(Phdr)*>(&info->dlpi_phdr[i]); |
| 57 | if (phdr->p_type == PT_LOAD) { |
| 58 | const ElfW(Ehdr)* ehdr = reinterpret_cast<const ElfW(Ehdr)*>(info->dlpi_addr + |
| 59 | phdr->p_vaddr); |
| 60 | // Does it look like an ELF file? |
| 61 | ASSERT_EQ(0, memcmp(ehdr, ELFMAG, SELFMAG)); |
| 62 | // Does the e_phnum match what dl_iterate_phdr told us? |
| 63 | ASSERT_EQ(info->dlpi_phnum, ehdr->e_phnum); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | size_t count; |
| 69 | } f = {}; |
| 70 | ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f)); |
| 71 | } |
| 72 | |
| 73 | #if __arm__ |
| 74 | static uintptr_t read_exidx_func(uintptr_t* entry) { |
| 75 | int32_t offset = *entry; |
| 76 | // Sign-extend from int31 to int32. |
| 77 | if ((offset & 0x40000000) != 0) { |
| 78 | offset += -0x7fffffff - 1; |
| 79 | } |
| 80 | return reinterpret_cast<uintptr_t>(entry) + offset; |
| 81 | } |
| 82 | __attribute__((__unused__)) static void another_function_in_same_ELF_file() {} |
| 83 | #endif |
| 84 | |
| 85 | TEST(link, dl_unwind_find_exidx) { |
| 86 | #if __arm__ |
| 87 | int count = 0; |
| 88 | struct eit_entry_t { |
| 89 | uintptr_t one; |
| 90 | uintptr_t two; |
| 91 | }; |
| 92 | eit_entry_t* entries = reinterpret_cast<eit_entry_t*>(dl_unwind_find_exidx( |
| 93 | reinterpret_cast<_Unwind_Ptr>(read_exidx_func), &count)); |
| 94 | ASSERT_TRUE(entries != nullptr); |
| 95 | ASSERT_GT(count, 0); |
| 96 | |
| 97 | // Sanity checks |
| 98 | uintptr_t func = reinterpret_cast<uintptr_t>(read_exidx_func); |
| 99 | bool found = false; |
| 100 | for (int i = 0; i < count; ++i) { |
| 101 | // Entries must have bit 31 clear. |
| 102 | ASSERT_TRUE((entries[i].one & (1<<31)) == 0); |
| 103 | |
| 104 | uintptr_t exidx_func = read_exidx_func(&entries[i].one); |
| 105 | |
| 106 | // If our function is compiled for thumb, exception table contains our address - 1. |
| 107 | if (func == exidx_func || func == exidx_func + 1) found = true; |
| 108 | |
| 109 | // Entries must be sorted. Some addresses may appear twice if function |
| 110 | // is compiled for arm. |
| 111 | if (i > 0) { |
| 112 | EXPECT_GE(exidx_func, read_exidx_func(&entries[i - 1].one)) << i; |
| 113 | } |
| 114 | } |
| 115 | ASSERT_TRUE(found); |
| 116 | #else |
| 117 | GTEST_LOG_(INFO) << "dl_unwind_find_exidx is an ARM-only API\n"; |
| 118 | #endif |
| 119 | } |