Skip to content

Commit

Permalink
Enable reading process environment variables in EventPipe (dotnet#87771)
Browse files Browse the repository at this point in the history
* Process Environment support

* using free to match malloc

* Add test to CI

* Fix FreeBSD build break

* Fix typo in OSX environ def

* FB
  • Loading branch information
LakshanF authored Jul 6, 2023
1 parent a3981dd commit 2f14594
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion eng/pipelines/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;tracing/eventpipe/simpleruntimeeventvalidation;" 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;tracing/eventpipe/simpleruntimeeventvalidation;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
liveLibrariesBuildConfig: Release
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)
extraVariablesTemplates:
Expand Down
21 changes: 21 additions & 0 deletions src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp
Original file line number Diff line number Diff line change
@@ -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 <windows.h>
#else
#include <stdlib.h>
#endif

#include <sys/types.h>

#ifdef __APPLE__
Expand Down Expand Up @@ -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);
free(nameNarrow);
free(valueNarrow);
return ret_value;
#else
return SetEnvironmentVariableW(reinterpret_cast<LPCWSTR>(name), reinterpret_cast<LPCWSTR>(value)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
#endif
}
#endif /* ENABLE_PERFTRACING */
5 changes: 2 additions & 3 deletions src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<LPCWSTR>(name), reinterpret_cast<LPCWSTR>(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
Expand Down
38 changes: 38 additions & 0 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,44 @@ 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" {char ***_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;
EP_ASSERT (env_array != NULL);

#ifdef HOST_WIN32
ep_char16_t * envs = reinterpret_cast<ep_char16_t *>(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<const ep_char16_t *>(next)));
next += ep_rt_utf16_string_len (reinterpret_cast<const ep_char16_t *>(next)) + 1;
}
FreeEnvironmentStringsW (reinterpret_cast<LPWSTR>(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)
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<GCStressIncompatible>true</GCStressIncompatible>
<JitOptimizationSensitive>true</JitOptimizationSensitive>
<IlasmRoundTripIncompatible>true</IlasmRoundTripIncompatible>
<EventSourceSupport Condition="'$(TestBuildMode)' == 'nativeaot'">true</EventSourceSupport>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
Expand Down

0 comments on commit 2f14594

Please sign in to comment.