Skip to content

Commit d717044

Browse files
authored
Copy //runtime/dart/utils from Topaz into the engine. (#8871)
Build rules and include paths have been updated as necessary. No other functional change.
1 parent 149b7b8 commit d717044

15 files changed

Lines changed: 714 additions & 0 deletions

File tree

‎ci/licenses_golden/licenses_flutter‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,18 @@ FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.cc
715715
FILE: ../../../flutter/shell/platform/embedder/platform_view_embedder.h
716716
FILE: ../../../flutter/shell/platform/embedder/vsync_waiter_embedder.cc
717717
FILE: ../../../flutter/shell/platform/embedder/vsync_waiter_embedder.h
718+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/files.cc
719+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/files.h
720+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/handle_exception.cc
721+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/handle_exception.h
722+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/inlines.h
723+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/logging.h
724+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.cc
725+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/tempfs.h
726+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.cc
727+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmo.h
728+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_object.cc
729+
FILE: ../../../flutter/shell/platform/fuchsia/runtime/dart/utils/vmservice_object.h
718730
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller.cc
719731
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/flutter_window_controller_unittests.cc
720732
FILE: ../../../flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_window.h
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2018 The Fuchsia 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+
assert(is_fuchsia)
6+
7+
import("//build/fuchsia/sdk.gni")
8+
9+
config("utils_config") {
10+
include_dirs = [ "../../.." ]
11+
}
12+
13+
source_set("utils") {
14+
sources = [
15+
"files.cc",
16+
"files.h",
17+
"handle_exception.cc",
18+
"handle_exception.h",
19+
"inlines.h",
20+
"logging.h",
21+
"tempfs.cc",
22+
"tempfs.h",
23+
"vmo.cc",
24+
"vmo.h",
25+
"vmservice_object.cc",
26+
"vmservice_object.h",
27+
]
28+
29+
deps = [
30+
"$fuchsia_sdk_root/fidl:fuchsia.crash",
31+
"$fuchsia_sdk_root/fidl:fuchsia.mem",
32+
"$fuchsia_sdk_root/pkg:async-loop",
33+
"$fuchsia_sdk_root/pkg:async-loop-cpp",
34+
"$fuchsia_sdk_root/pkg:fdio",
35+
"$fuchsia_sdk_root/pkg:memfs",
36+
"$fuchsia_sdk_root/pkg:syslog",
37+
"$fuchsia_sdk_root/pkg:zx",
38+
"$fuchsia_sdk_root/pkg/lib/sys/cpp",
39+
"$fuchsia_sdk_root/pkg/lib/vfs/cpp",
40+
"//third_party/tonic",
41+
]
42+
43+
public_configs = [ ":utils_config" ]
44+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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/files.h"
6+
7+
#include <fcntl.h>
8+
#include <stdint.h>
9+
10+
#include "runtime/dart/utils/inlines.h"
11+
#include "runtime/dart/utils/logging.h"
12+
13+
namespace dart_utils {
14+
15+
namespace {
16+
17+
bool ReadFileDescriptor(int fd, std::string* result) {
18+
DEBUG_CHECK(result, LOG_TAG, "");
19+
result->clear();
20+
21+
if (fd < 0) {
22+
return false;
23+
}
24+
25+
constexpr size_t kBufferSize = 1 << 16;
26+
size_t offset = 0;
27+
ssize_t bytes_read = 0;
28+
do {
29+
offset += bytes_read;
30+
result->resize(offset + kBufferSize);
31+
bytes_read = read(fd, &(*result)[offset], kBufferSize);
32+
} while (bytes_read > 0);
33+
34+
if (bytes_read < 0) {
35+
result->clear();
36+
return false;
37+
}
38+
39+
result->resize(offset + bytes_read);
40+
return true;
41+
}
42+
43+
bool WriteFileDescriptor(int fd, const char* data, ssize_t size) {
44+
ssize_t total = 0;
45+
for (ssize_t partial = 0; total < size; total += partial) {
46+
partial = write(fd, data + total, size - total);
47+
if (partial < 0)
48+
return false;
49+
}
50+
return true;
51+
}
52+
53+
} // namespace
54+
55+
bool ReadFileToString(const std::string& path, std::string* result) {
56+
return ReadFileToStringAt(AT_FDCWD, path, result);
57+
}
58+
59+
bool ReadFileToStringAt(int dirfd,
60+
const std::string& path,
61+
std::string* result) {
62+
int fd = openat(dirfd, path.c_str(), O_RDONLY);
63+
bool status = ReadFileDescriptor(fd, result);
64+
close(fd);
65+
return status;
66+
}
67+
68+
bool WriteFile(const std::string& path, const char* data, ssize_t size) {
69+
int fd = openat(AT_FDCWD, path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0666);
70+
if (fd < 0) {
71+
return false;
72+
}
73+
bool status = WriteFileDescriptor(fd, data, size);
74+
close(fd);
75+
return status;
76+
}
77+
78+
} // namespace dart_utils
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_FILES_H_
6+
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_FILES_H_
7+
8+
#include <string>
9+
10+
namespace dart_utils {
11+
12+
// Reads the contents of the file at the given path or file descriptor and
13+
// stores the data in result. Returns true if the file was read successfully,
14+
// otherwise returns false. If this function returns false, |result| will be
15+
// the empty string.
16+
bool ReadFileToString(const std::string& path, std::string* result);
17+
bool ReadFileToStringAt(int dirfd,
18+
const std::string& path,
19+
std::string* result);
20+
21+
// Writes the given data to the file at the given path. Returns true if the data
22+
// was successfully written, otherwise returns false.
23+
bool WriteFile(const std::string& path, const char* data, ssize_t size);
24+
25+
} // namespace dart_utils
26+
27+
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_FILES_H_
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_HANDLE_EXCEPTION_H_
6+
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_HANDLE_EXCEPTION_H_
7+
8+
#include <memory>
9+
#include <string>
10+
11+
#include <lib/sys/cpp/service_directory.h>
12+
#include <sys/types.h>
13+
#include <third_party/dart/runtime/include/dart_api.h>
14+
15+
namespace dart_utils {
16+
17+
// If |result| is a Dart Exception, passes the exception message and stack trace
18+
// to the crash analyzer service for further handling.
19+
//
20+
// Otherwise early returns with OK status.
21+
zx_status_t HandleIfException(std::shared_ptr<::sys::ServiceDirectory> services,
22+
const std::string& component_url,
23+
Dart_Handle result);
24+
25+
// Passes the exception message and stack trace to the crash analyzer service
26+
// for further handling.
27+
zx_status_t HandleException(std::shared_ptr<::sys::ServiceDirectory> services,
28+
const std::string& component_url,
29+
const std::string& error,
30+
const std::string& stack_trace);
31+
32+
} // namespace dart_utils
33+
34+
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_HANDLE_EXCEPTION_H_
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_INLINES_H_
6+
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_INLINES_H_
7+
8+
#include <lib/syslog/global.h>
9+
10+
namespace dart_utils {
11+
12+
inline void Check(bool condition, const char* tag, const char* message = "") {
13+
if (!condition) {
14+
FX_LOG(FATAL, tag, message);
15+
}
16+
}
17+
18+
#ifndef NDEBUG
19+
#define DEBUG_CHECK(condition, tag, message) \
20+
dart_utils::Check(condition, tag, message)
21+
#else
22+
#define DEBUG_CHECK(condition, tag, message) (true || (condition))
23+
#endif
24+
25+
template <size_t SIZE, typename T>
26+
inline size_t ArraySize(T (&array)[SIZE]) {
27+
return SIZE;
28+
}
29+
30+
} // namespace dart_utils
31+
32+
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_INLINES_H_
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_LOGGING_H_
6+
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_LOGGING_H_
7+
8+
namespace dart_utils {
9+
10+
// Use to mark logs published via the syslog API.
11+
#define LOG_TAG "dart-utils"
12+
13+
} // namespace dart_utils
14+
15+
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_RUNTIME_DART_UTILS_LOGGING_H_

0 commit comments

Comments
 (0)