Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Fix ReferenceCounter corner cases #406

Closed

Conversation

spencercorwin
Copy link

We're working on our TypeScript smart contract compiler and we seem to have hit two corner case errors. It's quite difficult to try to trace/log exactly what's going on with all the CompoundType items our compiler creates and how they are managed in the ReferenceCounter. But this change fixes those corner cases.

Two two corner cases are:

  • A key not found error here. From our logging it appears that subitem is being removed from counter before it gets here.
  • A null reference error here where entry.ObjectReferences is null. Not sure how this happens.

Let us know if these changes are unacceptable. We're not sure if these corner cases are something we need to fix in our compiler or not. We've been looking at this problem for a bit and aren't quite sure what to do.

@ndhuutai

@erikzhang
Copy link
Member

Hi, can you post the script that reproduces the error, let me study it?

@spencercorwin
Copy link
Author

spencercorwin commented Apr 9, 2021

@erikzhang Yes. This script will hit both error cases:

5601c26023590f00004a10b327500f00004510ce4a10ce18b327eb01000011ce4ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1051d0454546584aca9dce10ce10ce10ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1051d0454546584aca9dce10ce10ce10ce1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c270a0100004b505041f354bf1d11ce504e5011c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce35dafeffff58d44510b3252600000002e20000000c05747261636512c00c0574726163654195016f611114230b000000cf2382ffffff454a11b3274a00000045584aca9dce10ce10ce1151d04545584aca9dce10ce10ce11ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1151d04545584aca9dce10ce10ce11ce1040231b0000004a12b3270c0000004545230c000000454523f5feffff46181012c64a54d04e501150d0235b0d00004a10ce1ab327cd01000011ce10c35041ed64f727504b419c08ed9c27a50100004b504b41f354bf1d11ce5141f354bf1d10ce11c00c0b646573657269616c697a650c14c0ef39cee0e4e925c6c2a06a79e1440dd86fceac41627d5b52515111c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce3572fdffff58d44510b3252600000002b50100000c05747261636512c00c0574726163654195016f61111423750000005111c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce350cfdffff58d44510b3252600000002f70100000c05747261636512c00c0574726163654195016f611114230f00000012c04b50cf23e7feffff454a11b3274a00000045584aca9dce10ce10ce1251d04545584aca9dce10ce10ce12ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1251d04545584aca9dce10ce10ce12ce1040231b0000004a12b3270c0000004545230c0000004545235afeffff461a1012c64a54d04e501150d023890b00004a10ce1bb3276601000011ce10c35041ed64f727504b419c08ed9c273e0100004b504b41f354bf1d11ce5141f354bf1d10ce11c00c0b646573657269616c697a650c14c0ef39cee0e4e925c6c2a06a79e1440dd86fceac41627d5b525151454e5011c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce359dfbffff58d44510b3252600000002d10200000c05747261636512c00c0574726163654195016f611114230b000000cf234effffff454a11b3274a00000045584aca9dce10ce10ce1351d04545584aca9dce10ce10ce13ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1351d04545584aca9dce10ce10ce13ce1040231b0000004a12b3270c0000004545230c000000454523c1feffff461b1012c64a54d04e501150d0231e0a00004a10ce17b3275d0900004a4a584aca9dce10ce10ce1451d011ce10cecc4ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1551d0454546584aca9dce10ce10ce15ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1551d0454546584aca9dce10ce10ce15ce1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c27be0000004b505041f354bf1d11ce504b0c095f5f70726f746f5f5f97aa270d0000004e50cf23060000004623ceffffff454a11b3274a00000045584aca9dce10ce10ce1651d04545584aca9dce10ce10ce16ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1651d04545584aca9dce10ce10ce16ce1040231b0000004a12b3270c0000004545230c00000045452341ffffff464ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1751d0454546584aca9dce10ce10ce17ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1751d0454546584aca9dce10ce10ce17ce1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c27080200004b505041f354bf1d11ce504e50584aca9dce10ce10ce14ce504b50db28504a11ce10ce4a5111ce10ce4a144dcbaa510c095f5f70726f746f5f5fcbab2796000000450c095f5f70726f746f5f5fce10ce4a11ce10ce4a5111ce10ce4a144d23ceffffff454a11b3272900000045584aca9dce10ce10ce1851d0584aca9dce10ce10ce18ce111423f000000023470000004a10b3272900000045584aca9dce10ce10ce1851d0584aca9dce10ce10ce18ce101423c4000000231b0000004a12b3270c0000004545230c0000004545235fffffff464e4bcb2790000000ce4aca1297277e00000010ce115011ce12ce50cec1c04e4aca13b3270c0000004545230700000050cf10c3504a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce352ef7ffff58d44510b3252600000002c90500000c05747261636512c00c0574726163654195016f611114232200000023080000004610ce230f000000454545111011c64a54d0cf2384feffff454a11b3274a00000045584aca9dce10ce10ce1951d04545584aca9dce10ce10ce19ce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1951d04545584aca9dce10ce10ce19ce1040231b0000004a12b3270c0000004545230c000000454523f7fdffff464ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1a51d0454546584aca9dce10ce10ce1ace45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1a51d0454546584aca9dce10ce10ce1ace1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c270a0100004b505041f354bf1d11ce504e5011c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce3544f5ffff58d44510b3252600000002130700000c05747261636512c00c0574726163654195016f611114230b000000cf2382ffffff454a11b3274a00000045584aca9dce10ce10ce1b51d04545584aca9dce10ce10ce1bce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1b51d04545584aca9dce10ce10ce1bce1040231b0000004a12b3270c0000004545230c000000454523f5feffff465011ce10cecc4ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1c51d0454546584aca9dce10ce10ce1cce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1c51d0454546584aca9dce10ce10ce1cce1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c27be0000004b505041f354bf1d11ce504b0c095f5f70726f746f5f5f97aa270d0000004e50cf23060000004623ceffffff454a11b3274a00000045584aca9dce10ce10ce1d51d04545584aca9dce10ce10ce1dce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1d51d04545584aca9dce10ce10ce1dce1040231b0000004a12b3270c0000004545230c00000045452341ffffff464ac850ca104a514a51b727ae00000054514a514a51ce544a514a51501552d09c5451515423e1ffffff454a11b3274b00000045584aca9dce10ce10ce1e51d0454546584aca9dce10ce10ce1ece45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023450000004a10b3272700000045584aca9dce10ce10ce1e51d0454546584aca9dce10ce10ce1ece1040231b0000004a12b3270c0000004545230c00000045452352ffffff45454641ed64f72710c34b419c08ed9c27b10000004b505041f354bf1d11ce504e50141012c64a54d04e501150d0cf23dbffffff454a11b3274a00000045584aca9dce10ce10ce1f51d04545584aca9dce10ce10ce1fce45584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104023440000004a10b3272600000045584aca9dce10ce10ce1f51d04545584aca9dce10ce10ce1fce1040231b0000004a12b3270c0000004545230c0000004545234effffff4612c0171012c64a54d04e501150d023bc0000004a10ce11b34b10ce12b3ac4b10ce13b3ac4b10ce16b3ac4b10ce14b3ac4b10ce15b3ac4b10ce19b3ac270a0000002389000000450c09547970654572726f72141012c64a54d04e501150d04a4a10ce14b3250900000011ce10ce11ce02e2090000500c056572726f7213c00c056572726f724195016f6102e90900000c05747261636512c00c0574726163654195016f6145584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b000000382308000000101040104038111011c64a54d0111011c64a54d010c313c04a5850cf10cec8cf43c0c84e1051d04a1110c3d04a124139536e3cd04a584aca9dce4e4b1250d01150d04a11cec8cf4a13584aca9dcec1c04a104b4bcec1c0d01012c0c8c8c813c0171012c64a54d04e501150d04e11515111ce12ce5151d0d045c8c8c813c0171012c64a54d04e501150d04a0c036b65790c0376616c141012c64a54d04e501150d05111ce10ce51db285111c0d011c0181012c64a54d04e501150d011c0584aca9dce12ce13ce115011ce12ce50ce4a11cec1c04a104b10cec1c0d04bca13b3270c0000004b12ce4b1151d05850cf10ce35c2efffff58d44510b3254500000002e80a00000c05747261636512c00c0574726163654195016f6145584aca9dce12ce12ce41f9b4e2389741e97d38a010b3ac270b00000038230800000010104011c0100c0973657269616c697a650c14c0ef39cee0e4e925c6c2a06a79e1440dd86fceac41627d5b52020a0b00000c0b636f6e736f6c652e6c6f6713c00c0b636f6e736f6c652e6c6f674195016f61584aca9dce12ce4558d445

