Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Add GetLoadLibrary function for PAL & use in TrackErrorCode #15831

Merged
merged 21 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 2 additions & 2 deletions src/dlls/mscorrc/mscorrc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,8 @@ BEGIN
IDS_EE_NDIRECT_BADNATL_CALLCONV "Invalid PInvoke or UnmanagedFunctionPointer calling convention."
IDS_EE_NDIRECT_BADNATL_VARARGS_CALLCONV "Invalid PInvoke calling convention. Vararg functions must use the cdecl calling convention."
IDS_EE_NDIRECT_BADNATL_THISCALL "Invalid PInvoke calling convention. Thiscall requires that the first parameter is present and can be enregistered."
IDS_EE_NDIRECT_LOADLIB "Unable to load DLL '%1': %2"
IDS_EE_NDIRECT_GETPROCADDRESS "Unable to find an entry point named '%2' in DLL '%1'."
IDS_EE_NDIRECT_LOADLIB "Unable to load shared library '%1': %2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is pretty unusual to use "shared library" name on Windows. If we want to use "shared library" for Unix, Windows version of the error message should keep "DLL".

IDS_EE_NDIRECT_GETPROCADDRESS "Unable to find an entry point named '%2' in shared library '%1'."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need a windows form of this message too -- that says DLL?

IDS_EE_NDIRECT_GETPROCADDRESS_NONAME "A library name must be specified in a DllImport attribute applied to non-IJW methods."
IDS_EE_CLASS_CONSTRAINTS_VIOLATION "GenericArguments[%1], '%2', on '%3' violates the constraint of type parameter '%4'."
IDS_EE_METHOD_CONSTRAINTS_VIOLATION "Method %1.%2: type argument '%3' violates the constraint of type parameter '%4'."
Expand Down
5 changes: 5 additions & 0 deletions src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,11 @@ PALAPI
LPCVOID
PAL_GetSymbolModuleBase(void *symbol);

PALIMPORT
LPCSTR
PALAPI
PAL_GetLoadLibraryError();

PALIMPORT
LPVOID
PALAPI
Expand Down
38 changes: 32 additions & 6 deletions src/pal/src/loader/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ GetProcAddress(
}
else
{
TRACE("Symbol %s not found in module %p (named %S), dlerror message is \"%s\"\n",
lpProcName, module, MODNAME(module), dlerror());
TRACE("Symbol %s not found in module %p (named %S)\n",
lpProcName, module, MODNAME(module));
SetLastError(ERROR_PROC_NOT_FOUND);
}
done:
Expand Down Expand Up @@ -840,6 +840,33 @@ PAL_GetSymbolModuleBase(void *symbol)
return retval;
}

/*++
PAL_GetLoadLibraryError

Wrapper for dlerror() to be used by PAL functions

Return value:

A LPCSTR containing the output of dlerror()

--*/
PALIMPORT
LPCSTR
PALAPI
PAL_GetLoadLibraryError()
{

PERF_ENTRY(PAL_GetLoadLibraryError);
ENTRY("PAL_GetLoadLibraryError");

LPCSTR last_error = dlerror();

LOGEXIT("PAL_GetLoadLibraryError returns %p\n", last_error);
PERF_EXIT(PAL_GetLoadLibraryError);
return last_error;
}


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

/*++
Expand Down Expand Up @@ -870,7 +897,7 @@ BOOL LOADInitializeModules()
exe_module.dl_handle = dlopen(nullptr, RTLD_LAZY);
if (exe_module.dl_handle == nullptr)
{
ERROR("Executable module will be broken : dlopen(nullptr) failed dlerror message is \"%s\" \n", dlerror());
ERROR("Executable module will be broken : dlopen(nullptr) failed\n");
return FALSE;
}
exe_module.lib_name = nullptr;
Expand Down Expand Up @@ -1107,7 +1134,7 @@ static BOOL LOADFreeLibrary(MODSTRUCT *module, BOOL fCallDllMain)
if (module->dl_handle && 0 != dlclose(module->dl_handle))
{
/* report dlclose() failure, but proceed anyway. */
WARN("dlclose() call failed! error message is \"%s\"\n", dlerror());
WARN("dlclose() call failed!\n");
}

