Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/dlls/mscoree/exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ int coreclr_initialize(
&hostContract);

#ifdef TARGET_UNIX
DWORD error = PAL_InitializeCoreCLR(exePath, g_coreclr_embedded);
DWORD error = PAL_InitializeCoreCLR(g_coreclr_embedded);
hr = HRESULT_FROM_WIN32(error);

// If PAL initialization failed, then we should return right away and avoid
Expand Down
11 changes: 1 addition & 10 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ PALIMPORT
DWORD
PALAPI
PAL_InitializeCoreCLR(
const char *szExePath, BOOL runningInExe);
BOOL runningInExe);

/// <summary>
/// This function shuts down PAL WITHOUT exiting the current process.
Expand Down Expand Up @@ -3399,15 +3399,6 @@ PALAPI
SetLastError(
IN DWORD dwErrCode);

PALIMPORT
LPWSTR
PALAPI
GetCommandLineW();

#ifdef UNICODE
#define GetCommandLine GetCommandLineW
#endif

PALIMPORT
VOID
PALAPI
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/pal/src/include/pal/procobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ namespace CorUnix

PAL_ERROR
InitializeProcessCommandLine(
LPWSTR lpwstrCmdLine,
LPWSTR lpwstrFullPath
);

Expand Down
208 changes: 29 additions & 179 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ static minipal_mutex* init_critsec = NULL;

static DWORD g_initializeDLLFlags = PAL_INITIALIZE_DLL;

static int Initialize(int argc, const char *const argv[], DWORD flags);
static LPWSTR INIT_FormatCommandLine (int argc, const char * const *argv);
static int Initialize(DWORD flags);
static LPWSTR INIT_GetCurrentEXEPath();
static BOOL INIT_SharedFilesPath(void);

