EVMC
EVMC Helpers

EVMC Helpers. More...

Modules

 Result Optional Storage
 Helpers for optional storage of evmc_result.
 

Macros

#define EVMC_EXPORT   __attribute__((visibility("default")))
 Marks a function to be exported from a shared library.
 
#define EVMC_NOEXCEPT
 Safe way of marking a function with noexcept C++ specifier.
 

Functions

static bool evmc_is_abi_compatible (struct evmc_vm *vm)
 Returns true if the VM has a compatible ABI version.
 
static const char * evmc_vm_name (struct evmc_vm *vm)
 Returns the name of the VM.
 
static const char * evmc_vm_version (struct evmc_vm *vm)
 Returns the version of the VM.
 
static bool evmc_vm_has_capability (struct evmc_vm *vm, enum evmc_capabilities capability)
 Checks if the VM has the given capability. More...
 
static void evmc_destroy (struct evmc_vm *vm)
 Destroys the VM instance. More...
 
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. More...
 
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. More...
 
static void evmc_free_result_memory (const struct evmc_result *result)
 The evmc_result release function using free() for releasing the memory. More...
 
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. More...
 
static void evmc_release_result (struct evmc_result *result)
 Releases the resources allocated to the execution result. More...
 

Detailed Description

EVMC Helpers.

A collection of C helper functions for invoking a VM instance methods. These are convenient for languages where invoking function pointers is "ugly" or impossible (such as Go).

It also contains helpers (overloaded operators) for using EVMC types effectively in C++.

Function Documentation

◆ evmc_destroy()

static void evmc_destroy ( struct evmc_vm vm)
inlinestatic

Destroys the VM instance.

See also
evmc_destroy_fn

Definition at line 63 of file helpers.h.

64 {
65  vm->destroy(vm);
66 }
evmc_destroy_fn destroy
Pointer to function destroying the VM instance.
Definition: evmc.h:886

◆ evmc_execute()

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 
)
static

Executes code in the VM instance.

See also
evmc_execute_fn.

Definition at line 87 of file helpers.h.

94 {
95  return vm->execute(vm, host, context, rev, msg, code, code_size);
96 }
evmc_execute_fn execute
Pointer to function executing a code by the VM instance.
Definition: evmc.h:893

◆ evmc_free_result_memory()

static void evmc_free_result_memory ( const struct evmc_result result)
static

The evmc_result release function using free() for releasing the memory.

This function is used in the evmc_make_result(), but may be also used in other case if convenient.

Parameters
resultThe result object.

Definition at line 104 of file helpers.h.

105 {
106  free((uint8_t*)result->output_data);
107 }
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:364

◆ evmc_make_result()

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 
)
static

Creates the result from the provided arguments.

The provided output is copied to memory allocated with malloc() and the evmc_result::release function is set to one invoking free().

In case of memory allocation failure, the result has all fields zeroed and only evmc_result::status_code is set to EVMC_OUT_OF_MEMORY internal error.

Parameters
status_codeThe status code.
gas_leftThe amount of gas left.
output_dataThe pointer to the output.
output_sizeThe output size.

Definition at line 121 of file helpers.h.

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 }
The VM failed to allocate the amount of memory needed for execution.
Definition: evmc.h:315
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:343
size_t output_size
The size of the output data.
Definition: evmc.h:371
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:351
The EVM code execution result.
Definition: evmc.h:340
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:364
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

◆ evmc_release_result()

static void evmc_release_result ( struct evmc_result result)
inlinestatic

Releases the resources allocated to the execution result.

Parameters
resultThe result object to be released. MUST NOT be NULL.
See also
evmc_result::release() evmc_release_result_fn

Definition at line 157 of file helpers.h.

158 {
159  if (result->release)
160  result->release(result);
161 }
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:392

◆ evmc_set_option()

static enum evmc_set_option_result evmc_set_option ( struct evmc_vm vm,
char const *  name,
char const *  value 
)
inlinestatic

Sets the option for the VM, if the feature is supported by the VM.

See also
evmc_set_option_fn

Definition at line 73 of file helpers.h.

76 {
77  if (vm->set_option)
78  return vm->set_option(vm, name, value);
79  return EVMC_SET_OPTION_INVALID_NAME;
80 }
evmc_set_option_fn set_option
Optional pointer to function modifying VM's options.
Definition: evmc.h:912

◆ evmc_vm_has_capability()

static bool evmc_vm_has_capability ( struct evmc_vm vm,
enum evmc_capabilities  capability 
)
inlinestatic

Checks if the VM has the given capability.

See also
evmc_get_capabilities_fn

Definition at line 53 of file helpers.h.

54 {
55  return (vm->get_capabilities(vm) & (evmc_capabilities_flagset)capability) != 0;
56 }
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:836
evmc_get_capabilities_fn get_capabilities
A method returning capabilities supported by the VM instance.
Definition: evmc.h:905