mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
WebAssembly.cpp
Go to the documentation of this file.
1#include "backend/WebAssembly.hpp"
2
4#include <binaryen-c.h>
5#include <iostream>
6#include <sys/mman.h>
7#include <utility>
8
9
10using namespace m;
11
12
13/*======================================================================================================================
14 * WasmModule
15 *====================================================================================================================*/
16
17WasmModule::WasmModule() : ref_(BinaryenModuleCreate()) { }
18
19WasmModule::~WasmModule() { BinaryenModuleDispose(ref_); }
20
21std::pair<uint8_t*, std::size_t> WasmModule::binary() const
22{
23 auto result = BinaryenModuleAllocateAndWrite(ref_, nullptr);
24 return std::make_pair(reinterpret_cast<uint8_t*>(result.binary), result.binaryBytes);
25}
26
28std::ostream & m::operator<<(std::ostream &out, const WasmModule &module)
29{
30 auto result = BinaryenModuleAllocateAndWriteText(module.ref_);
31 out << result;
32 free(result);
33 return out;
34}
35
36void WasmModule::dump(std::ostream &out) const {
37 out << *this;
38 auto [buffer, length] = binary();
39 out << '[' << std::hex;
40 for (auto ptr = buffer, end = buffer + length; ptr != end; ++ptr) {
41 if (ptr != buffer) out << ", ";
42 out << "0x" << uint32_t(*ptr);
43 }
44 out << std::dec;
45 out << ']' << std::endl;
46 free(buffer);
47}
48void WasmModule::dump() const { dump(std::cerr); }
50
51
52/*======================================================================================================================
53 * WasmEngine
54 *====================================================================================================================*/
55
56WasmEngine::WasmContext::WasmContext(uint32_t id, const MatchBase &plan, config_t config, std::size_t size)
57 : config_(config)
58 , id(id)
59 , plan(plan)
60 , vm(size)
61{
62 install_guard_page(); // map nullptr page
63
65}
66
68{
70
71 const auto num_rows_per_instance = table.layout().child().num_tuples();
72 const auto instance_stride_in_bytes = table.layout().stride_in_bits() / 8U;
73 const std::size_t num_instances = (table.store().num_rows() + num_rows_per_instance - 1) / num_rows_per_instance;
74 const std::size_t bytes = instance_stride_in_bytes * num_instances;
75
76 /* Map entry into WebAssembly linear memory. */
77 const auto off = heap;
78 const auto aligned_bytes = Ceil_To_Next_Page(bytes);
79 const auto &mem = table.store().memory();
80 if (aligned_bytes) {
81 mem.map(aligned_bytes, 0, vm, off);
82 heap += aligned_bytes;
83 install_guard_page();
84 }
86
87 return off;
88}
89
91{
93 if (not config(TRAP_GUARD_PAGES)) {
94 /* Map the guard page to a fresh, zeroed page. */
95 M_DISCARD mmap(vm.as<uint8_t*>() + heap, get_pagesize(), PROT_READ, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
96 }
97 heap += get_pagesize(); // install guard page
99}
100
101
102/*======================================================================================================================
103 * WasmBackend
104 *====================================================================================================================*/
105
107void WasmBackend::execute(const MatchBase &plan) const { engine_->execute(plan); }
#define id(X)
#define M_DISCARD
Definition: macro.hpp:213
#define M_insist(...)
Definition: macro.hpp:129
‍mutable namespace
Definition: Backend.hpp:10
std::size_t Is_Page_Aligned(std::size_t n)
Returns true iff n is a integral multiple of the page size (in bytes).
Definition: fn.hpp:700
std::size_t M_EXPORT get_pagesize()
Returns the page size of the system.
Definition: fn.cpp:142
M_LCOV_EXCL_START std::ostream & operator<<(std::ostream &out, const PlanTableBase< Actual > &PT)
Definition: PlanTable.hpp:401
std::size_t Ceil_To_Next_Page(std::size_t n)
Returns the smallest integral multiple of the page size (in bytes) greater than or equals to n.
Definition: fn.hpp:703
void register_wasm_operators(PhysicalOptimizer &phys_opt)
Registers physical Wasm operators in phys_opt depending on the set CLI options.
The physical optimizer interface.
virtual const memory::Memory & memory() const =0
Returns the memory corresponding to the Linearization's root node.
virtual std::size_t num_rows() const =0
Return the number of rows in this store.
A table is a sorted set of attributes.
Definition: Schema.hpp:388
virtual const storage::DataLayout & layout() const =0
Returns a reference to the physical data layout.
virtual Store & store() const =0
Returns a reference to the backing store.
void register_operators(PhysicalOptimizer &phys_opt) const override
Registers all physical operators of this Backend in phys_opt.
void execute(const MatchBase &plan) const override
Executes the already computed physical covering represented by plan using this Backend.
void install_guard_page()
Installs a guard page at the current heap and increments heap to the next page.
Definition: WebAssembly.cpp:90
uint32_t map_table(const Table &table)
Maps a table at the current start of heap and advances heap past the mapped region.
Definition: WebAssembly.cpp:67
WasmContext(uint32_t id, const MatchBase &plan, config_t configuration, std::size_t size)
Definition: WebAssembly.cpp:56
static constexpr std::size_t WASM_MAX_MEMORY
The maximum memory of a WebAssembly module: 2^32 - 2^16 bytes ≈ 4 GiB.
Definition: WebAssembly.hpp:21
A WasmModule is a wrapper around a [Binaryen] (https://github.com/WebAssembly/binaryen) wasm::Module.
Definition: WebAssembly.hpp:18
void dump() const
Definition: WebAssembly.cpp:48
std::pair< uint8_t *, std::size_t > binary() const
Returns the binary representation of this module in a freshly allocated memory.
Definition: WebAssembly.cpp:21
::wasm::Module * ref_
the underlying [Binaryen] (https://github.com/WebAssembly/binaryen) WASM module
Definition: WebAssembly.hpp:20
void map(std::size_t size, std::size_t offset_src, const AddressSpace &vm, std::size_t offset_dst) const
Map size bytes starting at offset_src into the address space of vm at offset offset_dst.
Definition: memory.cpp:85
virtual size_type num_tuples() const =0
‍returns the number of tuples represented by an instance of this node
uint64_t stride_in_bits() const
‍return the stride (in bits) of the single child of the DataLayout
Definition: DataLayout.hpp:207
const Node & child() const
‍returns a reference to the single child of this DataLayout
Definition: DataLayout.hpp:209