From f185012d20cbbeb020caa970a0b384f13e3352a4 Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Mon, 29 Oct 2018 15:11:16 +0000 Subject: [PATCH] [gardening] Integrate Crashpad with run_vm_tests. Change-Id: Ia18b5e22cefe1fb4dfa8d8ed9361789304eb8acc Reviewed-on: https://dart-review.googlesource.com/c/81826 Commit-Queue: Vyacheslav Egorov Commit-Queue: Martin Kustermann Auto-Submit: Vyacheslav Egorov Reviewed-by: Martin Kustermann --- runtime/bin/BUILD.gn | 44 +++++++++++----- runtime/bin/crashpad.cc | 81 ++++++++++++++++++++++++++++++ runtime/bin/crashpad.h | 16 ++++++ runtime/bin/main.cc | 66 ++---------------------- runtime/bin/run_vm_tests.cc | 3 ++ tools/testing/dart/test_suite.dart | 5 +- 6 files changed, 137 insertions(+), 78 deletions(-) create mode 100644 runtime/bin/crashpad.cc create mode 100644 runtime/bin/crashpad.h diff --git a/runtime/bin/BUILD.gn b/runtime/bin/BUILD.gn index 8689f2c367a..cec8c5240b1 100644 --- a/runtime/bin/BUILD.gn +++ b/runtime/bin/BUILD.gn @@ -89,6 +89,35 @@ build_libdart_builtin("libdart_builtin_product_fuchsia") { ] } +static_library("crashpad") { + configs += [ + "..:dart_arch_config", + "..:dart_config", + "..:dart_product_config", + "..:dart_os_fuchsia_config", + ] + if (is_fuchsia) { + configs -= [ "//build/config:symbol_visibility_hidden" ] + } + include_dirs = [ ".." ] + sources = [ + "crashpad.cc", + ] + + if (dart_use_crashpad) { + assert(is_win, "dart_use_crashpad is only supported on Windows") + deps = [ + "//third_party/crashpad/crashpad/client", + "//third_party/mini_chromium/mini_chromium/base", + + # This binary is used to handle crashes of the dart binary. + "//third_party/crashpad/crashpad/handler:crashpad_handler", + ] + include_dirs += [ "//third_party/crashpad" ] + defines = [ "DART_USE_CRASHPAD" ] + } +} + template("build_gen_snapshot") { extra_configs = [] if (defined(invoker.extra_configs)) { @@ -664,6 +693,7 @@ template("dart_executable") { ":libdart_builtin", "//third_party/boringssl", "//third_party/zlib", + ":crashpad", ] + extra_deps defines = extra_defines @@ -680,19 +710,6 @@ template("dart_executable") { "//third_party", ] - if (dart_use_crashpad) { - assert(is_win, "dart_use_crashpad is only supported on Windows") - deps += [ - "//third_party/crashpad/crashpad/client", - "//third_party/mini_chromium/mini_chromium/base", - - # This binary is used to handle crashes of the dart binary. - "//third_party/crashpad/crashpad/handler:crashpad_handler", - ] - include_dirs += [ "//third_party/crashpad" ] - defines += [ "DART_USE_CRASHPAD" ] - } - sources = [ "dart_embedder_api_impl.cc", "error_exit.cc", @@ -882,6 +899,7 @@ executable("run_vm_tests") { } deps = [ + ":crashpad", ":dart_kernel_platform_cc", ":dart_snapshot_cc", ":gen_kernel_bytecode_dill", diff --git a/runtime/bin/crashpad.cc b/runtime/bin/crashpad.cc new file mode 100644 index 00000000000..184dba11d6b --- /dev/null +++ b/runtime/bin/crashpad.cc @@ -0,0 +1,81 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "bin/crashpad.h" + +#if defined(DART_USE_CRASHPAD) +#include +#include +#include + +#include "crashpad/client/crashpad_client.h" +#include "crashpad/client/crashpad_info.h" +#endif + +#include "bin/error_exit.h" +#include "bin/log.h" +#include "bin/platform.h" + +namespace dart { +namespace bin { + +#if defined(DART_USE_CRASHPAD) +#if !defined(HOST_OS_WINDOWS) +#error "Currently we only support Crashpad on Windows" +#endif + +void InitializeCrashpadClient() { + // DART_CRASHPAD_HANDLER and DART_CRASHPAD_CRASHES_DIR are set by the + // testing framework. + wchar_t* handler = _wgetenv(L"DART_CRASHPAD_HANDLER"); + wchar_t* crashes_dir = _wgetenv(L"DART_CRASHPAD_CRASHES_DIR"); + if (handler == nullptr || crashes_dir == nullptr || wcslen(handler) == 0 || + wcslen(crashes_dir) == 0) { + return; + } + + // Crashpad uses STL so we use it here too even though in general we + // avoid it. + const base::FilePath handler_path{std::wstring(handler)}; + const base::FilePath crashes_dir_path{std::wstring(crashes_dir)}; + const std::string url(""); + std::map annotations; + char* test_name = getenv("DART_TEST_NAME"); + if (test_name != nullptr) { + annotations["dart_test_name"] = test_name; + } + + std::vector arguments; + + crashpad::CrashpadClient client; + + // Prevent crashpad_handler from inheriting our standard output and error + // handles. Otherwise we would not be able to close them ourselves making + // tests that rely on that fail. + HANDLE original_stdout = GetStdHandle(STD_OUTPUT_HANDLE); + HANDLE original_stderr = GetStdHandle(STD_ERROR_HANDLE); + SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); + SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); + const bool success = + client.StartHandler(handler_path, crashes_dir_path, crashes_dir_path, url, + annotations, arguments, + /*restartable=*/true, + /*asynchronous_start=*/false); + SetStdHandle(STD_OUTPUT_HANDLE, original_stdout); + SetStdHandle(STD_ERROR_HANDLE, original_stderr); + + if (!success) { + Log::PrintErr("Failed to start the crash handler!\n"); + Platform::Exit(kErrorExitCode); + } + crashpad::CrashpadInfo::GetCrashpadInfo() + ->set_gather_indirectly_referenced_memory(crashpad::TriState::kEnabled, + /*limit=*/500 * MB); +} +#else +void InitializeCrashpadClient() {} +#endif // DART_USE_CRASHPAD + +} // namespace bin +} // namespace dart diff --git a/runtime/bin/crashpad.h b/runtime/bin/crashpad.h new file mode 100644 index 00000000000..2534fd1d5a0 --- /dev/null +++ b/runtime/bin/crashpad.h @@ -0,0 +1,16 @@ +// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#ifndef RUNTIME_BIN_CRASHPAD_H_ +#define RUNTIME_BIN_CRASHPAD_H_ + +namespace dart { +namespace bin { + +void InitializeCrashpadClient(); + +} // namespace bin +} // namespace dart + +#endif // RUNTIME_BIN_CRASHPAD_H_ diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc index ab8151e5a3a..aa10c515223 100644 --- a/runtime/bin/main.cc +++ b/runtime/bin/main.cc @@ -11,13 +11,9 @@ #include "include/dart_embedder_api.h" #include "include/dart_tools_api.h" -#if defined(DART_USE_CRASHPAD) -#include "crashpad/client/crashpad_client.h" -#include "crashpad/client/crashpad_info.h" -#endif - #include "bin/builtin.h" #include "bin/console.h" +#include "bin/crashpad.h" #include "bin/dartutils.h" #include "bin/dfe.h" #include "bin/directory.h" @@ -25,6 +21,7 @@ #include "bin/eventhandler.h" #include "bin/extensions.h" #include "bin/file.h" +#include "bin/gzip.h" #include "bin/isolate_data.h" #include "bin/loader.h" #include "bin/log.h" @@ -39,7 +36,6 @@ #include "platform/growable_array.h" #include "platform/hashmap.h" #include "platform/text_buffer.h" -#include "bin/gzip.h" #include "vm/flags.h" @@ -952,59 +948,6 @@ Dart_Handle GetVMServiceAssetsArchiveCallback() { static Dart_GetVMServiceAssetsArchive GetVMServiceAssetsArchiveCallback = NULL; #endif // !defined(NO_OBSERVATORY) -#if defined(DART_USE_CRASHPAD) -#if !defined(HOST_OS_WINDOWS) -#error "Currently we only support Crashpad on Windows" -#endif - -static void ConfigureCrashpadClient(crashpad::CrashpadClient* client) { - // DART_CRASHPAD_HANDLER and DART_CRASHPAD_CRASHES_DIR are set by the - // testing framework. - wchar_t* handler = _wgetenv(L"DART_CRASHPAD_HANDLER"); - wchar_t* crashes_dir = _wgetenv(L"DART_CRASHPAD_CRASHES_DIR"); - if (handler == nullptr || crashes_dir == nullptr || wcslen(handler) == 0 || - wcslen(crashes_dir) == 0) { - return; - } - - // Crashpad uses STL so we use it here too even though in general we - // avoid it. - const base::FilePath handler_path{std::wstring(handler)}; - const base::FilePath crashes_dir_path{std::wstring(crashes_dir)}; - const std::string url(""); - std::map annotations; - char* test_name = getenv("DART_TEST_NAME"); - if (test_name != nullptr) { - annotations["dart_test_name"] = test_name; - } - - std::vector arguments; - - // Prevent crashpad_handler from inheriting our standard output and error - // handles. Otherwise we would not be able to close them ourselves making - // tests that rely on that fail. - HANDLE original_stdout = GetStdHandle(STD_OUTPUT_HANDLE); - HANDLE original_stderr = GetStdHandle(STD_ERROR_HANDLE); - SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE); - SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE); - const bool success = - client->StartHandler(handler_path, crashes_dir_path, crashes_dir_path, - url, annotations, arguments, - /*restartable=*/true, - /*asynchronous_start=*/false); - SetStdHandle(STD_OUTPUT_HANDLE, original_stdout); - SetStdHandle(STD_ERROR_HANDLE, original_stderr); - - if (!success) { - Log::PrintErr("Failed to start the crash handler!\n"); - Platform::Exit(kErrorExitCode); - } - crashpad::CrashpadInfo::GetCrashpadInfo() - ->set_gather_indirectly_referenced_memory(crashpad::TriState::kEnabled, - /*limit=*/500 * MB); -} -#endif // DART_USE_CRASHPAD - void main(int argc, char** argv) { char* script_name; const int EXTRA_VM_ARGUMENTS = 10; @@ -1068,10 +1011,7 @@ void main(int argc, char** argv) { } DartUtils::SetEnvironment(Options::environment()); -#if defined(DART_USE_CRASHPAD) - crashpad::CrashpadClient crashpad_client; - ConfigureCrashpadClient(&crashpad_client); -#endif + InitializeCrashpadClient(); Loader::InitOnce(); diff --git a/runtime/bin/run_vm_tests.cc b/runtime/bin/run_vm_tests.cc index 2240355e3ed..7d1e2571537 100644 --- a/runtime/bin/run_vm_tests.cc +++ b/runtime/bin/run_vm_tests.cc @@ -3,6 +3,7 @@ // BSD-style license that can be found in the LICENSE file. #include "bin/console.h" +#include "bin/crashpad.h" #include "bin/dartutils.h" #include "bin/dfe.h" #include "bin/eventhandler.h" @@ -220,6 +221,8 @@ static int Main(int argc, const char** argv) { return 1; } + dart::bin::InitializeCrashpadClient(); + // Save the console state so we can restore it later. dart::bin::Console::SaveConfig(); diff --git a/tools/testing/dart/test_suite.dart b/tools/testing/dart/test_suite.dart index 4e1446762a7..9676462017b 100644 --- a/tools/testing/dart/test_suite.dart +++ b/tools/testing/dart/test_suite.dart @@ -446,12 +446,13 @@ class VMTestSuite extends TestSuite { VMTestSuite(TestConfiguration configuration) : dartDir = Repository.dir.toNativePath(), super(configuration, "vm", ["runtime/tests/vm/vm.status"]) { + var binarySuffix = Platform.operatingSystem == 'windows' ? '.exe' : ''; + // For running the tests we use the given '$runnerName' binary - targetRunnerPath = '$buildDir/run_vm_tests'; + targetRunnerPath = '$buildDir/run_vm_tests$binarySuffix'; // For listing the tests we use the '$runnerName.host' binary if it exists // and use '$runnerName' if it doesn't. - var binarySuffix = Platform.operatingSystem == 'windows' ? '.exe' : ''; var hostBinary = '$targetRunnerPath.host$binarySuffix'; if (new File(hostBinary).existsSync()) { hostRunnerPath = hostBinary;