jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 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> |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 20 | #include <libgen.h> |
| 21 | #include <limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <string> |
jeffhao | acf5aa7 | 2012-09-12 17:25:30 -0700 | [diff] [blame] | 26 | |
| 27 | static bool gCalled = false; |
| 28 | extern "C" void DlSymTestFunction() { |
| 29 | gCalled = true; |
| 30 | } |
| 31 | |
| 32 | TEST(dlopen, dlsym_in_self) { |
| 33 | void* self = dlopen(NULL, RTLD_NOW); |
| 34 | ASSERT_TRUE(self != NULL); |
| 35 | |
| 36 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 37 | ASSERT_TRUE(sym != NULL); |
| 38 | |
| 39 | void (*function)() = reinterpret_cast<void(*)()>(sym); |
| 40 | |
| 41 | gCalled = false; |
| 42 | function(); |
| 43 | ASSERT_TRUE(gCalled); |
| 44 | } |
Elliott Hughes | 8e15b08 | 2012-09-26 11:44:01 -0700 | [diff] [blame] | 45 | |
| 46 | TEST(dlopen, dladdr) { |
| 47 | void* self = dlopen(NULL, RTLD_NOW); |
| 48 | ASSERT_TRUE(self != NULL); |
| 49 | |
| 50 | void* sym = dlsym(self, "DlSymTestFunction"); |
| 51 | ASSERT_TRUE(sym != NULL); |
| 52 | |
| 53 | // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address. |
| 54 | void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2); |
| 55 | |
| 56 | Dl_info info; |
| 57 | int rc = dladdr(addr, &info); |
| 58 | ASSERT_NE(rc, 0); // Zero on error, non-zero on success. |
| 59 | |
| 60 | // Get the name of this executable. |
| 61 | char executable_path[PATH_MAX]; |
| 62 | rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); |
| 63 | ASSERT_NE(rc, -1); |
| 64 | executable_path[rc] = '\0'; |
| 65 | std::string executable_name(basename(executable_path)); |
| 66 | |
| 67 | // The filename should be that of this executable. |
| 68 | // Note that we don't know whether or not we have the full path, so we want an "ends_with" test. |
| 69 | std::string dli_fname(info.dli_fname); |
| 70 | dli_fname = basename(&dli_fname[0]); |
| 71 | ASSERT_EQ(dli_fname, executable_name); |
| 72 | |
| 73 | // The symbol name should be the symbol we looked up. |
| 74 | ASSERT_STREQ(info.dli_sname, "DlSymTestFunction"); |
| 75 | |
| 76 | // The address should be the exact address of the symbol. |
| 77 | ASSERT_EQ(info.dli_saddr, sym); |
| 78 | |
| 79 | // Look in /proc/pid/maps to find out what address we were loaded at. |
| 80 | // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic. |
| 81 | void* base_address = NULL; |
| 82 | char path[PATH_MAX]; |
| 83 | snprintf(path, sizeof(path), "/proc/%d/maps", getpid()); |
| 84 | char line[BUFSIZ]; |
| 85 | FILE* fp = fopen(path, "r"); |
| 86 | ASSERT_TRUE(fp != NULL); |
| 87 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 88 | uintptr_t start = strtoul(line, 0, 16); |
| 89 | line[strlen(line) - 1] = '\0'; // Chomp the '\n'. |
| 90 | char* path = strchr(line, '/'); |
| 91 | if (strcmp(executable_path, path) == 0) { |
| 92 | base_address = reinterpret_cast<void*>(start); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | fclose(fp); |
| 97 | |
| 98 | // The base address should be the address we were loaded at. |
| 99 | ASSERT_EQ(info.dli_fbase, base_address); |
| 100 | } |
| 101 | |
| 102 | TEST(dlopen, dladdr_invalid) { |
| 103 | Dl_info info; |
| 104 | |
| 105 | // No symbol corresponding to NULL. |
| 106 | ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success. |
| 107 | |
| 108 | // No symbol corresponding to a stack address. |
| 109 | ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success. |
| 110 | } |