Skip to content

Commit 445d815

Browse files
committed
Re-implement env functionality in minipal.
1 parent f2418ac commit 445d815

File tree

17 files changed

+2275
-794
lines changed

17 files changed

+2275
-794
lines changed

src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <cstdarg>
4242
#include <signal.h>
4343
#include <minipal/thread.h>
44+
#include <minipal/env.h>
4445

4546
#ifdef TARGET_LINUX
4647
#include <sys/syscall.h>
@@ -981,21 +982,25 @@ UInt32_BOOL PalResetEvent(HANDLE event)
981982

982983
uint32_t PalGetEnvironmentVariable(const char * name, char * buffer, uint32_t size)
983984
{
984-
const char* value = getenv(name);
985-
if (value == NULL)
985+
size_t valueLen = 0;
986+
if (!minipal_env_get_s(&valueLen, buffer, size, name, false))
986987
{
987988
return 0;
988989
}
989990

990-
size_t valueLen = strlen(value);
991+
// minipal_env_get_s returns the length of the value including the null terminator.
992+
if (valueLen > 0)
993+
{
994+
valueLen--;
995+
}
996+
991997
if (valueLen < size)
992998
{
993-
strcpy(buffer, value);
994-
return valueLen;
999+
return (uint32_t)valueLen;
9951000
}
9961001

9971002
// return required size including the null character or 0 if the size doesn't fit into uint32_t
998-
return (valueLen < UINT32_MAX) ? (valueLen + 1) : 0;
1003+
return valueLen < UINT32_MAX ? (uint32_t)(valueLen + 1) : 0;
9991004
}
10001005

10011006
uint16_t PalCaptureStackBackTrace(uint32_t arg1, uint32_t arg2, void* arg3, uint32_t* arg4)

src/coreclr/pal/src/config.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
#define PAL_THREAD_PRIORITY_MIN 0
100100
#define PAL_THREAD_PRIORITY_MAX 0
101101

102-
#cmakedefine01 HAVE__NSGETENVIRON
103102
#cmakedefine01 DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
104103
#cmakedefine PAL_PTRACE(cmd, pid, addr, data) @PAL_PTRACE@
105104
#cmakedefine01 SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING

src/coreclr/pal/src/configure.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,6 @@ if(NOT CLR_CMAKE_HOST_ARCH_ARM AND NOT CLR_CMAKE_HOST_ARCH_ARM64)
890890
endif()
891891

892892
if(CLR_CMAKE_TARGET_APPLE)
893-
set(HAVE__NSGETENVIRON 1)
894893
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 1)
895894
set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))")
896895
set(HAVE_SCHED_OTHER_ASSIGNABLE 1)

src/coreclr/pal/src/debug/debug.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,8 @@ OutputDebugStringA(
160160
lpOutputString ? lpOutputString : "NULL");
161161

162162
// As we don't support debug events, we are going to output the debug string
163-
// to stderr instead of generating OUT_DEBUG_STRING_EVENT. It's safe to tell
164-
// EnvironGetenv not to make a copy of the value here since we only want to
165-
// check whether it exists, not actually use it.
166-
if ((lpOutputString != NULL) &&
167-
(NULL != EnvironGetenv(PAL_OUTPUTDEBUGSTRING, /* copyValue */ FALSE)))
163+
// to stderr instead of generating OUT_DEBUG_STRING_EVENT.
164+
if ((lpOutputString != NULL) && (EnvironCheckenv(PAL_OUTPUTDEBUGSTRING)))
168165
{
169166
fprintf(stderr, "%s", lpOutputString);
170167
}

src/coreclr/pal/src/exception/machmessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using namespace CorUnix;
5050

5151
#ifdef _DEBUG
5252

53-
#define NONPAL_TRACE_ENABLED EnvironGetenv("NONPAL_TRACING", /* copyValue */ false)
53+
#define NONPAL_TRACE_ENABLED EnvironCheckenv("NONPAL_TRACING")
5454

5555
#define NONPAL_ASSERT(_msg, ...) NONPAL_RETAIL_ASSERT(_msg, __VA_ARGS__)
5656

src/coreclr/pal/src/include/pal/environ.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,41 @@ extern "C"
2626
#endif // __cplusplus
2727

2828
/*++
29-
Variables :
3029
31-
palEnvironment: a global variable equivalent to environ on systems on
32-
which that exists, and a pointer to an array of environment
33-
strings on systems without environ.
34-
gcsEnvironment: critical section to synchronize access to palEnvironment
30+
Function:
31+
EnvironInitialize
32+
33+
Initialization function for the PAL environment code.
3534
--*/
36-
extern char **palEnvironment;
37-
extern minipal_mutex gcsEnvironment;
35+
BOOL EnvironInitialize();
3836

3937
/*++
38+
Function:
39+
EnvironGetUnsafe
4040
41+
Get the current environment. This is similar accessing
42+
global environ variable and is not thread safe. This function
43+
should only be called from code that guarantees environment won't
44+
change while using returned pointer.
45+
--*/
46+
char **EnvironGetUnsafe();
47+
48+
/*++
4149
Function:
42-
EnvironInitialize
50+
EnvironCheckenv
4351
44-
Initialization function for the PAL environment code.
52+
Check if environment variable with the given name exists in environment.
4553
--*/
46-
BOOL EnvironInitialize();
54+
BOOL EnvironCheckenv(const char *name);
4755

4856
/*++
4957
Function:
5058
EnvironGetenv
5159
5260
Get the value of environment variable with the given name.
61+
Caller should free the returned string if it is not NULL.
5362
--*/
54-
char *EnvironGetenv(const char *name, BOOL copyValue = TRUE);
63+
char *EnvironGetenv(const char *name);
5564

5665
/*++
5766
Function:

src/coreclr/pal/src/misc/dbgmsg.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -705,24 +705,11 @@ bool DBG_ShouldCheckStackAlignment()
705705

706706
if (caMode == CheckAlignment_Uninitialized)
707707
{
708-
char* checkAlignmentSettings;
709-
bool shouldFreeCheckAlignmentSettings = false;
710-
if (palEnvironment == nullptr)
711-
{
712-
// This function might be called before the PAL environment is initialized.
713-
// In this case, use the system getenv instead.
714-
checkAlignmentSettings = ::getenv(PAL_CHECK_ALIGNMENT_MODE);
715-
}
716-
else
717-
{
718-
checkAlignmentSettings = EnvironGetenv(PAL_CHECK_ALIGNMENT_MODE);
719-
shouldFreeCheckAlignmentSettings = true;
720-
}
721-
708+
char* checkAlignmentSettings = EnvironGetenv(PAL_CHECK_ALIGNMENT_MODE);
722709
caMode = checkAlignmentSettings ?
723710
(CheckAlignmentMode)atoi(checkAlignmentSettings) : CheckAlignment_Default;
724711

725-
if (checkAlignmentSettings && shouldFreeCheckAlignmentSettings)
712+
if (checkAlignmentSettings)
726713
{
727714
free(checkAlignmentSettings);
728715
}

0 commit comments

Comments
 (0)