Code Monkey home page Code Monkey logo

hera's Introduction

Hera Status

Hera is an ewasm (revision 4) virtual machine implemented in C++ conforming to EVMC ABIv10.

It is designed to leverage various Wasm backends, both interpreters and AOT/JITs.

Client support

Hera has been tested with aleth and geth. It should however work with any client with compliant EVMC support.

Building Hera

First clone this repository and make sure the submodules are checked out:

$ git clone https://github.com/ewasm/hera
$ cd hera
$ git submodule update --init

The preferred way is building Hera as a shared library:

$ mkdir build
$ cd build
$ cmake -DBUILD_SHARED_LIBS=ON ..
$ cmake --build .

In this case the built library will be placed at build/src/libhera/libhera.so on Linux or build/src/libhera/libhera.dylib on MacOS.

Please also check the build options listed in the following section.

Build options

  • -DHERA_DEBUGGING=ON will turn on debugging features and messages. This is off by default.
  • -DBUILD_SHARED_LIBS=ON is a standard CMake option to build libraries as shared. This will build Hera shared library that can be then dynamically loaded by EVMC compatible Clients (e.g. aleth from aleth). This is the preferred way of compilation.

wabt support

Complete support.

wabt is built by default and needs no build options. It can be disabled with:

  • -DHERA_WABT=OFF

Binaryen support

Complete support.

Binaryen support needs to be enabled via the following build option and requested at runtime with engine=binaryen:

  • -DHERA_BINARYEN=ON will request the compilation of Binaryen support

WAVM support

Complete support.

WAVM support needs to be enabled via the following build option and requested at runtime with engine=wavm:

  • -DHERA_WAVM=ON will request the compilation of WAVM support
  • -DLLVM_DIR=... one will need to specify the path to LLVM's CMake file. In most installations this has to be within the lib/cmake/llvm directory, such as /usr/local/Cellar/llvm/6.0.1/lib/cmake/llvm on Homebrew.

Runtime options

These are to be used via EVMC set_option:

  • engine=<engine> will select the underlying WebAssembly engine, where the only accepted values currently are binaryen, wabt, and wavm
  • metering=true will enable metering of bytecode at deployment using the Sentinel system contract (set to false by default)
  • benchmark=true will produce execution timings and output it to both standard error output and hera_benchmarks.log file.
  • evm1mode=<evm1mode> will select how EVM1 bytecode is handled
  • sys:<alias/address>=file.wasm will override the code executing at the specified address with code loaded from a filepath at runtime. This option supports aliases for system contracts as well, such that sys:sentinel=file.wasm and sys:evm2wasm=file.wasm are both valid. This option is intended for debugging purposes.

evm1mode

  • reject will reject any EVM1 bytecode with an error (the default setting)
  • fallback will allow EVM1 bytecode to be passed through to the client for execution
  • evm2wasm will enable transformation of bytecode using the EVM Transcompiler
  • runevm will transform EVM1 bytecode using runevm

Interfaces

Hera implements two interfaces: EEI and a debugging module.

Debugging module

  • debug::print32(value: i32) - print value
  • debug::print64(value: i64) - print value
  • debug::printMem(offset: i32, len: i32) - print memory segment as printable characters
  • debug::printMemHex(offset: i32, len: i32) - print memory segment as hex
  • debug::printStorage(pathOffset: i32) - print storage value as printable characters
  • debug::printStorageHex(pathOffset: i32) - print storage value as hex

These are only enabled if Hera is compiled with debugging on.

EVM Tracing

  • debug::evmTrace(pc: i32, opcode: i32, cost: i32, sp: i32)

This is useful to trace the translated code by EVM Transcompiler. This is only enabled if Hera is compiled with debugging on.

Note: it is valid to invoke evmTrace with a negative value for sp. In this case, no stack values will be printed.

Fuzzing

To enable fuzzing you need clang compiler and provide -DHERA_FUZZING=ON option to CMake. You should also enable both Binaryen and WABT engines. This will build additional executable hera-fuzzer. Check out its help and libFuzzer documentation.

test/fuzzing/hera-fuzzer -help=1

Author(s)

  • Alex Beregszaszi
  • Jake Lang

License

Apache 2.0

hera's People

Contributors

