2012-09-13 02:25:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2012-09-26 20:44:01 +02:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <string>
|
2012-09-13 02:25:30 +02:00
|
|
|
|
2012-10-12 01:08:51 +02:00
|
|
|
#define ASSERT_SUBSTR(needle, haystack) \
|
|
|
|
ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
|
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static bool g_called = false;
|
2012-09-13 02:25:30 +02:00
|
|
|
extern "C" void DlSymTestFunction() {
|
2014-05-14 19:02:03 +02:00
|
|
|
g_called = true;
|
2012-09-13 02:25:30 +02:00
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dlsym_in_self) {
|
2012-10-12 01:08:51 +02:00
|
|
|
dlerror(); // Clear any pending errors.
|
2012-09-13 02:25:30 +02:00
|
|
|
void* self = dlopen(NULL, RTLD_NOW);
|
|
|
|
ASSERT_TRUE(self != NULL);
|
2012-10-12 01:08:51 +02:00
|
|
|
ASSERT_TRUE(dlerror() == NULL);
|
2012-09-13 02:25:30 +02:00
|
|
|
|
|
|
|
void* sym = dlsym(self, "DlSymTestFunction");
|
|
|
|
ASSERT_TRUE(sym != NULL);
|
|
|
|
|
|
|
|
void (*function)() = reinterpret_cast<void(*)()>(sym);
|
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
g_called = false;
|
2012-09-13 02:25:30 +02:00
|
|
|
function();
|
2014-05-14 19:02:03 +02:00
|
|
|
ASSERT_TRUE(g_called);
|
2012-11-01 21:49:32 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, dlclose(self));
|
2012-09-13 02:25:30 +02:00
|
|
|
}
|
2012-09-26 20:44:01 +02:00
|
|
|
|
2014-05-20 00:06:58 +02:00
|
|
|
TEST(dlfcn, dlopen_noload) {
|
|
|
|
void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
|
|
|
|
ASSERT_TRUE(handle == NULL);
|
|
|
|
handle = dlopen("libtest_simple.so", RTLD_NOW);
|
|
|
|
void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
|
|
|
|
ASSERT_TRUE(handle != NULL);
|
|
|
|
ASSERT_TRUE(handle2 != NULL);
|
|
|
|
ASSERT_TRUE(handle == handle2);
|
|
|
|
ASSERT_EQ(0, dlclose(handle));
|
|
|
|
ASSERT_EQ(0, dlclose(handle2));
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dlopen_failure) {
|
2012-10-12 01:08:51 +02:00
|
|
|
void* self = dlopen("/does/not/exist", RTLD_NOW);
|
|
|
|
ASSERT_TRUE(self == NULL);
|
2014-05-13 20:19:57 +02:00
|
|
|
#if defined(__BIONIC__)
|
2012-10-12 01:08:51 +02:00
|
|
|
ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
|
|
|
|
#else
|
|
|
|
ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-25 03:37:21 +02:00
|
|
|
static void* ConcurrentDlErrorFn(void*) {
|
2012-10-17 00:54:46 +02:00
|
|
|
dlopen("/child/thread", RTLD_NOW);
|
|
|
|
return reinterpret_cast<void*>(strdup(dlerror()));
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dlerror_concurrent) {
|
2012-10-17 00:54:46 +02:00
|
|
|
dlopen("/main/thread", RTLD_NOW);
|
|
|
|
const char* main_thread_error = dlerror();
|
|
|
|
ASSERT_SUBSTR("/main/thread", main_thread_error);
|
|
|
|
|
|
|
|
pthread_t t;
|
|
|
|
ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
|
|
|
|
void* result;
|
|
|
|
ASSERT_EQ(0, pthread_join(t, &result));
|
|
|
|
char* child_thread_error = static_cast<char*>(result);
|
|
|
|
ASSERT_SUBSTR("/child/thread", child_thread_error);
|
|
|
|
free(child_thread_error);
|
|
|
|
|
|
|
|
ASSERT_SUBSTR("/main/thread", main_thread_error);
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dlsym_failures) {
|
2012-10-12 01:08:51 +02:00
|
|
|
dlerror(); // Clear any pending errors.
|
|
|
|
void* self = dlopen(NULL, RTLD_NOW);
|
|
|
|
ASSERT_TRUE(self != NULL);
|
|
|
|
ASSERT_TRUE(dlerror() == NULL);
|
|
|
|
|
|
|
|
void* sym;
|
|
|
|
|
2014-05-22 18:49:24 +02:00
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
|
|
|
// RTLD_DEFAULT in lp32 bionic is not (void*)0
|
|
|
|
// so it can be distinguished from the NULL handle.
|
2012-10-12 01:08:51 +02:00
|
|
|
sym = dlsym(NULL, "test");
|
|
|
|
ASSERT_TRUE(sym == NULL);
|
|
|
|
ASSERT_SUBSTR("dlsym library handle is null", dlerror());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// NULL symbol name.
|
2014-05-13 20:19:57 +02:00
|
|
|
#if defined(__BIONIC__)
|
2012-10-12 01:08:51 +02:00
|
|
|
// glibc marks this parameter non-null and SEGVs if you cheat.
|
|
|
|
sym = dlsym(self, NULL);
|
|
|
|
ASSERT_TRUE(sym == NULL);
|
|
|
|
ASSERT_SUBSTR("", dlerror());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Symbol that doesn't exist.
|
|
|
|
sym = dlsym(self, "ThisSymbolDoesNotExist");
|
|
|
|
ASSERT_TRUE(sym == NULL);
|
|
|
|
ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
|
2012-11-01 21:49:32 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, dlclose(self));
|
2012-10-12 01:08:51 +02:00
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dladdr) {
|
2012-10-12 01:08:51 +02:00
|
|
|
dlerror(); // Clear any pending errors.
|
2012-09-26 20:44:01 +02:00
|
|
|
void* self = dlopen(NULL, RTLD_NOW);
|
|
|
|
ASSERT_TRUE(self != NULL);
|
2012-10-12 01:08:51 +02:00
|
|
|
ASSERT_TRUE(dlerror() == NULL);
|
2012-09-26 20:44:01 +02:00
|
|
|
|
|
|
|
void* sym = dlsym(self, "DlSymTestFunction");
|
|
|
|
ASSERT_TRUE(sym != NULL);
|
|
|
|
|
|
|
|
// Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
|
|
|
|
void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
|
|
|
|
|
|
|
|
Dl_info info;
|
|
|
|
int rc = dladdr(addr, &info);
|
|
|
|
ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
|
|
|
|
|
|
|
|
// Get the name of this executable.
|
|
|
|
char executable_path[PATH_MAX];
|
|
|
|
rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
|
|
|
|
ASSERT_NE(rc, -1);
|
|
|
|
executable_path[rc] = '\0';
|
|
|
|
std::string executable_name(basename(executable_path));
|
|
|
|
|
|
|
|
// The filename should be that of this executable.
|
|
|
|
// Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
|
|
|
|
std::string dli_fname(info.dli_fname);
|
|
|
|
dli_fname = basename(&dli_fname[0]);
|
|
|
|
ASSERT_EQ(dli_fname, executable_name);
|
|
|
|
|
|
|
|
// The symbol name should be the symbol we looked up.
|
|
|
|
ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
|
|
|
|
|
|
|
|
// The address should be the exact address of the symbol.
|
|
|
|
ASSERT_EQ(info.dli_saddr, sym);
|
|
|
|
|
|
|
|
// Look in /proc/pid/maps to find out what address we were loaded at.
|
|
|
|
// TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
|
|
|
|
void* base_address = NULL;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "/proc/%d/maps", getpid());
|
|
|
|
char line[BUFSIZ];
|
|
|
|
FILE* fp = fopen(path, "r");
|
|
|
|
ASSERT_TRUE(fp != NULL);
|
|
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
|
|
|
uintptr_t start = strtoul(line, 0, 16);
|
|
|
|
line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
|
|
|
|
char* path = strchr(line, '/');
|
2012-10-10 02:14:56 +02:00
|
|
|
if (path != NULL && strcmp(executable_path, path) == 0) {
|
2012-09-26 20:44:01 +02:00
|
|
|
base_address = reinterpret_cast<void*>(start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
// The base address should be the address we were loaded at.
|
|
|
|
ASSERT_EQ(info.dli_fbase, base_address);
|
2012-11-01 21:49:32 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(0, dlclose(self));
|
2012-09-26 20:44:01 +02:00
|
|
|
}
|
|
|
|
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dladdr_invalid) {
|
2012-09-26 20:44:01 +02:00
|
|
|
Dl_info info;
|
|
|
|
|
2012-10-12 01:08:51 +02:00
|
|
|
dlerror(); // Clear any pending errors.
|
|
|
|
|
2012-09-26 20:44:01 +02:00
|
|
|
// No symbol corresponding to NULL.
|
|
|
|
ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
|
2012-10-12 01:08:51 +02:00
|
|
|
ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
|
2012-09-26 20:44:01 +02:00
|
|
|
|
|
|
|
// No symbol corresponding to a stack address.
|
|
|
|
ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
|
2012-10-12 01:08:51 +02:00
|
|
|
ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
|
2012-09-26 20:44:01 +02:00
|
|
|
}
|
2012-10-31 22:20:03 +01:00
|
|
|
|
|
|
|
// Our dynamic linker doesn't support GNU hash tables.
|
2013-01-07 23:18:22 +01:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
// GNU-style ELF hash tables are incompatible with the MIPS ABI.
|
|
|
|
// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
|
|
|
|
#if !defined(__mips__)
|
2012-12-19 00:57:55 +01:00
|
|
|
TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
|
2012-10-31 22:20:03 +01:00
|
|
|
dlerror(); // Clear any pending errors.
|
2012-11-08 03:16:02 +01:00
|
|
|
void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
|
2012-10-31 22:20:03 +01:00
|
|
|
ASSERT_TRUE(handle == NULL);
|
2012-11-08 03:16:02 +01:00
|
|
|
ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror());
|
2012-10-31 22:20:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-01-07 23:18:22 +01:00
|
|
|
#endif
|
2012-12-19 00:57:55 +01:00
|
|
|
|
|
|
|
TEST(dlfcn, dlopen_bad_flags) {
|
|
|
|
dlerror(); // Clear any pending errors.
|
|
|
|
void* handle;
|
|
|
|
|
2014-05-13 20:19:57 +02:00
|
|
|
#if defined(__GLIBC__)
|
2012-12-19 00:57:55 +01:00
|
|
|
// glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
|
|
|
|
handle = dlopen(NULL, 0);
|
|
|
|
ASSERT_TRUE(handle == NULL);
|
|
|
|
ASSERT_SUBSTR("invalid", dlerror());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
handle = dlopen(NULL, 0xffffffff);
|
|
|
|
ASSERT_TRUE(handle == NULL);
|
|
|
|
ASSERT_SUBSTR("invalid", dlerror());
|
|
|
|
|
|
|
|
// glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
|
|
|
|
handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
|
|
|
|
ASSERT_TRUE(handle != NULL);
|
|
|
|
ASSERT_SUBSTR(NULL, dlerror());
|
|
|
|
}
|
2013-10-31 15:02:12 +01:00
|
|
|
|
|
|
|
TEST(dlfcn, rtld_default_unknown_symbol) {
|
2013-11-12 00:48:06 +01:00
|
|
|
void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
|
2013-10-31 15:02:12 +01:00
|
|
|
ASSERT_TRUE(addr == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dlfcn, rtld_default_known_symbol) {
|
2013-11-12 00:48:06 +01:00
|
|
|
void* addr = dlsym(RTLD_DEFAULT, "fopen");
|
|
|
|
ASSERT_TRUE(addr != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dlfcn, rtld_next_unknown_symbol) {
|
|
|
|
void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
|
|
|
|
ASSERT_TRUE(addr == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dlfcn, rtld_next_known_symbol) {
|
|
|
|
void* addr = dlsym(RTLD_NEXT, "fopen");
|
2013-10-31 15:02:12 +01:00
|
|
|
ASSERT_TRUE(addr != NULL);
|
|
|
|
}
|
2014-05-08 21:27:25 +02:00
|
|
|
|
|
|
|
TEST(dlfcn, dlopen_symlink) {
|
|
|
|
void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
|
|
|
|
void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
|
|
|
|
ASSERT_TRUE(handle1 != NULL);
|
|
|
|
ASSERT_TRUE(handle2 != NULL);
|
|
|
|
ASSERT_EQ(handle1, handle2);
|
|
|
|
}
|