Skip to content

Commit 5e9ca6a

Browse files
committed
Fix PAL double initialize
1 parent 5db69c2 commit 5e9ca6a

File tree

13 files changed

+302
-529
lines changed

13 files changed

+302
-529
lines changed

bin/ch/ChakraRtInterface.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,8 @@ bool ChakraRTInterface::LoadChakraDll(ArgInfo* argInfo, HINSTANCE *outLibrary)
4949
#ifndef CHAKRA_STATIC_LIBRARY
5050
HINSTANCE library = nullptr;
5151

52-
char filename[_MAX_PATH];
53-
char drive[_MAX_DRIVE];
54-
char dir[_MAX_DIR];
55-
56-
char modulename[_MAX_PATH];
57-
GetModuleFileNameA(NULL, modulename, _MAX_PATH);
58-
_splitpath_s(modulename, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
59-
_makepath_s(filename, drive, dir, chakraDllName, nullptr);
60-
LPCSTR dllName = filename;
52+
char dllName[_MAX_PATH];
53+
GetBinaryPathWithFileNameA(dllName, _MAX_PATH, chakraDllName);
6154

6255
library = LoadChakraCore(dllName);
6356
*outLibrary = library;

bin/ch/Helpers.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
#include <sys/stat.h>
88

9+
#if defined(__APPLE__)
10+
#include <mach-o/dyld.h> // _NSGetExecutablePath
11+
#elif defined(__linux__)
12+
#include <unistd.h> // readlink
13+
#elif !defined(_WIN32)
14+
#error "How to get the executable path for this platform?"
15+
#endif // _WIN32 ?
16+
917
//TODO: x-plat definitions
1018
#ifdef _WIN32
1119
typedef char16 TTDHostCharType;
@@ -725,3 +733,97 @@ void CALLBACK Helpers::TTFlushAndCloseStreamCallback(JsTTDStreamHandle handle, b
725733
fclose((FILE*)handle);
726734
}
727735

736+
#define SET_BINARY_PATH_ERROR_MESSAGE(path, msg) \
737+
str_len = (int) strlen(msg); \
738+
memcpy(path, msg, (size_t)str_len); \
739+
path[str_len] = char(0)
740+
741+
void GetBinaryLocation(char *path, const unsigned size)
742+
{
743+
AssertMsg(path != nullptr, "Path can not be nullptr");
744+
AssertMsg(size < INT_MAX, "Isn't it too big for a path buffer?");
745+
#ifdef _WIN32
746+
LPWSTR wpath = (WCHAR*)malloc(sizeof(WCHAR) * size);
747+
int str_len;
748+
if (!wpath)
749+
{
750+
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed. OutOfMemory!");
751+
return;
752+
}
753+
str_len = GetModuleFileNameW(NULL, wpath, size - 1);
754+
if (str_len <= 0)
755+
{
756+
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed.");
757+
free(wpath);
758+
return;
759+
}
760+
761+
str_len = WideCharToMultiByte(CP_UTF8, 0, wpath, str_len, path, size, NULL, NULL);
762+
free(wpath);
763+
764+
if (str_len <= 0)
765+
{
766+
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName (WideCharToMultiByte) has failed.");
767+
return;
768+
}
769+
770+
if ((unsigned)str_len > size - 1)
771+
{
772+
str_len = (int) size - 1;
773+
}
774+
path[str_len] = char(0);
775+
#elif defined(__APPLE__)
776+
uint32_t path_size = (uint32_t)size;
777+
char *tmp = nullptr;
778+
int str_len;
779+
if (_NSGetExecutablePath(path, &path_size))
780+
{
781+
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: _NSGetExecutablePath has failed.");
782+
return;
783+
}
784+
785+
tmp = (char*)malloc(size);
786+
char *result = realpath(path, tmp);
787+
str_len = strlen(result);
788+
memcpy(path, result, str_len);
789+
free(tmp);
790+
path[str_len] = char(0);
791+
#elif defined(__linux__)
792+
int str_len = readlink("/proc/self/exe", path, size - 1);
793+
if (str_len <= 0)
794+
{
795+
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: /proc/self/exe has failed.");
796+
return;
797+
}
798+
path[str_len] = char(0);
799+
#else
800+
#warning "Implement GetBinaryLocation for this platform"
801+
#endif
802+
}
803+
804+
// xplat-todo: Implement a corresponding solution for GetModuleFileNameW
805+
// and cleanup PAL. [ https://github.com/Microsoft/ChakraCore/pull/2288 should be merged first ]
806+
// GetModuleFileName* PAL is not reliable and forces us to explicitly double initialize PAL
807+
// with argc / argv....
808+
void GetBinaryPathWithFileNameA(char *path, const size_t buffer_size, const char* filename)
809+
{
810+
char fullpath[_MAX_PATH];
811+
char drive[_MAX_DRIVE];
812+
char dir[_MAX_DIR];
813+
814+
char modulename[_MAX_PATH];
815+
GetBinaryLocation(modulename, _MAX_PATH);
816+
_splitpath_s(modulename, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
817+
_makepath_s(fullpath, drive, dir, filename, nullptr);
818+
819+
size_t len = strlen(fullpath);
820+
if (len < buffer_size)
821+
{
822+
memcpy(path, fullpath, len * sizeof(char));
823+
}
824+
else
825+
{
826+
len = 0;
827+
}
828+
path[len] = char(0);
829+
}

bin/ch/WScriptJsrt.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
#define DEST_PLATFORM_TEXT "win32"
2121
#else // ! _WIN32
2222
#if defined(__APPLE__)
23-
#include <mach-o/dyld.h> // _NSGetExecutablePath
2423
#ifdef __IOS__
2524
#define DEST_PLATFORM_TEXT "ios"
2625
#else // ! iOS
2726
#define DEST_PLATFORM_TEXT "darwin"
2827
#endif // iOS ?
2928
#elif defined(__ANDROID__)
30-
#include <unistd.h> // readlink
3129
#define DEST_PLATFORM_TEXT "android"
3230
#elif defined(__linux__)
33-
#include <unistd.h> // readlink
3431
#define DEST_PLATFORM_TEXT "posix"
3532
#elif defined(__FreeBSD__) || defined(__unix__)
3633
#define DEST_PLATFORM_TEXT "bsd"
@@ -740,74 +737,6 @@ bool WScriptJsrt::InstallObjectsOnObject(JsValueRef object, const char* name,
740737
return true;
741738
}
742739

743-
#define SET_BINARY_PATH_ERROR_MESSAGE(path, msg) \
744-
str_len = (int) strlen(msg); \
745-
memcpy(path, msg, (size_t)str_len); \
746-
path[str_len] = char(0)
747-
748-
void GetBinaryLocation(char *path, const unsigned size)
749-
{
750-
AssertMsg(size >= 512 && path != nullptr, "Min path buffer size 512 and path can not be nullptr");
751-
AssertMsg(size < INT_MAX, "Isn't it too big for a path buffer?");
752-
#ifdef _WIN32
753-
LPWSTR wpath = (WCHAR*)malloc(sizeof(WCHAR) * size);
754-
int str_len;
755-
if (!wpath)
756-
{
757-
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed. OutOfMemory!");
758-
return;
759-
}
760-
str_len = GetModuleFileNameW(NULL, wpath, size - 1);
761-
if (str_len <= 0)
762-
{
763-
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed.");
764-
free(wpath);
765-
return;
766-
}
767-
768-
str_len = WideCharToMultiByte(CP_UTF8, 0, wpath, str_len, path, size, NULL, NULL);
769-
free(wpath);
770-
771-
if (str_len <= 0)
772-
{
773-
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName (WideCharToMultiByte) has failed.");
774-
return;
775-
}
776-
777-
if ((unsigned)str_len > size - 1)
778-
{
779-
str_len = (int) size - 1;
780-
}
781-
path[str_len] = char(0);
782-
#elif defined(__APPLE__)
783-
uint32_t path_size = (uint32_t)size;
784-
char *tmp = nullptr;
785-
int str_len;
786-
if (_NSGetExecutablePath(path, &path_size))
787-
{
788-
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: _NSGetExecutablePath has failed.");
789-
return;
790-
}
791-
792-
tmp = (char*)malloc(size);
793-
char *result = realpath(path, tmp);
794-
str_len = strlen(result);
795-
memcpy(path, result, str_len);
796-
free(tmp);
797-
path[str_len] = char(0);
798-
#elif defined(__linux__)
799-
int str_len = readlink("/proc/self/exe", path, size - 1);
800-
if (str_len <= 0)
801-
{
802-
SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: /proc/self/exe has failed.");
803-
return;
804-
}
805-
path[str_len] = char(0);
806-
#else
807-
#warning "Implement GetBinaryLocation for this platform"
808-
#endif
809-
}
810-
811740
bool WScriptJsrt::Initialize()
812741
{
813742
HRESULT hr = S_OK;

bin/ch/ch.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,10 @@ unsigned int WINAPI StaticThreadProc(void *lpParam)
737737
static char16** argv = nullptr;
738738
int main(int argc, char** c_argv)
739739
{
740-
PAL_InitializeChakraCore(argc, c_argv);
740+
#ifndef CHAKRA_STATIC_LIBRARY
741+
// xplat-todo: PAL free CH ?
742+
PAL_InitializeChakraCore();
743+
#endif
741744
argv = new char16*[argc];
742745
for (int i = 0; i < argc; i++)
743746
{

bin/ch/stdafx.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,6 @@ inline JsErrorCode CreatePropertyIdFromString(const char* str, JsPropertyIdRef *
257257
{
258258
return ChakraRTInterface::JsCreatePropertyId(str, strlen(str), Id);
259259
}
260+
261+
void GetBinaryLocation(char *path, const unsigned size);
262+
void GetBinaryPathWithFileNameA(char *path, const size_t buffer_size, const char* filename);

lib/Common/Core/SysInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ AutoSystemInfo::Initialize()
6565
{
6666
Assert(!initialized);
6767
#ifndef _WIN32
68-
PAL_InitializeDLL();
68+
PAL_InitializeChakraCore();
6969
majorVersion = CHAKRA_CORE_MAJOR_VERSION;
7070
minorVersion = CHAKRA_CORE_MINOR_VERSION;
7171
#endif

lib/Common/Memory/RecyclerWriteBarrierManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ X64WriteBarrierCardTableManager::OnThreadInit()
5757
ULONG_PTR stackEnd = 0;
5858
::GetCurrentThreadStackLimits(&stackEnd, &stackBase);
5959
#endif
60-
60+
6161
size_t numPages = (stackBase - stackEnd) / AutoSystemInfo::PageSize;
6262
// stackEnd is the lower boundary
6363
return OnSegmentAlloc((char*) stackEnd, numPages);

lib/Jsrt/JsrtHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void JsrtCallbackState::ObjectBeforeCallectCallbackWrapper(JsObjectBeforeCollect
120120
#endif
121121

122122
#ifndef _WIN32
123-
PAL_InitializeChakraCore(0, NULL);
123+
PAL_InitializeChakraCore();
124124
#endif
125125

126126
HMODULE mod = GetModuleHandleW(NULL);

pal/inc/pal.h

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -390,44 +390,14 @@ typedef long time_t;
390390
#define DLL_THREAD_DETACH 3
391391
#define DLL_PROCESS_DETACH 0
392392

393-
#define PAL_INITIALIZE_NONE 0x00
394-
#define PAL_INITIALIZE_SYNC_THREAD 0x01
395-
#define PAL_INITIALIZE_EXEC_ALLOCATOR 0x02
396-
#define PAL_INITIALIZE_REGISTER_SIGTERM_HANDLER 0x08
397-
#define PAL_INITIALIZE_DEBUGGER_EXCEPTIONS 0x10
398-
399-
// PAL_Initialize() flags
400-
#define PAL_INITIALIZE PAL_INITIALIZE_SYNC_THREAD
401-
402-
// PAL_InitializeDLL() flags - don't start any of the helper threads
403-
#define PAL_INITIALIZE_DLL PAL_INITIALIZE_NONE
404-
405-
// PAL_InitializeChakraCore() flags
406-
#define PAL_INITIALIZE_CHAKRACORE (PAL_INITIALIZE | PAL_INITIALIZE_EXEC_ALLOCATOR)
407-
408393
typedef DWORD (PALAPI *PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);
409394
typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
410395

411396
/******************* PAL-Specific Entrypoints *****************************/
412397

413-
PALIMPORT
414-
int
415-
PALAPI
416-
PAL_Initialize(
417-
int argc,
418-
const char * const argv[]);
419-
420-
PALIMPORT
421398
int
422399
PALAPI
423-
PAL_InitializeDLL();
424-
425-
PALIMPORT
426-
DWORD
427-
PALAPI
428-
PAL_InitializeChakraCore(
429-
int argc,
430-
char** argv);
400+
PAL_InitializeChakraCore();
431401

432402
PALIMPORT
433403
DWORD_PTR
@@ -464,30 +434,6 @@ PALAPI
464434
PAL_TerminateEx(
465435
int exitCode);
466436

467-
/*++
468-
Function:
469-
PAL_SetShutdownCallback
470-
471-
Abstract:
472-
Sets a callback that is executed when the PAL is shut down because of
473-
ExitProcess, TerminateProcess or PAL_Shutdown but not PAL_Terminate/Ex.
474-
475-
NOTE: Currently only one callback can be set at a time.
476-
--*/
477-
typedef VOID (*PSHUTDOWN_CALLBACK)(void);
478-
479-
PALIMPORT
480-
VOID
481-
PALAPI
482-
PAL_SetShutdownCallback(
483-
IN PSHUTDOWN_CALLBACK callback);
484-
485-
PALIMPORT
486-
void
487-
PALAPI
488-
PAL_InitializeDebug(
489-
void);
490-
491437
PALIMPORT
492438
VOID
493439
PALAPI
@@ -522,15 +468,6 @@ PAL_Random(
522468
IN OUT LPVOID lpBuffer,
523469
IN DWORD dwLength);
524470

525-
// This helper will be used *only* by the CoreCLR to determine
526-
// if an address lies inside CoreCLR or not.
527-
//
528-
// This shouldnt be used by any other component that links into the PAL.
529-
PALIMPORT
530-
BOOL
531-
PALAPI
532-
PAL_IsIPInCoreCLR(IN PVOID address);
533-
534471
#ifdef PLATFORM_UNIX
535472

536473
PALIMPORT

0 commit comments

Comments
 (0)