Skip to content
Draft
17 changes: 11 additions & 6 deletions src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <cstdarg>
#include <signal.h>
#include <minipal/thread.h>
#include <minipal/env.h>

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

uint32_t PalGetEnvironmentVariable(const char * name, char * buffer, uint32_t size)
{
const char* value = getenv(name);
if (value == NULL)
size_t valueLen = 0;
if (!minipal_env_get_s(&valueLen, buffer, size, name))
{
return 0;
}

size_t valueLen = strlen(value);
// minipal_env_get_s returns the length of the value including the null terminator.
if (valueLen > 0)
{
valueLen--;
}

if (valueLen < size)
{
strcpy(buffer, value);
return valueLen;
return (uint32_t)valueLen;
}

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

uint16_t PalCaptureStackBackTrace(uint32_t arg1, uint32_t arg2, void* arg3, uint32_t* arg4)
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/pal/src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
#define PAL_THREAD_PRIORITY_MIN 0
#define PAL_THREAD_PRIORITY_MAX 0

#cmakedefine01 HAVE__NSGETENVIRON
#cmakedefine01 DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX
#cmakedefine PAL_PTRACE(cmd, pid, addr, data) @PAL_PTRACE@
#cmakedefine01 SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/pal/src/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,6 @@ if(NOT CLR_CMAKE_HOST_ARCH_ARM AND NOT CLR_CMAKE_HOST_ARCH_ARM64)
endif()

if(CLR_CMAKE_TARGET_APPLE)
set(HAVE__NSGETENVIRON 1)
set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 1)
set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))")
set(HAVE_SCHED_OTHER_ASSIGNABLE 1)
Expand Down
43 changes: 9 additions & 34 deletions src/coreclr/pal/src/misc/environ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ Revision History:
#include "pal/dbgmsg.h"
#include "pal/environ.h"

#if HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif
#include "minipal/env.h"
#include "minipal/mutex.h"

#include <stdlib.h>

Expand Down Expand Up @@ -895,35 +894,6 @@ char* EnvironGetenv(const char* name, BOOL copyValue)
return retValue;
}


/*++
Function:
EnvironGetSystemEnvironment

Get a pointer to the array of pointers representing the process's
environment.

See 'man environ' for details.

Return Value

A pointer to the environment.

--*/
char** EnvironGetSystemEnvironment()
{
char** sysEnviron;

#if HAVE__NSGETENVIRON
sysEnviron = *(_NSGetEnviron());
#else // HAVE__NSGETENVIRON
extern char **environ;
sysEnviron = environ;
#endif // HAVE__NSGETENVIRON

return sysEnviron;
}

/*++
Function:
EnvironInitialize
Expand All @@ -943,7 +913,8 @@ EnvironInitialize(void)
CPalThread * pthrCurrent = InternalGetCurrentThread();
minipal_mutex_enter(&gcsEnvironment);

char** sourceEnviron = EnvironGetSystemEnvironment();
char** sourceEnviron = minipal_env_get_environ_copy();
_ASSERT(sourceEnviron != NULL);

int variableCount = 0;
while (sourceEnviron[variableCount] != nullptr)
Expand All @@ -965,14 +936,18 @@ EnvironInitialize(void)
_ASSERTE(palEnvironment != nullptr);
for (int i = 0; i < variableCount; ++i)
{
palEnvironment[i] = strdup(sourceEnviron[i]);
// Transfer ownership of the string to palEnvironment.
palEnvironment[i] = sourceEnviron[i];
sourceEnviron[i] = nullptr;
palEnvironmentCount++;
}

// Set the entry after the last variable to null to indicate the end.
palEnvironment[variableCount] = nullptr;
}

minipal_env_free_environ(sourceEnviron);

minipal_mutex_leave(&gcsEnvironment);
return ret;
}
Expand Down
11 changes: 11 additions & 0 deletions src/coreclr/utilcode/stresslog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,25 @@ void StressLog::Initialize(unsigned facilities, unsigned level, unsigned maxByte
// in this case, interpret the number as GB
maxBytesPerThread *= (1024 * 1024 * 1024);
}

theLog.MaxSizePerThread = (unsigned)min(maxBytesPerThread,(size_t)0xffffffff);

size_t maxBytesTotal = maxBytesTotalArg;

#ifdef MEMORY_MAPPED_STRESSLOG
if (logFilename != nullptr && logFilename[0] != '\0')
{
// we need at least sizeof(StressLogHeader) bytes for memory mapped stress log
maxBytesTotal = sizeof(StressLogHeader) + maxBytesTotal;
}
#endif

if (maxBytesTotal < STRESSLOG_CHUNK_SIZE * 256)
{
// in this case, interpret the number as GB
maxBytesTotal *= (1024 * 1024 * 1024);
}

theLog.MaxSizeTotal = (unsigned)min(maxBytesTotal, (size_t)0xffffffff);
theLog.totalChunk = 0;
theLog.facilitiesToLog = facilities | LF_ALWAYS;
Expand Down
7 changes: 6 additions & 1 deletion src/native/libs/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ if (CLR_CMAKE_TARGET_APPLE)
else()
list (APPEND NATIVE_SOURCES
pal_autoreleasepool.c
pal_environment.c
pal_searchpath.c
pal_log.c
pal_iossupportversion.c)

if (CLR_CMAKE_TARGET_ANDROID)
list (APPEND NATIVE_SOURCES pal_environment_android.c)
else()
list (APPEND NATIVE_SOURCES pal_environment.c)
endif()

if (CLR_CMAKE_TARGET_WASI)
list (APPEND NATIVE_SOURCES pal_console_wasi.c)
else()
Expand Down
16 changes: 3 additions & 13 deletions src/native/libs/System.Native/pal_environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@

#include "pal_config.h"
#include "pal_environment.h"

#include <stdlib.h>
#include <string.h>
#if HAVE_NSGETENVIRON
#include <crt_externs.h>
#endif
#include <minipal/env.h>

char* SystemNative_GetEnv(const char* variable)
{
return getenv(variable);
return minipal_env_get(variable);
}

char** SystemNative_GetEnviron(void)
{
#if HAVE_NSGETENVIRON
return *(_NSGetEnviron());
#else
extern char **environ;
return environ;
#endif
return minipal_env_get_environ();
}

void SystemNative_FreeEnviron(char** environ)
Expand Down
22 changes: 22 additions & 0 deletions src/native/libs/System.Native/pal_environment_android.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "pal_config.h"
#include "pal_environment.h"

#include <minipal/env.h>

char* SystemNative_GetEnv(const char* variable)
{
return minipal_env_get(variable);
}

char** SystemNative_GetEnviron(void)
{
return minipal_env_get_environ_copy();
}

void SystemNative_FreeEnviron(char** environ)
{
minipal_env_free_environ(environ);
}
1 change: 1 addition & 0 deletions src/native/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(SOURCES
utf8.c
xoshiro128pp.c
log.c
env.c
)

# Provide an object library for scenarios where we ship static libraries
Expand Down
45 changes: 45 additions & 0 deletions src/native/minipal/atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef HAVE_MINIPAL_ATOMIC_H
#define HAVE_MINIPAL_ATOMIC_H

#ifdef HOST_WINDOWS
#include <windows.h>
#else
#include <stdatomic.h>
#if ATOMIC_POINTER_LOCK_FREE != 2
#pragma message("C11 atomic pointer types are not lock free on targeted platform")
#endif
#endif

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

/**
* @brief Atomic compares and exchange a pointer value.
*
* @param dest Pointer to the destination pointer to compare and swap.
* @param exch The value to store if the comparison succeeds.
* @param comp The value to compare against.
*
* @return The previous value of the pointer.
*/
static inline void* minipal_atomic_compare_exchange_ptr(volatile void** dest, void* exch, void* comp)
{
#ifdef HOST_WINDOWS
return InterlockedCompareExchangePointer((PVOID volatile *)dest, (PVOID)exch, (PVOID)comp);
#else
atomic_compare_exchange_strong((volatile _Atomic(void *) *)dest, &comp, exch);
atomic_thread_fence(memory_order_seq_cst);
return comp;
#endif
}

