Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of ref return types. Added a test assembly to collect … #34

Merged
merged 1 commit into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MethodBoundaryAspect.Fody.Attributes;

namespace MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.Aspects
{
public class SetReturnValueAspect : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs arg)
{
SetReturnValueAspectMethods.Result = arg.ReturnValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

// Ignore PEVerify's complaint about ref return types.
[assembly: MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.Attributes.IgnorePEVerifyCode("80131870")]

namespace MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.Attributes
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple =true)]
public class IgnorePEVerifyCode : Attribute
{
public string ErrorCode { get; private set; }

public IgnorePEVerifyCode(string code) => ErrorCode = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MethodBoundaryAspect\MethodBoundaryAspect.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.Aspects;

namespace MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly
{
public class SetReturnValueAspectMethods
{
public static object Result { get; set; }

public static int Field = 10;

public void InstanceMethodCall_RefReturnValueType()
{
RefReturnMethodCall();
}

[SetReturnValueAspect]
ref int RefReturnMethodCall()
{
return ref Field;
}

public static string StrField = "overwritten";

public void InstanceMethodCall_RefReturnReferenceType()
{
RefReturnStringCall();
}

[SetReturnValueAspect]
ref string RefReturnStringCall()
{
return ref StrField;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ProjectReference Include="..\MethodBoundaryAspect.Fody.UnitTests.TestAssemblyAspects\MethodBoundaryAspect.Fody.UnitTests.TestAssemblyAspects.csproj" />
<ProjectReference Include="..\MethodBoundaryAspect.Fody.UnitTests.TestAssembly\MethodBoundaryAspect.Fody.UnitTests.TestAssembly.csproj" />
<ProjectReference Include="..\MethodBoundaryAspect.Fody.UnitTests.TestProgram\MethodBoundaryAspect.Fody.UnitTests.TestProgram.csproj" />
<ProjectReference Include="..\MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly\MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.csproj" />
<ProjectReference Include="..\MethodBoundaryAspect.Fody\MethodBoundaryAspect.Fody.csproj" />
<ProjectReference Include="..\MethodBoundaryAspect\MethodBoundaryAspect.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using FluentAssertions;
using MethodBoundaryAspect.Fody.UnitTests.Unified;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using FluentAssertions;
using MethodBoundaryAspect.Fody.UnitTests.Unified;

namespace MethodBoundaryAspect.Fody.UnitTests
{
Expand Down Expand Up @@ -115,7 +117,8 @@ private void WeaveAssemblyAndVerifyAndLoad(Type type, string methodName, string
}

WeaveAssembly(type, Weaver);
RunPeVerify();
var ignores = type.Assembly.GetCustomAttributes<UnverifiableTestAssembly.Attributes.IgnorePEVerifyCode>();
RunPeVerify(ignores.Select(a => a.ErrorCode));
LoadWeavedAssembly();
}

Expand Down Expand Up @@ -163,14 +166,14 @@ private Tuple<string,string> CreateFullPropertyName(Type type, string propertyNa
$"{type.FullName}.{propertyInfo.GetMethod.Name}");
}

private void RunPeVerify()
private void RunPeVerify(IEnumerable<string> ignoreErrorCodes)
{
Action action = () =>
{
var runIlSpy = false;
try
{
PeVerifier.Verify(WeavedAssemblyPath);
PeVerifier.Verify(WeavedAssemblyPath, ignoreErrorCodes);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using FluentAssertions;
using MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly;
using System;
using Xunit;

namespace MethodBoundaryAspect.Fody.UnitTests
{
public class SetRefReturnValueAspectTests : MethodBoundaryAspectTestBase
{
private static readonly Type TestClassType = typeof(SetReturnValueAspectMethods);

[Fact]
public void IfInstanceMethodWithRefReturnTypeIsCalled_ThenTheOnMethodBoundaryAspectShouldBeCalled()
{
// Arrange
const string testMethodName = "InstanceMethodCall_RefReturnValueType";
WeaveAssemblyClassAndLoad(TestClassType);

// Act
var result = AssemblyLoader.InvokeMethod(TestClassType.FullName, testMethodName);

// Assert
result.Should().Be(10);
}

[Fact]
public void IfInstanceMethodWithRefReturnReferenceTypeIsCalled_ThenTheOnMethodBoundaryAspectShouldBeCalled()
{
// Arrange
const string testMethodName = "InstanceMethodCall_RefReturnReferenceType";
WeaveAssemblyClassAndLoad(TestClassType);

// Act
var result = AssemblyLoader.InvokeMethod(TestClassType.FullName, testMethodName);

// Assert
result.Should().Be("overwritten");
}
}
}
15 changes: 13 additions & 2 deletions MethodBoundaryAspect.Fody.UnitTests/Unified/PeVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -7,14 +8,21 @@ namespace MethodBoundaryAspect.Fody.UnitTests.Unified
{
public static class PeVerifier
{
public static void Verify(string assemblyPath)
public static void Verify(string assemblyPath, IEnumerable<string> ignoreErrorCodes = null)
{
var peVerifyPath = GetPeVerifyPath();

string path = $"\"{assemblyPath}\"";
string joinedErrorCodes = String.Empty;
if (ignoreErrorCodes != null)
joinedErrorCodes = String.Join(",", ignoreErrorCodes);
if (!string.IsNullOrEmpty(joinedErrorCodes))
path = $"{path} /ignore={joinedErrorCodes}";

var psi = new ProcessStartInfo
{
FileName = peVerifyPath,
Arguments = $"\"{assemblyPath}\"",
Arguments = path,
CreateNoWindow = true,
RedirectStandardOutput = true,
UseShellExecute = false
Expand All @@ -34,6 +42,9 @@ private static string GetPeVerifyPath()
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
var possiblePeVerifyPaths = new[]
{
$@"{folderPath}\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools\peverify.exe",
$@"{folderPath}\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\peverify.exe",
$@"{folderPath}\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7 Tools\peverify.exe",
$@"{folderPath}\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\peverify.exe",
$@"{folderPath}\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\peverify.exe"
};
Expand Down
6 changes: 6 additions & 0 deletions MethodBoundaryAspect.Fody.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodBoundaryAspect.Fody.U
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodBoundaryAspect.Fody.UnitTests.TestAssembly", "MethodBoundaryAspect.Fody.UnitTests.TestAssembly\MethodBoundaryAspect.Fody.UnitTests.TestAssembly.csproj", "{B5D3E43D-1B47-46DA-9CFE-76750D195359}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly", "MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly\MethodBoundaryAspect.Fody.UnitTests.UnverifiableTestAssembly.csproj", "{DDB88B85-71FE-42B8-9653-EE0DBDC45620}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodBoundaryAspect.Fody.UnitTests.TestAssembly.Aspects", "MethodBoundaryAspect.Fody.UnitTests.TestAssemblyAspects\MethodBoundaryAspect.Fody.UnitTests.TestAssemblyAspects.csproj", "{EAD6BAD4-9DAE-44A4-9418-24A68F7C935A}"
EndProject
Global
Expand Down Expand Up @@ -47,6 +49,10 @@ Global
{B5D3E43D-1B47-46DA-9CFE-76750D195359}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5D3E43D-1B47-46DA-9CFE-76750D195359}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5D3E43D-1B47-46DA-9CFE-76750D195359}.Release|Any CPU.Build.0 = Release|Any CPU
{DDB88B85-71FE-42B8-9653-EE0DBDC45620}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDB88B85-71FE-42B8-9653-EE0DBDC45620}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDB88B85-71FE-42B8-9653-EE0DBDC45620}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDB88B85-71FE-42B8-9653-EE0DBDC45620}.Release|Any CPU.Build.0 = Release|Any CPU
{EAD6BAD4-9DAE-44A4-9418-24A68F7C935A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAD6BAD4-9DAE-44A4-9418-24A68F7C935A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAD6BAD4-9DAE-44A4-9418-24A68F7C935A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
12 changes: 9 additions & 3 deletions MethodBoundaryAspect.Fody/InstructionBlockCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,18 @@ private List<Instruction> CreateInstanceMethodCallInstructions(
foreach (var argument in arguments)
{
loadArgumentsInstructions.Add(_processor.Create(OpCodes.Ldloc, argument));
TypeReference varType = argument.VariableType;
if (argument.VariableType.IsByReference)
{
varType = ((ByReferenceType)argument.VariableType).ElementType;
loadArgumentsInstructions.Add(_processor.Create(OpCodes.Ldobj, varType));
}

var parameter = methodReference.Parameters[parameterCount];
if (parameter.ParameterType != argument.VariableType)
if (parameter.ParameterType != varType)
{
if (argument.VariableType.IsValueType || argument.VariableType.IsGenericParameter)
loadArgumentsInstructions.Add(_processor.Create(OpCodes.Box, argument.VariableType));
if (varType.IsValueType || varType.IsGenericParameter)
loadArgumentsInstructions.Add(_processor.Create(OpCodes.Box, varType));
}
parameterCount++;
}
Expand Down