Closed wanderer closed 6 years ago
All upgrades must be downwardly compatible. How do you ensure that old contracts will still run the same way?
@CJentzsch You would have to transcompile all the existing EVM code to WASM. Something like this has been done before with the EVM JIT. Except that it targeted LLVM IR. There are a couple of opcodes that are not backwards compatible. gas
and PC
might have a different expected values
Although this idea does have its merits, I honestly don't think this will ever happen. We've just launched Ethereum and the EVM, and even getting a single opcode added to it is a horribly painful and error prone procedure. By replacing the entire EVM we might as well just launch a new network from scratch. I don't think we could rationalize any speed increases with the enormous costs associated with implementing the proposed WASM based VM in all official languages, battle testing that they work and also potentially having them audited.
@karalabe yep that is the key question. C++ wouldn't have much to implement since they would just have to wrap Intel's JIT. But it might be a lot of work for go and Python. Lucky WASM is taking a similar approach to testing as us. They have a standalone test repo that all implementation must pass. We wouldn't be doing everything from scratch.
@wanderer Is there something specific to WASM which makes it a better fit than just using LLVM IR, which is already defined, and becoming very close to being a universal AST standard?
If it was feasible to use LLVM IR as the target then all the numerous existing LLVM backends could be used off-the-shelf (C++, C#, JS, and many more) right now, and you would still be able to target WASM as-and-when that settles down.
@bobsummerwill I actually compiled some notes on this subject. Here the are. Let me know if you have anymore questions!
From LLVM Language Reference:
The LLVM code representation is designed to be used in three different forms: as an in-memory compiler IR, as an on-disk bitcode representation (suitable for fast loading by a Just-In-Time compiler), and as a human readable assembly language representation.
Good
Bad
Response from Derek Schuff (one of the engineers for pNACL) from google on WASM vs LLVM
I'm guessing you are unfamiliar with PNaCl. This is more or less the approach taken by PNaCl; i.e. use LLVM as the starting point for a wire format. It turns out that LLVM IR/bitcode by itself is neither portable nor stable enough to be used for this purpose, and it is designed for compiler optimizations, it has a huge surface area, much more than is needed for this purpose. PNaCl solves these problems by defining a portable target triple (an architecture called "le32" used instead of e.g. i386 or arm), a subset of LLVM IR, and a stable frozen wire format based on LLVM's bitcode. So this approach (while not as simple as "use LLVM-IR directly") does work. However LLVM's IR and bitcode formats were designed (respectively) for use as a compiler IR and for temporary file serialization for link-time optimization. They were not designed for the goals we have, in particular a small compressed distribution format and fast decoding. We think we can do much better for wasm, with the experience we've gained from PNaCl
Further research on LLVM LLVM IR is meant to make compiler optimizations easy to implement, and to represent the constructs and semantics required by C, C++, and other languages on a large variety of operating systems and architectures. This means that by default the IR is not portable (the same program has different representations for different architectures) or stable (it changes over time as optimization and language requirements change). It has representations for a huge variety of information that is useful for implementing mid-level compiler optimizations but is not useful for code generation (but which represents a large surface area for codegen implementers to deal with). It also has undefined behavior (largely similar to that of C and C++) which makes some classes of optimization feasible or more powerful, but which can lead to unpredictable behavior at runtime. LLVM's binary format (bitcode) was designed for temporary on-disk serialization of the IR for link-time optimization, and not for stability or compressibility (although it does have some features for both of those).
None of these problems are insurmountable. For example PNaCl defines a small portable subset of the IR with reduced undefined behavior, and a stable version of the bitcode encoding. It also employs several techniques to improve startup performance. However, each customization, workaround, and special solution means less benefit from the common infrastructure
Thanks for the excellently detailed answer, @wanderer!
I agree LLVM IR is not a choice. It is huge and unstable (intentionally). Even LLVM does not have any proper interpreter for LLVM IR (they used to have it but it stopped being maintained some time ago).
update, the binary encoding has now been speficied https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
Future discussion should go here https://github.com/ethereum/ewasm-design
Would it make sense closing this issue and reopening it or opening a new issue, once an actual proposal can be made? The content here is really outdated, especially given eWASM depends on a final/stable version of WASM.
@wanderer as agreed on AllCoreDev#31, can you please close this?
@Arachnid @Souptacular @nicksavers in January it was agreed to close this outdated issue. Can an editor please do that?
Done!
Thanks!
Anyone looking at this issue after 2016: the proposal described here does not reflect the current state and specification of Ewasm. Please refer to https://github.com/ewasm instead for up to date information.
EVM 2.0
Abstract
This EIP proposes upgrading the VM by converting to use a subset of Webassembly (Wasm). Wasm is a new Assembly standard being built for the web. The main advantage of using wasm is performance (both speed and size). The main disadvantage would be the need to port our existing infrastructure to use a new ISA. Most of this EIP is taken from Wasm's design docs which should be referenced for futher details.
Motivation
To truly distinguish Ethereum as the World Computer we need to have a performant VM. The current architecture of the VM is one of the greatest blockers to raw performance. Being stack-based and the 256-bit word size make translation from EVM opcodes to hardware instructions more difficult than needed.
With an architecture that provides a closer mapping to hardware the VM will have a considerably enhanced performance which will effectively open the door to a much wider array of uses that require a much higher performance/throughput. Also, by choosing a common and more standardized architecture anyone will be able to compile C/C++, Solidity (, etc.) once, and the compiled code will run in multiple environments. Using the new Assembly standard will make running a program either directly on Ethereum, on a cloud hosting environment, or on one's local machine - a friction-less process.
Specification
Wasm is a kin to LLVM IR and Low Level Instruction Set Architectures (ISAs). It is an defined as an Abstract Syntax Tree (AST) which has a textual representation using s-expressions. As an AST it has some higher level semantics than pure hardware ISAs. Including functions and basic flow control. Wasm is not finished a specification and still in flux. This EIP suggest using a subset of wasm to allow us to tailor the semantics to Ethereum. This spec is designed so that the minimum amount of changes a wasm VM would need to be implemented. This should make existing implementations of wasm easy to integrate and provide a way to run Ethereum flavored wasm on a unmodified wasm VM.
A Wasm module expressed in s-expressions looks like
Eth Flavored wasm
For Ethereum we can consider each contract as a module therefore we can have an intrinsic modules and we do not need to specify the module’s closure (we leave it now for clarity). Furthermore if we restrict the types to 64 bit then we can eliminate the type information from the locals definition.
Difference from WASM
call
returnsMetering
Metering can initial be accomplished by injecting the counting code into the AST then passing the modified AST to a wasm VM. Modifying the AST is done by traversing the AST and adding a gas check immediately after each branch condition and at the start of functions and loops. For a more performant version gas counting could possibly be done at the VM directly. But from initial trials injecting gas at the AST level does not greatly affect performance. Since the gas counting code must be protected it would have to be implemented in a separate module.
Ethereum Specific Opcodes
The above opcodes should be used with WASM's call.
call_delegate
would becomecall $call_delegate <params>
and so on.Whether the module system could be used for contracts in general is an open questions. For now we will not use it in favor of using the
CALL
,CALLCODE
andCALLDELEGATE
semantics. This could be revisited once wasm has support for runtimedlopen
like dynamic linking.Modified Ethereum operations
Elimination of
PUSH
,DUP
,SWAP
,POP
,PUSH
,JUMP
,JUMPI
SUICIDE
,CALL
,CALLCODE
,CALLDELEGATE
now gets theto
address from memory and are expressions.note on CALLs
The module loading system can’t yet support
call
since exported function are only allowed to return one value and do not have shared memory access. When implementingSUICIDE
,CALL
,CALLCODE
,CALLDELEGATE
the host environment will have to load the return values into the specified location in linear memory.Abstract Syntax Tree Semantics
For the current full definition see here I’m only going to post here the core semantics that we are intersted in. For more details see the accompanying wiki. A formal spec such for each opcode will be worked out if this EIP gains Momentum. Currently this EIP suggests limiting the functions to only 64 bit Integer operations.
Internal Function Calls
Each function has a signature, which consists of: Return types, which are a sequence of value types Argument types, which are a sequence of value types
WebAssembly doesn't support variable-length argument lists (aka varargs). Compilers targeting WebAssembly can instead support them through explicit accesses to linear memory. The length of the return types sequence may only be 0 or 1
Direct calls to a function specify the callee by index into a main function table.
Integer Operations
add
: sign-agnostic additionsub
: sign-agnostic subtractionmul
: sign-agnostic multiplication (lower 32-bits)div_s
: signed division (result is truncated toward zero)div_u
: unsigned division (result is floored)rem_s
: signed remainder (result has the sign of the dividend)rem_u
: unsigned remainderand
: sign-agnostic bitwise andor
: sign-agnostic bitwise inclusive orxor
: sign-agnostic bitwise exclusive orshl
: sign-agnostic shift leftshr_u
: zero-replicating (logical) shift rightshr_s
: sign-replicating (arithmetic) shift righteq
: sign-agnostic compare equalne
: sign-agnostic compare unequallt_s
: signed less thanle_s
: signed less than or equallt_u
: unsigned less thanle_u
: unsigned less than or equalgt_s
: signed greater thange_s
: signed greater than or equalgt_u
: unsigned greater thange_u
: unsigned greater than or equalclz
: sign-agnostic count leading zero bits (All zero bits are considered leading if the value is zero)ctz
: sign-agnostic count trailing zero bits (All zero bits are considered trailing if the value is zero)popcnt
: sign-agnostic count number of one bitsFlow Control
block
: a fixed-length sequence of expressions with a label at the end.loop
: a block with an additional label at the beginning which may be used to form loopsbr
: branch to a given label in an enclosing constructbr_if
: conditionally branch to a given label in an enclosing constructget_reg
: gets a register. We constrain registers to be one byte. Therefor we have 255 registersstore_reg
: stores a register.The
br
andbr_if
constructs express low-level branching. Branches that exit a block or loop may take a subexpression that yields a value for the exited construct. Branches may only reference labels defined by an outer enclosing construct. This means that, for example, references to a block's label can only occur within the block's body. In practice, outer blocks can be used to place labels for any given branching pattern, except for one restriction: one can't branch into the middle of a loop from outside it. This restriction ensures all control flow graphs are well-structuredunreachable
: An expression which can take on any type, and which, if executed, always traps. It is intended to be used for example after calls to functions which are known by the producer not to return (otherwise the producer would have to create another expression with an unused value to satisfy the type check). This trap is intended to be impossible for user code to catch or handle, even in the future when it may be possible to handle some other kinds of traps or exceptions.Linear Memory
The main storage of a WebAssembly instance, called the linear memory, is a contiguous, byte-addressable range of memory spanning from offset
0
and extending formemory_size
bytes which can be dynamically grown bygrow_memory
. The linear memory can be considered to be an untyped array of bytes, and it is unspecified how embedders map this array into their process' own virtual memory. The linear memory is sandboxed; it does not alias the execution engine's internal data structures, the execution stack, local variables, or other process memory. The initial state of linear memory is specified by the module.Linear Memory Accesses
Linear memory access is accomplished with explicit load and store operators. Integer loads can specify a storage size which is smaller than the result type as well as a signedness which determines whether the bytes are sign- or zero- extended into the result type.
load8_s
: load 1 byte and sign-extend i8 to i32load8_u
: load 1 byte and zero-extend i8 to i32load16_s
: load 2 bytes and sign-extend i16 to i32load16_u
: load 2 bytes and zero-extend i16 to i32load
: load 4 bytes as i32Stores have an additional input operand which is the value to store to memory. Like loads, integer stores may specify a smaller storage size than the operand size in which case integer wrapping is implied.
store8
: wrap i64 to i8 and store 1 bytestore16
: wrap i64 to i16 and store 2 bytesstore32
: wrap i64 to i32 and store 4 bytesstore
: (no conversion) store 8 bytesLocal variables
Each function has a fixed, pre-declared number of local variables which occupy a single index space local to the function. Parameters are addressed as local variables. Local variables do not have addresses and are not aliased by linear memory. Local variables have value types and are initialized to the appropriate zero value for their type at the beginning of the function, except parameters which are initialized to the values of the arguments passed to the function. Local variables can be thought of as registers
get_local
: read the current value of a local variableset_local
: set the current value of a local variableModule start function
If the module has a start node defined, the function it refers should be called by the loader after the instance is initialized and before the exported functions are called.
For example, a start node in a module will be:
(start $start_function)
or
(start 0)
In the first example, the environment is expected to call the function $start_function before calling any other module function. In the second case, the environment is expected to call the module function indexed 0.
A module can:
Binary Specification
Further attention to the binary encode can be tracked here. Currently wasm only has an early binary encoding spec. Its ends up being fairly easy to follow. But not all of the features are relevant to Ethereum so some iteration can be done there. An overview of binary encoding can be seen here.
Rationale
A 64 bit word size would map to real world hardware. Most the time we don’t need 256 bit arithmetic. 256 was chosen to work with 256-bit hashes but we can easily store hash results in memory. Some opcodes such as CALL would have to load the to address from memory.
Implementation
While the complete EIP is not implemented many pieces of it are.
There are several Wasm implementations; here are a few.
References