Let us know how else we can help.

@erikzhang
Copy link
Member

The scripts are too long and contain multiple methods. Can you provide a specific method that will cause the error? Can you provide its script and source code?

@spencercorwin
Copy link
Author

This is the source code that produces that script: console.log({ key: 'val' });. This is from one of many unit tests for our compile which will run the equivalent of ApplicationEngine.Run() with all the default values and an empty snapshot.

I'm not sure what you mean by "multiple methods"?

I realize it's a lot of bytecode for such a small amount of source code. We've never had the change to optimize the compiler.

@erikzhang
Copy link
Member

This is the source code that produces that script: console.log({ key: 'val' });.

I don't think so. The script you provided contains 2842 instructions.

@roman-khimov
Copy link
Contributor

Well, this script can be thrown into invokescript with some results, but yeah, it's big. At the same time it already works differently in C# neo-vm:

{
   "jsonrpc" : "2.0",
   "result" : {
      "script" : "VgHCYCNZDwAAShCzJ1APAABFEM5KEM4YsyfrAQAAEc5KyFDKEEpRSlG3J64AAABUUUpRSlHOVEpRSlFQFVLQnFRRUVQj4f///0VKEbMnSwAAAEVYSsqdzhDOEM4QUdBFRUZYSsqdzhDOEM4QzkVYSsqdzhLOEs5B+bTiOJdB6X04oBCzrCcLAAAAOCMIAAAAEBBAI0UAAABKELMnJwAAAEVYSsqdzhDOEM4QUdBFRUZYSsqdzhDOEM4QzhBAIxsAAABKErMnDAAAAEVFIwwAAABFRSNS////RUVGQe1k9ycQw0tBnAjtnCcKAQAAS1BQQfNUvx0RzlBOUBHAWErKnc4SzhPOEVARzhLOUM5KEc7BwEoQSxDOwcDQS8oTsycMAAAASxLOSxFR0FhQzxDONdr+//9Y1EUQsyUmAAAAAuIAAAAMBXRyYWNlEsAMBXRyYWNlQZUBb2ERFCMLAAAAz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3OEs4SzkH5tOI4l0HpfTigELOsJwsAAAA4IwgAAAAQEEAjRAAAAEoQsycmAAAARVhKyp3OEM4Qzh9R0EVFWErKnc4QzhDOH84QQCMbAAAAShKzJwwAAABFRSMMAAAARUUjTv///0YSwBcQEsZKVNBOUBFQ0CO8AAAAShDOEbNLEM4Ss6xLEM4Ts6xLEM4Ws6xLEM4Us6xLEM4Vs6xLEM4Zs6wnCgAAACOJAAAARQwJVHlwZUVycm9yFBASxkpU0E5QEVDQSkoQzhSzJQkAAAARzhDOEc4C4gkAAFAMBWVycm9yE8AMBWVycm9yQZUBb2EC6QkAAAwFdHJhY2USwAwFdHJhY2VBlQFvYUVYSsqdzhLOEs5B+bTiOJdB6X04oBCzrCcLAAAAOCMIAAAAEBBAEEA4ERARxkpU0BEQEcZKVNAQwxPASlhQzxDOyM9DwMhOEFHQShEQw9BKEkE5U2480EpYSsqdzk5LElDQEVDQShHOyM9KE1hKyp3OwcBKEEtLzsHA0BASwMjIyBPAFxASxkpU0E5QEVDQThFRURHOEs5RUdDQRcjIyBPAFxASxkpU0E5QEVDQSgwDa2V5DAN2YWwUEBLGSlTQTlARUNBREc4QzlHbKFERwNARwBgQEsZKVNBOUBFQ0BHAWErKnc4SzhPOEVARzhLOUM5KEc7BwEoQSxDOwcDQS8oTsycMAAAASxLOSxFR0FhQzxDONcLv//9Y1EUQsyVFAAAAAugKAAAMBXRyYWNlEsAMBXRyYWNlQZUBb2FFWErKnc4SzhLOQfm04jiXQel9OKAQs6wnCwAAADgjCAAAABAQQBHAEAwJc2VyaWFsaXplDBTA7znO4OTpJcbCoGp54UQN2G/OrEFifVtSAgoLAAAMC2NvbnNvbGUubG9nE8AMC2NvbnNvbGUubG9nQZUBb2FYSsqdzhLORVjURQ==",
      "stack" : [],
      "state" : "FAULT",
      "exception" : "The given key 'Neo.VM.Types.Array' was not present in the dictionary.",
      "gasconsumed" : "9671670"
   },
   "id" : 1
}