aarlt avatar axic avatar bakaoh avatar cdetrio avatar chfast avatar esteve avatar gballet avatar greatattractor avatar hugo-dc avatar jakelang avatar jwasinger avatar lrettig avatar meowsbits avatar poemm avatar s1na avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hera's Issues

hera 217 vs 225

Running exact same evm2wasm branch against two different Hera PRs on the test case stCallCodes/callcall_00.

cpp reference trace: https://gist.github.com/cdetrio/cb945b5ddbc666f73a9c6debea50d8b7

hera 217 passing: https://gist.github.com/cdetrio/b46865ad3777b8b08cb8328af6751268

  • hera 217 has the correct CODESIZE for CALL -> CODESIZE.
  • but is wrong for CALLCODE -> CODESIZE

hera 225 postBalance bug: https://gist.github.com/cdetrio/a6f0832426ab4833a7fa7c76bee25403

  • hera 225 has correct CODESIZE for CALL -> CODESIZE
  • and is correct for CALLCODE -> CODESIZE
  • but it has a postBalance bug

getCodeSize after callCode

We found this issue while debugging stCallCodes/callcallcode_01 with evm2wasm, and the codeSize WIP patch #217.

Here's the correct trace, executing cpp-ethereum without hera:

test/testeth -t GeneralStateTests/stCallCodes -- --testpath ~/cpp-ethereum/test/jsontests --singlenet "Byzantium" --singletest "callcallcode_01" --jsontrace '{"disableStack": false, "fullStorage": false, "disableStorage": true, "disableMemory": true}'

trace output: https://gist.github.com/hugo-dc/487b7f3021f2aaadbcc26dcb2c62307b

Here's the trace with a patched Hera:

test/testeth -t GeneralStateTests/stCallCodes -- --testpath ~/cpp-ethereum/test/jsontests --singlenet "Byzantium" --singletest "callcallcode_01" --vm hera --evmc evm2wasm=true --evmc evm2wasm.js=true --evmc evm2wasm.js-trace=true --evmc fallback=false -d 0 

trace output: https://gist.github.com/hugo-dc/e7ec65c8d92f103e5c8c49bd7307e7ac

The key lines are around CALLCODE:

evmTrace
{"depth":1,"gas":352279,"gasCost":21,"op":CALLCODE,"pc":35,"stack":["0x000000000000000000000000000000000000000000000000000000000003d090","0x0000000000000000000000001000000000000000000000000000000000000002","0x0000000000000000000000000000000000000000000000000000000000000002","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000040","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000040"]}
useGas 0
useGas 6
~~~ call_message.destination: 0x1000000000000000000000000000000000000002
~~~ call_message.sender: 0x1000000000000000000000000000000000000001
$callCode 3d090 a0 80 8428 40
Calling evm2wasm.js (input 33 bytes)...
...
Executing...
hera.cpp execute...
~~~ msg.destination: 0x1000000000000000000000000000000000000001
~~~ msg.sender: 0x1000000000000000000000000000000000000001
importGlobals
evmTrace
{"depth":2,"gas":252300,"gasCost":0,"op":PUSH1,"pc":0,"stack":[]}
...

And then the CODESIZE inside callCode:

evmTrace
{"depth":2,"gas":132269,"gasCost":21,"op":CODESIZE,"pc":25,"stack":[]}
useGas 0
getCodeSize
~~~ getCodeSize|msg.destination: 0x1000000000000000000000000000000000000001
               |msg.sender: 0x1000000000000000000000000000000000000001
~~~ code_size: 33
0x1000000000000000000000000000000000000002
evmTrace
{"depth":2,"gas":132267,"gasCost":21,"op":PUSH1,"pc":26,"stack":["0x0000000000000000000000000000000000000000000000000000000000000021"]}
useGas 3
evmTrace
{"depth":2,"gas":132264,"gasCost":24,"op":SSTORE,"pc":28,"stack":["0x00000000000000000000000000000000000000000000000000000000000000ee","0x0000000000000000000000000000000000000000000000000000000000000021"]}
useGas 0

We forced the test to pass by patching getCodeSize:

hera/src/eei.cpp

Lines 370 to 381 in 8eea450

