Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApplyStartupHook diagnostic IPC command #86813

Merged
merged 23 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace System
{
internal static partial class StartupHookProvider
{
private static void ManagedStartup()
private static unsafe void ManagedStartup(char* pDiagnosticStartupHooks)
{
#if FEATURE_PERFTRACING
if (EventSource.IsSupported)
RuntimeEventSource.Initialize();
#endif

if (IsSupported)
ProcessStartupHooks();
ProcessStartupHooks(new string(pDiagnosticStartupHooks));
}
}
}
7 changes: 7 additions & 0 deletions src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ ds_rt_disable_perfmap (void)
return DS_IPC_E_NOTSUPPORTED;
}

static
uint32_t
ds_rt_apply_startup_hook (const ep_char16_t *startup_hook_path)
{
return DS_IPC_E_NOTSUPPORTED;
}

/*
* DiagnosticServer.
*/
Expand Down
49 changes: 47 additions & 2 deletions src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
//#define STRICT_JITLOCK_ENTRY_LEAK_DETECTION
//#define STRICT_CLSINITLOCK_ENTRY_LEAK_DETECTION

LPCWSTR s_wszDiagnosticStartupHookPaths = nullptr;

#ifndef DACCESS_COMPILE

Expand Down Expand Up @@ -1122,7 +1123,47 @@ bool Assembly::IgnoresAccessChecksTo(Assembly *pAccessedAssembly)
return GetFriendAssemblyInfo()->IgnoresAccessChecksTo(pAccessedAssembly);
}

void Assembly::AddDiagnosticStartupHookPath(LPCWSTR wszPath)
{
LPCWSTR wszDiagnosticStartupHookPathsLocal = s_wszDiagnosticStartupHookPaths;

size_t cchPath = u16_strlen(wszPath);
size_t cchDiagnosticStartupHookPathsNew = cchPath;
size_t cchDiagnosticStartupHookPathsLocal = 0;
if (nullptr != wszDiagnosticStartupHookPathsLocal)
{
cchDiagnosticStartupHookPathsLocal = u16_strlen(wszDiagnosticStartupHookPathsLocal);
// Add 1 for the path separator
cchDiagnosticStartupHookPathsNew += cchDiagnosticStartupHookPathsLocal + 1;
}

size_t currentSize = cchDiagnosticStartupHookPathsNew + 1;
LPWSTR wszDiagnosticStartupHookPathsNew = new WCHAR[currentSize];
LPWSTR wszCurrent = wszDiagnosticStartupHookPathsNew;

u16_strcpy_s(wszCurrent, currentSize, wszPath);
wszCurrent += cchPath;
currentSize -= cchPath;

if (cchDiagnosticStartupHookPathsLocal > 0)
{
u16_strcpy_s(wszCurrent, currentSize, PATH_SEPARATOR_STR_W);
wszCurrent += 1;
currentSize -= 1;

u16_strcpy_s(wszCurrent, currentSize, wszDiagnosticStartupHookPathsLocal);
wszCurrent += cchDiagnosticStartupHookPathsLocal;
currentSize -= cchDiagnosticStartupHookPathsLocal;
}

// Expect null terminating character
_ASSERTE(currentSize == 1);
_ASSERTE(wszCurrent[0] == W('\0'));

s_wszDiagnosticStartupHookPaths = wszDiagnosticStartupHookPathsNew;

delete [] wszDiagnosticStartupHookPathsLocal;
}

enum CorEntryPointType
{
Expand Down Expand Up @@ -1376,7 +1417,7 @@ static void RunMainPost()
}
}

static void RunManagedStartup()
void RunManagedStartup()
{
CONTRACTL
{
Expand All @@ -1388,7 +1429,11 @@ static void RunManagedStartup()
CONTRACTL_END;

MethodDescCallSite managedStartup(METHOD__STARTUP_HOOK_PROVIDER__MANAGED_STARTUP);
managedStartup.Call(NULL);

ARG_SLOT args[1];
args[0] = PtrToArgSlot(s_wszDiagnosticStartupHookPaths);

managedStartup.Call(args);
}

INT32 Assembly::ExecuteMainMethod(PTRARRAYREF *stringArgs, BOOL waitForOtherThreads)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/assembly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ class Assembly
}
#endif

static void AddDiagnosticStartupHookPath(LPCWSTR wszPath);


protected:
#ifdef FEATURE_COMINTEROP
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ DEFINE_FIELD_U(rgiLastFrameFromForeignExceptionStackTrace, StackFrame
DEFINE_FIELD_U(iFrameCount, StackFrameHelper, iFrameCount)

DEFINE_CLASS(STARTUP_HOOK_PROVIDER, System, StartupHookProvider)
DEFINE_METHOD(STARTUP_HOOK_PROVIDER, MANAGED_STARTUP, ManagedStartup, SM_RetVoid)
DEFINE_METHOD(STARTUP_HOOK_PROVIDER, MANAGED_STARTUP, ManagedStartup, SM_PtrChar_RetVoid)

DEFINE_CLASS(STREAM, IO, Stream)
DEFINE_METHOD(STREAM, BEGIN_READ, BeginRead, IM_ArrByte_Int_Int_AsyncCallback_Object_RetIAsyncResult)
Expand Down
22 changes: 22 additions & 0 deletions src/coreclr/vm/eventing/eventpipe/ds-rt-coreclr.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,28 @@ ds_rt_disable_perfmap (void)
#endif // FEATURE_PERFMAP
}

