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

Ensure nef file fit in vm #2939

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 4 additions & 6 deletions src/Neo/SmartContract/NefFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Neo.Cryptography;
using Neo.IO;
using Neo.Json;
using Neo.VM;
using System;
using System.Buffers.Binary;
using System.IO;
Expand Down Expand Up @@ -71,11 +72,6 @@ public class NefFile : ISerializable
/// </summary>
public uint CheckSum { get; set; }

/// <summary>
/// The maximum length of the script.
/// </summary>
public const int MaxScriptLength = 512 * 1024;

private const int HeaderSize =
sizeof(uint) + // Magic
64; // Compiler
Expand Down Expand Up @@ -108,16 +104,18 @@ private void SerializeHeader(BinaryWriter writer)

public void Deserialize(ref MemoryReader reader)
{
long startPosition = reader.Position;
if (reader.ReadUInt32() != Magic) throw new FormatException("Wrong magic");
Compiler = reader.ReadFixedString(64);
Source = reader.ReadVarString(256);
if (reader.ReadByte() != 0) throw new FormatException("Reserved bytes must be 0");
Tokens = reader.ReadSerializableArray<MethodToken>(128);
if (reader.ReadUInt16() != 0) throw new FormatException("Reserved bytes must be 0");
Script = reader.ReadVarMemory(MaxScriptLength);
Script = reader.ReadVarMemory((int)ExecutionEngineLimits.Default.MaxItemSize);
if (Script.Length == 0) throw new ArgumentException($"Script can't be empty");
CheckSum = reader.ReadUInt32();
if (CheckSum != ComputeChecksum(this)) throw new FormatException("CRC verification fail");
if (reader.Position - startPosition > ExecutionEngineLimits.Default.MaxItemSize) throw new FormatException("Max vm item size exceed");
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ public void TestContract_Create()

var script_exceedMaxLength = new NefFile()
{
Script = new byte[NefFile.MaxScriptLength - 1],
Script = new byte[ExecutionEngineLimits.Default.MaxItemSize - 50],
Source = string.Empty,
Compiler = "",
Tokens = System.Array.Empty<MethodToken>()
Tokens = Array.Empty<MethodToken>()
};
script_exceedMaxLength.CheckSum = NefFile.ComputeChecksum(nef);
shargon marked this conversation as resolved.
Show resolved Hide resolved
script_exceedMaxLength.CheckSum = NefFile.ComputeChecksum(script_exceedMaxLength);

Assert.ThrowsException<FormatException>(() => script_exceedMaxLength.ToArray().AsSerializable<NefFile>());
Assert.ThrowsException<InvalidOperationException>(() => snapshot.DeployContract(UInt160.Zero, script_exceedMaxLength.ToArray(), manifest.ToJson().ToByteArray(true)));

var script_zeroLength = System.Array.Empty<byte>();
Expand Down