fix: record helper error messages in electron_main_mac (#37807)

This commit is contained in:
Jeremy Rose 2023-04-03 13:14:19 -07:00 committed by GitHub
parent e31f101712
commit f40bd2da23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,6 +18,14 @@
#include "sandbox/mac/seatbelt_exec.h" // nogncheck #include "sandbox/mac/seatbelt_exec.h" // nogncheck
#endif #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 { namespace {
[[maybe_unused]] bool IsEnvSet(const char* name) { [[maybe_unused]] bool IsEnvSet(const char* name) {
@ -25,6 +33,20 @@ namespace {
return indicator && indicator[0] != '\0'; 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 } // namespace
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -42,27 +64,23 @@ int main(int argc, char* argv[]) {
uint32_t exec_path_size = 0; uint32_t exec_path_size = 0;
int rv = _NSGetExecutablePath(NULL, &exec_path_size); int rv = _NSGetExecutablePath(NULL, &exec_path_size);
if (rv != -1) { if (rv != -1) {
fprintf(stderr, "_NSGetExecutablePath: get length failed\n"); FatalError("_NSGetExecutablePath: get length failed.");
abort();
} }
auto exec_path = std::make_unique<char[]>(exec_path_size); auto exec_path = std::make_unique<char[]>(exec_path_size);
rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size); rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
if (rv != 0) { if (rv != 0) {
fprintf(stderr, "_NSGetExecutablePath: get path failed\n"); FatalError("_NSGetExecutablePath: get path failed.");
abort();
} }
sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt = sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt =
sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc, sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc,
argv); argv);
if (seatbelt.sandbox_required) { if (seatbelt.sandbox_required) {
if (!seatbelt.server) { if (!seatbelt.server) {
fprintf(stderr, "Failed to create seatbelt sandbox server.\n"); FatalError("Failed to create seatbelt sandbox server.");
abort();
} }
if (!seatbelt.server->InitializeSandbox()) { if (!seatbelt.server->InitializeSandbox()) {
fprintf(stderr, "Failed to initialize sandbox.\n"); FatalError("Failed to initialize sandbox.");
abort();
} }
} }
#endif // defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD #endif // defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD