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

Fix #449 #450

Merged
merged 2 commits into from
Feb 8, 2022
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
14 changes: 9 additions & 5 deletions Harmony/Internal/MethodCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ void ParseExceptions()
var instr2 = GetInstruction(handler_end, true);
instr2.blocks.Add(new ExceptionBlock(ExceptionBlockType.EndExceptionBlock, null));

// The FilterOffset property is meaningful only for Filter clauses.
// The CatchType property is not meaningful for Filter or Finally clauses.
// The FilterOffset property is meaningful only for Filter clauses.
// The CatchType property is not meaningful for Filter or Finally clauses.
//
switch (exception.Flags)
{
Expand Down Expand Up @@ -640,16 +640,20 @@ void ReadOperand(ILInstruction instruction)

ILInstruction GetInstruction(int offset, bool isEndOfInstruction)
{
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), offset, $"Instruction offset {offset} is less than 0");

var lastInstructionIndex = ilInstructions.Count - 1;
if (offset < 0 || offset > ilInstructions[lastInstructionIndex].offset)
throw new Exception($"Instruction offset {offset} is outside valid range 0 - {ilInstructions[lastInstructionIndex].offset}");
var instruction = ilInstructions[lastInstructionIndex];
if (offset > instruction.offset + instruction.GetSize() - 1)
throw new ArgumentOutOfRangeException(nameof(offset), offset, $"Instruction offset {offset} is outside valid range 0 - {instruction.offset + instruction.GetSize() - 1}");

var min = 0;
var max = lastInstructionIndex;
while (min <= max)
{
var mid = min + ((max - min) / 2);
var instruction = ilInstructions[mid];
instruction = ilInstructions[mid];

if (isEndOfInstruction)
{
Expand Down
4 changes: 4 additions & 0 deletions HarmonyTests/HarmonyTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoMod.Common" Version="$(MonoModCommonVersion)" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions HarmonyTests/IL/TestMethodBodyReader.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
using HarmonyLib;
using HarmonyLibTests.Assets;
using NUnit.Framework;
using System;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
using System.Web;

namespace HarmonyLibTests.IL
{
[TestFixture]
public class TestMethodBodyReader : TestLogger
{
#if NETFRAMEWORK
[Test]
public void FixIssue449()
{
var method = typeof(HttpRuntime).GetMethod("ReleaseResourcesAndUnloadAppDomain", BindingFlags.Instance | BindingFlags.NonPublic);

if (Environment.OSVersion.Platform != PlatformID.Win32NT) return;

Assert.NotNull(method);

Assert.AreEqual(29, MethodBodyReader.GetInstructions(null, method).Count);
}
#endif
[Test]
public void Test_CanGetInstructionsWithNoILGenerator()
{
Expand Down