Skip to content

Commit

Permalink
Merge branch 'master' into fix/neo-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuchardt88 authored Oct 13, 2024
2 parents 7fb64a6 + a326939 commit 0546aeb
Show file tree
Hide file tree
Showing 75 changed files with 2,186 additions and 178 deletions.
292 changes: 288 additions & 4 deletions benchmarks/Neo.VM.Benchmarks/Benchmarks.POC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void NeoIssue2528()
// L24: LDLOC 0
// L25: JMPIF_L L19
// L26: DROP
Run(nameof(NeoIssue2528), "VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U=");
Run("VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U=");
}

[Benchmark]
Expand Down Expand Up @@ -81,7 +81,7 @@ public void NeoVMIssue418()
// L25: DROP
// L26: ROT
// L27: DROP
Run(nameof(NeoVMIssue418), "whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU=");
Run("whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU=");
}

[Benchmark]
Expand All @@ -98,15 +98,299 @@ public void NeoIssue2723()
// L08: DUP
// L09: STSFLD 0
// L10: JMPIF L03
Run(nameof(NeoIssue2723), "VgEC0PsBAGcAAgAAEACIRV8AnUpnACTz");
Run("VgEC0PsBAGcAAgAAEACIRV8AnUpnACTz");
}

private static void Run(string name, string poc)
// Below are PoCs from issue https://github.com/neo-project/neo/issues/2723 by @dusmart
[Benchmark]
public void PoC_NewBuffer()
{
// INITSLOT 0100
// PUSHINT32 23000000
// STLOC 00
// PUSHINT32 1048576
// NEWBUFFER
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f2ffffff
// CLEAR
// RET
Run("VwEAAsDzXgF3AAIAABAAiEVvAJ13AG8AJfL///9JQA==");
}

[Benchmark]
public void PoC_Cat()
{
// INITSLOT 0100
// PUSHINT32 1048575
// NEWBUFFER
// PUSH1
// NEWBUFFER
// PUSHINT32 133333337
// STLOC 00
// OVER
// OVER
// CAT
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f5ffffff
// CLEAR
// RET
Run("VwEAAv//DwCIEYgCWYHyB3cAS0uLRW8AnXcAbwAl9f///0lA");
}

[Benchmark]
public void PoC_Left()
{
// INITSLOT 0100
// PUSHINT32 1048576
// NEWBUFFER
// PUSHINT32 133333337
// STLOC 00
// DUP
// PUSHINT32 1048576
// LEFT
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f1ffffff
// CLEAR
// RET
Run("VwEAAgAAEACIAlmB8gd3AEoCAAAQAI1FbwCddwBvACXx////SUA=");
}

[Benchmark]
public void PoC_Right()
{
// INITSLOT 0100
// PUSHINT32 1048576
// NEWBUFFER
// PUSHINT32 133333337
// STLOC 00
// DUP
// PUSHINT32 1048576
// RIGHT
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f1ffffff
// CLEAR
// RET
Run("VwEAAgAAEACIAlmB8gd3AEoCAAAQAI5FbwCddwBvACXx////SUA=");
}

[Benchmark]
public void PoC_ReverseN()
{
// INITSLOT 0100
// PUSHINT16 2040
// STLOC 00
// PUSHDATA1 aaabbbbbbbbbcccccccdddddddeeeeeeefffffff
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L cfffffff
// PUSHINT32 23000000
// STLOC 00
// PUSHINT16 2040
// REVERSEN
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f5ffffff
// CLEAR
// RET
Run("VwEAAfgHdwAMKGFhYWJiYmJiYmJiYmNjY2NjY2NkZGRkZGRkZWVlZWVlZWZmZmZmZmZvAJ13AG8AJc////8CwPNeAXcAAfgHVW8AnXcAbwAl9f///0lA");
}

[Benchmark]
public void PoC_Substr()
{
// INITSLOT 0100
// PUSHINT32 1048576
// NEWBUFFER
// PUSHINT32 133333337
// STLOC 00
// DUP
// PUSH0
// PUSHINT32 1048576
// SUBSTR
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f0ffffff
// CLEAR
// RET
Run("VwEAAgAAEACIAlmB8gd3AEoQAgAAEACMRW8AnXcAbwAl8P///0lA");
}

[Benchmark]
public void PoC_NewArray()
{
// INITSLOT 0100
// PUSHINT32 1333333337
// STLOC 00
// PUSHINT16 2040
// NEWARRAY
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f4ffffff
// RET
Run("VwEAAlkNeU93AAH4B8NFbwCddwBvACX0////QA==");
}

[Benchmark]
public void PoC_NewStruct()
{
// INITSLOT 0100
// PUSHINT32 1333333337
// STLOC 00
// PUSHINT16 2040
// NEWSTRUCT
// DROP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f4ffffff
// RET
Run("VwEAAlkNeU93AAH4B8ZFbwCddwBvACX0////QA==");
}

