From aa8542e1dbc2594a42635810d55eae84fec85c2f Mon Sep 17 00:00:00 2001 From: asiva Date: Tue, 6 May 2025 17:10:25 -0700 Subject: [PATCH] Enable use of Process::Start functions before initialization of the Dart VM is done. TEST=ci Change-Id: I9b20368048b07af5b37b1622676ad498a0c24225 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/427021 Commit-Queue: Siva Annamalai Reviewed-by: Ryan Macnak --- runtime/bin/process_win.cc | 47 ++++++++++++++++++++++++-------------- runtime/bin/utils_win.cc | 19 +++++++++++++++ runtime/bin/utils_win.h | 2 ++ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/runtime/bin/process_win.cc b/runtime/bin/process_win.cc index 43059b0d4dc..a531acf0a1d 100644 --- a/runtime/bin/process_win.cc +++ b/runtime/bin/process_win.cc @@ -377,12 +377,13 @@ class ProcessStarter { child_process_handle_ = INVALID_HANDLE_VALUE; // Transform input strings to system format. - const wchar_t* system_path = StringUtilsWin::Utf8ToWide(path_); - const wchar_t** system_arguments; - system_arguments = reinterpret_cast( - Dart_ScopeAllocate(arguments_length * sizeof(*system_arguments))); + wchar_t* system_path = nullptr; + StringUtilsWin::Utf8ToWide(path_, &system_path); + wchar_t** system_arguments; + system_arguments = reinterpret_cast( + malloc(arguments_length * sizeof(*system_arguments))); for (int i = 0; i < arguments_length; i++) { - system_arguments[i] = StringUtilsWin::Utf8ToWide(arguments[i]); + StringUtilsWin::Utf8ToWide(arguments[i], &(system_arguments[i])); } // Compute command-line length. @@ -395,7 +396,7 @@ class ProcessStarter { // Put together command-line string. command_line_ = reinterpret_cast( - Dart_ScopeAllocate(command_line_length * sizeof(*command_line_))); + malloc(command_line_length * sizeof(*command_line_))); int len = 0; int remaining = command_line_length; int written = @@ -410,16 +411,21 @@ class ProcessStarter { remaining -= written; ASSERT(remaining >= 0); } + for (int i = 0; i < arguments_length; i++) { + free(system_arguments[i]); + } + free(system_arguments); + free(system_path); // Create environment block if an environment is supplied. environment_block_ = nullptr; if (environment != nullptr) { wchar_t** system_environment; system_environment = reinterpret_cast( - Dart_ScopeAllocate(environment_length * sizeof(*system_environment))); + malloc(environment_length * sizeof(*system_environment))); // Convert environment strings to system strings. for (intptr_t i = 0; i < environment_length; i++) { - system_environment[i] = StringUtilsWin::Utf8ToWide(environment[i]); + StringUtilsWin::Utf8ToWide(environment[i], &(system_environment[i])); } // An environment block is a sequence of zero-terminated strings @@ -429,7 +435,7 @@ class ProcessStarter { block_size += wcslen(system_environment[i]) + 1; } environment_block_ = reinterpret_cast( - Dart_ScopeAllocate(block_size * sizeof(*environment_block_))); + malloc(block_size * sizeof(*environment_block_))); intptr_t block_index = 0; for (intptr_t i = 0; i < environment_length; i++) { intptr_t len = wcslen(system_environment[i]); @@ -442,21 +448,28 @@ class ProcessStarter { // Block-terminating zero char. environment_block_[block_index++] = '\0'; ASSERT(block_index == block_size); + for (intptr_t i = 0; i < environment_length; i++) { + free(system_environment[i]); + } + free(system_environment); } system_working_directory_ = nullptr; if (working_directory_ != nullptr) { - system_working_directory_ = - StringUtilsWin::Utf8ToWide(working_directory_); + StringUtilsWin::Utf8ToWide(working_directory_, + &system_working_directory_); } - attribute_list_ = nullptr; } ~ProcessStarter() { if (attribute_list_ != nullptr) { DeleteProcThreadAttributeList(attribute_list_); + free(attribute_list_); } + free(command_line_); + free(environment_block_); + free(system_working_directory_); } int Start() { @@ -485,8 +498,8 @@ class ProcessStarter { (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) { return CleanupAndReturnError(); } - attribute_list_ = reinterpret_cast( - Dart_ScopeAllocate(size)); + attribute_list_ = + reinterpret_cast(malloc(size)); ZeroMemory(attribute_list_, size); if (!InitializeProcThreadAttributeList(attribute_list_, 1, 0, &size)) { return CleanupAndReturnError(); @@ -598,8 +611,8 @@ class ProcessStarter { (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) { return CleanupAndReturnError(); } - attribute_list_ = reinterpret_cast( - Dart_ScopeAllocate(size)); + attribute_list_ = + reinterpret_cast(malloc(size)); ZeroMemory(attribute_list_, size); if (!InitializeProcThreadAttributeList(attribute_list_, 1, 0, &size)) { return CleanupAndReturnError(); @@ -701,7 +714,7 @@ class ProcessStarter { HANDLE exit_handles_[2]; HANDLE child_process_handle_; - const wchar_t* system_working_directory_; + wchar_t* system_working_directory_; wchar_t* command_line_; wchar_t* environment_block_; std::vector inherited_handles_; diff --git a/runtime/bin/utils_win.cc b/runtime/bin/utils_win.cc index 79a7eb80129..99452458f1f 100644 --- a/runtime/bin/utils_win.cc +++ b/runtime/bin/utils_win.cc @@ -125,6 +125,16 @@ char* StringUtilsWin::WideToUtf8(wchar_t* wide, return utf8; } +void StringUtilsWin::WideToUtf8(const wchar_t* wide, char** utf8) { + // The parameter -1 ensures WideCharToMultiByte will include the terminating + // NUL byte in the length. + intptr_t len = -1; + int utf8_len = + WideCharToMultiByte(CP_UTF8, 0, wide, len, nullptr, 0, nullptr, nullptr); + *utf8 = reinterpret_cast(malloc(utf8_len * sizeof(*utf8))); + WideCharToMultiByte(CP_UTF8, 0, wide, len, *utf8, utf8_len, nullptr, nullptr); +} + wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, intptr_t len, intptr_t* result_len) { @@ -141,6 +151,15 @@ wchar_t* StringUtilsWin::Utf8ToWide(char* utf8, return wide; } +void StringUtilsWin::Utf8ToWide(const char* utf8, wchar_t** wide) { + // The parameter -1 ensures MultiByteToWideChar will include the terminating + // NUL byte in the length. + intptr_t len = -1; + intptr_t wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, nullptr, 0); + *wide = reinterpret_cast(malloc(wide_len * sizeof(*wide))); + MultiByteToWideChar(CP_UTF8, 0, utf8, len, *wide, wide_len); +} + const char* StringUtils::Utf8ToConsoleString(const char* utf8, intptr_t len, intptr_t* result_len) { diff --git a/runtime/bin/utils_win.h b/runtime/bin/utils_win.h index 4f131cdc29c..101760e7bfc 100644 --- a/runtime/bin/utils_win.h +++ b/runtime/bin/utils_win.h @@ -32,12 +32,14 @@ class StringUtilsWin { static char* WideToUtf8(wchar_t* wide, intptr_t len = -1, intptr_t* result_len = nullptr); + static void WideToUtf8(const wchar_t* wide, char** result); static const char* WideToUtf8(const wchar_t* wide, intptr_t len = -1, intptr_t* result_len = nullptr); static wchar_t* Utf8ToWide(char* utf8, intptr_t len = -1, intptr_t* result_len = nullptr); + static void Utf8ToWide(const char* utf8, wchar_t** result); static const wchar_t* Utf8ToWide(const char* utf8, intptr_t len = -1, intptr_t* result_len = nullptr);