if (import->base == Name("getCodeSize")) {
heraAssert(arguments.size() == 0, string("Argument count mismatch in: ") + import->base.str);
HERA_DEBUG << "getCodeSize\n";
takeGas(GasSchedule::base);
evmc_address address = msg.destination;
size_t code_size = context->fn_table->get_code_size(context, &address);
return Literal(static_cast<uint32_t>(code_size));
}

With this code:

if (import->base == Name("getCodeSize")) {
  heraAssert(arguments.size() == 0, string("Argument count mismatch in: ") + import->base.str);

  HERA_DEBUG << "getCodeSize\n";

  takeGas(GasSchedule::base);
  cout << "~~~ getCodeSize|msg.destination: " << addrToHex(msg.destination) << endl;
  cout << "               |msg.sender: " << addrToHex(msg.sender) << endl;
  evmc_address address = msg.destination;
  address.bytes[19] = 2;
  size_t code_size = context->fn_table->get_code_size(context, &address);
  cout << "~~~ code_size: " << code_size << endl;
  cout << addrToHex(address) << endl;
  return Literal(static_cast<uint32_t>(code_size));
}

The destination of callCode is 0x100000....00002, but inside the execution context of callCode this destination address is lost. From within getCodeSize we want to get the code size of 0x100000...0000002, but both msg.destination and msg.sender are 0x100000000...000000001. So we manually set the address passed to get_code_size() to 0x100000000...000000002, and getCodeSize then returns the expected result.

Hera/evmc/cpp chokes on wasm codes with invalid namespace

It prints the following error messages and then a subsequent block never comes.

2018-06-26 19:17:45 eth client 1 blocks imported in 0 ms (2036.66 blocks/s) in #20
2018-06-26 19:18:01 eth client 1 blocks imported in 0 ms (1865.67 blocks/s) in #21
2018-06-26 19:18:03 eth client 1 blocks imported in 0 ms (1574.8 blocks/s) in #22
2018-06-26 19:18:11 eth client 1 blocks imported in 0 ms (2004.01 blocks/s) in #23
2018-06-26 19:18:28 eth client 1 blocks imported in 0 ms (2087.68 blocks/s) in #24
Executing...
Import from invalid namespace.
2018-06-26 19:18:50 aleth warn Internal VM Error (13)
/home/builder/project/libevm/EVMC.cpp(77): Throw in function virtual dev::eth::owning_bytes_ref dev::eth::EVMC::exec(dev::u256&, dev::eth::ExtVMFace&, const OnOpFunc&)
Dynamic exception type: boost::exception_detail::clone_impl<dev::eth::InternalVMError>
[dev::eth::tag_evmcStatusCode*] = 13

2018-06-26 19:19:01 eth client 1 blocks imported in 0 ms (1855.29 blocks/s) in #25
2018-06-26 19:19:06 eth client 1 blocks imported in 0 ms (1420.45 blocks/s) in #26
2018-06-26 19:19:09 eth client 1 blocks imported in 0 ms (2212.39 blocks/s) in #27
Executing...
Import from invalid namespace.
2018-06-26 19:19:11 eth warn Internal VM Error (13)
/home/builder/project/libevm/EVMC.cpp(77): Throw in function virtual dev::eth::owning_bytes_ref dev::eth::EVMC::exec(dev::u256&, dev::eth::ExtVMFace&, const OnOpFunc&)
Dynamic exception type: boost::exception_detail::clone_impl<dev::eth::InternalVMError>
[dev::eth::tag_evmcStatusCode*] = 13

2018-06-26 19:19:12 eth warn 


    Import Failure     /home/builder/project/libethereum/Block.cpp(644): Throw in function dev::u256 dev::eth::Block::enact(const dev::eth::VerifiedBlockRef&, const dev::eth::BlockChain&)
Dynamic exception type: boost::exception_detail::clone_impl<dev::eth::InvalidStateRoot>
[dev::tag_get_h256*] = 619ce5ffdacf048784b248a59e726562aa47e8d2489ead926e3180ba5fd29bb3
[dev::tag_extraData*] = [ type: std::vector<unsigned char, std::allocator<unsigned char> >, size: 24, dump: d0 73 05 10 70 7f 00 00 e7 73 05 10 70 7f 00 00 ]
[dev::tag_required_h256*] = 46f09189599356449753fb985b2b0751b2c36fe0d1125726f37e2389c7aefbae
[dev::eth::tag_now*] = 1530040752
[dev::eth::tag_block*] = [ type: std::vector<unsigned char, std::allocator<unsigned char> >, size: 24, dump: a0 74 05 10 70 7f 00 00 b6 76 05 10 70 7f 00 00 ]


                       Bad Block #00000028.9d040f9e…                      


