Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
memory.cpp
Go to the documentation of this file.
4
5#include <fc/scoped_exit.hpp>
6
7#include <unistd.h>
8#include <sys/syscall.h>
9#include <sys/mman.h>
10#include <linux/memfd.h>
11
12namespace sysio { namespace chain { namespace eosvmoc {
13
15 uint64_t number_slices = max_pages + 1;
16 uint64_t wasm_memory_size = max_pages * wasm_constraints::wasm_page_size;
17 int fd = syscall(SYS_memfd_create, "eosvmoc_mem", MFD_CLOEXEC);
18 FC_ASSERT(fd >= 0, "Failed to create memory memfd");
19 auto cleanup_fd = fc::make_scoped_exit([&fd](){close(fd);});
20 int ret = ftruncate(fd, wasm_memory_size+memory_prologue_size);
21 FC_ASSERT(!ret, "Failed to grow memory memfd");
22
23 mapsize = total_memory_per_slice*number_slices;
24 mapbase = (uint8_t*)mmap(nullptr, mapsize, PROT_NONE, MAP_PRIVATE|MAP_ANON, 0, 0);
25 FC_ASSERT(mapbase != MAP_FAILED, "Failed to mmap memory");
26
27 uint8_t* next_slice = mapbase;
28 uint8_t* last;
29
30 for(unsigned int p = 0; p < number_slices; ++p) {
31 last = (uint8_t*)mmap(next_slice, memory_prologue_size+64u*1024u*p, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0);
32 FC_ASSERT(last != MAP_FAILED, "Failed to mmap memory");
33 next_slice += total_memory_per_slice;
34 }
35
36 zeropage_base = mapbase + memory_prologue_size;
37 fullpage_base = last + memory_prologue_size;
38
39 //layout the intrinsic jump table
40 uintptr_t* const intrinsic_jump_table = reinterpret_cast<uintptr_t* const>(zeropage_base - first_intrinsic_offset);
41 const intrinsic_map_t& intrinsics = get_intrinsic_map();
42 for(const auto& intrinsic : intrinsics)
43 intrinsic_jump_table[-intrinsic.second.ordinal] = (uintptr_t)intrinsic.second.function_ptr;
44}
45
46void memory::reset(uint64_t max_pages) {
47 uint64_t old_max_pages = mapsize / memory::total_memory_per_slice - 1;
48 if(max_pages == old_max_pages) return;
49 memory new_memory{max_pages};
50 std::swap(mapbase, new_memory.mapbase);
51 std::swap(mapsize, new_memory.mapsize);
52 std::swap(zeropage_base, new_memory.zeropage_base);
53 std::swap(fullpage_base, new_memory.fullpage_base);
54}
55
57 munmap(mapbase, mapsize);
58}
59
60}}}
const mie::Vuint & p
Definition bn.cpp:27
memory(uint64_t max_pages)
Definition memory.cpp:14
static constexpr uintptr_t first_intrinsic_offset
Definition memory.hpp:49
void reset(uint64_t max_pages)
Definition memory.cpp:46
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
void close(T *e, websocketpp::connection_hdl hdl)
scoped_exit< Callback > make_scoped_exit(Callback &&c)
void swap(picojson::value &x, picojson::value &y)
const intrinsic_map_t & get_intrinsic_map()
Definition intrinsic.cpp:10
std::map< std::string, intrinsic_entry > intrinsic_map_t
Definition intrinsic.hpp:24
_W64 unsigned int uintptr_t
Definition stdint.h:165
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
CK_RV ret