Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Move .NET method invocation logging to after the needed type conversion is done for method arguments#25022

New issue

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

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

Already on GitHub?Sign in to your account

Merged
daxian-dbw merged 6 commits intoPowerShell:masterfromdaxian-dbw:amsilog
Mar 18, 2025
Merged
Changes fromall commits
Commits
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6943,12 +6943,6 @@ internal static DynamicMetaObject InvokeDotNetMethod(
expr=Expression.Block(expr,ExpressionCache.AutomationNullConstant);
}

// Expression block runs two expressions in order:
// - Log method invocation to AMSI Notifications (can throw PSSecurityException)
// - Invoke method
stringtargetName=methodInfo.ReflectedType?.FullName??string.Empty;
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
// propagation and ensure errors are attributed to the correct code (the
Expand DownExpand Up@@ -7111,6 +7105,7 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
invocationType!=MethodInvocationType.NonVirtual;
varparameters=mi.GetParameters();
varargExprs=newExpression[parameters.Length];
varargsToLog=newList<Expression>(Math.Max(parameters.Length,args.Length));

for(inti=0;i<parameters.Length;++i)
{
Expand All@@ -7135,16 +7130,21 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,

if(expandParameters)
{
argExprs[i]=Expression.NewArrayInit(
paramElementType,
args.Skip(i).Select(
a=>a.CastOrConvertMethodArgument(
IEnumerable<Expression>elements=args
.Skip(i)
.Select(a=>
a.CastOrConvertMethodArgument(
paramElementType,
paramName,
mi.Name,
allowCastingToByRefLikeType:false,
temps,
initTemps)));
initTemps))
.ToList();

argExprs[i]=Expression.NewArrayInit(paramElementType,elements);
// User specified the element arguments, so we log them instead of the compiler-created array.
argsToLog.AddRange(elements);
}
else
{
Expand All@@ -7155,13 +7155,18 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
allowCastingToByRefLikeType:false,
temps,
initTemps);

argExprs[i]=arg;
argsToLog.Add(arg);
}
}
elseif(i>=args.Length)
{
Diagnostics.Assert(parameters[i].IsOptional,
// We don't log the default value for an optional parameter, as it's not specified by the user.
Diagnostics.Assert(
parameters[i].IsOptional,
"if there are too few arguments, FindBestMethod should only succeed if parameters are optional");

varargValue=parameters[i].DefaultValue;
if(argValue==null)
{
Expand DownExpand Up@@ -7199,17 +7204,25 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
varpsRefValue=Expression.Property(args[i].Expression.Cast(typeof(PSReference)),CachedReflectionInfo.PSReference_Value);
initTemps.Add(Expression.Assign(temp,psRefValue.Convert(temp.Type)));
copyOutTemps.Add(Expression.Assign(psRefValue,temp.Cast(typeof(object))));

argExprs[i]=temp;
argsToLog.Add(temp);
}
else
{
argExprs[i]=args[i].CastOrConvertMethodArgument(
varconvertedArg=args[i].CastOrConvertMethodArgument(
parameterType,
paramName,
mi.Name,
allowCastingToByRefLikeType,
temps,
initTemps);

argExprs[i]=convertedArg;
// If the converted arg is a byref-like type, then we log the original arg.
argsToLog.Add(convertedArg.Type.IsByRefLike
?args[i].Expression
:convertedArg);
}
}
}
Expand DownExpand Up@@ -7255,6 +7268,12 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
}
}

// We need to add one expression to log the .NET invocation before actually invoking:
// - Log method invocation to AMSI Notifications (can throw PSSecurityException)
// - Invoke method
stringtargetName=mi.ReflectedType?.FullName??string.Empty;
stringmethodName=mi.Nameis".ctor"?"new":mi.Name;

if(temps.Count>0)
{
if(call.Type!=typeof(void)&&copyOutTemps.Count>0)
Expand All@@ -7265,8 +7284,13 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target,
copyOutTemps.Add(retValue);
}

AddMemberInvocationLogging(initTemps,targetName,methodName,argsToLog);
call=Expression.Block(call.Type,temps,initTemps.Append(call).Concat(copyOutTemps));
}
else
{
call=AddMemberInvocationLogging(call,targetName,methodName,argsToLog);
}

returncall;
}
Expand DownExpand Up@@ -7559,28 +7583,50 @@ internal static void InvalidateCache()
}

#nullable enable
privatestaticExpressionMaybeAddMemberInvocationLogging(
privatestaticExpressionAddMemberInvocationLogging(
Expressionexpr,
stringtargetName,
stringname,
DynamicMetaObject[]args)
List<Expression>args)
{
#ifUNIX&&!DEBUG
// For efficiency this is a no-op on non-Windows platforms in release builds.
#ifUNIX
// For efficiency this is a no-op on non-Windows platforms.
returnexpr;
#else
Expression[]invocationArgs=newExpression[args.Length];
for(inti=0;i<args.Length;i++)
Expression[]invocationArgs=newExpression[args.Count];
for(inti=0;i<args.Count;i++)
{
invocationArgs[i]=args[i].Expression.Cast(typeof(object));
invocationArgs[i]=args[i].Cast(typeof(object));
}

returnExpression.Block(
Expression.Call(
CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation,
Expression.Constant(targetName),
Expression.Constant(name),
Expression.NewArrayInit(typeof(object),invocationArgs)),
expr);
#endif
}

privatestaticvoidAddMemberInvocationLogging(
List<Expression>exprs,
stringtargetName,
stringname,
List<Expression>args)
{
#if!UNIX
Expression[]invocationArgs=newExpression[args.Count];
for(inti=0;i<args.Count;i++)
{
invocationArgs[i]=args[i].Cast(typeof(object));
}

exprs.Add(Expression.Call(
CachedReflectionInfo.MemberInvocationLoggingOps_LogMemberInvocation,
Expression.Constant(targetName),
Expression.Constant(name),
Expression.NewArrayInit(typeof(object),invocationArgs)));
#endif
}
#nullable disable
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp