Incorrect instruction order - optimization bug? #482

Closed
opened 2023-08-23 20:27:04 +09:00 by zxq9 · 1 comment
zxq9 commented 2023-08-23 20:27:04 +09:00 (Migrated from gitlab.com)

Created by: hanssv

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.

*Created by: hanssv* 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.
zxq9 commented 2023-08-23 21:34:49 +09:00 (Migrated from gitlab.com)

Created by: hanssv

Problem originally spotted by @brainiacfive

*Created by: hanssv* Problem originally spotted by @brainiacfive
Sign in to join this conversation.
No Milestone
No project
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: QPQ-AG/sophia#482
No description provided.