From d663cd8b7310c6b92b020f91c68c1d4c664bf580 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 16 Jun 2023 09:14:33 -0700 Subject: [PATCH 1/6] Process Environment support --- .../nativeaot/Runtime/eventpipe/ds-rt-aot.cpp | 21 ++++++++++++++++++ .../nativeaot/Runtime/eventpipe/ds-rt-aot.h | 5 ++--- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 22 +++++++++++++++++++ .../nativeaot/Runtime/eventpipe/ep-rt-aot.h | 6 ++--- .../processenvironment.csproj | 1 + 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp index 25272643d18a3..109bdac40cee6 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -1,6 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#ifdef TARGET_WINDOWS +#include +#else +#include +#endif + #include #ifdef __APPLE__ @@ -227,4 +233,19 @@ ds_rt_aot_transport_get_default_name ( return true; #endif } + +uint32_t +ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value) +{ +#ifdef TARGET_UNIX + ep_char8_t *nameNarrow = ep_rt_utf16le_to_utf8_string (name, ep_rt_utf16_string_len (name)); + ep_char8_t *valueNarrow = ep_rt_utf16le_to_utf8_string (value, ep_rt_utf16_string_len (value)); + int32_t ret_value = setenv(nameNarrow, valueNarrow, 1); + delete nameNarrow; + delete valueNarrow; + return ret_value; +#else + return SetEnvironmentVariableW(reinterpret_cast(name), reinterpret_cast(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError()); +#endif +} #endif /* ENABLE_PERFTRACING */ diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h index bd982b81da7ee..d4d18f5941c4f 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h @@ -266,9 +266,8 @@ static uint32_t ds_rt_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value) { - // return SetEnvironmentVariableW(reinterpret_cast(name), reinterpret_cast(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError()); - // PalDebugBreak(); - return 0xffff; + extern uint32_t ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t *value); + return ds_rt_aot_set_environment_variable(name, value); } static diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index f7793b4065f53..74a061727ea42 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -669,6 +669,28 @@ bool ep_rt_aot_spin_lock_release (ep_rt_spin_lock_handle_t *spin_lock) return false; } +void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array) +{ + STATIC_CONTRACT_NOTHROW; + EP_ASSERT (env_array != NULL); + +#ifdef HOST_WIN32 + ep_char16_t * envs = reinterpret_cast(GetEnvironmentStringsW ()); + if (envs) { + const ep_char16_t * next = envs; + while (*next) { + dn_vector_ptr_push_back (env_array, ep_rt_utf16_string_dup (reinterpret_cast(next))); + next += ep_rt_utf16_string_len (reinterpret_cast(next)) + 1; + } + FreeEnvironmentStringsW (reinterpret_cast(envs)); + } +#else + ep_char8_t **next = NULL; + for (next = environ; *next != NULL; ++next) + dn_vector_ptr_push_back (env_array, ep_rt_utf8_to_utf16le_string (*next, -1)); +#endif +} + #ifdef EP_CHECKED_BUILD void ep_rt_aot_lock_requires_lock_held (const ep_rt_lock_handle_t *lock) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h index 2687fc8f35ed5..4c88267b4bf05 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h @@ -1114,10 +1114,8 @@ static void ep_rt_os_environment_get_utf16 (dn_vector_ptr_t *env_array) { - STATIC_CONTRACT_NOTHROW; - EP_ASSERT (env_array != NULL); - - // PalDebugBreak(); + extern void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array); + ep_rt_aot_os_environment_get_utf16(env_array); } /* diff --git a/src/tests/tracing/eventpipe/processenvironment/processenvironment.csproj b/src/tests/tracing/eventpipe/processenvironment/processenvironment.csproj index 4cf5a34c0c141..ddab20e07466b 100644 --- a/src/tests/tracing/eventpipe/processenvironment/processenvironment.csproj +++ b/src/tests/tracing/eventpipe/processenvironment/processenvironment.csproj @@ -8,6 +8,7 @@ true true true + true From 2907e60a68eb4310f879b6d129f5137d8924e95a Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 16 Jun 2023 10:04:45 -0700 Subject: [PATCH 2/6] using free to match malloc --- src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp index 109bdac40cee6..f6346b3c04666 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -241,8 +241,8 @@ ds_rt_aot_set_environment_variable (const ep_char16_t *name, const ep_char16_t * ep_char8_t *nameNarrow = ep_rt_utf16le_to_utf8_string (name, ep_rt_utf16_string_len (name)); ep_char8_t *valueNarrow = ep_rt_utf16le_to_utf8_string (value, ep_rt_utf16_string_len (value)); int32_t ret_value = setenv(nameNarrow, valueNarrow, 1); - delete nameNarrow; - delete valueNarrow; + free(nameNarrow); + free(valueNarrow); return ret_value; #else return SetEnvironmentVariableW(reinterpret_cast(name), reinterpret_cast(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError()); From a86dbf08d33afa9be7c9959dc8a6127512d77bd7 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Mon, 19 Jun 2023 08:29:40 -0700 Subject: [PATCH 3/6] Add test to CI --- eng/pipelines/runtime.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/pipelines/runtime.yml b/eng/pipelines/runtime.yml index 784d3f86da5d6..12cd994ed0d96 100644 --- a/eng/pipelines/runtime.yml +++ b/eng/pipelines/runtime.yml @@ -248,7 +248,7 @@ extends: extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml extraStepsParameters: creator: dotnet-bot - testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/diagnosticport;tracing/eventpipe/reverse;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true' + testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/diagnosticport;tracing/eventpipe/reverse;tracing/eventpipe/processenvironment;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true' liveLibrariesBuildConfig: Release testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig) extraVariablesTemplates: From 82931d579425486ad52cdb65896c0a584b2e2870 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 23 Jun 2023 08:27:54 -0700 Subject: [PATCH 4/6] Fix FreeBSD build break --- .../nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index 74a061727ea42..e19b2f1dba91f 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -669,6 +669,24 @@ bool ep_rt_aot_spin_lock_release (ep_rt_spin_lock_handle_t *spin_lock) return false; } +#ifndef HOST_WIN32 +#if defined(__APPLE__) +#if defined (HOST_OSX) +extern "C" { +gchar ***_NSGetEnviron(void); +} +#define environ (*_NSGetEnviron()) +#else +static char *_ep_rt_aot_environ[1] = { NULL }; +#define environ _ep_rt_aot_environ +#endif /* defined (HOST_OSX) */ +#else +extern "C" { +extern char **environ; +} +#endif /* defined (__APPLE__) */ +#endif /* !defined (HOST_WIN32) */ + void ep_rt_aot_os_environment_get_utf16 (dn_vector_ptr_t *env_array) { STATIC_CONTRACT_NOTHROW; From e2310ca2709291cb2266efe023e2f937db115025 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 23 Jun 2023 09:59:53 -0700 Subject: [PATCH 5/6] Fix typo in OSX environ def --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index e19b2f1dba91f..79ab65d69461c 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -673,7 +673,7 @@ bool ep_rt_aot_spin_lock_release (ep_rt_spin_lock_handle_t *spin_lock) #if defined(__APPLE__) #if defined (HOST_OSX) extern "C" { -gchar ***_NSGetEnviron(void); +char ***_NSGetEnviron(void); } #define environ (*_NSGetEnviron()) #else From 7ea9e8009c15f2a2fe09e22ee20bd9d73ebddf2c Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 6 Jul 2023 03:19:27 -0700 Subject: [PATCH 6/6] FB --- src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp index 79ab65d69461c..90d2c27523c28 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp @@ -672,9 +672,7 @@ bool ep_rt_aot_spin_lock_release (ep_rt_spin_lock_handle_t *spin_lock) #ifndef HOST_WIN32 #if defined(__APPLE__) #if defined (HOST_OSX) -extern "C" { -char ***_NSGetEnviron(void); -} +extern "C" {char ***_NSGetEnviron(void);} #define environ (*_NSGetEnviron()) #else static char *_ep_rt_aot_environ[1] = { NULL };