Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); ApplyStartupHook diagnostic IPC command by jander-msft · Pull Request #86813 · dotnet/runtime · GitHub
Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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();
Comment thread
jander-msft marked this conversation as resolved.

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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 numberDiff line numberDiff 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