Merge "Stop libc from cross-referencing unwind symbols"
diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
index 5bdc5ed..4fec753 100644
--- a/libc/bionic/pthread_mutex.cpp
+++ b/libc/bionic/pthread_mutex.cpp
@@ -392,6 +392,30 @@
return 0;
}
+static inline __always_inline int __recursive_or_errorcheck_mutex_wait(
+ pthread_mutex_internal_t* mutex,
+ uint16_t shared,
+ uint16_t old_state,
+ const timespec* rel_timeout) {
+// __futex_wait always waits on a 32-bit value. But state is 16-bit. For a normal mutex, the owner_tid
+// field in mutex is not used. On 64-bit devices, the __pad field in mutex is not used.
+// But when a recursive or errorcheck mutex is used on 32-bit devices, we need to add the
+// owner_tid value in the value argument for __futex_wait, otherwise we may always get EAGAIN error.
+
+#if defined(__LP64__)
+ return __futex_wait_ex(&mutex->state, shared, old_state, rel_timeout);
+
+#else
+ // This implementation works only when the layout of pthread_mutex_internal_t matches below expectation.
+ // And it is based on the assumption that Android is always in little-endian devices.
+ static_assert(offsetof(pthread_mutex_internal_t, state) == 0, "");
+ static_assert(offsetof(pthread_mutex_internal_t, owner_tid) == 2, "");
+
+ uint32_t owner_tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed);
+ return __futex_wait_ex(&mutex->state, shared, (owner_tid << 16) | old_state, rel_timeout);
+#endif
+}
+
static int __pthread_mutex_lock_with_timeout(pthread_mutex_internal_t* mutex,
const timespec* abs_timeout_or_null, clockid_t clock) {
uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
@@ -469,7 +493,7 @@
return ETIMEDOUT;
}
}
- if (__futex_wait_ex(&mutex->state, shared, old_state, rel_timeout) == -ETIMEDOUT) {
+ if (__recursive_or_errorcheck_mutex_wait(mutex, shared, old_state, rel_timeout) == -ETIMEDOUT) {
return ETIMEDOUT;
}
old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
diff --git a/libc/tzcode/localtime.c b/libc/tzcode/localtime.c
index 29f605c..bf09c5e 100644
--- a/libc/tzcode/localtime.c
+++ b/libc/tzcode/localtime.c
@@ -2253,11 +2253,14 @@
}
static int __bionic_open_tzdata(const char* olson_id, int* data_size) {
- int fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
- if (fd == -2) {
- // The first thing that 'recovery' does is try to format the current time. It doesn't have
- // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
- fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
+ int fd = __bionic_open_tzdata_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata", olson_id, data_size);
+ if (fd < 0) {
+ fd = __bionic_open_tzdata_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata", olson_id, data_size);
+ if (fd == -2) {
+ // The first thing that 'recovery' does is try to format the current time. It doesn't have
+ // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
+ fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
+ }
}
return fd;
}
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 479e831..5ed8891 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -136,7 +136,7 @@
memset(info, 0, sizeof(Dl_info));
- info->dli_fname = si->name;
+ info->dli_fname = si->get_realpath();
// Address at which the shared object is loaded.
info->dli_fbase = reinterpret_cast<void*>(si->base);
@@ -228,23 +228,28 @@
static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
#endif
-static soinfo __libdl_info("libdl.so", nullptr, 0, RTLD_GLOBAL);
+static uint8_t __libdl_info_buf[sizeof(soinfo)] __attribute__((aligned(8)));
+static soinfo* __libdl_info = nullptr;
// This is used by the dynamic linker. Every process gets these symbols for free.
soinfo* get_libdl_info() {
- if ((__libdl_info.flags_ & FLAG_LINKED) == 0) {
- __libdl_info.flags_ |= FLAG_LINKED;
- __libdl_info.strtab_ = ANDROID_LIBDL_STRTAB;
- __libdl_info.symtab_ = g_libdl_symtab;
- __libdl_info.nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned);
- __libdl_info.nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned);
- __libdl_info.bucket_ = g_libdl_buckets;
- __libdl_info.chain_ = g_libdl_chains;
- __libdl_info.ref_count_ = 1;
- __libdl_info.strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB);
- __libdl_info.local_group_root_ = &__libdl_info;
- __libdl_info.soname_ = "libdl.so";
+ if (__libdl_info == nullptr) {
+ __libdl_info = new (__libdl_info_buf) soinfo("libdl.so", nullptr, 0, RTLD_GLOBAL);
+ __libdl_info->flags_ |= FLAG_LINKED;
+ __libdl_info->strtab_ = ANDROID_LIBDL_STRTAB;
+ __libdl_info->symtab_ = g_libdl_symtab;
+ __libdl_info->nbucket_ = sizeof(g_libdl_buckets)/sizeof(unsigned);
+ __libdl_info->nchain_ = sizeof(g_libdl_chains)/sizeof(unsigned);
+ __libdl_info->bucket_ = g_libdl_buckets;
+ __libdl_info->chain_ = g_libdl_chains;
+ __libdl_info->ref_count_ = 1;
+ __libdl_info->strtab_size_ = sizeof(ANDROID_LIBDL_STRTAB);
+ __libdl_info->local_group_root_ = __libdl_info;
+ __libdl_info->soname_ = "libdl.so";
+#if defined(__arm__)
+ strlcpy(__libdl_info->old_name_, __libdl_info->soname_, sizeof(__libdl_info->old_name_));
+#endif
}
- return &__libdl_info;
+ return __libdl_info;
}
diff --git a/linker/linker.cpp b/linker/linker.cpp
index a9c2bc1..3c8ba76 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -153,7 +153,8 @@
// Copy the necessary fields into the debug structure.
link_map* map = &(info->link_map_head);
map->l_addr = info->load_bias;
- map->l_name = info->name;
+ // link_map l_name field is not const.
+ map->l_name = const_cast<char*>(info->get_realpath());
map->l_ld = info->dynamic;
// Stick the new library at the end of the list.
@@ -238,7 +239,7 @@
static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
off64_t file_offset, uint32_t rtld_flags) {
- if (strlen(name) >= SOINFO_NAME_LEN) {
+ if (strlen(name) >= PATH_MAX) {
DL_ERR("library name \"%s\" too long", name);
return nullptr;
}
@@ -263,7 +264,7 @@
soinfo *prev = nullptr, *trav;
- TRACE("name %s: freeing soinfo @ %p", si->name, si);
+ TRACE("name %s: freeing soinfo @ %p", si->get_soname(), si);
for (trav = solist; trav != nullptr; trav = trav->next) {
if (trav == si) {
@@ -274,7 +275,7 @@
if (trav == nullptr) {
// si was not in solist
- DL_ERR("name \"%s\"@%p is not in solist!", si->name, si);
+ DL_ERR("name \"%s\"@%p is not in solist!", si->get_soname(), si);
return;
}
@@ -324,6 +325,17 @@
parse_path(path, " :", &g_ld_preload_names);
}
+static bool realpath_fd(int fd, std::string* realpath) {
+ std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
+ snprintf(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
+ if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
+ return false;
+ }
+
+ *realpath = std::string(&buf[0]);
+ return true;
+}
+
#if defined(__arm__)
// For a given PC, find the .so that it belongs to.
@@ -377,7 +389,7 @@
return s->st_shndx != SHN_UNDEF;
} else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
- ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->name);
+ ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_soname());
}
return false;
@@ -392,12 +404,12 @@
ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base));
+ symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
// test against bloom filter
if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base));
+ symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
return nullptr;
}
@@ -407,7 +419,7 @@
if (n == 0) {
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base));
+ symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
return nullptr;
}
@@ -418,14 +430,14 @@
strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
is_symbol_global_and_defined(this, s)) {
TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
- symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
+ symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(s->st_value),
static_cast<size_t>(s->st_size));
return s;
}
} while ((gnu_chain_[n++] & 1) == 0);
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base));
+ symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base));
return nullptr;
}
@@ -434,30 +446,36 @@
uint32_t hash = symbol_name.elf_hash();
TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
+ symbol_name.get_name(), get_soname(),
+ reinterpret_cast<void*>(base), hash, hash % nbucket_);
for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
ElfW(Sym)* s = symtab_ + n;
if (strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
is_symbol_global_and_defined(this, s)) {
TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
- symbol_name.get_name(), name, reinterpret_cast<void*>(s->st_value),
- static_cast<size_t>(s->st_size));
+ symbol_name.get_name(), get_soname(),
+ reinterpret_cast<void*>(s->st_value),
+ static_cast<size_t>(s->st_size));
return s;
}
}
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
- symbol_name.get_name(), name, reinterpret_cast<void*>(base), hash, hash % nbucket_);
+ symbol_name.get_name(), get_soname(),
+ reinterpret_cast<void*>(base), hash, hash % nbucket_);
return nullptr;
}
-soinfo::soinfo(const char* name, const struct stat* file_stat,
+soinfo::soinfo(const char* realpath, const struct stat* file_stat,
off64_t file_offset, int rtld_flags) {
memset(this, 0, sizeof(*this));
- strlcpy(this->name, name, sizeof(this->name));
+ if (realpath != nullptr) {
+ realpath_ = realpath;
+ }
+
flags_ = FLAG_NEW_SOINFO;
version_ = SOINFO_VERSION;
@@ -473,7 +491,7 @@
uint32_t SymbolName::elf_hash() {
if (!has_elf_hash_) {
- const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
+ const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
uint32_t h = 0, g;
while (*name) {
@@ -493,7 +511,7 @@
uint32_t SymbolName::gnu_hash() {
if (!has_gnu_hash_) {
uint32_t h = 5381;
- const unsigned char* name = reinterpret_cast<const unsigned char*>(name_);
+ const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
while (*name != 0) {
h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
}
@@ -522,7 +540,7 @@
* relocations for -Bsymbolic linked dynamic executables.
*/
if (si_from->has_DT_SYMBOLIC) {
- DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->name, name);
+ DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_soname(), name);
s = si_from->find_symbol_by_name(symbol_name);
if (s != nullptr) {
*si_found_in = si_from;
@@ -532,7 +550,8 @@
// 1. Look for it in global_group
if (s == nullptr) {
global_group.visit([&](soinfo* global_si) {
- DEBUG("%s: looking up %s in %s (from global group)", si_from->name, name, global_si->name);
+ DEBUG("%s: looking up %s in %s (from global group)",
+ si_from->get_soname(), name, global_si->get_soname());
s = global_si->find_symbol_by_name(symbol_name);
if (s != nullptr) {
*si_found_in = global_si;
@@ -551,7 +570,8 @@
return true;
}
- DEBUG("%s: looking up %s in %s (from local group)", si_from->name, name, local_si->name);
+ DEBUG("%s: looking up %s in %s (from local group)",
+ si_from->get_soname(), name, local_si->get_soname());
s = local_si->find_symbol_by_name(symbol_name);
if (s != nullptr) {
*si_found_in = local_si;
@@ -565,8 +585,8 @@
if (s != nullptr) {
TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
"found in %s, base = %p, load bias = %p",
- si_from->name, name, reinterpret_cast<void*>(s->st_value),
- (*si_found_in)->name, reinterpret_cast<void*>((*si_found_in)->base),
+ si_from->get_soname(), name, reinterpret_cast<void*>(s->st_value),
+ (*si_found_in)->get_soname(), reinterpret_cast<void*>((*si_found_in)->base),
reinterpret_cast<void*>((*si_found_in)->load_bias));
}
@@ -922,7 +942,7 @@
static int open_library_on_default_path(const char* name, off64_t* file_offset) {
for (size_t i = 0; kDefaultLdPaths[i] != nullptr; ++i) {
char buf[512];
- if(!format_path(buf, sizeof(buf), kDefaultLdPaths[i], name)) {
+ if (!format_path(buf, sizeof(buf), kDefaultLdPaths[i], name)) {
continue;
}
@@ -1053,7 +1073,7 @@
si->get_st_ino() == file_stat.st_ino &&
si->get_file_offset() == file_offset) {
TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
- "will return existing soinfo", name, si->name);
+ "will return existing soinfo", name, si->get_realpath());
return si;
}
}
@@ -1064,13 +1084,19 @@
return nullptr;
}
+ std::string realpath = name;
+ if (!realpath_fd(fd, &realpath)) {
+ PRINT("cannot resolve realpath for the library \"%s\": %s", name, strerror(errno));
+ realpath = name;
+ }
+
// Read the ELF header and load the segments.
- ElfReader elf_reader(name, fd, file_offset);
+ ElfReader elf_reader(realpath.c_str(), fd, file_offset);
if (!elf_reader.Load(extinfo)) {
return nullptr;
}
- soinfo* si = soinfo_alloc(name, &file_stat, file_offset, rtld_flags);
+ soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags);
if (si == nullptr) {
return nullptr;
}
@@ -1275,7 +1301,7 @@
}
if (!root->can_unload()) {
- TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->name);
+ TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_soname());
return;
}
@@ -1298,7 +1324,7 @@
if (si->has_min_version(0)) {
soinfo* child = nullptr;
while ((child = si->get_children().pop_front()) != nullptr) {
- TRACE("%s@%p needs to unload %s@%p", si->name, si, child->name, child);
+ TRACE("%s@%p needs to unload %s@%p", si->get_soname(), si, child->get_soname(), child);
if (local_unload_list.contains(child)) {
continue;
} else if (child->is_linked() && child->get_local_group_root() != root) {
@@ -1308,17 +1334,20 @@
}
}
} else {
-#ifdef __LP64__
- __libc_fatal("soinfo for \"%s\"@%p has no version", si->name, si);
+#if !defined(__arm__)
+ __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_soname(), si);
#else
- PRINT("warning: soinfo for \"%s\"@%p has no version", si->name, si);
+ PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_soname(), si);
for_each_dt_needed(si, [&] (const char* library_name) {
- TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
+ TRACE("deprecated (old format of soinfo): %s needs to unload %s",
+ si->get_soname(), library_name);
+
soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
if (needed != nullptr) {
// Not found: for example if symlink was deleted between dlopen and dlclose
// Since we cannot really handle errors at this point - print and continue.
- PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
+ PRINT("warning: couldn't find %s needed by %s on unload.",
+ library_name, si->get_soname());
return;
} else if (local_unload_list.contains(needed)) {
// already visited
@@ -1348,7 +1377,7 @@
soinfo_unload(si);
}
} else {
- TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->name, ref_count);
+ TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->get_soname(), ref_count);
}
}
@@ -1447,7 +1476,7 @@
const char* sym_name = nullptr;
ElfW(Addr) addend = get_addend(rel, reloc);
- DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
+ DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx);
if (type == R_GENERIC_NONE) {
continue;
}
@@ -1462,7 +1491,7 @@
// We only allow an undefined symbol if this is a weak reference...
s = &symtab_[sym];
if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
- DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
+ DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname());
return false;
}
@@ -1652,13 +1681,13 @@
/*
* ET_EXEC is not supported so this should not happen.
*
- * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
+ * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
*
- * Section 4.7.1.10 "Dynamic relocations"
+ * Section 4.6.11 "Dynamic relocations"
* R_AARCH64_COPY may only appear in executable objects where e_type is
* set to ET_EXEC.
*/
- DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
+ DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_soname());
return false;
case R_AARCH64_TLS_TPREL64:
TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
@@ -1711,11 +1740,11 @@
*
* http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
*
- * Section 4.7.1.10 "Dynamic relocations"
+ * Section 4.6.1.10 "Dynamic relocations"
* R_ARM_COPY may only appear in executable objects where e_type is
* set to ET_EXEC.
*/
- DL_ERR("%s R_ARM_COPY relocations are not supported", name);
+ DL_ERR("%s R_ARM_COPY relocations are not supported", get_soname());
return false;
#elif defined(__i386__)
case R_386_32:
@@ -1747,7 +1776,7 @@
return;
}
- TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
+ TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_soname());
int begin = reverse ? (count - 1) : 0;
int end = reverse ? -1 : count;
@@ -1758,7 +1787,7 @@
call_function("function", functions[i]);
}
- TRACE("[ Done calling %s for '%s' ]", array_name, name);
+ TRACE("[ Done calling %s for '%s' ]", array_name, get_soname());
}
void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
@@ -1766,9 +1795,9 @@
return;
}
- TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
+ TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_soname());
function();
- TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
+ TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_soname());
}
void soinfo::call_pre_init_constructors() {
@@ -1797,14 +1826,14 @@
if (!is_main_executable() && preinit_array_ != nullptr) {
// The GNU dynamic linker silently ignores these, but we warn the developer.
PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
- name, preinit_array_count_);
+ get_soname(), preinit_array_count_);
}
get_children().for_each([] (soinfo* si) {
si->call_constructors();
});
- TRACE("\"%s\": calling constructors", name);
+ TRACE("\"%s\": calling constructors", get_soname());
// DT_INIT should be called before DT_INIT_ARRAY if both are present.
call_function("DT_INIT", init_func_);
@@ -1815,7 +1844,7 @@
if (!constructors_called) {
return;
}
- TRACE("\"%s\": calling destructors", name);
+ TRACE("\"%s\": calling destructors", get_soname());
// DT_FINI_ARRAY must be parsed in reverse order.
call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
@@ -1912,12 +1941,28 @@
}
}
-const char* soinfo::get_soname() {
+const char* soinfo::get_realpath() const {
+#if defined(__arm__)
+ if (has_min_version(2)) {
+ return realpath_.c_str();
+ } else {
+ return old_name_;
+ }
+#else
+ return realpath_.c_str();
+#endif
+}
+
+const char* soinfo::get_soname() const {
+#if defined(__arm__)
if (has_min_version(2)) {
return soname_;
} else {
- return name;
+ return old_name_;
}
+#else
+ return soname_;
+#endif
}
// This is a return on get_children()/get_parents() if
@@ -1950,7 +1995,8 @@
const char* soinfo::get_string(ElfW(Word) index) const {
if (has_min_version(1) && (index >= strtab_size_)) {
- __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", name, strtab_size_, index);
+ __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
+ get_soname(), strtab_size_, index);
}
return strtab_ + index;
@@ -2065,13 +2111,13 @@
/* We can't log anything until the linker is relocated */
bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
if (!relocating_linker) {
- INFO("[ linking %s ]", name);
+ INFO("[ linking %s ]", get_soname());
DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
}
if (dynamic == nullptr) {
if (!relocating_linker) {
- DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
+ DL_ERR("missing PT_DYNAMIC in \"%s\"", get_soname());
}
return false;
} else {
@@ -2120,7 +2166,7 @@
if (!powerof2(gnu_maskwords_)) {
DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
- gnu_maskwords_, name);
+ gnu_maskwords_, get_realpath());
return false;
}
--gnu_maskwords_;
@@ -2142,7 +2188,8 @@
case DT_SYMENT:
if (d->d_un.d_val != sizeof(ElfW(Sym))) {
- DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", static_cast<size_t>(d->d_un.d_val), name);
+ DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
+ static_cast<size_t>(d->d_un.d_val), get_realpath());
return false;
}
break;
@@ -2150,12 +2197,12 @@
case DT_PLTREL:
#if defined(USE_RELA)
if (d->d_un.d_val != DT_RELA) {
- DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", name);
+ DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
return false;
}
#else
if (d->d_un.d_val != DT_REL) {
- DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", name);
+ DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
return false;
}
#endif
@@ -2216,11 +2263,11 @@
break;
case DT_ANDROID_REL:
- DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", name);
+ DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
return false;
case DT_ANDROID_RELSZ:
- DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", name);
+ DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
return false;
case DT_RELAENT:
@@ -2235,11 +2282,11 @@
break;
case DT_REL:
- DL_ERR("unsupported DT_REL in \"%s\"", name);
+ DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
return false;
case DT_RELSZ:
- DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
+ DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
return false;
#else
@@ -2267,11 +2314,11 @@
break;
case DT_ANDROID_RELA:
- DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", name);
+ DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
return false;
case DT_ANDROID_RELASZ:
- DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", name);
+ DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
return false;
// "Indicates that all RELATIVE relocations have been concatenated together,
@@ -2283,27 +2330,27 @@
break;
case DT_RELA:
- DL_ERR("unsupported DT_RELA in \"%s\"", name);
+ DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
return false;
case DT_RELASZ:
- DL_ERR("unsupported DT_RELASZ in \"%s\"", name);
+ DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
return false;
#endif
case DT_INIT:
init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
- DEBUG("%s constructors (DT_INIT) found at %p", name, init_func_);
+ DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
break;
case DT_FINI:
fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
- DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func_);
+ DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
break;
case DT_INIT_ARRAY:
init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
- DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array_);
+ DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
break;
case DT_INIT_ARRAYSZ:
@@ -2312,7 +2359,7 @@
case DT_FINI_ARRAY:
fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
- DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array_);
+ DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
break;
case DT_FINI_ARRAYSZ:
@@ -2321,7 +2368,7 @@
case DT_PREINIT_ARRAY:
preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
- DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array_);
+ DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
break;
case DT_PREINIT_ARRAYSZ:
@@ -2330,7 +2377,7 @@
case DT_TEXTREL:
#if defined(__LP64__)
- DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
+ DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
return false;
#else
has_text_relocations = true;
@@ -2348,7 +2395,7 @@
case DT_FLAGS:
if (d->d_un.d_val & DF_TEXTREL) {
#if defined(__LP64__)
- DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
+ DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
return false;
#else
has_text_relocations = true;
@@ -2415,7 +2462,7 @@
default:
if (!relocating_linker) {
- DL_WARN("%s: unused DT entry: type %p arg %p", name,
+ DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
}
break;
@@ -2426,6 +2473,9 @@
for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
if (d->d_tag == DT_SONAME) {
soname_ = get_string(d->d_un.d_val);
+#if defined(__arm__)
+ strlcpy(old_name_, soname_, sizeof(old_name_));
+#endif
break;
}
}
@@ -2439,15 +2489,16 @@
return false;
}
if (nbucket_ == 0 && gnu_nbucket_ == 0) {
- DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" (new hash type from the future?)", name);
+ DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
+ "(new hash type from the future?)", get_soname());
return false;
}
if (strtab_ == 0) {
- DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
+ DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_soname());
return false;
}
if (symtab_ == 0) {
- DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
+ DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_soname());
return false;
}
return true;
@@ -2466,10 +2517,10 @@
// Make segments writable to allow text relocations to work properly. We will later call
// phdr_table_protect_segments() after all of them are applied and all constructors are run.
DL_WARN("%s has text relocations. This is wasting memory and prevents "
- "security hardening. Please fix.", name);
+ "security hardening. Please fix.", get_soname());
if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
DL_ERR("can't unprotect loadable segments for \"%s\": %s",
- name, strerror(errno));
+ get_soname(), strerror(errno));
return false;
}
}
@@ -2482,7 +2533,7 @@
android_relocs_[1] == 'P' &&
(android_relocs_[2] == 'U' || android_relocs_[2] == 'S') &&
android_relocs_[3] == '2') {
- DEBUG("[ android relocating %s ]", name);
+ DEBUG("[ android relocating %s ]", get_soname());
bool relocated = false;
const uint8_t* packed_relocs = android_relocs_ + 4;
@@ -2511,26 +2562,26 @@
#if defined(USE_RELA)
if (rela_ != nullptr) {
- DEBUG("[ relocating %s ]", name);
+ DEBUG("[ relocating %s ]", get_soname());
if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
return false;
}
}
if (plt_rela_ != nullptr) {
- DEBUG("[ relocating %s plt ]", name);
+ DEBUG("[ relocating %s plt ]", get_soname());
if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
return false;
}
}
#else
if (rel_ != nullptr) {
- DEBUG("[ relocating %s ]", name);
+ DEBUG("[ relocating %s ]", get_soname());
if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
return false;
}
}
if (plt_rel_ != nullptr) {
- DEBUG("[ relocating %s plt ]", name);
+ DEBUG("[ relocating %s plt ]", get_soname());
if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
return false;
}
@@ -2543,14 +2594,14 @@
}
#endif
- DEBUG("[ finished linking %s ]", name);
+ DEBUG("[ finished linking %s ]", get_soname());
#if !defined(__LP64__)
if (has_text_relocations) {
// All relocations are done, we can protect our segments back to read-only.
if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
DL_ERR("can't protect segments for \"%s\": %s",
- name, strerror(errno));
+ get_soname(), strerror(errno));
return false;
}
}
@@ -2559,7 +2610,7 @@
/* We can also turn on GNU RELRO protection */
if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
- name, strerror(errno));
+ get_soname(), strerror(errno));
return false;
}
@@ -2568,14 +2619,14 @@
if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
extinfo->relro_fd) < 0) {
DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
- name, strerror(errno));
+ get_soname(), strerror(errno));
return false;
}
} else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
extinfo->relro_fd) < 0) {
DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
- name, strerror(errno));
+ get_soname(), strerror(errno));
return false;
}
}
@@ -2617,7 +2668,12 @@
#else
#define LINKER_PATH "/system/bin/linker"
#endif
-static soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr, 0, 0);
+
+// This is done to avoid calling c-tor prematurely
+// because soinfo c-tor needs memory allocator
+// which might be initialized after global variables.
+static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
+static soinfo* linker_soinfo_for_gdb = nullptr;
/* gdb expects the linker to be in the debug shared object list.
* Without this, gdb has trouble locating the linker's ".text"
@@ -2627,7 +2683,9 @@
* be on the soinfo list.
*/
static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
- linker_soinfo_for_gdb.base = linker_base;
+ linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0);
+
+ linker_soinfo_for_gdb->base = linker_base;
/*
* Set the dynamic field in the link map otherwise gdb will complain with
@@ -2638,8 +2696,8 @@
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
- &linker_soinfo_for_gdb.dynamic, nullptr);
- insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
+ &linker_soinfo_for_gdb->dynamic, nullptr);
+ insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
}
/*
@@ -2837,7 +2895,7 @@
fflush(stdout);
#endif
- TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
+ TRACE("[ Ready to execute '%s' @ %p ]", si->get_soname(), reinterpret_cast<void*>(si->entry));
return si->entry;
}
@@ -2883,7 +2941,7 @@
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
- soinfo linker_so("[dynamic linker]", nullptr, 0, 0);
+ soinfo linker_so(nullptr, nullptr, 0, 0);
// If the linker is not acting as PT_INTERP entry_point is equal to
// _start. Which means that the linker is running as an executable and
diff --git a/linker/linker.h b/linker/linker.h
index ec3d8f0..7482581 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -39,6 +39,8 @@
#include "private/libc_logging.h"
#include "linked_list.h"
+#include <string>
+
#define DL_ERR(fmt, x...) \
do { \
__libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
@@ -94,7 +96,9 @@
#define SOINFO_VERSION 2
+#if defined(__arm__)
#define SOINFO_NAME_LEN 128
+#endif
typedef void (*linker_function_t)();
@@ -141,8 +145,11 @@
struct soinfo {
public:
typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
+#if defined(__arm__)
+ private:
+ char old_name_[SOINFO_NAME_LEN];
+#endif
public:
- char name[SOINFO_NAME_LEN];
const ElfW(Phdr)* phdr;
size_t phnum;
ElfW(Addr) entry;
@@ -263,8 +270,12 @@
bool can_unload() const;
bool is_gnu_hash() const;
- bool inline has_min_version(uint32_t min_version) const {
+ bool inline has_min_version(uint32_t min_version __unused) const {
+#if defined(__arm__)
return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
+#else
+ return true;
+#endif
}
bool is_linked() const;
@@ -279,7 +290,8 @@
soinfo* get_local_group_root() const;
- const char* get_soname();
+ const char* get_soname() const;
+ const char* get_realpath() const;
private:
ElfW(Sym)* elf_lookup(SymbolName& symbol_name);
@@ -327,6 +339,7 @@
size_t android_relocs_size_;
const char* soname_;
+ std::string realpath_;
friend soinfo* get_libdl_info();
};
diff --git a/linker/linker_mips.cpp b/linker/linker_mips.cpp
index 44d39fd..14f6a1b 100644
--- a/linker/linker_mips.cpp
+++ b/linker/linker_mips.cpp
@@ -64,7 +64,7 @@
ElfW(Addr) sym_addr = 0;
const char* sym_name = nullptr;
- DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
+ DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx);
if (type == R_GENERIC_NONE) {
continue;
}
@@ -77,7 +77,7 @@
s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
if (s == nullptr) {
// mips does not support relocation with weak-undefined symbols
- DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
+ DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname());
return false;
} else {
// We got a definition.
diff --git a/tests/Android.mk b/tests/Android.mk
index 995877e..c942375 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -128,6 +128,9 @@
bionic/libc \
external/tinyxml2 \
+libBionicStandardTests_static_libraries := \
+ libbase \
+
libBionicStandardTests_ldlibs_host := \
-lrt \
@@ -257,6 +260,7 @@
bionic-unit-tests_static_libraries := \
libtinyxml2 \
liblog \
+ libbase \
# TODO: Include __cxa_thread_atexit_test.cpp to glibc tests once it is upgraded (glibc 2.18+)
bionic-unit-tests_src_files := \
@@ -317,6 +321,7 @@
libdl \
libtinyxml2 \
liblog \
+ libbase \
bionic-unit-tests-static_force_static_executable := true
@@ -355,6 +360,11 @@
libBionicGtestMain \
$(fortify_libs) \
+bionic-unit-tests-glibc_static_libraries := \
+ libbase \
+ liblog \
+ libcutils \
+
bionic-unit-tests-glibc_ldlibs := \
-lrt -ldl -lutil \
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index 700abff..7012418 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -156,9 +156,9 @@
ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror());
extinfo.library_fd_offset = PAGE_SIZE;
- handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo);
+ handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
ASSERT_TRUE(handle_ == nullptr);
- ASSERT_STREQ("dlopen failed: \"libname_placeholder\" has bad ELF magic", dlerror());
+ ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
close(extinfo.library_fd);
}
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index a63c070..1d62428 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -26,9 +26,12 @@
#include <string>
+#include "utils.h"
+
#define ASSERT_SUBSTR(needle, haystack) \
ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
+
static bool g_called = false;
extern "C" void DlSymTestFunction() {
g_called = true;
@@ -699,7 +702,7 @@
ASSERT_EQ(0, dlclose(self));
}
-TEST(dlfcn, dladdr) {
+TEST(dlfcn, dladdr_executable) {
dlerror(); // Clear any pending errors.
void* self = dlopen(NULL, RTLD_NOW);
ASSERT_TRUE(self != NULL);
@@ -720,13 +723,11 @@
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);
+ char dli_realpath[PATH_MAX];
+ ASSERT_TRUE(realpath(info.dli_fname, dli_realpath) != nullptr);
+ ASSERT_STREQ(executable_path, dli_realpath);
// The symbol name should be the symbol we looked up.
ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
@@ -734,22 +735,16 @@
// 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 line[BUFSIZ];
- FILE* fp = fopen("/proc/self/maps", "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, '/');
- if (path != NULL && strcmp(executable_path, path) == 0) {
- base_address = reinterpret_cast<void*>(start);
+ std::vector<map_record> maps;
+ ASSERT_TRUE(Maps::parse_maps(&maps));
+
+ void* base_address = nullptr;
+ for (const map_record& rec : maps) {
+ if (executable_path == rec.pathname) {
+ base_address = reinterpret_cast<void*>(rec.addr_start);
break;
}
}
- fclose(fp);
// The base address should be the address we were loaded at.
ASSERT_EQ(info.dli_fbase, base_address);
@@ -757,6 +752,28 @@
ASSERT_EQ(0, dlclose(self));
}
+#if defined(__LP64__)
+#define BIONIC_PATH_TO_LIBC "/system/lib64/libc.so"
+#else
+#define BIONIC_PATH_TO_LIBC "/system/lib/libc.so"
+#endif
+
+TEST(dlfcn, dladdr_libc) {
+#if defined(__BIONIC__)
+ Dl_info info;
+ void* addr = reinterpret_cast<void*>(puts); // well-known libc function
+ ASSERT_TRUE(dladdr(addr, &info) != 0);
+
+ ASSERT_STREQ(BIONIC_PATH_TO_LIBC, info.dli_fname);
+ // TODO: add check for dfi_fbase
+ ASSERT_STREQ("puts", info.dli_sname);
+ ASSERT_EQ(addr, info.dli_saddr);
+#else
+ GTEST_LOG_(INFO) << "This test does nothing for glibc. Glibc returns path from ldconfig "
+ "for libc.so, which is symlink itself (not a realpath).\n";
+#endif
+}
+
TEST(dlfcn, dladdr_invalid) {
Dl_info info;
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 5ab1f11..f96ccf9 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -29,13 +29,19 @@
#include <unistd.h>
#include <atomic>
+#include <regex>
#include <vector>
+#include <base/file.h>
+#include <base/stringprintf.h>
+
#include "private/bionic_macros.h"
#include "private/ScopeGuard.h"
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
+extern "C" pid_t gettid();
+
TEST(pthread, pthread_key_create) {
pthread_key_t key;
ASSERT_EQ(0, pthread_key_create(&key, NULL));
@@ -704,6 +710,23 @@
ASSERT_EQ(0, pthread_rwlock_destroy(&l));
}
+static void WaitUntilThreadSleep(std::atomic<pid_t>& pid) {
+ while (pid == 0) {
+ usleep(1000);
+ }
+ std::string filename = android::base::StringPrintf("/proc/%d/stat", pid.load());
+ std::regex regex {R"(\s+S\s+)"};
+
+ while (true) {
+ std::string content;
+ ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
+ if (std::regex_search(content, regex)) {
+ break;
+ }
+ usleep(1000);
+ }
+}
+
struct RwlockWakeupHelperArg {
pthread_rwlock_t lock;
enum Progress {
@@ -713,9 +736,11 @@
LOCK_ACCESSED
};
std::atomic<Progress> progress;
+ std::atomic<pid_t> tid;
};
static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
+ arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
@@ -732,14 +757,14 @@
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
+ wakeup_arg.tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
- while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(wakeup_arg.tid);
+ ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
+
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
@@ -749,6 +774,7 @@
}
static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
+ arg->tid = gettid();
ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
@@ -765,14 +791,14 @@
ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
+ wakeup_arg.tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
- while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(wakeup_arg.tid);
+ ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
+
wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
@@ -1263,7 +1289,6 @@
ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
}
-
class MutexWakeupHelper {
private:
PthreadMutex m;
@@ -1274,8 +1299,10 @@
LOCK_ACCESSED
};
std::atomic<Progress> progress;
+ std::atomic<pid_t> tid;
static void thread_fn(MutexWakeupHelper* helper) {
+ helper->tid = gettid();
ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
helper->progress = LOCK_WAITING;
@@ -1293,15 +1320,15 @@
void test() {
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
progress = LOCK_INITIALIZED;
+ tid = 0;
pthread_t thread;
ASSERT_EQ(0, pthread_create(&thread, NULL,
reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
- while (progress != LOCK_WAITING) {
- usleep(5000);
- }
- usleep(5000);
+ WaitUntilThreadSleep(tid);
+ ASSERT_EQ(LOCK_WAITING, progress);
+
progress = LOCK_RELEASED;
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
diff --git a/tests/stack_protector_test.cpp b/tests/stack_protector_test.cpp
index 8007711..22285d1 100644
--- a/tests/stack_protector_test.cpp
+++ b/tests/stack_protector_test.cpp
@@ -24,14 +24,10 @@
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
-#include <sys/syscall.h>
#include <unistd.h>
#include <set>
-#if defined(__GLIBC__)
-// glibc doesn't expose gettid(2).
-pid_t gettid() { return syscall(__NR_gettid); }
-#endif // __GLIBC__
+extern "C" pid_t gettid();
// For x86, bionic and glibc have per-thread stack guard values (all identical).
#if defined(__i386__)
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 2ecfc60..62677cd 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -151,6 +151,15 @@
fclose(fp);
}
+TEST(stdio, getdelim_directory) {
+ FILE* fp = fopen("/proc", "r");
+ ASSERT_TRUE(fp != NULL);
+ char* word_read;
+ size_t allocated_length;
+ ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
+ fclose(fp);
+}
+
TEST(stdio, getline) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
diff --git a/tests/utils.h b/tests/utils.h
new file mode 100644
index 0000000..fd012a3
--- /dev/null
+++ b/tests/utils.h
@@ -0,0 +1,84 @@
+/*
+ * 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.
+ */
+
+#ifndef __TEST_UTILS_H
+#define __TEST_UTILS_H
+#include <inttypes.h>
+#include <sys/mman.h>
+
+#include "private/ScopeGuard.h"
+
+struct map_record {
+ uintptr_t addr_start;
+ uintptr_t addr_end;
+
+ int perms;
+
+ size_t offset;
+
+ dev_t device;
+ ino_t inode;
+
+ std::string pathname;
+};
+
+class Maps {
+ public:
+ static bool parse_maps(std::vector<map_record>* maps) {
+ char path[64];
+ snprintf(path, sizeof(path), "/proc/self/task/%d/maps", getpid());
+ FILE* fp = fopen(path, "re");
+ if (fp == nullptr) {
+ return false;
+ }
+
+ auto fp_guard = make_scope_guard([&]() {
+ fclose(fp);
+ });
+
+ char line[BUFSIZ];
+ while (fgets(line, sizeof(line), fp) != nullptr) {
+ map_record record;
+ uint32_t dev_major, dev_minor;
+ char pathstr[BUFSIZ];
+ char prot[5]; // sizeof("rwxp")
+ if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %s",
+ &record.addr_start, &record.addr_end, prot, &record.offset,
+ &dev_major, &dev_minor, &record.inode, pathstr) == 8) {
+ record.perms = 0;
+ if (prot[0] == 'r') {
+ record.perms |= PROT_READ;
+ }
+ if (prot[1] == 'w') {
+ record.perms |= PROT_WRITE;
+ }
+ if (prot[2] == 'x') {
+ record.perms |= PROT_EXEC;
+ }
+
+ // TODO: parse shared/private?
+
+ record.device = makedev(dev_major, dev_minor);
+ record.pathname = pathstr;
+ maps->push_back(record);
+ }
+ }
+
+ return true;
+ }
+};
+
+#endif