From 55c9a27e7b1c9c76ffb1a0454e5e79ded646be02 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Thu, 19 May 2022 18:21:12 +0200 Subject: Consider side effects when evaluating manually substituted methods (#2800) --- .../FeatureSettings/FeatureSubstitutionsNested.cs | 35 +++--- .../Substitutions/StubBodyWithStaticCtor.cs | 136 +++++++++++++++++++++ .../Substitutions/StubBodyWithStaticCtor.xml | 12 ++ 3 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.cs create mode 100644 test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.xml (limited to 'test/Mono.Linker.Tests.Cases') diff --git a/test/Mono.Linker.Tests.Cases/FeatureSettings/FeatureSubstitutionsNested.cs b/test/Mono.Linker.Tests.Cases/FeatureSettings/FeatureSubstitutionsNested.cs index b9a4f1c13..204f48df8 100644 --- a/test/Mono.Linker.Tests.Cases/FeatureSettings/FeatureSubstitutionsNested.cs +++ b/test/Mono.Linker.Tests.Cases/FeatureSettings/FeatureSubstitutionsNested.cs @@ -15,24 +15,11 @@ namespace Mono.Linker.Tests.Cases.FeatureSettings [SetupLinkerArgument ("--feature", "MethodCondition", "false")] [SetupLinkerArgument ("--feature", "FieldCondition", "true")] [SetupLinkerArgument ("--feature", "ResourceCondition", "true")] + [SetupLinkerArgument ("--enable-opt", "ipconstprop")] [RemovedResourceInAssembly ("test.exe", "ResourceFileRemoveWhenTrue.txt")] [KeptResource ("ResourceFileRemoveWhenFalse.txt")] public class FeatureSubstitutionsNested { - [ExpectedInstructionSequence (new[] { - "nop", - "ldc.i4.1", - "pop", - "ldc.i4.0", - "pop", - "ldc.i4.1", - "pop", - "ldc.i4.0", - "pop", - "ldsfld System.Boolean Mono.Linker.Tests.Cases.FeatureSettings.FeatureSubstitutionsNested::FieldConditionField", - "pop", - "ret", - })] public static void Main () { GlobalConditionMethod (); @@ -42,21 +29,41 @@ namespace Mono.Linker.Tests.Cases.FeatureSettings _ = FieldConditionField; } + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4.1", + "ret", + })] static bool GlobalConditionMethod () { throw new NotImplementedException (); } + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4.0", + "ret", + })] static bool AssemblyConditionMethod () { throw new NotImplementedException (); } + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4.1", + "ret", + })] static bool TypeConditionMethod () { throw new NotImplementedException (); } + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4.0", + "ret", + })] static bool MethodConditionMethod () { throw new NotImplementedException (); diff --git a/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.cs b/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.cs new file mode 100644 index 000000000..8cb9b3621 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.cs @@ -0,0 +1,136 @@ +using System; +using System.Runtime.CompilerServices; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.Substitutions +{ + [SetupLinkerSubstitutionFile ("StubBodyWithStaticCtor.xml")] + [SetupCompileArgument ("/optimize+")] + [SetupLinkerArgument ("--enable-opt", "ipconstprop")] + public class StubBodyWithStaticCtor + { + public static void Main () + { + TestMethod_1 (); + TestMethod_2 (); + TestMethod_3 (); + } + + [Kept] + [ExpectedInstructionSequence (new[] { + "call System.Int32 Mono.Linker.Tests.Cases.Substitutions.StubBodyWithStaticCtorImpl::TestMethod()", + "ldc.i4.2", + "beq.s il_8", + "ldc.i4.3", + "ret", + })] + static int TestMethod_1 () + { + if (StubBodyWithStaticCtorImpl.TestMethod () != 2) { + Console.WriteLine (); + return 1; + } + + return 3; + } + + [Kept] + [ExpectedInstructionSequence (new[] { + "call System.Int32 Mono.Linker.Tests.Cases.Substitutions.IntermediateClass::GetValue()", + "ldc.i4.2", + "beq.s il_8", + "ldc.i4.3", + "ret", + })] + static int TestMethod_2 () + { + if (IntermediateClass.GetValue () != 2) { + Console.WriteLine (); + return 1; + } + + return 3; + } + + [Kept] + [ExpectedInstructionSequence (new[] { + "call System.Boolean Mono.Linker.Tests.Cases.Substitutions.WrappingClass::GetValue()", + "brfalse.s il_7", + "ldc.i4.3", + "ret", + })] + static int TestMethod_3 () + { + if (WrappingClass.GetValue ()) { + Console.WriteLine (); + return 1; + } + + return 3; + } + } + + [Kept] + class WrappingClass + { + [Kept] + public static bool GetValue () + { + return Settings.TestValue (); + } + + static class Settings + { + [Kept] + static Settings () + { + Console.WriteLine (); + } + + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4.0", + "ret", + })] + public static bool TestValue () + { + throw new NotImplementedException (); + } + } + } + + [Kept] + class StubBodyWithStaticCtorImpl + { + [Kept] + public static int count; + + [Kept] + static StubBodyWithStaticCtorImpl () + { + count = 100; + } + + [Kept] + [ExpectedInstructionSequence (new[] { + "ldc.i4 0x2", + "ret", + })] + public static int TestMethod () + { + ++count; + return Environment.ExitCode; + } + } + + [Kept] + class IntermediateClass + { + [Kept] + public static int GetValue () + { + return StubBodyWithStaticCtorImpl.TestMethod (); + } + } +} diff --git a/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.xml b/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.xml new file mode 100644 index 000000000..5fb03c8c4 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Substitutions/StubBodyWithStaticCtor.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + -- cgit v1.2.3