Skip to content

Commit

Permalink
Fix stack_trace_unittest for cobalt. (#2249)
Browse files Browse the repository at this point in the history
b/316403754
  • Loading branch information
aee-google authored Feb 24, 2024
1 parent 05807a1 commit f6e1743
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 43 deletions.
15 changes: 14 additions & 1 deletion base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,12 @@ component("base") {
]
}

if (use_cobalt_customizations) {
sources += [
"process/launch_starboard.cc",
]
}

if (!use_cobalt_customizations && is_posix && !is_android) {
sources += [ "debug/stack_trace_posix.cc" ]
}
Expand Down Expand Up @@ -1770,6 +1776,13 @@ component("base") {
]
}

if (!use_blink && use_cobalt_customizations) {
sources += [
"process/launch.cc",
"process/launch.h",
]
}

if (!use_cobalt_customizations && (is_linux || is_chromeos)) {
sources += [
"base_paths_posix.cc",
Expand Down Expand Up @@ -3211,7 +3224,7 @@ test("base_unittests") {
"debug/debugger_unittest.cc",
"debug/dump_without_crashing_unittest.cc",

# "debug/stack_trace_unittest.cc",
"debug/stack_trace_unittest.cc",
"debug/task_trace_unittest.cc",
"environment_unittest.cc",
"feature_list_unittest.cc",
Expand Down
2 changes: 1 addition & 1 deletion base/debug/stack_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class BASE_EXPORT ScopedStackFrameLinker {

namespace internal {

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) || defined(STARBOARD)
// POSIX doesn't define any async-signal safe function for converting
// an integer to ASCII. We'll have to define our own version.
// itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
Expand Down
85 changes: 46 additions & 39 deletions base/debug/stack_trace_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,45 @@ class StreamBacktraceOutputHandler : public BacktraceOutputHandler {
StreamBacktraceOutputHandler& operator=(const StreamBacktraceOutputHandler&) = delete;
};

void OutputPointer(void* pointer, BacktraceOutputHandler* handler) {
char buf[1024] = {'\0'};
handler->HandleOutput(" [0x");
internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, 0);
handler->HandleOutput(buf);
handler->HandleOutput("]");
}

void ProcessBacktrace(void* const* trace,
int size,
const char* prefix_string,
BacktraceOutputHandler* handler) {
for (int i = 0; i < size; ++i) {
if (prefix_string)
handler->HandleOutput(prefix_string);
handler->HandleOutput("\t");

char buf[1024] = {'\0'};

// Subtract by one as return address of function may be in the next
// function when a function is annotated as noreturn.
void* address = static_cast<char*>(trace[i]) - 1;
if (SbSystemSymbolize(address, buf, sizeof(buf))) {
handler->HandleOutput(buf);
} else {
handler->HandleOutput("<unknown>");
}

OutputPointer(trace[i], handler);
handler->HandleOutput("\n");
}
}

} // namespace

namespace internal {

// NOTE: code from sandbox/linux/seccomp-bpf/demo.cc.
char* itoa_r(intptr_t i, char* buf, size_t sz, int base) {
char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) {
// Make sure we can write at least one NUL byte.
size_t n = 1;
if (n > sz)
Expand Down Expand Up @@ -96,13 +133,16 @@ char* itoa_r(intptr_t i, char* buf, size_t sz, int base) {
// Make sure there is still enough space left in our output buffer.
if (++n > sz) {
buf[0] = '\000';
return NULL;
return nullptr;
}

// Output the next digit.
*ptr++ = "0123456789abcdef"[j % base];
j /= base;
} while (j);
*ptr++ = "0123456789abcdef"[j % static_cast<uintptr_t>(base)];
j /= static_cast<uintptr_t>(base);

if (padding > 0)
padding--;
} while (j > 0 || padding > 0);

// Terminate the output with a NUL character.
*ptr = '\000';
Expand All @@ -119,40 +159,7 @@ char* itoa_r(intptr_t i, char* buf, size_t sz, int base) {
return buf;
}

void OutputPointer(void* pointer, BacktraceOutputHandler* handler) {
char buf[1024] = {'\0'};
handler->HandleOutput(" [0x");
itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16);
handler->HandleOutput(buf);
handler->HandleOutput("]");
}

void ProcessBacktrace(void* const* trace,
int size,
const char* prefix_string,
BacktraceOutputHandler* handler) {
for (int i = 0; i < size; ++i) {
if (prefix_string)
handler->HandleOutput(prefix_string);
handler->HandleOutput("\t");

char buf[1024] = {'\0'};

// Subtract by one as return address of function may be in the next
// function when a function is annotated as noreturn.
void* address = static_cast<char*>(trace[i]) - 1;
if (SbSystemSymbolize(address, buf, sizeof(buf))) {
handler->HandleOutput(buf);
} else {
handler->HandleOutput("<unknown>");
}

OutputPointer(trace[i], handler);
handler->HandleOutput("\n");
}
}

} // namespace
} // namespace internal

