Commit graph

272 commits

Author SHA1 Message Date
Evgeniy Stratonikov
4f98ec2f53 vm: embed reference counter in compound items
```
name              old time/op  new time/op  delta
RefCounter_Add-8  44.8ns ± 4%  11.7ns ± 3%  -73.94%  (p=0.000 n=10+10)
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-08-13 14:41:26 +03:00
Roman Khimov
bdb2d24a5a vm: remove istack redirection in VM
VM always has istack and it doesn't even change, so doing this microallocation
makes no sense. Notice that estack is a bit harder to change we do replace it
in some cases and we compare pointers to it as well.
2021-08-11 14:42:01 +03:00
Roman Khimov
ff7d594bef vm: store refcounter directly in VM
VM always has it, so allocating yet another object makes no sense.
2021-08-11 13:25:58 +03:00
Roman Khimov
85936de254 vm: don't create reference counter when it's not needed
* invocation stack doesn't need refcounting
 * exception stack doesn't need refcounting
 * evaluation stack always has VM-level refcounter
2021-08-02 22:38:41 +03:00
Roman Khimov
1d8ad5b84a *: simplify some error messages
Log:
2021-07-23T09:59:18.948+0300    WARN    contract invocation failed      {"tx": "de3e3c1f1d37e4528990f894dea5583fd320485ad3862a95eb0e8823eecf4a5f", "block": 9643, "error": "error encountered at instruction 1 (SYSCALL): error during call from native: error encountered at instruction 745 (CAT): invalid conversion: Map/ByteString"}

The word "error" appears 4 times here.
2021-07-23 10:08:09 +03:00
Roman Khimov
a54e3516d1 slice: add Reverse function, deduplicate code a bit 2021-07-19 22:57:55 +03:00
Roman Khimov
1568ebc513 stackitem: rework struct cloning protection
Count everything, fail early, make it more compatible with
neo-project/neo-vm#423.
2021-07-19 15:42:42 +03:00
Roman Khimov
5f13de3a76 *: simplify some integer checks with IsUint64()
If we need a positive number we can do `IsUint64()` instead of checking that
`Int64()` is `> 0`.
2021-07-19 15:42:42 +03:00
Roman Khimov
233307aca5 stackitem: completely drop MaxArraySize
Turns out C# VM doesn't have it since preview2, so our limiting of
MaxArraySize in incompatible with it. Removing this limit shouldn't be a
problem with the reference counter we have, both APPEND and SETITEM add things
to reference counter and we can't exceed MaxStackSize. PACK on the other hand
can't get more than MaxStackSize-1 of input elements.

Unify NEWSTRUCT with NEWARRAY* and use better integer checks at the same time.

Multisig limit is still 1024.
2021-07-19 15:42:42 +03:00
Roman Khimov
15be763bb3 vm: limit POW, fix #2060
Calculating pow(pow(2, 255), 0xffffffff) takes unknown amount of time. See
also neo-project/neo-vm#422.
2021-07-19 15:42:41 +03:00
Roman Khimov
fe3c68b92d vm: panic in cloneIfStruct
It's internal, so we can deduplicate code a bit.
2021-07-12 11:52:24 +03:00
Roman Khimov
4000dd692c vm: make cloning limits a bit more effective
Take actual reference counter value into account.
2021-07-11 19:37:06 +03:00
Roman Khimov
cfe41abd35 vm: fix OOM during nested structure cloning
Resulting item can't have more than MaxStackSize elements. Technically this
limits to MaxStackSize cloned elements but it's considered to be enough to
mitigate the issue (the next size check is going to happen during push to the
stack). See neo-project/neo#2534, thanks @vang1ong7ang.
2021-07-11 18:47:50 +03:00
Evgeniy Stratonikov
1863023e27 vm: copy argument in unary opcodes, fix #2051
We already do this in binary opcodes, let's be consistent.

This fixes state difference at height 275663
for tx 3c498317684d63849b03e4c58ad57ce4b19bb206b7b01bcc64233de3b3e207f4

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
2021-07-08 17:09:04 +03:00
Anna Shaleva
e3611bfa4c vm, cli: allow to specify flags while loading VM 2021-05-28 12:07:41 +03:00
Roman Khimov
9d2712573f *: enable godot linter and fix all its warnings
It's important for NeoGo to have clean documentation. No functional changes.
2021-05-12 23:17:03 +03:00
Evgeniy Stratonikov
8fa4a576f4 vm: allow Null arguments for LT,LE,GT,GE opcodes 2021-04-29 16:10:51 +03:00
Evgeniy Stratonikov
dc393642a2 opcode: fix GTE, LTE string representations 2021-04-29 16:08:48 +03:00
Anna Shaleva
f57187e611 vm: throw unhandled exception with message instead of panic
The result is the same HALT state, but the exception message is the real
one got from user. Changes ported from C#:

1. Throw exception: 59b8ac73d2/src/neo-vm/ExecutionEngine.cs (L1448)
2. Prettify message: https://github.com/neo-project/neo-vm/blob/master/src/neo-vm/VMUnhandledException.cs#L28

The result is that instead of
```
2021-03-31T17:02:54.508+0300	WARN	contract invocation failed	{"tx": "2aefeb705f3a609df8767d9b45e036b9dd1eb77407e5732375981915668889b8", "block": 30640, "error": "error encountered at instruction 970 (THROW): runtime error: invalid memory address or nil pointer dereference"}
```

we'll get
```
2021-03-31T17:33:56.299+0300	WARN	contract invocation failed	{"tx": "2aefeb705f3a609df8767d9b45e036b9dd1eb77407e5732375981915668889b8", "block": 30640, "error": "error encountered at instruction 970 (THROW): unhandled exception: No authorization."}
```
in the node logs.
2021-03-31 19:37:52 +03:00
Anna Shaleva
793f27084b vm: specify syscall ID when panicing on syscall invocation
It's convinient to know the failing syscall without dumping
smartcontract instructions.
2021-03-31 19:00:59 +03:00
Evgeniy Stratonikov
d255c4a517 vm: implement SQRT opcode 2021-03-01 16:58:53 +03:00
Evgeniy Stratonikov
6496782736 vm: implement POW opcode 2021-03-01 16:58:53 +03:00
Roman Khimov
158f0d9d9c native/vm: add script check for deployed contracts
Refs. #1699.
2021-02-09 22:31:26 +03:00
Anna Shaleva
6a4e312eac core: move GetPrice from core to interop
We have additional logic for getting BaseExecFee policy value. This
logic should be moved to interop context instead of being in Policer,
because Policer is just an interface over Policy contract.

After moving this logic to interop context, we need to use it to define
BaseExecFee instead of (Policer).BaseExecFee. Thus, moving
(*Blockchain).GetPrice to (*Context).GetPrice is necessary.
2021-02-05 11:36:32 +03:00
Evgeniy Stratonikov
49de8161ef core: implement LoadToken handler 2021-01-22 09:04:37 +03:00
Evgenii Stratonikov
bb706aa55b vm: implement CALLT opcode 2021-01-21 19:30:04 +03:00
Evgeniy Stratonikov
324107b31e vm: implement POPITEM opcode 2021-01-19 09:46:01 +03:00
Evgenii Stratonikov
1c0c331e25 core: update System.Contract.Call syscall
1. Remove `System.Contract.CallEx`.
2. Extend number of parameters.
3. Add return value count to `VM.Context`.
2021-01-14 18:23:36 +03:00
Evgenii Stratonikov
dbe81f9b80 smartcontract: move flags to a separate package 2021-01-14 17:52:09 +03:00
Evgenii Stratonikov
e63191d31f core: hangle CallingScriptHash correctly
When using native contracts, script hash of second-to-top context
on invocation stack does not always correspond to a real calling
contract.
2020-12-10 16:52:36 +03:00
Evgenii Stratonikov
e903e40085 core: call from native contracts synchronously
Follow neo-project/neo#2130.
2020-12-10 16:43:46 +03:00
Evgenii Stratonikov
d7ffa89811 vmcli: set breakpoint before the instruction
Breakpoint should occur before actual instruction execution.
2020-12-02 10:49:37 +03:00
Evgenii Stratonikov
2f39701d76 vm: provide writer in PrintOps()
Make it more flexible and testable. Fallback to using
stdout if no writer is provided.
2020-12-02 10:49:37 +03:00
Roman Khimov
d93aa745bb contract: avoid going to the DB for entry scripts
This optimizes out DB access for non-deployed contracts under the assumption
that deployed ones are always loaded via `LoadScriptWithHash` (and if they're
not --- it's a bug anyway with the new hashing model) which actually is a very
popular case (every entry script does that).
2020-11-27 21:47:08 +03:00
Anna Shaleva
0f68528095 core: add callback to VM context 2020-11-25 18:37:29 +03:00
Roman Khimov
bf9ecc2bd3 vm: improve REVERSEITEMS for 1M Buffer
Before:
BenchmarkOpcodes/REVERSEITEMS/buffer/1M-8                   1680            758747 ns/op

After:
BenchmarkOpcodes/REVERSEITEMS/buffer/1M-8                   2649            442720 ns/op
2020-11-06 23:31:26 +03:00
Roman Khimov
2522271161 vm: use Application trigger by default
Don't mess with System, it's too powerful to be the default.
2020-10-29 19:17:07 +03:00
Roman Khimov
27a01c7759 vm: optimize stack traversal on exception handling
Before:
BenchmarkOpcodes/THROW/0/1-8       10000               506 ns/op
BenchmarkOpcodes/THROW/0/16-8      10000               524 ns/op
BenchmarkOpcodes/THROW/255/0-8     10000             49363 ns/op
BenchmarkOpcodes/THROW/1023/0-8                    10000           1628480 ns/op

After:
BenchmarkOpcodes/THROW/0/1-8       10000               575 ns/op
BenchmarkOpcodes/THROW/0/16-8      10000               516 ns/op
BenchmarkOpcodes/THROW/255/0-8     10000              8290 ns/op
BenchmarkOpcodes/THROW/1023/0-8                    10000             34605 ns/op
2020-10-15 16:20:34 +03:00
Roman Khimov
3d8434a50c vm: fix invocation stack checks
It should be done on every new context push.
2020-10-15 16:20:34 +03:00
Anna Shaleva
fe1f0a7245 core: introduce CheckReturnState constants
At the moment we should have 3 possible options to check return state
during vm context unloading:
	* no check
	* ensure the stack is empty
	* ensure the stack is not empty

It is necessary to distinguish them because new _deploy method shouldn't
left anything on stack. Example: if we use _deploy method before some
ordinary contract method which returns one value. Without these changes
the contract invocation will fail due to 2 elements on stack left after
invocation (the first `null` element is from _deploy, the second element
is return-value from the ordinary contract method).
2020-10-13 19:14:44 +03:00
Anna Shaleva
cbf89fbb19 vm: add Call method which increments invocation counter 2020-10-08 16:25:45 +03:00
Anna Shaleva
6ce00fde82 vm, core: move invocation counter from InteropContext to VM 2020-10-08 11:33:26 +03:00
Anna Shaleva
66ca654b07 vm: refactor ISNULL opcode handling 2020-09-29 18:46:20 +03:00
Anna Shaleva
543fd58e93 vm: restrict map key size 2020-09-29 18:46:15 +03:00
Roman Khimov
26339c75dc vm/core: drop old key caching system
Obsoleted by f5f58a7e91.
2020-09-10 14:43:24 +03:00
Evgenii Stratonikov
04f5fdefa0 vm: properly unload context on exception
Do not copy exception context on CALL*.
2020-08-27 10:28:50 +03:00
Evgenii Stratonikov
ab4cd8a990 vm: fix typo in MEMCPY handling 2020-08-25 08:53:29 +03:00
Roman Khimov
681ae4d5d6 vm: fix TRY offsets check
TRY can have an offset != 0 and still it can't have both parameters set to
zero.
2020-08-24 16:20:57 +03:00
Roman Khimov
32112249d5 vm: limit maximum nesting of exception contexts
Follow neo-project/neo#365. neo-vm submodule is updated just to show the
relevant latest commit, nothing really changed there.
2020-08-24 15:37:39 +03:00
Roman Khimov
324f4c265b stackitem: don't copy existing slices for TryBytes
Most often we only need to read them and it doesn't require copying. Make an
explicit copy (and copy only things we need!) where needed.

After the recent neo-vm tests update our vm package testing time jumped to
~12s, with this change it's now more like ~8s.
2020-08-22 23:36:38 +03:00