|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "runtime/dart/utils/handle_exception.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include <fuchsia/crash/cpp/fidl.h> |
| 10 | +#include <fuchsia/mem/cpp/fidl.h> |
| 11 | +#include <lib/syslog/global.h> |
| 12 | +#include <lib/zx/vmo.h> |
| 13 | +#include <sys/types.h> |
| 14 | +#include <third_party/tonic/converter/dart_converter.h> |
| 15 | +#include <zircon/errors.h> |
| 16 | +#include <zircon/status.h> |
| 17 | + |
| 18 | +#include "runtime/dart/utils/logging.h" |
| 19 | + |
| 20 | +namespace { |
| 21 | +static bool FillBuffer(const std::string& data, fuchsia::mem::Buffer* buffer) { |
| 22 | + uint64_t num_bytes = data.size(); |
| 23 | + zx::vmo vmo; |
| 24 | + |
| 25 | + if (zx::vmo::create(num_bytes, 0u, &vmo) < 0) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + if (num_bytes > 0) { |
| 30 | + if (vmo.write(data.data(), 0, num_bytes) < 0) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + buffer->vmo = std::move(vmo); |
| 36 | + buffer->size = num_bytes; |
| 37 | + |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +template <typename T, size_t N> |
| 42 | +void CopyToArray(const std::string& s, std::array<T, N>* arr) { |
| 43 | + const size_t max_size = arr->size(); |
| 44 | + auto end = s.end(); |
| 45 | + if (s.size() > max_size) { |
| 46 | + FX_LOGF(WARNING, LOG_TAG, "truncating '%s' to %d characters", s.c_str(), |
| 47 | + max_size); |
| 48 | + end = s.begin() + max_size; |
| 49 | + } |
| 50 | + std::copy(s.begin(), end, arr->data()); |
| 51 | +} |
| 52 | + |
| 53 | +fuchsia::crash::ManagedRuntimeException BuildException( |
| 54 | + const std::string& error, |
| 55 | + const std::string& stack_trace) { |
| 56 | + // The runtime type has already been pre-pended to the error message so we |
| 57 | + // expect the format to be '$RuntimeType: $Message'. |
| 58 | + std::string error_type; |
| 59 | + std::string error_message; |
| 60 | + const size_t delimiter_pos = error.find_first_of(':'); |
| 61 | + if (delimiter_pos == std::string::npos) { |
| 62 | + FX_LOGF(ERROR, LOG_TAG, |
| 63 | + "error parsing Dart exception: expected format '$RuntimeType: " |
| 64 | + "$Message', got '%s'", |
| 65 | + error.c_str()); |
| 66 | + // We still need to specify a type, otherwise the stack trace does not |
| 67 | + // show up in the crash server UI. |
| 68 | + error_type = "UnknownError"; |
| 69 | + error_message = error; |
| 70 | + } else { |
| 71 | + error_type = error.substr(0, delimiter_pos); |
| 72 | + error_message = |
| 73 | + error.substr(delimiter_pos + 2 /*to get rid of the leading ': '*/); |
| 74 | + } |
| 75 | + |
| 76 | + // Default-initialize to initialize the underlying arrays of characters with |
| 77 | + // 0s and null-terminate the strings. |
| 78 | + fuchsia::crash::GenericException exception = {}; |
| 79 | + CopyToArray(error_type, &exception.type); |
| 80 | + CopyToArray(error_message, &exception.message); |
| 81 | + if (!FillBuffer(stack_trace, &exception.stack_trace)) { |
| 82 | + FX_LOG(ERROR, LOG_TAG, "Failed to convert Dart stack trace to VMO"); |
| 83 | + } |
| 84 | + |
| 85 | + fuchsia::crash::ManagedRuntimeException dart_exception; |
| 86 | + dart_exception.set_dart(std::move(exception)); |
| 87 | + return dart_exception; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace |
| 91 | + |
| 92 | +namespace dart_utils { |
| 93 | + |
| 94 | +zx_status_t HandleIfException(std::shared_ptr<::sys::ServiceDirectory> services, |
| 95 | + const std::string& component_url, |
| 96 | + Dart_Handle result) { |
| 97 | + if (!Dart_IsError(result) || !Dart_ErrorHasException(result)) { |
| 98 | + return ZX_OK; |
| 99 | + } |
| 100 | + |
| 101 | + const std::string error = |
| 102 | + tonic::StdStringFromDart(Dart_ToString(Dart_ErrorGetException(result))); |
| 103 | + const std::string stack_trace = |
| 104 | + tonic::StdStringFromDart(Dart_ToString(Dart_ErrorGetStackTrace(result))); |
| 105 | + |
| 106 | + return HandleException(services, component_url, error, stack_trace); |
| 107 | +} |
| 108 | + |
| 109 | +zx_status_t HandleException(std::shared_ptr<::sys::ServiceDirectory> services, |
| 110 | + const std::string& component_url, |
| 111 | + const std::string& error, |
| 112 | + const std::string& stack_trace) { |
| 113 | + fuchsia::crash::ManagedRuntimeException exception = |
| 114 | + BuildException(error, stack_trace); |
| 115 | + |
| 116 | + fuchsia::crash::AnalyzerSyncPtr analyzer; |
| 117 | + services->Connect(analyzer.NewRequest()); |
| 118 | +#ifndef NDEBUG |
| 119 | + if (!analyzer) { |
| 120 | + FX_LOG(FATAL, LOG_TAG, "Could not connect to analyzer service"); |
| 121 | + } |
| 122 | +#endif |
| 123 | + |
| 124 | + fuchsia::crash::Analyzer_OnManagedRuntimeException_Result out_result; |
| 125 | + const zx_status_t status = analyzer->OnManagedRuntimeException( |
| 126 | + component_url, std::move(exception), &out_result); |
| 127 | + if (status != ZX_OK) { |
| 128 | + FX_LOGF(ERROR, LOG_TAG, "Failed to connect to crash analyzer: %d (%s)", |
| 129 | + status, zx_status_get_string(status)); |
| 130 | + return ZX_ERR_INTERNAL; |
| 131 | + } else if (out_result.is_err()) { |
| 132 | + FX_LOGF(ERROR, LOG_TAG, "Failed to handle Dart exception: %d (%s)", |
| 133 | + out_result.err(), zx_status_get_string(out_result.err())); |
| 134 | + return ZX_ERR_INTERNAL; |
| 135 | + } |
| 136 | + return ZX_OK; |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace dart_utils |
0 commit comments