3

Assumption 1:

If we create a local variable, call stack increases by 1, because we put this new local variable on the stack. if I create another local variable, it again increases by 1. So, with this logic, i shouldn't be able to create more than 1024 local variables ?

Assumption 2:

maybe solidity means 1024 of external calls . but as you can see, it justs says this:

External function calls can fail any time because they exceed the maximum call stack of 1024 It doesn't concrete and just say call stack of 1024 .

So, what does it mean exactly ? call stack increases even for local variables.

Nika Kurashvili
  • 1,175
  • 1
  • 10
  • 25
  • They are two different stacks.

    One is exclusively for external calls. It cannot be inspected from the EVM. The other one is used by EVM to store a manipulate data.

    – Ismael Feb 06 '21 at 16:32
  • I know that stack is the same thing as call stack. Isn't this principle in the ethereum world the same ? – Nika Kurashvili Feb 06 '21 at 16:39
  • They are two separate stacks, one is used by the contract for data manipulation, the other one is used when calling other contracts. Both have 1024 entries of 32 bytes each but they are different, they are not the same stack. – Ismael Feb 06 '21 at 16:45
  • Do you think the same principle applies to other languages(python, c++, js) or it's only Ethereum where we need 2 different stacks ? because I saw that in normal languages, we only have 1 stack... – Nika Kurashvili Feb 06 '21 at 16:46
  • Depends on the language and its abstraction level. I think Ethereum went that way to prevent a possible attacks by manipulation of the return address in the stack . – Ismael Feb 06 '21 at 16:53
  • One is exclusively for external calls. It cannot be inspected from the EVM . Why can't it inspected ? in remix, I can see that when external call is created, i can see its stack and memory respectively. – Nika Kurashvili Feb 06 '21 at 17:21
  • I don't know perhaps for security reasons. – Ismael Feb 06 '21 at 17:25
  • what i said was I could inspect it. after the call opcode was used, I can see the stack of the callee function. what did you mean by not being able to inspect ? – Nika Kurashvili Feb 06 '21 at 17:37
  • The calling stack is not available, a contract only knows the caller (msg.sender) and the originator (tx.origin), but the contract can't determine if there were others contracts calls in between. – Ismael Feb 06 '21 at 17:43
  • yes, but what i meant is that in the debugger, we can see the current execution contract's stack still. – Nika Kurashvili Feb 06 '21 at 17:46
  • Of course the debugger is a modified EVM that has access to everything. – Ismael Feb 06 '21 at 17:52

1 Answers1

1

Stack in Solidity means same as it means in any other machine or virtual machine.

Stack is being allocated for call return addresses, function arguments and local variables.

Besides limited by gas consumption, transactions are limited by stack size and cannot push more than 1024 EVM words on the stack.

Probably the easiest place to study the stack usage in EVM, is PyEVM implementation.

Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127
  • I meant something else with my question. As seen in google, stack is the same as call stack. If that's so, call stack can reach 1024 even without calling more than 1 function(just creating 1024 local variables). I think this is not what solidity docs mean. They mean the function call depth. and their explanation is misguiding. What do you think ? – Nika Kurashvili Feb 06 '21 at 14:13
  • "I think this is not what solidity docs mean." please if you are referring to any particular paragraph in the documentation link it, as I do not know what documentation you have been reading. – Mikko Ohtamaa Feb 06 '21 at 14:19
  • We can correct Solidity documentation then. – Mikko Ohtamaa Feb 06 '21 at 14:20
  • External function calls can fail any time because they exceed the maximum call stack of 1024 . That's all it says. nothing else. And I say the following: call stack is the same as stack. So 1024 limit can be reached without calling 1024 functions inside each other. How can it be reached ? just by creating 1024 local variables. The thing solidity's docs is misleading is that they mean 1024 as 1024 function calls inside each one and not 1024 call stack limit. makes sense ? – Nika Kurashvili Feb 06 '21 at 14:23
  • Can you give me a link to the page where you read this text and I can go to edit it for you if there is any unclarity? – Mikko Ohtamaa Feb 06 '21 at 14:28
  • This should be it - https://docs.soliditylang.org/en/v0.8.1/security-considerations.html#callstack-depth – Nika Kurashvili Feb 06 '21 at 14:29
  • I'd appreciate if you could ping me as soon as you do this – Nika Kurashvili Feb 06 '21 at 14:30
  • Here is now a patch to Solidity documentation https://github.com/ethereum/solidity/pull/10909 – Mikko Ohtamaa Feb 08 '21 at 11:18