chromium/base/debug/stack_trace_unittest.cc

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/debug/stack_trace.h"

#include <stddef.h>

#include <limits>
#include <sstream>
#include <string>

#include "base/allocator/buildflags.h"
#include "base/containers/span.h"
#include "base/debug/debugging_buildflags.h"
#include "base/immediate_crash.h"
#include "base/logging.h"
#include "base/process/kill.h"
#include "base/process/process_handle.h"
#include "base/profiler/stack_buffer.h"
#include "base/profiler/stack_copier.h"
#include "base/strings/cstring_view.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "partition_alloc/partition_alloc.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
#include "partition_alloc/shim/allocator_shim.h"
#endif

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
#include "base/test/multiprocess_test.h"
#endif

namespace base {
namespace debug {

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
StackTraceTest;
#else
typedef testing::Test StackTraceTest;
#endif
StackTraceDeathTest;

#if !defined(__UCLIBC__) && !defined(_AIX)
// StackTrace::OutputToStream() is not implemented under uclibc, nor AIX.
// See https://crbug.com/706728

TEST_F(StackTraceTest, OutputToStream) {}

#if !defined(OFFICIAL_BUILD) && !defined(NO_UNWIND_TABLES)
// Disabled in Official builds, where Link-Time Optimization can result in two
// or fewer stack frames being available, causing the test to fail.
TEST_F(StackTraceTest, TruncatedTrace) {}
#endif  // !defined(OFFICIAL_BUILD) && !defined(NO_UNWIND_TABLES)

// The test is used for manual testing, e.g., to see the raw output.
TEST_F(StackTraceTest, DebugOutputToStream) {}

// The test is used for manual testing, e.g., to see the raw output.
TEST_F(StackTraceTest, DebugPrintBacktrace) {}

// The test is used for manual testing, e.g., to see the raw output.
TEST_F(StackTraceTest, DebugPrintWithPrefixBacktrace) {}

// Make sure nullptr prefix doesn't crash. Output not examined, much
// like the DebugPrintBacktrace test above.
TEST_F(StackTraceTest, DebugPrintWithNullPrefixBacktrace) {}

// Test OutputToStreamWithPrefix, mainly to make sure it doesn't
// crash. Any "real" stack trace testing happens above.
TEST_F(StackTraceTest, DebugOutputToStreamWithPrefix) {}

// Make sure nullptr prefix doesn't crash. Output not examined, much
// like the DebugPrintBacktrace test above.
TEST_F(StackTraceTest, DebugOutputToStreamWithNullPrefix) {}

#endif  // !defined(__UCLIBC__) && !defined(_AIX)

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
// Since Mac's base::debug::StackTrace().Print() is not malloc-free, skip
// StackDumpSignalHandlerIsMallocFree if BUILDFLAG(IS_MAC).
#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) && !BUILDFLAG(IS_MAC)

namespace {

// ImmediateCrash if a signal handler incorrectly uses malloc().
// In an actual implementation, this could cause infinite recursion into the
// signal handler or other problems. Because malloc() is not guaranteed to be
// async signal safe.
void* BadMalloc(size_t, void*) {}

void* BadCalloc(size_t, size_t, void* context) {}

void* BadAlignedAlloc(size_t, size_t, void*) {}

void* BadAlignedRealloc(void*, size_t, size_t, void*) {}

void* BadRealloc(void*, size_t, void*) {}

void BadFree(void*, void*) {}

allocator_shim::AllocatorDispatch g_bad_malloc_dispatch =;

}  // namespace

// Regression test for StackDumpSignalHandler async-signal unsafety.
// Since malloc() is not guaranteed to be async signal safe, it is not allowed
// to use malloc() inside StackDumpSignalHandler().
TEST_F(StackTraceDeathTest, StackDumpSignalHandlerIsMallocFree) {}
#endif  // PA_BUILDFLAG(USE_ALLOCATOR_SHIM)

namespace {

std::string itoa_r_wrapper(intptr_t i, size_t sz, int base, size_t padding) {}

}  // namespace

TEST_F(StackTraceTest, itoa_r) {}
#endif  // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)

class CopyFunction : public StackCopier {};

template <size_t Depth>
NOINLINE NOOPT void ExpectStackFramePointers(span<const void*> frames) {}

template <>
NOINLINE NOOPT void ExpectStackFramePointers<1>(span<const void*> frames) {}

#if defined(MEMORY_SANITIZER)
// The test triggers use-of-uninitialized-value errors on MSan bots.
// This is expected because we're walking and reading the stack, and
// sometimes we read fp / pc from the place that previously held
// uninitialized value.
#define MAYBE_TraceStackFramePointers
#else
#define MAYBE_TraceStackFramePointers
#endif
TEST_F(StackTraceTest, MAYBE_TraceStackFramePointers) {}

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_APPLE)
#define MAYBE_StackEnd
#else
#define MAYBE_StackEnd
#endif

TEST_F(StackTraceTest, MAYBE_StackEnd) {}

#endif  // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)

#if !defined(ADDRESS_SANITIZER) && !defined(UNDEFINED_SANITIZER)

#if !defined(ARCH_CPU_ARM_FAMILY)
// On Arm architecture invalid math operations such as division by zero are not
// trapped and do not trigger a SIGFPE.
// Hence disable the test for Arm platforms.
TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGFPE) {}
#endif  // !defined(ARCH_CPU_ARM_FAMILY)

TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGSEGV) {}

#if defined(ARCH_CPU_X86_64)
TEST(CheckExitCodeAfterSignalHandlerDeathTest,
     CheckSIGSEGVNonCanonicalAddress) {}
#endif

#endif  // #if !defined(ADDRESS_SANITIZER) && !defined(UNDEFINED_SANITIZER)

TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGILL) {}

#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)

}  // namespace debug
}  // namespace base