-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathCoveredContract.cs
337 lines (291 loc) · 11.2 KB
/
CoveredContract.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Testing.Coverage.Formats;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Neo.SmartContract.Testing.Coverage
{
[DebuggerDisplay("{Name}")]
public class CoveredContract : CoverageBase
{
#region Internal
private readonly SortedDictionary<int, CoverageHit> _lines = new();
private readonly SortedDictionary<int, CoverageBranch> _branches = new();
#endregion
/// <summary>
/// Contract name
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Contract Hash
/// </summary>
public UInt160 Hash { get; }
/// <summary>
/// Methods
/// </summary>
public CoveredMethod[] Methods { get; private set; }
/// <summary>
/// Coverage Lines
/// </summary>
public override IEnumerable<CoverageHit> Lines => _lines.Values;
/// <summary>
/// Coverage Branches
/// </summary>
public override IEnumerable<CoverageBranch> Branches => _branches.Values;
/// <summary>
/// CoveredContract
/// </summary>
/// <param name="mechanism">Method detection mechanism</param>
/// <param name="hash">Hash</param>
/// <param name="state">Contract state</param>
public CoveredContract(MethodDetectionMechanism mechanism, UInt160 hash, ContractState? state)
{
Hash = hash;
Methods = Array.Empty<CoveredMethod>();
if (state is not null)
{
Name = state.Manifest.Name + $" [{Hash}]";
// Extract all methods
GenerateMethods(mechanism, state);
}
else
{
Name = Hash.ToString();
}
}
internal void GenerateMethods(MethodDetectionMechanism mechanism, ContractState state)
{
Name = state.Manifest.Name + $" [{Hash}]";
Script script = state.Script;
HashSet<int> privateAdded = new();
List<ContractMethodDescriptor> methods = new(state.Manifest.Abi.Methods);
// Iterate all valid instructions
int ip = 0;
while (ip < script.Length)
{
var instruction = script.GetInstruction(ip);
if (!_lines.ContainsKey(ip))
{
AddLine(instruction, new CoverageHit(ip, CoverageHit.DescriptionFromInstruction(instruction, state.Nef.Tokens), false));
}
if (mechanism == MethodDetectionMechanism.NextMethod)
{
// Find private methods
switch (instruction.OpCode)
{
case OpCode.CALL_L:
{
var offset = ip + instruction.TokenI32;
if (privateAdded.Add(offset))
{
methods.Add(new ContractMethodDescriptor()
{
Name = "_private" + offset,
Offset = offset,
ReturnType = ContractParameterType.Void,
Safe = false,
Parameters = Array.Empty<ContractParameterDefinition>(),
});
}
break;
}
case OpCode.CALLT:
{
var offset = ip + instruction.TokenI8;
if (privateAdded.Add(offset))
{
methods.Add(new ContractMethodDescriptor()
{
Name = "_private" + offset,
Offset = offset,
ReturnType = ContractParameterType.Void,
Safe = false,
Parameters = Array.Empty<ContractParameterDefinition>(),
});
}
break;
}
}
}
ip += instruction.Size;
}
Methods = methods
.Select(s => CreateMethod(mechanism, script, methods, s))
.OrderBy(o => o.Offset)
.ToArray()!;
}
private void AddLine(Instruction instruction, CoverageHit hit)
{
_lines[hit.Offset] = hit;
// Check if we should add a branc
switch (instruction.OpCode)
{
case OpCode.JMPIF:
case OpCode.JMPIF_L:
case OpCode.JMPIFNOT:
case OpCode.JMPIFNOT_L:
case OpCode.JMPEQ:
case OpCode.JMPEQ_L:
case OpCode.JMPNE:
case OpCode.JMPNE_L:
case OpCode.JMPGT:
case OpCode.JMPGT_L:
case OpCode.JMPGE:
case OpCode.JMPGE_L:
case OpCode.JMPLT:
case OpCode.JMPLT_L:
case OpCode.JMPLE:
case OpCode.JMPLE_L:
{
_branches[hit.Offset] = new CoverageBranch(hit.Offset, hit.OutOfScript);
break;
}
}
}
private CoveredMethod CreateMethod(
MethodDetectionMechanism mechanism, Script script,
List<ContractMethodDescriptor> allMethods, ContractMethodDescriptor abiMethod
)
{
int ip = abiMethod.Offset;
var to = script.Length - 1;
switch (mechanism)
{
case MethodDetectionMechanism.FindRET:
{
while (ip < script.Length)
{
var instruction = script.GetInstruction(ip);
if (instruction.OpCode == OpCode.RET) break;
ip += instruction.Size;
to = ip;
}
break;
}
case MethodDetectionMechanism.NextMethod:
case MethodDetectionMechanism.NextMethodInAbi:
{
var next = allMethods.OrderBy(u => u.Offset).Where(u => u.Offset > abiMethod.Offset).FirstOrDefault();
if (next is not null) to = next.Offset - 1;
break;
}
}
// Return method coverage
return new CoveredMethod(this, abiMethod, to - abiMethod.Offset);
}
/// <summary>
/// Get method coverage
/// </summary>
/// <param name="methodName">Method name</param>
/// <param name="pcount">Parameter count</param>
/// <returns>CoveredMethod</returns>
public CoveredMethod? GetCoverage(string methodName, int pcount)
{
return Methods.FirstOrDefault(m => m.Method.Name == methodName && m.Method.PCount == pcount);
}
/// <summary>
/// Get method coverage
/// </summary>
/// <param name="method">Method</param>
/// <returns>CoveredMethod</returns>
public CoveredMethod? GetCoverage(AbiMethod? method = null)
{
if (method is null) return null;
return Methods.FirstOrDefault(m => m.Method.Equals(method));
}
internal bool TryGetLine(int offset, [NotNullWhen(true)] out CoverageHit? lineHit)
{
return _lines.TryGetValue(offset, out lineHit);
}
internal bool TryGetBranch(int offset, [NotNullWhen(true)] out CoverageBranch? branch)
{
return _branches.TryGetValue(offset, out branch);
}
/// <summary>
/// Join coverage
/// </summary>
/// <param name="coverage">Coverage</param>
public void Join(CoverageBase? coverage)
{
if (coverage is null) return;
// Join the coverage lines
foreach (var c in coverage.Lines)
{
if (c.Hits == 0) continue;
lock (_lines)
{
if (_lines.TryGetValue(c.Offset, out var kvpValue))
{
kvpValue.Hit(c);
}
else
{
_lines.Add(c.Offset, c.Clone());
}
}
}
// Join the coverage branches
foreach (var c in coverage.Branches)
{
if (c.Hits == 0) continue;
lock (_branches)
{
if (_branches.TryGetValue(c.Offset, out var kvpValue))
{
kvpValue.Hit(c);
}
else
{
_branches.Add(c.Offset, c.Clone());
}
}
}
}
/// <summary>
/// Dump coverage
/// </summary>
/// <param name="format">Format</param>
/// <returns>Coverage dump</returns>
public override string Dump(DumpFormat format = DumpFormat.Console)
{
return format switch
{
DumpFormat.Console => new ConsoleFormat(this).Dump(),
DumpFormat.Html => new IntructionHtmlFormat(this).Dump(),
_ => throw new NotImplementedException(),
};
}
/// <summary>
/// Hit
/// </summary>
/// <param name="instructionPointer">Instruction pointer</param>
/// <param name="instruction">Instruction</param>
/// <param name="gas">Gas</param>
/// <param name="branchPath">Branch path</param>
public void Hit(int instructionPointer, Instruction instruction, long gas, bool? branchPath)
{
lock (_lines)
{
if (!_lines.TryGetValue(instructionPointer, out var coverage))
{
// Note: This call is unusual, out of the expected
coverage = new(instructionPointer, CoverageHit.DescriptionFromInstruction(instruction), true);
AddLine(instruction, coverage);
}
if (branchPath is not null && _branches.TryGetValue(instructionPointer, out var branch))
{
branch.Hit(branchPath.Value);
}
coverage.Hit(gas);
}
}
/// <summary>
/// String representation
/// </summary>
/// <returns>Hash</returns>
public override string ToString() => Hash.ToString();
}
}