Skip to content

Commit aa8542e

Browse files
a-sivaCommit Queue
authored andcommitted
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 <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent 7397194 commit aa8542e

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

runtime/bin/process_win.cc

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,13 @@ class ProcessStarter {
377377
child_process_handle_ = INVALID_HANDLE_VALUE;
378378

379379
// Transform input strings to system format.
380-
const wchar_t* system_path = StringUtilsWin::Utf8ToWide(path_);
381-
const wchar_t** system_arguments;
382-
system_arguments = reinterpret_cast<const wchar_t**>(
383-
Dart_ScopeAllocate(arguments_length * sizeof(*system_arguments)));
380+
wchar_t* system_path = nullptr;
381+
StringUtilsWin::Utf8ToWide(path_, &system_path);
382+
wchar_t** system_arguments;
383+
system_arguments = reinterpret_cast<wchar_t**>(
384+
malloc(arguments_length * sizeof(*system_arguments)));
384385
for (int i = 0; i < arguments_length; i++) {
385-
system_arguments[i] = StringUtilsWin::Utf8ToWide(arguments[i]);
386+
StringUtilsWin::Utf8ToWide(arguments[i], &(system_arguments[i]));
386387
}
387388

388389
// Compute command-line length.
@@ -395,7 +396,7 @@ class ProcessStarter {
395396

396397
// Put together command-line string.
397398
command_line_ = reinterpret_cast<wchar_t*>(
398-
Dart_ScopeAllocate(command_line_length * sizeof(*command_line_)));
399+
malloc(command_line_length * sizeof(*command_line_)));
399400
int len = 0;
400401
int remaining = command_line_length;
401402
int written =
@@ -410,16 +411,21 @@ class ProcessStarter {
410411
remaining -= written;
411412
ASSERT(remaining >= 0);
412413
}
414+
for (int i = 0; i < arguments_length; i++) {
415+
free(system_arguments[i]);
416+
}
417+
free(system_arguments);
418+
free(system_path);
413419

414420
// Create environment block if an environment is supplied.
415421
environment_block_ = nullptr;
416422
if (environment != nullptr) {
417423
wchar_t** system_environment;
418424
system_environment = reinterpret_cast<wchar_t**>(
419-
Dart_ScopeAllocate(environment_length * sizeof(*system_environment)));
425+
malloc(environment_length * sizeof(*system_environment)));
420426
// Convert environment strings to system strings.
421427
for (intptr_t i = 0; i < environment_length; i++) {
422-
system_environment[i] = StringUtilsWin::Utf8ToWide(environment[i]);
428+
StringUtilsWin::Utf8ToWide(environment[i], &(system_environment[i]));
423429
}
424430

425431
// An environment block is a sequence of zero-terminated strings
@@ -429,7 +435,7 @@ class ProcessStarter {
429435
block_size += wcslen(system_environment[i]) + 1;
430436
}
431437
environment_block_ = reinterpret_cast<wchar_t*>(
432-
Dart_ScopeAllocate(block_size * sizeof(*environment_block_)));
438+
malloc(block_size * sizeof(*environment_block_)));
433439
intptr_t block_index = 0;
434440
for (intptr_t i = 0; i < environment_length; i++) {
435441
intptr_t len = wcslen(system_environment[i]);
@@ -442,21 +448,28 @@ class ProcessStarter {
442448
// Block-terminating zero char.
443449
environment_block_[block_index++] = '\0';
444450
ASSERT(block_index == block_size);
451+
for (intptr_t i = 0; i < environment_length; i++) {
452+
free(system_environment[i]);
453+
}
454+
free(system_environment);
445455
}
446456

447457
system_working_directory_ = nullptr;
448458
if (working_directory_ != nullptr) {
449-
system_working_directory_ =
450-
StringUtilsWin::Utf8ToWide(working_directory_);
459+
StringUtilsWin::Utf8ToWide(working_directory_,
460+
&system_working_directory_);
451461
}
452-
453462
attribute_list_ = nullptr;
454463
}
455464

