aeternity / aesophia

Stand alone compiler for the Sophia smart contract language
https://docs.aeternity.com/aesophia
ISC License
51 stars 19 forks source link

Incorrect instruction order - optimization bug? #482

Closed hanssv closed 1 year ago

hanssv commented 1 year ago

The following contract is incorrectly compiled:

contract C =
  record state = {m : map(int, int)}
  entrypoint init() = {m = {[1] = 2}}

  stateful entrypoint foo(ix : int) =
    let res = state.m[ix]
    __foo(ix)
    res

  stateful entrypoint __foo(ix : int) =
    put(state{ m = Map.delete(ix, state.m) })

The optimization phase will incorrectly push the Map.lookup (let res = state.m[ix]) past the call to __foo as is evident by the resulting FATE code:

...
FUNCTION foo( integer) : integer
  ;; BB : 0
          PUSH arg0
          CALL "IBB"
  ;; BB : 1
          POP var9999
          MAP_LOOKUP a store1 arg0
          RETURN

This results in any call to foo failing since either ix is not there in the first place, or we delete it before looking at it - this is obviously a bug.

hanssv commented 1 year ago

Problem originally spotted by @brainiacfive