Skip to content

Commit c0d7ef5

Browse files
huoyaoyuanjkotasCopilot
authored
Remove GetCommandLineW from PAL (#125386)
Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 2d24182 commit c0d7ef5

12 files changed

Lines changed: 68 additions & 439 deletions

File tree

src/coreclr/dlls/mscoree/exports.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ int coreclr_initialize(
262262
&hostContract);
263263

264264
#ifdef TARGET_UNIX
265-
DWORD error = PAL_InitializeCoreCLR(exePath, g_coreclr_embedded);
265+
DWORD error = PAL_InitializeCoreCLR(g_coreclr_embedded);
266266
hr = HRESULT_FROM_WIN32(error);
267267

268268
// If PAL initialization failed, then we should return right away and avoid

src/coreclr/pal/inc/pal.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ PALIMPORT
201201
DWORD
202202
PALAPI
203203
PAL_InitializeCoreCLR(
204-
const char *szExePath, BOOL runningInExe);
204+
BOOL runningInExe);
205205

206206
/// <summary>
207207
/// This function shuts down PAL WITHOUT exiting the current process.
@@ -3303,15 +3303,6 @@ PALAPI
33033303
SetLastError(
33043304
IN DWORD dwErrCode);
33053305

3306-
PALIMPORT
3307-
LPWSTR
3308-
PALAPI
3309-
GetCommandLineW();
3310-
3311-
#ifdef UNICODE
3312-
#define GetCommandLine GetCommandLineW
3313-
#endif
3314-
33153306
PALIMPORT
33163307
VOID
33173308
PALAPI

src/coreclr/pal/src/include/pal/process.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,6 @@ Notes :
6969
--*/
7070
BOOL PROCCreateInitialProcess(LPWSTR lpwstrCmdLine, LPWSTR lpwstrFullPath);
7171

