-
Notifications
You must be signed in to change notification settings - Fork 1k
[Optimization] Parsing Smart Contract Script Analysis
#3420
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
Merged
NGDAdmin
merged 11 commits into
neo-project:master
from
cschuchardt88:fix/neo-cli-parse-scripts
Jul 30, 2024
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d56d836
Fixed Parsing Smart Contract Script Analysis
cschuchardt88 d1b01f9
Merge branch 'master' into fix/neo-cli-parse-scripts
797d076
Add more opcode outputs
cschuchardt88 a54d04a
Bug fixes
cschuchardt88 257a16f
Merge branch 'master' into fix/neo-cli-parse-scripts
cschuchardt88 616ea64
Merge branch 'master' into fix/neo-cli-parse-scripts
cschuchardt88 c31000a
Merge branch 'master' into fix/neo-cli-parse-scripts
vncoelho 830ce47
Move to ToString
shargon 6bd036e
Reorder using
shargon 360d54c
Change ToString
shargon 783c8c1
Merge branch 'master' into fix/neo-cli-parse-scripts
NGDAdmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright (C) 2015-2024 The Neo Project. | ||
| // | ||
| // VMInstruction.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 Neo.VM; | ||
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Reflection; | ||
|
|
||
| namespace Neo.CLI | ||
| { | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [DebuggerDisplay("OpCode={OpCode}, OperandSize={OperandSize}")] | ||
| internal sealed class VMInstruction : IEnumerable<VMInstruction> | ||
| { | ||
| private const int OpCodeSize = 1; | ||
|
|
||
| public int Position { get; private init; } | ||
| public OpCode OpCode { get; private init; } | ||
| public ReadOnlyMemory<byte> Operand { get; private init; } | ||
| public int OperandSize { get; private init; } | ||
| public int OperandPrefixSize { get; private init; } | ||
|
|
||
| private static readonly int[] s_operandSizeTable = new int[256]; | ||
| private static readonly int[] s_operandSizePrefixTable = new int[256]; | ||
|
|
||
| private readonly ReadOnlyMemory<byte> _script; | ||
|
|
||
| public VMInstruction(ReadOnlyMemory<byte> script, int start = 0) | ||
| { | ||
| if (script.IsEmpty) | ||
| throw new Exception("Bad Script."); | ||
|
|
||
| var opcode = (OpCode)script.Span[start]; | ||
|
|
||
| if (Enum.IsDefined(opcode) == false) | ||
| throw new InvalidDataException($"Invalid opcode at Position: {start}."); | ||
|
|
||
| OperandPrefixSize = s_operandSizePrefixTable[(int)opcode]; | ||
| OperandSize = OperandPrefixSize switch | ||
| { | ||
| 0 => s_operandSizeTable[(int)opcode], | ||
| 1 => script.Span[start + 1], | ||
| 2 => BinaryPrimitives.ReadUInt16LittleEndian(script.Span[(start + 1)..]), | ||
| 4 => unchecked((int)BinaryPrimitives.ReadUInt32LittleEndian(script.Span[(start + 1)..])), | ||
| _ => throw new InvalidDataException($"Invalid opcode prefix at Position: {start}."), | ||
| }; | ||
|
|
||
| OperandSize += OperandPrefixSize; | ||
|
|
||
| if (start + OperandSize + OpCodeSize > script.Length) | ||
| throw new IndexOutOfRangeException("Operand size exceeds end of script."); | ||
|
|
||
| Operand = script.Slice(start + OpCodeSize, OperandSize); | ||
|
|
||
| _script = script; | ||
| OpCode = opcode; | ||
| Position = start; | ||
| } | ||
|
|
||
| static VMInstruction() | ||
| { | ||
| foreach (var field in typeof(OpCode).GetFields(BindingFlags.Public | BindingFlags.Static)) | ||
| { | ||
| var attr = field.GetCustomAttribute<OperandSizeAttribute>(); | ||
| if (attr == null) continue; | ||
|
|
||
| var index = (uint)(OpCode)field.GetValue(null)!; | ||
| s_operandSizeTable[index] = attr.Size; | ||
| s_operandSizePrefixTable[index] = attr.SizePrefix; | ||
| } | ||
| } | ||
|
|
||
| public IEnumerator<VMInstruction> GetEnumerator() | ||
| { | ||
| var nip = Position + OperandSize + OpCodeSize; | ||
| yield return this; | ||
|
|
||
| VMInstruction? instruct; | ||
| for (var ip = nip; ip < _script.Length; ip += instruct.OperandSize + OpCodeSize) | ||
| yield return instruct = new VMInstruction(_script, ip); | ||
| } | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => | ||
| GetEnumerator(); | ||
|
|
||
| public T AsToken<T>(uint index = 0) | ||
| where T : unmanaged | ||
| { | ||
| var size = Unsafe.SizeOf<T>(); | ||
|
|
||
| if (size > OperandSize) | ||
| throw new ArgumentOutOfRangeException(nameof(T), $"SizeOf {typeof(T).FullName} is too big for operand. OpCode: {OpCode}."); | ||
| if (size + index > OperandSize) | ||
| throw new ArgumentOutOfRangeException(nameof(index), $"SizeOf {typeof(T).FullName} + {index} is too big for operand. OpCode: {OpCode}."); | ||
|
|
||
| var bytes = Operand[..OperandSize].ToArray(); | ||
| return Unsafe.As<byte, T>(ref bytes[index]); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.