And NeoGo:

{
   "result" : {
      "stack" : [],
      "gasconsum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SgwDa2V5DAN2YWwUEBLGSlTQTlARUNBREc4QzlHbKFERwNARwBgQEsZKVNBOUBFQ0BHAWErKnc4SzhPOEVARzhLOUM5KEc7BwEoQSxDOwcDQS8oTsycMAAAASxLOSxFR0FhQzxDONcLv//9Y1EUQsyVFAAAAAugKAAAMBXRyYWNlEsAMBXRyYWNlQZUBb2FFWErKnc4SzhLOQfm04jiXQel9OKAQs6wnCwAAADgjCAAAABAQQBHAEAwJc2VyaWFsaXplDBTA7znO4OTpJcbCoGp54UQN2G/OrEFifVtSAgoLAAAMC2NvbnNvbGUubG9nE8AMC2NvbnNvbGUubG9nQZUBb2FYSsqdzhLORVjURQ==",
      "state" : "HALT"
   },
   "id" : 1,
   "jsonrpc" : "2.0"
}

(I've used public RC1 RPC servers). So looks like there is something interesting going on here, but we're not yet sure what exactly that is.

@cloud8little
Copy link

@spencercorwin could you share the source code of the contract? the script is too long to take an analysis.

@spencercorwin
Copy link
Author

The source code for that script is just this:

console.log({ key: 'val' });

It is a simplified version of this unit test: https://github.com/neo-one-suite/neo-one/blob/master/packages/neo-one-smart-contract-compiler/src/__tests__/compile/builtins/console/log.test.ts#L128

I know the script is too long to analyze but I'm not sure how we can make it smaller right now. Our compiler unfortunately produces a lot of boilerplate bytecode for even small amounts of source code. I can try to see if there's an easy way to remove some of the excess bytecode today.

@roman-khimov The error you are seeing from the C# neo-vm is the same error we are getting. Here is the stack trace for that error:
Screen Shot 2021-04-13 at 10 13 00 AM

@erikzhang
Copy link
Member

I can try to see if there's an easy way to remove some of the excess bytecode today.

I can't imagine how to produce such a lot bytes for a single console.log({ key: 'val' });.

@shargon
Copy link
Member

shargon commented Apr 14, 2021

The patch fix the issue, but not the main problem, I tried to find it without luck with this script... It's a map, with and array, with and struct inside...

@spencercorwin
Copy link
Author

I can try to see if there's an easy way to remove some of the excess bytecode today.

I can't imagine how to produce such a lot bytes for a single console.log({ key: 'val' });.

Yeah I honestly don't know how/why our compile produces so much bytecode. I just tried looking into it, but it may take a while for me to understand what is going on. Unfortunately I didn't write the compiler, I've only maintained it and made updates to it, so I don't have a thorough understanding yet.

Luckily, this issue only comes up in two of our unit tests, so it doesn't happen often.

The patch fix the issue, but not the main problem, I tried to find it without luck with this script... It's a map, with and array, with and struct inside...

This sounds correct. The compiler creates, duplicates, and modifies a lot of compound types in order to create the stack representation of this JavaScript object and to turn it into a VM notification that can be converted to a human-readable format.

@Ashuaidehao
Copy link
Contributor

I create a CSharp smartcontract for reproducing this error. Here it is:

        public static object Test()
        {

            var iter = Iterator.Create(GetTestMap());
            while (iter.Next())
            {
                Runtime.Log(iter.Value.Item2.Text);
                var keys = iter.Value.Item2.Property.Keys;
            }
            return true;
        }

        private static Map<int, MyValue> GetTestMap()
        {
            var map = new Map<int, MyValue>();
            map[4] = new MyValue { Text = "hello", Property = new Map<string, string>() };
            return map;
        }

        struct MyValue
        {
            public string Text;
            public Map<string, string> Property;
        }

The main cause is that the VM cannot trace MapWrapper item's referenceCounter correctly. After "Iterator.Create", the map item and its subitem were removed in referenceCounter, then the iterator "reborn" its subitems, and will throw exception in the next zeroreffer check processs.

Attached a quick fix code for MapWrapper, need a better way to resolve the problem completely:

        private Map _map;
        private Struct _fake;

        public MapWrapper(IEnumerable<KeyValuePair<PrimitiveType, StackItem>> map, ReferenceCounter referenceCounter)
        {
            _map = (Map)map;
            _fake = new Struct();
            this.enumerator = map.GetEnumerator();
            this.referenceCounter = referenceCounter;
            referenceCounter.AddStackReference(_fake);
            referenceCounter.AddReference(_map, _fake);

        }

        public void Dispose()
        {
            enumerator.Dispose();
            referenceCounter.RemoveStackReference(_fake);
            referenceCounter.RemoveReference(_map, _fake);
        }

@erikzhang
Copy link
Member

erikzhang commented Apr 17, 2021

I prefer to remove MapWrapper and ArrayWrapper. What do you think? @Ashuaidehao @shargon

@shargon
Copy link
Member

shargon commented Apr 17, 2021

@erikzhang it's good to me

@Ashuaidehao
Copy link
Contributor

It looks good to me, too.

I prefer to remove MapWrapper and ArrayWrapper. What do you think? @Ashuaidehao @shargon

@erikzhang erikzhang closed this Apr 18, 2021
@spencercorwin spencercorwin deleted the reference-counter-fix branch April 18, 2021 02:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants