EVMC
helpers.h
Go to the documentation of this file.
1// EVMC: Ethereum Client-VM Connector API.
2// Copyright 2018 The EVMC Authors.
3// Licensed under the Apache License, Version 2.0.
4
15#pragma once
16
17#include <evmc/evmc.h>
18#include <stdlib.h>
19#include <string.h>
20
21#ifdef __cplusplus
22extern "C" {
23#ifdef __GNUC__
24#pragma GCC diagnostic push
25#pragma GCC diagnostic ignored "-Wold-style-cast"
26#endif
27#endif
28
32static inline bool evmc_is_abi_compatible(struct evmc_vm* vm)
33{
34 return vm->abi_version == EVMC_ABI_VERSION;
35}
36
40static inline const char* evmc_vm_name(struct evmc_vm* vm)
41{
42 return vm->name;
43}
44
48static inline const char* evmc_vm_version(struct evmc_vm* vm)
49{
50 return vm->version;
51}
52
58static inline bool evmc_vm_has_capability(struct evmc_vm* vm, enum evmc_capabilities capability)
59{
60 return (vm->get_capabilities(vm) & (evmc_capabilities_flagset)capability) != 0;
61}
62
68static inline void evmc_destroy(struct evmc_vm* vm)
69{
70 vm->destroy(vm);
71}
72
78static inline enum evmc_set_option_result evmc_set_option(struct evmc_vm* vm,
79 char const* name,
80 char const* value)
81{
82 if (vm->set_option)
83 return vm->set_option(vm, name, value);
84 return EVMC_SET_OPTION_INVALID_NAME;
85}
86
92static inline struct evmc_result evmc_execute(struct evmc_vm* vm,
93 const struct evmc_host_interface* host,
94 struct evmc_host_context* context,
95 enum evmc_revision rev,
96 const struct evmc_message* msg,
97 uint8_t const* code,
98 size_t code_size)
99{
100 return vm->execute(vm, host, context, rev, msg, code, code_size);
101}
102
109static void evmc_free_result_memory(const struct evmc_result* result)
110{
111 free((uint8_t*)result->output_data);
112}
113
128 int64_t gas_left,
129 int64_t gas_refund,
130 const uint8_t* output_data,
131 size_t output_size)
132{
133 struct evmc_result result;
134 memset(&result, 0, sizeof(result));
135
136 if (output_size != 0)
137 {
138 uint8_t* buffer = (uint8_t*)malloc(output_size);
139
140 if (!buffer)
141 {
143 return result;
144 }
145
146 memcpy(buffer, output_data, output_size);
147 result.output_data = buffer;
148 result.output_size = output_size;
150 }
151
152 result.status_code = status_code;
153 result.gas_left = gas_left;
154 result.gas_refund = gas_refund;
155 return result;
156}
157
165static inline void evmc_release_result(struct evmc_result* result)
166{
167 if (result->release)
168 result->release(result);
169}
170
171
199{
200 uint8_t bytes[24];
201 void* pointer;
202};
203
206 struct evmc_result* result)
207{
208 return (union evmc_result_optional_storage*)&result->create_address;
209}
210
213 const struct evmc_result* result)
214{
215 return (const union evmc_result_optional_storage*)&result->create_address;
216}
217
221static inline const char* evmc_status_code_to_string(enum evmc_status_code status_code)
222{
223 switch (status_code)
224 {
225 case EVMC_SUCCESS:
226 return "success";
227 case EVMC_FAILURE:
228 return "failure";
229 case EVMC_REVERT:
230 return "revert";
231 case EVMC_OUT_OF_GAS:
232 return "out of gas";
234 return "invalid instruction";
236 return "undefined instruction";
238 return "stack overflow";
240 return "stack underflow";
242 return "bad jump destination";
244 return "invalid memory access";
246 return "call depth exceeded";
248 return "static mode violation";
250 return "precompile failure";
252 return "contract validation failure";
254 return "argument out of range";
256 return "wasm unreachable instruction";
257 case EVMC_WASM_TRAP:
258 return "wasm trap";
260 return "insufficient balance";
262 return "internal error";
263 case EVMC_REJECTED:
264 return "rejected";
266 return "out of memory";
267 }
268 return "<unknown>";
269}
270
272static inline const char* evmc_revision_to_string(enum evmc_revision rev)
273{
274 switch (rev)
275 {
276 case EVMC_FRONTIER:
277 return "Frontier";
278 case EVMC_HOMESTEAD:
279 return "Homestead";
281 return "Tangerine Whistle";
283 return "Spurious Dragon";
284 case EVMC_BYZANTIUM:
285 return "Byzantium";
287 return "Constantinople";
288 case EVMC_PETERSBURG:
289 return "Petersburg";
290 case EVMC_ISTANBUL:
291 return "Istanbul";
292 case EVMC_BERLIN:
293 return "Berlin";
294 case EVMC_LONDON:
295 return "London";
296 case EVMC_PARIS:
297 return "Paris";
298 case EVMC_SHANGHAI:
299 return "Shanghai";
300 case EVMC_CANCUN:
301 return "Cancun";
302 case EVMC_PRAGUE:
303 return "Prague";
304 }
305 return "<unknown>";
306}
307
310#ifdef __cplusplus
311#ifdef __GNUC__
312#pragma GCC diagnostic pop
313#endif
314} // extern "C"
315#endif
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:910
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1085
evmc_status_code
The execution status code.
Definition: evmc.h:284
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:1114
evmc_revision
EVM revision.
Definition: evmc.h:941
@ 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:371
@ 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:357
@ EVMC_INVALID_MEMORY_ACCESS
Tried to read outside memory bounds.
Definition: evmc.h:332
@ EVMC_REJECTED
The execution of the given code and/or message has been rejected by the EVM implementation.
Definition: evmc.h:387
@ EVMC_UNDEFINED_INSTRUCTION
An undefined instruction has been encountered.
Definition: evmc.h:313
@ EVMC_SUCCESS
Execution finished with success.
Definition: evmc.h:286
@ EVMC_OUT_OF_MEMORY
The VM failed to allocate the amount of memory needed for execution.
Definition: evmc.h:390
@ EVMC_STACK_UNDERFLOW
Execution of an opcode has required more items on the EVM stack.
Definition: evmc.h:322
@ EVMC_BAD_JUMP_DESTINATION
Execution has violated the jump destination restrictions.
Definition: evmc.h:325
@ EVMC_INVALID_INSTRUCTION
The designated INVALID instruction has been hit during execution.
Definition: evmc.h:310
@ EVMC_STATIC_MODE_VIOLATION
Tried to execute an operation which is restricted in static mode.
Definition: evmc.h:338
@ EVMC_WASM_TRAP
A WebAssembly trap has been hit during execution.
Definition: evmc.h:368
@ EVMC_PRECOMPILE_FAILURE
A call to a precompiled or system contract has ended with a failure.
Definition: evmc.h:345
@ EVMC_INTERNAL_ERROR
EVM implementation generic internal error.
Definition: evmc.h:374
@ EVMC_OUT_OF_GAS
The execution has run out of gas.
Definition: evmc.h:300
@ EVMC_CONTRACT_VALIDATION_FAILURE
Contract validation has failed (e.g.
Definition: evmc.h:351
@ EVMC_CALL_DEPTH_EXCEEDED
Call depth has exceeded the limit (if any)
Definition: evmc.h:335
@ EVMC_WASM_UNREACHABLE_INSTRUCTION
A WebAssembly unreachable instruction has been hit during execution.
Definition: evmc.h:362
@ EVMC_STACK_OVERFLOW
The execution has attempted to put more items on the EVM stack than the specified limit.
Definition: evmc.h:319
@ EVMC_FAILURE
Generic execution failure.
Definition: evmc.h:289
@ EVMC_REVERT
Execution terminated with REVERT opcode.
Definition: evmc.h:297
@ EVMC_HOMESTEAD
The Homestead revision.
Definition: evmc.h:954
@ EVMC_PRAGUE
The Prague revision.
Definition: evmc.h:1041
@ EVMC_ISTANBUL
The Istanbul revision.
Definition: evmc.h:998
@ EVMC_FRONTIER
The Frontier revision.
Definition: evmc.h:947
@ EVMC_PETERSBURG
The Petersburg revision.
Definition: evmc.h:991
@ EVMC_CONSTANTINOPLE
The Constantinople revision.
Definition: evmc.h:982
@ EVMC_TANGERINE_WHISTLE
The Tangerine Whistle revision.
Definition: evmc.h:961
@ EVMC_SPURIOUS_DRAGON
The Spurious Dragon revision.
Definition: evmc.h:968
@ EVMC_BYZANTIUM
The Byzantium revision.
Definition: evmc.h:975
@ EVMC_CANCUN
The Cancun revision.
Definition: evmc.h:1034
@ EVMC_SHANGHAI
The Shanghai revision.
Definition: evmc.h:1026
@ EVMC_LONDON
The London revision.
Definition: evmc.h:1012
@ EVMC_PARIS
The Paris revision (aka The Merge).
Definition: evmc.h:1019
@ EVMC_BERLIN
The Berlin revision.
Definition: evmc.h:1005
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:109
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:58
static void evmc_destroy(struct evmc_vm *vm)
Destroys the VM instance.
Definition: helpers.h:68
static void evmc_release_result(struct evmc_result *result)
Releases the resources allocated to the execution result.
Definition: helpers.h:165
static bool evmc_is_abi_compatible(struct evmc_vm *vm)
Returns true if the VM has a compatible ABI version.
Definition: helpers.h:32
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:92
static const char * evmc_vm_version(struct evmc_vm *vm)
Returns the version of the VM.
Definition: helpers.h:48
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, int64_t gas_refund, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
Definition: helpers.h:127
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:272
static const char * evmc_vm_name(struct evmc_vm *vm)
Returns the name of the VM.
Definition: helpers.h:40
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:221
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:78
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:212
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:205
The opaque data type representing the Host execution context.
The Host interface.
Definition: evmc.h:845
The message describing an EVM call, including a zero-depth calls from a transaction origin.
Definition: evmc.h:98
const uint8_t * code
The code to be executed.
Definition: evmc.h:196
size_t code_size
The length of the code to be executed.
Definition: evmc.h:201
The EVM code execution result.
Definition: evmc.h:416
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:448
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:418
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:476
int64_t gas_refund
The refunded gas accumulated from this execution and its sub-calls.
Definition: evmc.h:434
size_t output_size
The size of the output data.
Definition: evmc.h:455
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:486
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:426
The VM instance.
Definition: evmc.h:1134
evmc_set_option_fn set_option
Optional pointer to function modifying VM's options.
Definition: evmc.h:1190
evmc_get_capabilities_fn get_capabilities
A method returning capabilities supported by the VM instance.
Definition: evmc.h:1183
const char * name
The name of the EVMC VM implementation.
Definition: evmc.h:1149
evmc_destroy_fn destroy
Pointer to function destroying the VM instance.
Definition: evmc.h:1164
const int abi_version
EVMC ABI version implemented by the VM instance.
Definition: evmc.h:1141
const char * version
The version of the EVMC VM implementation, e.g.
Definition: evmc.h:1157
The union representing evmc_result "optional storage".
Definition: helpers.h:199
void * pointer
Optional pointer.
Definition: helpers.h:201
uint8_t bytes[24]
24 bytes of optional storage.
Definition: helpers.h:200