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

Refator: Entry point analyser #971

Merged
merged 4 commits into from
Mar 1, 2024
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
88 changes: 88 additions & 0 deletions src/Neo.Compiler.CSharp/Optimizer/Analysers/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Optimizer
{
public enum EntryType
{
PublicMethod,
Initialize,
PUSHA,
}

public static class EntryPoint
{
/// <summary>
///
/// </summary>
/// <param name="nef"></param>
/// <param name="manifest"></param>
/// <param name="debugInfo"></param>
/// <returns>(addr -> EntryType, hasCallA)</returns>
public static Dictionary<int, EntryType> EntryPointsByMethod(ContractManifest manifest, JToken debugInfo)
{
Dictionary<int, EntryType> result = new();
foreach (ContractMethodDescriptor method in manifest.Abi.Methods)
{
if (method.Name == "_initialize")
result.Add(method.Offset, EntryType.Initialize);
else
result.Add(method.Offset, EntryType.PublicMethod);
}
foreach (JToken? method in (JArray)debugInfo["methods"]!)
{
string name = method!["name"]!.AsString(); // NFTLoan.NFTLoan,RegisterRental
name = name[(name.LastIndexOf(',') + 1)..]; // RegisterRental
name = char.ToLower(name[0]) + name[1..]; // registerRental
if (name == "_deploy")
{
int startAddr = int.Parse(method!["range"]!.AsString().Split("-")[0]);
result[startAddr] = EntryType.Initialize; // set instead of add; _deploy may be in the manifest
}
}
return result;
}

public static Dictionary<int, EntryType> EntryPointsByCallA(NefFile nef)
{
Dictionary<int, EntryType> result = new();
Script script = nef.Script;
List<(int, Instruction)> instructions = script.EnumerateInstructions().ToList();
bool hasCallA = HasCallA(instructions);
if (hasCallA)
foreach ((int addr, Instruction instruction) in instructions)
if (instruction.OpCode == OpCode.PUSHA)
{
int target = JumpTarget.ComputeJumpTarget(addr, instruction);
if (target != addr && target >= 0)
result.Add(addr, EntryType.PUSHA);
}
return result;
}

public static bool HasCallA(List<(int, Instruction)> instructions)
{
bool hasCallA = false;
foreach ((_, Instruction instruction) in instructions)
if (instruction.OpCode == OpCode.CALLA)
{
hasCallA = true;
break;
}
return hasCallA;
}

public static bool HasCallA(NefFile nef)
{
Script script = nef.Script;
return HasCallA(script.EnumerateInstructions().ToList());
}

public static Dictionary<int, EntryType> AllEntryPoints(NefFile nef, ContractManifest manifest, JToken debugInfo)
=> EntryPointsByCallA(nef).Concat(EntryPointsByMethod(manifest, debugInfo)).ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
20 changes: 2 additions & 18 deletions src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,12 @@ public static Dictionary<int, BranchType>
foreach ((int addr, Instruction _) in script.EnumerateInstructions())
coveredMap.Add(addr, BranchType.UNCOVERED);

Dictionary<int, string> publicMethodStartingAddressToName = new();
foreach (ContractMethodDescriptor method in manifest.Abi.Methods)
publicMethodStartingAddressToName.Add(method.Offset, method.Name);

// It is unsafe to go parallel, because the coveredMap value is not true/false
//Parallel.ForEach(manifest.Abi.Methods, method =>
// CoverInstruction(method.Offset, script, coveredMap)
//);
foreach (ContractMethodDescriptor method in manifest.Abi.Methods)
CoverInstruction(method.Offset, script, coveredMap);
// start from _deploy method
foreach (JToken? method in (JArray)debugInfo["methods"]!)
{
string name = method!["name"]!.AsString(); // NFTLoan.NFTLoan,RegisterRental
name = name[(name.LastIndexOf(',') + 1)..]; // RegisterRental
name = char.ToLower(name[0]) + name[1..]; // registerRental
if (name == "_deploy")
{
int startAddr = int.Parse(method!["range"]!.AsString().Split("-")[0]);
CoverInstruction(startAddr, script, coveredMap);
}
}
foreach ((int addr, _) in EntryPoint.EntryPointsByMethod(manifest, debugInfo))
CoverInstruction(addr, script, coveredMap);
return coveredMap;
}

Expand Down
Loading