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 incorrect range comparison for switch operands #3128

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void RewriteCondition (int index, Instruction instr, int operand)

case Code.Switch:
var targets = (Instruction[]) instr.Operand;
if (operand <= targets.Length) {
if (operand < targets.Length) {
// It does not need to be conditional but existing logic in BodySweeper would
// need to be updated to deal with 1->2 instruction replacement
RewriteConditionTo (index, Instruction.Create (operand == 0 ? OpCodes.Brfalse : OpCodes.Brtrue, targets[operand]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.Serialization;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
using System.Runtime.InteropServices;

namespace Mono.Linker.Tests.Cases.UnreachableBlock
{
Expand All @@ -17,6 +18,7 @@ public static void Main ()
TestSwitch.TestOffset ();
TestSwitch2.TestFallThrough ();
TestSwitchZero.Test ();
TestSwitch3.TestFallThrough(true);
}

[Kept]
Expand Down Expand Up @@ -125,6 +127,55 @@ public static void TestFallThrough ()
static void Unreached () { }
}

[Kept]
class TestSwitch3
{
[Kept]
[ExpectedInstructionSequence (new[] {
"ldc.i4.4",
"stloc.0",
"ldloc.0",
"pop",
"br.s il_6",
"newobj System.Void System.NotSupportedException::.ctor()",
"throw",
})]
public static object TestFallThrough (bool createReader)
{
object instance;

switch (ProcessArchitecture, createReader)
{
case (Architecture.X86, true):
Unreached ();
break;
case (Architecture.X86, false):
Unreached ();
break;
case (Architecture.X64, true):
Unreached ();
break;
case (Architecture.X64, false):
Unreached ();
break;
case (Architecture.Arm64, true):
Unreached ();
break;
case (Architecture.Arm64, false):
Unreached ();
break;
default:
throw new NotSupportedException();
}

return null;
}

static Architecture ProcessArchitecture => Architecture.Wasm;

static void Unreached () { }
}

[Kept]
class TestSwitchZero
{
Expand Down