#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/logging.h"
#include <sstream>
#include <string>
#include <string_view>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/no_destructor.h"
#include "base/process/process.h"
#include "base/run_loop.h"
#include "base/sanitizer_buildflags.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_logging_settings.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_POSIX)
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include "base/posix/eintr_wrapper.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
#include <ucontext.h>
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <excpt.h>
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/channel.h>
#include <lib/zx/event.h>
#include <lib/zx/exception.h>
#include <lib/zx/thread.h>
#include <zircon/syscalls/debug.h>
#include <zircon/syscalls/exception.h>
#include <zircon/types.h>
#endif
#include <optional>
namespace logging {
namespace {
Return;
_;
class LoggingTest : public testing::Test { … };
class MockLogSource { … };
class MockLogAssertHandler { … };
TEST_F(LoggingTest, BasicLogging) { … }
TEST_F(LoggingTest, LogIsOn) { … }
TEST_F(LoggingTest, LoggingIsLazyBySeverity) { … }
TEST_F(LoggingTest, LoggingIsLazyByDestination) { … }
TEST_F(LoggingTest, LogToStdErrFlag) { … }
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
namespace {
void TestForLogToStderr(int log_destinations,
bool* did_log_info,
bool* did_log_error) { … }
}
TEST_F(LoggingTest, AlwaysLogErrorsToStderr) { … }
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(LoggingTest, InitWithFileDescriptor) {
const char kErrorLogMessage[] = "something bad happened";
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_log_path = temp_dir.GetPath().Append("file.log");
FILE* log_file = fopen(file_log_path.value().c_str(), "w");
CHECK(log_file);
LoggingSettings settings;
settings.logging_dest = LOG_TO_FILE;
settings.log_file = log_file;
InitLogging(settings);
LOG(ERROR) << kErrorLogMessage;
std::string written_logs;
ASSERT_TRUE(base::ReadFileToString(file_log_path, &written_logs));
ASSERT_NE(written_logs.find(kErrorLogMessage), std::string::npos);
}
TEST_F(LoggingTest, DuplicateLogFile) {
const char kErrorLogMessage1[] = "something really bad happened";
const char kErrorLogMessage2[] = "some other bad thing happened";
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath file_log_path = temp_dir.GetPath().Append("file.log");
LoggingSettings settings;
settings.logging_dest = LOG_TO_FILE;
settings.log_file_path = file_log_path.value().c_str();
InitLogging(settings);
LOG(ERROR) << kErrorLogMessage1;
FILE* log_file_dup = DuplicateLogFILE();
CHECK(log_file_dup);
CloseLogFile();
fprintf(log_file_dup, "%s\n", kErrorLogMessage2);
fflush(log_file_dup);
std::string written_logs;
ASSERT_TRUE(base::ReadFileToString(file_log_path, &written_logs));
ASSERT_NE(written_logs.find(kErrorLogMessage1), std::string::npos);
ASSERT_NE(written_logs.find(kErrorLogMessage2), std::string::npos);
fclose(log_file_dup);
}
#endif
#if !CHECK_WILL_STREAM() && BUILDFLAG(IS_WIN)
NOINLINE void CheckContainingFunc(int death_location) {
CHECK(death_location != 1);
CHECK(death_location != 2);
CHECK(death_location != 3);
}
int GetCheckExceptionData(EXCEPTION_POINTERS* p, DWORD* code, void** addr) {
*code = p->ExceptionRecord->ExceptionCode;
*addr = p->ExceptionRecord->ExceptionAddress;
return EXCEPTION_EXECUTE_HANDLER;
}
TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) {
DWORD code1 = 0;
DWORD code2 = 0;
DWORD code3 = 0;
void* addr1 = nullptr;
void* addr2 = nullptr;
void* addr3 = nullptr;
__try {
CheckContainingFunc(1);
} __except (
GetCheckExceptionData(GetExceptionInformation(), &code1, &addr1)) {
}
__try {
CheckContainingFunc(2);
} __except (
GetCheckExceptionData(GetExceptionInformation(), &code2, &addr2)) {
}
__try {
CheckContainingFunc(3);
} __except (
GetCheckExceptionData(GetExceptionInformation(), &code3, &addr3)) {
}
EXPECT_EQ(STATUS_BREAKPOINT, code1);
EXPECT_EQ(STATUS_BREAKPOINT, code2);
EXPECT_EQ(STATUS_BREAKPOINT, code3);
EXPECT_NE(addr1, addr2);
EXPECT_NE(addr1, addr3);
EXPECT_NE(addr2, addr3);
}
#elif BUILDFLAG(IS_FUCHSIA)
#if !CHECK_WILL_STREAM()
#define DO_CHECK …
#else
#define DO_CHECK …
#endif
struct thread_data_t {
zx::event event;
zx::channel channel;
int death_location;
};
constexpr zx_signals_t kChannelReadySignal = ZX_USER_SIGNAL_0;
constexpr zx_signals_t kCrashThreadErrorSignal = ZX_USER_SIGNAL_1;
void* CrashThread(void* arg) {
thread_data_t* data = (thread_data_t*)arg;
int death_location = data->death_location;
zx_status_t status =
zx::thread::self()->create_exception_channel(0, &data->channel);
if (status != ZX_OK) {
data->event.signal(0, kCrashThreadErrorSignal);
return nullptr;
}
data->event.signal(0, kChannelReadySignal);
DO_CHECK(death_location != 1);
DO_CHECK(death_location != 2);
DO_CHECK(death_location != 3);
data->event.signal(0, kCrashThreadErrorSignal);
return nullptr;
}
_Noreturn __NO_SAFESTACK void exception_pthread_exit() {
pthread_exit(nullptr);
}
void SpawnCrashThread(int death_location, uintptr_t* child_crash_addr) {
zx::event event;
zx_status_t status = zx::event::create(0, &event);
ASSERT_EQ(status, ZX_OK);
thread_data_t thread_data = {std::move(event), zx::channel(), death_location};
pthread_t thread;
int ret = pthread_create(&thread, nullptr, CrashThread, &thread_data);
ASSERT_EQ(ret, 0);
zx_signals_t signals = 0;
status =
thread_data.event.wait_one(kChannelReadySignal | kCrashThreadErrorSignal,
zx::time::infinite(), &signals);
ASSERT_EQ(status, ZX_OK);
ASSERT_EQ(signals, kChannelReadySignal);
status =
thread_data.channel.wait_one(ZX_CHANNEL_READABLE | ZX_CHANNEL_PEER_CLOSED,
zx::time::infinite(), &signals);
ASSERT_EQ(status, ZX_OK);
ASSERT_FALSE(signals & ZX_CHANNEL_PEER_CLOSED);
zx_exception_info_t exception_info;
zx::exception exception;
status = thread_data.channel.read(
0, &exception_info, exception.reset_and_get_address(),
sizeof(exception_info), 1, nullptr, nullptr);
ASSERT_EQ(status, ZX_OK);
zx::thread zircon_thread;
status = exception.get_thread(&zircon_thread);
ASSERT_EQ(status, ZX_OK);
zx_thread_state_general_regs_t buffer;
status = zircon_thread.read_state(ZX_THREAD_STATE_GENERAL_REGS, &buffer,
sizeof(buffer));
ASSERT_EQ(status, ZX_OK);
#if defined(ARCH_CPU_X86_64)
*child_crash_addr = static_cast<uintptr_t>(buffer.rip);
buffer.rip = reinterpret_cast<uintptr_t>(exception_pthread_exit);
#elif defined(ARCH_CPU_ARM64)
*child_crash_addr = static_cast<uintptr_t>(buffer.pc);
buffer.pc = reinterpret_cast<uintptr_t>(exception_pthread_exit);
#else
#error Unsupported architecture
#endif
ASSERT_EQ(zircon_thread.write_state(ZX_THREAD_STATE_GENERAL_REGS, &buffer,
sizeof(buffer)),
ZX_OK);
uint32_t state = ZX_EXCEPTION_STATE_HANDLED;
ASSERT_EQ(
exception.set_property(ZX_PROP_EXCEPTION_STATE, &state, sizeof(state)),
ZX_OK);
exception.reset();
ASSERT_EQ(pthread_join(thread, nullptr), 0);
}
TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) {
uintptr_t child_crash_addr_1 = 0;
uintptr_t child_crash_addr_2 = 0;
uintptr_t child_crash_addr_3 = 0;
SpawnCrashThread(1, &child_crash_addr_1);
SpawnCrashThread(2, &child_crash_addr_2);
SpawnCrashThread(3, &child_crash_addr_3);
ASSERT_NE(0u, child_crash_addr_1);
ASSERT_NE(0u, child_crash_addr_2);
ASSERT_NE(0u, child_crash_addr_3);
ASSERT_NE(child_crash_addr_1, child_crash_addr_2);
ASSERT_NE(child_crash_addr_1, child_crash_addr_3);
ASSERT_NE(child_crash_addr_2, child_crash_addr_3);
}
#elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_IOS) && \
(defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY))
int g_child_crash_pipe;
void CheckCrashTestSighandler(int, siginfo_t* info, void* context_ptr) { … }
#if !CHECK_WILL_STREAM()
#define DO_CHECK …
#else
#define DO_CHECK(cond) …
#endif
void CrashChildMain(int death_location) { … }
void SpawnChildAndCrash(int death_location, uintptr_t* child_crash_addr) { … }
TEST_F(LoggingTest, CheckCausesDistinctBreakpoints) { … }
#endif
TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { … }
TEST_F(LoggingTest, NestedLogAssertHandlers) { … }
namespace nested_test {
class Streamable { … };
[[maybe_unused]] std::ostream& operator<<(std::ostream& out,
const Streamable&) { … }
TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { … }
}
TEST_F(LoggingTest, LogPrefix) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(LoggingTest, LogCrosSyslogFormat) {
scoped_logging_settings().SetLogFormat(LogFormat::LOG_FORMAT_SYSLOG);
const char* kTimestampPattern = R"(\d\d\d\d\-\d\d\-\d\d)"
R"(T\d\d\:\d\d\:\d\d\.\d\d\d\d\d\d)"
R"(Z.+\n)";
static base::NoDestructor<std::string> log_string;
SetLogMessageHandler([](int severity, const char* file, int line,
size_t start, const std::string& str) -> bool {
*log_string = str;
return true;
});
{
SetLogItems(true, true, true, true);
const char* kExpected =
R"(\S+ \d+ ERROR \S+\[\d+:\d+\]\: \[\S+\] message\n)";
LOG(ERROR) << "message";
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kTimestampPattern));
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kExpected));
}
{
SetLogItems(false, false, true, false);
const char* kExpected = R"(\S+ ERROR \S+\: \[\S+\] message\n)";
LOG(ERROR) << "message";
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kTimestampPattern));
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kExpected));
}
{
SetLogItems(true, false, true, false);
const char* kExpected = R"(\S+ ERROR \S+\[\d+\]: \[\S+\] message\n)";
LOG(ERROR) << "message";
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kTimestampPattern));
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kExpected));
}
{
SetLogItems(false, true, true, false);
const char* kExpected = R"(\S+ ERROR \S+\[:\d+\]: \[\S+\] message\n)";
LOG(ERROR) << "message";
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kTimestampPattern));
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kExpected));
}
{
SetLogItems(false, false, false, false);
const char* kExpected = R"(ERROR \S+: \[\S+\] message\n)";
LOG(ERROR) << "message";
EXPECT_THAT(*log_string, ::testing::MatchesRegex(kExpected));
}
}
#endif
TEST_F(LoggingTest, String16) { … }
TEST_F(LoggingTest, ScopedVmoduleSwitches) { … }
TEST_F(LoggingTest, BuildCrashString) { … }
TEST_F(LoggingTest, SystemErrorNotChanged) { … }
TEST_F(LoggingTest, CorrectSystemErrorUsed) { … }
TEST_F(LoggingTest, BuildTimeVLOG) { … }
}
}