-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathApplicationEngine.cs
More file actions
1050 lines (947 loc) · 46.8 KB
/
ApplicationEngine.cs
File metadata and controls
1050 lines (947 loc) · 46.8 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2015-2026 The Neo Project.
//
// ApplicationEngine.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.Extensions;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using Array = System.Array;
using Buffer = Neo.VM.Types.Buffer;
using VMArray = Neo.VM.Types.Array;
namespace Neo.SmartContract
{
/// <summary>
/// A virtual machine used to execute smart contracts in the NEO system.
/// </summary>
public partial class ApplicationEngine : ExecutionEngine
{
protected static readonly JumpTable DefaultJumpTable = ComposeDefaultJumpTable();
protected static readonly JumpTable NotEchidnaJumpTable = ComposeNotEchidnaJumpTable();
protected static readonly JumpTable NotGorgonJumpTable = ComposeNotGorgonJumpTable();
/// <summary>
/// The maximum cost that can be spent when a contract is executed in test mode.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS
/// </summary>
public const long TestModeGas = 20_00000000;
public delegate void OnInstanceHandlerEvent(ApplicationEngine engine);
public delegate void OnLogEvent(ApplicationEngine engine, LogEventArgs args);
public delegate void OnNotifyEvent(ApplicationEngine engine, NotifyEventArgs args);
/// <summary>
/// Triggered when a contract calls System.Runtime.Notify.
/// </summary>
public event OnNotifyEvent? Notify;
/// <summary>
/// Triggered when a contract calls System.Runtime.Log.
/// </summary>
public event OnLogEvent? Log;
/// <summary>
/// On Application Engine
/// </summary>
public static OnInstanceHandlerEvent? InstanceHandler;
private static Dictionary<uint, InteropDescriptor>? services;
// Total amount of GAS spent to execute.
// In the unit of picoGAS, 1 picoGAS = 1e-12 GAS
private readonly BigInteger _feeAmount;
private BigInteger _feeConsumed;
// Decimals for fee calculation
public const uint FeeFactor = 10000;
private Dictionary<Type, object>? states;
private readonly DataCache originalSnapshotCache;
private List<NotifyEventArgs>? notifications;
private List<IDisposable>? disposables;
private readonly Dictionary<UInt160, int> invocationCounter = new();
private readonly Dictionary<ExecutionContext, ContractTaskAwaiter> contractTasks = new();
// In the unit of picoGAS, 1 picoGAS = 1e-12 GAS
private readonly BigInteger _execFeeFactor;
// In the unit of datoshi, 1 datoshi = 1e-8 GAS
internal readonly uint StoragePrice;
private byte[] nonceData;
/// <summary>
/// Gets or sets the provider used to create the <see cref="ApplicationEngine"/>.
/// </summary>
public static IApplicationEngineProvider? Provider { get; set; }
/// <summary>
/// Gets the descriptors of all interoperable services available in NEO.
/// </summary>
public static IReadOnlyDictionary<uint, InteropDescriptor> Services => services ?? (IReadOnlyDictionary<uint, InteropDescriptor>)ImmutableDictionary<uint, InteropDescriptor>.Empty;
/// <summary>
/// The diagnostic used by the engine. This property can be <see langword="null"/>.
/// </summary>
public IDiagnostic? Diagnostic { get; }
private List<IDisposable> Disposables => disposables ??= new List<IDisposable>();
/// <summary>
/// The trigger of the execution.
/// </summary>
public TriggerType Trigger { get; }
/// <summary>
/// The container that containing the executed script. This field could be <see langword="null"/> if the contract is invoked by system.
/// </summary>
public IVerifiable? ScriptContainer { get; }
/// <summary>
/// The snapshot used to read or write data.
/// </summary>
[Obsolete("This property is deprecated. Use SnapshotCache instead.")]
public DataCache Snapshot => CurrentContext?.GetState<ExecutionContextState>().SnapshotCache ?? originalSnapshotCache;
/// <summary>
/// The snapshotcache <see cref="SnapshotCache"/> used to read or write data.
/// </summary>
public DataCache SnapshotCache => CurrentContext?.GetState<ExecutionContextState>().SnapshotCache ?? originalSnapshotCache;
/// <summary>
/// The block being persisted. This field could be <see langword="null"/> if the <see cref="Trigger"/> is <see cref="TriggerType.Verification"/>.
/// </summary>
public Block? PersistingBlock { get; }
/// <summary>
/// The <see cref="Neo.ProtocolSettings"/> used by the engine.
/// </summary>
public ProtocolSettings ProtocolSettings { get; }
/// <summary>
/// GAS spent to execute.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS, 1 GAS = 1e8 datoshi
/// </summary>
[Obsolete("This property is deprecated. Use FeeConsumed instead.")]
public long GasConsumed => FeeConsumed;
/// <summary>
/// Exec Fee Factor. In the unit of datoshi, 1 datoshi = 1e-8 GAS
/// </summary>
internal long ExecFeeFactor => (long)_execFeeFactor.DivideCeiling(FeeFactor);
/// <summary>
/// GAS spent to execute.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS, 1 GAS = 1e8 datoshi
/// </summary>
public long FeeConsumed => (long)_feeConsumed.DivideCeiling(FeeFactor);
/// <summary>
/// Exec Fee Factor. In the unit of picoGAS, 1 picoGAS = 1e-12 GAS
/// </summary>
internal BigInteger ExecFeePicoFactor => _execFeeFactor;
/// <summary>
/// The remaining GAS that can be spent in order to complete the execution.
/// In the unit of datoshi, 1 datoshi = 1e-8 GAS, 1 GAS = 1e8 datoshi
/// </summary>
public long GasLeft => (long)((_feeAmount - _feeConsumed) / FeeFactor);
/// <summary>
/// The exception that caused the execution to terminate abnormally. This field could be <see langword="null"/> if no exception is thrown.
/// </summary>
public Exception? FaultException { get; protected set; }
/// <summary>
/// The script hash of the current context. This field could be <see langword="null"/> if no context is loaded to the engine.
/// </summary>
public UInt160? CurrentScriptHash => CurrentContext?.GetScriptHash();
/// <summary>
/// The script hash of the calling contract. This field could be <see langword="null"/> if the current context is the entry context.
/// </summary>
public virtual UInt160? CallingScriptHash
{
get
{
if (CurrentContext is null) return null;
var state = CurrentContext.GetState<ExecutionContextState>();
return state.NativeCallingScriptHash ?? state.CallingContext?.GetState<ExecutionContextState>().ScriptHash;
}
}
/// <summary>
/// The script hash of the entry context. This field could be <see langword="null"/> if no context is loaded to the engine.
/// </summary>
public virtual UInt160? EntryScriptHash => EntryContext?.GetScriptHash();
/// <summary>
/// The notifications sent during the execution.
/// </summary>
public IReadOnlyList<NotifyEventArgs> Notifications => notifications ?? (IReadOnlyList<NotifyEventArgs>)Array.Empty<NotifyEventArgs>();
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationEngine"/> class.
/// </summary>
/// <param name="trigger">The trigger of the execution.</param>
/// <param name="container">The container of the script.</param>
/// <param name="snapshotCache">The snapshot used by the engine during execution.</param>
/// <param name="persistingBlock">
/// The block being persisted.
/// It should be <see langword="null"/> if the <paramref name="trigger"/> is <see cref="TriggerType.Verification"/>.
/// </param>
/// <param name="settings">The <see cref="Neo.ProtocolSettings"/> used by the engine.</param>
/// <param name="gas">
/// The maximum gas, in the unit of datoshi, used in this execution.
/// The execution will fail when the gas is exhausted.
/// </param>
/// <param name="diagnostic">The diagnostic to be used by the <see cref="ApplicationEngine"/>.</param>
/// <param name="jumpTable">The jump table to be used by the <see cref="ApplicationEngine"/>.</param>
protected ApplicationEngine(
TriggerType trigger, IVerifiable? container, DataCache snapshotCache, Block? persistingBlock,
ProtocolSettings settings, long gas, IDiagnostic? diagnostic = null, JumpTable? jumpTable = null)
: base(jumpTable ?? DefaultJumpTable)
{
Trigger = trigger;
ScriptContainer = container;
originalSnapshotCache = snapshotCache;
PersistingBlock = persistingBlock;
ProtocolSettings = settings;
_feeAmount = gas * FeeFactor; // PicoGAS
Diagnostic = diagnostic;
nonceData = container is Transaction tx ? tx.Hash.ToArray()[..16] : new byte[16];
if (snapshotCache is null || persistingBlock?.Index == 0)
{
_execFeeFactor = PolicyContract.DefaultExecFeeFactor * FeeFactor; // Add fee decimals
StoragePrice = PolicyContract.DefaultStoragePrice;
}
else
{
var persistingIndex = persistingBlock?.Index ?? NativeContract.Ledger.CurrentIndex(snapshotCache);
if (settings == null || !settings.IsHardforkEnabled(Hardfork.HF_Faun, persistingIndex))
{
// The values doesn't have the decimals stored
_execFeeFactor = NativeContract.Policy.GetExecFeeFactor(this) * FeeFactor;
}
else
{
// The values have the decimals stored starting from OnPersist of Faun's block.
_execFeeFactor = NativeContract.Policy.GetExecPicoFeeFactor(this);
if (trigger == TriggerType.OnPersist && persistingIndex > 0 && !settings.IsHardforkEnabled(Hardfork.HF_Faun, persistingIndex - 1))
_execFeeFactor *= FeeFactor;
}
StoragePrice = NativeContract.Policy.GetStoragePrice(snapshotCache);
}
if (persistingBlock is not null)
{
ref ulong nonce = ref Unsafe.As<byte, ulong>(ref nonceData[0]);
nonce ^= persistingBlock.Nonce;
}
diagnostic?.Initialized(this);
}
#region JumpTable
private static JumpTable ComposeDefaultJumpTable()
{
var table = new JumpTable();
table[OpCode.SYSCALL] = OnSysCall;
table[OpCode.CALLT] = OnCallT;
return table;
}
public static JumpTable ComposeNotEchidnaJumpTable()
{
var table = ComposeNotGorgonJumpTable();
table[OpCode.SUBSTR] = VulnerableSubStr;
return table;
}
public static JumpTable ComposeNotGorgonJumpTable()
{
var table = ComposeDefaultJumpTable();
// Before https://github.com/neo-project/neo-vm/pull/543
table[OpCode.HASKEY] = HasKey_Before543;
table[OpCode.PICKITEM] = PickItem_Before543;
table[OpCode.SETITEM] = SetItem_Before543;
table[OpCode.REMOVE] = Remove_Before543;
return table;
}
private static void Remove_Before543(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new InvalidOperationException($"The index of {nameof(VMArray)} is out of range, {index}/[0, {array.Count}).");
array.RemoveAt(index);
break;
case Map map:
map.Remove(key);
break;
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
private static void SetItem_Before543(ExecutionEngine engine, Instruction instruction)
{
var value = engine.Pop();
if (value is Struct s) value = s.Clone(engine.Limits);
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new CatchableException($"The index of {nameof(VMArray)} is out of range, {index}/[0, {array.Count}).");
array[index] = value;
break;
}
case Map map:
{
map[key] = value;
break;
}
case VM.Types.Buffer buffer:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= buffer.Size)
throw new CatchableException($"The index of {nameof(Buffer)} is out of range, {index}/[0, {buffer.Size}).");
if (value is not PrimitiveType p)
throw new InvalidOperationException($"Only primitive type values can be set in {nameof(Buffer)} in {instruction.OpCode}.");
var b = (int)p.GetInteger();
if (b < sbyte.MinValue || b > byte.MaxValue)
throw new InvalidOperationException($"Overflow in {instruction.OpCode}, {b} is not a byte type.");
buffer.InnerBuffer.Span[index] = (byte)b;
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
private static void PickItem_Before543(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
switch (x)
{
case VMArray array:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= array.Count)
throw new CatchableException($"The index of {nameof(VMArray)} is out of range, {index}/[0, {array.Count}).");
engine.Push(array[index]);
break;
}
case Map map:
{
if (!map.TryGetValue(key, out var value))
throw new CatchableException($"Key {key} not found in {nameof(Map)}.");
engine.Push(value);
break;
}
case PrimitiveType primitive:
{
var byteArray = primitive.GetSpan();
var index = (int)key.GetInteger();
if (index < 0 || index >= byteArray.Length)
throw new CatchableException($"The index of {nameof(PrimitiveType)} is out of range, {index}/[0, {byteArray.Length}).");
engine.Push((BigInteger)byteArray[index]);
break;
}
case Buffer buffer:
{
var index = (int)key.GetInteger();
if (index < 0 || index >= buffer.Size)
throw new CatchableException($"The index of {nameof(Buffer)} is out of range, {index}/[0, {buffer.Size}).");
engine.Push((BigInteger)buffer.InnerBuffer.Span[index]);
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
private static void HasKey_Before543(ExecutionEngine engine, Instruction instruction)
{
var key = engine.Pop<PrimitiveType>();
var x = engine.Pop();
// Check the type of the top item and perform the corresponding action.
switch (x)
{
// For arrays, check if the index is within bounds and push the result onto the stack.
case VMArray array:
{
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative index {index} is invalid for OpCode {instruction.OpCode}.");
engine.Push(index < array.Count);
break;
}
// For maps, check if the key exists and push the result onto the stack.
case Map map:
{
engine.Push(map.ContainsKey(key));
break;
}
// For buffers, check if the index is within bounds and push the result onto the stack.
case VM.Types.Buffer buffer:
{
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative index {index} is invalid for OpCode {instruction.OpCode}.");
engine.Push(index < buffer.Size);
break;
}
// For byte strings, check if the index is within bounds and push the result onto the stack.
case ByteString array:
{
var index = (int)key.GetInteger();
if (index < 0)
throw new InvalidOperationException($"The negative index {index} is invalid for OpCode {instruction.OpCode}.");
engine.Push(index < array.Size);
break;
}
default:
throw new InvalidOperationException($"Invalid type for {instruction.OpCode}: {x.Type}");
}
}
protected static void OnCallT(ExecutionEngine engine, Instruction instruction)
{
if (engine is ApplicationEngine app)
{
uint tokenId = instruction.TokenU16;
app.ValidateCallFlags(CallFlags.ReadStates | CallFlags.AllowCall);
ContractState? contract = app.CurrentContext!.GetState<ExecutionContextState>().Contract;
if (contract is null || tokenId >= contract.Nef.Tokens.Length)
throw new InvalidOperationException();
MethodToken token = contract.Nef.Tokens[tokenId];
if (token.ParametersCount > app.CurrentContext.EvaluationStack.Count)
throw new InvalidOperationException();
StackItem[] args = new StackItem[token.ParametersCount];
for (int i = 0; i < token.ParametersCount; i++)
args[i] = app.Pop();
app.CallContractInternal(token.Hash, token.Method, token.CallFlags, token.HasReturnValue, args);
}
else
{
throw new InvalidOperationException();
}
}
protected static void OnSysCall(ExecutionEngine engine, Instruction instruction)
{
if (engine is ApplicationEngine app)
{
var interop = GetInteropDescriptor(instruction.TokenU32);
if (interop.Hardfork != null && !app.IsHardforkEnabled(interop.Hardfork.Value))
{
// The syscall is not active
throw new KeyNotFoundException();
}
app.OnSysCall(interop);
}
else
{
throw new InvalidOperationException();
}
}
#endregion
/// <summary>
/// Adds GAS to <see cref="FeeConsumed"/> and checks if it has exceeded the maximum limit.
/// </summary>
/// <param name="picoGas">The amount of GAS, in the unit of picoGAS, 1 picoGAS = 1e-12 GAS, to be added.</param>
protected internal void AddFee(BigInteger picoGas)
{
// Check whitelist
if (CurrentContext?.GetState<ExecutionContextState>()?.WhiteListed == true)
{
// The execution is whitelisted
return;
}
_feeConsumed = _feeConsumed + picoGas;
if (_feeConsumed > _feeAmount)
throw new InvalidOperationException("Insufficient GAS.");
}
protected override void OnFault(Exception ex)
{
FaultException = ex;
notifications = null;
base.OnFault(ex);
}
internal void Throw(Exception ex)
{
OnFault(ex);
}
private ExecutionContext CallContractInternal(UInt160 contractHash, string method, CallFlags flags, bool hasReturnValue, StackItem[] args)
{
ContractState? contract = NativeContract.ContractManagement.GetContract(SnapshotCache, contractHash);
if (contract is null) throw new InvalidOperationException($"Called Contract Does Not Exist: {contractHash}");
ContractMethodDescriptor? md = contract.Manifest.Abi.GetMethod(method, args.Length);
if (md is null) throw new InvalidOperationException($"Method \"{method}\" with {args.Length} parameter(s) doesn't exist in the contract {contractHash}.");
return CallContractInternal(contract, md, flags, hasReturnValue, args);
}
private ExecutionContext CallContractInternal(ContractState contract, ContractMethodDescriptor method, CallFlags flags, bool hasReturnValue, IReadOnlyList<StackItem> args)
{
if (NativeContract.Policy.IsBlocked(SnapshotCache, contract.Hash))
throw new InvalidOperationException($"The contract {contract.Hash} has been blocked.");
ExecutionContext currentContext = CurrentContext!;
ExecutionContextState state = currentContext.GetState<ExecutionContextState>();
if (method.Safe)
{
flags &= ~(CallFlags.WriteStates | CallFlags.AllowNotify);
}
else
{
var executingContract = IsHardforkEnabled(Hardfork.HF_Domovoi)
? state.Contract // use executing contract state to avoid possible contract update/destroy side-effects, ref. https://github.com/neo-project/neo/pull/3290.
: NativeContract.ContractManagement.GetContract(SnapshotCache, CurrentScriptHash!);
if (executingContract?.CanCall(contract, method.Name) == false)
throw new InvalidOperationException($"Cannot Call Method {method.Name} Of Contract {contract.Hash} From Contract {CurrentScriptHash}");
}
if (invocationCounter.TryGetValue(contract.Hash, out var counter))
{
invocationCounter[contract.Hash] = counter + 1;
}
else
{
invocationCounter[contract.Hash] = 1;
}
CallFlags callingFlags = state.CallFlags;
if (args.Count != method.Parameters.Length) throw new InvalidOperationException($"Method {method} Expects {method.Parameters.Length} Arguments But Receives {args.Count} Arguments");
if (hasReturnValue ^ (method.ReturnType != ContractParameterType.Void)) throw new InvalidOperationException("The return value type does not match.");
var contextNew = LoadContract(contract, method, flags & callingFlags);
state = contextNew.GetState<ExecutionContextState>();
state.CallingContext = currentContext;
// Check whitelist
if (IsHardforkEnabled(Hardfork.HF_Faun) &&
NativeContract.Policy.IsWhitelistFeeContract(SnapshotCache, contract.Hash, method, out var fixedFee))
{
AddFee(fixedFee.Value * FeeFactor);
state.WhiteListed = true;
}
for (int i = args.Count - 1; i >= 0; i--)
contextNew.EvaluationStack.Push(args[i]);
return contextNew;
}
internal ContractTask CallFromNativeContractAsync(UInt160 callingScriptHash, UInt160 hash, string method, params StackItem[] args)
{
Diagnostic?.CallFromNative(hash, method, args);
var contextNew = CallContractInternal(hash, method, CallFlags.All, false, args);
var state = contextNew.GetState<ExecutionContextState>();
state.NativeCallingScriptHash = callingScriptHash;
ContractTask task = new();
contractTasks.Add(contextNew, task.GetAwaiter());
return task;
}
internal ContractTask<T> CallFromNativeContractAsync<T>(UInt160 callingScriptHash, UInt160 hash, string method, params StackItem[] args)
{
Diagnostic?.CallFromNative(hash, method, args);
var contextNew = CallContractInternal(hash, method, CallFlags.All, true, args);
var state = contextNew.GetState<ExecutionContextState>();
state.NativeCallingScriptHash = callingScriptHash;
ContractTask<T> task = new();
contractTasks.Add(contextNew, task.GetAwaiter());
return task;
}
protected override void ContextUnloaded(ExecutionContext context)
{
base.ContextUnloaded(context);
if (context.Script != CurrentContext?.Script)
{
ExecutionContextState state = context.GetState<ExecutionContextState>();
if (UncaughtException is null)
{
state.SnapshotCache?.Commit();
if (CurrentContext != null)
{
ExecutionContextState contextState = CurrentContext.GetState<ExecutionContextState>();
contextState.NotificationCount += state.NotificationCount;
if (state.IsDynamicCall)
{
if (context.EvaluationStack.Count == 0)
Push(StackItem.Null);
else if (context.EvaluationStack.Count > 1)
throw new NotSupportedException("Multiple return values are not allowed in cross-contract calls.");
}
}
}
else
{
if (state.NotificationCount > 0)
notifications!.RemoveRange(notifications.Count - state.NotificationCount, state.NotificationCount);
}
}
Diagnostic?.ContextUnloaded(context);
if (contractTasks.Remove(context, out var awaiter))
{
if (UncaughtException is not null)
throw new VMUnhandledException(UncaughtException);
awaiter.SetResult(this);
}
}
/// <summary>
/// Use the loaded <see cref="IApplicationEngineProvider"/> to create a new instance of the <see cref="ApplicationEngine"/> class.
/// If no <see cref="IApplicationEngineProvider"/> is loaded, the constructor of <see cref="ApplicationEngine"/> will be called.
/// </summary>
/// <param name="trigger">The trigger of the execution.</param>
/// <param name="container">The container of the script.</param>
/// <param name="snapshot">The snapshot used by the engine during execution.</param>
/// <param name="persistingBlock">
/// The block being persisted.
/// It should be <see langword="null"/> if the <paramref name="trigger"/> is <see cref="TriggerType.Verification"/>.
/// </param>
/// <param name="settings">The <see cref="Neo.ProtocolSettings"/> used by the engine.</param>
/// <param name="gas">
/// The maximum gas used in this execution, in the unit of datoshi.
/// The execution will fail when the gas is exhausted.
/// </param>
/// <param name="diagnostic">The diagnostic to be used by the <see cref="ApplicationEngine"/>.</param>
/// <returns>The engine instance created.</returns>
public static ApplicationEngine Create(TriggerType trigger, IVerifiable? container, DataCache snapshot, Block? persistingBlock = null, ProtocolSettings? settings = null, long gas = TestModeGas, IDiagnostic? diagnostic = null)
{
var index = persistingBlock?.Index ?? NativeContract.Ledger.CurrentIndex(snapshot);
settings ??= ProtocolSettings.Default;
// Adjust jump table according persistingBlock
JumpTable jumpTable;
if (settings.IsHardforkEnabled(Hardfork.HF_Gorgon, index))
{
jumpTable = DefaultJumpTable;
}
else
{
if (!settings.IsHardforkEnabled(Hardfork.HF_Echidna, index))
{
jumpTable = NotEchidnaJumpTable;
}
else
{
jumpTable = NotGorgonJumpTable;
}
}
var engine = Provider?.Create(trigger, container, snapshot, persistingBlock, settings, gas, diagnostic, jumpTable)
?? new ApplicationEngine(trigger, container, snapshot, persistingBlock, settings, gas, diagnostic, jumpTable);
InstanceHandler?.Invoke(engine);
return engine;
}
/// <summary>
/// Extracts a substring from the specified buffer and pushes it onto the evaluation stack.
/// <see cref="OpCode.SUBSTR"/>
/// </summary>
/// <param name="engine">The execution engine.</param>
/// <param name="instruction">The instruction being executed.</param>
/// <remarks>Pop 3, Push 1</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void VulnerableSubStr(ExecutionEngine engine, Instruction instruction)
{
var count = (int)engine.Pop().GetInteger();
if (count < 0)
throw new InvalidOperationException($"The count can not be negative for {nameof(OpCode.SUBSTR)}, count: {count}.");
var index = (int)engine.Pop().GetInteger();
if (index < 0)
throw new InvalidOperationException($"The index can not be negative for {nameof(OpCode.SUBSTR)}, index: {index}.");
var x = engine.Pop().GetSpan();
// Note: here it's the main change
if (index + count > x.Length)
throw new InvalidOperationException($"The index + count is out of range for {nameof(OpCode.SUBSTR)}, index: {index}, count: {count}, {index + count}/[0, {x.Length}].");
Buffer result = new(count, false);
x.Slice(index, count).CopyTo(result.InnerBuffer.Span);
engine.Push(result);
}
public override void LoadContext(ExecutionContext context)
{
// Set default execution context state
var state = context.GetState<ExecutionContextState>();
state.ScriptHash ??= ((ReadOnlyMemory<byte>)context.Script).Span.ToScriptHash();
invocationCounter.TryAdd(state.ScriptHash, 1);
base.LoadContext(context);
Diagnostic?.ContextLoaded(context);
}
/// <summary>
/// Loads a deployed contract to the invocation stack. If the _initialize method is found on the contract, loads it as well.
/// </summary>
/// <param name="contract">The contract to be loaded.</param>
/// <param name="method">The method of the contract to be called.</param>
/// <param name="callFlags">The <see cref="CallFlags"/> used to call the method.</param>
/// <returns>The loaded context.</returns>
public ExecutionContext LoadContract(ContractState contract, ContractMethodDescriptor method, CallFlags callFlags)
{
ExecutionContext context = LoadScript(contract.Script,
rvcount: method.ReturnType == ContractParameterType.Void ? 0 : 1,
initialPosition: method.Offset,
configureState: p =>
{
p.CallFlags = callFlags;
p.ScriptHash = contract.Hash;
p.Contract = new ContractState
{
Id = contract.Id,
UpdateCounter = contract.UpdateCounter,
Hash = contract.Hash,
Nef = contract.Nef,
Manifest = contract.Manifest
};
});
// Call initialization
var init = contract.Manifest.Abi.GetMethod(ContractBasicMethod.Initialize, ContractBasicMethod.InitializePCount);
if (init is not null)
{
LoadContext(context.Clone(init.Offset));
}
return context;
}
/// <summary>
/// Loads a script to the invocation stack.
/// </summary>
/// <param name="script">The script to be loaded.</param>
/// <param name="rvcount">The number of return values of the script.</param>
/// <param name="initialPosition">The initial position of the instruction pointer.</param>
/// <param name="configureState">The action used to configure the state of the loaded context.</param>
/// <returns>The loaded context.</returns>
public ExecutionContext LoadScript(Script script, int rvcount = -1, int initialPosition = 0, Action<ExecutionContextState>? configureState = null)
{
// Create and configure context
ExecutionContext context = CreateContext(script, rvcount, initialPosition);
ExecutionContextState state = context.GetState<ExecutionContextState>();
state.SnapshotCache = SnapshotCache?.CloneCache();
configureState?.Invoke(state);
// Load context
LoadContext(context);
return context;
}
/// <summary>
/// Converts an <see cref="object"/> to a <see cref="StackItem"/> that used in the virtual machine.
/// </summary>
/// <param name="value">The <see cref="object"/> to convert.</param>
/// <returns>The converted <see cref="StackItem"/>.</returns>
protected internal StackItem Convert(object? value)
{
if (value is IDisposable disposable) Disposables.Add(disposable);
return value switch
{
null => StackItem.Null,
bool b => b,
sbyte i => i,
byte i => (BigInteger)i,
short i => i,
ushort i => (BigInteger)i,
int i => i,
uint i => i,
long i => i,
ulong i => i,
Enum e => Convert(System.Convert.ChangeType(e, e.GetTypeCode())),
byte[] data => data,
ReadOnlyMemory<byte> m => m,
string s => s,
BigInteger i => i,
JObject o => o.ToByteArray(false),
IInteroperable interoperable => interoperable.ToStackItem(ReferenceCounter),
ISerializable i => i.ToArray(),
StackItem item => item,
(object a, object b) => new Struct(ReferenceCounter) { Convert(a), Convert(b) },
Array array => new VMArray(ReferenceCounter, array.OfType<object>().Select(p => Convert(p))),
_ => StackItem.FromInterface(value)
};
}
/// <summary>
/// Converts a <see cref="StackItem"/> to an <see cref="object"/> that to be used as an argument of an interoperable service or native contract.
/// </summary>
/// <param name="item">The <see cref="StackItem"/> to convert.</param>
/// <param name="descriptor">The descriptor of the parameter.</param>
/// <returns>The converted <see cref="object"/>.</returns>
protected internal object? Convert(StackItem item, InteropParameterDescriptor descriptor)
{
if (item.IsNull && !descriptor.IsNullable && descriptor.Type != typeof(StackItem))
throw new InvalidOperationException($"The argument `{descriptor.Name}` can't be null.");
descriptor.Validate(item);
if (descriptor.IsArray)
{
Array av;
if (item is VMArray array)
{
av = Array.CreateInstance(descriptor.Type.GetElementType()!, array.Count);
for (int i = 0; i < av.Length; i++)
{
if (array[i].IsNull && !descriptor.IsElementNullable)
throw new InvalidOperationException($"The element of `{descriptor.Name}` can't be null.");
av.SetValue(descriptor.Converter(array[i]), i);
}
}
else
{
int count = (int)item.GetInteger();
if (count > Limits.MaxStackSize) throw new InvalidOperationException();
av = Array.CreateInstance(descriptor.Type.GetElementType()!, count);
for (int i = 0; i < av.Length; i++)
{
StackItem popped = Pop();
if (popped.IsNull && !descriptor.IsElementNullable)
throw new InvalidOperationException($"The element of `{descriptor.Name}` can't be null.");
av.SetValue(descriptor.Converter(popped), i);
}
}
return av;
}
else
{
object? value = descriptor.Converter(item);
if (descriptor.IsEnum)
value = Enum.ToObject(descriptor.Type, value!);
else if (descriptor.IsInterface)
value = ((InteropInterface?)value)?.GetInterface<object>();
return value;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Diagnostic?.Disposed();
if (disposables != null)
{
foreach (var disposable in disposables)
disposable.Dispose();
disposables = null;
}
}
base.Dispose(disposing);
}
/// <summary>
/// Determines whether the <see cref="CallFlags"/> of the current context meets the specified requirements.
/// </summary>
/// <param name="requiredCallFlags">The requirements to check.</param>
internal protected void ValidateCallFlags(CallFlags requiredCallFlags)
{
ExecutionContextState state = CurrentContext!.GetState<ExecutionContextState>();
if (!state.CallFlags.HasFlag(requiredCallFlags))
throw new InvalidOperationException($"Cannot call this SYSCALL with the flag {state.CallFlags}.");
}
/// <summary>
/// Invokes the specified interoperable service.
/// </summary>
/// <param name="descriptor">The descriptor of the interoperable service.</param>
protected virtual void OnSysCall(InteropDescriptor descriptor)
{
ValidateCallFlags(descriptor.RequiredCallFlags);
AddFee(descriptor.FixedPrice * _execFeeFactor);
int parameterCount = descriptor.Parameters.Count;
object?[] parameters = parameterCount == 0 ? [] : ArrayPool<object?>.Shared.Rent(parameterCount);
try
{
for (int i = 0; i < parameterCount; i++)
parameters[i] = Convert(Pop(), descriptor.Parameters[i]);
object? returnValue = descriptor.Invoke(this, parameters);
if (descriptor.Handler.ReturnType != typeof(void))
Push(Convert(returnValue));
}
finally
{
if (parameterCount > 0)
{
Array.Clear(parameters, 0, parameterCount);
ArrayPool<object?>.Shared.Return(parameters);
}
}
}
protected override void PreExecuteInstruction(Instruction instruction)
{
Diagnostic?.PreExecuteInstruction(instruction);
AddFee(_execFeeFactor * OpCodePriceTable[(byte)instruction.OpCode]);
}
protected override void PostExecuteInstruction(Instruction instruction)
{
base.PostExecuteInstruction(instruction);
Diagnostic?.PostExecuteInstruction(instruction);
}
private static Block CreateDummyBlock(IReadOnlyStore snapshot, ProtocolSettings settings)
{
UInt256 hash = NativeContract.Ledger.CurrentHash(snapshot);
Block currentBlock = NativeContract.Ledger.GetBlock(snapshot, hash)!;
return new Block
{
Header = new Header
{
Version = 0,
PrevHash = hash,
MerkleRoot = new UInt256(),
Timestamp = currentBlock.Timestamp + (uint)snapshot.GetTimePerBlock(settings).TotalMilliseconds,
Index = currentBlock.Index + 1,
NextConsensus = currentBlock.NextConsensus,
Witness = Witness.Empty,
},
Transactions = [],
};
}
protected static InteropDescriptor Register(string name, string handler, long fixedPrice, CallFlags requiredCallFlags, Hardfork? hardfork = null)
{
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
var method = typeof(ApplicationEngine).GetMethod(handler, flags)
?? typeof(ApplicationEngine).GetProperty(handler, flags)?.GetMethod
?? throw new ArgumentException($"Handler {handler} is not found.", nameof(handler));
var descriptor = new InteropDescriptor()
{
Name = name,
Handler = method,
Hardfork = hardfork,
FixedPrice = fixedPrice,
RequiredCallFlags = requiredCallFlags
};
services ??= [];
services.Add(descriptor.Hash, descriptor);
return descriptor;
}
/// <summary>
/// Get Interop Descriptor
/// </summary>
/// <param name="methodHash">Method Hash</param>
/// <returns>InteropDescriptor</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static InteropDescriptor GetInteropDescriptor(uint methodHash)
{
return services![methodHash];
}
/// <summary>
/// Creates a new instance of the <see cref="ApplicationEngine"/> class, and use it to run the specified script.
/// </summary>
/// <param name="script">The script to be executed.</param>
/// <param name="snapshot">The snapshot used by the engine during execution.</param>
/// <param name="container">The container of the script.</param>
/// <param name="persistingBlock">The block being persisted.</param>
/// <param name="settings">The <see cref="Neo.ProtocolSettings"/> used by the engine.</param>
/// <param name="offset">The initial position of the instruction pointer.</param>
/// <param name="gas">The maximum gas, in the unit of datoshi, used in this execution. The execution will fail when the gas is exhausted.</param>
/// <param name="diagnostic">The diagnostic to be used by the <see cref="ApplicationEngine"/>.</param>
/// <returns>The engine instance created.</returns>
public static ApplicationEngine Run(ReadOnlyMemory<byte> script, DataCache snapshot, IVerifiable? container = null, Block? persistingBlock = null, ProtocolSettings? settings = null, int offset = 0, long gas = TestModeGas, IDiagnostic? diagnostic = null)
{
persistingBlock ??= CreateDummyBlock(snapshot, settings ?? ProtocolSettings.Default);
ApplicationEngine engine = Create(TriggerType.Application, container, snapshot, persistingBlock, settings, gas, diagnostic);