- Notifications
You must be signed in to change notification settings - Fork7.7k
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Move .NET method invocation logging to after the needed type conversion is done for method arguments#25022
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
58a4683
Make sure the arguments used with .NET method logging are after the n…
daxian-dbwa930a24
Remove unneeded nullable section
daxian-dbw159d5ff
Fix the bug that causes PowerShell fails to handle byref-like parameters
daxian-dbwd417722
Add back the `#nullable` section
daxian-dbw4c8643c
Log the value of 'PSReference' object for by-ref parameters
daxian-dbw1704965
Call 'ToList()' to avoid running 'CastOrConvertMethodArgument' twice
daxian-dbwFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
86 changes: 66 additions & 20 deletionssrc/System.Management.Automation/engine/runtime/Binding/Binders.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6943,12 +6943,6 @@ internal static DynamicMetaObject InvokeDotNetMethod( | ||
expr=Expression.Block(expr,ExpressionCache.AutomationNullConstant); | ||
} | ||
// 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 | ||
@@ -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) | ||
{ | ||
@@ -7135,16 +7130,21 @@ internal static Expression InvokeMethod(MethodBase mi, DynamicMetaObject target, | ||
if(expandParameters) | ||
{ | ||
IEnumerable<Expression>elements=args | ||
.Skip(i) | ||
.Select(a=> | ||
a.CastOrConvertMethodArgument( | ||
paramElementType, | ||
paramName, | ||
mi.Name, | ||
allowCastingToByRefLikeType:false, | ||
temps, | ||
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); | ||
SeeminglyScience marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
else | ||
{ | ||
@@ -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) | ||
{ | ||
// 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) | ||
{ | ||
@@ -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 | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
@@ -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)&©OutTemps.Count>0) | ||
@@ -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; | ||
} | ||
@@ -7559,28 +7583,50 @@ internal static void InvalidateCache() | ||
} | ||
#nullable enable | ||
privatestaticExpressionAddMemberInvocationLogging( | ||
Expressionexpr, | ||
stringtargetName, | ||
stringname, | ||
List<Expression>args) | ||
{ | ||
#ifUNIX | ||
daxian-dbw marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// For efficiency this is a no-op on non-Windows platforms. | ||
returnexpr; | ||
#else | ||
Expression[]invocationArgs=newExpression[args.Count]; | ||
for(inti=0;i<args.Count;i++) | ||
{ | ||
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 | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.