Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
ipc_helpers.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <boost/asio/local/datagram_protocol.hpp>
6
7#include <vector>
8
9#include <sys/syscall.h>
10#include <linux/memfd.h>
11
12namespace sysio { namespace chain { namespace eosvmoc {
13
15 public:
16 wrapped_fd() : _inuse(false) {}
17 wrapped_fd(int fd) : _inuse(true), _fd(fd) {}
18 wrapped_fd(const wrapped_fd&) = delete;
19 wrapped_fd& operator=(const wrapped_fd&) = delete;
20 wrapped_fd(wrapped_fd&& other) : _inuse(other._inuse), _fd(other._fd) {other._inuse = false;}
22 if(_inuse)
23 close(_fd);
24 _inuse = other._inuse;
25 _fd = other._fd;
26 other._inuse = false;
27 return *this;
28 }
29
30 operator int() const {
31 FC_ASSERT(_inuse, "trying to get the value of a not-in-use wrappedfd");
32 return _fd;
33 }
34
35 int release() {
36 _inuse = false;
37 return _fd;
38 }
39
41 if(_inuse)
42 close(_fd);
43 }
44
45 private:
46 bool _inuse = false;;
47 int _fd;
48};
49
50std::tuple<bool, eosvmoc_message, std::vector<wrapped_fd>> read_message_with_fds(boost::asio::local::datagram_protocol::socket& s);
51std::tuple<bool, eosvmoc_message, std::vector<wrapped_fd>> read_message_with_fds(int fd);
52bool write_message_with_fds(boost::asio::local::datagram_protocol::socket& s, const eosvmoc_message& message, const std::vector<wrapped_fd>& fds = std::vector<wrapped_fd>());
53bool write_message_with_fds(int fd_to_send_to, const eosvmoc_message& message, const std::vector<wrapped_fd>& fds = std::vector<wrapped_fd>());
54
55template<typename T>
57 int fd = syscall(SYS_memfd_create, "eosvmoc_code", MFD_CLOEXEC);
58 FC_ASSERT(fd >= 0, "Failed to create memfd");
59 FC_ASSERT(ftruncate(fd, bytes.size()) == 0, "failed to grow memfd");
60 if(bytes.size()) {
61 uint8_t* b = (uint8_t*)mmap(nullptr, bytes.size(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
62 FC_ASSERT(b != MAP_FAILED, "failed to mmap memfd");
63 memcpy(b, bytes.data(), bytes.size());
64 munmap(b, bytes.size());
65 }
66 return wrapped_fd(fd);
67}
68
69std::vector<uint8_t> vector_for_memfd(const wrapped_fd& memfd);
70}}}
wrapped_fd & operator=(wrapped_fd &&other)
wrapped_fd & operator=(const wrapped_fd &)=delete
wrapped_fd(const wrapped_fd &)=delete
#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)
std::vector< uint8_t > vector_for_memfd(const wrapped_fd &memfd)
bool write_message_with_fds(boost::asio::local::datagram_protocol::socket &s, const eosvmoc_message &message, const std::vector< wrapped_fd > &fds=std::vector< wrapped_fd >())
std::tuple< bool, eosvmoc_message, std::vector< wrapped_fd > > read_message_with_fds(boost::asio::local::datagram_protocol::socket &s)
std::variant< initialize_message, initalize_response_message, compile_wasm_message, evict_wasms_message, code_compilation_result_message, wasm_compilation_result_message > eosvmoc_message
wrapped_fd memfd_for_bytearray(const T &bytes)
#define T(meth, val, expected)
unsigned char uint8_t
Definition stdint.h:124
char * s
memcpy((char *) pInfo->slotDescription, s, l)