size_t CollectStackTrace(void** trace, size_t count) {
// NOTE: This code MUST be async-signal safe (it's used by in-process
Expand Down
12 changes: 10 additions & 2 deletions base/debug/stack_trace_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ TEST_F(StackTraceTest, TruncatedTrace) {

StackTrace truncated(2);
truncated.Addresses(&count);
#if defined(STARBOARD)
// Starboard removes removes a stack frame that is extra when not truncated.
EXPECT_EQ(1u, count);
#else
EXPECT_EQ(2u, count);
#endif
}
#endif // !defined(OFFICIAL_BUILD) && !defined(NO_UNWIND_TABLES)

Expand Down Expand Up @@ -157,7 +162,8 @@ TEST_F(StackTraceTest, DebugOutputToStreamWithNullPrefix) {
#endif // !defined(__UCLIBC__) && !defined(_AIX)

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
#if !BUILDFLAG(IS_IOS)
// Starboard does not support relative file paths, needed by |SpawnChild()|.
#if !BUILDFLAG(IS_IOS) && !defined(STARBOARD)
static char* newArray() {
// Clang warns about the mismatched new[]/delete if they occur in the same
// function.
Expand Down Expand Up @@ -348,7 +354,7 @@ TEST_F(StackTraceTest, MAYBE_TraceStackFramePointers) {
// sometimes we read fp / pc from the place that previously held
// uninitialized value.
// TODO(crbug.com/1132511): Enable this test on Fuchsia.
#if defined(MEMORY_SANITIZER) || BUILDFLAG(IS_FUCHSIA)
#if defined(MEMORY_SANITIZER) || BUILDFLAG(IS_FUCHSIA) || defined(STARBOARD) && defined(ADDRESS_SANITIZER)
#define MAYBE_TraceStackFramePointersFromBuffer \
DISABLED_TraceStackFramePointersFromBuffer
#else
Expand Down Expand Up @@ -421,6 +427,7 @@ TEST(CheckExitCodeAfterSignalHandlerDeathTest,

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

#if !defined(STARBOARD)
TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGILL) {
auto const raise_sigill = []() {
#if defined(ARCH_CPU_X86_FAMILY)
Expand All @@ -434,6 +441,7 @@ TEST(CheckExitCodeAfterSignalHandlerDeathTest, CheckSIGILL) {

EXPECT_EXIT(raise_sigill(), ::testing::KilledBySignal(SIGILL), "");
}
#endif // !defined(STARBOARD)

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

Expand Down
31 changes: 31 additions & 0 deletions base/process/launch_starboard.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Cobalt Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process/launch.h"

#include "base/notreached.h"

namespace base {

void RaiseProcessToHighPriority() {
// Impossible on iOS. Do nothing.
}

bool GetAppOutput(const CommandLine& cl, std::string* output) {
NOTIMPLEMENTED();
return false;
}

bool GetAppOutputAndError(const CommandLine& cl, std::string* output) {
NOTIMPLEMENTED();
return false;
}

Process LaunchProcess(const CommandLine& cmdline,
const LaunchOptions& options) {
NOTIMPLEMENTED();
return Process();
}

} // namespace base

0 comments on commit f6e1743

Please sign in to comment.