2015-02-04 18:32:42 -05:00
|
|
|
|
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include "handler/mac/crash_report_exception_handler.h"
|
|
|
|
|
|
2015-03-10 17:09:59 -04:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "base/logging.h"
|
2015-03-10 17:09:59 -04:00
|
|
|
|
#include "base/mac/mach_logging.h"
|
2015-10-02 14:40:38 -04:00
|
|
|
|
#include "base/mac/scoped_mach_port.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "base/strings/stringprintf.h"
|
2015-03-11 17:10:50 -04:00
|
|
|
|
#include "client/settings.h"
|
2017-04-18 11:50:39 -04:00
|
|
|
|
#include "handler/mac/file_limit_annotation.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "minidump/minidump_file_writer.h"
|
2017-04-11 14:48:10 -04:00
|
|
|
|
#include "minidump/minidump_user_extension_stream_data_source.h"
|
2015-05-01 13:48:23 -07:00
|
|
|
|
#include "snapshot/crashpad_info_client_options.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "snapshot/mac/process_snapshot_mac.h"
|
|
|
|
|
#include "util/file/file_writer.h"
|
2015-03-10 17:09:59 -04:00
|
|
|
|
#include "util/mach/exc_client_variants.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "util/mach/exception_behaviors.h"
|
2015-04-08 17:46:09 -04:00
|
|
|
|
#include "util/mach/exception_types.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "util/mach/mach_extensions.h"
|
2015-03-12 14:00:38 -04:00
|
|
|
|
#include "util/mach/mach_message.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "util/mach/scoped_task_suspend.h"
|
2015-04-02 15:49:51 -04:00
|
|
|
|
#include "util/mach/symbolic_constants_mach.h"
|
2016-09-19 10:44:02 -07:00
|
|
|
|
#include "util/misc/metrics.h"
|
2015-03-11 17:07:11 -04:00
|
|
|
|
#include "util/misc/tri_state.h"
|
2015-02-04 18:32:42 -05:00
|
|
|
|
#include "util/misc/uuid.h"
|
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
|
|
CrashReportExceptionHandler::CrashReportExceptionHandler(
|
2015-02-12 15:03:59 -05:00
|
|
|
|
CrashReportDatabase* database,
|
2015-03-05 15:40:47 -05:00
|
|
|
|
CrashReportUploadThread* upload_thread,
|
2017-04-11 14:48:10 -04:00
|
|
|
|
const std::map<std::string, std::string>* process_annotations,
|
|
|
|
|
const UserStreamDataSources* user_stream_data_sources)
|
2015-02-12 15:03:59 -05:00
|
|
|
|
: database_(database),
|
2015-03-05 15:40:47 -05:00
|
|
|
|
upload_thread_(upload_thread),
|
2017-04-11 14:48:10 -04:00
|
|
|
|
process_annotations_(process_annotations),
|
|
|
|
|
user_stream_data_sources_(user_stream_data_sources) {}
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
|
|
|
|
CrashReportExceptionHandler::~CrashReportExceptionHandler() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kern_return_t CrashReportExceptionHandler::CatchMachException(
|
|
|
|
|
exception_behavior_t behavior,
|
|
|
|
|
exception_handler_t exception_port,
|
|
|
|
|
thread_t thread,
|
|
|
|
|
task_t task,
|
|
|
|
|
exception_type_t exception,
|
|
|
|
|
const mach_exception_data_type_t* code,
|
|
|
|
|
mach_msg_type_number_t code_count,
|
|
|
|
|
thread_state_flavor_t* flavor,
|
2015-04-02 15:28:28 -04:00
|
|
|
|
ConstThreadState old_state,
|
2015-02-04 18:32:42 -05:00
|
|
|
|
mach_msg_type_number_t old_state_count,
|
|
|
|
|
thread_state_t new_state,
|
|
|
|
|
mach_msg_type_number_t* new_state_count,
|
|
|
|
|
const mach_msg_trailer_t* trailer,
|
|
|
|
|
bool* destroy_complex_request) {
|
2017-04-18 11:50:39 -04:00
|
|
|
|
RecordFileLimitAnnotation();
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionEncountered();
|
mac: Report richer exception codes via metrics
Previously, only the top-level exception code was reported via the
Crashpad.ExceptionCode.Mac histogram. Making this histogram work
(https://crbug.com/678720) has revealed that Chrome is triggering
EXC_RESOURCE exceptions at a rate in excess of 4x that of ordinary
crashes. These exceptions were not previously visible because they are
not uploaded unless the system treats them as fatal, which it does not
normally do absent an explicit request.
In order to learn more about the problem, this change augments the data
reported via the Crashpad.ExceptionCode.Mac histogram to report (at
least) second-level exception data. This means that we will no longer
see just EXC_RESOURCE, but potentially more useful information such as
EXC_RESOURCE / RESOURCE_TYPE_IO / FLAVOR_IO_PHYSICAL_WRITES. This also
applies to other exception types, so that the majority of crashes
currently falling into the EXC_CRASH bucket will now have additional
information decoded and will be reported as, for example, EXC_BAD_ACCESS
/ KERN_INVALID_ADDRESS, EXC_BAD_INSTRUCTION / EXC_I386_INVOP, and
EXC_CRASH / SIGABRT.
Because the old mechanism was only live (in an “it works” sense) for
several days, and the new mechanism does not overlap with histogram
values used by the old one, there’s no need to invent a new histogram
name.
BUG=chromium:684051
Change-Id: Ia0a372b4127f7b3b2e7dbbaac9304cce3b5aadfe
Reviewed-on: https://chromium-review.googlesource.com/430933
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-01-24 10:59:18 -05:00
|
|
|
|
Metrics::ExceptionCode(ExceptionCodeForMetrics(exception, code[0]));
|
2015-02-04 18:32:42 -05:00
|
|
|
|
*destroy_complex_request = true;
|
|
|
|
|
|
|
|
|
|
// The expected behavior is EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
|
|
|
|
|
// but it’s possible to deal with any exception behavior as long as it
|
|
|
|
|
// carries identity information (valid thread and task ports).
|
|
|
|
|
if (!ExceptionBehaviorHasIdentity(behavior)) {
|
|
|
|
|
LOG(ERROR) << base::StringPrintf(
|
2015-04-02 15:49:51 -04:00
|
|
|
|
"unexpected exception behavior %s, rejecting",
|
|
|
|
|
ExceptionBehaviorToString(
|
|
|
|
|
behavior, kUseFullName | kUnknownIsNumeric | kUseOr).c_str());
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kUnexpectedExceptionBehavior);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
} else if (behavior != (EXCEPTION_STATE_IDENTITY | kMachExceptionCodes)) {
|
|
|
|
|
LOG(WARNING) << base::StringPrintf(
|
2015-04-02 15:49:51 -04:00
|
|
|
|
"unexpected exception behavior %s, proceeding",
|
|
|
|
|
ExceptionBehaviorToString(
|
|
|
|
|
behavior, kUseFullName | kUnknownIsNumeric | kUseOr).c_str());
|
2015-02-04 18:32:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (task == mach_task_self()) {
|
|
|
|
|
LOG(ERROR) << "cannot suspend myself";
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kFailedDueToSuspendSelf);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScopedTaskSuspend suspend(task);
|
|
|
|
|
|
|
|
|
|
ProcessSnapshotMac process_snapshot;
|
|
|
|
|
if (!process_snapshot.Initialize(task)) {
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kSnapshotFailed);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 14:00:38 -04:00
|
|
|
|
// Check for suspicious message sources. A suspicious exception message comes
|
|
|
|
|
// from a source other than the kernel or the process that the exception
|
|
|
|
|
// purportedly occurred in.
|
|
|
|
|
//
|
|
|
|
|
// TODO(mark): Consider exceptions outside of the range (0, 32) from the
|
|
|
|
|
// kernel to be suspicious, and exceptions other than kMachExceptionSimulated
|
|
|
|
|
// from the process itself to be suspicious.
|
2015-04-08 17:46:09 -04:00
|
|
|
|
const pid_t pid = process_snapshot.ProcessID();
|
2015-03-12 14:00:38 -04:00
|
|
|
|
pid_t audit_pid = AuditPIDFromMachMessageTrailer(trailer);
|
|
|
|
|
if (audit_pid != -1 && audit_pid != 0) {
|
2015-04-08 17:46:09 -04:00
|
|
|
|
if (audit_pid != pid) {
|
|
|
|
|
LOG(WARNING) << "exception for pid " << pid << " sent by pid "
|
2015-03-12 14:00:38 -04:00
|
|
|
|
<< audit_pid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
CrashpadInfoClientOptions client_options;
|
|
|
|
|
process_snapshot.GetCrashpadOptions(&client_options);
|
|
|
|
|
|
2015-10-05 17:09:45 -04:00
|
|
|
|
if (client_options.crashpad_handler_behavior != TriState::kDisabled &&
|
|
|
|
|
!IsExceptionNonfatalResource(exception, code[0], pid)) {
|
|
|
|
|
// Non-fatal resource exceptions are never user-visible and are not
|
|
|
|
|
// currently of interest to Crashpad.
|
|
|
|
|
|
2015-04-02 15:49:51 -04:00
|
|
|
|
if (!process_snapshot.InitializeException(behavior,
|
|
|
|
|
thread,
|
2015-03-11 17:07:11 -04:00
|
|
|
|
exception,
|
|
|
|
|
code,
|
|
|
|
|
code_count,
|
|
|
|
|
*flavor,
|
|
|
|
|
old_state,
|
|
|
|
|
old_state_count)) {
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kExceptionInitializationFailed);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
2015-03-05 15:40:47 -05:00
|
|
|
|
|
2015-03-11 17:10:50 -04:00
|
|
|
|
UUID client_id;
|
|
|
|
|
Settings* const settings = database_->GetSettings();
|
|
|
|
|
if (settings) {
|
|
|
|
|
// If GetSettings() or GetClientID() fails, something else will log a
|
|
|
|
|
// message and client_id will be left at its default value, all zeroes,
|
|
|
|
|
// which is appropriate.
|
|
|
|
|
settings->GetClientID(&client_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process_snapshot.SetClientID(client_id);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
process_snapshot.SetAnnotationsSimpleMap(*process_annotations_);
|
2015-03-05 15:40:47 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
CrashReportDatabase::NewReport* new_report;
|
|
|
|
|
CrashReportDatabase::OperationStatus database_status =
|
|
|
|
|
database_->PrepareNewCrashReport(&new_report);
|
|
|
|
|
if (database_status != CrashReportDatabase::kNoError) {
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kPrepareNewCrashReportFailed);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2015-03-13 13:00:56 -04:00
|
|
|
|
process_snapshot.SetReportID(new_report->uuid);
|
|
|
|
|
|
2015-04-03 18:57:01 -04:00
|
|
|
|
CrashReportDatabase::CallErrorWritingCrashReport
|
|
|
|
|
call_error_writing_crash_report(database_, new_report);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
WeakFileHandleFileWriter file_writer(new_report->handle);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
MinidumpFileWriter minidump;
|
|
|
|
|
minidump.InitializeFromSnapshot(&process_snapshot);
|
2017-04-11 14:48:10 -04:00
|
|
|
|
AddUserExtensionStreams(
|
|
|
|
|
user_stream_data_sources_, &process_snapshot, &minidump);
|
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
if (!minidump.WriteEverything(&file_writer)) {
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kMinidumpWriteFailed);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
call_error_writing_crash_report.Disarm();
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
UUID uuid;
|
|
|
|
|
database_status = database_->FinishedWritingCrashReport(new_report, &uuid);
|
|
|
|
|
if (database_status != CrashReportDatabase::kNoError) {
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(
|
|
|
|
|
Metrics::CaptureResult::kFinishedWritingCrashReportFailed);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
return KERN_FAILURE;
|
|
|
|
|
}
|
2015-02-04 18:32:42 -05:00
|
|
|
|
|
2017-04-14 14:50:11 -04:00
|
|
|
|
upload_thread_->ReportPending(uuid);
|
2015-03-11 17:07:11 -04:00
|
|
|
|
}
|
2015-02-12 15:03:59 -05:00
|
|
|
|
|
2015-03-11 17:07:11 -04:00
|
|
|
|
if (client_options.system_crash_reporter_forwarding != TriState::kDisabled &&
|
|
|
|
|
(exception == EXC_CRASH ||
|
|
|
|
|
exception == EXC_RESOURCE ||
|
|
|
|
|
exception == EXC_GUARD)) {
|
2015-03-10 17:09:59 -04:00
|
|
|
|
// Don’t forward simulated exceptions such as kMachExceptionSimulated to the
|
|
|
|
|
// system crash reporter. Only forward the types of exceptions that it would
|
|
|
|
|
// receive under normal conditions. Although the system crash reporter is
|
|
|
|
|
// able to deal with other exceptions including simulated ones, forwarding
|
|
|
|
|
// them to the system crash reporter could present the system’s crash UI for
|
|
|
|
|
// processes that haven’t actually crashed, and could result in reports not
|
|
|
|
|
// actually associated with crashes being sent to the operating system
|
|
|
|
|
// vendor.
|
2015-10-02 14:40:38 -04:00
|
|
|
|
base::mac::ScopedMachSendRight
|
|
|
|
|
system_crash_reporter_handler(SystemCrashReporterHandler());
|
2015-10-20 11:03:25 -04:00
|
|
|
|
if (system_crash_reporter_handler.get()) {
|
2015-03-10 17:09:59 -04:00
|
|
|
|
// Make copies of mutable out parameters so that the system crash reporter
|
|
|
|
|
// can’t influence the state returned by this method.
|
|
|
|
|
thread_state_flavor_t flavor_forward = *flavor;
|
|
|
|
|
mach_msg_type_number_t new_state_forward_count = *new_state_count;
|
|
|
|
|
std::vector<natural_t> new_state_forward(
|
|
|
|
|
new_state, new_state + new_state_forward_count);
|
|
|
|
|
|
|
|
|
|
// The system crash reporter requires the behavior to be
|
|
|
|
|
// EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES. It uses the identity
|
|
|
|
|
// parameters but doesn’t appear to use the state parameters, including
|
|
|
|
|
// |flavor|, and doesn’t care if they are 0 or invalid. As long as an
|
|
|
|
|
// identity is available (checked above), any other exception behavior is
|
|
|
|
|
// converted to what the system crash reporter wants, with the caveat that
|
|
|
|
|
// problems may arise if the state wasn’t available and the system crash
|
|
|
|
|
// reporter changes in the future to use it. However, normally, the state
|
|
|
|
|
// will be available.
|
2015-10-02 14:40:38 -04:00
|
|
|
|
kern_return_t kr = UniversalExceptionRaise(
|
2015-03-10 17:09:59 -04:00
|
|
|
|
EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
|
2015-10-20 11:03:25 -04:00
|
|
|
|
system_crash_reporter_handler.get(),
|
2015-03-10 17:09:59 -04:00
|
|
|
|
thread,
|
|
|
|
|
task,
|
|
|
|
|
exception,
|
|
|
|
|
code,
|
|
|
|
|
code_count,
|
|
|
|
|
&flavor_forward,
|
|
|
|
|
old_state,
|
|
|
|
|
old_state_count,
|
|
|
|
|
new_state_forward_count ? &new_state_forward[0] : nullptr,
|
|
|
|
|
&new_state_forward_count);
|
2015-10-02 14:40:38 -04:00
|
|
|
|
MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "UniversalExceptionRaise";
|
2015-03-10 17:09:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 14:29:12 -04:00
|
|
|
|
ExcServerCopyState(
|
|
|
|
|
behavior, old_state, old_state_count, new_state, new_state_count);
|
2015-04-01 12:16:22 -04:00
|
|
|
|
|
2016-09-19 10:44:02 -07:00
|
|
|
|
Metrics::ExceptionCaptureResult(Metrics::CaptureResult::kSuccess);
|
2015-09-04 14:29:12 -04:00
|
|
|
|
return ExcServerSuccessfulReturnValue(exception, behavior, false);
|
2015-02-04 18:32:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace crashpad
|