2018-06-26 19:19:12 eth client Tried to seal sealed block...
2018-06-26 19:19:13 eth client Tried to seal sealed block...
2018-06-26 19:19:14 eth client Tried to seal sealed block...
2018-06-26 19:19:15 eth client Tried to seal sealed block...
2018-06-26 19:19:16 eth client Tried to seal sealed block...
2018-06-26 19:19:17 eth client Tried to seal sealed block...
2018-06-26 19:19:18 eth client Tried to seal sealed block...
2018-06-26 19:19:19 eth client Tried to seal sealed block...
2018-06-26 19:19:20 eth client Tried to seal sealed block...
2018-06-26 19:19:21 eth client Tried to seal sealed block...
2018-06-26 19:19:22 eth client Tried to seal sealed block...
2018-06-26 19:19:23 eth client Tried to seal sealed block...
2018-06-26 19:19:24 eth client Tried to seal sealed block...
2018-06-26 19:19:25 eth client Tried to seal sealed block...
2018-06-26 19:19:26 eth client Tried to seal sealed block...
2018-06-26 19:19:27 eth client Tried to seal sealed block...
2018-06-26 19:19:28 eth client Tried to seal sealed block...
2018-06-26 19:19:29 eth client Tried to seal sealed block...
2018-06-26 19:19:30 eth client Tried to seal sealed block...
2018-06-26 19:19:31 eth client Tried to seal sealed block...
2018-06-26 19:19:32 eth client Tried to seal sealed block...

Add evm tracing debug method

Similar to the evmStackTrace method in ewasm-kernel: https://github.com/ewasm/ewasm-kernel/blob/master/debugInterface.js#L30-L40

It should target to have all the content needed for the tracing format as described here: ethereum/tests#249

I propose the following: evmTrace(pc: i32, opcode: i32, cost: i32, sp: i32)

It will then output the following JSON: {'pc': $pc, 'op': $opcode, 'gas': $gasLeft, 'gasCost': $cost, 'stack': $stack, 'depth': $depth} where $gasLeft and $depth are the current values from the environment and $stack is an array of reading 32 bytes from memory_start + $pc and decreasing $pc by 32 until it is less than 0.

Build Hera as a shared library

The cmake config for this should be cmake -DBUILD_SHARED_LIBS=ON.

We need a new toolchain file with set(CMAKE_POSITION_INDEPENDENT_CODE ON). Let's call it cxx11-pic.cmake following Hunter/polly convention. This toolchain file should be selected when BUILD_SHARED_LIBS is on.

Also add a CI build of shared lib.

I started to extract these common CMake patterns in https://github.com/ethereum/cable (currently used in 2 other projects). We can also consider using it.

Hera fails to execute AssemblyScript WAST

I'm getting the following error running a test that includes valid WAST that's generated from AssemblyScript:

> cpp-ethereum/build/test/testeth -t GeneralStateTests/stEWASMTests -- --testpath tests/ --singlenet Byzantium --jsontrace '' --vm hera/build/src/libhera.dylib --filltests --singletest wrc20Challenge
Running tests using path: "tests/"
Running 1 test case...
Test Case "stEWASMTests":
100%
cpp-ethereum/test/tools/libtesteth/wast2wasm.cpp:38: error: in "GeneralStateTests/stEWASMTests": Error parsing wasm: [parse exception: expected list (at 1:804)]
Executing...
importGlobals
$call 186a0 80 120 0 18
Executing...
Contract entry point ("main") missing.
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<dev::eth::InternalVMError>: cpp-ethereum/libevm/EVMC.cpp(76): Throw in function virtual dev::eth::owning_bytes_ref dev::eth::EVMC::exec(dev::u256 &, dev::eth::ExtVMFace &, const dev::eth::OnOpFunc &)
Dynamic exception type: boost::exception_detail::clone_impl<dev::eth::InternalVMError>
[dev::eth::tag_evmcStatusCode*] = 13

