Skip to content

cmd/evm returns incorrect value for sender address when executing opcode BALANCE #30254

@Alleysira

Description

@Alleysira

System information

Geth version: evm version 1.14.5-stable-0dd173a7 and evm version 1.14.8-unstable-de6d5976
CL client & version: Nope
OS & Version: Ubuntu 20.04
Commit hash : Nope

Expected behaviour

Hello developers,

I'm currently testing EVM implementations and have encountered a peculiar issue. When executing the BALANCE opcode (0x31), if the address specified by --sender is the same as the address at the top of the stack, the EVM appears to return a balance of 0 instead of the actual balance from genesis.json.

Steps to reproduce the behaviour

I used the latest version of geth image pulled from dockerhub to reproduce this bug.

docker pull ethereum/client-go:alltools-latest
docker run ethereum/client-go:alltools-latest tail -f /dev/null
docker exec -it containerid /bin/sh

This is the genesis.json file for --prestate, which sets the balance of address 0x1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f to 0xffffffffffffffffffffffffffffffffffffffff.

{
  "config": {
    "chainId": 9599,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0,
    "shanghaiBlock": 0,
    "shanghaiTime":0
  },
  "alloc": {
    "0x1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f": {
     "balance": "0x1234"
    }
  },
  "coinbase": "0x000000000000000000000000000000000000abcd",
  "difficulty": "0x0",
  "extraData": "",
  "gasLimit": "0xffffff",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}

Use evm to run the bytecode 731c7cd2d37ffd63856a5bd56a9af1643f2bcf545f3160005260406000f3 and set the --sender to the address in stack.

evm --debug --sender 0x1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f --gas 0xffffff --nomemory=false --json --code 731c7cd2d37ffd63856a5bd56a9af1643f2bcf545f3160005260406000f3 --prestate ./genesis.json run

The mnemonics of the bytecode here are as follows.

PUSH20 1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f
BALANCE 31
PUSH1 00
MSTORE
PUSH1 40
PUSH1 00
RETURN 

The result from evm's output, the stack output of BALANCE is 0x0.

{"pc":0,"op":115,"gas":"0xffffff","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH20"}
{"pc":21,"op":49,"gas":"0xfffffc","gasCost":"0x64","memSize":0,"stack":["0x1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f"],"depth":1,"refund":0,"opName":"BALANCE"}
{"pc":22,"op":96,"gas":"0xffff98","gasCost":"0x3","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":24,"op":82,"gas":"0xffff95","gasCost":"0x6","memSize":0,"stack":["0x0","0x0"],"depth":1,"refund":0,"opName":"MSTORE"}
{"pc":25,"op":96,"gas":"0xffff8f","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000000000","memSize":32,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":27,"op":96,"gas":"0xffff8c","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000000000","memSize":32,"stack":["0x40"],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":29,"op":243,"gas":"0xffff89","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000000000","memSize":32,"stack":["0x40","0x0"],"depth":1,"refund":0,"opName":"RETURN"}
{"output":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","gasUsed":"0x79"}

Backtrace

I checked in evm version 1.14.8-unstable-de6d5976 and evm version 1.14.5-stable-0dd173a7, the problem occured in both versions.

If the --sender parameter is not the same with address in the stack, the opcode BALANCE works fine.

evm --debug --gas 0xffffff --nomemory=false --json --code 731c7cd2d37ffd63856a5bd56a9af1643f2bcf545f3160005260406000f3 --prestate ./genesis.json run
{"pc":0,"op":115,"gas":"0xffffff","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH20"}
{"pc":21,"op":49,"gas":"0xfffffc","gasCost":"0xa28","memSize":0,"stack":["0x1c7cd2d37ffd63856a5bd56a9af1643f2bcf545f"],"depth":1,"refund":0,"opName":"BALANCE"}
{"pc":22,"op":96,"gas":"0xfff5d4","gasCost":"0x3","memSize":0,"stack":["0x1234"],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":24,"op":82,"gas":"0xfff5d1","gasCost":"0x6","memSize":0,"stack":["0x1234","0x0"],"depth":1,"refund":0,"opName":"MSTORE"}
{"pc":25,"op":96,"gas":"0xfff5cb","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000001234","memSize":32,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":27,"op":96,"gas":"0xfff5c8","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000001234","memSize":32,"stack":["0x40"],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":29,"op":243,"gas":"0xfff5c5","gasCost":"0x3","memory":"0x0000000000000000000000000000000000000000000000000000000000001234","memSize":32,"stack":["0x40","0x0"],"depth":1,"refund":0,"opName":"RETURN"}
{"output":"00000000000000000000000000000000000000000000000000000000000012340000000000000000000000000000000000000000000000000000000000000000","gasUsed":"0xa3d"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions