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 <asiva@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
asiva
2025-05-06 17:10:25 -07:00
committed by Commit Queue
parent 73971941f5
commit aa8542e1db
3 changed files with 51 additions and 17 deletions
+30 -17
View File
@@ -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<const wchar_t**>(
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<wchar_t**>(
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<wchar_t*>(
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<wchar_t**>(
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<wchar_t*>(
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<LPPROC_THREAD_ATTRIBUTE_LIST>(
Dart_ScopeAllocate(size));
attribute_list_ =
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(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<LPPROC_THREAD_ATTRIBUTE_LIST>(
Dart_ScopeAllocate(size));
attribute_list_ =
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(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<HANDLE> inherited_handles_;
+19
View File
@@ -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<char*>(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<wchar_t*>(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) {
+2
View File
@@ -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);