unknown location:0: fatal error: in "GeneralStateTests/stEWASMTests": signal: SIGABRT (application abort requested)
cpp-ethereum/test/tools/libtesteth/wast2wasm.cpp:41: last checkpoint

*** 2 failures are detected (5 failures are expected) in the test module "Master Test Suite"

Here's the WAST in question:

(module
  (type $t0 (func))
  (type $t1 (func (result i32)))
  (type $t2 (func (param i32 i32)))
  (type $t3 (func (param i32) (result i32)))
  (type $t4 (func (param i32 i32 i32)))
  (import "ethereum" "getCallDataSize" (func $main/ethereum_getCallDataSize (type $t1)))
  (import "ethereum" "revert" (func $main/ethereum_revert (type $t2)))
  (import "ethereum" "callDataCopy" (func $main/ethereum_callDataCopy (type $t4)))
  (import "ethereum" "storageLoad" (func $main/ethereum_storageLoad (type $t2)))
  (import "ethereum" "return" (func $main/ethereum_return (type $t2)))
  (import "debug" "printMemHex" (func $printMemHex (param i32 i32)))
  (func $~lib/allocator/arena/allocate_memory (type $t3) (param $p0 i32) (result i32)
    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32)
    get_local $p0
    if $I0
      get_local $p0
      i32.const 1073741824
      i32.gt_u
      if $I1
        unreachable
      end
      get_global $g5
      set_local $l0
      get_local $l0
      get_local $p0
      i32.add
      i32.const 7
      i32.add
      i32.const 7
      i32.const -1
      i32.xor
      i32.and
      set_local $l1
      current_memory
      set_local $l2
      get_local $l1
      get_local $l2
      i32.const 16
      i32.shl
      i32.gt_u
      if $I2
        get_local $l1
        get_local $l0
        i32.sub
        i32.const 65535
        i32.add
        i32.const 65535
        i32.const -1
        i32.xor
        i32.and
        i32.const 16
        i32.shr_u
        set_local $l3
        get_local $l2
        tee_local $l4
        get_local $l3
        tee_local $l5
        get_local $l4
        get_local $l5
        i32.gt_s
        select
        set_local $l4
        get_local $l4
        grow_memory
        i32.const 0
        i32.lt_s
        if $I3
          get_local $l3
          grow_memory
          i32.const 0
          i32.lt_s
          if $I4
            unreachable
          end
        end
      end
      get_local $l1
      set_global $g5
      get_local $l0
      return
    end
    i32.const 0)
  (func $main/do_balance (type $t0)
    (local $l0 i32) (local $l1 i32) (local $l2 i32)
    call $main/ethereum_getCallDataSize
    i32.const 24
    i32.ne
    if $I0
      i32.const 0
      i32.const 0
      call $main/ethereum_revert
    end
    i32.const 32
    call $~lib/allocator/arena/allocate_memory
    set_local $l0
    get_local $l0
    i32.const 4
    i32.const 20
    call $main/ethereum_callDataCopy
    get_local $l0
    i32.load
    set_local $l1
    i32.const 32
    call $~lib/allocator/arena/allocate_memory
    set_local $l2
    get_local $l0
    i32.const 32
    call $printMemHex
    get_local $l1
    get_local $l2
    call $main/ethereum_storageLoad
    get_local $l2
    i32.const 32
    call $main/ethereum_return)
  (func $main/do_transfer (type $t0)
    nop)
  (func $main/main (export "main") (type $t0)
    (local $l0 i32) (local $l1 i32) (local $l2 i32)
    call $main/ethereum_getCallDataSize
    i32.const 4
    i32.lt_s
    if $I0
      i32.const 0
      i32.const 0
      call $main/ethereum_revert
    end
    i32.const 4
    call $~lib/allocator/arena/allocate_memory
    set_local $l0
    get_local $l0
    i32.const 0
    i32.const 4
    call $main/ethereum_callDataCopy
    get_local $l0
    i32.load
    set_local $l1
    get_local $l0
    i32.const 32
    call $printMemHex
    block $B1
      block $B2
        block $B3
          block $B4
            get_local $l1
            set_local $l2
            get_local $l2
            i32.const -1718418918
            i32.eq
            br_if $B4
            get_local $l2
            i32.const 1563795389
            i32.eq
            br_if $B3
            br $B2
          end
          call $main/do_balance
          br $B1
        end
        call $main/do_transfer
        br $B1
      end
      i32.const 0
      i32.const 0
      call $main/ethereum_revert
    end)
  (func $start (type $t0)
    get_global $g6
    i32.const 7
    i32.add
    i32.const 7
    i32.const -1
    i32.xor
    i32.and
    set_global $g4
    get_global $g4
    set_global $g5)
  (memory $memory (export "memory") 1)
  (global $g0 i32 (i32.const 3))
  (global $g1 i32 (i32.const 8))
  (global $g2 i32 (i32.const 7))
  (global $g3 i32 (i32.const 1073741824))
  (global $g4 (mut i32) (i32.const 0))
  (global $g5 (mut i32) (i32.const 0))
  (global $g6 i32 (i32.const 8))
  (start 9))

