From a9f7fb3332b5c666d4a878d3319f63d5673d52ee Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 4 Aug 2020 17:35:38 +0800 Subject: [PATCH 1/4] fix optimizer error --- src/Neo.Compiler.MSIL/NeoModule.cs | 15 --------------- src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs | 14 ++++++-------- src/Neo.Compiler.MSIL/Program.cs | 1 - .../Utils/BuildScript.cs | 2 +- 4 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/Neo.Compiler.MSIL/NeoModule.cs b/src/Neo.Compiler.MSIL/NeoModule.cs index 4cf587d0d..316abc018 100644 --- a/src/Neo.Compiler.MSIL/NeoModule.cs +++ b/src/Neo.Compiler.MSIL/NeoModule.cs @@ -68,21 +68,6 @@ public string GenJson() json.ConvertToStringWithFormat(sb, 4); return sb.ToString(); } - - internal void ConvertFuncAddr() - { - foreach (var method in this.mapMethods.Values) - { - foreach (var code in method.body_Codes.Values) - { - if (code.code != VM.OpCode.NOP) - { - method.funcaddr = code.addr; - break; - } - } - } - } } public class NeoMethod diff --git a/src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs b/src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs index 9b5b659a6..1723cf34b 100644 --- a/src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs +++ b/src/Neo.Compiler.MSIL/Optimizer/NefOptimizer.cs @@ -70,19 +70,19 @@ public void Optimize() /// Entry points public void LoadNef(Stream stream, int[] entryPoints) { - //read all Instruction to listInst + // Read all Instruction to listInst. var listInst = new List(); - //read all Address to listAddr + // Read all Address to listAddr. var mapLabel = new Dictionary(); int labelindex = 1; - //insert EntryPointLabel + // Insert EntryPoint Label. for (var i = 0; i < entryPoints.Length; i++) { if (i > 0 && entryPoints[i - 1] == entryPoints[i]) { - //不允许出现一样的EntryPoint - throw new Exception("now allow same EntryPoint."); + // Same EntryPoints are not allowed + throw new Exception("Same EntryPoints are not allowed."); } if (!mapLabel.ContainsKey(entryPoints[i])) { @@ -116,7 +116,6 @@ public void LoadNef(Stream stream, int[] entryPoints) labelindex++; var label = new NefLabel(labelname, addr); mapLabel.Add(addr, label); - } inst.Labels[i] = mapLabel[addr].Name; @@ -124,8 +123,7 @@ public void LoadNef(Stream stream, int[] entryPoints) } } while (inst != null); - - //Add Labels and split to Methods + // Add Labels and split to Methods Methods = new List(); var curMethod = new NefMethod(); diff --git a/src/Neo.Compiler.MSIL/Program.cs b/src/Neo.Compiler.MSIL/Program.cs index 23fe1d368..f2a5f7789 100644 --- a/src/Neo.Compiler.MSIL/Program.cs +++ b/src/Neo.Compiler.MSIL/Program.cs @@ -167,7 +167,6 @@ public static int Compile(Options options) Dictionary addrConvTable = null; if (options.Optimize) { - module.ConvertFuncAddr(); HashSet entryPoints = new HashSet(); foreach (var func in module.mapMethods) { diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs index 412a8949b..81588627f 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs @@ -55,7 +55,7 @@ public void Build(Stream fs, Stream fspdb, bool optimizer) List entryPoints = new List(); foreach (var f in converterIL.outModule.mapMethods.Values) { - if (entryPoints.Contains(f.funcaddr) == false) + if (!entryPoints.Contains(f.funcaddr)) entryPoints.Add(f.funcaddr); } var opbytes = NefOptimizeTool.Optimize(finalNEF, entryPoints.ToArray(), out addrConvTable); From 3022d380baa2b8ad5daa7dacbe0003798c55fdb7 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 4 Aug 2020 12:10:30 +0200 Subject: [PATCH 2/4] Add UT --- .../TestClasses/Contract_OptimizationTest.cs | 14 ++++++++++++++ .../UnitTest_NefOptimizer.cs | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs new file mode 100644 index 000000000..0095c86ac --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs @@ -0,0 +1,14 @@ +using Neo.SmartContract.Framework.Services.Neo; +using Neo.SmartContract.Framework; + +namespace Neo.Compiler.MSIL.TestClasses +{ + public class Contract_OptimizationTest : SmartContract.Framework.SmartContract + { + private static byte[] Owner = "Ne9ipxm2sPUaetvh3ZvjhyCzRZqP355dTZ".ToScriptHash(); + public static bool Verify() + { + return Runtime.CheckWitness(Owner); + } + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs index 802186280..10e29259e 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Compiler.Optimizer; using Neo.VM; using System; @@ -10,6 +11,16 @@ namespace Neo.Compiler.MSIL [TestClass] public class UnitTest_NefOptimizer { + [TestMethod] + public void Test_OptimizerNopEntryPoint() + { + var testengine = new TestEngine(); + var build = testengine.Build("./TestClasses/Contract_OptimizationTest.cs", false, true); + + Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["name"].AsString(), "verify"); + Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["offset"].AsString(), "0"); + } + [TestMethod] public void Test_Optimize_RemoveNOPS() { From 20b5c821ca195ad45648e422bab490bcd86c6a5c Mon Sep 17 00:00:00 2001 From: ShawnYun Date: Tue, 11 Aug 2020 11:04:34 +0800 Subject: [PATCH 3/4] Update tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs Co-authored-by: Luchuan --- .../TestClasses/Contract_OptimizationTest.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs index 0095c86ac..8d195d51f 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_OptimizationTest.cs @@ -1,14 +1,15 @@ -using Neo.SmartContract.Framework.Services.Neo; -using Neo.SmartContract.Framework; - -namespace Neo.Compiler.MSIL.TestClasses +using Neo.SmartContract.Framework.Services.Neo; +using Neo.SmartContract.Framework; + +namespace Neo.Compiler.MSIL.TestClasses { - public class Contract_OptimizationTest : SmartContract.Framework.SmartContract + public class Contract_OptimizationTest : SmartContract.Framework.SmartContract { - private static byte[] Owner = "Ne9ipxm2sPUaetvh3ZvjhyCzRZqP355dTZ".ToScriptHash(); - public static bool Verify() - { - return Runtime.CheckWitness(Owner); + private static byte[] Owner = "Ne9ipxm2sPUaetvh3ZvjhyCzRZqP355dTZ".ToScriptHash(); + + public static bool Verify() + { + return Runtime.CheckWitness(Owner); } - } -} + } +} From b399e8bba9b513ea591928df3965692eb326fc7f Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 11 Aug 2020 11:18:35 +0800 Subject: [PATCH 4/4] fix jarray --- tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs index 10e29259e..6b61c955e 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NefOptimizer.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Compiler.Optimizer; +using Neo.IO.Json; using Neo.VM; using System; using System.Buffers.Binary; @@ -17,8 +18,8 @@ public void Test_OptimizerNopEntryPoint() var testengine = new TestEngine(); var build = testengine.Build("./TestClasses/Contract_OptimizationTest.cs", false, true); - Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["name"].AsString(), "verify"); - Assert.AreEqual(build.finalABI["methods"].AsList()[0].asDict()["offset"].AsString(), "0"); + Assert.AreEqual((build.finalABI["methods"] as JArray)[0]["name"].AsString(), "verify"); + Assert.AreEqual((build.finalABI["methods"] as JArray)[0]["offset"].AsString(), "0"); } [TestMethod]