Skip to content

Commit

Permalink
fix: record helper error messages in electron_main_mac (#37810)
Browse files Browse the repository at this point in the history
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
  • Loading branch information
trop[bot] and nornagon committed Apr 3, 2023
1 parent a2d0af4 commit 0c2cb44
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions shell/app/electron_main_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,35 @@
#include "sandbox/mac/seatbelt_exec.h" // nogncheck
#endif

extern "C" {
// abort_report_np() records the message in a special section that both the
// system CrashReporter and Crashpad collect in crash reports. Using a Crashpad
// Annotation would be preferable, but this executable cannot depend on
// Crashpad directly.
void abort_report_np(const char* fmt, ...);
}

namespace {

[[maybe_unused]] bool IsEnvSet(const char* name) {
char* indicator = getenv(name);
return indicator && indicator[0] != '\0';
}

#if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD()
[[noreturn]] void FatalError(const char* format, ...) {
va_list valist;
va_start(valist, format);
char message[4096];
if (vsnprintf(message, sizeof(message), format, valist) >= 0) {
fputs(message, stderr);
abort_report_np("%s", message);
}
va_end(valist);
abort();
}
#endif

} // namespace

int main(int argc, char* argv[]) {
Expand All @@ -42,27 +64,23 @@ int main(int argc, char* argv[]) {
uint32_t exec_path_size = 0;
int rv = _NSGetExecutablePath(NULL, &exec_path_size);
if (rv != -1) {
fprintf(stderr, "_NSGetExecutablePath: get length failed\n");
abort();
FatalError("_NSGetExecutablePath: get length failed.");
}

auto exec_path = std::make_unique<char[]>(exec_path_size);
rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
if (rv != 0) {
fprintf(stderr, "_NSGetExecutablePath: get path failed\n");
abort();
FatalError("_NSGetExecutablePath: get path failed.");
}
sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
argv);
if (seatbelt.sandbox_required) {
if (!seatbelt.server) {
fprintf(stderr, "Failed to create seatbelt sandbox server.\n");
abort();
FatalError("Failed to create seatbelt sandbox server.");
}
if (!seatbelt.server->InitializeSandbox()) {
fprintf(stderr, "Failed to initialize sandbox.\n");
abort();
FatalError("Failed to initialize sandbox.");
}
}
#endif // defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD
Expand Down

0 comments on commit 0c2cb44

Please sign in to comment.