Skip to content

Commit

Permalink
Address some linker issues when consuming from the PAL.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT committed Sep 23, 2021
1 parent 6994a17 commit 4aab957
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 48 deletions.
84 changes: 47 additions & 37 deletions src/coreclr/inc/clrconfignocache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// Logic for resolving configuration names.
//

#ifndef _CLRCONFIGNOCACHE_H_
#define _CLRCONFIGNOCACHE_H_

#ifndef StrLen
#define StrLen(STR) ((sizeof(STR) / sizeof(STR[0])) - 1)
#endif // !StrLen
Expand Down Expand Up @@ -48,50 +51,57 @@ class CLRConfigNoCache
return fSuccess;
}

static CLRConfigNoCache Get(LPCSTR cfg, bool noPrefix = false)
{
if (cfg == nullptr)
return {};
friend CLRConfigNoCache GetCLRConfigNoCache(LPCSTR cfg, bool noPrefix);
};

char nameBuffer[64];
const char* fallbackPrefix = NULL;
const size_t namelen = strlen(cfg);
// This function is not a member of CLRConfigNoCache and marked inline so linkage
// is relative to the compilation unit it is included in. The linkage concerns here
// are due to usage in the PAL regarding getenv.
inline CLRConfigNoCache GetCLRConfigNoCache(LPCSTR cfg, bool noPrefix = false)
{
if (cfg == nullptr)
return {};

if (noPrefix)
{
if (namelen >= _countof(nameBuffer))
{
_ASSERTE(!"Environment variable name too long.");
return {};
}
char nameBuffer[64];
const char* fallbackPrefix = NULL;
const size_t namelen = strlen(cfg);

*nameBuffer = W('\0');
}
else
if (noPrefix)
{
if (namelen >= _countof(nameBuffer))
{
bool dotnetValid = namelen < (size_t)(_countof(nameBuffer) - 1 - LEN_OF_DOTNET_PREFIX);
bool complusValid = namelen < (size_t)(_countof(nameBuffer) - 1 - LEN_OF_COMPLUS_PREFIX);
if (!dotnetValid || !complusValid)
{
_ASSERTE(!"Environment variable name too long.");
return {};
}

// Priority order is DOTNET_ and then COMPlus_.
strcpy_s(nameBuffer, _countof(nameBuffer), DOTNET_PREFIX_A);
fallbackPrefix = COMPLUS_PREFIX_A;
_ASSERTE(!"Environment variable name too long.");
return {};
}

strcat_s(nameBuffer, _countof(nameBuffer), cfg);

LPCSTR val = getenv(nameBuffer);
if (val == NULL && fallbackPrefix != NULL)
*nameBuffer = W('\0');
}
else
{
bool dotnetValid = namelen < (size_t)(_countof(nameBuffer) - 1 - LEN_OF_DOTNET_PREFIX);
bool complusValid = namelen < (size_t)(_countof(nameBuffer) - 1 - LEN_OF_COMPLUS_PREFIX);
if (!dotnetValid || !complusValid)
{
strcpy_s(nameBuffer, _countof(nameBuffer), fallbackPrefix);
strcat_s(nameBuffer, _countof(nameBuffer), cfg);
val = getenv(nameBuffer);
_ASSERTE(!"Environment variable name too long.");
return {};
}

return { val };
// Priority order is DOTNET_ and then COMPlus_.
strcpy_s(nameBuffer, _countof(nameBuffer), DOTNET_PREFIX_A);
fallbackPrefix = COMPLUS_PREFIX_A;
}
};

strcat_s(nameBuffer, _countof(nameBuffer), cfg);

LPCSTR val = getenv(nameBuffer);
if (val == NULL && fallbackPrefix != NULL)
{
strcpy_s(nameBuffer, _countof(nameBuffer), fallbackPrefix);
strcat_s(nameBuffer, _countof(nameBuffer), cfg);
val = getenv(nameBuffer);
}

return { val };
}

#endif // _CLRCONFIGNOCACHE_H_
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/exception/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ BOOL SEHInitializeSignals(CorUnix::CPalThread *pthrCurrent, DWORD flags)
#if !HAVE_MACH_EXCEPTIONS
g_enable_alternate_stack_check = false;