static ep_char16_t * _ds_rt_coreclr_diagnostic_startup_hook_paths = NULL;

static
uint32_t
ds_rt_apply_startup_hook (const ep_char16_t *startup_hook_path)
{
HRESULT hr = S_OK;
// This is set to true when the EE has initialized, which occurs after
// the diagnostic suspension point has completed.
if (g_fEEStarted)
{
// TODO: Support loading and executing startup hook after EE has completely initialized.
return DS_IPC_E_INVALIDARG;
}
else
{
Assembly::AddDiagnosticStartupHookPath(reinterpret_cast<LPCWSTR>(startup_hook_path));
}

return DS_IPC_S_OK;
}

/*
* DiagnosticServer.
*/
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/metasig.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ DEFINE_METASIG(SM(IntPtr_Bool_RetVoid, I F, v))
DEFINE_METASIG(SM(IntPtr_UInt_IntPtr_RetVoid, I K I, v))
DEFINE_METASIG(SM(IntPtr_RetUInt, I, K))
DEFINE_METASIG(SM(PtrChar_RetInt, P(u), i))
DEFINE_METASIG(SM(PtrChar_RetVoid, P(u), v))
DEFINE_METASIG(SM(IntPtr_IntPtr_RetIntPtr, I I, I))
DEFINE_METASIG(SM(IntPtr_IntPtr_Int_RetIntPtr, I I i, I))
DEFINE_METASIG(SM(PtrVoid_PtrVoid_RetVoid, P(v) P(v), v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<argument>ILLink</argument>
<argument>IL2026</argument>
<property name="Scope">member</property>
<property name="Target">M:System.StartupHookProvider.ProcessStartupHooks()</property>
<property name="Target">M:System.StartupHookProvider.ProcessStartupHooks(System.String)</property>
<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app with System.StartupHookProvider.IsSupported=true.</property>
</attribute>
</assembly>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -27,15 +28,25 @@ private struct StartupHookNameOrPath

// Parse a string specifying a list of assemblies and types
// containing a startup hook, and call each hook in turn.
private static void ProcessStartupHooks()
private static void ProcessStartupHooks(string diagnosticStartupHooks)
{
if (!IsSupported)
return;

string? startupHooksVariable = AppContext.GetData("STARTUP_HOOKS") as string;
if (startupHooksVariable == null)
{
if (null == startupHooksVariable && string.IsNullOrEmpty(diagnosticStartupHooks))
return;

List<string> startupHookParts = new();

if (!string.IsNullOrEmpty(diagnosticStartupHooks))
{
startupHookParts.AddRange(diagnosticStartupHooks.Split(Path.PathSeparator));
}

if (null != startupHooksVariable)
{
startupHookParts.AddRange(startupHooksVariable.Split(Path.PathSeparator));
}

ReadOnlySpan<char> disallowedSimpleAssemblyNameChars = stackalloc char[4]
Expand All @@ -47,9 +58,8 @@ private static void ProcessStartupHooks()
};

// Parse startup hooks variable
string[] startupHookParts = startupHooksVariable.Split(Path.PathSeparator);
StartupHookNameOrPath[] startupHooks = new StartupHookNameOrPath[startupHookParts.Length];
for (int i = 0; i < startupHookParts.Length; i++)
StartupHookNameOrPath[] startupHooks = new StartupHookNameOrPath[startupHookParts.Count];
for (int i = 0; i < startupHookParts.Count; i++)
{
string startupHookPart = startupHookParts[i];
if (string.IsNullOrEmpty(startupHookPart))
Expand Down
8 changes: 8 additions & 0 deletions src/mono/mono/eventpipe/ds-rt-mono.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ ds_rt_disable_perfmap (void)
return DS_IPC_E_NOTSUPPORTED;
}

static
uint32_t
ds_rt_apply_startup_hook (const ep_char16_t *startup_hook_path)
{
// TODO: Implement.
return DS_IPC_E_NOTSUPPORTED;
}

/*
* DiagnosticServer.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/mono/mono/metadata/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -8135,7 +8135,11 @@ mono_runtime_run_startup_hooks (void)
mono_error_cleanup (error);
if (!method)
return;
mono_runtime_invoke_checked (method, NULL, NULL, error);

gpointer args [1];
args[0] = mono_string_empty_internal (mono_domain_get ());

mono_runtime_invoke_checked (method, NULL, args, error);
// runtime hooks design doc says not to catch exceptions from the hooks
mono_error_raise_exception_deprecated (error);
}
Expand Down
90 changes: 90 additions & 0 deletions src/native/eventpipe/ds-process-protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ process_protocol_helper_disable_perfmap (
DiagnosticsIpcMessage *message,
DiagnosticsIpcStream *stream);

static
bool
process_protocol_helper_apply_startup_hook (
DiagnosticsIpcMessage *message,
DiagnosticsIpcStream *stream);

static
bool
process_protocol_helper_unknown_command (
Expand Down Expand Up @@ -872,6 +878,87 @@ process_protocol_helper_disable_perfmap (
ep_exit_error_handler ();
}

DiagnosticsApplyStartupHookPayload *
ds_apply_startup_hook_payload_alloc (void)
{
return ep_rt_object_alloc (DiagnosticsApplyStartupHookPayload);
}

void
ds_apply_startup_hook_payload_free (DiagnosticsApplyStartupHookPayload *payload)
{
ep_return_void_if_nok (payload != NULL);
ep_rt_byte_array_free (payload->incoming_buffer);
ep_rt_object_free (payload);
}

static
uint8_t *
apply_startup_hook_command_try_parse_payload (
uint8_t *buffer,
uint16_t buffer_len)
{
EP_ASSERT (buffer != NULL);

uint8_t * buffer_cursor = buffer;
uint32_t buffer_cursor_len = buffer_len;

DiagnosticsApplyStartupHookPayload *instance = ds_apply_startup_hook_payload_alloc ();
ep_raise_error_if_nok (instance != NULL);

instance->incoming_buffer = buffer;

if (!ds_ipc_message_try_parse_string_utf16_t (&buffer_cursor, &buffer_cursor_len, &instance->startup_hook_path))
ep_raise_error ();

ep_on_exit:
return (uint8_t *)instance;

ep_on_error:
ds_apply_startup_hook_payload_free (instance);
instance = NULL;
ep_exit_error_handler ();
}

static
bool
process_protocol_helper_apply_startup_hook (
DiagnosticsIpcMessage *message,
DiagnosticsIpcStream *stream)
{
EP_ASSERT (message != NULL);
EP_ASSERT (stream != NULL);

if (!stream)
return false;

bool result = false;
DiagnosticsApplyStartupHookPayload *payload = (DiagnosticsApplyStartupHookPayload *)ds_ipc_message_try_parse_payload (message, apply_startup_hook_command_try_parse_payload);
if (!payload) {
ds_ipc_message_send_error (stream, DS_IPC_E_BAD_ENCODING);
ep_raise_error ();
}

ds_ipc_result_t ipc_result;
ipc_result = ds_rt_apply_startup_hook (payload->startup_hook_path);
if (ipc_result != DS_IPC_S_OK) {
ds_ipc_message_send_error (stream, ipc_result);
ep_raise_error ();
} else {
ds_ipc_message_send_success (stream, ipc_result);
}

result = true;

ep_on_exit:
ds_ipc_stream_free (stream);
return result;

ep_on_error:
EP_ASSERT (!result);
ep_exit_error_handler ();
}

static
bool
process_protocol_helper_unknown_command (
Expand Down Expand Up @@ -916,6 +1003,9 @@ ds_process_protocol_helper_handle_ipc_message (
case DS_PROCESS_COMMANDID_DISABLE_PERFMAP:
result = process_protocol_helper_disable_perfmap (message, stream);
break;
case DS_PROCESS_COMMANDID_APPLY_STARTUP_HOOK:
result = process_protocol_helper_apply_startup_hook (message, stream);
break;
default:
result = process_protocol_helper_unknown_command (message, stream);
break;
Expand Down
26 changes: 26 additions & 0 deletions src/native/eventpipe/ds-process-protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ ds_enable_perfmap_payload_alloc (void);
void
ds_enable_perfmap_payload_free (DiagnosticsEnablePerfmapPayload *payload);

/*
* DiagnosticsApplyStartupHookPayload
*/

#if defined(DS_INLINE_GETTER_SETTER) || defined(DS_IMPL_PROCESS_PROTOCOL_GETTER_SETTER)
struct _DiagnosticsApplyStartupHookPayload {
#else
struct _DiagnosticsApplyStartupHookPayload_Internal {
#endif
uint8_t * incoming_buffer;

const ep_char16_t *startup_hook_path;
};

#if !defined(DS_INLINE_GETTER_SETTER) && !defined(DS_IMPL_PROCESS_PROTOCOL_GETTER_SETTER)
struct _DiagnosticsApplyStartupHookPayload {
uint8_t _internal [sizeof (struct _DiagnosticsApplyStartupHookPayload_Internal)];
};
#endif

DiagnosticsApplyStartupHookPayload *
ds_apply_startup_hook_payload_alloc (void);

void
ds_apply_startup_hook_payload_free (DiagnosticsApplyStartupHookPayload *payload);

/*
* DiagnosticsProcessProtocolHelper.
*/
Expand Down
Loading