[Benchmark]
public void PoC_Roll()
{
// INITSLOT 0100
// PUSHINT16 2040
// STLOC 00
// PUSHDATA1 aaabbbbbbbbbcccccccdddddddeeeeeeefffffff
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L cfffffff
// PUSHINT32 23000000
// STLOC 00
// PUSHINT16 2039
// ROLL
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f5ffffff
// CLEAR
// RET
Run("VwEAAfgHdwAMKGFhYWJiYmJiYmJiYmNjY2NjY2NkZGRkZGRkZWVlZWVlZWZmZmZmZmZvAJ13AG8AJc////8CwPNeAXcAAfcHUm8AnXcAbwAl9f///0lA");
}

[Benchmark]
public void PoC_XDrop()
{
// INITSLOT 0100
// PUSHINT16 2040
// STLOC 00
// PUSHDATA1 aaabbbbbbbbbcccccccdddddddeeeeeeefffffff
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L cfffffff
// PUSHINT32 23000000
// STLOC 00
// PUSHINT16 2039
// XDROP
// DUP
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f4ffffff
// CLEAR
// RET
Run("VwEAAfgHdwAMKGFhYWJiYmJiYmJiYmNjY2NjY2NkZGRkZGRkZWVlZWVlZWZmZmZmZmZvAJ13AG8AJc////8CwPNeAXcAAfcHSEpvAJ13AG8AJfT///9JQA==");
}

[Benchmark]
public void PoC_MemCpy()
{
// INITSLOT 0100
// PUSHINT32 1048576
// NEWBUFFER
// PUSHINT32 1048576
// NEWBUFFER
// PUSHINT32 133333337
// STLOC 00
// OVER
// PUSH0
// PUSH2
// PICK
// PUSH0
// PUSHINT32 1048576
// MEMCPY
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L eeffffff
// CLEAR
// RET
Run("VwEAAgAAEACIAgAAEACIAlmB8gd3AEsQEk0QAgAAEACJbwCddwBvACXu////SUA=");
}

[Benchmark]
public void PoC_Unpack()
{
// INITSLOT 0200
// PUSHINT16 1010
// NEWARRAY
// STLOC 01
// PUSHINT32 1333333337
// STLOC 00
// LDLOC 01
// UNPACK
// CLEAR
// LDLOC 00
// DEC
// STLOC 00
// LDLOC 00
// JMPIF_L f5ffffff
// RET
Run("VwIAAfIDw3cBAlkNeU93AG8BwUlvAJ13AG8AJfX///9A");
}

[Benchmark]
public void PoC_GetScriptContainer()
{
// SYSCALL System.Runtime.GetScriptContainer
// DROP
// JMP fa
Run("QS1RCDBFIvo=");
}

private static void Run(string poc)
{
byte[] script = Convert.FromBase64String(poc);
using ExecutionEngine engine = new();
engine.LoadScript(script);
engine.Execute();

Debug.Assert(engine.State == VMState.HALT);
}
}
Expand Down
72 changes: 72 additions & 0 deletions benchmarks/Neo.VM.Benchmarks/InstructionBuilder/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Helper.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Buffers.Binary;

namespace Neo.VM.Benchmark;

public static class Helper
{
public static void RebuildOffsets(this IReadOnlyList<Instruction> instructions)
{
var offset = 0;
foreach (var instruction in instructions)
{
instruction._offset = offset;
offset += instruction.Size;
}
}

public static void RebuildOperands(this IReadOnlyList<Instruction> instructions)
{
foreach (var instruction in instructions)
{
if (instruction._target is null) continue;
bool isLong;
if (instruction._opCode >= VM.OpCode.JMP && instruction._opCode <= VM.OpCode.CALL_L)
isLong = (instruction._opCode - VM.OpCode.JMP) % 2 != 0;
else
isLong = instruction._opCode == VM.OpCode.PUSHA || instruction._opCode == VM.OpCode.CALLA || instruction._opCode == VM.OpCode.TRY_L || instruction._opCode == VM.OpCode.ENDTRY_L;
if (instruction._opCode == VM.OpCode.TRY || instruction._opCode == VM.OpCode.TRY_L)
{
var offset1 = (instruction._target._instruction?._offset - instruction._offset) ?? 0;
var offset2 = (instruction._target2!._instruction?._offset - instruction._offset) ?? 0;
if (isLong)
{
instruction._operand = new byte[sizeof(int) + sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(instruction._operand, offset1);
BinaryPrimitives.WriteInt32LittleEndian(instruction._operand.AsSpan(sizeof(int)), offset2);
}
else
{
instruction._operand = new byte[sizeof(sbyte) + sizeof(sbyte)];
var sbyte1 = checked((sbyte)offset1);
var sbyte2 = checked((sbyte)offset2);
instruction._operand[0] = unchecked((byte)sbyte1);
instruction._operand[1] = unchecked((byte)sbyte2);
}
}
else
{
int offset = instruction._target._instruction!._offset - instruction._offset;
if (isLong)
{
instruction._operand = BitConverter.GetBytes(offset);
}
else
{
var sbyte1 = checked((sbyte)offset);
instruction._operand = [unchecked((byte)sbyte1)];
}
}
}
}
}
Loading

0 comments on commit 0546aeb

Please sign in to comment.