From 06e2093e8c35a4805cfab1c72cf7d7fe33afa9a3 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Sun, 8 Dec 2024 13:36:04 +1000 Subject: [PATCH] Change call to optional add in the binder expression --- .../engine/runtime/Binding/Binders.cs | 37 ++++++++++++++----- .../engine/runtime/Operations/MiscOps.cs | 5 --- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index de36b9d724..ab4e1067ac 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -6947,15 +6947,7 @@ namespace System.Management.Automation.Language // - Log method invocation to AMSI Notifications (can throw PSSecurityException) // - Invoke method string targetName = methodInfo.ReflectedType?.FullName ?? string.Empty; - expr = Expression.Block( - Expression.Call( - CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation, - Expression.Constant(targetName), - Expression.Constant(name), - Expression.NewArrayInit( - typeof(object), - args.Select(static e => e.Expression.Cast(typeof(object))))), - expr); + MaybeAddMemberInvocationLogging(expr, targetName, name, args); // If we're calling SteppablePipeline.{Begin|Process|End}, we don't want // to wrap exceptions - this is very much a special case to help error @@ -7566,6 +7558,33 @@ namespace System.Management.Automation.Language } } +#nullable enable + private static Expression MaybeAddMemberInvocationLogging( + Expression expr, + string targetName, + string name, + DynamicMetaObject[] args) + { +#if UNIX && !DEBUG + // For efficiency this is a no-op on non-Windows platforms in release builds. + return expr; +#else + Expression[] invocationArgs = new Expression[args.Length]; + for (int i = 0; i < args.Length; i++) + { + invocationArgs[i] = args[i].Expression.Cast(typeof(object)); + } + return Expression.Block( + Expression.Call( + CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation, + Expression.Constant(targetName), + Expression.Constant(name), + Expression.NewArrayInit(typeof(object), invocationArgs)), + expr); +#endif + } +#nullable disable + #endregion } diff --git a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs index 2a37bfb507..ddc70fabd5 100644 --- a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs +++ b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs @@ -3694,10 +3694,6 @@ namespace System.Management.Automation internal static void LogMemberInvocation(string targetName, string name, object[] args) { -#if UNIX && !DEBUG - // For efficiency this is a no-op on non-Windows platforms in release builds. - return; -#else try { var contentName = "PowerShellMemberInvocation"; @@ -3745,7 +3741,6 @@ namespace System.Management.Automation Console.WriteLine($"!!! Amsi notification report exception: {ex} !!!"); } } -#endif } } }