diff options
author | Vladimir Marko <vmarko@google.com> | 2015-01-02 14:23:26 +0000 |
---|---|---|
committer | Vladimir Marko <vmarko@google.com> | 2015-01-02 15:57:36 +0000 |
commit | b8f2f63c3d5f0d6fb2a165b3fb8379608076ddae (patch) | |
tree | 37e38e233a924389408fcb1c5ede80fc36405ac7 /runtime/base | |
parent | ba31c3c400cfc59bfe284701763ef96c8810aa14 (diff) | |
download | art-b8f2f63c3d5f0d6fb2a165b3fb8379608076ddae.zip art-b8f2f63c3d5f0d6fb2a165b3fb8379608076ddae.tar.gz art-b8f2f63c3d5f0d6fb2a165b3fb8379608076ddae.tar.bz2 |
Implement LogLineLowStack() properly.
Avoid stack-based buffers used by the log formatting in
__android_log_print().
Move the VLOG(threads) in Thread::InitStackHwm() after the
low stack check as it's not safe to actually log before
that; even the StringPrintf() is using a stack-based buffer.
Bug: 18830897
Change-Id: I13b2166438e871c52ab91dabfe98f2200fd7c1cf
Diffstat (limited to 'runtime/base')
-rw-r--r-- | runtime/base/logging.cc | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/runtime/base/logging.cc b/runtime/base/logging.cc index b781d60..7c2ef71 100644 --- a/runtime/base/logging.cc +++ b/runtime/base/logging.cc @@ -16,6 +16,7 @@ #include "logging.h" +#include <limits> #include <sstream> #include "base/mutex.h" @@ -239,8 +240,25 @@ void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity log_se void LogMessage::LogLineLowStack(const char* file, unsigned int line, LogSeverity log_severity, const char* message) { #ifdef HAVE_ANDROID_OS - // TODO: be more conservative on stack usage here. - LogLine(file, line, log_severity, message); + // Use android_writeLog() to avoid stack-based buffers used by android_printLog(). + const char* tag = ProgramInvocationShortName(); + int priority = kLogSeverityToAndroidLogPriority[log_severity]; + char* buf = nullptr; + size_t buf_size = 0u; + if (priority == ANDROID_LOG_FATAL) { + // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below. + // If allocation fails, fall back to printing only the message. + buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<typeof(line)>::max_digits10 + + 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */; + buf = reinterpret_cast<char*>(malloc(buf_size)); + } + if (buf != nullptr) { + snprintf(buf, buf_size, "%s:%u] %s", file, line, message); + android_writeLog(priority, tag, buf); + free(buf); + } else { + android_writeLog(priority, tag, message); + } #else static const char* log_characters = "VDIWEFF"; CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U); |