Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

infinite loop issue on smart contract #2787

Closed
mjshin82 opened this issue May 6, 2018 · 2 comments
Closed

infinite loop issue on smart contract #2787

mjshin82 opened this issue May 6, 2018 · 2 comments

Comments

@mjshin82
Copy link

mjshin82 commented May 6, 2018

An infinite loop occurs when using multi index table iterator.

// infinite loop! (only when table is not empty)
for (auto itr = table.cbegin(); itr != table.cend(); itr++) {
}

// no infinite loop
for (auto itr = table.cbegin(); itr != table.cend(); ++itr) {
}

Also, in case of infinite loop, the contract is not terminated (more than 0.2 seconds) and nodeos itself blocks with 99% cpu usage.

@bytemaster
Copy link
Contributor

I have managed to replicate the issue....

for (auto itr = table.cbegin(); itr != table.cend(); itr++) {
      print( "*" );
}

Turns out operator++ is implemented wrong in multi_index container, it returns a copy of a temporary which is incremented rather than incrementing itself after making a copy and returning the original value.

This test will eventually exit, but it takes several seconds using binaryen before it hits the block-cpu limit. Our unit tests don't enforce a deadline and binaryen is slow processing the tight loop.

while( true ) {
   now();
}

There is a big difference in behavior between binaryen and wavm when it comes to the performance of these tight loops.

If I lower the block-cpu limit then it exits quicker without peging CPU. If you were running this in debug vs release I suspect there could be a 10x long delay which could give the perception of minutes of runtime or "infinite" for the impatient.

@mjshin82
Copy link
Author

Thanks!
It solved

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants