Move libc_log code into libasync_safe.
This library is used by a number of different libraries in the system.
Make it easy for platform libraries to use this library and create
an actual exported include file.
Change the names of the functions to reflect the new name of the library.
Run clang_format on the async_safe_log.cpp file since the formatting is
all over the place.
Bug: 31919199
Test: Compiled for angler/bullhead, and booted.
Test: Ran bionic unit tests.
Test: Ran the malloc debug tests.
Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
diff --git a/linker/Android.bp b/linker/Android.bp
index d617189..efd91ac 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -9,6 +9,8 @@
// We need to access Bionic private headers in the linker.
include_dirs: ["bionic/libc"],
+
+ static_libs: ["libasync_safe"],
}
cc_binary {
diff --git a/linker/linker.cpp b/linker/linker.cpp
index afd990a..0b4490d 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -46,6 +46,8 @@
#include <android-base/scopeguard.h>
+#include <async_safe/log.h>
+
// Private C library headers.
#include "linker.h"
@@ -344,7 +346,7 @@
static bool realpath_fd(int fd, std::string* realpath) {
std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
- __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
+ async_safe_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
return false;
@@ -493,7 +495,7 @@
}
if (ref_count_ == 0) { // overflow
- __libc_fatal("Too many nested calls to dlopen()");
+ async_safe_fatal("Too many nested calls to dlopen()");
}
}
@@ -992,7 +994,7 @@
}
static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
- int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
+ int n = async_safe_format_buffer(buf, buf_size, "%s/%s", path, name);
if (n < 0 || n >= static_cast<int>(buf_size)) {
PRINT("Warning: ignoring very long library path: %s/%s", path, name);
return false;
@@ -1781,7 +1783,7 @@
}
} else {
#if !defined(__work_around_b_24465209__)
- __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
+ async_safe_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
#else
PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
for_each_dt_needed(si, [&] (const char* library_name) {
@@ -1855,8 +1857,8 @@
}
if (buffer_size < required_size) {
- __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
- "buffer len %zu, required len %zu", buffer_size, required_size);
+ async_safe_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
+ "buffer len %zu, required len %zu", buffer_size, required_size);
}
char* end = buffer;
diff --git a/linker/linker.h b/linker/linker.h
index 53dac6c..fdd7b66 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -38,7 +38,6 @@
#include <unistd.h>
#include "private/bionic_page.h"
-#include "private/libc_logging.h"
#include "linked_list.h"
#include "linker_common_types.h"
#include "linker_logger.h"
diff --git a/linker/linker_allocator.cpp b/linker/linker_allocator.cpp
index 723ea2b..fd6f496 100644
--- a/linker/linker_allocator.cpp
+++ b/linker/linker_allocator.cpp
@@ -37,6 +37,8 @@
#include <sys/mman.h>
#include <unistd.h>
+#include <async_safe/log.h>
+
#include "private/bionic_prctl.h"
//
@@ -149,7 +151,7 @@
ssize_t offset = reinterpret_cast<uintptr_t>(ptr) - sizeof(page_info);
if (offset % block_size_ != 0) {
- __libc_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
+ async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
}
memset(ptr, 0, block_size_);
@@ -180,7 +182,7 @@
if (it == page_records_.end() || it->page_addr != addr) {
// not found...
- __libc_fatal("page record for %p was not found (block_size=%zd)", ptr, block_size_);
+ async_safe_fatal("page record for %p was not found (block_size=%zd)", ptr, block_size_);
}
return it;
@@ -203,7 +205,7 @@
void* map_ptr = mmap(nullptr, PAGE_SIZE,
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map_ptr == MAP_FAILED) {
- __libc_fatal("mmap failed");
+ async_safe_fatal("mmap failed");
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, "linker_alloc_small_objects");
@@ -248,7 +250,7 @@
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map_ptr == MAP_FAILED) {
- __libc_fatal("mmap failed");
+ async_safe_fatal("mmap failed");
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob");
@@ -283,7 +285,7 @@
page_info* LinkerMemoryAllocator::get_page_info(void* ptr) {
page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr)));
if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
- __libc_fatal("invalid pointer %p (page signature mismatch)", ptr);
+ async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
}
return info;
@@ -308,7 +310,7 @@
} else {
LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
if (allocator != info->allocator_addr) {
- __libc_fatal("invalid pointer %p (page signature mismatch)", ptr);
+ async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
}
old_size = allocator->get_block_size();
@@ -336,7 +338,7 @@
} else {
LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
if (allocator != info->allocator_addr) {
- __libc_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
+ async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
}
allocator->free(ptr);
@@ -345,7 +347,7 @@
LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) {
if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
- __libc_fatal("invalid type: %u", type);
+ async_safe_fatal("invalid type: %u", type);
}
initialize_allocators();
diff --git a/linker/linker_allocator.h b/linker/linker_allocator.h
index beffc52..80ae508 100644
--- a/linker/linker_allocator.h
+++ b/linker/linker_allocator.h
@@ -37,8 +37,9 @@
#include <vector>
+#include <async_safe/log.h>
+
#include "private/bionic_prctl.h"
-#include "private/libc_logging.h"
const uint32_t kSmallObjectMaxSizeLog2 = 10;
const uint32_t kSmallObjectMinSizeLog2 = 4;
@@ -92,7 +93,7 @@
if (ptr == MAP_FAILED) {
// Spec says we need to throw std::bad_alloc here but because our
// code does not support exception handling anyways - we are going to abort.
- __libc_fatal("mmap failed");
+ async_safe_fatal("mmap failed");
}
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "linker_alloc_vector");
diff --git a/linker/linker_config.cpp b/linker/linker_config.cpp
index f614ba0..0a9aeab 100644
--- a/linker/linker_config.cpp
+++ b/linker/linker_config.cpp
@@ -36,6 +36,8 @@
#include <android-base/scopeguard.h>
#include <android-base/strings.h>
+#include <async_safe/log.h>
+
#include <stdlib.h>
#include <string>
@@ -149,7 +151,7 @@
size_t lineno,
const std::string& msg) {
char buf[1024];
- __libc_format_buffer(buf, sizeof(buf), "%s:%zu: error: %s", file, lineno, msg.c_str());
+ async_safe_format_buffer(buf, sizeof(buf), "%s:%zu: error: %s", file, lineno, msg.c_str());
return std::string(buf);
}
@@ -328,7 +330,7 @@
params.push_back({ "LIB", kLibParamValue });
if (target_sdk_version_ != 0) {
char buf[16];
- __libc_format_buffer(buf, sizeof(buf), "%d", target_sdk_version_);
+ async_safe_format_buffer(buf, sizeof(buf), "%d", target_sdk_version_);
params.push_back({ "SDK_VER", buf });
}
diff --git a/linker/linker_debug.h b/linker/linker_debug.h
index 42796e9..7ceab08 100644
--- a/linker/linker_debug.h
+++ b/linker/linker_debug.h
@@ -54,20 +54,21 @@
* To enable/disable specific debug options, change the defines above
*********************************************************************/
-#include "private/libc_logging.h"
#include <unistd.h>
+#include <async_safe/log.h>
+
__LIBC_HIDDEN__ extern int g_ld_debug_verbosity;
#if LINKER_DEBUG_TO_LOG
#define _PRINTVF(v, x...) \
do { \
- if (g_ld_debug_verbosity > (v)) __libc_format_log(5-(v), "linker", x); \
+ if (g_ld_debug_verbosity > (v)) async_safe_format_log(5-(v), "linker", x); \
} while (0)
#else /* !LINKER_DEBUG_TO_LOG */
#define _PRINTVF(v, x...) \
do { \
- if (g_ld_debug_verbosity > (v)) { __libc_format_fd(1, x); write(1, "\n", 1); } \
+ if (g_ld_debug_verbosity > (v)) { async_safe_format_fd(1, x); write(1, "\n", 1); } \
} while (0)
#endif /* !LINKER_DEBUG_TO_LOG */
diff --git a/linker/linker_globals.h b/linker/linker_globals.h
index 1ed479c..d8134af 100644
--- a/linker/linker_globals.h
+++ b/linker/linker_globals.h
@@ -34,20 +34,20 @@
#include <unordered_map>
-#include "private/libc_logging.h"
+#include <async_safe/log.h>
#define DL_ERR(fmt, x...) \
do { \
- __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
+ async_safe_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
/* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
} while (false)
#define DL_WARN(fmt, x...) \
do { \
- __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
- __libc_format_fd(2, "WARNING: linker: "); \
- __libc_format_fd(2, fmt, ##x); \
- __libc_format_fd(2, "\n"); \
+ async_safe_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
+ async_safe_format_fd(2, "WARNING: linker: "); \
+ async_safe_format_fd(2, fmt, ##x); \
+ async_safe_format_fd(2, "\n"); \
} while (false)
#define DL_ERR_AND_LOG(fmt, x...) \
diff --git a/linker/linker_libcxx_support.cpp b/linker/linker_libcxx_support.cpp
index e7b23e0..11e0f40 100644
--- a/linker/linker_libcxx_support.cpp
+++ b/linker/linker_libcxx_support.cpp
@@ -26,8 +26,8 @@
* SUCH DAMAGE.
*/
-#include "private/libc_logging.h"
+#include <async_safe/log.h>
void* __find_icu_symbol(const char* symbol_name __attribute__((__unused__))) {
- __libc_fatal("__find_icu_symbol should not be called in the linker");
+ async_safe_fatal("__find_icu_symbol should not be called in the linker");
}
diff --git a/linker/linker_logger.cpp b/linker/linker_logger.cpp
index 717667c..4c6603b 100644
--- a/linker/linker_logger.cpp
+++ b/linker/linker_logger.cpp
@@ -35,9 +35,10 @@
#include <string>
#include <vector>
+#include <async_safe/log.h>
+
#include "android-base/strings.h"
#include "private/CachedProperty.h"
-#include "private/libc_logging.h"
LinkerLogger g_linker_logger;
bool g_greylist_disabled = false;
@@ -59,8 +60,8 @@
} else if (o == "dlsym") {
flags |= kLogDlsym;
} else {
- __libc_format_log(ANDROID_LOG_WARN, "linker", "Ignoring unknown debug.ld option \"%s\"",
- o.c_str());
+ async_safe_format_log(ANDROID_LOG_WARN, "linker", "Ignoring unknown debug.ld option \"%s\"",
+ o.c_str());
}
}
@@ -95,8 +96,8 @@
bool old_value = g_greylist_disabled;
g_greylist_disabled = (strcmp(greylist_disabled.Get(), "true") == 0);
if (g_greylist_disabled != old_value) {
- __libc_format_log(ANDROID_LOG_INFO, "linker", "%s greylist",
- g_greylist_disabled ? "Disabling" : "Enabling");
+ async_safe_format_log(ANDROID_LOG_INFO, "linker", "%s greylist",
+ g_greylist_disabled ? "Disabling" : "Enabling");
}
flags_ = 0;
@@ -124,6 +125,6 @@
va_list ap;
va_start(ap, format);
- __libc_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap);
+ async_safe_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap);
va_end(ap);
}
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 40f82a1..3d26e91 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -45,6 +45,8 @@
#include "debuggerd/handler.h"
#endif
+#include <async_safe/log.h>
+
#include <vector>
extern void __libc_init_globals(KernelArgumentBlock&);
@@ -189,7 +191,7 @@
char path[PATH_MAX];
ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
- __libc_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
+ async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
}
executable_path = std::string(path, path_len);
}
@@ -267,13 +269,13 @@
// the executable could be unlinked by this point and it should
// not cause a crash (see http://b/31084669)
if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &file_stat)) != 0) {
- __libc_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
+ async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
}
const char* executable_path = get_executable_path();
soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL);
if (si == nullptr) {
- __libc_fatal("Couldn't allocate soinfo: out of memory?");
+ async_safe_fatal("Couldn't allocate soinfo: out of memory?");
}
/* bootstrap the link map, the main exe always needs to be first */
@@ -319,7 +321,7 @@
// tombstone for them. The tombstone never provided any detail relevant to
// fixing the problem anyway, and the utility of drawing extra attention
// to the problem is non-existent at this late date.
- __libc_format_fd(STDERR_FILENO,
+ async_safe_format_fd(STDERR_FILENO,
"\"%s\": error: Android 5.0 and later only support "
"position-independent executables (-fPIE).\n",
g_argv[0]);
@@ -335,7 +337,7 @@
init_default_namespace(executable_path);
if (!si->prelink_image()) {
- __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
+ async_safe_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
}
// add somain to global group
@@ -369,10 +371,10 @@
nullptr,
true /* add_as_children */,
true /* search_linked_namespaces */)) {
- __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
+ async_safe_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
} else if (needed_libraries_count == 0) {
if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) {
- __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
+ async_safe_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
}
si->increment_ref_count();
}
@@ -380,7 +382,7 @@
add_vdso(args);
if (!get_cfi_shadow()->InitialLinkDone(solist)) {
- __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
+ async_safe_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
}
si->call_pre_init_constructors();
@@ -464,7 +466,7 @@
}
static void __linker_cannot_link(const char* argv0) {
- __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", argv0, linker_get_error_buffer());
+ async_safe_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", argv0, linker_get_error_buffer());
}
/*
@@ -546,7 +548,7 @@
// This happens when user tries to run 'adb shell /system/bin/linker'
// see also https://code.google.com/p/android/issues/detail?id=63174
if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
- __libc_format_fd(STDOUT_FILENO,
+ async_safe_format_fd(STDOUT_FILENO,
"This is %s, the helper program for dynamic executables.\n",
args.argv[0]);
exit(0);
diff --git a/linker/linker_memory.cpp b/linker/linker_memory.cpp
index f8852e1..472c4e8 100644
--- a/linker/linker_memory.cpp
+++ b/linker/linker_memory.cpp
@@ -32,7 +32,7 @@
#include <sys/cdefs.h>
#include <unistd.h>
-#include "private/libc_logging.h"
+#include <async_safe/log.h>
static LinkerMemoryAllocator g_linker_allocator;
static pid_t fallback_tid = 0;
@@ -41,7 +41,7 @@
// case the linker heap is corrupted. Do not use this function.
extern "C" void __linker_enable_fallback_allocator() {
if (fallback_tid != 0) {
- __libc_fatal("attempted to use currently-in-use fallback allocator");
+ async_safe_fatal("attempted to use currently-in-use fallback allocator");
}
fallback_tid = gettid();
@@ -49,7 +49,7 @@
extern "C" void __linker_disable_fallback_allocator() {
if (fallback_tid == 0) {
- __libc_fatal("attempted to disable unused fallback allocator");
+ async_safe_fatal("attempted to disable unused fallback allocator");
}
fallback_tid = 0;
diff --git a/linker/linker_sleb128.h b/linker/linker_sleb128.h
index 74b69e4..01e127d 100644
--- a/linker/linker_sleb128.h
+++ b/linker/linker_sleb128.h
@@ -31,6 +31,8 @@
#include <stdint.h>
+#include <async_safe/log.h>
+
#include "linker_debug.h"
// Helper classes for decoding LEB128, used in packed relocation data.
@@ -50,7 +52,7 @@
do {
if (current_ >= end_) {
- __libc_fatal("sleb128_decoder ran out of bounds");
+ async_safe_fatal("sleb128_decoder ran out of bounds");
}
byte = *current_++;
value |= (static_cast<size_t>(byte & 127) << shift);
diff --git a/linker/linker_soinfo.cpp b/linker/linker_soinfo.cpp
index 1d59dbb..fbff7cf 100644
--- a/linker/linker_soinfo.cpp
+++ b/linker/linker_soinfo.cpp
@@ -34,6 +34,8 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <async_safe/log.h>
+
#include "linker_debug.h"
#include "linker_globals.h"
#include "linker_logger.h"
@@ -636,7 +638,7 @@
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",
+ async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
get_realpath(), strtab_size_, index);
}
diff --git a/linker/tests/Android.mk b/linker/tests/Android.mk
index 61c43c9..8284bea 100644
--- a/linker/tests/Android.mk
+++ b/linker/tests/Android.mk
@@ -51,9 +51,6 @@
../linker_config.cpp \
../linker_utils.cpp \
-# for __libc_fatal
-LOCAL_SRC_FILES += ../../libc/bionic/libc_logging.cpp
-
-LOCAL_STATIC_LIBRARIES += libbase
+LOCAL_STATIC_LIBRARIES += libasync_safe libbase
include $(BUILD_NATIVE_TEST)