Change call to optional add in the binder expression

This commit is contained in:
Jordan Borean
2024-12-09 14:35:05 -05:00
committed by Patrick Meinecke
parent 89921dd500
commit 06e2093e8c
2 changed files with 28 additions and 14 deletions
@@ -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
}
@@ -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
}
}
}