2018-03-28 13:40:18 +00:00
|
|
|
#include <evmc.h>
|
2017-09-22 17:27:30 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2016-08-22 17:22:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
struct examplevm
|
|
|
|
{
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_instance instance;
|
2017-09-22 17:27:30 +00:00
|
|
|
int verbose;
|
2016-08-22 17:22:34 +00:00
|
|
|
};
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void evmc_destroy(struct evmc_instance* evm)
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
|
|
|
free(evm);
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:24:22 +00:00
|
|
|
/// Example options.
|
|
|
|
///
|
|
|
|
/// VMs are allowed to omit this function implementation.
|
2018-04-13 06:39:47 +00:00
|
|
|
int evmc_set_option(struct evmc_instance* instance, char const* name, char const* value)
|
2016-08-23 19:24:22 +00:00
|
|
|
{
|
2017-04-25 18:07:50 +00:00
|
|
|
struct examplevm* vm = (struct examplevm*)instance;
|
2018-04-13 06:39:47 +00:00
|
|
|
if (strcmp(name, "verbose") == 0)
|
|
|
|
{
|
2017-04-25 18:07:50 +00:00
|
|
|
long int v = strtol(value, NULL, 0);
|
|
|
|
if (v > INT_MAX || v < INT_MIN)
|
|
|
|
return 0;
|
2017-09-22 17:27:30 +00:00
|
|
|
vm->verbose = (int)v;
|
2016-08-23 19:24:22 +00:00
|
|
|
return 1;
|
2017-04-25 18:07:50 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 19:24:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void evmc_release_result(struct evmc_result const* result)
|
2016-09-27 15:14:19 +00:00
|
|
|
{
|
2017-04-28 13:26:44 +00:00
|
|
|
(void)result;
|
2016-09-27 15:14:19 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void free_result_output_data(struct evmc_result const* result)
|
2017-04-24 15:32:28 +00:00
|
|
|
{
|
|
|
|
free((uint8_t*)result->output_data);
|
|
|
|
}
|
|
|
|
|
2018-04-11 12:56:14 +00:00
|
|
|
static struct evmc_result execute(struct evmc_instance* instance, struct evmc_context* context,
|
|
|
|
enum evmc_revision rev, const struct evmc_message* msg, const uint8_t* code, size_t code_size)
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
2018-04-13 06:39:47 +00:00
|
|
|
struct evmc_result ret = {.status_code = EVMC_INTERNAL_ERROR};
|
2018-04-11 12:56:14 +00:00
|
|
|
if (code_size == 0)
|
|
|
|
{
|
2017-04-24 15:32:28 +00:00
|
|
|
// In case of empty code return a fancy error message.
|
2018-04-13 06:39:47 +00:00
|
|
|
const char* error = rev == EVMC_BYZANTIUM ? "Welcome to Byzantium!" : "Hello Ethereum!";
|
2017-01-27 15:27:34 +00:00
|
|
|
ret.output_data = (const uint8_t*)error;
|
|
|
|
ret.output_size = strlen(error);
|
2018-03-28 13:09:07 +00:00
|
|
|
ret.status_code = EVMC_FAILURE;
|
2017-04-24 15:32:28 +00:00
|
|
|
ret.release = NULL; // We don't need to release the constant messages.
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct examplevm* vm = (struct examplevm*)instance;
|
|
|
|
|
2017-04-25 15:01:46 +00:00
|
|
|
// Simulate executing by checking for some code patterns.
|
|
|
|
// Solidity inline assembly is used in the examples instead of EVM bytecode.
|
2016-08-22 17:22:34 +00:00
|
|
|
|
2017-04-25 15:01:46 +00:00
|
|
|
// Assembly: `{ mstore(0, address()) return(0, msize()) }`.
|
2017-04-25 18:07:50 +00:00
|
|
|
const char return_address[] = "30600052596000f3";
|
|
|
|
|
|
|
|
// Assembly: `{ sstore(0, add(sload(0), 1)) }`
|
|
|
|
const char counter[] = "600160005401600055";
|
|
|
|
|
|
|
|
if (code_size == strlen(return_address) &&
|
2018-04-13 06:39:47 +00:00
|
|
|
strncmp((const char*)code, return_address, code_size) == 0)
|
|
|
|
{
|
2018-01-23 10:00:55 +00:00
|
|
|
static const size_t address_size = sizeof(msg->destination);
|
2017-04-24 15:32:28 +00:00
|
|
|
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
2018-04-13 06:39:47 +00:00
|
|
|
if (!output_data)
|
|
|
|
{
|
2017-04-25 15:01:46 +00:00
|
|
|
// malloc failed, report internal error.
|
2018-03-28 13:09:07 +00:00
|
|
|
ret.status_code = EVMC_INTERNAL_ERROR;
|
2017-04-25 15:01:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2018-01-23 10:00:55 +00:00
|
|
|
memcpy(output_data, &msg->destination, address_size);
|
2018-03-28 13:09:07 +00:00
|
|
|
ret.status_code = EVMC_SUCCESS;
|
2017-04-24 15:32:28 +00:00
|
|
|
ret.output_data = output_data;
|
|
|
|
ret.output_size = address_size;
|
|
|
|
ret.release = &free_result_output_data;
|
|
|
|
return ret;
|
|
|
|
}
|
2018-04-13 06:39:47 +00:00
|
|
|
else if (code_size == strlen(counter) && strncmp((const char*)code, counter, code_size) == 0)
|
|
|
|
{
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_uint256be value;
|
2018-04-13 06:39:47 +00:00
|
|
|
const struct evmc_uint256be index = {{0}};
|
2018-01-23 10:00:55 +00:00
|
|
|
context->fn_table->get_storage(&value, context, &msg->destination, &index);
|
2017-06-09 14:16:17 +00:00
|
|
|
value.bytes[31] += 1;
|
2018-01-23 10:00:55 +00:00
|
|
|
context->fn_table->set_storage(context, &msg->destination, &index, &value);
|
2018-03-28 13:09:07 +00:00
|
|
|
ret.status_code = EVMC_SUCCESS;
|
2017-04-25 18:07:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-08-22 17:22:34 +00:00
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
ret.release = evmc_release_result;
|
|
|
|
ret.status_code = EVMC_FAILURE;
|
2016-08-22 17:22:34 +00:00
|
|
|
ret.gas_left = 0;
|
|
|
|
|
2017-09-22 17:27:30 +00:00
|
|
|
if (vm->verbose)
|
|
|
|
printf("Execution done.\n");
|
|
|
|
|
2016-08-22 17:22:34 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_instance* examplevm_create()
|
2016-12-20 15:50:23 +00:00
|
|
|
{
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_instance init = {
|
|
|
|
.abi_version = EVMC_ABI_VERSION,
|
2018-04-17 09:11:07 +00:00
|
|
|
.name = "examplevm",
|
|
|
|
.version = "0.0.0",
|
2018-03-28 13:09:07 +00:00
|
|
|
.destroy = evmc_destroy,
|
2017-09-22 16:50:20 +00:00
|
|
|
.execute = execute,
|
2018-04-13 06:39:47 +00:00
|
|
|
.set_option = evmc_set_option,
|
2017-09-22 16:50:20 +00:00
|
|
|
};
|
2016-12-20 15:50:23 +00:00
|
|
|
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_instance* interface = &vm->instance;
|
2017-09-22 16:50:20 +00:00
|
|
|
memcpy(interface, &init, sizeof(init));
|
2016-12-20 15:50:23 +00:00
|
|
|
return interface;
|
|
|
|
}
|