456465
~ProcessStarter() {
457466
if (attribute_list_ != nullptr) {
458467
DeleteProcThreadAttributeList(attribute_list_);
468+
free(attribute_list_);
459469
}
470+
free(command_line_);
471+
free(environment_block_);
472+
free(system_working_directory_);
460473
}
461474

462475
int Start() {
@@ -485,8 +498,8 @@ class ProcessStarter {
485498
(GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
486499
return CleanupAndReturnError();
487500
}
488-
attribute_list_ = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(
489-
Dart_ScopeAllocate(size));
501+
attribute_list_ =
502+
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(size));
490503
ZeroMemory(attribute_list_, size);
491504
if (!InitializeProcThreadAttributeList(attribute_list_, 1, 0, &size)) {
492505
return CleanupAndReturnError();
@@ -598,8 +611,8 @@ class ProcessStarter {
598611
(GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
599612
return CleanupAndReturnError();
600613
}
601-
attribute_list_ = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(
602-
Dart_ScopeAllocate(size));
614+
attribute_list_ =
615+
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(size));
603616
ZeroMemory(attribute_list_, size);
604617
if (!InitializeProcThreadAttributeList(attribute_list_, 1, 0, &size)) {
605618
return CleanupAndReturnError();
@@ -701,7 +714,7 @@ class ProcessStarter {
701714
HANDLE exit_handles_[2];
702715
HANDLE child_process_handle_;
703716

704-
const wchar_t* system_working_directory_;
717+
wchar_t* system_working_directory_;
705718
wchar_t* command_line_;
706719
wchar_t* environment_block_;
707720
std::vector<HANDLE> inherited_handles_;

runtime/bin/utils_win.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ char* StringUtilsWin::WideToUtf8(wchar_t* wide,
125125
return utf8;
126126
}
127127

128+
void StringUtilsWin::WideToUtf8(const wchar_t* wide, char** utf8) {
129+
// The parameter -1 ensures WideCharToMultiByte will include the terminating
130+
// NUL byte in the length.
131+
intptr_t len = -1;
132+
int utf8_len =
133+
WideCharToMultiByte(CP_UTF8, 0, wide, len, nullptr, 0, nullptr, nullptr);
134+
*utf8 = reinterpret_cast<char*>(malloc(utf8_len * sizeof(*utf8)));
135+
WideCharToMultiByte(CP_UTF8, 0, wide, len, *utf8, utf8_len, nullptr, nullptr);
136+
}
137+
128138
wchar_t* StringUtilsWin::Utf8ToWide(char* utf8,
129139
intptr_t len,
130140
intptr_t* result_len) {
@@ -141,6 +151,15 @@ wchar_t* StringUtilsWin::Utf8ToWide(char* utf8,
141151
return wide;
142152
}
143153

154+
void StringUtilsWin::Utf8ToWide(const char* utf8, wchar_t** wide) {
155+
// The parameter -1 ensures MultiByteToWideChar will include the terminating
156+
// NUL byte in the length.
157+
intptr_t len = -1;
158+
intptr_t wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, nullptr, 0);
159+
*wide = reinterpret_cast<wchar_t*>(malloc(wide_len * sizeof(*wide)));
160+
MultiByteToWideChar(CP_UTF8, 0, utf8, len, *wide, wide_len);
161+
}
162+
144163
const char* StringUtils::Utf8ToConsoleString(const char* utf8,
145164
intptr_t len,
146165
intptr_t* result_len) {

runtime/bin/utils_win.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ class StringUtilsWin {
3232
static char* WideToUtf8(wchar_t* wide,
3333
intptr_t len = -1,
3434
intptr_t* result_len = nullptr);
35+
static void WideToUtf8(const wchar_t* wide, char** result);
3536
static const char* WideToUtf8(const wchar_t* wide,
3637
intptr_t len = -1,
3738
intptr_t* result_len = nullptr);
3839
static wchar_t* Utf8ToWide(char* utf8,
3940
intptr_t len = -1,
4041
intptr_t* result_len = nullptr);
42+
static void Utf8ToWide(const char* utf8, wchar_t** result);
4143
static const wchar_t* Utf8ToWide(const char* utf8,
4244
intptr_t len = -1,
4345
intptr_t* result_len = nullptr);

0 commit comments

Comments
 (0)