Call with no data, no value, does nothing

See the test case: https://github.com/ewasm/tests/tree/call-no-data-value . The issue is that none of the code in the called account executes.

Test Case "stEWASMTests":
100%
Executing...
useGas 270
$call 30d40 0 0 0 0
storageStore c8 64
/home/jwasinger/projects/cpp-ethereum/test/tools/libtesteth/ImportTest.cpp(510): error: in "GeneralStateTests/stEWASMTests": callNoDataNoValue Check State: deadbeef00000000000000000000000000000001: incorrect storage [0x] = 0x, expected [0x] = 0x01
Byzantium data: 0 gas: 0 val: 0

Cleanup exception handling

See #187 (comment)

Probably makes sense keeping heraAssert for internal exceptions and introducing a new function ensureCondition(condition, exception) to throw specific exceptions, such as static mode violation, out of bounds memory access, etc.

EVM-C requires non-const reference to evm_context

error: invalid conversion from ‘const evm_context*’ to ‘evm_context*’ [-fpermissive]
       context.fn_table->get_balance(result, &context, address);
                                             ^~~~~~~~

The evm_context singleton cannot be a constant reference, because EVM-C functions will not accept this.

Implement each interface method

  • useGas
  • getAddress
  • getBalance
  • getBlockHash
  • call
  • callDataCopy
  • getCallDataSize
  • callCode
  • callDelegate
  • callStatic
  • storageStore
  • storageLoad
  • getCaller
  • getCallValue
  • codeCopy
  • getCodeSize
  • getBlockCoinbase
  • create
  • getBlockDifficulty
  • externalCodeCopy
  • getExternalCodeSize
  • getGasLeft
  • getBlockGasLimit
  • getTxGasPrice
  • log
  • getBlockNumber
  • getTxOrigin
  • return
  • revert
  • selfDestruct
  • getBlockTimestamp
  • returnDataCopy
  • returnDataSize

Binaryen: properly implement ExternalInterface

The ExternalInterface is the API used to handle memory and call access during executing wasm bytecode. It is defined here: https://github.com/WebAssembly/binaryen/blob/master/src/wasm-interpreter.h#L567-L644