#ifdef __cplusplus
}
#endif // __cplusplus

#endif /* HAVE_MINIPAL_ATOMIC_H */
40 changes: 34 additions & 6 deletions src/native/minipal/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,43 @@ include(CheckSymbolExists)
check_include_files("windows.h;bcrypt.h" HAVE_BCRYPT_H)
check_include_files("sys/auxv.h;asm/hwcap.h" HAVE_AUXV_HWCAP_H)
check_include_files("asm/hwprobe.h" HAVE_HWPROBE_H)
check_include_files("crt_externs.h" HAVE_CRT_EXTERNS_H)

check_function_exists(sysctlbyname HAVE_SYSCTLBYNAME)
check_function_exists(fsync HAVE_FSYNC)
check_symbol_exists(sysctlbyname "sys/sysctl.h" HAVE_SYSCTLBYNAME)
check_symbol_exists(fsync "unistd.h" HAVE_FSYNC)

check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF)
check_symbol_exists(O_CLOEXEC fcntl.h HAVE_O_CLOEXEC)
check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC)
check_symbol_exists(CLOCK_MONOTONIC_COARSE time.h HAVE_CLOCK_MONOTONIC_COARSE)
check_symbol_exists(clock_gettime_nsec_np time.h HAVE_CLOCK_GETTIME_NSEC_NP)
check_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
check_symbol_exists(CLOCK_MONOTONIC_COARSE "time.h" HAVE_CLOCK_MONOTONIC_COARSE)
check_symbol_exists(clock_gettime_nsec_np "time.h" HAVE_CLOCK_GETTIME_NSEC_NP)

check_symbol_exists(getenv "stdlib.h" HAVE_GETENV)
check_symbol_exists(strcpy_s "string.h" HAVE_STRCPY_S)
check_symbol_exists(strncpy_s "string.h" HAVE_STRNCPY_S)
check_symbol_exists(strcat_s "string.h" HAVE_STRCAT_S)

if (HAVE_CRT_EXTERNS_H)
check_c_source_compiles(
"
#include <crt_externs.h>
int main(void) { char** e = *(_NSGetEnviron()); return 0; }
"
HAVE__NSGETENVIRON)
endif()

check_c_source_compiles(
"
#include <stdlib.h>
int main(void) { char** e = _environ; return 0; }
"
HAVE__ENVIRON)

check_c_source_compiles(
"
int main(void) { extern char **environ; char** e = environ; return 0; }
"
HAVE_ENVIRON)

if(CMAKE_C_BYTE_ORDER STREQUAL "BIG_ENDIAN")
set(BIGENDIAN 1)
Expand Down
Loading
Loading