EVMC
helpers.h
Go to the documentation of this file.
1 /* EVMC: Ethereum Client-VM Connector API.
2  * Copyright 2018-2019 The EVMC Authors.
3  * Licensed under the Apache License, Version 2.0.
4  */
5 
18 #pragma once
19 
20 #include <evmc/evmc.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
27 static inline bool evmc_is_abi_compatible(struct evmc_vm* vm)
28 {
29  return vm->abi_version == EVMC_ABI_VERSION;
30 }
31 
35 static inline const char* evmc_vm_name(struct evmc_vm* vm)
36 {
37  return vm->name;
38 }
39 
43 static inline const char* evmc_vm_version(struct evmc_vm* vm)
44 {
45  return vm->version;
46 }
47 
53 static inline bool evmc_vm_has_capability(struct evmc_vm* vm, enum evmc_capabilities capability)
54 {
55  return (vm->get_capabilities(vm) & (evmc_capabilities_flagset)capability) != 0;
56 }
57 
63 static inline void evmc_destroy(struct evmc_vm* vm)
64 {
65  vm->destroy(vm);
66 }
67 
73 static inline enum evmc_set_option_result evmc_set_option(struct evmc_vm* vm,
74  char const* name,
75  char const* value)
76 {
77  if (vm->set_option)
78  return vm->set_option(vm, name, value);
79  return EVMC_SET_OPTION_INVALID_NAME;
80 }
81 
87 static inline struct evmc_result evmc_execute(struct evmc_vm* vm,
88  const struct evmc_host_interface* host,
89  struct evmc_host_context* context,
90  enum evmc_revision rev,
91  const struct evmc_message* msg,
92  uint8_t const* code,
93  size_t code_size)
94 {
95  return vm->execute(vm, host, context, rev, msg, code, code_size);
96 }
97 
104 static void evmc_free_result_memory(const struct evmc_result* result)
105 {
106  free((uint8_t*)result->output_data);
107 }
108 
121 static inline struct evmc_result evmc_make_result(enum evmc_status_code status_code,
122  int64_t gas_left,
123  const uint8_t* output_data,
124  size_t output_size)
125 {
126  struct evmc_result result;
127  memset(&result, 0, sizeof(result));
128 
129  if (output_size != 0)
130  {
131  uint8_t* buffer = (uint8_t*)malloc(output_size);
132 
133  if (!buffer)
134  {
135  result.status_code = EVMC_OUT_OF_MEMORY;
136  return result;
137  }
138 
139  memcpy(buffer, output_data, output_size);
140  result.output_data = buffer;
141  result.output_size = output_size;
142  result.release = evmc_free_result_memory;
143  }
144 
145  result.status_code = status_code;
146  result.gas_left = gas_left;
147  return result;
148 }
149 
157 static inline void evmc_release_result(struct evmc_result* result)
158 {
159  if (result->release)
160  result->release(result);
161 }
162 
163 
191 {
192  uint8_t bytes[24];
193  void* pointer;
194 };
195 
198  struct evmc_result* result)
199 {
200  return (union evmc_result_optional_storage*)&result->create_address;
201 }
202 
205  const struct evmc_result* result)
206 {
207  return (const union evmc_result_optional_storage*)&result->create_address;
208 }
209 
213 static inline const char* evmc_status_code_to_string(enum evmc_status_code status_code)
214 {
215  switch (status_code)
216  {
217  case EVMC_SUCCESS:
218  return "success";
219  case EVMC_FAILURE:
220  return "failure";
221  case EVMC_REVERT:
222  return "revert";
223  case EVMC_OUT_OF_GAS:
224  return "out of gas";
226  return "invalid instruction";
228  return "undefined instruction";
229  case EVMC_STACK_OVERFLOW:
230  return "stack overflow";
232  return "stack underflow";
234  return "bad jump destination";
236  return "invalid memory access";
238  return "call depth exceeded";
240  return "static mode violation";
242  return "precompile failure";
244  return "contract validation failure";
246  return "argument out of range";
248  return "wasm unreachable instruction";
249  case EVMC_WASM_TRAP:
250  return "wasm trap";
252  return "insufficient balance";
253  case EVMC_INTERNAL_ERROR:
254  return "internal error";
255  case EVMC_REJECTED:
256  return "rejected";
257  case EVMC_OUT_OF_MEMORY:
258  return "out of memory";
259  }
260  return "<unknown>";
261 }
262 
264 static inline const char* evmc_revision_to_string(enum evmc_revision rev)
265 {
266  switch (rev)
267  {
268  case EVMC_FRONTIER:
269  return "Frontier";
270  case EVMC_HOMESTEAD:
271  return "Homestead";
273  return "Tangerine Whistle";
275  return "Spurious Dragon";
276  case EVMC_BYZANTIUM:
277  return "Byzantium";
278  case EVMC_CONSTANTINOPLE:
279  return "Constantinople";
280  case EVMC_PETERSBURG:
281  return "Petersburg";
282  case EVMC_ISTANBUL:
283  return "Istanbul";
284  case EVMC_BERLIN:
285  return "Berlin";
286  case EVMC_LONDON:
287  return "London";
288  case EVMC_SHANGHAI:
289  return "Shanghai";
290  }
291  return "<unknown>";
292 }
293 
EVMC: Ethereum Client-VM Connector API.
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:778
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:931
evmc_status_code
The execution status code.
Definition: evmc.h:260
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:960
evmc_revision
EVM revision.
Definition: evmc.h:809
@ EVMC_ABI_VERSION
The EVMC ABI version number of the interface declared in this file.
Definition: evmc.h:47
@ EVMC_INSUFFICIENT_BALANCE
The caller does not have enough funds for value transfer.
Definition: evmc.h:347
@ EVMC_ARGUMENT_OUT_OF_RANGE
An argument to a state accessing method has a value outside of the accepted range of values.
Definition: evmc.h:333
@ EVMC_INVALID_MEMORY_ACCESS
Tried to read outside memory bounds.
Definition: evmc.h:308
@ EVMC_REJECTED
The execution of the given code and/or message has been rejected by the EVM implementation.
Definition: evmc.h:363
@ EVMC_UNDEFINED_INSTRUCTION
An undefined instruction has been encountered.
Definition: evmc.h:289
@ EVMC_SUCCESS
Execution finished with success.
Definition: evmc.h:262
@ EVMC_OUT_OF_MEMORY
The VM failed to allocate the amount of memory needed for execution.
Definition: evmc.h:366
@ EVMC_STACK_UNDERFLOW
Execution of an opcode has required more items on the EVM stack.
Definition: evmc.h:298
@ EVMC_BAD_JUMP_DESTINATION
Execution has violated the jump destination restrictions.
Definition: evmc.h:301
@ EVMC_INVALID_INSTRUCTION
The designated INVALID instruction has been hit during execution.
Definition: evmc.h:286
@ EVMC_STATIC_MODE_VIOLATION
Tried to execute an operation which is restricted in static mode.
Definition: evmc.h:314
@ EVMC_WASM_TRAP
A WebAssembly trap has been hit during execution.
Definition: evmc.h:344
@ EVMC_PRECOMPILE_FAILURE
A call to a precompiled or system contract has ended with a failure.
Definition: evmc.h:321
@ EVMC_INTERNAL_ERROR
EVM implementation generic internal error.
Definition: evmc.h:350
@ EVMC_OUT_OF_GAS
The execution has run out of gas.
Definition: evmc.h:276
@ EVMC_CONTRACT_VALIDATION_FAILURE
Contract validation has failed (e.g.
Definition: evmc.h:327
@ EVMC_CALL_DEPTH_EXCEEDED
Call depth has exceeded the limit (if any)
Definition: evmc.h:311
@ EVMC_WASM_UNREACHABLE_INSTRUCTION
A WebAssembly unreachable instruction has been hit during execution.
Definition: evmc.h:338
@ EVMC_STACK_OVERFLOW
The execution has attempted to put more items on the EVM stack than the specified limit.
Definition: evmc.h:295
@ EVMC_FAILURE
Generic execution failure.
Definition: evmc.h:265
@ EVMC_REVERT
Execution terminated with REVERT opcode.
Definition: evmc.h:273
@ EVMC_HOMESTEAD
The Homestead revision.
Definition: evmc.h:822
@ EVMC_ISTANBUL
The Istanbul revision.
Definition: evmc.h:866
@ EVMC_FRONTIER
The Frontier revision.
Definition: evmc.h:815
@ EVMC_PETERSBURG
The Petersburg revision.
Definition: evmc.h:859
@ EVMC_CONSTANTINOPLE
The Constantinople revision.
Definition: evmc.h:850
@ EVMC_TANGERINE_WHISTLE
The Tangerine Whistle revision.
Definition: evmc.h:829
@ EVMC_SPURIOUS_DRAGON
The Spurious Dragon revision.
Definition: evmc.h:836
@ EVMC_BYZANTIUM
The Byzantium revision.
Definition: evmc.h:843
@ EVMC_SHANGHAI
The Shanghai revision.
Definition: evmc.h:887
@ EVMC_LONDON
The London revision.
Definition: evmc.h:880
@ EVMC_BERLIN
The Berlin revision.
Definition: evmc.h:873
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
Definition: helpers.h:121
static void evmc_free_result_memory(const struct evmc_result *result)
The evmc_result release function using free() for releasing the memory.
Definition: helpers.h:104
static bool evmc_vm_has_capability(struct evmc_vm *vm, enum evmc_capabilities capability)
Checks if the VM has the given capability.
Definition: helpers.h:53
static void evmc_destroy(struct evmc_vm *vm)
Destroys the VM instance.
Definition: helpers.h:63
static const char * evmc_status_code_to_string(enum evmc_status_code status_code)
Returns text representation of the evmc_status_code.
Definition: helpers.h:213
static void evmc_release_result(struct evmc_result *result)
Releases the resources allocated to the execution result.
Definition: helpers.h:157
static bool evmc_is_abi_compatible(struct evmc_vm *vm)
Returns true if the VM has a compatible ABI version.
Definition: helpers.h:27
static struct evmc_result evmc_execute(struct evmc_vm *vm, const struct evmc_host_interface *host, struct evmc_host_context *context, enum evmc_revision rev, const struct evmc_message *msg, uint8_t const *code, size_t code_size)
Executes code in the VM instance.
Definition: helpers.h:87
static const char * evmc_vm_version(struct evmc_vm *vm)
Returns the version of the VM.
Definition: helpers.h:43
static enum evmc_set_option_result evmc_set_option(struct evmc_vm *vm, char const *name, char const *value)
Sets the option for the VM, if the feature is supported by the VM.
Definition: helpers.h:73
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:264
static const char * evmc_vm_name(struct evmc_vm *vm)
Returns the name of the VM.
Definition: helpers.h:35
static const union evmc_result_optional_storage * evmc_get_const_optional_storage(const struct evmc_result *result)
Provides read-only access to evmc_result "optional storage".
Definition: helpers.h:204
static union evmc_result_optional_storage * evmc_get_optional_storage(struct evmc_result *result)
Provides read-write access to evmc_result "optional storage".
Definition: helpers.h:197
The opaque data type representing the Host execution context.
The Host interface.
Definition: evmc.h:719
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:97
The EVM code execution result.
Definition: evmc.h:392
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:415
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:394
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:443
size_t output_size
The size of the output data.
Definition: evmc.h:422
evmc_address create_address
The address of the contract created by create instructions.
Definition: evmc.h:454
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:402
The VM instance.
Definition: evmc.h:980
evmc_set_option_fn set_option
Optional pointer to function modifying VM's options.
Definition: evmc.h:1036
evmc_get_capabilities_fn get_capabilities
A method returning capabilities supported by the VM instance.
Definition: evmc.h:1029
const char * name
The name of the EVMC VM implementation.
Definition: evmc.h:995
evmc_destroy_fn destroy
Pointer to function destroying the VM instance.
Definition: evmc.h:1010
const int abi_version
EVMC ABI version implemented by the VM instance.
Definition: evmc.h:987
const char * version
The version of the EVMC VM implementation, e.g.
Definition: evmc.h:1003
The union representing evmc_result "optional storage".
Definition: helpers.h:191
void * pointer
Optional pointer.
Definition: helpers.h:193
uint8_t bytes[24]
24 bytes of optional storage.
Definition: helpers.h:192