CLRConfigNoCache stackCheck = CLRConfigNoCache::Get("EnableAlternateStackCheck");
CLRConfigNoCache stackCheck = GetCLRConfigNoCache("EnableAlternateStackCheck");
if (stackCheck.IsSet())
{
DWORD value;
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ EnsureStackSize(SIZE_T stackSize)
void
InitializeDefaultStackSize()
{
CLRConfigNoCache defStackSize = CLRConfigNoCache::Get("DefaultStackSize");
CLRConfigNoCache defStackSize = GetCLRConfigNoCache("DefaultStackSize");
if (defStackSize.IsSet())
{
DWORD size;
Expand Down Expand Up @@ -403,7 +403,7 @@ Initialize(
#endif // ENSURE_PRIMARY_STACK_SIZE

#ifdef FEATURE_ENABLE_NO_ADDRESS_SPACE_RANDOMIZATION
CLRConfigNoCache useDefaultBaseAddr = CLRConfigNoCache::Get("UseDefaultBaseAddr");
CLRConfigNoCache useDefaultBaseAddr = GetCLRConfigNoCache("UseDefaultBaseAddr");
if (useDefaultBaseAddr.IsSet())
{
DWORD flag;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/misc/tracepointprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ PAL_InitializeTracing(void)
// Check if loading the LTTng providers should be disabled.
// Note: this env var is formally declared in clrconfigvalues.h, but
// this code is executed too early to use the mechanics that come with that definition.
CLRConfigNoCache cfgLTTng = CLRConfigNoCache::Get("LTTng");
CLRConfigNoCache cfgLTTng = GetCLRConfigNoCache("LTTng");
if (cfgLTTng.IsSet())
{
DWORD value;
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/pal/src/thread/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3276,16 +3276,16 @@ Return
BOOL
PROCAbortInitialize()
{
CLRConfigNoCache enabledCfg= CLRConfigNoCache::Get("DbgEnableMiniDump");
CLRConfigNoCache enabledCfg= GetCLRConfigNoCache("DbgEnableMiniDump");

DWORD enabled = 0;
if (enabledCfg.IsSet()
&& enabledCfg.TryAsInteger(10, enabled)
&& enabled)
{
CLRConfigNoCache dmpNameCfg = CLRConfigNoCache::Get("DbgMiniDumpName");
CLRConfigNoCache dmpNameCfg = GetCLRConfigNoCache("DbgMiniDumpName");

CLRConfigNoCache dmpTypeCfg = CLRConfigNoCache::Get("DbgMiniDumpType");
CLRConfigNoCache dmpTypeCfg = GetCLRConfigNoCache("DbgMiniDumpType");
DWORD dumpType = UndefinedDumpType;
if (dmpTypeCfg.IsSet())
{
Expand All @@ -3296,11 +3296,11 @@ PROCAbortInitialize()
}
}

CLRConfigNoCache createDumpCfg = CLRConfigNoCache::Get("CreateDumpDiagnostics");
CLRConfigNoCache createDumpCfg = GetCLRConfigNoCache("CreateDumpDiagnostics");
DWORD val = 0;
BOOL diag = createDumpCfg.IsSet() && createDumpCfg.TryAsInteger(10, val) && val == 1;

CLRConfigNoCache enabldReportCfg = CLRConfigNoCache::Get("EnableCrashReport");
CLRConfigNoCache enabldReportCfg = GetCLRConfigNoCache("EnableCrashReport");
val = 0;
BOOL crashReport = enabldReportCfg.IsSet() && enabldReportCfg.TryAsInteger(10, val) && val == 1;

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/utilcode/clrconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ namespace
SString valueAsUTF8;
temp.ConvertToUTF8(valueAsUTF8);

CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8NoConvert(), noPrefix);
CLRConfigNoCache nonCache = GetCLRConfigNoCache(nameAsUTF8.GetUTF8NoConvert(), noPrefix);
LPCSTR valueNoCache = nonCache.AsString();

_ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8NoConvert()) == 0);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/perfmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void PerfMap::Initialize()
const char* jitdumpPath;
char jitdumpPathBuffer[4096];

CLRConfigNoCache value = CLRConfigNoCache::Get("PerfMapJitDumpPath");
CLRConfigNoCache value = GetCLRConfigNoCache("PerfMapJitDumpPath");
if (value.IsSet())
{
jitdumpPath = value.AsString();
Expand Down

0 comments on commit 4aab957

Please sign in to comment.