/* release all memory */
Expand Down Expand Up @@ -1376,7 +1403,6 @@ static void *LOADLoadLibraryDirect(LPCSTR libraryNameOrPath)
void *dl_handle = dlopen(libraryNameOrPath, RTLD_LAZY);
if (dl_handle == nullptr)
{
WARN("dlopen() failed; dlerror says '%s'\n", dlerror());
SetLastError(ERROR_MOD_NOT_FOUND);
}
else
Expand Down Expand Up @@ -1696,7 +1722,7 @@ MODSTRUCT *LOADGetPalLibrary()
Dl_info info;
if (dladdr((PVOID)&LOADGetPalLibrary, &info) == 0)
{
ERROR("LOADGetPalLibrary: dladdr() failed. dlerror message is \"%s\"\n", dlerror());
ERROR("LOADGetPalLibrary: dladdr() failed.\n");
goto exit;
}
// Stash a copy of the CoreCLR installation path in a global variable.
Expand Down
2 changes: 1 addition & 1 deletion src/pal/src/loader/modulename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const char *PAL_dladdr(LPVOID ProcAddress)
Dl_info dl_info;
if (!dladdr(ProcAddress, &dl_info))
{
WARN("dladdr() call failed! dlerror says '%s'\n", dlerror());
WARN("dladdr() call failed!\n");
/* If we get an error, return NULL */
return (NULL);
}
Expand Down
35 changes: 30 additions & 5 deletions src/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5721,12 +5721,19 @@ class LoadLibErrorTracker
m_priorityOfLastError = 0;
}

VOID TrackErrorCode(DWORD dwLastError)
VOID TrackErrorCode()
{
LIMITED_METHOD_CONTRACT;

DWORD priority;

#ifdef FEATURE_PAL

SetMessage(PAL_GetLoadLibraryError());
#else

DWORD dwLastError = GetLastError();

switch (dwLastError)
{
case ERROR_FILE_NOT_FOUND:
Expand All @@ -5747,8 +5754,8 @@ class LoadLibErrorTracker
priority = const_priorityCouldNotLoad;
break;
}

UpdateHR(priority, HRESULT_FROM_WIN32(dwLastError));
#endif
}

// Sets the error code to HRESULT as could not load DLL
Expand All @@ -5762,10 +5769,18 @@ class LoadLibErrorTracker
return m_hr;
}

SString& GetMessage()
{
return m_message;
}

void DECLSPEC_NORETURN Throw(SString &libraryNameOrPath)
{
STANDARD_VM_CONTRACT;

#ifdef FEATURE_PAL
COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB, libraryNameOrPath.GetUnicode(), GetMessage());
#else
HRESULT theHRESULT = GetHR();
if (theHRESULT == HRESULT_FROM_WIN32(ERROR_BAD_EXE_FORMAT))
{
Expand All @@ -5777,6 +5792,7 @@ class LoadLibErrorTracker
GetHRMsg(theHRESULT, hrString);
COMPlusThrow(kDllNotFoundException, IDS_EE_NDIRECT_LOADLIB, libraryNameOrPath.GetUnicode(), hrString);
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, not worth resetting for, but normally after #else / #elif / #endif you add an inline comment noting what #if or #endif they match with. Eg., line 5785 would end with // APPLE and 5797 with // FEATURE_PAL -- if I understand the convention correctly


__UNREACHABLE();
}
Expand All @@ -5791,8 +5807,17 @@ class LoadLibErrorTracker
}
}

void SetMessage(LPCSTR message)
{
SString append_string = SString(SString::Utf8, message);
SString newline = SString(SString::Utf8, "\n");
m_message.Append(newline);
m_message.Append(append_string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just going to jam the strings together isn't it?, you probably want to include \n's.

}

HRESULT m_hr;
DWORD m_priorityOfLastError;
SString m_message = SString(SString::Utf8, "");
}; // class LoadLibErrorTracker

// Local helper function for the LoadLibraryModule function below
Expand All @@ -5819,7 +5844,7 @@ static HMODULE LocalLoadLibraryHelper( LPCWSTR name, DWORD flags, LoadLibErrorTr
DWORD dwLastError = GetLastError();
if (dwLastError != ERROR_INVALID_PARAMETER)
{
pErrorTracker->TrackErrorCode(dwLastError);
pErrorTracker->TrackErrorCode();
return hmod;
}
}
Expand All @@ -5832,7 +5857,7 @@ static HMODULE LocalLoadLibraryHelper( LPCWSTR name, DWORD flags, LoadLibErrorTr

if (hmod == NULL)
{
pErrorTracker->TrackErrorCode(GetLastError());
pErrorTracker->TrackErrorCode();
}

return hmod;
Expand All @@ -5852,7 +5877,7 @@ static HMODULE LocalLoadLibraryDirectHelper(LPCWSTR name, DWORD flags, LoadLibEr

if (hmod == NULL)
{
pErrorTracker->TrackErrorCode(GetLastError());
pErrorTracker->TrackErrorCode();
}

return hmod;
Expand Down