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#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wold-style-cast"
25#endif
26
30static inline bool evmc_is_abi_compatible(struct evmc_vm* vm)
31{
32 return vm->abi_version == EVMC_ABI_VERSION;
33}
34
38static inline const char* evmc_vm_name(struct evmc_vm* vm)
39{
40 return vm->name;
41}
42
46static inline const char* evmc_vm_version(struct evmc_vm* vm)
47{
48 return vm->version;
49}
50
56static inline bool evmc_vm_has_capability(struct evmc_vm* vm, enum evmc_capabilities capability)
57{
58 return (vm->get_capabilities(vm) & (evmc_capabilities_flagset)capability) != 0;
59}
60
66static inline void evmc_destroy(struct evmc_vm* vm)
67{
68 vm->destroy(vm);
69}
70
76static inline enum evmc_set_option_result evmc_set_option(struct evmc_vm* vm,
77 char const* name,
78 char const* value)
79{
80 if (vm->set_option)
81 return vm->set_option(vm, name, value);
82 return EVMC_SET_OPTION_INVALID_NAME;
83}
84
90static inline struct evmc_result evmc_execute(struct evmc_vm* vm,
91 const struct evmc_host_interface* host,
92 struct evmc_host_context* context,
93 enum evmc_revision rev,
94 const struct evmc_message* msg,
95 uint8_t const* code,
96 size_t code_size)
97{
98 return vm->execute(vm, host, context, rev, msg, code, code_size);
99}
100
107static void evmc_free_result_memory(const struct evmc_result* result)
108{
109 free((uint8_t*)result->output_data);
110}
111
126 int64_t gas_left,
127 int64_t gas_refund,
128 const uint8_t* output_data,
129 size_t output_size)
130{
131 struct evmc_result result;
132 memset(&result, 0, sizeof(result));
133
134 if (output_size != 0)
135 {
136 uint8_t* buffer = (uint8_t*)malloc(output_size);
137
138 if (!buffer)
139 {
141 return result;
142 }
143
144 memcpy(buffer, output_data, output_size);
145 result.output_data = buffer;
146 result.output_size = output_size;
148 }
149
150 result.status_code = status_code;
151 result.gas_left = gas_left;
152 result.gas_refund = gas_refund;
153 return result;
154}
155
163static inline void evmc_release_result(struct evmc_result* result)
164{
165 if (result->release)
166 result->release(result);
167}
168
169
197{
198 uint8_t bytes[24];
199 void* pointer;
200};
201
204 struct evmc_result* result)
205{
206 return (union evmc_result_optional_storage*)&result->create_address;
207}
208
211 const struct evmc_result* result)
212{
213 return (const union evmc_result_optional_storage*)&result->create_address;
214}
215
219static inline const char* evmc_status_code_to_string(enum evmc_status_code status_code)
220{
221 switch (status_code)
222 {
223 case EVMC_SUCCESS:
224 return "success";
225 case EVMC_FAILURE:
226 return "failure";
227 case EVMC_REVERT:
228 return "revert";
229 case EVMC_OUT_OF_GAS:
230 return "out of gas";
232 return "invalid instruction";
234 return "undefined instruction";
236 return "stack overflow";
238 return "stack underflow";
240 return "bad jump destination";
242 return "invalid memory access";
244 return "call depth exceeded";
246 return "static mode violation";
248 return "precompile failure";
250 return "contract validation failure";
252 return "argument out of range";
254 return "wasm unreachable instruction";
255 case EVMC_WASM_TRAP:
256 return "wasm trap";
258 return "insufficient balance";
260 return "internal error";
261 case EVMC_REJECTED:
262 return "rejected";
264 return "out of memory";
265 }
266 return "<unknown>";
267}
268
270static inline const char* evmc_revision_to_string(enum evmc_revision rev)
271{
272 switch (rev)
273 {
274 case EVMC_FRONTIER:
275 return "Frontier";
276 case EVMC_HOMESTEAD:
277 return "Homestead";
279 return "Tangerine Whistle";
281 return "Spurious Dragon";
282 case EVMC_BYZANTIUM:
283 return "Byzantium";
285 return "Constantinople";
286 case EVMC_PETERSBURG:
287 return "Petersburg";
288 case EVMC_ISTANBUL:
289 return "Istanbul";
290 case EVMC_BERLIN:
291 return "Berlin";
292 case EVMC_LONDON:
293 return "London";
294 case EVMC_PARIS:
295 return "Paris";
296 case EVMC_SHANGHAI:
297 return "Shanghai";
298 case EVMC_CANCUN:
299 return "Cancun";
300 }
301 return "<unknown>";
302}
303
306#ifdef __cplusplus
307#pragma GCC diagnostic pop
308} // extern "C"
309#endif
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:845
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:1012
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:1041
evmc_revision
EVM revision.
Definition: evmc.h:876
@ 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:889
@ EVMC_ISTANBUL
The Istanbul revision.
Definition: evmc.h:933
@ EVMC_FRONTIER
The Frontier revision.
Definition: evmc.h:882
@ EVMC_PETERSBURG
The Petersburg revision.
Definition: evmc.h:926
@ EVMC_CONSTANTINOPLE
The Constantinople revision.
Definition: evmc.h:917
@ EVMC_TANGERINE_WHISTLE
The Tangerine Whistle revision.
Definition: evmc.h:896
@ EVMC_SPURIOUS_DRAGON
The Spurious Dragon revision.
Definition: evmc.h:903
@ EVMC_BYZANTIUM
The Byzantium revision.
Definition: evmc.h:910
@ EVMC_CANCUN
The Cancun revision.
Definition: evmc.h:968
@ EVMC_SHANGHAI
The Shanghai revision.
Definition: evmc.h:961
@ EVMC_LONDON
The London revision.
Definition: evmc.h:947
@ EVMC_PARIS
The Paris revision (aka The Merge).
Definition: evmc.h:954
@ EVMC_BERLIN
The Berlin revision.
Definition: evmc.h:940
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:107
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:56
static void evmc_destroy(struct evmc_vm *vm)
Destroys the VM instance.
Definition: helpers.h:66
static void evmc_release_result(struct evmc_result *result)
Releases the resources allocated to the execution result.
Definition: helpers.h:163
static bool evmc_is_abi_compatible(struct evmc_vm *vm)
Returns true if the VM has a compatible ABI version.
Definition: helpers.h:30
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:90
static const char * evmc_vm_version(struct evmc_vm *vm)
Returns the version of the VM.
Definition: helpers.h:46
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:125
static const char * evmc_revision_to_string(enum evmc_revision rev)
Returns the name of the evmc_revision.
Definition: helpers.h:270
static const char * evmc_vm_name(struct evmc_vm *vm)
Returns the name of the VM.
Definition: helpers.h:38
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:219
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:76
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:210
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:203
The opaque data type representing the Host execution context.
The Host interface.
Definition: evmc.h:786
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:424
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:452
int64_t gas_refund
The refunded gas accumulated from this execution and its sub-calls.
Definition: evmc.h:410
size_t output_size
The size of the output data.
Definition: evmc.h:431
evmc_address create_address
The address of the possibly created contract.
Definition: evmc.h:462
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:402
The VM instance.
Definition: evmc.h:1061
evmc_set_option_fn set_option
Optional pointer to function modifying VM's options.
Definition: evmc.h:1117
evmc_get_capabilities_fn get_capabilities
A method returning capabilities supported by the VM instance.
Definition: evmc.h:1110
const char * name
The name of the EVMC VM implementation.
Definition: evmc.h:1076
evmc_destroy_fn destroy
Pointer to function destroying the VM instance.
Definition: evmc.h:1091
const int abi_version
EVMC ABI version implemented by the VM instance.
Definition: evmc.h:1068
const char * version
The version of the EVMC VM implementation, e.g.
Definition: evmc.h:1084
The union representing evmc_result "optional storage".
Definition: helpers.h:197
void * pointer
Optional pointer.
Definition: helpers.h:199
uint8_t bytes[24]
24 bytes of optional storage.
Definition: helpers.h:198