/*++
Function:
Expand All @@ -139,7 +137,7 @@ PAL_Initialize(
int argc,
char *const argv[])
{
return Initialize(argc, argv, PAL_INITIALIZE);
return Initialize(PAL_INITIALIZE);
}

/*++
Expand All @@ -163,7 +161,7 @@ PAL_InitializeWithFlags(
const char *const argv[],
DWORD flags)
{
return Initialize(argc, argv, flags);
return Initialize(flags);
}

/*++
Expand All @@ -182,7 +180,7 @@ int
PALAPI
PAL_InitializeDLL()
{
return Initialize(0, nullptr, g_initializeDLLFlags);
return Initialize(g_initializeDLLFlags);
}

/*++
Expand Down Expand Up @@ -285,22 +283,19 @@ InitializeDefaultStackSize()
--*/
int
Initialize(
int argc,
const char *const argv[],
DWORD flags)
{
PAL_ERROR palError = ERROR_GEN_FAILURE;
CPalThread *pThread = nullptr;
CListedObjectManager *plom = nullptr;
LPWSTR command_line = nullptr;
LPWSTR exe_path = nullptr;
int retval = -1;
bool fFirstTimeInit = false;

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

/*Firstly initiate a lastError */
SetLastError(ERROR_GEN_FAILURE);
Expand Down Expand Up @@ -468,50 +463,34 @@ Initialize(

palError = ERROR_GEN_FAILURE;

if (argc > 0 && argv != nullptr)
/* find out the application's full path */
exe_path = INIT_GetCurrentEXEPath();
if (nullptr == exe_path)
{
/* build the command line */
command_line = INIT_FormatCommandLine(argc, argv);
if (nullptr == command_line)
{
ERROR("Error building command line\n");
palError = ERROR_PALINIT_COMMAND_LINE;
goto CLEANUP1d;
}

/* find out the application's full path */
exe_path = INIT_GetCurrentEXEPath();
if (nullptr == exe_path)
{
ERROR("Unable to find exe path\n");
palError = ERROR_PALINIT_CONVERT_EXE_PATH;
goto CLEANUP1e;
}

palError = InitializeProcessCommandLine(
command_line,
exe_path);

if (NO_ERROR != palError)
{
ERROR("Unable to initialize command line\n");
goto CLEANUP2;
}
ERROR("Unable to find exe path\n");
palError = ERROR_PALINIT_CONVERT_EXE_PATH;
goto CLEANUP1e;
}

// InitializeProcessCommandLine took ownership of this memory.
command_line = nullptr;
palError = InitializeProcessCommandLine(
exe_path);

if (!LOADSetExeName(exe_path))
{
ERROR("Unable to set exe name\n");
palError = ERROR_PALINIT_SET_EXE_NAME;
goto CLEANUP2;
}
if (NO_ERROR != palError)
{
ERROR("Unable to initialize command line\n");
goto CLEANUP2;
}

// LOADSetExeName took ownership of this memory.
exe_path = nullptr;
if (!LOADSetExeName(exe_path))
{
ERROR("Unable to set exe name\n");
palError = ERROR_PALINIT_SET_EXE_NAME;
goto CLEANUP2;
}

// LOADSetExeName took ownership of this memory.
exe_path = nullptr;

if (init_count == 0)
{
//
Expand Down Expand Up @@ -619,8 +598,6 @@ Initialize(
CLEANUP2:
free(exe_path);
CLEANUP1e:
free(command_line);
CLEANUP1d:
// Cleanup synchronization manager
CLEANUP1c:
// Cleanup object manager
Expand Down Expand Up @@ -670,12 +647,11 @@ Initialize(
--*/
PAL_ERROR
PALAPI
PAL_InitializeCoreCLR(const char *szExePath, BOOL runningInExe)
PAL_InitializeCoreCLR(BOOL runningInExe)
{
g_running_in_exe = runningInExe;

// Fake up a command line to call PAL initialization with.
int result = Initialize(1, &szExePath, PAL_INITIALIZE_CORECLR);
int result = Initialize(PAL_INITIALIZE_CORECLR);
if (result != 0)
{
return GetLastError();
Expand Down Expand Up @@ -866,132 +842,6 @@ void PALInitUnlock(void)

/* Internal functions *********************************************************/

/*++
Function:
INIT_FormatCommandLine [Internal]

Abstract:
This function converts an array of arguments (argv) into a Unicode
command-line for use by GetCommandLineW

Parameters :
int argc : number of arguments in argv
char **argv : argument list in an array of NULL-terminated strings

Return value :
pointer to Unicode command line. This is a buffer allocated with malloc;
caller is responsible for freeing it with free()

Note : not all peculiarities of Windows command-line processing are supported;

-what is supported :
-arguments with white-space must be double quoted (we'll just double-quote
all arguments to simplify things)
-some characters must be escaped with \ : particularly, the double-quote,
to avoid confusion with the double-quotes at the start and end of
arguments, and \ itself, to avoid confusion with escape sequences.
-what is not supported:
-under Windows, \\ is interpreted as an escaped \ ONLY if it's followed by
an escaped double-quote \". \\\" is passed to argv as \", but \\a is
passed to argv as \\a... there may be other similar cases
-there may be other characters which must be escaped
--*/
static LPWSTR INIT_FormatCommandLine (int argc, const char * const *argv)
{
LPWSTR retval;
LPSTR command_line=nullptr, command_ptr;
LPCSTR arg_ptr;
INT length, i,j;
BOOL bQuoted = FALSE;

/* list of characters that need no be escaped with \ when building the
command line. currently " and \ */
LPCSTR ESCAPE_CHARS="\"\\";

/* allocate temporary memory for the string. Play it safe :
double the length of each argument (in case they're composed
exclusively of escaped characters), and add 3 (for the double-quotes
and separating space). This is temporary anyway, we return a LPWSTR */
length=0;
for(i=0; i<argc; i++)
{
TRACE("argument %d is %s\n", i, argv[i]);
length+=3;
length+=strlen(argv[i])*2;
}
command_line = reinterpret_cast<LPSTR>(malloc(length != 0 ? length : 1));

if(!command_line)
{
ERROR("couldn't allocate memory for command line!\n");
return nullptr;
}

command_ptr=command_line;
for(i=0; i<argc; i++)
{
/* double-quote at beginning of argument containing at least one space */
for(j = 0; (argv[i][j] != 0) && (!isspace((unsigned char) argv[i][j])); j++);

if (argv[i][j] != 0)
{
*command_ptr++='"';
bQuoted = TRUE;
}
/* process the argument one character at a time */
for(arg_ptr=argv[i]; *arg_ptr; arg_ptr++)
{
/* if character needs to be escaped, prepend a \ to it. */
if( strchr(ESCAPE_CHARS,*arg_ptr))
{
*command_ptr++='\\';
}

/* now we can copy the actual character over. */
*command_ptr++=*arg_ptr;
}
/* double-quote at end of argument; space to separate arguments */
if (bQuoted == TRUE)
{
*command_ptr++='"';
bQuoted = FALSE;
}
*command_ptr++=' ';
}
/* replace the last space with a NULL terminator */
command_ptr--;
*command_ptr='\0';

/* convert to Unicode */
i = MultiByteToWideChar(CP_ACP, 0,command_line, -1, nullptr, 0);
if (i == 0)
{
ASSERT("MultiByteToWideChar failure\n");
free(command_line);
return nullptr;
}

retval = reinterpret_cast<LPWSTR>(malloc((sizeof(WCHAR)*i)));
if(retval == nullptr)
{
ERROR("can't allocate memory for Unicode command line!\n");
free(command_line);
return nullptr;
}

if(!MultiByteToWideChar(CP_ACP, 0,command_line, -1, retval, i))
{
ASSERT("MultiByteToWideChar failure\n");
free(retval);
retval = nullptr;
}
else
TRACE("Command line is %s\n", command_line);

free(command_line);
return retval;
}

/*++
Function:
INIT_GetCurrentEXEPath
Expand Down
Loading
Loading