72-
/*++
73-
Function:
74-
PROCCleanupInitialProcess
75-
76-
Abstract
77-
Cleanup all the structures for the initial process.
78-
79-
Parameter
80-
VOID
81-
82-
Return
83-
VOID
84-
85-
--*/
86-
VOID PROCCleanupInitialProcess(VOID);
87-
8872
/*++
8973
Function
9074
PROCAbortInitialize()

src/coreclr/pal/src/include/pal/procobj.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ Module Name:
2323

2424
namespace CorUnix
2525
{
26-
PAL_ERROR
27-
InitializeProcessCommandLine(
28-
LPWSTR lpwstrCmdLine,
29-
LPWSTR lpwstrFullPath
30-
);
31-
3226
PAL_ERROR
3327
CreateInitialProcessAndThreadObjects(
3428
CPalThread *pThread

src/coreclr/pal/src/init/pal.cpp

Lines changed: 24 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ static minipal_mutex* init_critsec = NULL;
114114

115115
static DWORD g_initializeDLLFlags = PAL_INITIALIZE_DLL;
116116

117-
static int Initialize(int argc, const char *const argv[], DWORD flags);
118-
static LPWSTR INIT_FormatCommandLine (int argc, const char * const *argv);
117+
static int Initialize(DWORD flags);
119118
static LPWSTR INIT_GetCurrentEXEPath();
120-
static BOOL INIT_SharedFilesPath(void);
121119

122120
/*++
123121
Function:
@@ -139,7 +137,7 @@ PAL_Initialize(
139137
int argc,
140138
char *const argv[])
141139
{
142-
return Initialize(argc, argv, PAL_INITIALIZE);
140+
return Initialize(PAL_INITIALIZE);
143141
}
144142

145143
/*++
@@ -163,7 +161,7 @@ PAL_InitializeWithFlags(
163161
const char *const argv[],
164162
DWORD flags)
165163
{
166-
return Initialize(argc, argv, flags);
164+
return Initialize(flags);
167165
}
168166

169167
/*++
@@ -182,7 +180,7 @@ int
182180
PALAPI
183181
PAL_InitializeDLL()
184182
{
185-
return Initialize(0, nullptr, g_initializeDLLFlags);
183+
return Initialize(g_initializeDLLFlags);
186184
}
187185

188186
/*++
@@ -285,22 +283,19 @@ InitializeDefaultStackSize()
285283
--*/
286284
int
287285
Initialize(
288-
int argc,
289-
const char *const argv[],
290286
DWORD flags)
291287
{
292288
PAL_ERROR palError = ERROR_GEN_FAILURE;
293289
CPalThread *pThread = nullptr;
294290
CListedObjectManager *plom = nullptr;
295-
LPWSTR command_line = nullptr;
296291
LPWSTR exe_path = nullptr;
297292
int retval = -1;
298293
bool fFirstTimeInit = false;
299294

300295
/* the first ENTRY within the first call to PAL_Initialize is a special
301296
case, since debug channels are not initialized yet. So in that case the
302297
ENTRY will be called after the DBG channels initialization */
303-
ENTRY_EXTERNAL("PAL_Initialize(argc = %d argv = %p)\n", argc, argv);
298+
ENTRY_EXTERNAL("PAL_Initialize\n");
304299

305300
/*Firstly initiate a lastError */
306301
SetLastError(ERROR_GEN_FAILURE);
@@ -467,50 +462,25 @@ Initialize(
467462

468463
palError = ERROR_GEN_FAILURE;
469464

470-
if (argc > 0 && argv != nullptr)
465+
/* find out the application's full path */
466+
exe_path = INIT_GetCurrentEXEPath();
467+
if (nullptr == exe_path)
471468
{
472-
/* build the command line */
473-
command_line = INIT_FormatCommandLine(argc, argv);
474-
if (nullptr == command_line)
475-
{
476-
ERROR("Error building command line\n");
477-
palError = ERROR_PALINIT_COMMAND_LINE;
478-
goto CLEANUP1d;
479-
}
480-
481-
/* find out the application's full path */
482-
exe_path = INIT_GetCurrentEXEPath();
483-
if (nullptr == exe_path)
484-
{
485-
ERROR("Unable to find exe path\n");
486-
palError = ERROR_PALINIT_CONVERT_EXE_PATH;
487-
goto CLEANUP1e;
488-
}
489-
490-
palError = InitializeProcessCommandLine(
491-
command_line,
492-
exe_path);
493-
494-
if (NO_ERROR != palError)
495-
{
496-
ERROR("Unable to initialize command line\n");
497-
goto CLEANUP2;
498-
}
499-
500-
// InitializeProcessCommandLine took ownership of this memory.
501-
command_line = nullptr;
502-
503-
if (!LOADSetExeName(exe_path))
504-
{
505-
ERROR("Unable to set exe name\n");
506-
palError = ERROR_PALINIT_SET_EXE_NAME;
507-
goto CLEANUP2;
508-
}
469+
ERROR("Unable to find exe path\n");
470+
palError = ERROR_PALINIT_CONVERT_EXE_PATH;
471+
goto CLEANUP1e;
472+
}
509473

510-
// LOADSetExeName took ownership of this memory.
511-
exe_path = nullptr;
474+
if (!LOADSetExeName(exe_path))
475+
{
476+
ERROR("Unable to set exe name\n");
477+
palError = ERROR_PALINIT_SET_EXE_NAME;
478+
goto CLEANUP2;
512479
}
513480

481+
// LOADSetExeName took ownership of this memory.
482+
exe_path = nullptr;
483+
514484
if (init_count == 0)
515485
{
516486
//
@@ -600,12 +570,10 @@ Initialize(
600570
CLEANUP10:
601571
MAPCleanup();
602572
CLEANUP6:
603-
PROCCleanupInitialProcess();
573+
// Cleanup initial process data
604574
CLEANUP2:
605575
free(exe_path);
606576
CLEANUP1e:
607-
free(command_line);
608-
CLEANUP1d:
609577
// Cleanup synchronization manager
610578
CLEANUP1c:
611579
// Cleanup object manager
@@ -642,8 +610,7 @@ Initialize(
642610
PAL_InitializeCoreCLR
643611
644612
Abstract:
645-
A replacement for PAL_Initialize when loading CoreCLR. Instead of taking a command line (which CoreCLR
646-
instances aren't given anyway) the path into which the CoreCLR is installed is supplied instead.
613+
A replacement for PAL_Initialize when loading CoreCLR.
647614
648615
This routine also makes sure the psuedo dynamic libraries PALRT and mscorwks have their initialization
649616
methods called.
@@ -655,12 +622,11 @@ Initialize(
655622
--*/
656623
PAL_ERROR
657624
PALAPI
658-
PAL_InitializeCoreCLR(const char *szExePath, BOOL runningInExe)
625+
PAL_InitializeCoreCLR(BOOL runningInExe)
659626
{
660627
g_running_in_exe = runningInExe;
661628

662-
// Fake up a command line to call PAL initialization with.
663-
int result = Initialize(1, &szExePath, PAL_INITIALIZE_CORECLR);
629+
int result = Initialize(PAL_INITIALIZE_CORECLR);
664630
if (result != 0)
665631
{
666632
return GetLastError();
@@ -851,132 +817,6 @@ void PALInitUnlock(void)
851817

852818
/* Internal functions *********************************************************/
853819

854-
/*++
855-
Function:
856-
INIT_FormatCommandLine [Internal]
857-
858-
Abstract:
859-
This function converts an array of arguments (argv) into a Unicode
860-
command-line for use by GetCommandLineW
861-
862-
Parameters :
863-
int argc : number of arguments in argv
864-
char **argv : argument list in an array of NULL-terminated strings
865-
866-
Return value :
867-
pointer to Unicode command line. This is a buffer allocated with malloc;
868-
caller is responsible for freeing it with free()
869-
870-
Note : not all peculiarities of Windows command-line processing are supported;
871-
872-
-what is supported :
873-
-arguments with white-space must be double quoted (we'll just double-quote
874-
all arguments to simplify things)
875-
-some characters must be escaped with \ : particularly, the double-quote,
876-
to avoid confusion with the double-quotes at the start and end of
877-
arguments, and \ itself, to avoid confusion with escape sequences.
878-
-what is not supported:
879-
-under Windows, \\ is interpreted as an escaped \ ONLY if it's followed by
880-
an escaped double-quote \". \\\" is passed to argv as \", but \\a is
881-
passed to argv as \\a... there may be other similar cases
882-
-there may be other characters which must be escaped
883-
--*/
884-
static LPWSTR INIT_FormatCommandLine (int argc, const char * const *argv)
885-
{
886-
LPWSTR retval;
887-
LPSTR command_line=nullptr, command_ptr;
888-
LPCSTR arg_ptr;
889-
INT length, i,j;
890-
BOOL bQuoted = FALSE;
891-
892-
/* list of characters that need no be escaped with \ when building the
893-
command line. currently " and \ */
894-
LPCSTR ESCAPE_CHARS="\"\\";
895-
896-
/* allocate temporary memory for the string. Play it safe :
897-
double the length of each argument (in case they're composed
898-
exclusively of escaped characters), and add 3 (for the double-quotes
899-
and separating space). This is temporary anyway, we return a LPWSTR */
900-
length=0;
901-
for(i=0; i<argc; i++)
902-
{
903-
TRACE("argument %d is %s\n", i, argv[i]);
904-
length+=3;
905-
length+=strlen(argv[i])*2;
906-
}
907-
command_line = reinterpret_cast<LPSTR>(malloc(length != 0 ? length : 1));
908-
909-
if(!command_line)
910-
{
911-
ERROR("couldn't allocate memory for command line!\n");
912-
return nullptr;
913-
}
914-
915-
command_ptr=command_line;
916-
for(i=0; i<argc; i++)
917-
{
918-
/* double-quote at beginning of argument containing at least one space */
919-
for(j = 0; (argv[i][j] != 0) && (!isspace((unsigned char) argv[i][j])); j++);
920-
921-
if (argv[i][j] != 0)
922-
{
923-
*command_ptr++='"';
924-
bQuoted = TRUE;
925-
}
926-
/* process the argument one character at a time */
927-
for(arg_ptr=argv[i]; *arg_ptr; arg_ptr++)
928-
{
929-
/* if character needs to be escaped, prepend a \ to it. */
930-
if( strchr(ESCAPE_CHARS,*arg_ptr))
931-
{
932-
*command_ptr++='\\';
933-
}
934-
935-
/* now we can copy the actual character over. */
936-
*command_ptr++=*arg_ptr;
937-
}
938-
/* double-quote at end of argument; space to separate arguments */
939-
if (bQuoted == TRUE)
940-
{
941-
*command_ptr++='"';
942-
bQuoted = FALSE;
943-
}
944-
*command_ptr++=' ';
945-
}
946-
/* replace the last space with a NULL terminator */
947-
command_ptr--;
948-
*command_ptr='\0';
949-
950-
/* convert to Unicode */
951-
i = MultiByteToWideChar(CP_ACP, 0,command_line, -1, nullptr, 0);
952-
if (i == 0)
953-
{
954-
ASSERT("MultiByteToWideChar failure\n");
955-
free(command_line);
956-
return nullptr;
957-
}
958-
959-
retval = reinterpret_cast<LPWSTR>(malloc((sizeof(WCHAR)*i)));
960-
if(retval == nullptr)
961-
{
962-
ERROR("can't allocate memory for Unicode command line!\n");
963-
free(command_line);
964-
return nullptr;
965-
}
966-
967-
if(!MultiByteToWideChar(CP_ACP, 0,command_line, -1, retval, i))
968-
{
969-
ASSERT("MultiByteToWideChar failure\n");
970-
free(retval);
971-
retval = nullptr;
972-
}
973-
else
974-
TRACE("Command line is %s\n", command_line);
975-
976-
free(command_line);
977-
return retval;
978-
}
979-
980820
/*++
981821
Function:
982822
INIT_GetCurrentEXEPath

0 commit comments

Comments
 (0)