Currently the Hera implementation uses a sample implementation called ShellExternalInterface (https://github.com/WebAssembly/binaryen/blob/master/src/shell-interface.h), which handles the memory functions and we override it to provide our own call interfaces.

This works well, however in the latest Binaryen version ShellExternalInterface is marked final and we cannot extend it anymore.

Compilation error in MacOS

When I try to compile cpp-ethereum using Hera branch eei-charge-gas I get the following compilation error:

[ 14%] Building CXX object hera/src/CMakeFiles/hera.dir/eei.cpp.o
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.cpp:222:73: error: comparison of constant 18446744073709551612 with expression of type 'unsigned int' is always true
      [-Werror,-Wtautological-constant-out-of-range-compare]
      heraAssert(numeric_limits<uint64_t>::max() - GasSchedule::verylow >= GasSchedule::copy * ((length + 31) / 32), "Gas charge overflow");
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.h:51:9: note: expanded from macro 'heraAssert'
  if (!(condition)) throw InternalErrorException(msg); \
        ^~~~~~~~~
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.cpp:261:73: error: comparison of constant 18446744073709551612 with expression of type 'unsigned int' is always true
      [-Werror,-Wtautological-constant-out-of-range-compare]
      heraAssert(numeric_limits<uint64_t>::max() - GasSchedule::verylow >= GasSchedule::copy * ((length + 31) / 32), "Gas charge overflow");
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.h:51:9: note: expanded from macro 'heraAssert'
  if (!(condition)) throw InternalErrorException(msg); \
        ^~~~~~~~~
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.cpp:289:73: error: comparison of constant 18446744073709550915 with expression of type 'unsigned int' is always true
      [-Werror,-Wtautological-constant-out-of-range-compare]
      heraAssert(numeric_limits<uint64_t>::max() - GasSchedule::extcode >= GasSchedule::copy * ((length + 31) / 32), "Gas charge overflow");
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/hugo/workspace/EWASM/GasUsage/cpp-ethereum/hera/src/eei.h:51:9: note: expanded from macro 'heraAssert'
  if (!(condition)) throw InternalErrorException(msg); \
        ^~~~~~~~~
3 errors generated.
make[2]: *** [hera/src/CMakeFiles/hera.dir/eei.cpp.o] Error 1
make[1]: *** [hera/src/CMakeFiles/hera.dir/all] Error 2
make: *** [all] Error 2

However, it compiles without errors in linux.

Code after revert is being executed

The revert operation should immediately halt execution of a contract, and no code after the revert code should run. However, in testing I found that code following a revert call is in fact being called (I tested using useGas to cause the executing contract to run out of gas).

Value exceeds 64 bits

I wrote a new test case in order to validate that we get an OOG exception when there is a call sending a value and the value is higher than the current sender Balance. Instead the result is the following:

100%
Executing...
importGlobals
DEBUG printMemHex(0x14:0x20): 20 a1 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
InternalError: Value exceeds 64 bits.
Executing...
importGlobals
DEBUG printMemHex(0x14:0x20): 20 a1 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
InternalError: Value exceeds 64 bits.

if (safeLoadUint64(balance) < safeLoadUint64(value))

Here it is comparint the balance and value, both are u128 and it is being validated by a function that validates 64 bit numbers.

Update to latest EVM-C / EEI

The latest version of the EVM-C spec can be found here: https://github.com/ethereum/evmjit/blob/develop/include/evm.h

It has 4 data types:

  • arbitrary memory buffer
  • big endian 256 bit number
  • big endian 160 bit number (address)
  • host endian 64 bit number (gas, etc.)

The latest version of the EEI spec can be found here: https://github.com/ewasm/design/blob/master/eth_interface.md#data-types

It has 4 data types:

  • arbitrary memory buffer
  • little endian 256 bit number
  • little endian 160 bit number (address)
  • little endian 128 bit number (value)

First few bytes of code in dynamic CREATE is being overwritten

CC @axic

Test: https://github.com/ethereum/tests/blob/develop/src/GeneralStateTestsFiller/stCallCodes/callcodeDynamicCodeFiller.json
Reference trace: https://gist.github.com/lrettig/17e00c2847d9ca13781aa5c6ff1d2d83
Hera trace (including a bunch of new debug output): https://gist.github.com/lrettig/89bd77db5091e943eb98d31ac612c405

Note that I already fixed one issue here (haven't PRed yet): lrettig@52c3c9e

The dynamic code is still correct here:

context->fn_table->call(&create_result, context, &create_message);

And it's screwed up here: https://github.com/ethereum/cpp-ethereum/blob/c14ccdbb35681b572b1102958ef9bfd58a1f003e/libethereum/Executive.cpp#L364

Hera fails to build with latest evmc

> cmake -DHERA_DEBUGGING=ON -DBUILD_SHARED_LIBS=ON .
> make -j8
...
...
[100%] Linking CXX shared library libhera.so
/usr/bin/ld: ../evmc/lib/instructions/libevmc-instructions.a(instruction_names.c.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
../evmc/lib/instructions/libevmc-instructions.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
src/CMakeFiles/hera.dir/build.make:130: recipe for target 'src/libhera.so' failed
make[2]: *** [src/libhera.so] Error 1
CMakeFiles/Makefile2:498: recipe for target 'src/CMakeFiles/hera.dir/all' failed
make[1]: *** [src/CMakeFiles/hera.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

Gas charged before revert call is being reverted

The revert operation should halt the executing contract without consuming all remaining gas. However, gas that has already been spent before calling revert should not be refunded.

When testing I noticed that if I insert a useGas call before calling revert, that gas is actually not subtracted from the final state.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.